From 2730a5851707f95bfdaea908549ef6410776fa0f Mon Sep 17 00:00:00 2001 From: Austin Larson <78000745+alarso16@users.noreply.github.com> Date: Wed, 17 Sep 2025 16:52:07 -0400 Subject: [PATCH 1/2] Remove duplicate cancun checks (#1223) Co-authored-by: Ceyhun Onur --- consensus/dummy/consensus.go | 34 +++++---------------- plugin/evm/wrapped_block.go | 57 ++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 52 deletions(-) diff --git a/consensus/dummy/consensus.go b/consensus/dummy/consensus.go index a67a26bfb1..44d168c591 100644 --- a/consensus/dummy/consensus.go +++ b/consensus/dummy/consensus.go @@ -9,7 +9,6 @@ import ( "math/big" "github.com/ava-labs/libevm/common" - "github.com/ava-labs/libevm/consensus/misc/eip4844" "github.com/ava-labs/libevm/core/state" "github.com/ava-labs/libevm/core/types" "github.com/ava-labs/libevm/trie" @@ -23,7 +22,13 @@ import ( "github.com/ava-labs/subnet-evm/utils" ) -var errUnclesUnsupported = errors.New("uncles unsupported") +var ( + errUnclesUnsupported = errors.New("uncles unsupported") + errExtDataGasUsedNil = errors.New("extDataGasUsed is nil") + errExtDataGasUsedTooLarge = errors.New("extDataGasUsed is not uint64") + ErrInvalidBlockGasCost = errors.New("invalid blockGasCost") + errInvalidExtDataGasUsed = errors.New("invalid extDataGasUsed") +) type Mode struct { ModeSkipHeader bool @@ -165,31 +170,6 @@ func (eng *DummyEngine) verifyHeader(chain consensus.ChainHeaderReader, header * if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 { return consensus.ErrInvalidNumber } - // Verify the existence / non-existence of excessBlobGas - cancun := chain.Config().IsCancun(header.Number, header.Time) - 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.ParentBeaconRoot != nil: - return fmt.Errorf("invalid parentBeaconRoot, have %#x, expected nil", *header.ParentBeaconRoot) - } - } else { - if header.ParentBeaconRoot == nil { - return errors.New("header is missing beaconRoot") - } - if *header.ParentBeaconRoot != (common.Hash{}) { - return fmt.Errorf("invalid parentBeaconRoot, have %#x, expected empty", *header.ParentBeaconRoot) - } - if err := eip4844.VerifyEIP4844Header(parent, header); err != nil { - return err - } - if *header.BlobGasUsed > 0 { // VerifyEIP4844Header ensures BlobGasUsed is non-nil - return fmt.Errorf("blobs not enabled on avalanche networks: used %d blob gas, expected 0", *header.BlobGasUsed) - } - } return nil } diff --git a/plugin/evm/wrapped_block.go b/plugin/evm/wrapped_block.go index dbab069172..3ebc236594 100644 --- a/plugin/evm/wrapped_block.go +++ b/plugin/evm/wrapped_block.go @@ -34,13 +34,25 @@ var ( _ snowman.Block = (*wrappedBlock)(nil) _ block.WithVerifyContext = (*wrappedBlock)(nil) - errInvalidParent = errors.New("parent header not found") errMissingParentBlock = errors.New("missing parent block") errInvalidGasUsedRelativeToCapacity = errors.New("invalid gas used relative to capacity") errTotalIntrinsicGasCostExceedsClaimed = errors.New("total intrinsic gas cost is greater than claimed gas used") ) -// wrappedBlock implements the snowman.Block interface by wrapping a libevm block +// Sentinel errors for header validation in this file +var ( + errInvalidExcessBlobGasBeforeCancun = errors.New("invalid excessBlobGas before cancun") + errInvalidBlobGasUsedBeforeCancun = errors.New("invalid blobGasUsed before cancun") + errInvalidParent = errors.New("parent header not found") + errInvalidParentBeaconRootBeforeCancun = errors.New("invalid parentBeaconRoot before cancun") + errInvalidExcessBlobGas = errors.New("invalid excessBlobGas") + errMissingParentBeaconRoot = errors.New("header is missing parentBeaconRoot") + errParentBeaconRootNonEmpty = errors.New("invalid non-empty parentBeaconRoot") + errBlobGasUsedNilInCancun = errors.New("blob gas used must not be nil in Cancun") + errBlobsNotEnabled = errors.New("blobs not enabled on avalanche networks") +) + +// wrappedBlock implements the snowman.wrappedBlock interface type wrappedBlock struct { id ids.ID ethBlock *types.Block @@ -285,7 +297,6 @@ func (b *wrappedBlock) semanticVerify() error { if parent == nil { return fmt.Errorf("%w: %s at height %d", errInvalidParent, b.ethBlock.ParentHash(), b.ethBlock.NumberU64()-1) } - // Ensure Time and TimeMilliseconds are consistent with rules. if err := customheader.VerifyTime(extraConfig, parent, b.ethBlock.Header(), b.vm.clock.Time()); err != nil { return err @@ -374,33 +385,29 @@ func (b *wrappedBlock) syntacticVerify() error { } // Verify the existence / non-existence of excessBlobGas - cancun := rules.IsCancun - if !cancun && ethHeader.ExcessBlobGas != nil { - return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", *ethHeader.ExcessBlobGas) - } - if !cancun && ethHeader.BlobGasUsed != nil { - return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", *ethHeader.BlobGasUsed) - } - if cancun && ethHeader.ExcessBlobGas == nil { - return errors.New("header is missing excessBlobGas") - } - if cancun && ethHeader.BlobGasUsed == nil { - return errors.New("header is missing blobGasUsed") - } - if !cancun && ethHeader.ParentBeaconRoot != nil { - return fmt.Errorf("invalid parentBeaconRoot: have %x, expected nil", *ethHeader.ParentBeaconRoot) - } - if cancun { + if rules.IsCancun { switch { case ethHeader.ParentBeaconRoot == nil: return errors.New("header is missing parentBeaconRoot") case *ethHeader.ParentBeaconRoot != (common.Hash{}): - return fmt.Errorf("invalid parentBeaconRoot: have %x, expected empty hash", ethHeader.ParentBeaconRoot) + return fmt.Errorf("%w: have %x, expected empty hash", errParentBeaconRootNonEmpty, ethHeader.ParentBeaconRoot) + case ethHeader.BlobGasUsed == nil: + return errBlobGasUsedNilInCancun + case *ethHeader.BlobGasUsed != 0: + return fmt.Errorf("%w: used %d blob gas, expected 0", errBlobsNotEnabled, *ethHeader.BlobGasUsed) + case ethHeader.ExcessBlobGas == nil: + return fmt.Errorf("%w: have nil, expected 0", errInvalidExcessBlobGas) + case *ethHeader.ExcessBlobGas != 0: + return fmt.Errorf("%w: have %d, expected 0", errInvalidExcessBlobGas, *ethHeader.ExcessBlobGas) } - if ethHeader.BlobGasUsed == nil { - return errors.New("blob gas used must not be nil in Cancun") - } else if *ethHeader.BlobGasUsed > 0 { - return fmt.Errorf("blobs not enabled on avalanche networks: used %d blob gas, expected 0", *ethHeader.BlobGasUsed) + } else { + switch { + case ethHeader.ExcessBlobGas != nil: + return fmt.Errorf("%w: have %d, expected nil", errInvalidExcessBlobGasBeforeCancun, *ethHeader.ExcessBlobGas) + case ethHeader.BlobGasUsed != nil: + return fmt.Errorf("%w: have %d, expected nil", errInvalidBlobGasUsedBeforeCancun, *ethHeader.BlobGasUsed) + case ethHeader.ParentBeaconRoot != nil: + return fmt.Errorf("%w: have %x, expected nil", errInvalidParentBeaconRootBeforeCancun, *ethHeader.ParentBeaconRoot) } } From 1ad967d32248b187231a3fa266db7b3058fa9b8d Mon Sep 17 00:00:00 2001 From: Jonathan Oppenheimer Date: Wed, 8 Oct 2025 13:42:02 -0400 Subject: [PATCH 2/2] lint --- consensus/dummy/consensus.go | 8 +------- plugin/evm/wrapped_block.go | 1 - 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/consensus/dummy/consensus.go b/consensus/dummy/consensus.go index 44d168c591..bae368e6a8 100644 --- a/consensus/dummy/consensus.go +++ b/consensus/dummy/consensus.go @@ -22,13 +22,7 @@ import ( "github.com/ava-labs/subnet-evm/utils" ) -var ( - errUnclesUnsupported = errors.New("uncles unsupported") - errExtDataGasUsedNil = errors.New("extDataGasUsed is nil") - errExtDataGasUsedTooLarge = errors.New("extDataGasUsed is not uint64") - ErrInvalidBlockGasCost = errors.New("invalid blockGasCost") - errInvalidExtDataGasUsed = errors.New("invalid extDataGasUsed") -) +var errUnclesUnsupported = errors.New("uncles unsupported") type Mode struct { ModeSkipHeader bool diff --git a/plugin/evm/wrapped_block.go b/plugin/evm/wrapped_block.go index 3ebc236594..e9e3ddde89 100644 --- a/plugin/evm/wrapped_block.go +++ b/plugin/evm/wrapped_block.go @@ -46,7 +46,6 @@ var ( errInvalidParent = errors.New("parent header not found") errInvalidParentBeaconRootBeforeCancun = errors.New("invalid parentBeaconRoot before cancun") errInvalidExcessBlobGas = errors.New("invalid excessBlobGas") - errMissingParentBeaconRoot = errors.New("header is missing parentBeaconRoot") errParentBeaconRootNonEmpty = errors.New("invalid non-empty parentBeaconRoot") errBlobGasUsedNilInCancun = errors.New("blob gas used must not be nil in Cancun") errBlobsNotEnabled = errors.New("blobs not enabled on avalanche networks")