Golang implemented sidechain for Bytom
Révision | b37eb8733855495baf0b3c8a5dccaa6bef1f9eaf (tree) |
---|---|
l'heure | 2019-07-25 15:48:06 |
Auteur | iczc <iczcalan@gmai...> |
Commiter | iczc |
Merge branch 'feature_consensusreward' of https://github.com/Bytom/vapor into feature_consensusreward
@@ -7,18 +7,21 @@ import ( | ||
7 | 7 | |
8 | 8 | "github.com/vapor/errors" |
9 | 9 | "github.com/vapor/netsync/peers" |
10 | + "github.com/vapor/p2p/security" | |
10 | 11 | "github.com/vapor/protocol/bc" |
11 | 12 | "github.com/vapor/protocol/bc/types" |
12 | 13 | ) |
13 | 14 | |
14 | 15 | var ( |
15 | - maxNumOfSkeletonPerSync = uint64(10) | |
16 | - numOfBlocksSkeletonGap = maxNumOfBlocksPerMsg | |
17 | - maxNumOfBlocksPerSync = numOfBlocksSkeletonGap * maxNumOfSkeletonPerSync | |
18 | - fastSyncPivotGap = uint64(64) | |
19 | - minGapStartFastSync = uint64(128) | |
20 | - | |
21 | - errNoSyncPeer = errors.New("can't find sync peer") | |
16 | + minSizeOfSyncSkeleton = 2 | |
17 | + maxSizeOfSyncSkeleton = 11 | |
18 | + numOfBlocksSkeletonGap = maxNumOfBlocksPerMsg | |
19 | + maxNumOfBlocksPerSync = numOfBlocksSkeletonGap * uint64(maxSizeOfSyncSkeleton-1) | |
20 | + fastSyncPivotGap = uint64(64) | |
21 | + minGapStartFastSync = uint64(128) | |
22 | + | |
23 | + errNoSyncPeer = errors.New("can't find sync peer") | |
24 | + errSkeletonSize = errors.New("fast sync skeleton size wrong") | |
22 | 25 | ) |
23 | 26 | |
24 | 27 | type fastSync struct { |
@@ -88,6 +91,11 @@ func (fs *fastSync) createFetchBlocksTasks(stopBlock *types.Block) ([]*fetchBloc | ||
88 | 91 | return nil, errors.New("No main skeleton found") |
89 | 92 | } |
90 | 93 | |
94 | + if len(mainSkeleton) < minSizeOfSyncSkeleton || len(mainSkeleton) > maxSizeOfSyncSkeleton { | |
95 | + fs.peers.ProcessIllegal(fs.mainSyncPeer.ID(), security.LevelMsgIllegal, errSkeletonSize.Error()) | |
96 | + return nil, errSkeletonSize | |
97 | + } | |
98 | + | |
91 | 99 | // collect peers that match the skeleton of the primary sync peer |
92 | 100 | fs.msgFetcher.addSyncPeer(fs.mainSyncPeer.ID()) |
93 | 101 | delete(skeletonMap, fs.mainSyncPeer.ID()) |
@@ -80,19 +80,19 @@ func TestFastBlockSync(t *testing.T) { | ||
80 | 80 | os.RemoveAll(tmp) |
81 | 81 | }() |
82 | 82 | |
83 | - maxNumOfSkeletonPerSync = 10 | |
83 | + maxSizeOfSyncSkeleton = 11 | |
84 | 84 | numOfBlocksSkeletonGap = 10 |
85 | - maxNumOfBlocksPerSync = maxNumOfSkeletonPerSync * maxNumOfSkeletonPerSync | |
85 | + maxNumOfBlocksPerSync = numOfBlocksSkeletonGap * uint64(maxSizeOfSyncSkeleton-1) | |
86 | 86 | fastSyncPivotGap = uint64(5) |
87 | 87 | minGapStartFastSync = uint64(6) |
88 | 88 | |
89 | 89 | defer func() { |
90 | - maxNumOfSkeletonPerSync = 10 | |
90 | + maxSizeOfSyncSkeleton = 11 | |
91 | 91 | numOfBlocksSkeletonGap = maxNumOfBlocksPerMsg |
92 | - maxNumOfBlocksPerSync = maxNumOfSkeletonPerSync * maxNumOfSkeletonPerSync | |
92 | + maxNumOfBlocksPerSync = numOfBlocksSkeletonGap * uint64(maxSizeOfSyncSkeleton-1) | |
93 | 93 | fastSyncPivotGap = uint64(64) |
94 | 94 | minGapStartFastSync = uint64(128) |
95 | - | |
95 | + requireHeadersTimeout = 30 * time.Second | |
96 | 96 | }() |
97 | 97 | |
98 | 98 | baseChain := mockBlocks(nil, 300) |
@@ -139,6 +139,13 @@ func TestFastBlockSync(t *testing.T) { | ||
139 | 139 | want: baseChain[:5], |
140 | 140 | err: nil, |
141 | 141 | }, |
142 | + { | |
143 | + syncTimeout: 0 * time.Second, | |
144 | + aBlocks: baseChain[:50], | |
145 | + bBlocks: baseChain[:301], | |
146 | + want: baseChain[:50], | |
147 | + err: errSkeletonSize, | |
148 | + }, | |
142 | 149 | } |
143 | 150 | |
144 | 151 | for i, c := range cases { |
@@ -156,6 +163,7 @@ func TestFastBlockSync(t *testing.T) { | ||
156 | 163 | a.blockKeeper.syncPeer = a.peers.GetPeer("test node B") |
157 | 164 | a.blockKeeper.fastSync.setSyncPeer(a.blockKeeper.syncPeer) |
158 | 165 | |
166 | + requireHeadersTimeout = c.syncTimeout | |
159 | 167 | if err := a.blockKeeper.fastSync.process(); errors.Root(err) != c.err { |
160 | 168 | t.Errorf("case %d: got %v want %v", i, err, c.err) |
161 | 169 | } |