Skip to content

Commit 82dd9eb

Browse files
consistently use uint64 for eip-1559 params (ethereum-optimism#406)
1 parent a7d3295 commit 82dd9eb

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

consensus/misc/eip1559/eip1559.go

+26-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/binary"
2121
"errors"
2222
"fmt"
23+
gomath "math"
2324
"math/big"
2425

2526
"github.com/ethereum/go-ethereum/common"
@@ -56,11 +57,11 @@ func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Heade
5657
return nil
5758
}
5859

59-
// DecodeHolocene1599Params extracts the Holcene 1599 parameters from the encoded form:
60-
// https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/holocene/exec-engine.md#eip1559params-encoding
60+
// DecodeHolocene1599Params extracts the Holcene 1599 parameters from the encoded form defined here:
61+
// https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/holocene/exec-engine.md#eip-1559-parameters-in-payloadattributesv3
6162
//
62-
// Returns 0,0 if the format is invalid, though ValidateHolocene1559Params should be used instead of
63-
// this function for validity checking.
63+
// Returns 0,0 if the format is invalid, though ValidateHolocene1559Params should be used instead of this function for
64+
// validity checking.
6465
func DecodeHolocene1559Params(params []byte) (uint64, uint64) {
6566
if len(params) != 8 {
6667
return 0, 0
@@ -70,26 +71,40 @@ func DecodeHolocene1559Params(params []byte) (uint64, uint64) {
7071
return uint64(denominator), uint64(elasticity)
7172
}
7273

73-
// Decodes holocene extra data without performing full validation.
74+
// DecodeHoloceneExtraData decodes the Holocene 1559 parameters from the encoded form defined here:
75+
// https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/holocene/exec-engine.md#eip-1559-parameters-in-block-header
76+
//
77+
// Returns 0,0 if the format is invalid, though ValidateHoloceneExtraData should be used instead of this function for
78+
// validity checking.
7479
func DecodeHoloceneExtraData(extra []byte) (uint64, uint64) {
7580
if len(extra) != 9 {
7681
return 0, 0
7782
}
7883
return DecodeHolocene1559Params(extra[1:])
7984
}
8085

81-
func EncodeHolocene1559Params(denom, elasticity uint32) []byte {
86+
// EncodeHolocene1559Params encodes the eip-1559 parameters into 'PayloadAttributes.EIP1559Params' format. Will panic if
87+
// either value is outside uint32 range.
88+
func EncodeHolocene1559Params(denom, elasticity uint64) []byte {
8289
r := make([]byte, 8)
83-
binary.BigEndian.PutUint32(r[:4], denom)
84-
binary.BigEndian.PutUint32(r[4:], elasticity)
90+
if denom > gomath.MaxUint32 || elasticity > gomath.MaxUint32 {
91+
panic("eip-1559 parameters out of uint32 range")
92+
}
93+
binary.BigEndian.PutUint32(r[:4], uint32(denom))
94+
binary.BigEndian.PutUint32(r[4:], uint32(elasticity))
8595
return r
8696
}
8797

88-
func EncodeHoloceneExtraData(denom, elasticity uint32) []byte {
98+
// EncodeHoloceneExtraData encodes the eip-1559 parameters into the header 'ExtraData' format. Will panic if either
99+
// value is outside uint32 range.
100+
func EncodeHoloceneExtraData(denom, elasticity uint64) []byte {
89101
r := make([]byte, 9)
102+
if denom > gomath.MaxUint32 || elasticity > gomath.MaxUint32 {
103+
panic("eip-1559 parameters out of uint32 range")
104+
}
90105
// leave version byte 0
91-
binary.BigEndian.PutUint32(r[1:5], denom)
92-
binary.BigEndian.PutUint32(r[5:], elasticity)
106+
binary.BigEndian.PutUint32(r[1:5], uint32(denom))
107+
binary.BigEndian.PutUint32(r[5:], uint32(elasticity))
93108
return r
94109
}
95110

consensus/misc/eip1559/eip1559_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func TestCalcBaseFeeOptimismHolocene(t *testing.T) {
195195
tests := []struct {
196196
parentGasUsed uint64
197197
expectedBaseFee int64
198-
denom, elasticity uint32
198+
denom, elasticity uint64
199199
}{
200200
{parentGasLimit / 2, parentBaseFee, 10, 2}, // target
201201
{10_000_000, 9_666_667, 10, 2}, // below

miner/worker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (miner *Miner) prepareWork(genParams *generateParams, witness bool) (*envir
262262
d = miner.chainConfig.BaseFeeChangeDenominator(header.Time)
263263
e = miner.chainConfig.ElasticityMultiplier()
264264
}
265-
header.Extra = eip1559.EncodeHoloceneExtraData(uint32(d), uint32(e))
265+
header.Extra = eip1559.EncodeHoloceneExtraData(d, e)
266266
} else if genParams.eip1559Params != nil {
267267
return nil, errors.New("got eip1559 params, expected none")
268268
}

0 commit comments

Comments
 (0)