fix(consensus): hardening header verification #33860 (#2071)

This commit is contained in:
Daniel Liu 2026-03-06 13:40:32 +08:00 committed by GitHub
parent 3fad5e1ab4
commit 2eed6c057b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 89 additions and 4 deletions

View file

@ -235,7 +235,7 @@ func (x *XDPoS_v1) verifyCascadingFields(chain consensus.ChainReader, header *ty
return utils.ErrInvalidTimestamp
}
// Verify the header's EIP-1559 attributes.
if err := eip1559.VerifyEip1559Header(chain.Config(), header); err != nil {
if err := eip1559.VerifyEip1559Header(chain.Config(), parent, header); err != nil {
return err
}

View file

@ -110,7 +110,7 @@ func (x *XDPoS_v2) verifyHeader(chain consensus.ChainReader, header *types.Heade
return utils.ErrInvalidUncleHash
}
// Verify the header's EIP-1559 attributes.
if err := eip1559.VerifyEip1559Header(chain.Config(), header); err != nil {
if err := eip1559.VerifyEip1559Header(chain.Config(), parent, header); err != nil {
return err
}
if header.Difficulty.Cmp(big.NewInt(1)) != 0 {

View file

@ -202,7 +202,7 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
return err
}
// Verify the header's EIP-1559 attributes.
if err := eip1559.VerifyEip1559Header(chain.Config(), header); err != nil {
if err := eip1559.VerifyEip1559Header(chain.Config(), parent, header); err != nil {
return err
}

View file

@ -29,7 +29,7 @@ import (
// VerifyEip1559Header verifies some header attributes which were changed in EIP-1559,
// - gas limit check
// - basefee check
func VerifyEip1559Header(config *params.ChainConfig, header *types.Header) error {
func VerifyEip1559Header(config *params.ChainConfig, parent, header *types.Header) error {
if !config.IsEIP1559(header.Number) {
if header.BaseFee != nil {
return fmt.Errorf("invalid baseFee: have %s, want <nil>",
@ -42,6 +42,10 @@ func VerifyEip1559Header(config *params.ChainConfig, header *types.Header) error
if header.BaseFee == nil {
return errors.New("header is missing baseFee")
}
// Verify the parent header is not malformed
if parent != nil && config.IsEIP1559(parent.Number) && parent.BaseFee == nil {
return errors.New("parent header is missing baseFee")
}
// Verify the baseFee is correct based on the current header.
expectedBaseFee := CalcBaseFee(config, header)

View file

@ -0,0 +1,81 @@
// Copyright 2021 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package eip1559
import (
"math/big"
"testing"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/params"
)
func testConfigEip1559() *params.ChainConfig {
config := *params.TestChainConfig
config.Eip1559Block = big.NewInt(1)
return &config
}
func TestVerifyEip1559HeaderParentBaseFee(t *testing.T) {
config := testConfigEip1559()
for _, tc := range []struct {
name string
parent *types.Header
headerNum int64
wantOk bool
}{
{
name: "eip1559 parent missing basefee",
parent: &types.Header{
Number: big.NewInt(1),
},
headerNum: 2,
wantOk: false,
},
{
name: "eip1559 parent with basefee",
parent: &types.Header{
Number: big.NewInt(1),
BaseFee: new(big.Int).Set(common.BaseFee),
},
headerNum: 2,
wantOk: true,
},
{
name: "pre-eip1559 parent ignores basefee",
parent: &types.Header{
Number: big.NewInt(0),
},
headerNum: 1,
wantOk: true,
},
} {
header := &types.Header{
Number: big.NewInt(tc.headerNum),
BaseFee: new(big.Int).Set(common.BaseFee),
}
err := VerifyEip1559Header(config, tc.parent, header)
if tc.wantOk && err != nil {
t.Fatalf("%s: expected no error, got %v", tc.name, err)
}
if !tc.wantOk && err == nil {
t.Fatalf("%s: expected error, got nil", tc.name)
}
}
}