@@ -39,17 +39,15 @@ import (
3939func TestHeaderVerification (t * testing.T ) {
4040 // Create a simple chain to verify
4141 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 )
4644 )
4745 headers := make ([]* types.Header , len (blocks ))
4846 for i , block := range blocks {
4947 headers [i ] = block .Header ()
5048 }
5149 // 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 )
5351 defer chain .Stop ()
5452
5553 for i := 0 ; i < len (blocks ); i ++ {
@@ -89,72 +87,66 @@ func TestHeaderVerificationForMergingEthash(t *testing.T) { testHeaderVerificati
8987// Tests the verification for eth1/2 merging, including pre-merge and post-merge
9088func testHeaderVerificationForMerging (t * testing.T , isClique bool ) {
9189 var (
92- testdb = rawdb . NewMemoryDatabase ()
90+ gspec * Genesis
9391 preBlocks []* types.Block
9492 postBlocks []* types.Block
95- runEngine consensus.Engine
96- genspec * Genesis
93+ engine consensus.Engine
9794 merger = consensus .NewMerger (rawdb .NewMemoryDatabase ())
9895 )
9996 if isClique {
10097 var (
10198 key , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
10299 addr = crypto .PubkeyToAddress (key .PublicKey )
103- engine = clique . New ( params .AllCliqueProtocolChanges . Clique , testdb )
100+ config = * params .AllCliqueProtocolChanges
104101 )
105- genspec = & Genesis {
106- Config : params .AllCliqueProtocolChanges ,
102+ engine = beacon .New (clique .New (params .AllCliqueProtocolChanges .Clique , rawdb .NewMemoryDatabase ()))
103+ gspec = & Genesis {
104+ Config : & config ,
107105 ExtraData : make ([]byte , 32 + common .AddressLength + crypto .SignatureLength ),
108106 Alloc : map [common.Address ]GenesisAccount {
109107 addr : {Balance : big .NewInt (1 )},
110108 },
111109 BaseFee : big .NewInt (params .InitialBaseFee ),
112110 Difficulty : new (big.Int ),
113111 }
114- copy (genspec .ExtraData [32 :], addr [:])
115- genesis := genspec .MustCommit (testdb )
112+ copy (gspec .ExtraData [32 :], addr [:])
116113
117- genEngine := beacon .New (engine )
118- preBlocks , _ = GenerateChain (params .AllCliqueProtocolChanges , genesis , genEngine , testdb , 8 , nil )
119114 td := 0
120- for i , block := range preBlocks {
115+ genDb , blocks , _ := GenerateChainWithGenesis (gspec , engine , 8 , nil )
116+ for i , block := range blocks {
121117 header := block .Header ()
122118 if i > 0 {
123- header .ParentHash = preBlocks [i - 1 ].Hash ()
119+ header .ParentHash = blocks [i - 1 ].Hash ()
124120 }
125121 header .Extra = make ([]byte , 32 + crypto .SignatureLength )
126122 header .Difficulty = big .NewInt (2 )
127123
128- sig , _ := crypto .Sign (genEngine .SealHash (header ).Bytes (), key )
124+ sig , _ := crypto .Sign (engine .SealHash (header ).Bytes (), key )
129125 copy (header .Extra [len (header .Extra )- crypto .SignatureLength :], sig )
130- preBlocks [i ] = block .WithSeal (header )
126+ blocks [i ] = block .WithSeal (header )
127+
131128 // calculate td
132129 td += int (block .Difficulty ().Uint64 ())
133130 }
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 )
139134 } 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 ())
143138
144- preBlocks , _ = GenerateChain (params .TestChainConfig , genesis , genEngine , testdb , 8 , nil )
145139 td := 0
140+ genDb , blocks , _ := GenerateChainWithGenesis (gspec , engine , 8 , nil )
146141 for _ , block := range preBlocks {
147142 // calculate td
148143 td += int (block .Difficulty ().Uint64 ())
149144 }
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 )
156148 }
157-
149+ // Assemble header batch
158150 preHeaders := make ([]* types.Header , len (preBlocks ))
159151 for i , block := range preBlocks {
160152 preHeaders [i ] = block .Header ()
@@ -170,12 +162,12 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
170162 t .Logf ("Log header after the merging %d: %v" , block .NumberU64 (), string (blob ))
171163 }
172164 // 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 )
174166 defer chain .Stop ()
175167
176168 // Verify the blocks before the merging
177169 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 })
179171 // Wait for the verification result
180172 select {
181173 case result := <- results :
@@ -200,7 +192,7 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
200192
201193 // Verify the blocks after the merging
202194 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 })
204196 // Wait for the verification result
205197 select {
206198 case result := <- results :
@@ -232,7 +224,7 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
232224 headers = append (headers , block .Header ())
233225 seals = append (seals , true )
234226 }
235- _ , results := runEngine .VerifyHeaders (chain , headers , seals )
227+ _ , results := engine .VerifyHeaders (chain , headers , seals )
236228 for i := 0 ; i < len (headers ); i ++ {
237229 select {
238230 case result := <- results :
@@ -259,10 +251,8 @@ func TestHeaderConcurrentVerification32(t *testing.T) { testHeaderConcurrentVeri
259251func testHeaderConcurrentVerification (t * testing.T , threads int ) {
260252 // Create a simple chain to verify
261253 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 )
266256 )
267257 headers := make ([]* types.Header , len (blocks ))
268258 seals := make ([]bool , len (blocks ))
@@ -281,11 +271,11 @@ func testHeaderConcurrentVerification(t *testing.T, threads int) {
281271 var results <- chan error
282272
283273 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 )
285275 _ , results = chain .engine .VerifyHeaders (chain , headers , seals )
286276 chain .Stop ()
287277 } 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 )
289279 _ , results = chain .engine .VerifyHeaders (chain , headers , seals )
290280 chain .Stop ()
291281 }
@@ -331,10 +321,8 @@ func TestHeaderConcurrentAbortion32(t *testing.T) { testHeaderConcurrentAbortion
331321func testHeaderConcurrentAbortion (t * testing.T , threads int ) {
332322 // Create a simple chain to verify
333323 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 )
338326 )
339327 headers := make ([]* types.Header , len (blocks ))
340328 seals := make ([]bool , len (blocks ))
@@ -348,7 +336,7 @@ func testHeaderConcurrentAbortion(t *testing.T, threads int) {
348336 defer runtime .GOMAXPROCS (old )
349337
350338 // 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 )
352340 defer chain .Stop ()
353341
354342 abort , results := chain .engine .VerifyHeaders (chain , headers , seals )
0 commit comments