From 10b478daad8bfa866e5482ea3184929b0d5f3e44 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 7 Aug 2023 10:54:33 +0200 Subject: [PATCH] core: move check from state processor to header validation, update types --- beacon/engine/types.go | 2 ++ consensus/beacon/consensus.go | 22 ++++++++++++++-------- core/genesis.go | 3 +++ core/state_processor.go | 9 --------- core/state_processor_test.go | 3 +++ core/types/block.go | 4 ++++ core/types/gen_header_json.go | 6 ++++++ core/types/gen_header_rlp.go | 16 ++++++++++++---- 8 files changed, 44 insertions(+), 21 deletions(-) diff --git a/beacon/engine/types.go b/beacon/engine/types.go index f1801edd1a..cf06a6333e 100644 --- a/beacon/engine/types.go +++ b/beacon/engine/types.go @@ -226,6 +226,7 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash) WithdrawalsHash: withdrawalsRoot, ExcessBlobGas: params.ExcessBlobGas, BlobGasUsed: params.BlobGasUsed, + // TODO BeaconRoot } block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */).WithWithdrawals(params.Withdrawals) if block.Hash() != params.BlockHash { @@ -255,6 +256,7 @@ func BlockToExecutableData(block *types.Block, fees *big.Int, blobs []kzg4844.Bl Withdrawals: block.Withdrawals(), BlobGasUsed: block.BlobGasUsed(), ExcessBlobGas: block.ExcessBlobGas(), + // TODO BeaconRoot } blobsBundle := BlobsBundleV1{ Commitments: make([]hexutil.Bytes, 0), diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index 1ad4358cff..49dca8f520 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -269,15 +269,21 @@ func (beacon *Beacon) verifyHeader(chain consensus.ChainHeaderReader, header, pa if !shanghai && header.WithdrawalsHash != nil { return fmt.Errorf("invalid withdrawalsHash: have %x, expected nil", header.WithdrawalsHash) } - // Verify the existence / non-existence of excessBlobGas + // Verify the existence / non-existence of cancun-specific header fields cancun := chain.Config().IsCancun(header.Number, header.Time) - if !cancun && header.ExcessBlobGas != nil { - return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", header.ExcessBlobGas) - } - if !cancun && header.BlobGasUsed != nil { - return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", header.BlobGasUsed) - } - if cancun { + if !cancun { + switch { + case header.ExcessBlobGas != nil: + return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", header.ExcessBlobGas) + case header.BlobGasUsed != nil: + return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", header.BlobGasUsed) + case header.BeaconRoot != nil: + return fmt.Errorf("invalid beaconRoot, have %#x, expected nil", header.BeaconRoot) + } + } else { + if header.BeaconRoot == nil { + return errors.New("header is missing beaconRoot") + } if err := eip4844.VerifyEIP4844Header(parent, header); err != nil { return err } diff --git a/core/genesis.go b/core/genesis.go index 33716d0b61..1742c70e9e 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -485,6 +485,9 @@ func (g *Genesis) ToBlock() *types.Block { if head.BlobGasUsed == nil { head.BlobGasUsed = new(uint64) } + if head.BeaconRoot == nil { + head.BeaconRoot = new(common.Hash) + } } } return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil)).WithWithdrawals(withdrawals) diff --git a/core/state_processor.go b/core/state_processor.go index 103465c649..bd066b0613 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -72,15 +72,6 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { misc.ApplyDAOHardFork(statedb) } - if p.config.IsCancun(blockNumber, block.Time()) { - if header.BeaconRoot == nil { - return nil, nil, 0, errors.New("expected beacon root post-cancun") - } - beaconRoot = header.BeaconRoot - } else if header.BeaconRoot != nil { - return nil, nil, 0, errors.New("beacon root set pre-cancun") - } - var ( context = NewEVMBlockContext(header, p.bc, nil) vmenv = vm.NewEVM(context, vm.TxContext{}, statedb, p.config, cfg) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index b4482acf35..2318a129a0 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -410,6 +410,9 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr used := uint64(nBlobs * params.BlobTxBlobGasPerBlob) header.ExcessBlobGas = &excess header.BlobGasUsed = &used + + beaconRoot := common.HexToHash("0xbeac00") + header.BeaconRoot = &beaconRoot } // Assemble and return the final block for sealing if config.IsShanghai(header.Number, header.Time) { diff --git a/core/types/block.go b/core/types/block.go index f97b4d3b9d..d3b0070026 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -300,6 +300,10 @@ func CopyHeader(h *Header) *Header { cpy.BlobGasUsed = new(uint64) *cpy.BlobGasUsed = *h.BlobGasUsed } + if h.BeaconRoot != nil { + cpy.BeaconRoot = new(common.Hash) + *cpy.BeaconRoot = *h.BeaconRoot + } return &cpy } diff --git a/core/types/gen_header_json.go b/core/types/gen_header_json.go index bac475959d..0169b4366a 100644 --- a/core/types/gen_header_json.go +++ b/core/types/gen_header_json.go @@ -35,6 +35,7 @@ func (h Header) MarshalJSON() ([]byte, error) { WithdrawalsHash *common.Hash `json:"withdrawalsRoot" rlp:"optional"` BlobGasUsed *hexutil.Uint64 `json:"blobGasUsed" rlp:"optional"` ExcessBlobGas *hexutil.Uint64 `json:"excessBlobGas" rlp:"optional"` + BeaconRoot *common.Hash `json:"beaconRoot" rlp:"optional"` Hash common.Hash `json:"hash"` } var enc Header @@ -57,6 +58,7 @@ func (h Header) MarshalJSON() ([]byte, error) { enc.WithdrawalsHash = h.WithdrawalsHash enc.BlobGasUsed = (*hexutil.Uint64)(h.BlobGasUsed) enc.ExcessBlobGas = (*hexutil.Uint64)(h.ExcessBlobGas) + enc.BeaconRoot = h.BeaconRoot enc.Hash = h.Hash() return json.Marshal(&enc) } @@ -83,6 +85,7 @@ func (h *Header) UnmarshalJSON(input []byte) error { WithdrawalsHash *common.Hash `json:"withdrawalsRoot" rlp:"optional"` BlobGasUsed *hexutil.Uint64 `json:"blobGasUsed" rlp:"optional"` ExcessBlobGas *hexutil.Uint64 `json:"excessBlobGas" rlp:"optional"` + BeaconRoot *common.Hash `json:"beaconRoot" rlp:"optional"` } var dec Header if err := json.Unmarshal(input, &dec); err != nil { @@ -157,5 +160,8 @@ func (h *Header) UnmarshalJSON(input []byte) error { if dec.ExcessBlobGas != nil { h.ExcessBlobGas = (*uint64)(dec.ExcessBlobGas) } + if dec.BeaconRoot != nil { + h.BeaconRoot = dec.BeaconRoot + } return nil } diff --git a/core/types/gen_header_rlp.go b/core/types/gen_header_rlp.go index a5ed5cd150..62e585bffb 100644 --- a/core/types/gen_header_rlp.go +++ b/core/types/gen_header_rlp.go @@ -44,7 +44,8 @@ func (obj *Header) EncodeRLP(_w io.Writer) error { _tmp2 := obj.WithdrawalsHash != nil _tmp3 := obj.BlobGasUsed != nil _tmp4 := obj.ExcessBlobGas != nil - if _tmp1 || _tmp2 || _tmp3 || _tmp4 { + _tmp5 := obj.BeaconRoot != nil + if _tmp1 || _tmp2 || _tmp3 || _tmp4 || _tmp5 { if obj.BaseFee == nil { w.Write(rlp.EmptyString) } else { @@ -54,27 +55,34 @@ func (obj *Header) EncodeRLP(_w io.Writer) error { w.WriteBigInt(obj.BaseFee) } } - if _tmp2 || _tmp3 || _tmp4 { + if _tmp2 || _tmp3 || _tmp4 || _tmp5 { if obj.WithdrawalsHash == nil { w.Write([]byte{0x80}) } else { w.WriteBytes(obj.WithdrawalsHash[:]) } } - if _tmp3 || _tmp4 { + if _tmp3 || _tmp4 || _tmp5 { if obj.BlobGasUsed == nil { w.Write([]byte{0x80}) } else { w.WriteUint64((*obj.BlobGasUsed)) } } - if _tmp4 { + if _tmp4 || _tmp5 { if obj.ExcessBlobGas == nil { w.Write([]byte{0x80}) } else { w.WriteUint64((*obj.ExcessBlobGas)) } } + if _tmp5 { + if obj.BeaconRoot == nil { + w.Write([]byte{0x80}) + } else { + w.WriteBytes(obj.BeaconRoot[:]) + } + } w.ListEnd(_tmp0) return w.Flush() }