Skip to content

Commit b6f54b9

Browse files
committed
common: drop BigMin and BigMax, they pollute our dep graph (ethereum#30645)
1 parent 0d0a04d commit b6f54b9

File tree

10 files changed

+57
-73
lines changed

10 files changed

+57
-73
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,10 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call XDPoSChain.Cal
409409
// Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
410410
call.GasPrice = new(big.Int)
411411
if call.GasFeeCap.BitLen() > 0 || call.GasTipCap.BitLen() > 0 {
412-
call.GasPrice = math.BigMin(new(big.Int).Add(call.GasTipCap, head.BaseFee), call.GasFeeCap)
412+
call.GasPrice = new(big.Int).Add(call.GasTipCap, head.BaseFee)
413+
if call.GasPrice.Cmp(call.GasFeeCap) > 0 {
414+
call.GasPrice.Set(call.GasFeeCap)
415+
}
413416
}
414417
}
415418
}

common/math/big.go

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

111-
// BigMax returns the larger of x or y.
112-
func BigMax(x, y *big.Int) *big.Int {
113-
if x.Cmp(y) < 0 {
114-
return y
115-
}
116-
return x
117-
}
118-
119-
// BigMin returns the smaller of x or y.
120-
func BigMin(x, y *big.Int) *big.Int {
121-
if x.Cmp(y) > 0 {
122-
return y
123-
}
124-
return x
125-
}
126-
127111
// PaddedBigBytes encodes a big integer as a big-endian byte slice. The length
128112
// of the slice is at least n bytes.
129113
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,9 @@ func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int {
458458
expDiff := periodCount.Sub(periodCount, big2)
459459
expDiff.Exp(big2, expDiff, nil)
460460
diff.Add(diff, expDiff)
461-
diff = math.BigMax(diff, params.MinimumDifficulty)
461+
if diff.Cmp(params.MinimumDifficulty) < 0 {
462+
diff = params.MinimumDifficulty
463+
}
462464
}
463465
return diff
464466
}

core/state_transition.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"math/big"
2424

2525
"github.com/XinFinOrg/XDPoSChain/common"
26-
cmath "github.com/XinFinOrg/XDPoSChain/common/math"
2726
"github.com/XinFinOrg/XDPoSChain/core/types"
2827
"github.com/XinFinOrg/XDPoSChain/core/vm"
2928
"github.com/XinFinOrg/XDPoSChain/crypto"
@@ -379,7 +378,10 @@ func (st *StateTransition) TransitionDb(owner common.Address) (ret []byte, usedG
379378
} else {
380379
effectiveTip := st.gasPrice
381380
if st.evm.ChainConfig().IsEIP1559(st.evm.Context.BlockNumber) {
382-
effectiveTip = cmath.BigMin(st.gasTipCap, new(big.Int).Sub(st.gasFeeCap, st.evm.Context.BaseFee))
381+
effectiveTip = new(big.Int).Sub(st.gasFeeCap, st.evm.Context.BaseFee)
382+
if effectiveTip.Cmp(st.gasTipCap) > 0 {
383+
effectiveTip = st.gasTipCap
384+
}
383385
}
384386
st.state.AddBalance(st.evm.Context.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), effectiveTip))
385387
}

core/types/transaction.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828

2929
"github.com/XinFinOrg/XDPoSChain/common"
3030
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
31-
"github.com/XinFinOrg/XDPoSChain/common/math"
3231
"github.com/XinFinOrg/XDPoSChain/crypto"
3332
"github.com/XinFinOrg/XDPoSChain/rlp"
3433
)
@@ -364,10 +363,16 @@ func (tx *Transaction) EffectiveGasTip(baseFee *big.Int) (*big.Int, error) {
364363
}
365364
var err error
366365
gasFeeCap := tx.GasFeeCap()
367-
if gasFeeCap.Cmp(baseFee) == -1 {
366+
if gasFeeCap.Cmp(baseFee) < 0 {
368367
err = ErrGasFeeCapTooLow
369368
}
370-
return math.BigMin(tx.GasTipCap(), gasFeeCap.Sub(gasFeeCap, baseFee)), err
369+
gasFeeCap = gasFeeCap.Sub(gasFeeCap, baseFee)
370+
371+
gasTipCap := tx.GasTipCap()
372+
if gasTipCap.Cmp(gasFeeCap) < 0 {
373+
return gasTipCap, err
374+
}
375+
return gasFeeCap, err
371376
}
372377

373378
// EffectiveGasTipValue is identical to EffectiveGasTip, but does not return an
@@ -453,7 +458,10 @@ func (tx *Transaction) AsMessage(s Signer, balanceFee, blockNumber, baseFee *big
453458
}
454459
} else if baseFee != nil {
455460
// If baseFee provided, set gasPrice to effectiveGasPrice.
456-
msg.gasPrice = math.BigMin(msg.gasPrice.Add(msg.gasTipCap, baseFee), msg.gasFeeCap)
461+
msg.gasPrice = msg.gasPrice.Add(msg.gasTipCap, baseFee)
462+
if msg.gasPrice.Cmp(msg.gasFeeCap) > 0 {
463+
msg.gasPrice.Set(msg.gasFeeCap)
464+
}
457465
}
458466

459467
var err error

core/vm/contracts.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ import (
2020
"crypto/sha256"
2121
"encoding/binary"
2222
"errors"
23-
gomath "math"
23+
"math"
2424
"math/big"
2525

2626
"github.com/XinFinOrg/XDPoSChain/common"
27-
"github.com/XinFinOrg/XDPoSChain/common/math"
2827
"github.com/XinFinOrg/XDPoSChain/core/vm/privacy"
2928
"github.com/XinFinOrg/XDPoSChain/crypto"
3029
"github.com/XinFinOrg/XDPoSChain/crypto/blake2b"
@@ -347,7 +346,12 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
347346
}
348347
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
349348
// Calculate the gas cost of the operation
350-
gas := new(big.Int).Set(math.BigMax(modLen, baseLen))
349+
gas := new(big.Int)
350+
if modLen.Cmp(baseLen) < 0 {
351+
gas.Set(baseLen)
352+
} else {
353+
gas.Set(modLen)
354+
}
351355
if c.eip2565 {
352356
// EIP-2565 has three changes
353357
// 1. Different multComplexity (inlined here)
@@ -361,11 +365,13 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
361365
gas = gas.Div(gas, big8)
362366
gas.Mul(gas, gas)
363367

364-
gas.Mul(gas, math.BigMax(adjExpLen, big1))
368+
if adjExpLen.Cmp(big1) > 0 {
369+
gas.Mul(gas, adjExpLen)
370+
}
365371
// 2. Different divisor (`GQUADDIVISOR`) (3)
366372
gas.Div(gas, big3)
367373
if gas.BitLen() > 64 {
368-
return gomath.MaxUint64
374+
return math.MaxUint64
369375
}
370376
// 3. Minimum price of 200 gas
371377
if gas.Uint64() < 200 {
@@ -374,11 +380,13 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
374380
return gas.Uint64()
375381
}
376382
gas = modexpMultComplexity(gas)
377-
gas.Mul(gas, math.BigMax(adjExpLen, big1))
383+
if adjExpLen.Cmp(big1) > 0 {
384+
gas.Mul(gas, adjExpLen)
385+
}
378386
gas.Div(gas, big20)
379387

380388
if gas.BitLen() > 64 {
381-
return gomath.MaxUint64
389+
return math.MaxUint64
382390
}
383391
return gas.Uint64()
384392
}

internal/ethapi/api.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24+
"math"
2425
"math/big"
2526
"strings"
2627
"time"
27-
gomath "math"
2828

2929
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
3030
"github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate"
@@ -34,7 +34,6 @@ import (
3434
"github.com/XinFinOrg/XDPoSChain/accounts/keystore"
3535
"github.com/XinFinOrg/XDPoSChain/common"
3636
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
37-
"github.com/XinFinOrg/XDPoSChain/common/math"
3837
"github.com/XinFinOrg/XDPoSChain/common/sort"
3938
"github.com/XinFinOrg/XDPoSChain/consensus"
4039
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
@@ -416,7 +415,7 @@ func (s *PrivateAccountAPI) ImportRawKey(privkey string, password string) (commo
416415
// the given password for duration seconds. If duration is nil it will use a
417416
// default of 300 seconds. It returns an indication if the account was unlocked.
418417
func (s *PrivateAccountAPI) UnlockAccount(addr common.Address, password string, duration *uint64) (bool, error) {
419-
const max = uint64(time.Duration(gomath.MaxInt64) / time.Second)
418+
const max = uint64(time.Duration(math.MaxInt64) / time.Second)
420419
var d time.Duration
421420
if duration == nil {
422421
d = 300 * time.Second
@@ -1390,7 +1389,7 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash
13901389
}()
13911390

13921391
// Execute the message.
1393-
gp := new(core.GasPool).AddGas(gomath.MaxUint64)
1392+
gp := new(core.GasPool).AddGas(math.MaxUint64)
13941393
owner := common.Address{}
13951394
res, gas, failed, err, vmErr := core.ApplyMessage(evm, msg, gp, owner)
13961395
if err := vmError(); err != nil {
@@ -1933,7 +1932,11 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
19331932
// if the transaction has been mined, compute the effective gas price
19341933
if baseFee != nil && blockHash != (common.Hash{}) {
19351934
// price = min(tip, gasFeeCap - baseFee) + baseFee
1936-
price := math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee), tx.GasFeeCap())
1935+
price := new(big.Int).Add(tx.GasTipCap(), baseFee)
1936+
txGasFeeCap := tx.GasFeeCap()
1937+
if price.Cmp(txGasFeeCap) > 0 {
1938+
price = txGasFeeCap
1939+
}
19371940
result.GasPrice = (*hexutil.Big)(price)
19381941
} else {
19391942
result.GasPrice = (*hexutil.Big)(tx.GasFeeCap())

internal/ethapi/transaction_args.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24-
gomath "math"
24+
"math"
2525
"math/big"
2626

2727
"github.com/XinFinOrg/XDPoSChain/common"
2828
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
29-
"github.com/XinFinOrg/XDPoSChain/common/math"
3029
"github.com/XinFinOrg/XDPoSChain/core/types"
3130
"github.com/XinFinOrg/XDPoSChain/log"
3231
"github.com/XinFinOrg/XDPoSChain/rpc"
@@ -100,7 +99,7 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, skipGas
10099
if skipGasEstimation { // Skip gas usage estimation if a precise gas limit is not critical, e.g., in non-transaction calls.
101100
gas := hexutil.Uint64(b.RPCGasCap())
102101
if gas == 0 {
103-
gas = hexutil.Uint64(gomath.MaxUint64 / 2)
102+
gas = hexutil.Uint64(math.MaxUint64 / 2)
104103
}
105104
args.Gas = &gas
106105
} else { // Estimate the gas usage otherwise.
@@ -246,7 +245,7 @@ func (args *TransactionArgs) ToMessage(b Backend, number *big.Int, globalGasCap
246245
gas = uint64(*args.Gas)
247246
}
248247
if gas == 0 {
249-
gas = gomath.MaxUint64 / 2
248+
gas = math.MaxUint64 / 2
250249
}
251250
if globalGasCap != 0 && globalGasCap < gas {
252251
log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap)
@@ -287,7 +286,10 @@ func (args *TransactionArgs) ToMessage(b Backend, number *big.Int, globalGasCap
287286
// Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
288287
gasPrice = new(big.Int)
289288
if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 {
290-
gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap)
289+
gasPrice = gasPrice.Add(gasTipCap, baseFee)
290+
if gasPrice.Cmp(gasFeeCap) > 0 {
291+
gasPrice = gasFeeCap
292+
}
291293
}
292294
}
293295
}

tests/state_test_util.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,10 @@ func (tx *stTransaction) toMessage(ps stPostState, number *big.Int, baseFee *big
275275
if tx.MaxPriorityFeePerGas == nil {
276276
tx.MaxPriorityFeePerGas = tx.MaxFeePerGas
277277
}
278-
gasPrice = math.BigMin(new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee),
279-
tx.MaxFeePerGas)
278+
gasPrice = new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee)
279+
if gasPrice.Cmp(tx.MaxFeePerGas) > 0 {
280+
gasPrice.Set(tx.MaxFeePerGas)
281+
}
280282
}
281283
if gasPrice == nil {
282284
return nil, errors.New("no gas price provided")

0 commit comments

Comments
 (0)