mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-23 15:14:32 +00:00
common: drop BigMin and BigMax, they pollute our dep graph (#30645)
This commit is contained in:
parent
156de2bf90
commit
6e33633d28
10 changed files with 57 additions and 73 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 := 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())
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Reference in a new issue