@@ -35,12 +35,14 @@ import (
35
35
"github.com/hyperledger/fabric/gossip/api"
36
36
"github.com/hyperledger/fabric/gossip/comm"
37
37
"github.com/hyperledger/fabric/gossip/common"
38
+ "github.com/hyperledger/fabric/gossip/discovery"
38
39
"github.com/hyperledger/fabric/gossip/gossip"
39
40
"github.com/hyperledger/fabric/gossip/identity"
40
41
"github.com/hyperledger/fabric/gossip/state/mocks"
41
42
gutil "github.com/hyperledger/fabric/gossip/util"
42
43
pcomm "github.com/hyperledger/fabric/protos/common"
43
44
proto "github.com/hyperledger/fabric/protos/gossip"
45
+ "github.com/op/go-logging"
44
46
"github.com/spf13/viper"
45
47
"github.com/stretchr/testify/assert"
46
48
"github.com/stretchr/testify/mock"
@@ -63,6 +65,7 @@ type joinChanMsg struct {
63
65
64
66
func init () {
65
67
gutil .SetupTestLogging ()
68
+ logging .SetLevel (logging .DEBUG , gutil .LoggingStateModule )
66
69
}
67
70
68
71
// SequenceNumber returns the sequence number of the block that the message
@@ -273,7 +276,7 @@ func TestNilDirectMsg(t *testing.T) {
273
276
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
274
277
g := & mocks.GossipMock {}
275
278
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
276
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
279
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
277
280
p := newPeerNodeWithGossip (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor , g )
278
281
defer p .shutdown ()
279
282
p .s .handleStateRequest (nil )
@@ -290,7 +293,7 @@ func TestNilAddPayload(t *testing.T) {
290
293
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
291
294
g := & mocks.GossipMock {}
292
295
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
293
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
296
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
294
297
p := newPeerNodeWithGossip (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor , g )
295
298
defer p .shutdown ()
296
299
err := p .s .AddPayload (nil )
@@ -303,7 +306,7 @@ func TestAddPayloadLedgerUnavailable(t *testing.T) {
303
306
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
304
307
g := & mocks.GossipMock {}
305
308
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
306
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
309
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
307
310
p := newPeerNodeWithGossip (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor , g )
308
311
defer p .shutdown ()
309
312
// Simulate a problem in the ledger
@@ -324,6 +327,77 @@ func TestAddPayloadLedgerUnavailable(t *testing.T) {
324
327
assert .Contains (t , err .Error (), "cannot query ledger" )
325
328
}
326
329
330
+ func TestLargeBlockGap (t * testing.T ) {
331
+ // Scenario: the peer knows of a peer who has a ledger height much higher
332
+ // than itself (500 blocks higher).
333
+ // The peer needs to ask blocks in a way such that the size of the payload buffer
334
+ // never rises above a certain threshold.
335
+
336
+ mc := & mockCommitter {}
337
+ blocksPassedToLedger := make (chan uint64 , 200 )
338
+ mc .On ("Commit" , mock .Anything ).Run (func (arg mock.Arguments ) {
339
+ blocksPassedToLedger <- arg .Get (0 ).(* pcomm.Block ).Header .Number
340
+ })
341
+ msgsFromPeer := make (chan proto.ReceivedMessage )
342
+ mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
343
+ g := & mocks.GossipMock {}
344
+ metaState := NewNodeMetastate (500 )
345
+ md , _ := metaState .Bytes ()
346
+ membership := []discovery.NetworkMember {
347
+ {
348
+ PKIid : common .PKIidType ("a" ),
349
+ Endpoint : "a" ,
350
+ Metadata : md ,
351
+ }}
352
+ g .On ("PeersOfChannel" , mock .Anything ).Return (membership )
353
+ g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
354
+ g .On ("Accept" , mock .Anything , true ).Return (nil , msgsFromPeer )
355
+ g .On ("Send" , mock .Anything , mock .Anything ).Run (func (arguments mock.Arguments ) {
356
+ msg := arguments .Get (0 ).(* proto.GossipMessage )
357
+ // The peer requested a state request
358
+ req := msg .GetStateRequest ()
359
+ // Construct a skeleton for the response
360
+ res := & proto.GossipMessage {
361
+ Nonce : msg .Nonce ,
362
+ Channel : []byte (util .GetTestChainID ()),
363
+ Content : & proto.GossipMessage_StateResponse {
364
+ StateResponse : & proto.RemoteStateResponse {},
365
+ },
366
+ }
367
+ // Populate the response with payloads according to what the peer asked
368
+ for seq := req .StartSeqNum ; seq <= req .EndSeqNum ; seq ++ {
369
+ rawblock := pcomm .NewBlock (seq , []byte {})
370
+ b , _ := pb .Marshal (rawblock )
371
+ payload := & proto.Payload {
372
+ SeqNum : seq ,
373
+ Data : b ,
374
+ }
375
+ res .GetStateResponse ().Payloads = append (res .GetStateResponse ().Payloads , payload )
376
+ }
377
+ // Finally, send the response down the channel the peer expects to receive it from
378
+ sMsg , _ := res .NoopSign ()
379
+ msgsFromPeer <- & comm.ReceivedMessageImpl {
380
+ SignedGossipMessage : sMsg ,
381
+ }
382
+ })
383
+ p := newPeerNodeWithGossip (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor , g )
384
+ defer p .shutdown ()
385
+
386
+ // Process blocks at a speed of 20 Millisecond for each block.
387
+ // The imaginative peer that responds to state
388
+ // If the payload buffer expands above defMaxBlockDistance*2 + defAntiEntropyBatchSize blocks, fail the test
389
+ blockProcessingTime := 20 * time .Millisecond // 10 seconds for total 500 blocks
390
+ expectedSequence := 1
391
+ for expectedSequence < 500 {
392
+ blockSeq := <- blocksPassedToLedger
393
+ assert .Equal (t , expectedSequence , int (blockSeq ))
394
+ // Ensure payload buffer isn't over-populated
395
+ assert .True (t , p .s .payloads .Size () <= defMaxBlockDistance * 2 + defAntiEntropyBatchSize , "payload buffer size is %d" , p .s .payloads .Size ())
396
+ expectedSequence ++
397
+ time .Sleep (blockProcessingTime )
398
+ }
399
+ }
400
+
327
401
func TestOverPopulation (t * testing.T ) {
328
402
// Scenario: Add to the state provider blocks
329
403
// with a gap in between, and ensure that the payload buffer
@@ -338,7 +412,7 @@ func TestOverPopulation(t *testing.T) {
338
412
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
339
413
g := & mocks.GossipMock {}
340
414
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
341
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
415
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
342
416
p := newPeerNode (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor )
343
417
defer p .shutdown ()
344
418
@@ -400,7 +474,7 @@ func TestBlockingEnqueue(t *testing.T) {
400
474
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
401
475
g := & mocks.GossipMock {}
402
476
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
403
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
477
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
404
478
p := newPeerNode (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor )
405
479
defer p .shutdown ()
406
480
@@ -461,7 +535,8 @@ func TestFailures(t *testing.T) {
461
535
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (0 ), nil )
462
536
g := & mocks.GossipMock {}
463
537
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
464
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
538
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
539
+ g .On ("PeersOfChannel" , mock .Anything ).Return ([]discovery.NetworkMember {})
465
540
assert .Panics (t , func () {
466
541
newPeerNodeWithGossip (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor , g )
467
542
})
@@ -519,7 +594,8 @@ func TestGossipReception(t *testing.T) {
519
594
g .On ("Accept" , mock .Anything , false ).Return (rmc , nil ).Run (func (_ mock.Arguments ) {
520
595
signalChan <- struct {}{}
521
596
})
522
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
597
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
598
+ g .On ("PeersOfChannel" , mock .Anything ).Return ([]discovery.NetworkMember {})
523
599
mc := & mockCommitter {}
524
600
receivedChan := make (chan struct {})
525
601
mc .On ("Commit" , mock .Anything ).Run (func (arguments mock.Arguments ) {
0 commit comments