From af03289998673b9d7b11046187dd429a81170ac3 Mon Sep 17 00:00:00 2001 From: weiihann Date: Fri, 24 Nov 2023 13:19:00 +0800 Subject: [PATCH] legacypool: use uint256 instead of big --- common/big.go | 10 +++++++++ core/txpool/legacypool/legacypool.go | 19 ++++++++-------- core/txpool/legacypool/legacypool_test.go | 5 +---- core/txpool/legacypool/list.go | 27 ++++++++++++++--------- core/txpool/legacypool/list_test.go | 4 ++-- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/common/big.go b/common/big.go index cbb562a28ef8..eed30048bef0 100644 --- a/common/big.go +++ b/common/big.go @@ -34,3 +34,13 @@ var ( U2560 = uint256.NewInt(0) ) + +var ( + Uint1 = uint256.NewInt(1) + Uint2 = uint256.NewInt(2) + Uint3 = uint256.NewInt(3) + Uint0 = uint256.NewInt(0) + Uint32 = uint256.NewInt(32) + Uint256 = uint256.NewInt(256) + Uint257 = uint256.NewInt(257) +) diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 624dafc60d03..949515cac858 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -37,6 +37,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/params" + "github.com/holiman/uint256" ) const ( @@ -202,7 +203,7 @@ type LegacyPool struct { config Config chainconfig *params.ChainConfig chain BlockChain - gasTip atomic.Pointer[big.Int] + gasTip atomic.Pointer[uint256.Int] txFeed event.Feed signer types.Signer mu sync.RWMutex @@ -292,7 +293,7 @@ func (pool *LegacyPool) Init(gasTip *big.Int, head *types.Header, reserve txpool pool.reserve = reserve // Set the basic pool parameters - pool.gasTip.Store(gasTip) + pool.gasTip.Store(uint256.MustFromBig(gasTip)) // Initialize the state with head block, or fallback to empty one in // case the head state is not available(might occur when node is not @@ -434,10 +435,10 @@ func (pool *LegacyPool) SetGasTip(tip *big.Int) { defer pool.mu.Unlock() old := pool.gasTip.Load() - pool.gasTip.Store(new(big.Int).Set(tip)) + pool.gasTip.Store(uint256.MustFromBig(tip)) // If the min miner fee increased, remove transactions below the new threshold - if tip.Cmp(old) > 0 { + if uint256.MustFromBig(tip).Cmp(old) > 0 { // pool.priced is sorted by GasFeeCap, so we have to iterate through pool.all instead drop := pool.all.RemotesBelowTip(tip) for _, tx := range drop { @@ -532,7 +533,7 @@ func (pool *LegacyPool) Pending(enforceTips bool) map[common.Address][]*txpool.L // If the miner requests tip enforcement, cap the lists now if enforceTips && !pool.locals.contains(addr) { for i, tx := range txs { - if tx.EffectiveGasTipIntCmp(pool.gasTip.Load(), pool.priced.urgent.baseFee) < 0 { + if tx.EffectiveGasTipIntCmp(pool.gasTip.Load().ToBig(), pool.priced.urgent.baseFee) < 0 { txs = txs[:i] break } @@ -594,7 +595,7 @@ func (pool *LegacyPool) validateTxBasics(tx *types.Transaction, local bool) erro 1< gasLimit || tx.Cost().Cmp(costLimit) > 0 + return tx.Gas() > gasLimit || tx.Cost().Cmp(costLimit.ToBig()) > 0 }) if len(removed) == 0 { @@ -456,7 +458,10 @@ func (l *list) LastElement() *types.Transaction { // total cost of all transactions. func (l *list) subTotalCost(txs []*types.Transaction) { for _, tx := range txs { - l.totalcost.Sub(l.totalcost, tx.Cost()) + _, underflow := l.totalcost.SubOverflow(l.totalcost, uint256.MustFromBig(tx.Cost())) + if underflow { + panic("totalcost underflow") + } } } diff --git a/core/txpool/legacypool/list_test.go b/core/txpool/legacypool/list_test.go index b5cd34b23b62..b96bf9b45a71 100644 --- a/core/txpool/legacypool/list_test.go +++ b/core/txpool/legacypool/list_test.go @@ -17,12 +17,12 @@ package legacypool import ( - "math/big" "math/rand" "testing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + "github.com/holiman/uint256" ) // Tests that transactions can be added to strict lists and list contents and @@ -60,7 +60,7 @@ func BenchmarkListAdd(b *testing.B) { txs[i] = transaction(uint64(i), 0, key) } // Insert the transactions in a random order - priceLimit := big.NewInt(int64(DefaultConfig.PriceLimit)) + priceLimit := uint256.NewInt(DefaultConfig.PriceLimit) b.ResetTimer() for i := 0; i < b.N; i++ { list := newList(true)