Skip to content

Commit

Permalink
[TxPool] Reject future transactions during high memory usage (#690)
Browse files Browse the repository at this point in the history
* reject future transactions when the TxPool is low on available slots
  • Loading branch information
dbrajovic committed Aug 29, 2022
1 parent 210de9e commit 5d2db1e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
ErrAlreadyKnown = errors.New("already known")
ErrOversizedData = errors.New("oversized data")
ErrMaxEnqueuedLimitReached = errors.New("maximum number of enqueued transactions reached")
ErrRejectFutureTx = errors.New("rejected future tx due to low slots")
)

// indicates origin of a transaction
Expand Down Expand Up @@ -656,6 +657,12 @@ func (p *TxPool) addTx(origin txOrigin, tx *types.Transaction) error {

if p.gauge.highPressure() {
p.signalPruning()

// only accept transactions with expected nonce
if account := p.accounts.get(tx.From); account != nil &&
tx.Nonce > account.getNonce() {
return ErrRejectFutureTx
}
}

// check for overflow
Expand Down
55 changes: 55 additions & 0 deletions txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,61 @@ func TestAddTxHighPressure(t *testing.T) {
// pick up signal
_, ok := <-pool.pruneCh
assert.True(t, ok)

// unblock the handler (handler would block entire test run)
<-pool.enqueueReqCh
},
)

t.Run(
"reject tx with nonce not matching expected",
func(t *testing.T) {
t.Parallel()

pool, err := newTestPool()
assert.NoError(t, err)
pool.SetSigner(&mockSigner{})

pool.createAccountOnce(addr1)
pool.accounts.get(addr1).nextNonce = 5

// mock high pressure
slots := 1 + (highPressureMark*pool.gauge.max)/100
pool.gauge.increase(slots)

assert.ErrorIs(t,
ErrRejectFutureTx,
pool.addTx(local, newTx(addr1, 8, 1)),
)
},
)

t.Run(
"accept tx with expected nonce during high gauge level",
func(t *testing.T) {
t.Parallel()

pool, err := newTestPool()
assert.NoError(t, err)
pool.SetSigner(&mockSigner{})

pool.createAccountOnce(addr1)
pool.accounts.get(addr1).nextNonce = 5

// mock high pressure
slots := 1 + (highPressureMark*pool.gauge.max)/100
println("slots", slots, "max", pool.gauge.max)
pool.gauge.increase(slots)

go func() {
assert.NoError(t,
pool.addTx(local, newTx(addr1, 5, 1)),
)
}()
enq := <-pool.enqueueReqCh

_, exists := pool.index.get(enq.tx.Hash)
assert.True(t, exists)
},
)
}
Expand Down

0 comments on commit 5d2db1e

Please sign in to comment.