@@ -39,17 +39,15 @@ import (
39
39
func TestHeaderVerification (t * testing.T ) {
40
40
// Create a simple chain to verify
41
41
var (
42
- testdb = rawdb .NewMemoryDatabase ()
43
- gspec = & Genesis {Config : params .TestChainConfig }
44
- genesis = gspec .MustCommit (testdb )
45
- blocks , _ = GenerateChain (params .TestChainConfig , genesis , ethash .NewFaker (), testdb , 8 , nil )
42
+ gspec = & Genesis {Config : params .TestChainConfig }
43
+ _ , blocks , _ = GenerateChainWithGenesis (gspec , ethash .NewFaker (), 8 , nil )
46
44
)
47
45
headers := make ([]* types.Header , len (blocks ))
48
46
for i , block := range blocks {
49
47
headers [i ] = block .Header ()
50
48
}
51
49
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
52
- chain , _ := NewBlockChain (testdb , nil , gspec , nil , ethash .NewFaker (), vm.Config {}, nil , nil )
50
+ chain , _ := NewBlockChain (rawdb . NewMemoryDatabase () , nil , gspec , nil , ethash .NewFaker (), vm.Config {}, nil , nil )
53
51
defer chain .Stop ()
54
52
55
53
for i := 0 ; i < len (blocks ); i ++ {
@@ -89,72 +87,66 @@ func TestHeaderVerificationForMergingEthash(t *testing.T) { testHeaderVerificati
89
87
// Tests the verification for eth1/2 merging, including pre-merge and post-merge
90
88
func testHeaderVerificationForMerging (t * testing.T , isClique bool ) {
91
89
var (
92
- testdb = rawdb . NewMemoryDatabase ()
90
+ gspec * Genesis
93
91
preBlocks []* types.Block
94
92
postBlocks []* types.Block
95
- runEngine consensus.Engine
96
- genspec * Genesis
93
+ engine consensus.Engine
97
94
merger = consensus .NewMerger (rawdb .NewMemoryDatabase ())
98
95
)
99
96
if isClique {
100
97
var (
101
98
key , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
102
99
addr = crypto .PubkeyToAddress (key .PublicKey )
103
- engine = clique . New ( params .AllCliqueProtocolChanges . Clique , testdb )
100
+ config = * params .AllCliqueProtocolChanges
104
101
)
105
- genspec = & Genesis {
106
- Config : params .AllCliqueProtocolChanges ,
102
+ engine = beacon .New (clique .New (params .AllCliqueProtocolChanges .Clique , rawdb .NewMemoryDatabase ()))
103
+ gspec = & Genesis {
104
+ Config : & config ,
107
105
ExtraData : make ([]byte , 32 + common .AddressLength + crypto .SignatureLength ),
108
106
Alloc : map [common.Address ]GenesisAccount {
109
107
addr : {Balance : big .NewInt (1 )},
110
108
},
111
109
BaseFee : big .NewInt (params .InitialBaseFee ),
112
110
Difficulty : new (big.Int ),
113
111
}
114
- copy (genspec .ExtraData [32 :], addr [:])
115
- genesis := genspec .MustCommit (testdb )
112
+ copy (gspec .ExtraData [32 :], addr [:])
116
113
117
- genEngine := beacon .New (engine )
118
- preBlocks , _ = GenerateChain (params .AllCliqueProtocolChanges , genesis , genEngine , testdb , 8 , nil )
119
114
td := 0
120
- for i , block := range preBlocks {
115
+ gendb , blocks , _ := GenerateChainWithGenesis (gspec , engine , 8 , nil )
116
+ for i , block := range blocks {
121
117
header := block .Header ()
122
118
if i > 0 {
123
- header .ParentHash = preBlocks [i - 1 ].Hash ()
119
+ header .ParentHash = blocks [i - 1 ].Hash ()
124
120
}
125
121
header .Extra = make ([]byte , 32 + crypto .SignatureLength )
126
122
header .Difficulty = big .NewInt (2 )
127
123
128
- sig , _ := crypto .Sign (genEngine .SealHash (header ).Bytes (), key )
124
+ sig , _ := crypto .Sign (engine .SealHash (header ).Bytes (), key )
129
125
copy (header .Extra [len (header .Extra )- crypto .SignatureLength :], sig )
130
- preBlocks [i ] = block .WithSeal (header )
126
+ blocks [i ] = block .WithSeal (header )
127
+
131
128
// calculate td
132
129
td += int (block .Difficulty ().Uint64 ())
133
130
}
134
- config := * params .AllCliqueProtocolChanges
135
- config .TerminalTotalDifficulty = big .NewInt (int64 (td ))
136
- postBlocks , _ = GenerateChain (& config , preBlocks [len (preBlocks )- 1 ], genEngine , testdb , 8 , nil )
137
- runEngine = beacon .New (engine )
138
- genspec .Config = & config
131
+ preBlocks = blocks
132
+ gspec .Config .TerminalTotalDifficulty = big .NewInt (int64 (td ))
133
+ postBlocks , _ = GenerateChain (gspec .Config , preBlocks [len (preBlocks )- 1 ], engine , gendb , 8 , nil )
139
134
} else {
140
- genspec = & Genesis { Config : params .TestChainConfig }
141
- genesis := genspec . MustCommit ( testdb )
142
- genEngine : = beacon .New (ethash .NewFaker ())
135
+ config := * params .TestChainConfig
136
+ gspec = & Genesis { Config : & config }
137
+ engine = beacon .New (ethash .NewFaker ())
143
138
144
- preBlocks , _ = GenerateChain (params .TestChainConfig , genesis , genEngine , testdb , 8 , nil )
145
139
td := 0
140
+ gendb , blocks , _ := GenerateChainWithGenesis (gspec , engine , 8 , nil )
146
141
for _ , block := range preBlocks {
147
142
// calculate td
148
143
td += int (block .Difficulty ().Uint64 ())
149
144
}
150
- config := * params .TestChainConfig
151
- config .TerminalTotalDifficulty = big .NewInt (int64 (td ))
152
- postBlocks , _ = GenerateChain (params .TestChainConfig , preBlocks [len (preBlocks )- 1 ], genEngine , testdb , 8 , nil )
153
-
154
- runEngine = beacon .New (ethash .NewFaker ())
155
- genspec .Config = & config
145
+ preBlocks = blocks
146
+ gspec .Config .TerminalTotalDifficulty = big .NewInt (int64 (td ))
147
+ postBlocks , _ = GenerateChain (gspec .Config , preBlocks [len (preBlocks )- 1 ], engine , gendb , 8 , nil )
156
148
}
157
-
149
+ // Assemble header batch
158
150
preHeaders := make ([]* types.Header , len (preBlocks ))
159
151
for i , block := range preBlocks {
160
152
preHeaders [i ] = block .Header ()
@@ -170,12 +162,12 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
170
162
t .Logf ("Log header after the merging %d: %v" , block .NumberU64 (), string (blob ))
171
163
}
172
164
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
173
- chain , _ := NewBlockChain (testdb , nil , genspec , nil , runEngine , vm.Config {}, nil , nil )
165
+ chain , _ := NewBlockChain (rawdb . NewMemoryDatabase () , nil , gspec , nil , engine , vm.Config {}, nil , nil )
174
166
defer chain .Stop ()
175
167
176
168
// Verify the blocks before the merging
177
169
for i := 0 ; i < len (preBlocks ); i ++ {
178
- _ , results := runEngine .VerifyHeaders (chain , []* types.Header {preHeaders [i ]}, []bool {true })
170
+ _ , results := engine .VerifyHeaders (chain , []* types.Header {preHeaders [i ]}, []bool {true })
179
171
// Wait for the verification result
180
172
select {
181
173
case result := <- results :
@@ -200,7 +192,7 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
200
192
201
193
// Verify the blocks after the merging
202
194
for i := 0 ; i < len (postBlocks ); i ++ {
203
- _ , results := runEngine .VerifyHeaders (chain , []* types.Header {postHeaders [i ]}, []bool {true })
195
+ _ , results := engine .VerifyHeaders (chain , []* types.Header {postHeaders [i ]}, []bool {true })
204
196
// Wait for the verification result
205
197
select {
206
198
case result := <- results :
@@ -232,7 +224,7 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
232
224
headers = append (headers , block .Header ())
233
225
seals = append (seals , true )
234
226
}
235
- _ , results := runEngine .VerifyHeaders (chain , headers , seals )
227
+ _ , results := engine .VerifyHeaders (chain , headers , seals )
236
228
for i := 0 ; i < len (headers ); i ++ {
237
229
select {
238
230
case result := <- results :
@@ -259,10 +251,8 @@ func TestHeaderConcurrentVerification32(t *testing.T) { testHeaderConcurrentVeri
259
251
func testHeaderConcurrentVerification (t * testing.T , threads int ) {
260
252
// Create a simple chain to verify
261
253
var (
262
- testdb = rawdb .NewMemoryDatabase ()
263
- gspec = & Genesis {Config : params .TestChainConfig }
264
- genesis = gspec .MustCommit (testdb )
265
- blocks , _ = GenerateChain (params .TestChainConfig , genesis , ethash .NewFaker (), testdb , 8 , nil )
254
+ gspec = & Genesis {Config : params .TestChainConfig }
255
+ _ , blocks , _ = GenerateChainWithGenesis (gspec , ethash .NewFaker (), 8 , nil )
266
256
)
267
257
headers := make ([]* types.Header , len (blocks ))
268
258
seals := make ([]bool , len (blocks ))
@@ -281,11 +271,11 @@ func testHeaderConcurrentVerification(t *testing.T, threads int) {
281
271
var results <- chan error
282
272
283
273
if valid {
284
- chain , _ := NewBlockChain (testdb , nil , gspec , nil , ethash .NewFaker (), vm.Config {}, nil , nil )
274
+ chain , _ := NewBlockChain (rawdb . NewMemoryDatabase () , nil , gspec , nil , ethash .NewFaker (), vm.Config {}, nil , nil )
285
275
_ , results = chain .engine .VerifyHeaders (chain , headers , seals )
286
276
chain .Stop ()
287
277
} else {
288
- chain , _ := NewBlockChain (testdb , nil , gspec , nil , ethash .NewFakeFailer (uint64 (len (headers )- 1 )), vm.Config {}, nil , nil )
278
+ chain , _ := NewBlockChain (rawdb . NewMemoryDatabase () , nil , gspec , nil , ethash .NewFakeFailer (uint64 (len (headers )- 1 )), vm.Config {}, nil , nil )
289
279
_ , results = chain .engine .VerifyHeaders (chain , headers , seals )
290
280
chain .Stop ()
291
281
}
@@ -331,10 +321,8 @@ func TestHeaderConcurrentAbortion32(t *testing.T) { testHeaderConcurrentAbortion
331
321
func testHeaderConcurrentAbortion (t * testing.T , threads int ) {
332
322
// Create a simple chain to verify
333
323
var (
334
- testdb = rawdb .NewMemoryDatabase ()
335
- gspec = & Genesis {Config : params .TestChainConfig }
336
- genesis = gspec .MustCommit (testdb )
337
- blocks , _ = GenerateChain (params .TestChainConfig , genesis , ethash .NewFaker (), testdb , 1024 , nil )
324
+ gspec = & Genesis {Config : params .TestChainConfig }
325
+ _ , blocks , _ = GenerateChainWithGenesis (gspec , ethash .NewFaker (), 1024 , nil )
338
326
)
339
327
headers := make ([]* types.Header , len (blocks ))
340
328
seals := make ([]bool , len (blocks ))
@@ -348,7 +336,7 @@ func testHeaderConcurrentAbortion(t *testing.T, threads int) {
348
336
defer runtime .GOMAXPROCS (old )
349
337
350
338
// Start the verifications and immediately abort
351
- chain , _ := NewBlockChain (testdb , nil , gspec , nil , ethash .NewFakeDelayer (time .Millisecond ), vm.Config {}, nil , nil )
339
+ chain , _ := NewBlockChain (rawdb . NewMemoryDatabase () , nil , gspec , nil , ethash .NewFakeDelayer (time .Millisecond ), vm.Config {}, nil , nil )
352
340
defer chain .Stop ()
353
341
354
342
abort , results := chain .engine .VerifyHeaders (chain , headers , seals )
0 commit comments