OSDN Git Service

Add a regular sync maximum block limit (#287)
authoryahtoo <yahtoo.ma@gmail.com>
Mon, 15 Jul 2019 08:10:01 +0000 (16:10 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 15 Jul 2019 08:10:01 +0000 (16:10 +0800)
netsync/chainmgr/block_keeper.go
netsync/chainmgr/block_keeper_test.go

index 43f8521..b810a1a 100644 (file)
@@ -22,8 +22,9 @@ const (
 )
 
 var (
 )
 
 var (
-       maxNumOfBlocksPerMsg  = uint64(1000)
-       maxNumOfHeadersPerMsg = uint64(1000)
+       maxNumOfBlocksPerMsg      = uint64(1000)
+       maxNumOfHeadersPerMsg     = uint64(1000)
+       maxNumOfBlocksRegularSync = uint64(128)
 )
 
 type FastSync interface {
 )
 
 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()
 func (bk *blockKeeper) regularBlockSync() error {
        peerHeight := bk.syncPeer.Height()
        bestHeight := bk.chain.BestBlockHeight()
+       targetHeight := bestHeight + maxNumOfBlocksRegularSync
+       if targetHeight > peerHeight {
+               targetHeight = peerHeight
+       }
+
        i := bestHeight + 1
        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())
                block, err := bk.msgFetcher.requireBlock(bk.syncPeer.ID(), i)
                if err != nil {
                        bk.peers.ProcessIllegal(bk.syncPeer.ID(), security.LevelConnException, err.Error())
index 4a12346..06a36d2 100644 (file)
@@ -55,7 +55,7 @@ func TestRegularBlockSync(t *testing.T) {
                        syncTimeout: 30 * time.Second,
                        aBlocks:     chainX[:52],
                        bBlocks:     chainZ,
                        syncTimeout: 30 * time.Second,
                        aBlocks:     chainX[:52],
                        bBlocks:     chainZ,
-                       want:        chainZ[:201],
+                       want:        chainZ[:180],
                        err:         nil,
                },
        }
                        err:         nil,
                },
        }