fix testcases

This commit is contained in:
Arpit Temani 2023-09-21 21:07:50 +05:30
parent cc2c27dbd3
commit 4e3bcbc90d
6 changed files with 616 additions and 671 deletions

View file

@ -37,7 +37,6 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
) )
const ( const (
@ -1444,8 +1443,7 @@ func (pool *LegacyPool) promoteExecutables(accounts []common.Address) []*types.T
} }
log.Trace("Removed old queued transactions", "count", len(forwards)) log.Trace("Removed old queued transactions", "count", len(forwards))
// Drop all transactions that are too costly (low balance or out of gas) // Drop all transactions that are too costly (low balance or out of gas)
balUint256, _ := uint256.FromBig(pool.currentState.GetBalance(addr)) drops, _ := list.Filter(pool.currentState.GetBalance(addr), gasLimit)
drops, _ := list.Filter(balUint256, gasLimit)
for _, tx := range drops { for _, tx := range drops {
hash := tx.Hash() hash := tx.Hash()
pool.all.Remove(hash) pool.all.Remove(hash)
@ -1646,8 +1644,7 @@ func (pool *LegacyPool) demoteUnexecutables() {
log.Trace("Removed old pending transaction", "hash", hash) 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 // 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(pool.currentState.GetBalance(addr), gasLimit)
drops, invalids := list.Filter(balUint256, gasLimit)
for _, tx := range drops { for _, tx := range drops {
hash := tx.Hash() hash := tx.Hash()
log.Trace("Removed unpayable pending transaction", "hash", hash) log.Trace("Removed unpayable pending transaction", "hash", hash)

View file

@ -25,10 +25,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/holiman/uint256"
"github.com/ethereum/go-ethereum/common" "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/state"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
@ -155,46 +152,19 @@ func (m *sortedMap) Filter(filter func(*types.Transaction) bool) types.Transacti
removed := m.filter(filter) removed := m.filter(filter)
// If transactions were removed, the heap and cache are ruined // If transactions were removed, the heap and cache are ruined
if len(removed) > 0 { if len(removed) > 0 {
m.reheap(false) m.reheap()
} }
return removed return removed
} }
func (m *sortedMap) reheap(withRlock bool) { func (m *sortedMap) reheap() {
index := make(nonceHeap, 0, len(m.items)) *m.index = make([]uint64, 0, len(m.items))
if withRlock {
m.m.RLock()
log.Debug("Acquired lock over txpool map while performing reheap")
}
for nonce := range m.items { for nonce := range m.items {
index = append(index, nonce) *m.index = append(*m.index, nonce)
} }
heap.Init(m.index)
if withRlock {
m.m.RUnlock()
}
heap.Init(&index)
if withRlock {
m.m.Lock()
}
m.index = &index
if withRlock {
m.m.Unlock()
}
m.cacheMu.Lock()
m.cache = nil 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 // filter is identical to Filter, but **does not** regenerate the heap. This method
@ -431,7 +401,7 @@ type list struct {
strict bool // Whether nonces are strictly continuous or not strict bool // Whether nonces are strictly continuous or not
txs *sortedMap // Heap indexed sorted hash map of the transactions txs *sortedMap // Heap indexed sorted hash map of the transactions
costcap *uint256.Int // Price of the highest costing transaction (reset only if exceeds balance) 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) 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 totalcost *big.Int // Total cost of all transactions in the list
} }
@ -442,6 +412,7 @@ func newList(strict bool) *list {
return &list{ return &list{
strict: strict, strict: strict,
txs: newSortedMap(), txs: newSortedMap(),
costcap: new(big.Int),
totalcost: new(big.Int), totalcost: new(big.Int),
} }
} }
@ -487,10 +458,8 @@ func (l *list) Add(tx *types.Transaction, priceBump uint64) (bool, *types.Transa
l.totalcost.Add(l.totalcost, tx.Cost()) l.totalcost.Add(l.totalcost, tx.Cost())
// Otherwise overwrite the old transaction with the current one // Otherwise overwrite the old transaction with the current one
l.txs.Put(tx) l.txs.Put(tx)
cost := tx.Cost() if cost := tx.Cost(); l.costcap.Cmp(cost) < 0 {
costUint256, _ := uint256.FromBig(cost) l.costcap = cost
if cost = tx.Cost(); l.costcap.Cmp(costUint256) < 0 {
l.costcap = costUint256
} }
if gas := tx.Gas(); l.gascap < gas { if gas := tx.Gas(); l.gascap < gas {
l.gascap = gas l.gascap = gas
@ -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 // 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 // is lower than the costgas cap, the caps will be reset to a new high after removing
// the newly invalidated transactions. // 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 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 return nil, nil
} }
l.costcap = new(big.Int).Set(costLimit) // Lower the caps to the thresholds
l.costcap = costLimit.Clone() // Lower the caps to the thresholds
l.gascap = gasLimit l.gascap = gasLimit
// Filter out all the transactions above the account's funds // Filter out all the transactions above the account's funds
cost := uint256.NewInt(0)
removed := l.txs.Filter(func(tx *types.Transaction) bool { removed := l.txs.Filter(func(tx *types.Transaction) bool {
cost.SetFromBig(tx.Cost()) return tx.Gas() > gasLimit || tx.Cost().Cmp(costLimit) > 0
return tx.Gas() > gasLimit || cost.Gt(costLimit)
}) })
if len(removed) == 0 { if len(removed) == 0 {
return nil, nil return nil, nil
} }
var invalids types.Transactions var invalids types.Transactions
// If the list was strict, filter anything above the lowest nonce // If the list was strict, filter anything above the lowest nonce
if l.strict { if l.strict {
@ -546,17 +511,12 @@ func (l *list) Filter(costLimit *uint256.Int, gasLimit uint64) (types.Transactio
lowest = nonce lowest = nonce
} }
} }
l.txs.m.Lock()
invalids = l.txs.filter(func(tx *types.Transaction) bool { return tx.Nonce() > lowest }) invalids = l.txs.filter(func(tx *types.Transaction) bool { return tx.Nonce() > lowest })
l.txs.m.Unlock()
} }
// Reset total cost // Reset total cost
l.subTotalCost(removed) l.subTotalCost(removed)
l.subTotalCost(invalids) l.subTotalCost(invalids)
l.txs.reheap()
l.txs.reheap(true)
return removed, invalids return removed, invalids
} }
@ -581,7 +541,7 @@ func (l *list) FilterTxConditional(state *state.StateDB) types.Transactions {
return nil return nil
} }
l.txs.reheap(true) l.txs.reheap()
return removed return removed
} }

View file

@ -21,7 +21,6 @@ import (
"math/rand" "math/rand"
"testing" "testing"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -70,15 +69,12 @@ func BenchmarkListAdd(b *testing.B) {
} }
// Insert the transactions in a random order // Insert the transactions in a random order
priceLimit := big.NewInt(int64(DefaultConfig.PriceLimit)) priceLimit := big.NewInt(int64(DefaultConfig.PriceLimit))
b.ResetTimer() b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
list := newList(true) list := newList(true)
for _, v := range rand.Perm(len(txs)) { for _, v := range rand.Perm(len(txs)) {
list.Add(txs[v], DefaultConfig.PriceBump) list.Add(txs[v], DefaultConfig.PriceBump)
list.Filter(uint256.NewInt(priceLimit.Uint64()), DefaultConfig.PriceBump) list.Filter(priceLimit, DefaultConfig.PriceBump)
} }
} }
} }

View file

@ -915,17 +915,14 @@ func (s *BlockChainAPI) GetHeaderByNumber(ctx context.Context, number rpc.BlockN
header, err := s.b.HeaderByNumber(ctx, number) header, err := s.b.HeaderByNumber(ctx, number)
if header != nil && err == nil { if header != nil && err == nil {
response := s.rpcMarshalHeader(ctx, header) response := s.rpcMarshalHeader(ctx, header)
if number == rpc.PendingBlockNumber { if number == rpc.PendingBlockNumber {
// Pending header need to nil out a few fields // Pending header need to nil out a few fields
for _, field := range []string{"hash", "nonce", "miner"} { for _, field := range []string{"hash", "nonce", "miner"} {
response[field] = nil response[field] = nil
} }
} }
return response, err return response, err
} }
return nil, err return nil, err
} }
@ -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{} { func (s *BlockChainAPI) rpcMarshalHeader(ctx context.Context, header *types.Header) map[string]interface{} {
fields := RPCMarshalHeader(header) fields := RPCMarshalHeader(header)
fields["totalDifficulty"] = (*hexutil.Big)(s.b.GetTd(ctx, header.Hash())) fields["totalDifficulty"] = (*hexutil.Big)(s.b.GetTd(ctx, header.Hash()))
return fields return fields
} }

File diff suppressed because it is too large Load diff

View file

@ -21,9 +21,8 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/golang-jwt/jwt/v4"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"github.com/golang-jwt/jwt/v4"
) )
// NewJWTAuth creates an rpc client authentication provider that uses JWT. The // NewJWTAuth creates an rpc client authentication provider that uses JWT. The
@ -36,14 +35,11 @@ func NewJWTAuth(jwtsecret [32]byte) rpc.HTTPAuth {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"iat": &jwt.NumericDate{Time: time.Now()}, "iat": &jwt.NumericDate{Time: time.Now()},
}) })
s, err := token.SignedString(jwtsecret[:]) s, err := token.SignedString(jwtsecret[:])
if err != nil { if err != nil {
return fmt.Errorf("failed to create JWT token: %w", err) return fmt.Errorf("failed to create JWT token: %w", err)
} }
h.Set("Authorization", "Bearer "+s) h.Set("Authorization", "Bearer "+s)
return nil return nil
} }
} }