@ -22,6 +22,7 @@ import (
"math/big"
"math/rand"
"os"
"path"
"sync"
"testing"
"time"
@ -61,7 +62,7 @@ func newCanonical(engine consensus.Engine, n int, full bool, scheme string) (eth
}
)
// Initialize a fresh chain with only a genesis block
blockchain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil , nil )
blockchain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil )
// Create and inject the requested chain
if n == 0 {
@ -159,17 +160,18 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
}
return err
}
statedb , err := state . New ( blockchain . GetBlockByHash ( block . ParentHash ( ) ) . Root ( ) , blockchain . stateCache , nil )
statedb , err := state . New ( blockchain . GetBlockByHash ( block . ParentHash ( ) ) . Root ( ) , blockchain . statedb )
if err != nil {
return err
}
receipts , _ , usedGa s , err := blockchain . processor . Process ( block , statedb , vm . Config { } )
res , err := blockchain . processor . Process ( block , statedb , vm . Config { } )
if err != nil {
blockchain . reportBlock ( block , receipt s , err )
blockchain . reportBlock ( block , res , err )
return err
}
if err = blockchain . validator . ValidateState ( block , statedb , receipts , usedGas , false ) ; err != nil {
blockchain . reportBlock ( block , receipts , err )
err = blockchain . validator . ValidateState ( block , statedb , res , false )
if err != nil {
blockchain . reportBlock ( block , res , err )
return err
}
@ -762,7 +764,7 @@ func testFastVsFullChains(t *testing.T, scheme string) {
} )
// Import the chain as an archive node for the comparison baseline
archiveDb := rawdb . NewMemoryDatabase ( )
archive , _ := NewBlockChain ( archiveDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
archive , _ := NewBlockChain ( archiveDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer archive . Stop ( )
if n , err := archive . InsertChain ( blocks ) ; err != nil {
@ -770,7 +772,7 @@ func testFastVsFullChains(t *testing.T, scheme string) {
}
// Fast import the chain as a non-archive node to test
fastDb := rawdb . NewMemoryDatabase ( )
fast , _ := NewBlockChain ( fastDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
fast , _ := NewBlockChain ( fastDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer fast . Stop ( )
headers := make ( [ ] * types . Header , len ( blocks ) )
@ -790,7 +792,7 @@ func testFastVsFullChains(t *testing.T, scheme string) {
}
defer ancientDb . Close ( )
ancient , _ := NewBlockChain ( ancientDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
ancient , _ := NewBlockChain ( ancientDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer ancient . Stop ( )
if n , err := ancient . InsertHeaderChain ( headers ) ; err != nil {
@ -910,7 +912,7 @@ func testLightVsFastVsFullChainHeads(t *testing.T, scheme string) {
archiveCaching . TrieDirtyDisabled = true
archiveCaching . StateScheme = scheme
archive , _ := NewBlockChain ( archiveDb , & archiveCaching , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
archive , _ := NewBlockChain ( archiveDb , & archiveCaching , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
if n , err := archive . InsertChain ( blocks ) ; err != nil {
t . Fatalf ( "failed to process block %d: %v" , n , err )
}
@ -923,7 +925,7 @@ func testLightVsFastVsFullChainHeads(t *testing.T, scheme string) {
// Import the chain as a non-archive node and ensure all pointers are updated
fastDb := makeDb ( )
defer fastDb . Close ( )
fast , _ := NewBlockChain ( fastDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
fast , _ := NewBlockChain ( fastDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer fast . Stop ( )
headers := make ( [ ] * types . Header , len ( blocks ) )
@ -943,7 +945,7 @@ func testLightVsFastVsFullChainHeads(t *testing.T, scheme string) {
// Import the chain as a ancient-first node and ensure all pointers are updated
ancientDb := makeDb ( )
defer ancientDb . Close ( )
ancient , _ := NewBlockChain ( ancientDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
ancient , _ := NewBlockChain ( ancientDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer ancient . Stop ( )
if n , err := ancient . InsertHeaderChain ( headers ) ; err != nil {
@ -962,7 +964,7 @@ func testLightVsFastVsFullChainHeads(t *testing.T, scheme string) {
// Import the chain as a light node and ensure all pointers are updated
lightDb := makeDb ( )
defer lightDb . Close ( )
light , _ := NewBlockChain ( lightDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
light , _ := NewBlockChain ( lightDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
if n , err := light . InsertHeaderChain ( headers ) ; err != nil {
t . Fatalf ( "failed to insert header %d: %v" , n , err )
}
@ -1035,7 +1037,7 @@ func testChainTxReorgs(t *testing.T, scheme string) {
} )
// Import the chain. This runs all block validation rules.
db := rawdb . NewMemoryDatabase ( )
blockchain , _ := NewBlockChain ( db , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
blockchain , _ := NewBlockChain ( db , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
if i , err := blockchain . InsertChain ( chain ) ; err != nil {
t . Fatalf ( "failed to insert original chain[%d]: %v" , i , err )
}
@ -1109,7 +1111,7 @@ func testLogReorgs(t *testing.T, scheme string) {
signer = types . LatestSigner ( gspec . Config )
)
blockchain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
blockchain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer blockchain . Stop ( )
rmLogsCh := make ( chan RemovedLogsEvent )
@ -1165,7 +1167,7 @@ func testLogRebirth(t *testing.T, scheme string) {
gspec = & Genesis { Config : params . TestChainConfig , Alloc : types . GenesisAlloc { addr1 : { Balance : big . NewInt ( 10000000000000000 ) } } }
signer = types . LatestSigner ( gspec . Config )
engine = ethash . NewFaker ( )
blockchain , _ = NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil , nil )
blockchain , _ = NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil )
)
defer blockchain . Stop ( )
@ -1246,7 +1248,7 @@ func testSideLogRebirth(t *testing.T, scheme string) {
addr1 = crypto . PubkeyToAddress ( key1 . PublicKey )
gspec = & Genesis { Config : params . TestChainConfig , Alloc : types . GenesisAlloc { addr1 : { Balance : big . NewInt ( 10000000000000000 ) } } }
signer = types . LatestSigner ( gspec . Config )
blockchain , _ = NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
blockchain , _ = NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
)
defer blockchain . Stop ( )
@ -1265,7 +1267,7 @@ func testSideLogRebirth(t *testing.T, scheme string) {
}
checkLogEvents ( t , newLogCh , rmLogsCh , 0 , 0 )
// Generate side chain with lower difficulty
// Generate side chain with lower difficulty, after the merge, the chain will be accepted even if it is lower difficulty
genDb , sideChain , _ := GenerateChainWithGenesis ( gspec , ethash . NewFaker ( ) , 2 , func ( i int , gen * BlockGen ) {
if i == 1 {
tx , err := types . SignTx ( types . NewContractCreation ( gen . TxNonce ( addr1 ) , new ( big . Int ) , 1000000 , gen . header . BaseFee , logCode ) , signer , key1 )
@ -1278,14 +1280,14 @@ func testSideLogRebirth(t *testing.T, scheme string) {
if _ , err := blockchain . InsertChain ( sideChain ) ; err != nil {
t . Fatalf ( "failed to insert forked chain: %v" , err )
}
checkLogEvents ( t , newLogCh , rmLogsCh , 0 , 0 )
checkLogEvents ( t , newLogCh , rmLogsCh , 1 , 0 )
// Generate a new block based on side chain.
// Generate a new block based on side chain. Should not emit any events anymore.
newBlocks , _ := GenerateChain ( gspec . Config , sideChain [ len ( sideChain ) - 1 ] , ethash . NewFaker ( ) , genDb , 1 , func ( i int , gen * BlockGen ) { } )
if _ , err := blockchain . InsertChain ( newBlocks ) ; err != nil {
t . Fatalf ( "failed to insert forked chain: %v" , err )
}
checkLogEvents ( t , newLogCh , rmLogsCh , 1 , 0 )
checkLogEvents ( t , newLogCh , rmLogsCh , 0 , 0 )
}
func checkLogEvents ( t * testing . T , logsCh <- chan [ ] * types . Log , rmLogsCh <- chan RemovedLogsEvent , wantNew , wantRemoved int ) {
@ -1345,7 +1347,7 @@ func testReorgSideEvent(t *testing.T, scheme string) {
}
signer = types . LatestSigner ( gspec . Config )
)
blockchain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
blockchain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer blockchain . Stop ( )
_ , chain , _ := GenerateChainWithGenesis ( gspec , ethash . NewFaker ( ) , 3 , func ( i int , gen * BlockGen ) { } )
@ -1369,15 +1371,10 @@ func testReorgSideEvent(t *testing.T, scheme string) {
t . Fatalf ( "failed to insert chain: %v" , err )
}
// first two block of the secondary chain are for a brief moment considered
// side chains because up to that point the first one is considered the
// heavier chain.
expectedSideHashes := map [ common . Hash ] bool {
replacementBlocks [ 0 ] . Hash ( ) : true ,
replacementBlocks [ 1 ] . Hash ( ) : true ,
chain [ 0 ] . Hash ( ) : true ,
chain [ 1 ] . Hash ( ) : true ,
chain [ 2 ] . Hash ( ) : true ,
chain [ 0 ] . Hash ( ) : true ,
chain [ 1 ] . Hash ( ) : true ,
chain [ 2 ] . Hash ( ) : true ,
}
i := 0
@ -1402,7 +1399,7 @@ done:
timeout . Reset ( timeoutDura )
case <- timeout . C :
t . Fatal ( "Timeout. Possibly not all blocks were triggered for sideevent" )
t . Fatalf ( "Timeout. Possibly not all blocks were triggered for sideevent: %v " , i )
}
}
@ -1529,7 +1526,7 @@ func testEIP155Transition(t *testing.T, scheme string) {
}
} )
blockchain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
blockchain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer blockchain . Stop ( )
if _ , err := blockchain . InsertChain ( blocks ) ; err != nil {
@ -1622,7 +1619,7 @@ func testEIP161AccountRemoval(t *testing.T, scheme string) {
block . AddTx ( tx )
} )
// account must exist pre eip 161
blockchain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
blockchain , _ := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer blockchain . Stop ( )
if _ , err := blockchain . InsertChain ( types . Blocks { blocks [ 0 ] } ) ; err != nil {
@ -1680,7 +1677,7 @@ func testBlockchainHeaderchainReorgConsistency(t *testing.T, scheme string) {
}
// Import the canonical and fork chain side by side, verifying the current block
// and current header consistency
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -1724,7 +1721,7 @@ func TestTrieForkGC(t *testing.T) {
forks [ i ] = fork [ 0 ]
}
// Import the canonical and fork chain side by side, forcing the trie cache to cache both
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , genesis , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , genesis , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -1770,7 +1767,7 @@ func testLargeReorgTrieGC(t *testing.T, scheme string) {
db , _ := rawdb . NewDatabaseWithFreezer ( rawdb . NewMemoryDatabase ( ) , "" , "" , false )
defer db . Close ( )
chain , err := NewBlockChain ( db , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( db , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -1786,18 +1783,15 @@ func testLargeReorgTrieGC(t *testing.T, scheme string) {
if chain . HasState ( shared [ len ( shared ) - 1 ] . Root ( ) ) {
t . Fatalf ( "common-but-old ancestor still cache" )
}
// Import the competitor chain without exceeding the canonical's TD and ensure
// we have not processed any of the blocks (protection against malicious blocks)
// Import the competitor chain without exceeding the canonical's TD.
// Post-merge the side chain should be executed
if _ , err := chain . InsertChain ( competitor [ : len ( competitor ) - 2 ] ) ; err != nil {
t . Fatalf ( "failed to insert competitor chain: %v" , err )
}
for i , block := range competitor [ : len ( competitor ) - 2 ] {
if chain . HasState ( block . Root ( ) ) {
t . Fatalf ( "competitor %d: low TD chain became processed" , i )
}
if ! chain . HasState ( competitor [ len ( competitor ) - 3 ] . Root ( ) ) {
t . Fatalf ( "failed to insert low-TD chain" )
}
// Import the head of the competitor chain, triggering the reorg and ensure we
// successfully reprocess all the stashed away blocks.
// Import the head of the competitor chain.
if _ , err := chain . InsertChain ( competitor [ len ( competitor ) - 2 : ] ) ; err != nil {
t . Fatalf ( "failed to finalize competitor chain: %v" , err )
}
@ -1841,7 +1835,7 @@ func testBlockchainRecovery(t *testing.T, scheme string) {
t . Fatalf ( "failed to create temp freezer db: %v" , err )
}
defer ancientDb . Close ( )
ancient , _ := NewBlockChain ( ancientDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
ancient , _ := NewBlockChain ( ancientDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
headers := make ( [ ] * types . Header , len ( blocks ) )
for i , block := range blocks {
@ -1861,7 +1855,7 @@ func testBlockchainRecovery(t *testing.T, scheme string) {
rawdb . WriteHeadFastBlockHash ( ancientDb , midBlock . Hash ( ) )
// Reopen broken blockchain again
ancient , _ = NewBlockChain ( ancientDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
ancient , _ = NewBlockChain ( ancientDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer ancient . Stop ( )
if num := ancient . CurrentBlock ( ) . Number . Uint64 ( ) ; num != 0 {
t . Errorf ( "head block mismatch: have #%v, want #%v" , num , 0 )
@ -1913,7 +1907,7 @@ func testInsertReceiptChainRollback(t *testing.T, scheme string) {
}
defer ancientDb . Close ( )
ancientChain , _ := NewBlockChain ( ancientDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
ancientChain , _ := NewBlockChain ( ancientDb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , ethash . NewFaker ( ) , vm . Config { } , nil )
defer ancientChain . Stop ( )
// Import the canonical header chain.
@ -1980,7 +1974,7 @@ func testLowDiffLongChain(t *testing.T, scheme string) {
diskdb , _ := rawdb . NewDatabaseWithFreezer ( rawdb . NewMemoryDatabase ( ) , "" , "" , false )
defer diskdb . Close ( )
chain , err := NewBlockChain ( diskdb , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( diskdb , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -2041,7 +2035,7 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
mergeBlock = math . MaxInt32
)
// Generate and import the canonical chain
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -2195,7 +2189,7 @@ func testInsertKnownChainData(t *testing.T, typ string, scheme string) {
}
defer chaindb . Close ( )
chain , err := NewBlockChain ( chaindb , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( chaindb , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -2279,10 +2273,10 @@ func testInsertKnownChainData(t *testing.T, typ string, scheme string) {
if err := inserter ( append ( blocks , blocks2 ... ) , append ( receipts , receipts2 ... ) ) ; err != nil {
t . Fatalf ( "failed to insert chain data: %v" , err )
}
// The head shouldn't change .
asserter ( t , blocks3 [ len ( blocks3 ) - 1 ] )
// Post-merge the chain should change even if td is lower .
asserter ( t , blocks2 [ len ( blocks2 ) - 1 ] )
// Rollback the heavier chain and re-insert the longer chain again
// Rollback the heavier chain and re-insert the longer chain again.
chain . SetHead ( rollback - 1 )
if err := inserter ( append ( blocks , blocks2 ... ) , append ( receipts , receipts2 ... ) ) ; err != nil {
t . Fatalf ( "failed to insert chain data: %v" , err )
@ -2366,7 +2360,7 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i
}
defer chaindb . Close ( )
chain , err := NewBlockChain ( chaindb , nil , genesis , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( chaindb , nil , genesis , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -2480,7 +2474,7 @@ func getLongAndShortChains(scheme string) (*BlockChain, []*types.Block, []*types
genDb , longChain , _ := GenerateChainWithGenesis ( genesis , engine , 80 , func ( i int , b * BlockGen ) {
b . SetCoinbase ( common . Address { 1 } )
} )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil )
if err != nil {
return nil , nil , nil , nil , fmt . Errorf ( "failed to create tester chain: %v" , err )
}
@ -2656,7 +2650,7 @@ func benchmarkLargeNumberOfValueToNonexisting(b *testing.B, numTxs, numBlocks in
b . ResetTimer ( )
for i := 0 ; i < b . N ; i ++ {
// Import the shared chain and the original canonical one
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vm . Config { } , nil )
if err != nil {
b . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -2743,7 +2737,21 @@ func testSideImportPrunedBlocks(t *testing.T, scheme string) {
// Generate and import the canonical chain
_ , blocks , _ := GenerateChainWithGenesis ( genesis , engine , 2 * state . TriesInMemory , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil , nil )
// Construct a database with freezer enabled
datadir := t . TempDir ( )
ancient := path . Join ( datadir , "ancient" )
db , err := rawdb . Open ( rawdb . OpenOptions {
Directory : datadir ,
AncientsDirectory : ancient ,
Ephemeral : true ,
} )
if err != nil {
t . Fatalf ( "Failed to create persistent database: %v" , err )
}
defer db . Close ( )
chain , err := NewBlockChain ( db , DefaultCacheConfigWithScheme ( scheme ) , genesis , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -2770,7 +2778,6 @@ func testSideImportPrunedBlocks(t *testing.T, scheme string) {
if ! chain . HasBlockAndState ( firstNonPrunedBlock . Hash ( ) , firstNonPrunedBlock . NumberU64 ( ) ) {
t . Errorf ( "Block %d pruned" , firstNonPrunedBlock . NumberU64 ( ) )
}
// Now re-import some old blocks
blockToReimport := blocks [ 5 : 8 ]
_ , err = chain . InsertChain ( blockToReimport )
if err != nil {
@ -2843,7 +2850,7 @@ func testDeleteCreateRevert(t *testing.T, scheme string) {
b . AddTx ( tx )
} )
// Import the canonical chain
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -2958,7 +2965,7 @@ func testDeleteRecreateSlots(t *testing.T, scheme string) {
// Import the canonical chain
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config {
Tracer : logger . NewJSONLogger ( nil , os . Stdout ) ,
} , nil , nil )
} , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -3040,7 +3047,7 @@ func testDeleteRecreateAccount(t *testing.T, scheme string) {
// Import the canonical chain
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config {
Tracer : logger . NewJSONLogger ( nil , os . Stdout ) ,
} , nil , nil )
} , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -3216,7 +3223,7 @@ func testDeleteRecreateSlotsAcrossManyBlocks(t *testing.T, scheme string) {
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config {
//Debug: true,
//Tracer: vm.NewJSONLogger(nil, os.Stdout),
} , nil , nil )
} , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -3354,7 +3361,7 @@ func testInitThenFailCreateContract(t *testing.T, scheme string) {
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config {
//Debug: true,
//Tracer: vm.NewJSONLogger(nil, os.Stdout),
} , nil , nil )
} , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -3441,7 +3448,7 @@ func testEIP2718Transition(t *testing.T, scheme string) {
} )
// Import the canonical chain
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -3535,7 +3542,7 @@ func testEIP1559Transition(t *testing.T, scheme string) {
b . AddTx ( tx )
} )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -3648,7 +3655,7 @@ func testSetCanonical(t *testing.T, scheme string) {
diskdb , _ := rawdb . NewDatabaseWithFreezer ( rawdb . NewMemoryDatabase ( ) , "" , "" , false )
defer diskdb . Close ( )
chain , err := NewBlockChain ( diskdb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( diskdb , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -3757,7 +3764,7 @@ func testCanonicalHashMarker(t *testing.T, scheme string) {
_ , forkB , _ := GenerateChainWithGenesis ( gspec , engine , c . forkB , func ( i int , gen * BlockGen ) { } )
// Initialize test chain
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , DefaultCacheConfigWithScheme ( scheme ) , gspec , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -3894,7 +3901,7 @@ func testCreateThenDelete(t *testing.T, config *params.ChainConfig) {
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vm . Config {
//Debug: true,
//Tracer: logger.NewJSONLogger(nil, os.Stdout),
} , nil , nil )
} , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -4006,7 +4013,7 @@ func TestDeleteThenCreate(t *testing.T) {
}
} )
// Import the canonical chain
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vm . Config { } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vm . Config { } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -4091,7 +4098,7 @@ func TestTransientStorageReset(t *testing.T) {
} )
// Initialize the blockchain with 1153 enabled.
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vmConfig , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vmConfig , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -4186,7 +4193,7 @@ func TestEIP3651(t *testing.T) {
b . AddTx ( tx )
} )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vm . Config { Tracer : logger . NewMarkdownLogger ( & logger . Config { } , os . Stderr ) . Hooks ( ) } , nil , nil )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vm . Config { Tracer : logger . NewMarkdownLogger ( & logger . Config { } , os . Stderr ) . Hooks ( ) } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
@ -4220,3 +4227,90 @@ func TestEIP3651(t *testing.T) {
t . Fatalf ( "sender balance incorrect: expected %d, got %d" , expected , actual )
}
}
func TestEIP6110 ( t * testing . T ) {
var (
engine = beacon . NewFaker ( )
// A sender who makes transactions, has some funds
key , _ = crypto . HexToECDSA ( "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
addr = crypto . PubkeyToAddress ( key . PublicKey )
funds = new ( big . Int ) . Mul ( common . Big1 , big . NewInt ( params . Ether ) )
config = * params . AllEthashProtocolChanges
gspec = & Genesis {
Config : & config ,
Alloc : types . GenesisAlloc {
addr : { Balance : funds } ,
config . DepositContractAddress : {
// Simple deposit generator, source: https://gist.github.com/lightclient/54abb2af2465d6969fa6d1920b9ad9d7
Code : common . Hex2Bytes ( "6080604052366103aa575f603067ffffffffffffffff811115610025576100246103ae565b5b6040519080825280601f01601f1916602001820160405280156100575781602001600182028036833780820191505090505b5090505f8054906101000a900460ff1660f81b815f8151811061007d5761007c6103db565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff191690815f1a9053505f602067ffffffffffffffff8111156100c7576100c66103ae565b5b6040519080825280601f01601f1916602001820160405280156100f95781602001600182028036833780820191505090505b5090505f8054906101000a900460ff1660f81b815f8151811061011f5761011e6103db565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff191690815f1a9053505f600867ffffffffffffffff811115610169576101686103ae565b5b6040519080825280601f01601f19166020018201604052801561019b5781602001600182028036833780820191505090505b5090505f8054906101000a900460ff1660f81b815f815181106101c1576101c06103db565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff191690815f1a9053505f606067ffffffffffffffff81111561020b5761020a6103ae565b5b6040519080825280601f01601f19166020018201604052801561023d5781602001600182028036833780820191505090505b5090505f8054906101000a900460ff1660f81b815f81518110610263576102626103db565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff191690815f1a9053505f600867ffffffffffffffff8111156102ad576102ac6103ae565b5b6040519080825280601f01601f1916602001820160405280156102df5781602001600182028036833780820191505090505b5090505f8054906101000a900460ff1660f81b815f81518110610305576103046103db565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff191690815f1a9053505f8081819054906101000a900460ff168092919061035090610441565b91906101000a81548160ff021916908360ff160217905550507f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c585858585856040516103a09594939291906104d9565b60405180910390a1005b5f80fd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f60ff82169050919050565b5f61044b82610435565b915060ff820361045e5761045d610408565b5b600182019050919050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f601f19601f8301169050919050565b5f6104ab82610469565b6104b58185610473565b93506104c5818560208601610483565b6104ce81610491565b840191505092915050565b5f60a0820190508181035f8301526104f181886104a1565b9050818103602083015261050581876104a1565b9050818103604083015261051981866104a1565b9050818103606083015261052d81856104a1565b9050818103608083015261054181846104a1565b9050969550505050505056fea26469706673582212208569967e58690162d7d6fe3513d07b393b4c15e70f41505cbbfd08f53eba739364736f6c63430008190033" ) ,
Nonce : 0 ,
Balance : big . NewInt ( 0 ) ,
} ,
} ,
}
)
gspec . Config . BerlinBlock = common . Big0
gspec . Config . LondonBlock = common . Big0
gspec . Config . TerminalTotalDifficulty = common . Big0
gspec . Config . TerminalTotalDifficultyPassed = true
gspec . Config . ShanghaiTime = u64 ( 0 )
gspec . Config . CancunTime = u64 ( 0 )
gspec . Config . PragueTime = u64 ( 0 )
signer := types . LatestSigner ( gspec . Config )
_ , blocks , _ := GenerateChainWithGenesis ( gspec , engine , 1 , func ( i int , b * BlockGen ) {
for i := 0 ; i < 5 ; i ++ {
txdata := & types . DynamicFeeTx {
ChainID : gspec . Config . ChainID ,
Nonce : uint64 ( i ) ,
To : & config . DepositContractAddress ,
Gas : 500000 ,
GasFeeCap : newGwei ( 5 ) ,
GasTipCap : big . NewInt ( 2 ) ,
AccessList : nil ,
Data : [ ] byte { } ,
}
tx := types . NewTx ( txdata )
tx , _ = types . SignTx ( tx , signer , key )
b . AddTx ( tx )
}
} )
chain , err := NewBlockChain ( rawdb . NewMemoryDatabase ( ) , nil , gspec , nil , engine , vm . Config { Tracer : logger . NewMarkdownLogger ( & logger . Config { DisableStack : true } , os . Stderr ) . Hooks ( ) } , nil )
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
defer chain . Stop ( )
if n , err := chain . InsertChain ( blocks ) ; err != nil {
t . Fatalf ( "block %d: failed to insert into chain: %v" , n , err )
}
block := chain . GetBlockByNumber ( 1 )
if len ( block . Requests ( ) ) != 5 {
t . Fatalf ( "failed to retrieve deposits: have %d, want %d" , len ( block . Requests ( ) ) , 5 )
}
// Verify each index is correct.
for want , req := range block . Requests ( ) {
d , ok := req . Inner ( ) . ( * types . Deposit )
if ! ok {
t . Fatalf ( "expected deposit object" )
}
if got := int ( d . PublicKey [ 0 ] ) ; got != want {
t . Fatalf ( "invalid pubkey: have %d, want %d" , got , want )
}
if got := int ( d . WithdrawalCredentials [ 0 ] ) ; got != want {
t . Fatalf ( "invalid withdrawal credentials: have %d, want %d" , got , want )
}
if d . Amount != uint64 ( want ) {
t . Fatalf ( "invalid amounbt: have %d, want %d" , d . Amount , want )
}
if got := int ( d . Signature [ 0 ] ) ; got != want {
t . Fatalf ( "invalid signature: have %d, want %d" , got , want )
}
if d . Index != uint64 ( want ) {
t . Fatalf ( "invalid index: have %d, want %d" , d . Index , want )
}
}
}