Skip to content

Commit b103828

Browse files
karalabezfy0701
authored andcommitted
common: drop BigMin and BigMax, they pollute our dep graph (ethereum#30645)
Way back we've added `common.math.BigMin` and `common.math.BigMax`. These were kind of cute helpers, but unfortunate ones, because package all over out codebase added dependencies to this package just to avoid having to write out 3 lines of code. Because of this, we've also started having package name clashes with the stdlib `math`, which got solves even more badly by moving some helpers over ***from*** the stdlib into our custom lib (e.g. MaxUint64). The latter ones were nuked out in a previous PR and this PR nukes out BigMin and BigMax, inlining them at all call sites. As we're transitioning to uint256, if need be, we can add a min and max to that.
1 parent 29f4bed commit b103828

File tree

10 files changed

+64
-77
lines changed

10 files changed

+64
-77
lines changed

common/math/big.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,6 @@ func BigPow(a, b int64) *big.Int {
143143
return r.Exp(r, big.NewInt(b), nil)
144144
}
145145

146-
// BigMax returns the larger of x or y.
147-
func BigMax(x, y *big.Int) *big.Int {
148-
if x.Cmp(y) < 0 {
149-
return y
150-
}
151-
return x
152-
}
153-
154-
// BigMin returns the smaller of x or y.
155-
func BigMin(x, y *big.Int) *big.Int {
156-
if x.Cmp(y) > 0 {
157-
return y
158-
}
159-
return x
160-
}
161-
162146
// PaddedBigBytes encodes a big integer as a big-endian byte slice. The length
163147
// of the slice is at least n bytes.
164148
func PaddedBigBytes(bigint *big.Int, n int) []byte {

common/math/big_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,36 +68,6 @@ func TestMustParseBig256(t *testing.T) {
6868
MustParseBig256("ggg")
6969
}
7070

71-
func TestBigMax(t *testing.T) {
72-
a := big.NewInt(10)
73-
b := big.NewInt(5)
74-
75-
max1 := BigMax(a, b)
76-
if max1 != a {
77-
t.Errorf("Expected %d got %d", a, max1)
78-
}
79-
80-
max2 := BigMax(b, a)
81-
if max2 != a {
82-
t.Errorf("Expected %d got %d", a, max2)
83-
}
84-
}
85-
86-
func TestBigMin(t *testing.T) {
87-
a := big.NewInt(10)
88-
b := big.NewInt(5)
89-
90-
min1 := BigMin(a, b)
91-
if min1 != b {
92-
t.Errorf("Expected %d got %d", b, min1)
93-
}
94-
95-
min2 := BigMin(b, a)
96-
if min2 != b {
97-
t.Errorf("Expected %d got %d", b, min2)
98-
}
99-
}
100-
10171
func TestPaddedBigBytes(t *testing.T) {
10272
tests := []struct {
10373
num *big.Int

consensus/ethash/consensus.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
mapset "github.com/deckarep/golang-set/v2"
2626
"github.com/ethereum/go-ethereum/common"
27-
"github.com/ethereum/go-ethereum/common/math"
2827
"github.com/ethereum/go-ethereum/consensus"
2928
"github.com/ethereum/go-ethereum/consensus/misc"
3029
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
@@ -480,7 +479,9 @@ func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int {
480479
expDiff := periodCount.Sub(periodCount, big2)
481480
expDiff.Exp(big2, expDiff, nil)
482481
diff.Add(diff, expDiff)
483-
diff = math.BigMax(diff, params.MinimumDifficulty)
482+
if diff.Cmp(params.MinimumDifficulty) < 0 {
483+
diff = params.MinimumDifficulty
484+
}
484485
}
485486
return diff
486487
}

consensus/misc/eip1559/eip1559.go

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

2424
"github.com/ethereum/go-ethereum/common"
25-
"github.com/ethereum/go-ethereum/common/math"
2625
"github.com/ethereum/go-ethereum/consensus/misc"
2726
"github.com/ethereum/go-ethereum/core/types"
2827
"github.com/ethereum/go-ethereum/params"
@@ -78,18 +77,22 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
7877
num.Mul(num, parent.BaseFee)
7978
num.Div(num, denom.SetUint64(parentGasTarget))
8079
num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
81-
baseFeeDelta := math.BigMax(num, common.Big1)
82-
83-
return num.Add(parent.BaseFee, baseFeeDelta)
80+
if num.Cmp(common.Big1) < 0 {
81+
return num.Add(parent.BaseFee, common.Big1)
82+
}
83+
return num.Add(parent.BaseFee, num)
8484
} else {
8585
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
8686
// max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
8787
num.SetUint64(parentGasTarget - parent.GasUsed)
8888
num.Mul(num, parent.BaseFee)
8989
num.Div(num, denom.SetUint64(parentGasTarget))
9090
num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
91-
baseFee := num.Sub(parent.BaseFee, num)
9291

93-
return math.BigMax(baseFee, common.Big0)
92+
baseFee := num.Sub(parent.BaseFee, num)
93+
if baseFee.Cmp(common.Big0) < 0 {
94+
baseFee = common.Big0
95+
}
96+
return baseFee
9497
}
9598
}

core/state_transition.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"math/big"
2323

2424
"github.com/ethereum/go-ethereum/common"
25-
cmath "github.com/ethereum/go-ethereum/common/math"
2625
"github.com/ethereum/go-ethereum/core/tracing"
2726
"github.com/ethereum/go-ethereum/core/types"
2827
"github.com/ethereum/go-ethereum/core/vm"
@@ -170,7 +169,10 @@ func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.In
170169
}
171170
// If baseFee provided, set gasPrice to effectiveGasPrice.
172171
if baseFee != nil {
173-
msg.GasPrice = cmath.BigMin(msg.GasPrice.Add(msg.GasTipCap, baseFee), msg.GasFeeCap)
172+
msg.GasPrice = msg.GasPrice.Add(msg.GasTipCap, baseFee)
173+
if msg.GasPrice.Cmp(msg.GasFeeCap) > 0 {
174+
msg.GasPrice = msg.GasFeeCap
175+
}
174176
}
175177
var err error
176178
msg.From, err = types.Sender(s, tx)
@@ -461,7 +463,10 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
461463
}
462464
effectiveTip := msg.GasPrice
463465
if rules.IsLondon {
464-
effectiveTip = cmath.BigMin(msg.GasTipCap, new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee))
466+
effectiveTip = new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee)
467+
if effectiveTip.Cmp(msg.GasTipCap) > 0 {
468+
effectiveTip = msg.GasTipCap
469+
}
465470
}
466471
effectiveTipU256, _ := uint256.FromBig(effectiveTip)
467472

core/types/transaction.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"time"
2727

2828
"github.com/ethereum/go-ethereum/common"
29-
"github.com/ethereum/go-ethereum/common/math"
3029
"github.com/ethereum/go-ethereum/crypto"
3130
"github.com/ethereum/go-ethereum/rlp"
3231
)
@@ -355,10 +354,16 @@ func (tx *Transaction) EffectiveGasTip(baseFee *big.Int) (*big.Int, error) {
355354
}
356355
var err error
357356
gasFeeCap := tx.GasFeeCap()
358-
if gasFeeCap.Cmp(baseFee) == -1 {
357+
if gasFeeCap.Cmp(baseFee) < 0 {
359358
err = ErrGasFeeCapTooLow
360359
}
361-
return math.BigMin(tx.GasTipCap(), gasFeeCap.Sub(gasFeeCap, baseFee)), err
360+
gasFeeCap = gasFeeCap.Sub(gasFeeCap, baseFee)
361+
362+
gasTipCap := tx.GasTipCap()
363+
if gasTipCap.Cmp(gasFeeCap) < 0 {
364+
return gasTipCap, err
365+
}
366+
return gasFeeCap, err
362367
}
363368

364369
// EffectiveGasTipValue is identical to EffectiveGasTip, but does not return an

core/vm/contracts.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ import (
2222
"errors"
2323
"fmt"
2424
"maps"
25-
gomath "math"
25+
"math"
2626
"math/big"
2727

2828
"github.com/consensys/gnark-crypto/ecc"
2929
bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381"
3030
"github.com/consensys/gnark-crypto/ecc/bls12-381/fp"
3131
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
3232
"github.com/ethereum/go-ethereum/common"
33-
"github.com/ethereum/go-ethereum/common/math"
3433
"github.com/ethereum/go-ethereum/core/tracing"
3534
"github.com/ethereum/go-ethereum/crypto"
3635
"github.com/ethereum/go-ethereum/crypto/blake2b"
@@ -399,7 +398,12 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
399398
}
400399
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
401400
// Calculate the gas cost of the operation
402-
gas := new(big.Int).Set(math.BigMax(modLen, baseLen))
401+
gas := new(big.Int)
402+
if modLen.Cmp(baseLen) < 0 {
403+
gas.Set(baseLen)
404+
} else {
405+
gas.Set(modLen)
406+
}
403407
if c.eip2565 {
404408
// EIP-2565 has three changes
405409
// 1. Different multComplexity (inlined here)
@@ -413,11 +417,13 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
413417
gas.Rsh(gas, 3)
414418
gas.Mul(gas, gas)
415419

416-
gas.Mul(gas, math.BigMax(adjExpLen, big1))
420+
if adjExpLen.Cmp(big1) > 0 {
421+
gas.Mul(gas, adjExpLen)
422+
}
417423
// 2. Different divisor (`GQUADDIVISOR`) (3)
418424
gas.Div(gas, big3)
419425
if gas.BitLen() > 64 {
420-
return gomath.MaxUint64
426+
return math.MaxUint64
421427
}
422428
// 3. Minimum price of 200 gas
423429
if gas.Uint64() < 200 {
@@ -426,11 +432,13 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
426432
return gas.Uint64()
427433
}
428434
gas = modexpMultComplexity(gas)
429-
gas.Mul(gas, math.BigMax(adjExpLen, big1))
435+
if adjExpLen.Cmp(big1) > 0 {
436+
gas.Mul(gas, adjExpLen)
437+
}
430438
gas.Div(gas, big20)
431439

432440
if gas.BitLen() > 64 {
433-
return gomath.MaxUint64
441+
return math.MaxUint64
434442
}
435443
return gas.Uint64()
436444
}

graphql/graphql.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/ethereum/go-ethereum"
3131
"github.com/ethereum/go-ethereum/common"
3232
"github.com/ethereum/go-ethereum/common/hexutil"
33-
"github.com/ethereum/go-ethereum/common/math"
3433
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
3534
"github.com/ethereum/go-ethereum/core/state"
3635
"github.com/ethereum/go-ethereum/core/types"
@@ -277,7 +276,11 @@ func (t *Transaction) GasPrice(ctx context.Context) hexutil.Big {
277276
if block != nil {
278277
if baseFee, _ := block.BaseFeePerGas(ctx); baseFee != nil {
279278
// price = min(gasTipCap + baseFee, gasFeeCap)
280-
return (hexutil.Big)(*math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee.ToInt()), tx.GasFeeCap()))
279+
gasFeeCap, effectivePrice := tx.GasFeeCap(), new(big.Int).Add(tx.GasTipCap(), baseFee.ToInt())
280+
if effectivePrice.Cmp(gasFeeCap) < 0 {
281+
return (hexutil.Big)(*effectivePrice)
282+
}
283+
return (hexutil.Big)(*gasFeeCap)
281284
}
282285
}
283286
return hexutil.Big(*tx.GasPrice())
@@ -302,7 +305,11 @@ func (t *Transaction) EffectiveGasPrice(ctx context.Context) (*hexutil.Big, erro
302305
if header.BaseFee == nil {
303306
return (*hexutil.Big)(tx.GasPrice()), nil
304307
}
305-
return (*hexutil.Big)(math.BigMin(new(big.Int).Add(tx.GasTipCap(), header.BaseFee), tx.GasFeeCap())), nil
308+
gasFeeCap, effectivePrice := tx.GasFeeCap(), new(big.Int).Add(tx.GasTipCap(), header.BaseFee)
309+
if effectivePrice.Cmp(gasFeeCap) < 0 {
310+
return (*hexutil.Big)(effectivePrice), nil
311+
}
312+
return (*hexutil.Big)(gasFeeCap), nil
306313
}
307314

308315
func (t *Transaction) MaxFeePerGas(ctx context.Context) *hexutil.Big {

internal/ethapi/transaction_args.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ import (
2222
"crypto/sha256"
2323
"errors"
2424
"fmt"
25-
gomath "math"
25+
"math"
2626
"math/big"
2727

2828
"github.com/ethereum/go-ethereum/common"
2929
"github.com/ethereum/go-ethereum/common/hexutil"
30-
"github.com/ethereum/go-ethereum/common/math"
3130
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
3231
"github.com/ethereum/go-ethereum/core"
3332
"github.com/ethereum/go-ethereum/core/types"
@@ -141,7 +140,7 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, skipGas
141140
if skipGasEstimation { // Skip gas usage estimation if a precise gas limit is not critical, e.g., in non-transaction calls.
142141
gas := hexutil.Uint64(b.RPCGasCap())
143142
if gas == 0 {
144-
gas = hexutil.Uint64(gomath.MaxUint64 / 2)
143+
gas = hexutil.Uint64(math.MaxUint64 / 2)
145144
}
146145
args.Gas = &gas
147146
} else { // Estimate the gas usage otherwise.
@@ -379,7 +378,7 @@ func (args *TransactionArgs) CallDefaults(globalGasCap uint64, baseFee *big.Int,
379378
if args.Gas == nil {
380379
gas := globalGasCap
381380
if gas == 0 {
382-
gas = uint64(gomath.MaxUint64 / 2)
381+
gas = uint64(math.MaxUint64 / 2)
383382
}
384383
args.Gas = (*hexutil.Uint64)(&gas)
385384
} else {
@@ -441,7 +440,10 @@ func (args *TransactionArgs) ToMessage(baseFee *big.Int, skipNonceCheck, skipEoA
441440
// Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
442441
gasPrice = new(big.Int)
443442
if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 {
444-
gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap)
443+
gasPrice = gasPrice.Add(gasTipCap, baseFee)
444+
if gasPrice.Cmp(gasFeeCap) > 0 {
445+
gasPrice = gasFeeCap
446+
}
445447
}
446448
}
447449
}

tests/state_test_util.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,10 @@ func (tx *stTransaction) toMessage(ps stPostState, baseFee *big.Int) (*core.Mess
411411
if tx.MaxPriorityFeePerGas == nil {
412412
tx.MaxPriorityFeePerGas = tx.MaxFeePerGas
413413
}
414-
gasPrice = math.BigMin(new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee),
415-
tx.MaxFeePerGas)
414+
gasPrice = new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee)
415+
if gasPrice.Cmp(tx.MaxFeePerGas) > 0 {
416+
gasPrice = tx.MaxFeePerGas
417+
}
416418
}
417419
if gasPrice == nil {
418420
return nil, errors.New("no gas price provided")

0 commit comments

Comments
 (0)