Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mempool order with different currency tip #275

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion miner/ordering.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func newTxWithMinerFee(tx *txpool.LazyTransaction, from common.Address, baseFee
if tx.GasFeeCap.Cmp(baseFeeConverted) < 0 {
return nil, types.ErrGasFeeCapTooLow
}
tip = new(uint256.Int).Sub(tx.GasFeeCap, baseFee)
tip = new(uint256.Int).Sub(tx.GasFeeCap, baseFeeConverted)
if tip.Gt(tx.GasTipCap) {
tip = tx.GasTipCap
}
Expand Down
52 changes: 52 additions & 0 deletions miner/ordering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,55 @@ func TestTransactionTimeSort(t *testing.T) {
}
}
}

func TestCorrectTransactionSortWithFeeCurrency(t *testing.T) {
t.Parallel()

user1, _ := crypto.GenerateKey()
user2, _ := crypto.GenerateKey()
signer := types.LatestSignerForChainID(big.NewInt(1))

rates := make(common.ExchangeRates)
feeCurrency := common.BigToAddress(common.Big1)
rates[feeCurrency] = big.NewRat(10, 1)

groups := map[common.Address][]*txpool.LazyTransaction{}

baseFee := big.NewInt(10)

addr1 := crypto.PubkeyToAddress(user1.PublicKey)
user1GasFeeCap := uint256.NewInt(950)
// user1 pays 950 feeCurrency, rate is 10/1, baseFee is 10 in feeCurrency is 100,
// 950 - 100 = 850 feeCurrency tip => 85 celo tip
// without converting the tip (error fixed): 950 - 10 = 940 feeCurrency tip => 94 celo tip (this test fails)
groups[addr1] = append(groups[addr1], &txpool.LazyTransaction{
GasFeeCap: user1GasFeeCap,
GasTipCap: user1GasFeeCap,
Gas: 100,
FeeCurrency: &feeCurrency,
})

addr2 := crypto.PubkeyToAddress(user2.PublicKey)
user2GasFeeCap := uint256.NewInt(100)
// user2 pays 100 celos, baseFee is 10, tip is 90
groups[addr2] = append(groups[addr2], &txpool.LazyTransaction{
GasFeeCap: user2GasFeeCap,
GasTipCap: user2GasFeeCap,
Gas: 100,
})

// Sort the transactions and cross check the nonce ordering
txset := newTransactionsByPriceAndNonce(signer, groups, baseFee, rates)

var auxTx *txpool.LazyTransaction
var fees *uint256.Int
auxTx, fees = txset.Peek()
if auxTx.FeeCurrency != nil || auxTx.GasFeeCap.Cmp(user2GasFeeCap) != 0 || fees.Cmp(uint256.NewInt(90)) != 0 {
t.Error("expected tx from user2, got the tx from user1")
gastonponti marked this conversation as resolved.
Show resolved Hide resolved
}
txset.Shift()
auxTx, fees = txset.Peek()
if auxTx.FeeCurrency == nil || *auxTx.FeeCurrency != feeCurrency || auxTx.GasFeeCap.Cmp(user1GasFeeCap) != 0 || fees.Cmp(uint256.NewInt(85)) != 0 {
t.Error("expected tx from user1, got the tx from user2")
}
}