From f27f7e884d1b3d29fff748bb5641ad1e461b756d Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 6 Feb 2023 18:06:27 +0100 Subject: [PATCH] core: add block body validity check for presence of withdrawals --- core/block_validator.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/core/block_validator.go b/core/block_validator.go index 813ac0a4384d..c1d28ee6f738 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: have %x, want %x", hash, header.TxHash) } + // 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: have %x, want %x", hash, *header.WithdrawalsHash) } } + if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) { if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) { return consensus.ErrUnknownAncestor @@ -107,7 +115,7 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD return nil } -// CalcGasLimit computes the gas limit of the next block after parent. It aims +// CalcGasLimit computes the gas limit of the next Block after parent. It aims // to keep the baseline gas close to the provided target, and increase it towards // the target if the baseline gas is lower. func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 {