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

feat: loop through all txs before returning the tx set to consensus #1113

Merged
merged 3 commits into from
Oct 17, 2023
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
12 changes: 7 additions & 5 deletions mempool/cat/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,14 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs {
var keep []types.Tx //nolint:prealloc
for _, w := range txmp.allEntriesSorted() {
// N.B. When computing byte size, we need to include the overhead for
// encoding as protobuf to send to the application.
totalGas += w.gasWanted
totalBytes += types.ComputeProtoSizeForTxs([]types.Tx{w.tx})
if (maxGas >= 0 && totalGas > maxGas) || (maxBytes >= 0 && totalBytes > maxBytes) {
break
// encoding as protobuf to send to the application. This actually overestimates it
// as we add the proto overhead to each transaction
txBytes := types.ComputeProtoSizeForTxs([]types.Tx{w.tx})
if (maxGas >= 0 && totalGas+w.gasWanted > maxGas) || (maxBytes >= 0 && totalBytes+txBytes > maxBytes) {
continue
}
totalBytes += txBytes
totalGas += w.gasWanted
keep = append(keep, w.tx)
}
return keep
Expand Down
22 changes: 22 additions & 0 deletions mempool/cat/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,28 @@ func TestTxPool_ReapMaxBytesMaxGas(t *testing.T) {
require.Len(t, reapedTxs, 25)
}

func TestTxMempoolTxLargerThanMaxBytes(t *testing.T) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
txmp := setup(t, 0)
bigPrefix := make([]byte, 100)
_, err := rng.Read(bigPrefix)
require.NoError(t, err)
// large high priority tx
bigTx := []byte(fmt.Sprintf("sender-1-1=%X=2", bigPrefix))
smallPrefix := make([]byte, 20)
_, err = rng.Read(smallPrefix)
require.NoError(t, err)
// smaller low priority tx with different sender
smallTx := []byte(fmt.Sprintf("sender-2-1=%X=1", smallPrefix))
require.NoError(t, txmp.CheckTx(bigTx, nil, mempool.TxInfo{SenderID: 1}))
require.NoError(t, txmp.CheckTx(smallTx, nil, mempool.TxInfo{SenderID: 1}))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it impacts the results of the test but is this line supposed to have a different SenderID?

Suggested change
require.NoError(t, txmp.CheckTx(smallTx, nil, mempool.TxInfo{SenderID: 1}))
require.NoError(t, txmp.CheckTx(smallTx, nil, mempool.TxInfo{SenderID: 2}))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no the sender can be the same. It's okay for the txs to be sent by the same peer. You might be confusing it with sender in CheckTx

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't follow. Is this line referencing the sender in CheckTx?


// reap by max bytes less than the large tx
reapedTxs := txmp.ReapMaxBytesMaxGas(100, -1)
require.Len(t, reapedTxs, 1)
require.Equal(t, types.Tx(smallTx), reapedTxs[0])
}

func TestTxPool_ReapMaxTxs(t *testing.T) {
txmp := setup(t, 0)
txs := checkTxs(t, txmp, 100, 0)
Expand Down
12 changes: 7 additions & 5 deletions mempool/v1/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,14 @@ func (txmp *TxMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs {
var keep []types.Tx //nolint:prealloc
for _, w := range txmp.allEntriesSorted() {
// N.B. When computing byte size, we need to include the overhead for
// encoding as protobuf to send to the application.
totalGas += w.gasWanted
totalBytes += types.ComputeProtoSizeForTxs([]types.Tx{w.tx})
if (maxGas >= 0 && totalGas > maxGas) || (maxBytes >= 0 && totalBytes > maxBytes) {
break
// encoding as protobuf to send to the application. This actually overestimates it
// as we add the proto overhead to each transaction
txBytes := types.ComputeProtoSizeForTxs([]types.Tx{w.tx})
if (maxGas >= 0 && totalGas+w.gasWanted > maxGas) || (maxBytes >= 0 && totalBytes+txBytes > maxBytes) {
continue
}
totalBytes += txBytes
totalGas += w.gasWanted
keep = append(keep, w.tx)
}
return keep
Expand Down
22 changes: 22 additions & 0 deletions mempool/v1/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,28 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) {
require.Len(t, reapedTxs, 25)
}

func TestTxMempoolTxLargerThanMaxBytes(t *testing.T) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
txmp := setup(t, 0)
bigPrefix := make([]byte, 100)
_, err := rng.Read(bigPrefix)
require.NoError(t, err)
// large high priority tx
bigTx := []byte(fmt.Sprintf("sender-1-1=%X=2", bigPrefix))
smallPrefix := make([]byte, 20)
_, err = rng.Read(smallPrefix)
require.NoError(t, err)
// smaller low priority tx with different sender
smallTx := []byte(fmt.Sprintf("sender-2-1=%X=1", smallPrefix))
require.NoError(t, txmp.CheckTx(bigTx, nil, mempool.TxInfo{SenderID: 1}))
require.NoError(t, txmp.CheckTx(smallTx, nil, mempool.TxInfo{SenderID: 1}))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require.NoError(t, txmp.CheckTx(smallTx, nil, mempool.TxInfo{SenderID: 1}))
require.NoError(t, txmp.CheckTx(smallTx, nil, mempool.TxInfo{SenderID: 2}))


// reap by max bytes less than the large tx
reapedTxs := txmp.ReapMaxBytesMaxGas(100, -1)
require.Len(t, reapedTxs, 1)
require.Equal(t, types.Tx(smallTx), reapedTxs[0])
}

func TestTxMempool_ReapMaxTxs(t *testing.T) {
txmp := setup(t, 0)
tTxs := checkTxs(t, txmp, 100, 0)
Expand Down
Loading