Skip to content

Commit

Permalink
Retry syncing block with halfing batch size if fails
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <zyl.skysniper@gmail.com>
  • Loading branch information
yilunzhang committed Aug 6, 2019
1 parent 50a1ac8 commit bb883b3
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions node/syncblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,21 @@ func (localNode *LocalNode) StartSyncing(stopHash common.Uint256, stopHeight uin
log.Infof("Synced %d block headers in %s", stopHeight-currentHeight, time.Since(startTime))

startTime = time.Now()
err = localNode.syncBlocks(currentHeight+1, stopHeight, neighbors, headersHash)
for syncBlocksBatchSize := config.Parameters.SyncBlocksBatchSize; syncBlocksBatchSize > 0; syncBlocksBatchSize /= 2 {
numSyncedBlocks := chain.DefaultLedger.Store.GetHeight() - currentHeight
err = localNode.syncBlocks(currentHeight+numSyncedBlocks+1, stopHeight, syncBlocksBatchSize, neighbors, headersHash[numSyncedBlocks:])
if err == nil {
log.Infof("Synced %d blocks in %s", stopHeight-currentHeight, time.Since(startTime))
break
}
log.Errorf("Sync blocks error with batch size %d: %v", syncBlocksBatchSize, err)
}

if err != nil {
err = fmt.Errorf("sync blocks error: %v", err)
return
}

log.Infof("Synced %d blocks in %s", stopHeight-currentHeight, time.Since(startTime))

localNode.SetSyncState(pb.SYNC_FINISHED)
})

Expand Down Expand Up @@ -453,13 +460,13 @@ func (localNode *LocalNode) syncBlockHeaders(startHeight, stopHeight uint32, sta
return headersHash, nil
}

func (localNode *LocalNode) syncBlocks(startHeight, stopHeight uint32, neighbors []*RemoteNode, headersHash []common.Uint256) error {
numBatches := (stopHeight-startHeight)/config.Parameters.SyncBlocksBatchSize + 1
func (localNode *LocalNode) syncBlocks(startHeight, stopHeight, syncBlocksBatchSize uint32, neighbors []*RemoteNode, headersHash []common.Uint256) error {
numBatches := (stopHeight-startHeight)/syncBlocksBatchSize + 1
numWorkers := uint32(len(neighbors)) * concurrentSyncRequestPerNeighbor

getBatchHeightRange := func(batchID uint32) (uint32, uint32) {
batchStartHeight := startHeight + batchID*config.Parameters.SyncBlocksBatchSize
batchEndHeight := batchStartHeight + config.Parameters.SyncBlocksBatchSize - 1
batchStartHeight := startHeight + batchID*syncBlocksBatchSize
batchEndHeight := batchStartHeight + syncBlocksBatchSize - 1
if batchEndHeight > stopHeight {
batchEndHeight = stopHeight
}
Expand Down

0 comments on commit bb883b3

Please sign in to comment.