Skip to content

Commit

Permalink
Add TxComparator
Browse files Browse the repository at this point in the history
  • Loading branch information
hbandura committed Oct 30, 2023
1 parent 9a32fe5 commit 092f594
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions core/txpool/legacypool/celo_legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)

// filter Filters transactions from the given list, according to remaining balance (per currency, minus l1Cost)
// and gasLimit. Returns drops and invalid txs.
func (pool *LegacyPool) filter(list *celo_list, addr common.Address, l1Cost *big.Int, gasLimit uint64) (types.Transactions, types.Transactions) {
st := pool.currentState
fcv := pool.feeCurrencyValidator
Expand Down
5 changes: 5 additions & 0 deletions core/txpool/legacypool/celo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)

type TxComparator interface {
GasFeeCapCmp(*types.Transaction, *types.Transaction) int
GasTipCapCmp(*types.Transaction, *types.Transaction) int
}

type celo_list struct {
list *list
totalCost map[common.Address]*big.Int
Expand Down
3 changes: 2 additions & 1 deletion core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ type LegacyPool struct {

// Celo
feeCurrencyValidator txpool.FeeCurrencyValidator
txComparator TxComparator
}

type txpoolResetRequest struct {
Expand Down Expand Up @@ -278,7 +279,7 @@ func New(config Config, chain BlockChain) *LegacyPool {
log.Info("Setting new local account", "address", addr)
pool.locals.add(addr)
}
pool.priced = newPricedList(pool.all)
pool.priced = newPricedList(pool.all, pool.txComparator)

if (!config.NoLocals || config.JournalRemote) && config.Journal != "" {
pool.journal = newTxJournal(config.Journal)
Expand Down
15 changes: 11 additions & 4 deletions core/txpool/legacypool/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ func (l *list) subTotalCost(txs []*types.Transaction) {
type priceHeap struct {
baseFee *big.Int // heap should always be re-sorted after baseFee is changed
list []*types.Transaction

// Celo currency comparator
txComparator TxComparator
}

func (h *priceHeap) Len() int { return len(h.list) }
Expand All @@ -505,12 +508,13 @@ func (h *priceHeap) cmp(a, b *types.Transaction) int {
return c
}
}
// Celo modification, using a txComparator (which uses currency exchange rates)
// Compare fee caps if baseFee is not specified or effective tips are equal
if c := a.GasFeeCapCmp(b); c != 0 {
if c := h.txComparator.GasFeeCapCmp(a, b); c != 0 {
return c
}
// Compare tips if effective tips and fee caps are equal
return a.GasTipCapCmp(b)
return h.txComparator.GasTipCapCmp(a, b)
}

func (h *priceHeap) Push(x interface{}) {
Expand Down Expand Up @@ -554,10 +558,13 @@ const (
)

// newPricedList creates a new price-sorted transaction heap.
func newPricedList(all *lookup) *pricedList {
return &pricedList{
func newPricedList(all *lookup, txComparator TxComparator) *pricedList {
ret := &pricedList{
all: all,
}
ret.urgent.txComparator = txComparator
ret.floating.txComparator = txComparator
return ret
}

// Put inserts a new transaction into the heap.
Expand Down

0 comments on commit 092f594

Please sign in to comment.