From 91cb6f863a965481e51d5d9c0e5ccd54796fd967 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 6 Feb 2023 14:52:51 -0500 Subject: [PATCH] tests: update (#26548) This updates the reference tests to the latest version. Co-authored-by: Felix Lange --- core/block_validator.go | 12 ++++++++++-- core/state_processor_test.go | 3 +++ tests/block_test_util.go | 8 ++++++++ tests/gen_btheader.go | 6 ++++++ tests/init.go | 18 ++++++++++++++++++ tests/state_test.go | 8 ++++---- tests/testdata | 2 +- 7 files changed, 50 insertions(+), 7 deletions(-) diff --git a/core/block_validator.go b/core/block_validator.go index 7c202292f..3704158c1 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -50,11 +50,13 @@ func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engin // header's transaction and uncle roots. The headers are assumed to be already // validated at this point. func (v *BlockValidator) ValidateBody(block *types.Block) error { - // Check whether the block's known, and if not, that it's linkable + // Check whether the block is already imported. if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) { return ErrKnownBlock } - // Header validity is known at this point, check the uncles and transactions + + // Header validity is known at this point. Here we verify that uncles, transactions + // and withdrawals given in the block body match the header. header := block.Header() if err := v.engine.VerifyUncles(v.bc, block); err != nil { return err @@ -65,11 +67,17 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error { if hash := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)); hash != header.TxHash { return fmt.Errorf("transaction root hash mismatch (header value %x, calculated %x)", header.TxHash, hash) } + // Withdrawals are present after the Shanghai fork. if header.WithdrawalsHash != nil { + // Withdrawals list must be present in body after Shanghai. + if block.Withdrawals() == nil { + return fmt.Errorf("missing withdrawals in block body") + } if hash := types.DeriveSha(block.Withdrawals(), trie.NewStackTrie(nil)); hash != *header.WithdrawalsHash { return fmt.Errorf("withdrawals root hash mismatch (header value %x, calculated %x)", *header.WithdrawalsHash, hash) } } + if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) { if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) { return consensus.ErrUnknownAncestor diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 305948d54..821b85e9b 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -423,5 +423,8 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr } header.Root = common.BytesToHash(hasher.Sum(nil)) // Assemble and return the final block for sealing + if config.IsShanghai(header.Time) { + return types.NewBlockWithWithdrawals(header, txs, nil, receipts, []*types.Withdrawal{}, trie.NewStackTrie(nil)) + } return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil)) } diff --git a/tests/block_test_util.go b/tests/block_test_util.go index df4f08a76..8da95a640 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -24,6 +24,7 @@ import ( "fmt" "math/big" "os" + "reflect" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -87,6 +88,7 @@ type btHeader struct { GasUsed uint64 Timestamp uint64 BaseFeePerGas *big.Int + WithdrawalsRoot *common.Hash } type btHeaderMarshaling struct { @@ -275,6 +277,12 @@ func validateHeader(h *btHeader, h2 *types.Header) error { if h.Timestamp != h2.Time { return fmt.Errorf("timestamp: want: %v have: %v", h.Timestamp, h2.Time) } + if !reflect.DeepEqual(h.BaseFeePerGas, h2.BaseFee) { + return fmt.Errorf("baseFeePerGas: want: %v have: %v", h.BaseFeePerGas, h2.BaseFee) + } + if !reflect.DeepEqual(h.WithdrawalsRoot, h2.WithdrawalsHash) { + return fmt.Errorf("withdrawalsRoot: want: %v have: %v", h.WithdrawalsRoot, h2.WithdrawalsHash) + } return nil } diff --git a/tests/gen_btheader.go b/tests/gen_btheader.go index 4387f8db4..985ea692d 100644 --- a/tests/gen_btheader.go +++ b/tests/gen_btheader.go @@ -34,6 +34,7 @@ func (b btHeader) MarshalJSON() ([]byte, error) { GasUsed math.HexOrDecimal64 Timestamp math.HexOrDecimal64 BaseFeePerGas *math.HexOrDecimal256 + WithdrawalsRoot *common.Hash } var enc btHeader enc.Bloom = b.Bloom @@ -53,6 +54,7 @@ func (b btHeader) MarshalJSON() ([]byte, error) { enc.GasUsed = math.HexOrDecimal64(b.GasUsed) enc.Timestamp = math.HexOrDecimal64(b.Timestamp) enc.BaseFeePerGas = (*math.HexOrDecimal256)(b.BaseFeePerGas) + enc.WithdrawalsRoot = b.WithdrawalsRoot return json.Marshal(&enc) } @@ -76,6 +78,7 @@ func (b *btHeader) UnmarshalJSON(input []byte) error { GasUsed *math.HexOrDecimal64 Timestamp *math.HexOrDecimal64 BaseFeePerGas *math.HexOrDecimal256 + WithdrawalsRoot *common.Hash } var dec btHeader if err := json.Unmarshal(input, &dec); err != nil { @@ -132,5 +135,8 @@ func (b *btHeader) UnmarshalJSON(input []byte) error { if dec.BaseFeePerGas != nil { b.BaseFeePerGas = (*big.Int)(dec.BaseFeePerGas) } + if dec.WithdrawalsRoot != nil { + b.WithdrawalsRoot = dec.WithdrawalsRoot + } return nil } diff --git a/tests/init.go b/tests/init.go index a36ab4d9d..db037e3e1 100644 --- a/tests/init.go +++ b/tests/init.go @@ -268,6 +268,24 @@ var Forks = map[string]*params.ChainConfig{ TerminalTotalDifficulty: big.NewInt(0), ShanghaiTime: u64(0), }, + "MergeToShanghaiAtTime15k": { + ChainID: big.NewInt(1), + HomesteadBlock: big.NewInt(0), + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(0), + ArrowGlacierBlock: big.NewInt(0), + MergeNetsplitBlock: big.NewInt(0), + TerminalTotalDifficulty: big.NewInt(0), + ShanghaiTime: u64(15_000), + }, } // AvailableForks returns the set of defined fork names diff --git a/tests/state_test.go b/tests/state_test.go index a68321e76..7dd2f678c 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -56,11 +56,11 @@ func TestState(t *testing.T) { // Uses 1GB RAM per tested fork st.skipLoad(`^stStaticCall/static_Call1MB`) - // Not yet supported TODO - st.skipLoad(`^stEIP3540/`) - st.skipLoad(`^stEIP3860/`) - // Broken tests: + // + // The stEOF tests are generated with EOF as part of Shanghai, which + // is erroneous. Therefore, these tests are skipped. + st.skipLoad(`^EIPTests/stEOF/`) // Expected failures: // For Istanbul, older tests were moved into LegacyTests diff --git a/tests/testdata b/tests/testdata index 24fa31adb..bac70c50a 160000 --- a/tests/testdata +++ b/tests/testdata @@ -1 +1 @@ -Subproject commit 24fa31adb30f71ee700b27decb5204e53a11d9f3 +Subproject commit bac70c50a579197af68af5fc6d8c7b6163b92c52