@ -39,17 +39,15 @@ import (
func TestHeaderVerification ( t * testing . T ) {
func TestHeaderVerification ( t * testing . T ) {
// Create a simple chain to verify
// Create a simple chain to verify
var (
var (
testdb = rawdb . NewMemoryDatabase ( )
gspec = & Genesis { Config : params . TestChainConfig }
gspec = & Genesis { Config : params . TestChainConfig }
_ , blocks , _ = GenerateChainWithGenesis ( gspec , ethash . NewFaker ( ) , 8 , nil )
genesis = gspec . MustCommit ( testdb )
blocks , _ = GenerateChain ( params . TestChainConfig , genesis , ethash . NewFaker ( ) , testdb , 8 , nil )
)
)
headers := make ( [ ] * types . Header , len ( blocks ) )
headers := make ( [ ] * types . Header , len ( blocks ) )
for i , block := range blocks {
for i , block := range blocks {
headers [ i ] = block . Header ( )
headers [ i ] = block . Header ( )
}
}
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
chain , _ := NewBlockChain ( testdb , nil , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
chain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
defer chain . Stop ( )
defer chain . Stop ( )
for i := 0 ; i < len ( blocks ) ; i ++ {
for i := 0 ; i < len ( blocks ) ; i ++ {
@ -89,21 +87,21 @@ func TestHeaderVerificationForMergingEthash(t *testing.T) { testHeaderVerificati
// Tests the verification for eth1/2 merging, including pre-merge and post-merge
// Tests the verification for eth1/2 merging, including pre-merge and post-merge
func testHeaderVerificationForMerging ( t * testing . T , isClique bool ) {
func testHeaderVerificationForMerging ( t * testing . T , isClique bool ) {
var (
var (
testdb = rawdb . NewMemoryDatabase ( )
gspec * Genesis
preBlocks [ ] * types . Block
preBlocks [ ] * types . Block
postBlocks [ ] * types . Block
postBlocks [ ] * types . Block
runEngine consensus . Engine
engine consensus . Engine
genspec * Genesis
merger = consensus . NewMerger ( rawdb . NewMemoryDatabase ( ) )
merger = consensus . NewMerger ( rawdb . NewMemoryDatabase ( ) )
)
)
if isClique {
if isClique {
var (
var (
key , _ = crypto . HexToECDSA ( "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
key , _ = crypto . HexToECDSA ( "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
addr = crypto . PubkeyToAddress ( key . PublicKey )
addr = crypto . PubkeyToAddress ( key . PublicKey )
engine = clique . New ( params . AllCliqueProtocolChanges . Clique , testdb )
config = * params . AllCliqueProtocolChanges
)
)
genspec = & Genesis {
engine = beacon . New ( clique . New ( params . AllCliqueProtocolChanges . Clique , rawdb . NewMemoryDatabase ( ) ) )
Config : params . AllCliqueProtocolChanges ,
gspec = & Genesis {
Config : & config ,
ExtraData : make ( [ ] byte , 32 + common . AddressLength + crypto . SignatureLength ) ,
ExtraData : make ( [ ] byte , 32 + common . AddressLength + crypto . SignatureLength ) ,
Alloc : map [ common . Address ] GenesisAccount {
Alloc : map [ common . Address ] GenesisAccount {
addr : { Balance : big . NewInt ( 1 ) } ,
addr : { Balance : big . NewInt ( 1 ) } ,
@ -111,50 +109,44 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
BaseFee : big . NewInt ( params . InitialBaseFee ) ,
BaseFee : big . NewInt ( params . InitialBaseFee ) ,
Difficulty : new ( big . Int ) ,
Difficulty : new ( big . Int ) ,
}
}
copy ( genspec . ExtraData [ 32 : ] , addr [ : ] )
copy ( gspec . ExtraData [ 32 : ] , addr [ : ] )
genesis := genspec . MustCommit ( testdb )
genEngine := beacon . New ( engine )
preBlocks , _ = GenerateChain ( params . AllCliqueProtocolChanges , genesis , genEngine , testdb , 8 , nil )
td := 0
td := 0
for i , block := range preBlocks {
genDb , blocks , _ := GenerateChainWithGenesis ( gspec , engine , 8 , nil )
for i , block := range blocks {
header := block . Header ( )
header := block . Header ( )
if i > 0 {
if i > 0 {
header . ParentHash = preB locks[ i - 1 ] . Hash ( )
header . ParentHash = b locks[ i - 1 ] . Hash ( )
}
}
header . Extra = make ( [ ] byte , 32 + crypto . SignatureLength )
header . Extra = make ( [ ] byte , 32 + crypto . SignatureLength )
header . Difficulty = big . NewInt ( 2 )
header . Difficulty = big . NewInt ( 2 )
sig , _ := crypto . Sign ( g enE ngine. SealHash ( header ) . Bytes ( ) , key )
sig , _ := crypto . Sign ( engine . SealHash ( header ) . Bytes ( ) , key )
copy ( header . Extra [ len ( header . Extra ) - crypto . SignatureLength : ] , sig )
copy ( header . Extra [ len ( header . Extra ) - crypto . SignatureLength : ] , sig )
preBlocks [ i ] = block . WithSeal ( header )
blocks [ i ] = block . WithSeal ( header )
// calculate td
// calculate td
td += int ( block . Difficulty ( ) . Uint64 ( ) )
td += int ( block . Difficulty ( ) . Uint64 ( ) )
}
}
config := * params . AllCliqueProtocolChanges
preBlocks = blocks
config . TerminalTotalDifficulty = big . NewInt ( int64 ( td ) )
gspec . Config . TerminalTotalDifficulty = big . NewInt ( int64 ( td ) )
postBlocks , _ = GenerateChain ( & config , preBlocks [ len ( preBlocks ) - 1 ] , genEngine , testdb , 8 , nil )
postBlocks , _ = GenerateChain ( gspec . Config , preBlocks [ len ( preBlocks ) - 1 ] , engine , genDb , 8 , nil )
runEngine = beacon . New ( engine )
genspec . Config = & config
} else {
} else {
genspec = & Genesis { Config : params . TestChainConfig }
config := * params . TestChainConfig
genesis := genspec . MustCommit ( testdb )
gspec = & Genesis { Config : & config }
genEngine : = beacon . New ( ethash . NewFaker ( ) )
engine = beacon . New ( ethash . NewFaker ( ) )
preBlocks , _ = GenerateChain ( params . TestChainConfig , genesis , genEngine , testdb , 8 , nil )
td := 0
td := 0
genDb , blocks , _ := GenerateChainWithGenesis ( gspec , engine , 8 , nil )
for _ , block := range preBlocks {
for _ , block := range preBlocks {
// calculate td
// calculate td
td += int ( block . Difficulty ( ) . Uint64 ( ) )
td += int ( block . Difficulty ( ) . Uint64 ( ) )
}
}
config := * params . TestChainConfig
preBlocks = blocks
config . TerminalTotalDifficulty = big . NewInt ( int64 ( td ) )
gspec . Config . TerminalTotalDifficulty = big . NewInt ( int64 ( td ) )
postBlocks , _ = GenerateChain ( params . TestChainConfig , preBlocks [ len ( preBlocks ) - 1 ] , genEngine , testdb , 8 , nil )
postBlocks , _ = GenerateChain ( gspec . Config , preBlocks [ len ( preBlocks ) - 1 ] , engine , genDb , 8 , nil )
runEngine = beacon . New ( ethash . NewFaker ( ) )
genspec . Config = & config
}
}
// Assemble header batch
preHeaders := make ( [ ] * types . Header , len ( preBlocks ) )
preHeaders := make ( [ ] * types . Header , len ( preBlocks ) )
for i , block := range preBlocks {
for i , block := range preBlocks {
preHeaders [ i ] = block . Header ( )
preHeaders [ i ] = block . Header ( )
@ -170,12 +162,12 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
t . Logf ( "Log header after the merging %d: %v" , block . NumberU64 ( ) , string ( blob ) )
t . Logf ( "Log header after the merging %d: %v" , block . NumberU64 ( ) , string ( blob ) )
}
}
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
chain , _ := NewBlockChain ( testdb , nil , gen spec , nil , runE ngine, vm . Config { } , nil , nil )
chain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , e ngine, vm . Config { } , nil , nil )
defer chain . Stop ( )
defer chain . Stop ( )
// Verify the blocks before the merging
// Verify the blocks before the merging
for i := 0 ; i < len ( preBlocks ) ; i ++ {
for i := 0 ; i < len ( preBlocks ) ; i ++ {
_ , results := runE ngine. VerifyHeaders ( chain , [ ] * types . Header { preHeaders [ i ] } , [ ] bool { true } )
_ , results := e ngine. VerifyHeaders ( chain , [ ] * types . Header { preHeaders [ i ] } , [ ] bool { true } )
// Wait for the verification result
// Wait for the verification result
select {
select {
case result := <- results :
case result := <- results :
@ -200,7 +192,7 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
// Verify the blocks after the merging
// Verify the blocks after the merging
for i := 0 ; i < len ( postBlocks ) ; i ++ {
for i := 0 ; i < len ( postBlocks ) ; i ++ {
_ , results := runE ngine. VerifyHeaders ( chain , [ ] * types . Header { postHeaders [ i ] } , [ ] bool { true } )
_ , results := e ngine. VerifyHeaders ( chain , [ ] * types . Header { postHeaders [ i ] } , [ ] bool { true } )
// Wait for the verification result
// Wait for the verification result
select {
select {
case result := <- results :
case result := <- results :
@ -232,7 +224,7 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
headers = append ( headers , block . Header ( ) )
headers = append ( headers , block . Header ( ) )
seals = append ( seals , true )
seals = append ( seals , true )
}
}
_ , results := runE ngine. VerifyHeaders ( chain , headers , seals )
_ , results := e ngine. VerifyHeaders ( chain , headers , seals )
for i := 0 ; i < len ( headers ) ; i ++ {
for i := 0 ; i < len ( headers ) ; i ++ {
select {
select {
case result := <- results :
case result := <- results :
@ -259,10 +251,8 @@ func TestHeaderConcurrentVerification32(t *testing.T) { testHeaderConcurrentVeri
func testHeaderConcurrentVerification ( t * testing . T , threads int ) {
func testHeaderConcurrentVerification ( t * testing . T , threads int ) {
// Create a simple chain to verify
// Create a simple chain to verify
var (
var (
testdb = rawdb . NewMemoryDatabase ( )
gspec = & Genesis { Config : params . TestChainConfig }
gspec = & Genesis { Config : params . TestChainConfig }
_ , blocks , _ = GenerateChainWithGenesis ( gspec , ethash . NewFaker ( ) , 8 , nil )
genesis = gspec . MustCommit ( testdb )
blocks , _ = GenerateChain ( params . TestChainConfig , genesis , ethash . NewFaker ( ) , testdb , 8 , nil )
)
)
headers := make ( [ ] * types . Header , len ( blocks ) )
headers := make ( [ ] * types . Header , len ( blocks ) )
seals := make ( [ ] bool , len ( blocks ) )
seals := make ( [ ] bool , len ( blocks ) )
@ -281,11 +271,11 @@ func testHeaderConcurrentVerification(t *testing.T, threads int) {
var results <- chan error
var results <- chan error
if valid {
if valid {
chain , _ := NewBlockChain ( testdb , nil , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
chain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
_ , results = chain . engine . VerifyHeaders ( chain , headers , seals )
_ , results = chain . engine . VerifyHeaders ( chain , headers , seals )
chain . Stop ( )
chain . Stop ( )
} else {
} else {
chain , _ := NewBlockChain ( testdb , nil , gspec , nil , ethash . NewFakeFailer ( uint64 ( len ( headers ) - 1 ) ) , vm . Config { } , nil , nil )
chain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , ethash . NewFakeFailer ( uint64 ( len ( headers ) - 1 ) ) , vm . Config { } , nil , nil )
_ , results = chain . engine . VerifyHeaders ( chain , headers , seals )
_ , results = chain . engine . VerifyHeaders ( chain , headers , seals )
chain . Stop ( )
chain . Stop ( )
}
}
@ -331,10 +321,8 @@ func TestHeaderConcurrentAbortion32(t *testing.T) { testHeaderConcurrentAbortion
func testHeaderConcurrentAbortion ( t * testing . T , threads int ) {
func testHeaderConcurrentAbortion ( t * testing . T , threads int ) {
// Create a simple chain to verify
// Create a simple chain to verify
var (
var (
testdb = rawdb . NewMemoryDatabase ( )
gspec = & Genesis { Config : params . TestChainConfig }
gspec = & Genesis { Config : params . TestChainConfig }
_ , blocks , _ = GenerateChainWithGenesis ( gspec , ethash . NewFaker ( ) , 1024 , nil )
genesis = gspec . MustCommit ( testdb )
blocks , _ = GenerateChain ( params . TestChainConfig , genesis , ethash . NewFaker ( ) , testdb , 1024 , nil )
)
)
headers := make ( [ ] * types . Header , len ( blocks ) )
headers := make ( [ ] * types . Header , len ( blocks ) )
seals := make ( [ ] bool , len ( blocks ) )
seals := make ( [ ] bool , len ( blocks ) )
@ -348,7 +336,7 @@ func testHeaderConcurrentAbortion(t *testing.T, threads int) {
defer runtime . GOMAXPROCS ( old )
defer runtime . GOMAXPROCS ( old )
// Start the verifications and immediately abort
// Start the verifications and immediately abort
chain , _ := NewBlockChain ( testdb , nil , gspec , nil , ethash . NewFakeDelayer ( time . Millisecond ) , vm . Config { } , nil , nil )
chain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , ethash . NewFakeDelayer ( time . Millisecond ) , vm . Config { } , nil , nil )
defer chain . Stop ( )
defer chain . Stop ( )
abort , results := chain . engine . VerifyHeaders ( chain , headers , seals )
abort , results := chain . engine . VerifyHeaders ( chain , headers , seals )