@@ -9,7 +9,6 @@ package channel
9
9
import (
10
10
"errors"
11
11
"fmt"
12
- "strconv"
13
12
"sync"
14
13
"sync/atomic"
15
14
"testing"
@@ -400,10 +399,9 @@ func TestChannelPeriodicalPublishStateInfo(t *testing.T) {
400
399
msg = m
401
400
}
402
401
403
- md := msg .GetStateInfo ().Metadata
404
- height , err := strconv .ParseInt (string (md ), 10 , 64 )
402
+ nodeMeta , err := common .FromBytes (msg .GetStateInfo ().Metadata )
405
403
assert .NoError (t , err , "ReceivedMetadata is invalid" )
406
- assert .Equal (t , ledgerHeight , int (height ), "Received different ledger height than expected" )
404
+ assert .Equal (t , ledgerHeight , int (nodeMeta . LedgerHeight ), "Received different ledger height than expected" )
407
405
}
408
406
409
407
func TestChannelMsgStoreEviction (t * testing.T ) {
@@ -1097,7 +1095,9 @@ func TestChannelStateInfoSnapshot(t *testing.T) {
1097
1095
stateInfoMsg := & receivedMsg {PKIID : pkiIDInOrg1 , msg : stateInfoSnapshotForChannel (channelA , createStateInfoMsg (4 , pkiIDInOrg1 , channelA ))}
1098
1096
gc .HandleMessage (stateInfoMsg )
1099
1097
assert .NotEmpty (t , gc .GetPeers ())
1100
- assert .Equal (t , "4" , string (gc .GetPeers ()[0 ].Metadata ))
1098
+ nodeMeta , err := common .FromBytes (gc .GetPeers ()[0 ].Metadata )
1099
+ assert .NoError (t , err )
1100
+ assert .Equal (t , 4 , int (nodeMeta .LedgerHeight ))
1101
1101
1102
1102
// Check we don't respond to stateInfoSnapshot requests with wrong MAC
1103
1103
sMsg , _ := (& proto.GossipMessage {
@@ -1149,7 +1149,9 @@ func TestChannelStateInfoSnapshot(t *testing.T) {
1149
1149
assert .Len (t , elements , 1 )
1150
1150
sMsg , err := elements [0 ].ToGossipMessage ()
1151
1151
assert .NoError (t , err )
1152
- assert .Equal (t , []byte ("4" ), sMsg .GetStateInfo ().Metadata )
1152
+ nodeMeta , err := common .FromBytes (sMsg .GetStateInfo ().Metadata )
1153
+ assert .NoError (t , err )
1154
+ assert .Equal (t , 4 , int (nodeMeta .LedgerHeight ))
1153
1155
}
1154
1156
1155
1157
// Ensure we don't crash if we got an invalid state info message
@@ -1615,6 +1617,44 @@ func TestOnDemandGossip(t *testing.T) {
1615
1617
}
1616
1618
}
1617
1619
1620
+ func TestChannelPullWithDigestsFilter (t * testing.T ) {
1621
+ t .Parallel ()
1622
+ cs := & cryptoService {}
1623
+ cs .On ("VerifyBlock" , mock .Anything ).Return (nil )
1624
+ receivedBlocksChan := make (chan * proto.SignedGossipMessage , 2 )
1625
+ adapter := new (gossipAdapterMock )
1626
+ configureAdapter (adapter , discovery.NetworkMember {PKIid : pkiIDInOrg1 })
1627
+ adapter .On ("Gossip" , mock .Anything )
1628
+ adapter .On ("DeMultiplex" , mock .Anything ).Run (func (arg mock.Arguments ) {
1629
+ msg := arg .Get (0 ).(* proto.SignedGossipMessage )
1630
+ if ! msg .IsDataMsg () {
1631
+ return
1632
+ }
1633
+ // The peer is supposed to de-multiplex 1 ledger block
1634
+ assert .True (t , msg .IsDataMsg ())
1635
+ receivedBlocksChan <- msg
1636
+ })
1637
+ gc := NewGossipChannel (pkiIDInOrg1 , orgInChannelA , cs , channelA , adapter , & joinChanMsg {})
1638
+ go gc .HandleMessage (& receivedMsg {PKIID : pkiIDInOrg1 , msg : createStateInfoMsg (100 , pkiIDInOrg1 , channelA )})
1639
+
1640
+ gc .UpdateStateInfo (createStateInfoMsg (11 , pkiIDInOrg1 , channelA ))
1641
+
1642
+ var wg sync.WaitGroup
1643
+ wg .Add (1 )
1644
+
1645
+ pullPhase := simulatePullPhaseWithVariableDigest (gc , t , & wg , func (envelope * proto.Envelope ) {}, []string {"10" , "11" }, []string {"11" }, 11 )
1646
+ adapter .On ("Send" , mock .Anything , mock .Anything ).Run (pullPhase )
1647
+ wg .Wait ()
1648
+
1649
+ select {
1650
+ case <- time .After (time .Second * 5 ):
1651
+ t .Fatal ("Haven't received blocks on time" )
1652
+ case msg := <- receivedBlocksChan :
1653
+ assert .Equal (t , uint64 (11 ), msg .GetDataMsg ().Payload .SeqNum )
1654
+ }
1655
+
1656
+ }
1657
+
1618
1658
func createDataUpdateMsg (nonce uint64 , seqs ... uint64 ) * proto.SignedGossipMessage {
1619
1659
msg := & proto.GossipMessage {
1620
1660
Nonce : 0 ,
@@ -1669,13 +1709,15 @@ func dataMsgOfChannel(seqnum uint64, channel common.ChainID) *proto.SignedGossip
1669
1709
}
1670
1710
1671
1711
func createStateInfoMsg (ledgerHeight int , pkiID common.PKIidType , channel common.ChainID ) * proto.SignedGossipMessage {
1712
+ nodeMeta := common .NewNodeMetastate (uint64 (ledgerHeight ))
1713
+ metaBytes , _ := nodeMeta .Bytes ()
1672
1714
sMsg , _ := (& proto.GossipMessage {
1673
1715
Tag : proto .GossipMessage_CHAN_OR_ORG ,
1674
1716
Content : & proto.GossipMessage_StateInfo {
1675
1717
StateInfo : & proto.StateInfo {
1676
1718
Channel_MAC : GenerateMAC (pkiID , channel ),
1677
1719
Timestamp : & proto.PeerTime {IncNum : uint64 (time .Now ().UnixNano ()), SeqNum : 1 },
1678
- Metadata : [] byte ( fmt . Sprintf ( "%d" , ledgerHeight )) ,
1720
+ Metadata : metaBytes ,
1679
1721
PkiId : []byte (pkiID ),
1680
1722
},
1681
1723
},
@@ -1719,6 +1761,10 @@ func createDataMsg(seqnum uint64, channel common.ChainID) *proto.SignedGossipMes
1719
1761
}
1720
1762
1721
1763
func simulatePullPhase (gc GossipChannel , t * testing.T , wg * sync.WaitGroup , mutator msgMutator , seqs ... uint64 ) func (args mock.Arguments ) {
1764
+ return simulatePullPhaseWithVariableDigest (gc , t , wg , mutator , []string {"10" , "11" }, []string {"10" , "11" }, seqs ... )
1765
+ }
1766
+
1767
+ func simulatePullPhaseWithVariableDigest (gc GossipChannel , t * testing.T , wg * sync.WaitGroup , mutator msgMutator , proposedDigestSeqs []string , resultDigestSeqs []string , seqs ... uint64 ) func (args mock.Arguments ) {
1722
1768
var l sync.Mutex
1723
1769
var sentHello bool
1724
1770
var sentReq bool
@@ -1735,7 +1781,7 @@ func simulatePullPhase(gc GossipChannel, t *testing.T, wg *sync.WaitGroup, mutat
1735
1781
Content : & proto.GossipMessage_DataDig {
1736
1782
DataDig : & proto.DataDigest {
1737
1783
MsgType : proto .PullMsgType_BLOCK_MSG ,
1738
- Digests : [] string { "10" , "11" } ,
1784
+ Digests : proposedDigestSeqs ,
1739
1785
Nonce : msg .GetHello ().Nonce ,
1740
1786
},
1741
1787
},
@@ -1749,10 +1795,10 @@ func simulatePullPhase(gc GossipChannel, t *testing.T, wg *sync.WaitGroup, mutat
1749
1795
if msg .IsDataReq () && ! sentReq {
1750
1796
sentReq = true
1751
1797
dataReq := msg .GetDataReq ()
1752
- for _ , expectedDigest := range [] string { "10" , "11" } {
1798
+ for _ , expectedDigest := range resultDigestSeqs {
1753
1799
assert .Contains (t , dataReq .Digests , expectedDigest )
1754
1800
}
1755
- assert .Equal (t , 2 , len (dataReq .Digests ))
1801
+ assert .Equal (t , len ( resultDigestSeqs ) , len (dataReq .Digests ))
1756
1802
// When we send a data request, simulate a response of a data update
1757
1803
// from the imaginary peer that got the request
1758
1804
dataUpdateMsg := new (receivedMsg )
0 commit comments