@@ -288,7 +288,7 @@ func TestNilDirectMsg(t *testing.T) {
288
288
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
289
289
g := & mocks.GossipMock {}
290
290
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
291
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
291
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
292
292
p := newPeerNodeWithGossip (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor , g )
293
293
defer p .shutdown ()
294
294
p .s .handleStateRequest (nil )
@@ -305,7 +305,7 @@ func TestNilAddPayload(t *testing.T) {
305
305
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
306
306
g := & mocks.GossipMock {}
307
307
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
308
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
308
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
309
309
p := newPeerNodeWithGossip (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor , g )
310
310
defer p .shutdown ()
311
311
err := p .s .AddPayload (nil )
@@ -318,7 +318,7 @@ func TestAddPayloadLedgerUnavailable(t *testing.T) {
318
318
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
319
319
g := & mocks.GossipMock {}
320
320
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
321
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
321
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
322
322
p := newPeerNodeWithGossip (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor , g )
323
323
defer p .shutdown ()
324
324
// Simulate a problem in the ledger
@@ -339,6 +339,77 @@ func TestAddPayloadLedgerUnavailable(t *testing.T) {
339
339
assert .Contains (t , err .Error (), "cannot query ledger" )
340
340
}
341
341
342
+ func TestLargeBlockGap (t * testing.T ) {
343
+ // Scenario: the peer knows of a peer who has a ledger height much higher
344
+ // than itself (500 blocks higher).
345
+ // The peer needs to ask blocks in a way such that the size of the payload buffer
346
+ // never rises above a certain threshold.
347
+
348
+ mc := & mockCommitter {}
349
+ blocksPassedToLedger := make (chan uint64 , 200 )
350
+ mc .On ("CommitWithPvtData" , mock .Anything ).Run (func (arg mock.Arguments ) {
351
+ blocksPassedToLedger <- arg .Get (0 ).(* pcomm.Block ).Header .Number
352
+ })
353
+ msgsFromPeer := make (chan proto.ReceivedMessage )
354
+ mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
355
+ g := & mocks.GossipMock {}
356
+ membership := []discovery.NetworkMember {
357
+ {
358
+ PKIid : common .PKIidType ("a" ),
359
+ Endpoint : "a" ,
360
+ Properties : & proto.Properties {
361
+ LedgerHeight : 500 ,
362
+ },
363
+ }}
364
+ g .On ("PeersOfChannel" , mock .Anything ).Return (membership )
365
+ g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
366
+ g .On ("Accept" , mock .Anything , true ).Return (nil , msgsFromPeer )
367
+ g .On ("Send" , mock .Anything , mock .Anything ).Run (func (arguments mock.Arguments ) {
368
+ msg := arguments .Get (0 ).(* proto.GossipMessage )
369
+ // The peer requested a state request
370
+ req := msg .GetStateRequest ()
371
+ // Construct a skeleton for the response
372
+ res := & proto.GossipMessage {
373
+ Nonce : msg .Nonce ,
374
+ Channel : []byte (util .GetTestChainID ()),
375
+ Content : & proto.GossipMessage_StateResponse {
376
+ StateResponse : & proto.RemoteStateResponse {},
377
+ },
378
+ }
379
+ // Populate the response with payloads according to what the peer asked
380
+ for seq := req .StartSeqNum ; seq <= req .EndSeqNum ; seq ++ {
381
+ rawblock := pcomm .NewBlock (seq , []byte {})
382
+ b , _ := pb .Marshal (rawblock )
383
+ payload := & proto.Payload {
384
+ SeqNum : seq ,
385
+ Data : b ,
386
+ }
387
+ res .GetStateResponse ().Payloads = append (res .GetStateResponse ().Payloads , payload )
388
+ }
389
+ // Finally, send the response down the channel the peer expects to receive it from
390
+ sMsg , _ := res .NoopSign ()
391
+ msgsFromPeer <- & comm.ReceivedMessageImpl {
392
+ SignedGossipMessage : sMsg ,
393
+ }
394
+ })
395
+ p := newPeerNodeWithGossip (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor , g )
396
+ defer p .shutdown ()
397
+
398
+ // Process blocks at a speed of 20 Millisecond for each block.
399
+ // The imaginative peer that responds to state
400
+ // If the payload buffer expands above defMaxBlockDistance*2 + defAntiEntropyBatchSize blocks, fail the test
401
+ blockProcessingTime := 20 * time .Millisecond // 10 seconds for total 500 blocks
402
+ expectedSequence := 1
403
+ for expectedSequence < 500 {
404
+ blockSeq := <- blocksPassedToLedger
405
+ assert .Equal (t , expectedSequence , int (blockSeq ))
406
+ // Ensure payload buffer isn't over-populated
407
+ assert .True (t , p .s .payloads .Size () <= defMaxBlockDistance * 2 + defAntiEntropyBatchSize , "payload buffer size is %d" , p .s .payloads .Size ())
408
+ expectedSequence ++
409
+ time .Sleep (blockProcessingTime )
410
+ }
411
+ }
412
+
342
413
func TestOverPopulation (t * testing.T ) {
343
414
// Scenario: Add to the state provider blocks
344
415
// with a gap in between, and ensure that the payload buffer
@@ -353,7 +424,7 @@ func TestOverPopulation(t *testing.T) {
353
424
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
354
425
g := & mocks.GossipMock {}
355
426
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
356
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
427
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
357
428
p := newPeerNode (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor )
358
429
defer p .shutdown ()
359
430
@@ -415,7 +486,7 @@ func TestBlockingEnqueue(t *testing.T) {
415
486
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (1 ), nil )
416
487
g := & mocks.GossipMock {}
417
488
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
418
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
489
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
419
490
p := newPeerNode (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor )
420
491
defer p .shutdown ()
421
492
@@ -476,7 +547,7 @@ func TestFailures(t *testing.T) {
476
547
mc .On ("LedgerHeight" , mock .Anything ).Return (uint64 (0 ), nil )
477
548
g := & mocks.GossipMock {}
478
549
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
479
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
550
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
480
551
g .On ("PeersOfChannel" , mock .Anything ).Return ([]discovery.NetworkMember {})
481
552
assert .Panics (t , func () {
482
553
newPeerNodeWithGossip (newGossipConfig (0 ), mc , noopPeerIdentityAcceptor , g )
@@ -535,7 +606,7 @@ func TestGossipReception(t *testing.T) {
535
606
g .On ("Accept" , mock .Anything , false ).Return (rmc , nil ).Run (func (_ mock.Arguments ) {
536
607
signalChan <- struct {}{}
537
608
})
538
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
609
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
539
610
g .On ("PeersOfChannel" , mock .Anything ).Return ([]discovery.NetworkMember {})
540
611
mc := & mockCommitter {}
541
612
receivedChan := make (chan struct {})
@@ -576,7 +647,7 @@ func TestMetadataCompatibility(t *testing.T) {
576
647
finChan <- struct {}{}
577
648
})
578
649
g .On ("Accept" , mock .Anything , false ).Return (make (<- chan * proto.GossipMessage ), nil )
579
- g .On ("Accept" , mock .Anything , true ).Return (nil , make (<- chan proto.ReceivedMessage ))
650
+ g .On ("Accept" , mock .Anything , true ).Return (nil , make (chan proto.ReceivedMessage ))
580
651
metaState := common .NewNodeMetastate (5 )
581
652
b , _ := metaState .Bytes ()
582
653
defaultPeer := discovery.NetworkMember {
@@ -1149,12 +1220,8 @@ func TestTransferOfPrivateRWSet(t *testing.T) {
1149
1220
return ch
1150
1221
}
1151
1222
1152
- commChannelFactory := func (ch chan proto.ReceivedMessage ) <- chan proto.ReceivedMessage {
1153
- return ch
1154
- }
1155
-
1156
1223
g .On ("Accept" , mock .Anything , false ).Return (gossipChannelFactory (gossipChannel ), nil )
1157
- g .On ("Accept" , mock .Anything , true ).Return (nil , commChannelFactory ( commChannel ) )
1224
+ g .On ("Accept" , mock .Anything , true ).Return (nil , commChannel )
1158
1225
1159
1226
g .On ("UpdateChannelMetadata" , mock .Anything , mock .Anything )
1160
1227
g .On ("PeersOfChannel" , mock .Anything ).Return ([]discovery.NetworkMember {})
@@ -1331,7 +1398,7 @@ func (t testPeer) Gossip() <-chan *proto.GossipMessage {
1331
1398
return t .gossipChannel
1332
1399
}
1333
1400
1334
- func (t testPeer ) Comm () <- chan proto.ReceivedMessage {
1401
+ func (t testPeer ) Comm () chan proto.ReceivedMessage {
1335
1402
return t .commChannel
1336
1403
}
1337
1404
@@ -1372,7 +1439,7 @@ func TestTransferOfPvtDataBetweenPeers(t *testing.T) {
1372
1439
Return (nil , peer .Comm ()).
1373
1440
Once ().
1374
1441
On ("Accept" , mock .Anything , true ).
1375
- Return (nil , make (<- chan proto.ReceivedMessage ))
1442
+ Return (nil , make (chan proto.ReceivedMessage ))
1376
1443
1377
1444
peer .On ("UpdateChannelMetadata" , mock .Anything , mock .Anything )
1378
1445
peer .coord .On ("Close" )
0 commit comments