|
|
|
@ -18,6 +18,7 @@ package core |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"crypto/ecdsa" |
|
|
|
|
"encoding/binary" |
|
|
|
|
"math/big" |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
@ -29,11 +30,14 @@ import ( |
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip1559" |
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip4844" |
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb" |
|
|
|
|
"github.com/ethereum/go-ethereum/core/state" |
|
|
|
|
"github.com/ethereum/go-ethereum/core/types" |
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm" |
|
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb/memorydb" |
|
|
|
|
"github.com/ethereum/go-ethereum/params" |
|
|
|
|
"github.com/ethereum/go-ethereum/trie" |
|
|
|
|
"github.com/ethereum/go-ethereum/triedb" |
|
|
|
|
"github.com/holiman/uint256" |
|
|
|
|
"golang.org/x/crypto/sha3" |
|
|
|
|
) |
|
|
|
@ -528,3 +532,54 @@ func TestProcessVerkle(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestProcessParentBlockHash(t *testing.T) { |
|
|
|
|
var ( |
|
|
|
|
chainConfig = params.MergedTestChainConfig |
|
|
|
|
hashA = common.Hash{0x01} |
|
|
|
|
hashB = common.Hash{0x02} |
|
|
|
|
header = &types.Header{ParentHash: hashA, Number: big.NewInt(2), Difficulty: big.NewInt(0)} |
|
|
|
|
parent = &types.Header{ParentHash: hashB, Number: big.NewInt(1), Difficulty: big.NewInt(0)} |
|
|
|
|
coinbase = common.Address{} |
|
|
|
|
) |
|
|
|
|
test := func(statedb *state.StateDB) { |
|
|
|
|
statedb.SetNonce(params.HistoryStorageAddress, 1) |
|
|
|
|
statedb.SetCode(params.HistoryStorageAddress, params.HistoryStorageCode) |
|
|
|
|
statedb.IntermediateRoot(true) |
|
|
|
|
|
|
|
|
|
vmContext := NewEVMBlockContext(header, nil, &coinbase) |
|
|
|
|
evm := vm.NewEVM(vmContext, vm.TxContext{}, statedb, chainConfig, vm.Config{}) |
|
|
|
|
ProcessParentBlockHash(header.ParentHash, evm, statedb) |
|
|
|
|
|
|
|
|
|
vmContext = NewEVMBlockContext(parent, nil, &coinbase) |
|
|
|
|
evm = vm.NewEVM(vmContext, vm.TxContext{}, statedb, chainConfig, vm.Config{}) |
|
|
|
|
ProcessParentBlockHash(parent.ParentHash, evm, statedb) |
|
|
|
|
|
|
|
|
|
// make sure that the state is correct
|
|
|
|
|
if have := getParentBlockHash(statedb, 1); have != hashA { |
|
|
|
|
t.Errorf("want parent hash %v, have %v", hashA, have) |
|
|
|
|
} |
|
|
|
|
if have := getParentBlockHash(statedb, 0); have != hashB { |
|
|
|
|
t.Errorf("want parent hash %v, have %v", hashB, have) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
t.Run("MPT", func(t *testing.T) { |
|
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewDatabase(memorydb.New())), nil) |
|
|
|
|
test(statedb) |
|
|
|
|
}) |
|
|
|
|
t.Run("Verkle", func(t *testing.T) { |
|
|
|
|
db := rawdb.NewMemoryDatabase() |
|
|
|
|
cacheConfig := DefaultCacheConfigWithScheme(rawdb.PathScheme) |
|
|
|
|
cacheConfig.SnapshotLimit = 0 |
|
|
|
|
triedb := triedb.NewDatabase(db, cacheConfig.triedbConfig(true)) |
|
|
|
|
statedb, _ := state.New(types.EmptyVerkleHash, state.NewDatabaseWithNodeDB(db, triedb), nil) |
|
|
|
|
test(statedb) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func getParentBlockHash(statedb *state.StateDB, number uint64) common.Hash { |
|
|
|
|
ringIndex := number % params.HistoryServeWindow |
|
|
|
|
var key common.Hash |
|
|
|
|
binary.BigEndian.PutUint64(key[24:], ringIndex) |
|
|
|
|
return statedb.GetState(params.HistoryStorageAddress, key) |
|
|
|
|
} |
|
|
|
|