Skip to content

Commit

Permalink
fix testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
temaniarpit27 committed Sep 21, 2023
1 parent cc2c27d commit 4e3bcbc
Show file tree
Hide file tree
Showing 6 changed files with 616 additions and 671 deletions.
7 changes: 2 additions & 5 deletions core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
)

const (
Expand Down Expand Up @@ -1444,8 +1443,7 @@ func (pool *LegacyPool) promoteExecutables(accounts []common.Address) []*types.T
}
log.Trace("Removed old queued transactions", "count", len(forwards))
// Drop all transactions that are too costly (low balance or out of gas)
balUint256, _ := uint256.FromBig(pool.currentState.GetBalance(addr))
drops, _ := list.Filter(balUint256, gasLimit)
drops, _ := list.Filter(pool.currentState.GetBalance(addr), gasLimit)
for _, tx := range drops {
hash := tx.Hash()
pool.all.Remove(hash)
Expand Down Expand Up @@ -1646,8 +1644,7 @@ func (pool *LegacyPool) demoteUnexecutables() {
log.Trace("Removed old pending transaction", "hash", hash)
}
// Drop all transactions that are too costly (low balance or out of gas), and queue any invalids back for later
balUint256, _ := uint256.FromBig(pool.currentState.GetBalance(addr))
drops, invalids := list.Filter(balUint256, gasLimit)
drops, invalids := list.Filter(pool.currentState.GetBalance(addr), gasLimit)
for _, tx := range drops {
hash := tx.Hash()
log.Trace("Removed unpayable pending transaction", "hash", hash)
Expand Down
74 changes: 17 additions & 57 deletions core/txpool/legacypool/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ import (
"sync/atomic"
"time"

"github.com/holiman/uint256"

"github.com/ethereum/go-ethereum/common"
cmath "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -155,46 +152,19 @@ func (m *sortedMap) Filter(filter func(*types.Transaction) bool) types.Transacti
removed := m.filter(filter)
// If transactions were removed, the heap and cache are ruined
if len(removed) > 0 {
m.reheap(false)
m.reheap()
}

return removed
}

func (m *sortedMap) reheap(withRlock bool) {
index := make(nonceHeap, 0, len(m.items))

if withRlock {
m.m.RLock()
log.Debug("Acquired lock over txpool map while performing reheap")
}

func (m *sortedMap) reheap() {
*m.index = make([]uint64, 0, len(m.items))
for nonce := range m.items {
index = append(index, nonce)
}

if withRlock {
m.m.RUnlock()
}

heap.Init(&index)

if withRlock {
m.m.Lock()
}

m.index = &index

if withRlock {
m.m.Unlock()
*m.index = append(*m.index, nonce)
}

m.cacheMu.Lock()
heap.Init(m.index)
m.cache = nil
m.isEmpty = true
m.cacheMu.Unlock()

resetCacheGauge.Inc(1)
}

// filter is identical to Filter, but **does not** regenerate the heap. This method
Expand Down Expand Up @@ -431,9 +401,9 @@ type list struct {
strict bool // Whether nonces are strictly continuous or not
txs *sortedMap // Heap indexed sorted hash map of the transactions

costcap *uint256.Int // Price of the highest costing transaction (reset only if exceeds balance)
gascap uint64 // Gas limit of the highest spending transaction (reset only if exceeds block limit)
totalcost *big.Int // Total cost of all transactions in the list
costcap *big.Int // Price of the highest costing transaction (reset only if exceeds balance)
gascap uint64 // Gas limit of the highest spending transaction (reset only if exceeds block limit)
totalcost *big.Int // Total cost of all transactions in the list
}

// newList create a new transaction list for maintaining nonce-indexable fast,
Expand All @@ -442,6 +412,7 @@ func newList(strict bool) *list {
return &list{
strict: strict,
txs: newSortedMap(),
costcap: new(big.Int),
totalcost: new(big.Int),
}
}
Expand Down Expand Up @@ -487,10 +458,8 @@ func (l *list) Add(tx *types.Transaction, priceBump uint64) (bool, *types.Transa
l.totalcost.Add(l.totalcost, tx.Cost())
// Otherwise overwrite the old transaction with the current one
l.txs.Put(tx)
cost := tx.Cost()
costUint256, _ := uint256.FromBig(cost)
if cost = tx.Cost(); l.costcap.Cmp(costUint256) < 0 {
l.costcap = costUint256
if cost := tx.Cost(); l.costcap.Cmp(cost) < 0 {
l.costcap = cost
}
if gas := tx.Gas(); l.gascap < gas {
l.gascap = gas
Expand All @@ -517,26 +486,22 @@ func (l *list) Forward(threshold uint64) types.Transactions {
// a point in calculating all the costs or if the balance covers all. If the threshold
// is lower than the costgas cap, the caps will be reset to a new high after removing
// the newly invalidated transactions.
func (l *list) Filter(costLimit *uint256.Int, gasLimit uint64) (types.Transactions, types.Transactions) {
func (l *list) Filter(costLimit *big.Int, gasLimit uint64) (types.Transactions, types.Transactions) {
// If all transactions are below the threshold, short circuit
if cmath.U256LTE(l.costcap, costLimit) && l.gascap <= gasLimit {
if l.costcap.Cmp(costLimit) <= 0 && l.gascap <= gasLimit {
return nil, nil
}

l.costcap = costLimit.Clone() // Lower the caps to the thresholds
l.costcap = new(big.Int).Set(costLimit) // Lower the caps to the thresholds
l.gascap = gasLimit

// Filter out all the transactions above the account's funds
cost := uint256.NewInt(0)
removed := l.txs.Filter(func(tx *types.Transaction) bool {
cost.SetFromBig(tx.Cost())
return tx.Gas() > gasLimit || cost.Gt(costLimit)
return tx.Gas() > gasLimit || tx.Cost().Cmp(costLimit) > 0
})

if len(removed) == 0 {
return nil, nil
}

var invalids types.Transactions
// If the list was strict, filter anything above the lowest nonce
if l.strict {
Expand All @@ -546,17 +511,12 @@ func (l *list) Filter(costLimit *uint256.Int, gasLimit uint64) (types.Transactio
lowest = nonce
}
}

l.txs.m.Lock()
invalids = l.txs.filter(func(tx *types.Transaction) bool { return tx.Nonce() > lowest })
l.txs.m.Unlock()
}
// Reset total cost
l.subTotalCost(removed)
l.subTotalCost(invalids)

l.txs.reheap(true)

l.txs.reheap()
return removed, invalids
}

Expand All @@ -581,7 +541,7 @@ func (l *list) FilterTxConditional(state *state.StateDB) types.Transactions {
return nil
}

l.txs.reheap(true)
l.txs.reheap()

return removed
}
Expand Down
6 changes: 1 addition & 5 deletions core/txpool/legacypool/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"math/rand"
"testing"

"github.com/holiman/uint256"
"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -70,15 +69,12 @@ func BenchmarkListAdd(b *testing.B) {
}
// Insert the transactions in a random order
priceLimit := big.NewInt(int64(DefaultConfig.PriceLimit))

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
list := newList(true)
for _, v := range rand.Perm(len(txs)) {
list.Add(txs[v], DefaultConfig.PriceBump)
list.Filter(uint256.NewInt(priceLimit.Uint64()), DefaultConfig.PriceBump)
list.Filter(priceLimit, DefaultConfig.PriceBump)
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,17 +915,14 @@ func (s *BlockChainAPI) GetHeaderByNumber(ctx context.Context, number rpc.BlockN
header, err := s.b.HeaderByNumber(ctx, number)
if header != nil && err == nil {
response := s.rpcMarshalHeader(ctx, header)

if number == rpc.PendingBlockNumber {
// Pending header need to nil out a few fields
for _, field := range []string{"hash", "nonce", "miner"} {
response[field] = nil
}
}

return response, err
}

return nil, err
}

Expand Down Expand Up @@ -1650,7 +1647,6 @@ func RPCMarshalBlock(block *types.Block, inclTx bool, fullTx bool, config *param
func (s *BlockChainAPI) rpcMarshalHeader(ctx context.Context, header *types.Header) map[string]interface{} {
fields := RPCMarshalHeader(header)
fields["totalDifficulty"] = (*hexutil.Big)(s.b.GetTd(ctx, header.Hash()))

return fields
}

Expand Down
Loading

0 comments on commit 4e3bcbc

Please sign in to comment.