From 319e99e72a0ba9d6e98c80e3fec3b9d25e0214ef Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Tue, 23 May 2023 22:14:18 +0200 Subject: [PATCH] Skip gas limit checks for chains with gas limit contract (#7567) Dirty hack to fix Chiado until #7151 is properly implemented. Should fix the following error: ``` [WARN] [05-23|17:07:39.303] Verification failed for header hash=0x4eee718e5958a862746ea9a9531a16e6a404698d3a0e8120e8aff743b9f1e0a2 height=1 err="invalid gas limit: have 12500000, want 10000000 +-= 9764" ``` --- consensus/aura/aura.go | 6 +++++- consensus/bor/bor.go | 2 +- consensus/clique/verifier.go | 13 +++---------- consensus/ethash/consensus.go | 20 ++++++++------------ consensus/merge/merge.go | 6 +++++- consensus/misc/eip1559.go | 18 ++++++++++-------- consensus/misc/eip1559_test.go | 2 +- 7 files changed, 33 insertions(+), 34 deletions(-) diff --git a/consensus/aura/aura.go b/consensus/aura/aura.go index 3def0aa21..445265c39 100644 --- a/consensus/aura/aura.go +++ b/consensus/aura/aura.go @@ -493,7 +493,11 @@ func (c *AuRa) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Hea log.Error("consensus.ErrUnknownAncestor", "parentNum", number-1, "hash", header.ParentHash.String()) return consensus.ErrUnknownAncestor } - return ethash.VerifyHeaderBasics(chain, header, parent, false) + return ethash.VerifyHeaderBasics(chain, header, parent, true /*checkTimestamp*/, c.HasGasLimitContract() /*skipGasLimit*/) +} + +func (c *AuRa) HasGasLimitContract() bool { + return len(c.cfg.BlockRewardContractTransitions) != 0 } // nolint diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index c6f84b245..7d9ddb3ee 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -461,7 +461,7 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil { return err } - } else if err := misc.VerifyEip1559Header(chain.Config(), parent, header); err != nil { + } else if err := misc.VerifyEip1559Header(chain.Config(), parent, header, false /*skipGasLimit*/); err != nil { // Verify the header's EIP-1559 attributes. return err } diff --git a/consensus/clique/verifier.go b/consensus/clique/verifier.go index 572d17510..ce6583225 100644 --- a/consensus/clique/verifier.go +++ b/consensus/clique/verifier.go @@ -115,17 +115,10 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header if header.BaseFee != nil { return fmt.Errorf("invalid baseFee before fork: have %d, want ", header.BaseFee) } - // Verify that the gas limit remains within allowed bounds - diff := int64(parent.GasLimit) - int64(header.GasLimit) - if diff < 0 { - diff *= -1 + if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil { + return err } - limit := parent.GasLimit / params.GasLimitBoundDivisor - - if uint64(diff) >= limit || header.GasLimit < params.MinGasLimit { - return fmt.Errorf("invalid gas limit: have %d, want %d += %d", header.GasLimit, parent.GasLimit, limit) - } - } else if err := misc.VerifyEip1559Header(chain.Config(), parent, header); err != nil { + } else if err := misc.VerifyEip1559Header(chain.Config(), parent, header, false /*skipGasLimit*/); err != nil { // Verify the header's EIP-1559 attributes. return err } diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index c7dfc46bc..d0d25eea7 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -197,13 +197,13 @@ func (ethash *Ethash) VerifyUncle(chain consensus.ChainHeaderReader, header *typ return ethash.verifyHeader(chain, uncle, ancestors[uncle.ParentHash], true, seal) } -func VerifyHeaderBasics(chain consensus.ChainHeaderReader, header, parent *types.Header, uncle bool) error { +func VerifyHeaderBasics(chain consensus.ChainHeaderReader, header, parent *types.Header, checkTimestamp, skipGasLimit bool) error { // Ensure that the header's extra-data section is of a reasonable size if uint64(len(header.Extra)) > params.MaximumExtraDataSize { return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize) } // Verify the header's timestamp - if !uncle { + if checkTimestamp { unixNow := time.Now().Unix() if header.Time > uint64(unixNow+allowedFutureBlockTimeSeconds) { return consensus.ErrFutureBlock @@ -226,16 +226,12 @@ func VerifyHeaderBasics(chain consensus.ChainHeaderReader, header, parent *types if header.BaseFee != nil { return fmt.Errorf("invalid baseFee before fork: have %d, expected 'nil'", header.BaseFee) } - // Verify that the gas limit remains within allowed bounds - diff := int64(parent.GasLimit) - int64(header.GasLimit) - if diff < 0 { - diff *= -1 + if !skipGasLimit { + if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil { + return err + } } - limit := parent.GasLimit / params.GasLimitBoundDivisor - if uint64(diff) >= limit || header.GasLimit < params.MinGasLimit { - return fmt.Errorf("invalid gas limit: have %d, want %d += %d", header.GasLimit, parent.GasLimit, limit) - } - } else if err := misc.VerifyEip1559Header(chain.Config(), parent, header); err != nil { + } else if err := misc.VerifyEip1559Header(chain.Config(), parent, header, skipGasLimit); err != nil { // Verify the header's EIP-1559 attributes. return err } @@ -263,7 +259,7 @@ func VerifyHeaderBasics(chain consensus.ChainHeaderReader, header, parent *types // stock Ethereum ethash engine. // See YP section 4.3.4. "Block Header Validity" func (ethash *Ethash) verifyHeader(chain consensus.ChainHeaderReader, header, parent *types.Header, uncle bool, seal bool) error { - if err := VerifyHeaderBasics(chain, header, parent, uncle); err != nil { + if err := VerifyHeaderBasics(chain, header, parent, !uncle /*checkTimestamp*/, false /*skipGasLimit*/); err != nil { return err } // Verify the block's difficulty based on its timestamp and parent's difficulty diff --git a/consensus/merge/merge.go b/consensus/merge/merge.go index e58b6387a..276ca4c9e 100644 --- a/consensus/merge/merge.go +++ b/consensus/merge/merge.go @@ -237,7 +237,11 @@ func (s *Merge) verifyHeader(chain consensus.ChainHeaderReader, header, parent * return errInvalidUncleHash } - if err := misc.VerifyEip1559Header(chain.Config(), parent, header); err != nil { + skipGasLimit := false + if auraEngine, ok := s.eth1Engine.(*aura.AuRa); ok { + skipGasLimit = auraEngine.HasGasLimitContract() + } + if err := misc.VerifyEip1559Header(chain.Config(), parent, header, skipGasLimit); err != nil { return err } diff --git a/consensus/misc/eip1559.go b/consensus/misc/eip1559.go index bf9490961..bd4305506 100644 --- a/consensus/misc/eip1559.go +++ b/consensus/misc/eip1559.go @@ -31,14 +31,16 @@ import ( // VerifyEip1559Header verifies some header attributes which were changed in EIP-1559, // - gas limit check // - basefee check -func VerifyEip1559Header(config *chain.Config, parent, header *types.Header) error { - // Verify that the gas limit remains within allowed bounds - parentGasLimit := parent.GasLimit - if !config.IsLondon(parent.Number.Uint64()) { - parentGasLimit = parent.GasLimit * params.ElasticityMultiplier - } - if err := VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil { - return err +func VerifyEip1559Header(config *chain.Config, parent, header *types.Header, skipGasLimit bool) error { + if !skipGasLimit { + // Verify that the gas limit remains within allowed bounds + parentGasLimit := parent.GasLimit + if !config.IsLondon(parent.Number.Uint64()) { + parentGasLimit = parent.GasLimit * params.ElasticityMultiplier + } + if err := VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil { + return err + } } // Verify the header is not malformed if header.BaseFee == nil { diff --git a/consensus/misc/eip1559_test.go b/consensus/misc/eip1559_test.go index 4abef2d20..c937880ff 100644 --- a/consensus/misc/eip1559_test.go +++ b/consensus/misc/eip1559_test.go @@ -80,7 +80,7 @@ func TestBlockGasLimits(t *testing.T) { BaseFee: initial, Number: big.NewInt(tc.pNum + 1), } - err := VerifyEip1559Header(config(), parent, header) + err := VerifyEip1559Header(config(), parent, header, false /*skipGasLimit*/) if tc.ok && err != nil { t.Errorf("test %d: Expected valid header: %s", i, err) }