@@ -47,7 +47,7 @@ var conf = Config{
47
47
PullInterval : time .Second ,
48
48
RequestStateInfoInterval : time .Millisecond * 100 ,
49
49
BlockExpirationInterval : time .Second * 6 ,
50
- StateInfoExpirationInterval : time .Second * 6 ,
50
+ StateInfoCacheSweepInterval : time .Second ,
51
51
}
52
52
53
53
func init () {
@@ -196,6 +196,10 @@ func (ga *gossipAdapterMock) GetMembership() []discovery.NetworkMember {
196
196
197
197
// Lookup returns a network member, or nil if not found
198
198
func (ga * gossipAdapterMock ) Lookup (PKIID common.PKIidType ) * discovery.NetworkMember {
199
+ // Ensure we have configured Lookup prior
200
+ if ! ga .wasMocked ("Lookup" ) {
201
+ return & discovery.NetworkMember {}
202
+ }
199
203
args := ga .Called (PKIID )
200
204
if args .Get (0 ) == nil {
201
205
return nil
@@ -205,14 +209,7 @@ func (ga *gossipAdapterMock) Lookup(PKIID common.PKIidType) *discovery.NetworkMe
205
209
206
210
func (ga * gossipAdapterMock ) Send (msg * proto.SignedGossipMessage , peers ... * comm.RemotePeer ) {
207
211
// Ensure we have configured Send prior
208
- foundSend := false
209
- for _ , ec := range ga .ExpectedCalls {
210
- if ec .Method == "Send" {
211
- foundSend = true
212
- }
213
-
214
- }
215
- if ! foundSend {
212
+ if ! ga .wasMocked ("Send" ) {
216
213
return
217
214
}
218
215
ga .Called (msg , peers )
@@ -238,6 +235,18 @@ func (ga *gossipAdapterMock) GetIdentityByPKIID(pkiID common.PKIidType) api.Peer
238
235
return api .PeerIdentityType (pkiID )
239
236
}
240
237
238
+ func (ga * gossipAdapterMock ) wasMocked (methodName string ) bool {
239
+ // The following On call is just to synchronize the ExpectedCalls
240
+ // access with 'On' calls from the test goroutine
241
+ ga .On ("bla" , mock .Anything )
242
+ for _ , ec := range ga .ExpectedCalls {
243
+ if ec .Method == methodName {
244
+ return true
245
+ }
246
+ }
247
+ return false
248
+ }
249
+
241
250
func configureAdapter (adapter * gossipAdapterMock , members ... discovery.NetworkMember ) {
242
251
adapter .On ("GetConf" ).Return (conf )
243
252
adapter .On ("GetMembership" ).Return (members )
@@ -265,6 +274,98 @@ func TestBadInput(t *testing.T) {
265
274
gc .HandleMessage (& receivedMsg {msg : createDataUpdateMsg (0 ), PKIID : pkiIDnilOrg })
266
275
}
267
276
277
+ func TestMsgStoreNotExpire (t * testing.T ) {
278
+ t .Parallel ()
279
+
280
+ cs := & cryptoService {}
281
+
282
+ pkiID1 := common .PKIidType ("1" )
283
+ pkiID2 := common .PKIidType ("2" )
284
+ pkiID3 := common .PKIidType ("3" )
285
+
286
+ peer1 := discovery.NetworkMember {PKIid : pkiID2 , InternalEndpoint : "1" , Endpoint : "1" }
287
+ peer2 := discovery.NetworkMember {PKIid : pkiID2 , InternalEndpoint : "2" , Endpoint : "2" }
288
+ peer3 := discovery.NetworkMember {PKIid : pkiID3 , InternalEndpoint : "3" , Endpoint : "3" }
289
+
290
+ jcm := & joinChanMsg {
291
+ members2AnchorPeers : map [string ][]api.AnchorPeer {
292
+ string (orgInChannelA ): {},
293
+ },
294
+ }
295
+
296
+ adapter := new (gossipAdapterMock )
297
+ adapter .On ("GetOrgOfPeer" , pkiID1 ).Return (orgInChannelA )
298
+ adapter .On ("GetOrgOfPeer" , pkiID2 ).Return (orgInChannelA )
299
+ adapter .On ("GetOrgOfPeer" , pkiID3 ).Return (orgInChannelA )
300
+
301
+ adapter .On ("ValidateStateInfoMessage" , mock .Anything ).Return (nil )
302
+ adapter .On ("GetMembership" ).Return ([]discovery.NetworkMember {peer2 , peer3 })
303
+ adapter .On ("DeMultiplex" , mock .Anything )
304
+ adapter .On ("Gossip" , mock .Anything )
305
+ adapter .On ("GetConf" ).Return (conf )
306
+
307
+ gc := NewGossipChannel (pkiID1 , orgInChannelA , cs , channelA , adapter , jcm )
308
+ gc .UpdateStateInfo (createStateInfoMsg (1 , pkiID1 , channelA ))
309
+ // Receive StateInfo messages from other peers
310
+ gc .HandleMessage (& receivedMsg {PKIID : pkiID2 , msg : createStateInfoMsg (1 , pkiID2 , channelA )})
311
+ gc .HandleMessage (& receivedMsg {PKIID : pkiID3 , msg : createStateInfoMsg (1 , pkiID3 , channelA )})
312
+
313
+ simulateStateInfoRequest := func (pkiID []byte , outChan chan * proto.SignedGossipMessage ) {
314
+ sentMessages := make (chan * proto.GossipMessage , 1 )
315
+ // Ensure we respond to stateInfoSnapshot requests with valid MAC
316
+ snapshotReq := & receivedMsg {
317
+ PKIID : pkiID ,
318
+ msg : (& proto.GossipMessage {
319
+ Tag : proto .GossipMessage_CHAN_OR_ORG ,
320
+ Content : & proto.GossipMessage_StateInfoPullReq {
321
+ StateInfoPullReq : & proto.StateInfoPullRequest {
322
+ Channel_MAC : GenerateMAC (pkiID , channelA ),
323
+ },
324
+ },
325
+ }).NoopSign (),
326
+ }
327
+ snapshotReq .On ("Respond" , mock .Anything ).Run (func (args mock.Arguments ) {
328
+ sentMessages <- args .Get (0 ).(* proto.GossipMessage )
329
+ })
330
+
331
+ go gc .HandleMessage (snapshotReq )
332
+ select {
333
+ case <- time .After (time .Second ):
334
+ t .Fatal ("Haven't received a state info snapshot on time" )
335
+ case msg := <- sentMessages :
336
+ for _ , el := range msg .GetStateSnapshot ().Elements {
337
+ sMsg , err := el .ToGossipMessage ()
338
+ assert .NoError (t , err )
339
+ outChan <- sMsg
340
+ }
341
+ }
342
+ }
343
+
344
+ c := make (chan * proto.SignedGossipMessage , 3 )
345
+ simulateStateInfoRequest (pkiID2 , c )
346
+ assert .Len (t , c , 3 )
347
+
348
+ c = make (chan * proto.SignedGossipMessage , 3 )
349
+ simulateStateInfoRequest (pkiID3 , c )
350
+ assert .Len (t , c , 3 )
351
+
352
+ // Now simulate an expiration of peer 3 in the membership view
353
+ adapter .On ("Lookup" , pkiID1 ).Return (& peer1 )
354
+ adapter .On ("Lookup" , pkiID2 ).Return (& peer2 )
355
+ adapter .On ("Lookup" , pkiID3 ).Return (nil )
356
+ // Ensure that we got at least 1 sweep before continuing
357
+ // the test
358
+ time .Sleep (conf .StateInfoCacheSweepInterval * 2 )
359
+
360
+ c = make (chan * proto.SignedGossipMessage , 3 )
361
+ simulateStateInfoRequest (pkiID2 , c )
362
+ assert .Len (t , c , 2 )
363
+
364
+ c = make (chan * proto.SignedGossipMessage , 3 )
365
+ simulateStateInfoRequest (pkiID3 , c )
366
+ assert .Len (t , c , 2 )
367
+ }
368
+
268
369
func TestChannelPeriodicalPublishStateInfo (t * testing.T ) {
269
370
t .Parallel ()
270
371
ledgerHeight := 5
@@ -290,6 +391,7 @@ func TestChannelPeriodicalPublishStateInfo(t *testing.T) {
290
391
gc := NewGossipChannel (pkiIDInOrg1 , orgInChannelA , cs , channelA , adapter , & joinChanMsg {})
291
392
stateInfoMsg := createStateInfoMsg (ledgerHeight , pkiIDInOrg1 , channelA )
292
393
gc .UpdateStateInfo (stateInfoMsg )
394
+ defer gc .Stop ()
293
395
294
396
var msg * proto.SignedGossipMessage
295
397
select {
@@ -303,14 +405,6 @@ func TestChannelPeriodicalPublishStateInfo(t *testing.T) {
303
405
height , err := strconv .ParseInt (string (md ), 10 , 64 )
304
406
assert .NoError (t , err , "ReceivedMetadata is invalid" )
305
407
assert .Equal (t , ledgerHeight , int (height ), "Received different ledger height than expected" )
306
-
307
- // We will not update StateInfo in store, so store will become empty
308
- time .Sleep (conf .StateInfoExpirationInterval + time .Second )
309
- //Store is empty
310
- assert .Equal (t , 0 , gc .(* gossipChannel ).stateInfoMsgStore .MessageStore .Size (), "StateInfo MessageStore should be empty" )
311
- assert .Equal (t , 0 , gc .(* gossipChannel ).stateInfoMsgStore .MembershipStore .Size (), "StateInfo MembershipStore should be empty" )
312
-
313
- gc .Stop ()
314
408
}
315
409
316
410
func TestChannelMsgStoreEviction (t * testing.T ) {
@@ -733,7 +827,7 @@ func TestChannelAddToMessageStore(t *testing.T) {
733
827
assert .True (t , gc .EligibleForChannel (discovery.NetworkMember {PKIid : pkiIDInOrg1 }))
734
828
}
735
829
736
- func TestChannelAddToMessageStoreExpire (t * testing.T ) {
830
+ func TestChannelBlockExpiration (t * testing.T ) {
737
831
t .Parallel ()
738
832
739
833
cs := & cryptoService {}
@@ -747,7 +841,6 @@ func TestChannelAddToMessageStoreExpire(t *testing.T) {
747
841
adapter .On ("DeMultiplex" , mock .Anything ).Run (func (arg mock.Arguments ) {
748
842
demuxedMsgs <- arg .Get (0 ).(* proto.SignedGossipMessage )
749
843
})
750
-
751
844
respondedChan := make (chan * proto.GossipMessage , 1 )
752
845
messageRelayer := func (arg mock.Arguments ) {
753
846
msg := arg .Get (0 ).(* proto.GossipMessage )
@@ -1061,52 +1154,18 @@ func TestChannelStateInfoSnapshot(t *testing.T) {
1061
1154
// Ensure we don't crash if we got a stateInfoMessage from a peer that its org isn't known
1062
1155
invalidStateInfoSnapshot = stateInfoSnapshotForChannel (channelA , createStateInfoMsg (4 , common .PKIidType ("unknown" ), channelA ))
1063
1156
gc .HandleMessage (& receivedMsg {PKIID : pkiIDInOrg1 , msg : invalidStateInfoSnapshot })
1064
- // Lets expire msg in store
1065
- time .Sleep (gc .(* gossipChannel ).GetConf ().StateInfoExpirationInterval + time .Second )
1066
-
1067
- // Lets check is state info store can't add expired msg but appear as empty to outside world
1068
- gc .HandleMessage (stateInfoMsg )
1069
- assert .Empty (t , gc .GetPeers ())
1070
- // Lets see if snapshot now empty, after message in store expired
1071
- go gc .HandleMessage (snapshotReq )
1072
- select {
1073
- case <- time .After (time .Second ):
1074
- t .Fatal ("Haven't received a state info snapshot on time" )
1075
- case msg := <- sentMessages :
1076
- elements := msg .GetStateSnapshot ().Elements
1077
- assert .Len (t , elements , 0 , "StateInfo snapshot should contain zero messages" )
1078
- }
1079
-
1080
- // Lets make sure msg removed from store
1081
- time .Sleep (gc .(* gossipChannel ).GetConf ().StateInfoExpirationInterval + time .Second )
1082
-
1083
- // Lets check is state info store add just expired msg
1084
- gc .HandleMessage (stateInfoMsg )
1085
- assert .NotEmpty (t , gc .GetPeers ())
1086
- // Lets see if snapshot is not empty now, after message was added back to store
1087
- go gc .HandleMessage (snapshotReq )
1088
- select {
1089
- case <- time .After (time .Second ):
1090
- t .Fatal ("Haven't received a state info snapshot on time" )
1091
- case msg := <- sentMessages :
1092
- elements := msg .GetStateSnapshot ().Elements
1093
- assert .Len (t , elements , 1 )
1094
- sMsg , err := elements [0 ].ToGossipMessage ()
1095
- assert .NoError (t , err )
1096
- assert .Equal (t , []byte ("4" ), sMsg .GetStateInfo ().Metadata )
1097
- }
1098
1157
}
1099
1158
1100
1159
func TestInterOrgExternalEndpointDisclosure (t * testing.T ) {
1101
1160
t .Parallel ()
1102
-
1103
1161
cs := & cryptoService {}
1104
1162
adapter := new (gossipAdapterMock )
1105
1163
pkiID1 := common .PKIidType ("withExternalEndpoint" )
1106
1164
pkiID2 := common .PKIidType ("noExternalEndpoint" )
1107
1165
pkiID3 := common .PKIidType ("pkiIDinOrg2" )
1108
1166
adapter .On ("Lookup" , pkiID1 ).Return (& discovery.NetworkMember {Endpoint : "localhost:5000" })
1109
1167
adapter .On ("Lookup" , pkiID2 ).Return (& discovery.NetworkMember {})
1168
+ adapter .On ("Lookup" , pkiID3 ).Return (& discovery.NetworkMember {})
1110
1169
adapter .On ("GetOrgOfPeer" , pkiID1 ).Return (orgInChannelA )
1111
1170
adapter .On ("GetOrgOfPeer" , pkiID2 ).Return (orgInChannelA )
1112
1171
adapter .On ("GetOrgOfPeer" , pkiID3 ).Return (api .OrgIdentityType ("ORG2" ))
0 commit comments