@ -152,7 +152,7 @@ func TestProcessVerkle(t *testing.T) {
txCost1 * 2 + txCost2 ,
txCost1 * 2 + txCost2 ,
txCost1 * 2 + txCost2 + contractCreationCost + codeWithExtCodeCopyGas ,
txCost1 * 2 + txCost2 + contractCreationCost + codeWithExtCodeCopyGas ,
}
}
_ , chain , _ , proofs , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 2 , func ( i int , gen * BlockGen ) {
_ , _ , chain , _ , proofs , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 2 , func ( i int , gen * BlockGen ) {
gen . SetPoS ( )
gen . SetPoS ( )
// TODO need to check that the tx cost provided is the exact amount used (no remaining left-over)
// TODO need to check that the tx cost provided is the exact amount used (no remaining left-over)
@ -219,7 +219,7 @@ func TestProcessParentBlockHash(t *testing.T) {
// block 1 parent hash is 0x0100....
// block 1 parent hash is 0x0100....
// block 2 parent hash is 0x0200....
// block 2 parent hash is 0x0200....
// etc
// etc
checkBlockHashes := func ( statedb * state . StateDB ) {
checkBlockHashes := func ( statedb * state . StateDB , isVerkle bool ) {
statedb . SetNonce ( params . HistoryStorageAddress , 1 )
statedb . SetNonce ( params . HistoryStorageAddress , 1 )
statedb . SetCode ( params . HistoryStorageAddress , params . HistoryStorageCode )
statedb . SetCode ( params . HistoryStorageAddress , params . HistoryStorageCode )
// Process n blocks, from 1 .. num
// Process n blocks, from 1 .. num
@ -227,20 +227,24 @@ func TestProcessParentBlockHash(t *testing.T) {
for i := 1 ; i <= num ; i ++ {
for i := 1 ; i <= num ; i ++ {
header := & types . Header { ParentHash : common . Hash { byte ( i ) } , Number : big . NewInt ( int64 ( i ) ) , Difficulty : new ( big . Int ) }
header := & types . Header { ParentHash : common . Hash { byte ( i ) } , Number : big . NewInt ( int64 ( i ) ) , Difficulty : new ( big . Int ) }
vmContext := NewEVMBlockContext ( header , nil , new ( common . Address ) )
vmContext := NewEVMBlockContext ( header , nil , new ( common . Address ) )
evm := vm . NewEVM ( vmContext , statedb , params . MergedTestChainConfig , vm . Config { } )
chainConfig := params . MergedTestChainConfig
if isVerkle {
chainConfig = testVerkleChainConfig
}
evm := vm . NewEVM ( vmContext , statedb , chainConfig , vm . Config { } )
ProcessParentBlockHash ( header . ParentHash , evm )
ProcessParentBlockHash ( header . ParentHash , evm )
}
}
// Read block hashes for block 0 .. num-1
// Read block hashes for block 0 .. num-1
for i := 0 ; i < num ; i ++ {
for i := 0 ; i < num ; i ++ {
have , want := getContractStoredBlockHash ( statedb , uint64 ( i ) ) , common . Hash { byte ( i + 1 ) }
have , want := getContractStoredBlockHash ( statedb , uint64 ( i ) , isVerkle ) , common . Hash { byte ( i + 1 ) }
if have != want {
if have != want {
t . Errorf ( "block %d, have parent hash %v, want %v" , i , have , want )
t . Errorf ( "block %d, verkle=%v, have parent hash %v, want %v" , i , isVerkle , have , want )
}
}
}
}
}
}
t . Run ( "MPT" , func ( t * testing . T ) {
t . Run ( "MPT" , func ( t * testing . T ) {
statedb , _ := state . New ( types . EmptyRootHash , state . NewDatabaseForTesting ( ) )
statedb , _ := state . New ( types . EmptyRootHash , state . NewDatabaseForTesting ( ) )
checkBlockHashes ( statedb )
checkBlockHashes ( statedb , false )
} )
} )
t . Run ( "Verkle" , func ( t * testing . T ) {
t . Run ( "Verkle" , func ( t * testing . T ) {
db := rawdb . NewMemoryDatabase ( )
db := rawdb . NewMemoryDatabase ( )
@ -248,15 +252,18 @@ func TestProcessParentBlockHash(t *testing.T) {
cacheConfig . SnapshotLimit = 0
cacheConfig . SnapshotLimit = 0
triedb := triedb . NewDatabase ( db , cacheConfig . triedbConfig ( true ) )
triedb := triedb . NewDatabase ( db , cacheConfig . triedbConfig ( true ) )
statedb , _ := state . New ( types . EmptyVerkleHash , state . NewDatabase ( triedb , nil ) )
statedb , _ := state . New ( types . EmptyVerkleHash , state . NewDatabase ( triedb , nil ) )
checkBlockHashes ( statedb )
checkBlockHashes ( statedb , true )
} )
} )
}
}
// getContractStoredBlockHash is a utility method which reads the stored parent blockhash for block 'number'
// getContractStoredBlockHash is a utility method which reads the stored parent blockhash for block 'number'
func getContractStoredBlockHash ( statedb * state . StateDB , number uint64 ) common . Hash {
func getContractStoredBlockHash ( statedb * state . StateDB , number uint64 , isVerkle bool ) common . Hash {
ringIndex := number % params . HistoryServeWindow
ringIndex := number % params . HistoryServeWindow
var key common . Hash
var key common . Hash
binary . BigEndian . PutUint64 ( key [ 24 : ] , ringIndex )
binary . BigEndian . PutUint64 ( key [ 24 : ] , ringIndex )
if isVerkle {
return statedb . GetState ( params . HistoryStorageAddress , key )
}
return statedb . GetState ( params . HistoryStorageAddress , key )
return statedb . GetState ( params . HistoryStorageAddress , key )
}
}
@ -279,7 +286,7 @@ func TestProcessVerkleInvalidContractCreation(t *testing.T) {
//
//
// - The second block contains a single failing contract creation transaction,
// - The second block contains a single failing contract creation transaction,
// that fails right off the bat.
// that fails right off the bat.
_ , chain , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 2 , func ( i int , gen * BlockGen ) {
genesisH , _ , chain , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 2 , func ( i int , gen * BlockGen ) {
gen . SetPoS ( )
gen . SetPoS ( )
if i == 0 {
if i == 0 {
@ -364,8 +371,8 @@ func TestProcessVerkleInvalidContractCreation(t *testing.T) {
if stemStateDiff . SuffixDiffs [ 0 ] . NewValue == nil {
if stemStateDiff . SuffixDiffs [ 0 ] . NewValue == nil {
t . Fatalf ( "nil new value in BLOCKHASH contract insert" )
t . Fatalf ( "nil new value in BLOCKHASH contract insert" )
}
}
if * stemStateDiff . SuffixDiffs [ 0 ] . NewValue != chain [ 0 ] . Hash ( ) {
if * stemStateDiff . SuffixDiffs [ 0 ] . NewValue != genesisH {
t . Fatalf ( "invalid BLOCKHASH value: %x != %x" , * stemStateDiff . SuffixDiffs [ 0 ] . NewValue , chain [ 0 ] . Hash ( ) )
t . Fatalf ( "invalid BLOCKHASH value: %x != %x" , * stemStateDiff . SuffixDiffs [ 0 ] . NewValue , genesisH )
}
}
} else {
} else {
// For all other entries present in the witness, check that nothing beyond
// For all other entries present in the witness, check that nothing beyond
@ -393,8 +400,8 @@ func TestProcessVerkleInvalidContractCreation(t *testing.T) {
if stemStateDiff . SuffixDiffs [ 0 ] . NewValue == nil {
if stemStateDiff . SuffixDiffs [ 0 ] . NewValue == nil {
t . Fatalf ( "missing post state value for BLOCKHASH contract at block #2" )
t . Fatalf ( "missing post state value for BLOCKHASH contract at block #2" )
}
}
if * stemStateDiff . SuffixDiffs [ 0 ] . NewValue != common . HexToHash ( "0788c2c0f23aa07eb8bf76fe6c1ca9064a4821c1fd0af803913da488a58dba54" ) {
if * stemStateDiff . SuffixDiffs [ 0 ] . NewValue != chain [ 0 ] . Hash ( ) {
t . Fatalf ( "invalid post state value for BLOCKHASH contract at block #2: 0788c2c0f23aa07eb8bf76fe6c1ca9064a4821c1fd0af803913da488a58dba54 != %x" , ( * stemStateDiff . SuffixDiffs [ 0 ] . NewValue ) [ : ] )
t . Fatalf ( "invalid post state value for BLOCKHASH contract at block #2: %x != %x" , chain [ 0 ] . Hash ( ) , ( * stemStateDiff . SuffixDiffs [ 0 ] . NewValue ) [ : ] )
}
}
} else if suffixDiff . Suffix > 4 {
} else if suffixDiff . Suffix > 4 {
t . Fatalf ( "invalid suffix diff found for %x in block #2: %d\n" , stemStateDiff . Stem , suffixDiff . Suffix )
t . Fatalf ( "invalid suffix diff found for %x in block #2: %d\n" , stemStateDiff . Stem , suffixDiff . Suffix )
@ -440,7 +447,7 @@ func TestProcessVerkleContractWithEmptyCode(t *testing.T) {
config . ChainID . SetUint64 ( 69421 )
config . ChainID . SetUint64 ( 69421 )
gspec := verkleTestGenesis ( & config )
gspec := verkleTestGenesis ( & config )
_ , chain , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 1 , func ( i int , gen * BlockGen ) {
genesisH , _ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 1 , func ( i int , gen * BlockGen ) {
gen . SetPoS ( )
gen . SetPoS ( )
var tx types . Transaction
var tx types . Transaction
// a transaction that does some PUSH1n but returns a 0-sized contract
// a transaction that does some PUSH1n but returns a 0-sized contract
@ -472,8 +479,8 @@ func TestProcessVerkleContractWithEmptyCode(t *testing.T) {
if stemStateDiff . SuffixDiffs [ 0 ] . NewValue == nil {
if stemStateDiff . SuffixDiffs [ 0 ] . NewValue == nil {
t . Fatalf ( "nil new value in BLOCKHASH contract insert" )
t . Fatalf ( "nil new value in BLOCKHASH contract insert" )
}
}
if * stemStateDiff . SuffixDiffs [ 0 ] . NewValue != chain [ 0 ] . Hash ( ) {
if * stemStateDiff . SuffixDiffs [ 0 ] . NewValue != genesisH {
t . Fatalf ( "invalid BLOCKHASH value: %x != %x" , * stemStateDiff . SuffixDiffs [ 0 ] . NewValue , chain [ 0 ] . Hash ( ) )
t . Fatalf ( "invalid BLOCKHASH value: %x != %x" , * stemStateDiff . SuffixDiffs [ 0 ] . NewValue , genesisH )
}
}
} else {
} else {
for _ , suffixDiff := range stemStateDiff . SuffixDiffs {
for _ , suffixDiff := range stemStateDiff . SuffixDiffs {
@ -532,7 +539,7 @@ func TestProcessVerkleExtCodeHashOpcode(t *testing.T) {
}
}
extCodeHashContractAddr := crypto . CreateAddress ( deployer , 1 )
extCodeHashContractAddr := crypto . CreateAddress ( deployer , 1 )
_ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 2 , func ( i int , gen * BlockGen ) {
_ , _ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 2 , func ( i int , gen * BlockGen ) {
gen . SetPoS ( )
gen . SetPoS ( )
if i == 0 {
if i == 0 {
@ -605,7 +612,7 @@ func TestProcessVerkleBalanceOpcode(t *testing.T) {
account2 = common . HexToAddress ( "0x6177843db3138ae69679A54b95cf345ED759450d" )
account2 = common . HexToAddress ( "0x6177843db3138ae69679A54b95cf345ED759450d" )
gspec = verkleTestGenesis ( & config )
gspec = verkleTestGenesis ( & config )
)
)
_ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 1 , func ( i int , gen * BlockGen ) {
_ , _ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 1 , func ( i int , gen * BlockGen ) {
gen . SetPoS ( )
gen . SetPoS ( )
txData := slices . Concat (
txData := slices . Concat (
[ ] byte { byte ( vm . PUSH20 ) } ,
[ ] byte { byte ( vm . PUSH20 ) } ,
@ -686,7 +693,7 @@ func TestProcessVerkleSelfDestructInSeparateTx(t *testing.T) {
deployer := crypto . PubkeyToAddress ( testKey . PublicKey )
deployer := crypto . PubkeyToAddress ( testKey . PublicKey )
contract := crypto . CreateAddress ( deployer , 0 )
contract := crypto . CreateAddress ( deployer , 0 )
_ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 2 , func ( i int , gen * BlockGen ) {
_ , _ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 2 , func ( i int , gen * BlockGen ) {
gen . SetPoS ( )
gen . SetPoS ( )
if i == 0 {
if i == 0 {
@ -794,7 +801,7 @@ func TestProcessVerkleSelfDestructInSameTx(t *testing.T) {
deployer := crypto . PubkeyToAddress ( testKey . PublicKey )
deployer := crypto . PubkeyToAddress ( testKey . PublicKey )
contract := crypto . CreateAddress ( deployer , 0 )
contract := crypto . CreateAddress ( deployer , 0 )
_ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 1 , func ( i int , gen * BlockGen ) {
_ , _ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 1 , func ( i int , gen * BlockGen ) {
gen . SetPoS ( )
gen . SetPoS ( )
tx , _ := types . SignNewTx ( testKey , signer , & types . LegacyTx { Nonce : 0 ,
tx , _ := types . SignNewTx ( testKey , signer , & types . LegacyTx { Nonce : 0 ,
Value : big . NewInt ( 42 ) ,
Value : big . NewInt ( 42 ) ,
@ -897,7 +904,7 @@ func TestProcessVerkleSelfDestructInSeparateTxWithSelfBeneficiary(t *testing.T)
deployer := crypto . PubkeyToAddress ( testKey . PublicKey )
deployer := crypto . PubkeyToAddress ( testKey . PublicKey )
contract := crypto . CreateAddress ( deployer , 0 )
contract := crypto . CreateAddress ( deployer , 0 )
_ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 2 , func ( i int , gen * BlockGen ) {
_ , _ , _ , _ , _ , statediffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 2 , func ( i int , gen * BlockGen ) {
gen . SetPoS ( )
gen . SetPoS ( )
if i == 0 {
if i == 0 {
// Create self-destruct contract, sending 42 wei.
// Create self-destruct contract, sending 42 wei.
@ -977,7 +984,7 @@ func TestProcessVerkleSelfDestructInSameTxWithSelfBeneficiary(t *testing.T) {
selfDestructContract := [ ] byte { byte ( vm . ADDRESS ) , byte ( vm . SELFDESTRUCT ) }
selfDestructContract := [ ] byte { byte ( vm . ADDRESS ) , byte ( vm . SELFDESTRUCT ) }
_ , _ , _ , _ , stateDiffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 1 , func ( i int , gen * BlockGen ) {
_ , _ , _ , _ , _ , stateDiffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 1 , func ( i int , gen * BlockGen ) {
gen . SetPoS ( )
gen . SetPoS ( )
tx , _ := types . SignNewTx ( testKey , signer , & types . LegacyTx { Nonce : 0 ,
tx , _ := types . SignNewTx ( testKey , signer , & types . LegacyTx { Nonce : 0 ,
Value : big . NewInt ( 42 ) ,
Value : big . NewInt ( 42 ) ,
@ -1043,7 +1050,7 @@ func TestProcessVerkleSelfDestructInSameTxWithSelfBeneficiaryAndPrefundedAccount
selfDestructContract := [ ] byte { byte ( vm . ADDRESS ) , byte ( vm . SELFDESTRUCT ) }
selfDestructContract := [ ] byte { byte ( vm . ADDRESS ) , byte ( vm . SELFDESTRUCT ) }
_ , _ , _ , _ , stateDiffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 1 , func ( i int , gen * BlockGen ) {
_ , _ , _ , _ , _ , stateDiffs := GenerateVerkleChainWithGenesis ( gspec , beacon . New ( ethash . NewFaker ( ) ) , 1 , func ( i int , gen * BlockGen ) {
gen . SetPoS ( )
gen . SetPoS ( )
tx , _ := types . SignNewTx ( testKey , signer , & types . LegacyTx { Nonce : 0 ,
tx , _ := types . SignNewTx ( testKey , signer , & types . LegacyTx { Nonce : 0 ,
Value : big . NewInt ( 42 ) ,
Value : big . NewInt ( 42 ) ,