core: move check from state processor to header validation, update types

This commit is contained in:
Martin Holst Swende 2023-08-07 10:54:33 +02:00
parent 71ade509bf
commit 10b478daad
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
8 changed files with 44 additions and 21 deletions

View file

@ -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),

View file

@ -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
}

View file

@ -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)

View file

@ -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)

View file

@ -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) {

View file

@ -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
}

View file

@ -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
}

View file

@ -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()
}