OSDN Git Service

Add skeleton size validity check (#354)
authoryahtoo <yahtoo.ma@gmail.com>
Thu, 25 Jul 2019 06:33:33 +0000 (14:33 +0800)
committerPaladz <yzhu101@uottawa.ca>
Thu, 25 Jul 2019 06:33:33 +0000 (14:33 +0800)
* Add skeleton size validity check

* Add max size check

* Add peer illegal process

netsync/chainmgr/fast_sync.go
netsync/chainmgr/fast_sync_test.go

index ed426b7..550d2df 100644 (file)
@@ -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())
index efd5f5b..ffacb56 100644 (file)
@@ -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)
                }