From 74cf033ce4ad67ac46b3cc6375582a13a5f38a0d Mon Sep 17 00:00:00 2001 From: yahtoo Date: Mon, 15 Jul 2019 16:10:01 +0800 Subject: [PATCH] Add a regular sync maximum block limit (#287) --- netsync/chainmgr/block_keeper.go | 12 +++++++++--- netsync/chainmgr/block_keeper_test.go | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/netsync/chainmgr/block_keeper.go b/netsync/chainmgr/block_keeper.go index 43f8521e..b810a1a5 100644 --- a/netsync/chainmgr/block_keeper.go +++ b/netsync/chainmgr/block_keeper.go @@ -22,8 +22,9 @@ const ( ) var ( - maxNumOfBlocksPerMsg = uint64(1000) - maxNumOfHeadersPerMsg = uint64(1000) + maxNumOfBlocksPerMsg = uint64(1000) + maxNumOfHeadersPerMsg = uint64(1000) + maxNumOfBlocksRegularSync = uint64(128) ) type FastSync interface { @@ -156,8 +157,13 @@ func (bk *blockKeeper) processHeaders(peerID string, headers []*types.BlockHeade func (bk *blockKeeper) regularBlockSync() error { peerHeight := bk.syncPeer.Height() bestHeight := bk.chain.BestBlockHeight() + targetHeight := bestHeight + maxNumOfBlocksRegularSync + if targetHeight > peerHeight { + targetHeight = peerHeight + } + i := bestHeight + 1 - for i <= peerHeight { + for i <= targetHeight { block, err := bk.msgFetcher.requireBlock(bk.syncPeer.ID(), i) if err != nil { bk.peers.ProcessIllegal(bk.syncPeer.ID(), security.LevelConnException, err.Error()) diff --git a/netsync/chainmgr/block_keeper_test.go b/netsync/chainmgr/block_keeper_test.go index 4a12346f..06a36d24 100644 --- a/netsync/chainmgr/block_keeper_test.go +++ b/netsync/chainmgr/block_keeper_test.go @@ -55,7 +55,7 @@ func TestRegularBlockSync(t *testing.T) { syncTimeout: 30 * time.Second, aBlocks: chainX[:52], bBlocks: chainZ, - want: chainZ[:201], + want: chainZ[:180], err: nil, }, } -- 2.11.0