Skip to content

Commit

Permalink
common: drop BigMin and BigMax, they pollute our dep graph (30645)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Dec 19, 2024
1 parent 5149332 commit 2ff92e6
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 73 deletions.
5 changes: 4 additions & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,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)
}
}
}
}
Expand Down
16 changes: 0 additions & 16 deletions common/math/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 0 additions & 30 deletions common/math/big_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -379,7 +378,10 @@ func (st *StateTransition) TransitionDb(owner common.Address) (ret []byte, usedG
} 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))
}
Expand Down
16 changes: 12 additions & 4 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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()
}
Expand Down
13 changes: 8 additions & 5 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{}
res, gas, failed, err, vmErr := core.ApplyMessage(evm, msg, gp, owner)
if err := vmError(); err != nil {
Expand Down Expand Up @@ -1933,7 +1932,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())
Expand Down
12 changes: 7 additions & 5 deletions internal/ethapi/transaction_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 2ff92e6

Please sign in to comment.