common: drop BigMin and BigMax, they pollute our dep graph (#30645)

This commit is contained in:
Daniel Liu 2024-12-19 15:38:22 +08:00
parent 156de2bf90
commit 6e33633d28
10 changed files with 57 additions and 73 deletions

View file

@ -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 // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
call.GasPrice = new(big.Int) call.GasPrice = new(big.Int)
if call.GasFeeCap.BitLen() > 0 || call.GasTipCap.BitLen() > 0 { 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)
}
} }
} }
} }

View file

@ -108,22 +108,6 @@ func BigPow(a, b int64) *big.Int {
return r.Exp(r, big.NewInt(b), nil) 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 // PaddedBigBytes encodes a big integer as a big-endian byte slice. The length
// of the slice is at least n bytes. // of the slice is at least n bytes.
func PaddedBigBytes(bigint *big.Int, n int) []byte { func PaddedBigBytes(bigint *big.Int, n int) []byte {

View file

@ -68,36 +68,6 @@ func TestMustParseBig256(t *testing.T) {
MustParseBig256("ggg") 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) { func TestPaddedBigBytes(t *testing.T) {
tests := []struct { tests := []struct {
num *big.Int num *big.Int

View file

@ -458,7 +458,9 @@ func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int {
expDiff := periodCount.Sub(periodCount, big2) expDiff := periodCount.Sub(periodCount, big2)
expDiff.Exp(big2, expDiff, nil) expDiff.Exp(big2, expDiff, nil)
diff.Add(diff, expDiff) diff.Add(diff, expDiff)
diff = math.BigMax(diff, params.MinimumDifficulty) if diff.Cmp(params.MinimumDifficulty) < 0 {
diff = params.MinimumDifficulty
}
} }
return diff return diff
} }

View file

@ -23,7 +23,6 @@ import (
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
cmath "github.com/XinFinOrg/XDPoSChain/common/math"
"github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/core/vm" "github.com/XinFinOrg/XDPoSChain/core/vm"
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
@ -397,7 +396,10 @@ func (st *StateTransition) TransitionDb(owner common.Address) (*ExecutionResult,
} else { } else {
effectiveTip := st.gasPrice effectiveTip := st.gasPrice
if st.evm.ChainConfig().IsEIP1559(st.evm.Context.BlockNumber) { 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)) st.state.AddBalance(st.evm.Context.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), effectiveTip))
} }

View file

@ -28,7 +28,6 @@ import (
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/hexutil" "github.com/XinFinOrg/XDPoSChain/common/hexutil"
"github.com/XinFinOrg/XDPoSChain/common/math"
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/rlp" "github.com/XinFinOrg/XDPoSChain/rlp"
) )
@ -364,10 +363,16 @@ func (tx *Transaction) EffectiveGasTip(baseFee *big.Int) (*big.Int, error) {
} }
var err error var err error
gasFeeCap := tx.GasFeeCap() gasFeeCap := tx.GasFeeCap()
if gasFeeCap.Cmp(baseFee) == -1 { if gasFeeCap.Cmp(baseFee) < 0 {
err = ErrGasFeeCapTooLow 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 // 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 { } else if baseFee != nil {
// If baseFee provided, set gasPrice to effectiveGasPrice. // 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 var err error

View file

@ -20,11 +20,10 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/binary" "encoding/binary"
"errors" "errors"
gomath "math" "math"
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/math"
"github.com/XinFinOrg/XDPoSChain/core/vm/privacy" "github.com/XinFinOrg/XDPoSChain/core/vm/privacy"
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/crypto/blake2b" "github.com/XinFinOrg/XDPoSChain/crypto/blake2b"
@ -347,7 +346,12 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
} }
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb))) adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
// Calculate the gas cost of the operation // 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 { if c.eip2565 {
// EIP-2565 has three changes // EIP-2565 has three changes
// 1. Different multComplexity (inlined here) // 1. Different multComplexity (inlined here)
@ -361,11 +365,13 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
gas = gas.Div(gas, big8) gas = gas.Div(gas, big8)
gas.Mul(gas, gas) 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) // 2. Different divisor (`GQUADDIVISOR`) (3)
gas.Div(gas, big3) gas.Div(gas, big3)
if gas.BitLen() > 64 { if gas.BitLen() > 64 {
return gomath.MaxUint64 return math.MaxUint64
} }
// 3. Minimum price of 200 gas // 3. Minimum price of 200 gas
if gas.Uint64() < 200 { if gas.Uint64() < 200 {
@ -374,11 +380,13 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
return gas.Uint64() return gas.Uint64()
} }
gas = modexpMultComplexity(gas) gas = modexpMultComplexity(gas)
gas.Mul(gas, math.BigMax(adjExpLen, big1)) if adjExpLen.Cmp(big1) > 0 {
gas.Mul(gas, adjExpLen)
}
gas.Div(gas, big20) gas.Div(gas, big20)
if gas.BitLen() > 64 { if gas.BitLen() > 64 {
return gomath.MaxUint64 return math.MaxUint64
} }
return gas.Uint64() return gas.Uint64()
} }

View file

@ -21,10 +21,10 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"math"
"math/big" "math/big"
"strings" "strings"
"time" "time"
gomath "math"
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate" "github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
"github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate" "github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate"
@ -34,7 +34,6 @@ import (
"github.com/XinFinOrg/XDPoSChain/accounts/keystore" "github.com/XinFinOrg/XDPoSChain/accounts/keystore"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/hexutil" "github.com/XinFinOrg/XDPoSChain/common/hexutil"
"github.com/XinFinOrg/XDPoSChain/common/math"
"github.com/XinFinOrg/XDPoSChain/common/sort" "github.com/XinFinOrg/XDPoSChain/common/sort"
"github.com/XinFinOrg/XDPoSChain/consensus" "github.com/XinFinOrg/XDPoSChain/consensus"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS" "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 // 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. // 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) { 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 var d time.Duration
if duration == nil { if duration == nil {
d = 300 * time.Second d = 300 * time.Second
@ -1390,7 +1389,7 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash
}() }()
// Execute the message. // Execute the message.
gp := new(core.GasPool).AddGas(gomath.MaxUint64) gp := new(core.GasPool).AddGas(math.MaxUint64)
owner := common.Address{} owner := common.Address{}
result, err := core.ApplyMessage(evm, msg, gp, owner) result, err := core.ApplyMessage(evm, msg, gp, owner)
if err := vmError(); err != nil { 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 the transaction has been mined, compute the effective gas price
if baseFee != nil && blockHash != (common.Hash{}) { if baseFee != nil && blockHash != (common.Hash{}) {
// price = min(tip, gasFeeCap - baseFee) + baseFee // 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) result.GasPrice = (*hexutil.Big)(price)
} else { } else {
result.GasPrice = (*hexutil.Big)(tx.GasFeeCap()) result.GasPrice = (*hexutil.Big)(tx.GasFeeCap())

View file

@ -21,12 +21,11 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
gomath "math" "math"
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/hexutil" "github.com/XinFinOrg/XDPoSChain/common/hexutil"
"github.com/XinFinOrg/XDPoSChain/common/math"
"github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/rpc" "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. 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()) gas := hexutil.Uint64(b.RPCGasCap())
if gas == 0 { if gas == 0 {
gas = hexutil.Uint64(gomath.MaxUint64 / 2) gas = hexutil.Uint64(math.MaxUint64 / 2)
} }
args.Gas = &gas args.Gas = &gas
} else { // Estimate the gas usage otherwise. } else { // Estimate the gas usage otherwise.
@ -246,7 +245,7 @@ func (args *TransactionArgs) ToMessage(b Backend, number *big.Int, globalGasCap
gas = uint64(*args.Gas) gas = uint64(*args.Gas)
} }
if gas == 0 { if gas == 0 {
gas = gomath.MaxUint64 / 2 gas = math.MaxUint64 / 2
} }
if globalGasCap != 0 && globalGasCap < gas { if globalGasCap != 0 && globalGasCap < gas {
log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap) 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 // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
gasPrice = new(big.Int) gasPrice = new(big.Int)
if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 { 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
}
} }
} }
} }

View file

@ -275,8 +275,10 @@ func (tx *stTransaction) toMessage(ps stPostState, number *big.Int, baseFee *big
if tx.MaxPriorityFeePerGas == nil { if tx.MaxPriorityFeePerGas == nil {
tx.MaxPriorityFeePerGas = tx.MaxFeePerGas tx.MaxPriorityFeePerGas = tx.MaxFeePerGas
} }
gasPrice = math.BigMin(new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee), gasPrice = new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee)
tx.MaxFeePerGas) if gasPrice.Cmp(tx.MaxFeePerGas) > 0 {
gasPrice.Set(tx.MaxFeePerGas)
}
} }
if gasPrice == nil { if gasPrice == nil {
return nil, errors.New("no gas price provided") return nil, errors.New("no gas price provided")