From: yahtoo Date: Thu, 25 Jul 2019 06:33:33 +0000 (+0800) Subject: Add skeleton size validity check (#354) X-Git-Tag: v1.0.5~76 X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=commitdiff_plain;h=eae6d5358dbda7efed55b18b9b6585e58a993072 Add skeleton size validity check (#354) * Add skeleton size validity check * Add max size check * Add peer illegal process --- diff --git a/netsync/chainmgr/fast_sync.go b/netsync/chainmgr/fast_sync.go index ed426b7c..550d2dff 100644 --- a/netsync/chainmgr/fast_sync.go +++ b/netsync/chainmgr/fast_sync.go @@ -7,18 +7,21 @@ import ( "github.com/vapor/errors" "github.com/vapor/netsync/peers" + "github.com/vapor/p2p/security" "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" ) var ( - maxNumOfSkeletonPerSync = uint64(10) - numOfBlocksSkeletonGap = maxNumOfBlocksPerMsg - maxNumOfBlocksPerSync = numOfBlocksSkeletonGap * maxNumOfSkeletonPerSync - fastSyncPivotGap = uint64(64) - minGapStartFastSync = uint64(128) - - errNoSyncPeer = errors.New("can't find sync peer") + minSizeOfSyncSkeleton = 2 + maxSizeOfSyncSkeleton = 11 + numOfBlocksSkeletonGap = maxNumOfBlocksPerMsg + maxNumOfBlocksPerSync = numOfBlocksSkeletonGap * uint64(maxSizeOfSyncSkeleton-1) + fastSyncPivotGap = uint64(64) + minGapStartFastSync = uint64(128) + + errNoSyncPeer = errors.New("can't find sync peer") + errSkeletonSize = errors.New("fast sync skeleton size wrong") ) type fastSync struct { @@ -88,6 +91,11 @@ func (fs *fastSync) createFetchBlocksTasks(stopBlock *types.Block) ([]*fetchBloc return nil, errors.New("No main skeleton found") } + if len(mainSkeleton) < minSizeOfSyncSkeleton || len(mainSkeleton) > maxSizeOfSyncSkeleton { + fs.peers.ProcessIllegal(fs.mainSyncPeer.ID(), security.LevelMsgIllegal, errSkeletonSize.Error()) + return nil, errSkeletonSize + } + // collect peers that match the skeleton of the primary sync peer fs.msgFetcher.addSyncPeer(fs.mainSyncPeer.ID()) delete(skeletonMap, fs.mainSyncPeer.ID()) diff --git a/netsync/chainmgr/fast_sync_test.go b/netsync/chainmgr/fast_sync_test.go index efd5f5b5..ffacb56a 100644 --- a/netsync/chainmgr/fast_sync_test.go +++ b/netsync/chainmgr/fast_sync_test.go @@ -80,19 +80,19 @@ func TestFastBlockSync(t *testing.T) { os.RemoveAll(tmp) }() - maxNumOfSkeletonPerSync = 10 + maxSizeOfSyncSkeleton = 11 numOfBlocksSkeletonGap = 10 - maxNumOfBlocksPerSync = maxNumOfSkeletonPerSync * maxNumOfSkeletonPerSync + maxNumOfBlocksPerSync = numOfBlocksSkeletonGap * uint64(maxSizeOfSyncSkeleton-1) fastSyncPivotGap = uint64(5) minGapStartFastSync = uint64(6) defer func() { - maxNumOfSkeletonPerSync = 10 + maxSizeOfSyncSkeleton = 11 numOfBlocksSkeletonGap = maxNumOfBlocksPerMsg - maxNumOfBlocksPerSync = maxNumOfSkeletonPerSync * maxNumOfSkeletonPerSync + maxNumOfBlocksPerSync = numOfBlocksSkeletonGap * uint64(maxSizeOfSyncSkeleton-1) fastSyncPivotGap = uint64(64) minGapStartFastSync = uint64(128) - + requireHeadersTimeout = 30 * time.Second }() baseChain := mockBlocks(nil, 300) @@ -139,6 +139,13 @@ func TestFastBlockSync(t *testing.T) { want: baseChain[:5], err: nil, }, + { + syncTimeout: 0 * time.Second, + aBlocks: baseChain[:50], + bBlocks: baseChain[:301], + want: baseChain[:50], + err: errSkeletonSize, + }, } for i, c := range cases { @@ -156,6 +163,7 @@ func TestFastBlockSync(t *testing.T) { a.blockKeeper.syncPeer = a.peers.GetPeer("test node B") a.blockKeeper.fastSync.setSyncPeer(a.blockKeeper.syncPeer) + requireHeadersTimeout = c.syncTimeout if err := a.blockKeeper.fastSync.process(); errors.Root(err) != c.err { t.Errorf("case %d: got %v want %v", i, err, c.err) }