Skip to content

Commit 97dc2d9

Browse files
authored
Merge pull request #129 from hyperledger/fix-127
Do not keep dispatching after block listener falls behind
2 parents 293e998 + 9ec77ad commit 97dc2d9

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

internal/confirmations/confirmed_block_listener.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ func (cbl *confirmedBlockListener) processBlockNotification(block *apitypes.Bloc
194194
var dispatchHead *apitypes.BlockInfo
195195
if len(cbl.newHeadToAdd) > 0 {
196196
// we've snuck in multiple notifications while the dispatcher is busy... don't add indefinitely to this list
197-
if len(cbl.newHeadToAdd) > 10 /* not considered worth adding/explaining a tuning property for this */ {
197+
if len(cbl.newHeadToAdd) >= 10 /* not considered worth adding/explaining a tuning property for this */ {
198198
log.L(cbl.ctx).Infof("Block listener fell behind head of chain")
199-
cbl.newHeadToAdd = nil
200-
} else {
201-
dispatchHead = cbl.newHeadToAdd[len(cbl.newHeadToAdd)-1]
199+
// Nothing more we can do in this function until it catches up - we just have to discard the notification
200+
return
202201
}
202+
dispatchHead = cbl.newHeadToAdd[len(cbl.newHeadToAdd)-1]
203203
}
204204
if dispatchHead == nil && len(cbl.blocksSinceCheckpoint) > 0 {
205205
dispatchHead = cbl.blocksSinceCheckpoint[len(cbl.blocksSinceCheckpoint)-1]

internal/confirmations/confirmed_block_listener_test.go

+23-21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package confirmations
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"strings"
2021
"sync/atomic"
@@ -338,38 +339,39 @@ func TestCBLDispatcherFallsBehindHead(t *testing.T) {
338339
mbiHash := mca.On("BlockInfoByHash", mock.Anything, mock.Anything)
339340
mbiHash.Run(func(args mock.Arguments) { mockBlockHashReturn(mbiHash, args, blocks) })
340341

341-
// We'll fall back to this because we don't keep up
342-
mbiNum := mca.On("BlockInfoByNumber", mock.Anything, mock.Anything)
343-
mbiNum.Run(func(args mock.Arguments) { mockBlockNumberReturn(mbiNum, args, blocks) })
344-
345342
bcm.requiredConfirmations = 5
343+
344+
// Start a CBL, but then cancel the dispatcher
346345
cbl, err := bcm.startConfirmedBlockListener(bcm.ctx, id, "", nil, esDispatch)
347346
assert.NoError(t, err)
347+
cbl.cancelFunc()
348+
<-cbl.dispatcherDone
349+
<-cbl.processorDone
350+
351+
// Restart just the processor
352+
cbl.ctx, cbl.cancelFunc = context.WithCancel(bcm.ctx)
353+
cbl.processorDone = make(chan struct{})
354+
go cbl.notificationProcessor()
348355

349-
// Notify all the blocks before we process any
356+
// Notify all the blocks
350357
for i := 0; i < len(blocks); i++ {
351358
bcm.propagateBlockHashToCBLs(&ffcapi.BlockHashEvent{
352359
BlockHashes: []string{blocks[i].BlockHash},
353360
})
354361
}
355362

356-
for i := 0; i < len(blocks)-bcm.requiredConfirmations; i++ {
357-
// The dispatches should have been added, until it got too far ahead
358-
// and then set to nil.
359-
for cbl.newHeadToAdd != nil {
360-
time.Sleep(1 * time.Millisecond)
361-
}
362-
b := <-esDispatch
363-
assert.Equal(t, b.BlockEvent.BlockNumber.Uint64(), blocks[i].BlockNumber.Uint64())
364-
assert.Equal(t, b.BlockEvent.BlockInfo, blocks[i].BlockInfo)
365-
}
363+
// Wait until there's a first tap to the dispatcher, meaning
364+
// dispatches should have been added to the newHeadToAdd list
365+
<-cbl.dispatcherTap
366366

367-
time.Sleep(1 * time.Millisecond)
368-
assert.Len(t, cbl.blocksSinceCheckpoint, bcm.requiredConfirmations)
369-
select {
370-
case <-esDispatch:
371-
assert.Fail(t, "should not have received block in confirmation window")
372-
default: // good - we should have the confirmations sat there, but no dispatch
367+
// ... until it got too far ahead and then set to nil.
368+
checkHeadLen := func() int {
369+
cbl.stateLock.Lock()
370+
defer cbl.stateLock.Unlock()
371+
return len(cbl.newHeadToAdd)
372+
}
373+
for checkHeadLen() < 10 {
374+
time.Sleep(1 * time.Millisecond)
373375
}
374376

375377
bcm.Stop()

0 commit comments

Comments
 (0)