@@ -62,6 +62,13 @@ func acceptData(m interface{}) bool {
62
62
return false
63
63
}
64
64
65
+ func acceptLeadershp (message interface {}) bool {
66
+ validMsg := message .(* proto.GossipMessage ).Tag == proto .GossipMessage_CHAN_AND_ORG &&
67
+ message .(* proto.GossipMessage ).IsLeadershipMsg ()
68
+
69
+ return validMsg
70
+ }
71
+
65
72
type joinChanMsg struct {
66
73
anchorPeers []api.AnchorPeer
67
74
}
@@ -489,6 +496,34 @@ func TestDissemination(t *testing.T) {
489
496
for i := 0 ; i < n ; i ++ {
490
497
assert .Equal (t , msgsCount2Send , receivedMessages [i ])
491
498
}
499
+
500
+ //Sending leadership messages
501
+ receivedLeadershipMessages := make ([]int , n )
502
+ wgLeadership := sync.WaitGroup {}
503
+ wgLeadership .Add (n )
504
+ for i := 1 ; i <= n ; i ++ {
505
+ leadershipChan , _ := peers [i - 1 ].Accept (acceptLeadershp , false )
506
+ go func (index int , ch <- chan * proto.GossipMessage ) {
507
+ defer wgLeadership .Done ()
508
+ <- ch
509
+ receivedLeadershipMessages [index ]++
510
+ }(i - 1 , leadershipChan )
511
+ }
512
+
513
+ seqNum := 0
514
+ incTime := uint64 (time .Now ().UnixNano ())
515
+ t3 := time .Now ()
516
+
517
+ leadershipMsg := createLeadershipMsg (true , common .ChainID ("A" ), incTime , uint64 (seqNum ), boot .(* gossipServiceImpl ).conf .SelfEndpoint , boot .(* gossipServiceImpl ).comm .GetPKIid ())
518
+ boot .Gossip (leadershipMsg )
519
+
520
+ waitUntilOrFailBlocking (t , wgLeadership .Wait )
521
+ t .Log ("Leadership message dissemination took" , time .Since (t3 ))
522
+
523
+ for i := 0 ; i < n ; i ++ {
524
+ assert .Equal (t , 1 , receivedLeadershipMessages [i ])
525
+ }
526
+
492
527
t .Log ("Stopping peers" )
493
528
494
529
stop := func () {
@@ -784,6 +819,78 @@ func TestEndedGoroutines(t *testing.T) {
784
819
ensureGoroutineExit (t )
785
820
}
786
821
822
+ //func TestLeadershipMsgDissemination(t *testing.T) {
823
+ // t.Parallel()
824
+ // portPrefix := 3610
825
+ // t1 := time.Now()
826
+ // // Scenario: 20 nodes and a bootstrap node.
827
+ // // The bootstrap node sends 10 leadership messages and we count
828
+ // // that each node got 10 messages after a few seconds
829
+ //
830
+ // stopped := int32(0)
831
+ // go waitForTestCompletion(&stopped, t)
832
+ //
833
+ // n := 10
834
+ // msgsCount2Send := 10
835
+ // boot := newGossipInstance(portPrefix, 0, 100)
836
+ // boot.JoinChan(&joinChanMsg{}, common.ChainID("A"))
837
+ // boot.UpdateChannelMetadata([]byte{}, common.ChainID("A"))
838
+ //
839
+ // peers := make([]Gossip, n)
840
+ // receivedMessages := make([]int, n)
841
+ // wg := sync.WaitGroup{}
842
+ // wg.Add(n)
843
+ // for i := 1; i <= n; i++ {
844
+ // pI := newGossipInstance(portPrefix, i, 100, 0)
845
+ // peers[i-1] = pI
846
+ // pI.JoinChan(&joinChanMsg{}, common.ChainID("A"))
847
+ // pI.UpdateChannelMetadata([]byte{}, common.ChainID("A"))
848
+ // acceptChan, _ := pI.Accept(acceptLeadershp, false)
849
+ // go func(index int, ch <-chan *proto.GossipMessage) {
850
+ // defer wg.Done()
851
+ // for j := 0; j < msgsCount2Send; j++ {
852
+ // <-ch
853
+ // receivedMessages[index]++
854
+ // }
855
+ // }(i-1, acceptChan)
856
+ // }
857
+ //
858
+ // membershipTime := time.Now()
859
+ // waitUntilOrFail(t, checkPeersMembership(peers, n))
860
+ // t.Log("Membership establishment took", time.Since(membershipTime))
861
+ //
862
+ // seqNum := 0
863
+ // incTime := uint64(time.Now().UnixNano())
864
+ //
865
+ // for i := 1; i <= msgsCount2Send; i++ {
866
+ // seqNum++
867
+ // leadershipMsg := createLeadershipMsg(true, common.ChainID("A"), incTime, uint64(seqNum), boot.(*gossipServiceImpl).conf.SelfEndpoint, boot.(*gossipServiceImpl).comm.GetPKIid())
868
+ // boot.Gossip(leadershipMsg)
869
+ // time.Sleep(time.Duration(500) * time.Millisecond)
870
+ // }
871
+ //
872
+ // t2 := time.Now()
873
+ // waitUntilOrFailBlocking(t, wg.Wait)
874
+ // t.Log("Leadership message dissemination took", time.Since(t2))
875
+ //
876
+ // for i := 0; i < n; i++ {
877
+ // assert.Equal(t, msgsCount2Send, receivedMessages[i])
878
+ // }
879
+ // t.Log("Stopping peers")
880
+ //
881
+ // stop := func() {
882
+ // stopPeers(append(peers, boot))
883
+ // }
884
+ //
885
+ // stopTime := time.Now()
886
+ // waitUntilOrFailBlocking(t, stop)
887
+ // t.Log("Stop took", time.Since(stopTime))
888
+ // t.Log("Took", time.Since(t1))
889
+ // atomic.StoreInt32(&stopped, int32(1))
890
+ // fmt.Println("<<<TestLeadershipMsgDissemination>>>")
891
+ // testWG.Done()
892
+ //}
893
+
787
894
func createDataMsg (seqnum uint64 , data []byte , hash string , channel common.ChainID ) * proto.GossipMessage {
788
895
return & proto.GossipMessage {
789
896
Channel : []byte (channel ),
@@ -801,6 +908,32 @@ func createDataMsg(seqnum uint64, data []byte, hash string, channel common.Chain
801
908
}
802
909
}
803
910
911
+ func createLeadershipMsg (isDeclaration bool , channel common.ChainID , incTime uint64 , seqNum uint64 , endpoint string , pkiid []byte ) * proto.GossipMessage {
912
+
913
+ metadata := []byte {}
914
+ metadata = strconv .AppendBool (metadata , isDeclaration )
915
+
916
+ leadershipMsg := & proto.LeadershipMessage {
917
+ Membership : & proto.Member {
918
+ PkiID : pkiid ,
919
+ Endpoint : endpoint ,
920
+ Metadata : metadata ,
921
+ },
922
+ Timestamp : & proto.PeerTime {
923
+ IncNumber : incTime ,
924
+ SeqNum : seqNum ,
925
+ },
926
+ }
927
+
928
+ msg := & proto.GossipMessage {
929
+ Nonce : 0 ,
930
+ Tag : proto .GossipMessage_CHAN_AND_ORG ,
931
+ Content : & proto.GossipMessage_LeadershipMsg {LeadershipMsg : leadershipMsg },
932
+ Channel : channel ,
933
+ }
934
+ return msg
935
+ }
936
+
804
937
type goroutinePredicate func (g goroutine ) bool
805
938
806
939
var connectionLeak = func (g goroutine ) bool {
0 commit comments