Skip to content

Commit 2730a58

Browse files
alarso16ceyonur
authored andcommitted
Remove duplicate cancun checks (#1223)
Co-authored-by: Ceyhun Onur <ceyhun.onur@avalabs.org>
1 parent 9af16ac commit 2730a58

File tree

2 files changed

+39
-52
lines changed

2 files changed

+39
-52
lines changed

consensus/dummy/consensus.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"math/big"
1010

1111
"github.com/ava-labs/libevm/common"
12-
"github.com/ava-labs/libevm/consensus/misc/eip4844"
1312
"github.com/ava-labs/libevm/core/state"
1413
"github.com/ava-labs/libevm/core/types"
1514
"github.com/ava-labs/libevm/trie"
@@ -23,7 +22,13 @@ import (
2322
"github.com/ava-labs/subnet-evm/utils"
2423
)
2524

26-
var errUnclesUnsupported = errors.New("uncles unsupported")
25+
var (
26+
errUnclesUnsupported = errors.New("uncles unsupported")
27+
errExtDataGasUsedNil = errors.New("extDataGasUsed is nil")
28+
errExtDataGasUsedTooLarge = errors.New("extDataGasUsed is not uint64")
29+
ErrInvalidBlockGasCost = errors.New("invalid blockGasCost")
30+
errInvalidExtDataGasUsed = errors.New("invalid extDataGasUsed")
31+
)
2732

2833
type Mode struct {
2934
ModeSkipHeader bool
@@ -165,31 +170,6 @@ func (eng *DummyEngine) verifyHeader(chain consensus.ChainHeaderReader, header *
165170
if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 {
166171
return consensus.ErrInvalidNumber
167172
}
168-
// Verify the existence / non-existence of excessBlobGas
169-
cancun := chain.Config().IsCancun(header.Number, header.Time)
170-
if !cancun {
171-
switch {
172-
case header.ExcessBlobGas != nil:
173-
return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", *header.ExcessBlobGas)
174-
case header.BlobGasUsed != nil:
175-
return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", *header.BlobGasUsed)
176-
case header.ParentBeaconRoot != nil:
177-
return fmt.Errorf("invalid parentBeaconRoot, have %#x, expected nil", *header.ParentBeaconRoot)
178-
}
179-
} else {
180-
if header.ParentBeaconRoot == nil {
181-
return errors.New("header is missing beaconRoot")
182-
}
183-
if *header.ParentBeaconRoot != (common.Hash{}) {
184-
return fmt.Errorf("invalid parentBeaconRoot, have %#x, expected empty", *header.ParentBeaconRoot)
185-
}
186-
if err := eip4844.VerifyEIP4844Header(parent, header); err != nil {
187-
return err
188-
}
189-
if *header.BlobGasUsed > 0 { // VerifyEIP4844Header ensures BlobGasUsed is non-nil
190-
return fmt.Errorf("blobs not enabled on avalanche networks: used %d blob gas, expected 0", *header.BlobGasUsed)
191-
}
192-
}
193173
return nil
194174
}
195175

plugin/evm/wrapped_block.go

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,25 @@ var (
3434
_ snowman.Block = (*wrappedBlock)(nil)
3535
_ block.WithVerifyContext = (*wrappedBlock)(nil)
3636

37-
errInvalidParent = errors.New("parent header not found")
3837
errMissingParentBlock = errors.New("missing parent block")
3938
errInvalidGasUsedRelativeToCapacity = errors.New("invalid gas used relative to capacity")
4039
errTotalIntrinsicGasCostExceedsClaimed = errors.New("total intrinsic gas cost is greater than claimed gas used")
4140
)
4241

43-
// wrappedBlock implements the snowman.Block interface by wrapping a libevm block
42+
// Sentinel errors for header validation in this file
43+
var (
44+
errInvalidExcessBlobGasBeforeCancun = errors.New("invalid excessBlobGas before cancun")
45+
errInvalidBlobGasUsedBeforeCancun = errors.New("invalid blobGasUsed before cancun")
46+
errInvalidParent = errors.New("parent header not found")
47+
errInvalidParentBeaconRootBeforeCancun = errors.New("invalid parentBeaconRoot before cancun")
48+
errInvalidExcessBlobGas = errors.New("invalid excessBlobGas")
49+
errMissingParentBeaconRoot = errors.New("header is missing parentBeaconRoot")
50+
errParentBeaconRootNonEmpty = errors.New("invalid non-empty parentBeaconRoot")
51+
errBlobGasUsedNilInCancun = errors.New("blob gas used must not be nil in Cancun")
52+
errBlobsNotEnabled = errors.New("blobs not enabled on avalanche networks")
53+
)
54+
55+
// wrappedBlock implements the snowman.wrappedBlock interface
4456
type wrappedBlock struct {
4557
id ids.ID
4658
ethBlock *types.Block
@@ -285,7 +297,6 @@ func (b *wrappedBlock) semanticVerify() error {
285297
if parent == nil {
286298
return fmt.Errorf("%w: %s at height %d", errInvalidParent, b.ethBlock.ParentHash(), b.ethBlock.NumberU64()-1)
287299
}
288-
289300
// Ensure Time and TimeMilliseconds are consistent with rules.
290301
if err := customheader.VerifyTime(extraConfig, parent, b.ethBlock.Header(), b.vm.clock.Time()); err != nil {
291302
return err
@@ -374,33 +385,29 @@ func (b *wrappedBlock) syntacticVerify() error {
374385
}
375386

376387
// Verify the existence / non-existence of excessBlobGas
377-
cancun := rules.IsCancun
378-
if !cancun && ethHeader.ExcessBlobGas != nil {
379-
return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", *ethHeader.ExcessBlobGas)
380-
}
381-
if !cancun && ethHeader.BlobGasUsed != nil {
382-
return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", *ethHeader.BlobGasUsed)
383-
}
384-
if cancun && ethHeader.ExcessBlobGas == nil {
385-
return errors.New("header is missing excessBlobGas")
386-
}
387-
if cancun && ethHeader.BlobGasUsed == nil {
388-
return errors.New("header is missing blobGasUsed")
389-
}
390-
if !cancun && ethHeader.ParentBeaconRoot != nil {
391-
return fmt.Errorf("invalid parentBeaconRoot: have %x, expected nil", *ethHeader.ParentBeaconRoot)
392-
}
393-
if cancun {
388+
if rules.IsCancun {
394389
switch {
395390
case ethHeader.ParentBeaconRoot == nil:
396391
return errors.New("header is missing parentBeaconRoot")
397392
case *ethHeader.ParentBeaconRoot != (common.Hash{}):
398-
return fmt.Errorf("invalid parentBeaconRoot: have %x, expected empty hash", ethHeader.ParentBeaconRoot)
393+
return fmt.Errorf("%w: have %x, expected empty hash", errParentBeaconRootNonEmpty, ethHeader.ParentBeaconRoot)
394+
case ethHeader.BlobGasUsed == nil:
395+
return errBlobGasUsedNilInCancun
396+
case *ethHeader.BlobGasUsed != 0:
397+
return fmt.Errorf("%w: used %d blob gas, expected 0", errBlobsNotEnabled, *ethHeader.BlobGasUsed)
398+
case ethHeader.ExcessBlobGas == nil:
399+
return fmt.Errorf("%w: have nil, expected 0", errInvalidExcessBlobGas)
400+
case *ethHeader.ExcessBlobGas != 0:
401+
return fmt.Errorf("%w: have %d, expected 0", errInvalidExcessBlobGas, *ethHeader.ExcessBlobGas)
399402
}
400-
if ethHeader.BlobGasUsed == nil {
401-
return errors.New("blob gas used must not be nil in Cancun")
402-
} else if *ethHeader.BlobGasUsed > 0 {
403-
return fmt.Errorf("blobs not enabled on avalanche networks: used %d blob gas, expected 0", *ethHeader.BlobGasUsed)
403+
} else {
404+
switch {
405+
case ethHeader.ExcessBlobGas != nil:
406+
return fmt.Errorf("%w: have %d, expected nil", errInvalidExcessBlobGasBeforeCancun, *ethHeader.ExcessBlobGas)
407+
case ethHeader.BlobGasUsed != nil:
408+
return fmt.Errorf("%w: have %d, expected nil", errInvalidBlobGasUsedBeforeCancun, *ethHeader.BlobGasUsed)
409+
case ethHeader.ParentBeaconRoot != nil:
410+
return fmt.Errorf("%w: have %x, expected nil", errInvalidParentBeaconRootBeforeCancun, *ethHeader.ParentBeaconRoot)
404411
}
405412
}
406413

0 commit comments

Comments
 (0)