OSDN Git Service

Add skeleton size validity check (#354)
[bytom/vapor.git] / netsync / chainmgr / fast_sync.go
index 06c67c8..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())
@@ -131,7 +139,7 @@ func (fs *fastSync) process() error {
        var wg sync.WaitGroup
        wg.Add(2)
        go fs.msgFetcher.parallelFetchBlocks(tasks, downloadNotifyCh, processStopCh, &wg)
-       go fs.blockProcessor.process(downloadNotifyCh, processStopCh, &wg)
+       go fs.blockProcessor.process(downloadNotifyCh, processStopCh, tasks[0].startHeader.Height, &wg)
        wg.Wait()
        fs.msgFetcher.resetParameter()
        log.WithFields(log.Fields{"module": logModule, "height": fs.chain.BestBlockHeight()}).Info("fast sync complete")