@@ -40,11 +40,13 @@ type msgMutator func(message *proto.Envelope)
40
40
41
41
var conf = Config {
42
42
ID : "test" ,
43
- PublishStateInfoInterval : time .Millisecond * 100 ,
44
- MaxBlockCountToStore : 100 ,
45
- PullPeerNum : 3 ,
46
- PullInterval : time .Second ,
47
- RequestStateInfoInterval : time .Millisecond * 100 ,
43
+ PublishStateInfoInterval : time .Millisecond * 100 ,
44
+ MaxBlockCountToStore : 100 ,
45
+ PullPeerNum : 3 ,
46
+ PullInterval : time .Second ,
47
+ RequestStateInfoInterval : time .Millisecond * 100 ,
48
+ BlockExpirationInterval : time .Second * 6 ,
49
+ StateInfoExpirationInterval : time .Second * 6 ,
48
50
}
49
51
50
52
func init () {
@@ -275,7 +277,8 @@ func TestChannelPeriodicalPublishStateInfo(t *testing.T) {
275
277
})
276
278
277
279
gc := NewGossipChannel (pkiIDInOrg1 , cs , channelA , adapter , & joinChanMsg {})
278
- gc .UpdateStateInfo (createStateInfoMsg (ledgerHeight , pkiIDInOrg1 , channelA ))
280
+ stateInfoMsg := createStateInfoMsg (ledgerHeight , pkiIDInOrg1 , channelA )
281
+ gc .UpdateStateInfo (stateInfoMsg )
279
282
280
283
var msg * proto.SignedGossipMessage
281
284
select {
@@ -289,6 +292,14 @@ func TestChannelPeriodicalPublishStateInfo(t *testing.T) {
289
292
height , err := strconv .ParseInt (string (md ), 10 , 64 )
290
293
assert .NoError (t , err , "ReceivedMetadata is invalid" )
291
294
assert .Equal (t , ledgerHeight , int (height ), "Received different ledger height than expected" )
295
+
296
+ // We will not update StateInfo in store, so store will become empty
297
+ time .Sleep (conf .StateInfoExpirationInterval + time .Second )
298
+ //Store is empty
299
+ assert .Equal (t , 0 , gc .(* gossipChannel ).stateInfoMsgStore .MessageStore .Size (), "StateInfo MessageStore should be empty" )
300
+ assert .Equal (t , 0 , gc .(* gossipChannel ).stateInfoMsgStore .MembershipStore .Size (), "StateInfo MembershipStore should be empty" )
301
+
302
+ gc .Stop ()
292
303
}
293
304
294
305
func TestChannelPull (t * testing.T ) {
@@ -523,6 +534,99 @@ func TestChannelAddToMessageStore(t *testing.T) {
523
534
assert .True (t , gc .EligibleForChannel (discovery.NetworkMember {PKIid : pkiIDInOrg1 }))
524
535
}
525
536
537
+ func TestChannelAddToMessageStoreExpire (t * testing.T ) {
538
+ t .Parallel ()
539
+
540
+ cs := & cryptoService {}
541
+ cs .On ("VerifyBlock" , mock .Anything ).Return (nil )
542
+ demuxedMsgs := make (chan * proto.SignedGossipMessage , 1 )
543
+ adapter := new (gossipAdapterMock )
544
+ configureAdapter (adapter )
545
+ gc := NewGossipChannel (pkiIDInOrg1 , cs , channelA , adapter , & joinChanMsg {})
546
+ adapter .On ("Gossip" , mock .Anything )
547
+ adapter .On ("Send" , mock .Anything , mock .Anything )
548
+ adapter .On ("DeMultiplex" , mock .Anything ).Run (func (arg mock.Arguments ) {
549
+ demuxedMsgs <- arg .Get (0 ).(* proto.SignedGossipMessage )
550
+ })
551
+
552
+ respondedChan := make (chan * proto.GossipMessage , 1 )
553
+ messageRelayer := func (arg mock.Arguments ) {
554
+ msg := arg .Get (0 ).(* proto.GossipMessage )
555
+ respondedChan <- msg
556
+ }
557
+
558
+ // We make sure that if we get a new message it is de-multiplexed,
559
+ gc .HandleMessage (& receivedMsg {msg : dataMsgOfChannel (11 , channelA ), PKIID : pkiIDInOrg1 })
560
+ select {
561
+ case <- time .After (time .Second ):
562
+ t .Fatal ("Haven't detected a demultiplexing within a time period" )
563
+ case <- demuxedMsgs :
564
+ }
565
+
566
+ // Lets check digests and state info store
567
+ stateInfoMsg := createStateInfoMsg (10 , pkiIDInOrg1 , channelA )
568
+ gc .AddToMsgStore (stateInfoMsg )
569
+ helloMsg := createHelloMsg (pkiIDInOrg1 )
570
+ helloMsg .On ("Respond" , mock .Anything ).Run (messageRelayer )
571
+ gc .HandleMessage (helloMsg )
572
+ select {
573
+ case <- time .After (time .Second ):
574
+ t .Fatal ("Haven't responded to hello message within a time period" )
575
+ case msg := <- respondedChan :
576
+ if msg .IsDigestMsg () {
577
+ assert .Equal (t , 1 , len (msg .GetDataDig ().Digests ), "Number of digests returned by channel blockPuller incorrect" )
578
+ } else {
579
+ t .Fatal ("Not correct pull msg type in responce - expect digest" )
580
+ }
581
+ }
582
+
583
+ time .Sleep (gc .(* gossipChannel ).GetConf ().BlockExpirationInterval + time .Second )
584
+
585
+ // message expired in store, but still isn't demultiplexed when we
586
+ // receive that message again
587
+ gc .HandleMessage (& receivedMsg {msg : dataMsgOfChannel (11 , channelA ), PKIID : pkiIDInOrg1 })
588
+ select {
589
+ case <- time .After (time .Second ):
590
+ case <- demuxedMsgs :
591
+ t .Fatal ("Demultiplexing detected, even though it wasn't supposed to happen" )
592
+ }
593
+
594
+ // Lets check digests and state info store - state info expired, its add will do nothing and digest should not be sent
595
+ gc .AddToMsgStore (stateInfoMsg )
596
+ gc .HandleMessage (helloMsg )
597
+ select {
598
+ case <- time .After (time .Second ):
599
+ case <- respondedChan :
600
+ t .Fatal ("No digest should be sent" )
601
+ }
602
+
603
+ time .Sleep (gc .(* gossipChannel ).GetConf ().BlockExpirationInterval + time .Second )
604
+ // message removed from store, so it will be demultiplexed when we
605
+ // receive that message again
606
+ gc .HandleMessage (& receivedMsg {msg : dataMsgOfChannel (11 , channelA ), PKIID : pkiIDInOrg1 })
607
+ select {
608
+ case <- time .After (time .Second ):
609
+ t .Fatal ("Haven't detected a demultiplexing within a time period" )
610
+ case <- demuxedMsgs :
611
+ }
612
+
613
+ // Lets check digests and state info store - state info removed as well, so it will be added back and digest will be created
614
+ gc .AddToMsgStore (stateInfoMsg )
615
+ gc .HandleMessage (helloMsg )
616
+ select {
617
+ case <- time .After (time .Second ):
618
+ t .Fatal ("Haven't responded to hello message within a time period" )
619
+ case msg := <- respondedChan :
620
+ if msg .IsDigestMsg () {
621
+ assert .Equal (t , 1 , len (msg .GetDataDig ().Digests ), "Number of digests returned by channel blockPuller incorrect" )
622
+ } else {
623
+ t .Fatal ("Not correct pull msg type in responce - expect digest" )
624
+ }
625
+ }
626
+
627
+ gc .Stop ()
628
+ }
629
+
526
630
func TestChannelBadBlocks (t * testing.T ) {
527
631
t .Parallel ()
528
632
receivedMessages := make (chan * proto.SignedGossipMessage , 1 )
@@ -693,7 +797,8 @@ func TestChannelStateInfoSnapshot(t *testing.T) {
693
797
gc .HandleMessage (& receivedMsg {PKIID : pkiIDInOrg1 , msg : stateInfoSnapshotForChannel (channelA , createStateInfoMsg (4 , pkiIDInOrg1 , channelA ))})
694
798
695
799
// Ensure we process stateInfo snapshots that are OK
696
- gc .HandleMessage (& receivedMsg {PKIID : pkiIDInOrg1 , msg : stateInfoSnapshotForChannel (channelA , createStateInfoMsg (4 , pkiIDInOrg1 , channelA ))})
800
+ stateInfoMsg := & receivedMsg {PKIID : pkiIDInOrg1 , msg : stateInfoSnapshotForChannel (channelA , createStateInfoMsg (4 , pkiIDInOrg1 , channelA ))}
801
+ gc .HandleMessage (stateInfoMsg )
697
802
assert .NotEmpty (t , gc .GetPeers ())
698
803
assert .Equal (t , "4" , string (gc .GetPeers ()[0 ].Metadata ))
699
804
@@ -757,6 +862,41 @@ func TestChannelStateInfoSnapshot(t *testing.T) {
757
862
invalidStateInfoSnapshot = stateInfoSnapshotForChannel (channelA , createStateInfoMsg (4 , common .PKIidType ("unknown" ), channelA ))
758
863
gc .HandleMessage (& receivedMsg {PKIID : pkiIDInOrg1 , msg : invalidStateInfoSnapshot })
759
864
865
+ // Lets expire msg in store
866
+ time .Sleep (gc .(* gossipChannel ).GetConf ().StateInfoExpirationInterval + time .Second )
867
+
868
+ // Lets check is state info store can't add expired msg but appear as empty to outside world
869
+ gc .HandleMessage (stateInfoMsg )
870
+ assert .Empty (t , gc .GetPeers ())
871
+ // Lets see if snapshot now empty, after message in store expired
872
+ go gc .HandleMessage (snapshotReq )
873
+ select {
874
+ case <- time .After (time .Second ):
875
+ t .Fatal ("Haven't received a state info snapshot on time" )
876
+ case msg := <- sentMessages :
877
+ elements := msg .GetStateSnapshot ().Elements
878
+ assert .Len (t , elements , 0 , "StateInfo snapshot should contain zero messages" )
879
+ }
880
+
881
+ // Lets make sure msg removed from store
882
+ time .Sleep (gc .(* gossipChannel ).GetConf ().StateInfoExpirationInterval + time .Second )
883
+
884
+ // Lets check is state info store add just expired msg
885
+ gc .HandleMessage (stateInfoMsg )
886
+ assert .NotEmpty (t , gc .GetPeers ())
887
+ // Lets see if snapshot is not empty now, after message was added back to store
888
+ go gc .HandleMessage (snapshotReq )
889
+ select {
890
+ case <- time .After (time .Second ):
891
+ t .Fatal ("Haven't received a state info snapshot on time" )
892
+ case msg := <- sentMessages :
893
+ elements := msg .GetStateSnapshot ().Elements
894
+ assert .Len (t , elements , 1 )
895
+ sMsg , err := elements [0 ].ToGossipMessage ()
896
+ assert .NoError (t , err )
897
+ assert .Equal (t , []byte ("4" ), sMsg .GetStateInfo ().Metadata )
898
+ }
899
+
760
900
}
761
901
762
902
func TestChannelStop (t * testing.T ) {
0 commit comments