From 82dd9eb217a78a87699683d80546885314b721a9 Mon Sep 17 00:00:00 2001 From: Roberto Bayardo Date: Mon, 21 Oct 2024 09:00:57 -0500 Subject: [PATCH] consistently use uint64 for eip-1559 params (#406) --- consensus/misc/eip1559/eip1559.go | 37 ++++++++++++++++++-------- consensus/misc/eip1559/eip1559_test.go | 2 +- miner/worker.go | 2 +- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/consensus/misc/eip1559/eip1559.go b/consensus/misc/eip1559/eip1559.go index 08a88c5ff5..a970bdcd31 100644 --- a/consensus/misc/eip1559/eip1559.go +++ b/consensus/misc/eip1559/eip1559.go @@ -20,6 +20,7 @@ import ( "encoding/binary" "errors" "fmt" + gomath "math" "math/big" "github.com/ethereum/go-ethereum/common" @@ -56,11 +57,11 @@ func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Heade return nil } -// DecodeHolocene1599Params extracts the Holcene 1599 parameters from the encoded form: -// https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/holocene/exec-engine.md#eip1559params-encoding +// DecodeHolocene1599Params extracts the Holcene 1599 parameters from the encoded form defined here: +// https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/holocene/exec-engine.md#eip-1559-parameters-in-payloadattributesv3 // -// Returns 0,0 if the format is invalid, though ValidateHolocene1559Params should be used instead of -// this function for validity checking. +// Returns 0,0 if the format is invalid, though ValidateHolocene1559Params should be used instead of this function for +// validity checking. func DecodeHolocene1559Params(params []byte) (uint64, uint64) { if len(params) != 8 { return 0, 0 @@ -70,7 +71,11 @@ func DecodeHolocene1559Params(params []byte) (uint64, uint64) { return uint64(denominator), uint64(elasticity) } -// Decodes holocene extra data without performing full validation. +// DecodeHoloceneExtraData decodes the Holocene 1559 parameters from the encoded form defined here: +// https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/holocene/exec-engine.md#eip-1559-parameters-in-block-header +// +// Returns 0,0 if the format is invalid, though ValidateHoloceneExtraData should be used instead of this function for +// validity checking. func DecodeHoloceneExtraData(extra []byte) (uint64, uint64) { if len(extra) != 9 { return 0, 0 @@ -78,18 +83,28 @@ func DecodeHoloceneExtraData(extra []byte) (uint64, uint64) { return DecodeHolocene1559Params(extra[1:]) } -func EncodeHolocene1559Params(denom, elasticity uint32) []byte { +// EncodeHolocene1559Params encodes the eip-1559 parameters into 'PayloadAttributes.EIP1559Params' format. Will panic if +// either value is outside uint32 range. +func EncodeHolocene1559Params(denom, elasticity uint64) []byte { r := make([]byte, 8) - binary.BigEndian.PutUint32(r[:4], denom) - binary.BigEndian.PutUint32(r[4:], elasticity) + if denom > gomath.MaxUint32 || elasticity > gomath.MaxUint32 { + panic("eip-1559 parameters out of uint32 range") + } + binary.BigEndian.PutUint32(r[:4], uint32(denom)) + binary.BigEndian.PutUint32(r[4:], uint32(elasticity)) return r } -func EncodeHoloceneExtraData(denom, elasticity uint32) []byte { +// EncodeHoloceneExtraData encodes the eip-1559 parameters into the header 'ExtraData' format. Will panic if either +// value is outside uint32 range. +func EncodeHoloceneExtraData(denom, elasticity uint64) []byte { r := make([]byte, 9) + if denom > gomath.MaxUint32 || elasticity > gomath.MaxUint32 { + panic("eip-1559 parameters out of uint32 range") + } // leave version byte 0 - binary.BigEndian.PutUint32(r[1:5], denom) - binary.BigEndian.PutUint32(r[5:], elasticity) + binary.BigEndian.PutUint32(r[1:5], uint32(denom)) + binary.BigEndian.PutUint32(r[5:], uint32(elasticity)) return r } diff --git a/consensus/misc/eip1559/eip1559_test.go b/consensus/misc/eip1559/eip1559_test.go index f9cd1f40a2..c69dc80f93 100644 --- a/consensus/misc/eip1559/eip1559_test.go +++ b/consensus/misc/eip1559/eip1559_test.go @@ -195,7 +195,7 @@ func TestCalcBaseFeeOptimismHolocene(t *testing.T) { tests := []struct { parentGasUsed uint64 expectedBaseFee int64 - denom, elasticity uint32 + denom, elasticity uint64 }{ {parentGasLimit / 2, parentBaseFee, 10, 2}, // target {10_000_000, 9_666_667, 10, 2}, // below diff --git a/miner/worker.go b/miner/worker.go index ac9588e821..e3300db11b 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -262,7 +262,7 @@ func (miner *Miner) prepareWork(genParams *generateParams, witness bool) (*envir d = miner.chainConfig.BaseFeeChangeDenominator(header.Time) e = miner.chainConfig.ElasticityMultiplier() } - header.Extra = eip1559.EncodeHoloceneExtraData(uint32(d), uint32(e)) + header.Extra = eip1559.EncodeHoloceneExtraData(d, e) } else if genParams.eip1559Params != nil { return nil, errors.New("got eip1559 params, expected none") }