From 4fe9a2a63ced21509b182e466862079255b45597 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Thu, 19 Dec 2024 15:38:22 +0800 Subject: [PATCH] common: drop BigMin and BigMax, they pollute our dep graph (#30645) --- accounts/abi/bind/backends/simulated.go | 5 ++++- common/math/big.go | 16 ------------- common/math/big_test.go | 30 ------------------------- consensus/ethash/consensus.go | 4 +++- core/state_transition.go | 6 +++-- core/types/transaction.go | 16 +++++++++---- core/vm/contracts.go | 22 ++++++++++++------ internal/ethapi/api.go | 13 ++++++----- internal/ethapi/transaction_args.go | 12 +++++----- tests/state_test_util.go | 6 +++-- 10 files changed, 57 insertions(+), 73 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 3f202062049c..08a104b311e4 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -449,7 +449,10 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call XDPoSChain.Cal // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes call.GasPrice = new(big.Int) if call.GasFeeCap.BitLen() > 0 || call.GasTipCap.BitLen() > 0 { - call.GasPrice = math.BigMin(new(big.Int).Add(call.GasTipCap, head.BaseFee), call.GasFeeCap) + call.GasPrice = new(big.Int).Add(call.GasTipCap, head.BaseFee) + if call.GasPrice.Cmp(call.GasFeeCap) > 0 { + call.GasPrice.Set(call.GasFeeCap) + } } } } diff --git a/common/math/big.go b/common/math/big.go index 5f59195e3d7e..78e3c8255d97 100644 --- a/common/math/big.go +++ b/common/math/big.go @@ -108,22 +108,6 @@ func BigPow(a, b int64) *big.Int { return r.Exp(r, big.NewInt(b), nil) } -// BigMax returns the larger of x or y. -func BigMax(x, y *big.Int) *big.Int { - if x.Cmp(y) < 0 { - return y - } - return x -} - -// BigMin returns the smaller of x or y. -func BigMin(x, y *big.Int) *big.Int { - if x.Cmp(y) > 0 { - return y - } - return x -} - // PaddedBigBytes encodes a big integer as a big-endian byte slice. The length // of the slice is at least n bytes. func PaddedBigBytes(bigint *big.Int, n int) []byte { diff --git a/common/math/big_test.go b/common/math/big_test.go index 92a322e29d66..6f701b621a7a 100644 --- a/common/math/big_test.go +++ b/common/math/big_test.go @@ -68,36 +68,6 @@ func TestMustParseBig256(t *testing.T) { MustParseBig256("ggg") } -func TestBigMax(t *testing.T) { - a := big.NewInt(10) - b := big.NewInt(5) - - max1 := BigMax(a, b) - if max1 != a { - t.Errorf("Expected %d got %d", a, max1) - } - - max2 := BigMax(b, a) - if max2 != a { - t.Errorf("Expected %d got %d", a, max2) - } -} - -func TestBigMin(t *testing.T) { - a := big.NewInt(10) - b := big.NewInt(5) - - min1 := BigMin(a, b) - if min1 != b { - t.Errorf("Expected %d got %d", b, min1) - } - - min2 := BigMin(b, a) - if min2 != b { - t.Errorf("Expected %d got %d", b, min2) - } -} - func TestPaddedBigBytes(t *testing.T) { tests := []struct { num *big.Int diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 92806693aeb4..e6ecc9f7ab64 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -458,7 +458,9 @@ func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int { expDiff := periodCount.Sub(periodCount, big2) expDiff.Exp(big2, expDiff, nil) diff.Add(diff, expDiff) - diff = math.BigMax(diff, params.MinimumDifficulty) + if diff.Cmp(params.MinimumDifficulty) < 0 { + diff = params.MinimumDifficulty + } } return diff } diff --git a/core/state_transition.go b/core/state_transition.go index 42e044a8de73..b34ac07fef8e 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -23,7 +23,6 @@ import ( "math/big" "github.com/XinFinOrg/XDPoSChain/common" - cmath "github.com/XinFinOrg/XDPoSChain/common/math" "github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/core/vm" "github.com/XinFinOrg/XDPoSChain/crypto" @@ -397,7 +396,10 @@ func (st *StateTransition) TransitionDb(owner common.Address) (*ExecutionResult, } else { effectiveTip := st.gasPrice if st.evm.ChainConfig().IsEIP1559(st.evm.Context.BlockNumber) { - effectiveTip = cmath.BigMin(st.gasTipCap, new(big.Int).Sub(st.gasFeeCap, st.evm.Context.BaseFee)) + effectiveTip = new(big.Int).Sub(st.gasFeeCap, st.evm.Context.BaseFee) + if effectiveTip.Cmp(st.gasTipCap) > 0 { + effectiveTip = st.gasTipCap + } } st.state.AddBalance(st.evm.Context.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), effectiveTip)) } diff --git a/core/types/transaction.go b/core/types/transaction.go index dca1ef50a5bf..3054534e558d 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -28,7 +28,6 @@ import ( "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common/hexutil" - "github.com/XinFinOrg/XDPoSChain/common/math" "github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/rlp" ) @@ -364,10 +363,16 @@ func (tx *Transaction) EffectiveGasTip(baseFee *big.Int) (*big.Int, error) { } var err error gasFeeCap := tx.GasFeeCap() - if gasFeeCap.Cmp(baseFee) == -1 { + if gasFeeCap.Cmp(baseFee) < 0 { err = ErrGasFeeCapTooLow } - return math.BigMin(tx.GasTipCap(), gasFeeCap.Sub(gasFeeCap, baseFee)), err + gasFeeCap = gasFeeCap.Sub(gasFeeCap, baseFee) + + gasTipCap := tx.GasTipCap() + if gasTipCap.Cmp(gasFeeCap) < 0 { + return gasTipCap, err + } + return gasFeeCap, err } // EffectiveGasTipValue is identical to EffectiveGasTip, but does not return an @@ -453,7 +458,10 @@ func (tx *Transaction) AsMessage(s Signer, balanceFee, blockNumber, baseFee *big } } else if baseFee != nil { // If baseFee provided, set gasPrice to effectiveGasPrice. - msg.gasPrice = math.BigMin(msg.gasPrice.Add(msg.gasTipCap, baseFee), msg.gasFeeCap) + msg.gasPrice = msg.gasPrice.Add(msg.gasTipCap, baseFee) + if msg.gasPrice.Cmp(msg.gasFeeCap) > 0 { + msg.gasPrice.Set(msg.gasFeeCap) + } } var err error diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 9f17ba33109d..98e1fdc43666 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -20,11 +20,10 @@ import ( "crypto/sha256" "encoding/binary" "errors" - gomath "math" + "math" "math/big" "github.com/XinFinOrg/XDPoSChain/common" - "github.com/XinFinOrg/XDPoSChain/common/math" "github.com/XinFinOrg/XDPoSChain/core/vm/privacy" "github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto/blake2b" @@ -347,7 +346,12 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 { } adjExpLen.Add(adjExpLen, big.NewInt(int64(msb))) // Calculate the gas cost of the operation - gas := new(big.Int).Set(math.BigMax(modLen, baseLen)) + gas := new(big.Int) + if modLen.Cmp(baseLen) < 0 { + gas.Set(baseLen) + } else { + gas.Set(modLen) + } if c.eip2565 { // EIP-2565 has three changes // 1. Different multComplexity (inlined here) @@ -361,11 +365,13 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 { gas = gas.Div(gas, big8) gas.Mul(gas, gas) - gas.Mul(gas, math.BigMax(adjExpLen, big1)) + if adjExpLen.Cmp(big1) > 0 { + gas.Mul(gas, adjExpLen) + } // 2. Different divisor (`GQUADDIVISOR`) (3) gas.Div(gas, big3) if gas.BitLen() > 64 { - return gomath.MaxUint64 + return math.MaxUint64 } // 3. Minimum price of 200 gas if gas.Uint64() < 200 { @@ -374,11 +380,13 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 { return gas.Uint64() } gas = modexpMultComplexity(gas) - gas.Mul(gas, math.BigMax(adjExpLen, big1)) + if adjExpLen.Cmp(big1) > 0 { + gas.Mul(gas, adjExpLen) + } gas.Div(gas, big20) if gas.BitLen() > 64 { - return gomath.MaxUint64 + return math.MaxUint64 } return gas.Uint64() } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 2af342738438..31040d77b83b 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -21,10 +21,10 @@ import ( "context" "errors" "fmt" + "math" "math/big" "strings" "time" - gomath "math" "github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate" "github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate" @@ -34,7 +34,6 @@ import ( "github.com/XinFinOrg/XDPoSChain/accounts/keystore" "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common/hexutil" - "github.com/XinFinOrg/XDPoSChain/common/math" "github.com/XinFinOrg/XDPoSChain/common/sort" "github.com/XinFinOrg/XDPoSChain/consensus" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS" @@ -416,7 +415,7 @@ func (s *PrivateAccountAPI) ImportRawKey(privkey string, password string) (commo // the given password for duration seconds. If duration is nil it will use a // default of 300 seconds. It returns an indication if the account was unlocked. func (s *PrivateAccountAPI) UnlockAccount(addr common.Address, password string, duration *uint64) (bool, error) { - const max = uint64(time.Duration(gomath.MaxInt64) / time.Second) + const max = uint64(time.Duration(math.MaxInt64) / time.Second) var d time.Duration if duration == nil { d = 300 * time.Second @@ -1390,7 +1389,7 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash }() // Execute the message. - gp := new(core.GasPool).AddGas(gomath.MaxUint64) + gp := new(core.GasPool).AddGas(math.MaxUint64) owner := common.Address{} result, err, vmErr := core.ApplyMessage(evm, msg, gp, owner) if err := vmError(); err != nil { @@ -1954,7 +1953,11 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber // if the transaction has been mined, compute the effective gas price if baseFee != nil && blockHash != (common.Hash{}) { // price = min(tip, gasFeeCap - baseFee) + baseFee - price := math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee), tx.GasFeeCap()) + price := new(big.Int).Add(tx.GasTipCap(), baseFee) + txGasFeeCap := tx.GasFeeCap() + if price.Cmp(txGasFeeCap) > 0 { + price = txGasFeeCap + } result.GasPrice = (*hexutil.Big)(price) } else { result.GasPrice = (*hexutil.Big)(tx.GasFeeCap()) diff --git a/internal/ethapi/transaction_args.go b/internal/ethapi/transaction_args.go index 0bdf9903e572..68807629a20d 100644 --- a/internal/ethapi/transaction_args.go +++ b/internal/ethapi/transaction_args.go @@ -21,12 +21,11 @@ import ( "context" "errors" "fmt" - gomath "math" + "math" "math/big" "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common/hexutil" - "github.com/XinFinOrg/XDPoSChain/common/math" "github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/rpc" @@ -100,7 +99,7 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, skipGas if skipGasEstimation { // Skip gas usage estimation if a precise gas limit is not critical, e.g., in non-transaction calls. gas := hexutil.Uint64(b.RPCGasCap()) if gas == 0 { - gas = hexutil.Uint64(gomath.MaxUint64 / 2) + gas = hexutil.Uint64(math.MaxUint64 / 2) } args.Gas = &gas } else { // Estimate the gas usage otherwise. @@ -246,7 +245,7 @@ func (args *TransactionArgs) ToMessage(b Backend, number *big.Int, globalGasCap gas = uint64(*args.Gas) } if gas == 0 { - gas = gomath.MaxUint64 / 2 + gas = math.MaxUint64 / 2 } if globalGasCap != 0 && globalGasCap < gas { 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 // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes gasPrice = new(big.Int) if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 { - gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap) + gasPrice = gasPrice.Add(gasTipCap, baseFee) + if gasPrice.Cmp(gasFeeCap) > 0 { + gasPrice = gasFeeCap + } } } } diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 1452ecd5d6a0..13d1a98fe558 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -275,8 +275,10 @@ func (tx *stTransaction) toMessage(ps stPostState, number *big.Int, baseFee *big if tx.MaxPriorityFeePerGas == nil { tx.MaxPriorityFeePerGas = tx.MaxFeePerGas } - gasPrice = math.BigMin(new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee), - tx.MaxFeePerGas) + gasPrice = new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee) + if gasPrice.Cmp(tx.MaxFeePerGas) > 0 { + gasPrice.Set(tx.MaxFeePerGas) + } } if gasPrice == nil { return nil, errors.New("no gas price provided")