Skip to content

Commit bda72a4

Browse files
committed
Use cached checker for bundles
1 parent 4877789 commit bda72a4

6 files changed

Lines changed: 25 additions & 88 deletions

File tree

evmcore/bundle_integration.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,13 @@ import (
2020
"github.com/0xsoniclabs/sonic/gossip/blockproc/bundle"
2121
"github.com/0xsoniclabs/sonic/inter/state"
2222
"github.com/0xsoniclabs/sonic/opera"
23+
"github.com/0xsoniclabs/sonic/utils"
2324
"github.com/Fantom-foundation/lachesis-base/inter/idx"
2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/core/types"
2627
params "github.com/ethereum/go-ethereum/params"
2728
)
2829

29-
//go:generate mockgen -source=bundle_integration.go -destination=bundle_integration_mock.go -package=evmcore
30-
31-
// bundleChecker is an interface for checking if a bundle transaction is pending
32-
// for execution. A bundle is pending if it has not yet been processed, its
33-
// block range is not yet exceeded, and it is not permanently blocked due to an
34-
// on-chain state mutation (e.g. a mandatory transaction in the bundle using a
35-
// nonce that has already been used).
36-
//
37-
// This interface facilitates testing and decouples the bundle integration
38-
// logic from the transaction pool.
39-
type bundleChecker interface {
40-
isPending(tx *types.Transaction) bool
41-
}
42-
4330
// BundleIntegrationImplementation uses the chain and state to determine if a
4431
// bundle transaction is still pending for execution or obsolete.
4532
type BundleIntegrationImplementation struct {
@@ -48,18 +35,19 @@ type BundleIntegrationImplementation struct {
4835
state state.StateDB
4936
}
5037

51-
// newBundleChecker creates a new BundleChecker instance.
52-
func newBundleChecker(
38+
// createBundleChecker creates a new BundleChecker instance.
39+
func createBundleChecker(
5340
rules opera.Rules,
5441
chain StateReader,
5542
state state.StateDB,
5643
_ types.Signer, // needed for type compatibility
57-
) bundleChecker {
58-
return &BundleIntegrationImplementation{
44+
) utils.Checker {
45+
impl := &BundleIntegrationImplementation{
5946
rules: rules,
6047
chain: chain,
6148
state: state,
6249
}
50+
return utils.NewUnchachedChecker(impl.isPending)
6351
}
6452

6553
func (s *BundleIntegrationImplementation) isPending(tx *types.Transaction) bool {

evmcore/bundle_integration_mock.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

evmcore/tx_list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func (l *txList) Forward(threshold uint64) types.Transactions {
358358
// a point in calculating all the costs or if the balance covers all. If the threshold
359359
// is lower than the costgas cap, the caps will be reset to a new high after removing
360360
// the newly invalidated transactions.
361-
func (l *txList) Filter(costLimit *big.Int, gasLimit uint64, subsidiesChecker utils.Checker, bundleChecker bundleChecker) (types.Transactions, types.Transactions) {
361+
func (l *txList) Filter(costLimit *big.Int, gasLimit uint64, subsidiesChecker utils.Checker, bundleChecker utils.Checker) (types.Transactions, types.Transactions) {
362362
hasSponsored := l.txs.containsFunc(subsidies.IsSponsorshipRequest)
363363

364364
// If all transactions are below the threshold, short circuit
@@ -373,7 +373,7 @@ func (l *txList) Filter(costLimit *big.Int, gasLimit uint64, subsidiesChecker ut
373373
// Bundle transactions need to be checked before sponsored transactions
374374
// since bundles qualify as sponsored transactions.
375375
if bundle.IsEnvelope(tx) {
376-
return !bundleChecker.isPending(tx)
376+
return !bundleChecker.Check(tx)
377377
}
378378
if subsidies.IsSponsorshipRequest(tx) {
379379
return !subsidiesChecker.Check(tx)

evmcore/tx_pool.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ type bundleCheckerFactory func(
205205
chain StateReader,
206206
state state.StateDB,
207207
signer types.Signer,
208-
) bundleChecker
208+
) utils.Checker
209209

210210
// TxPoolConfig are the configuration parameters of the transaction pool.
211211
type TxPoolConfig struct {
@@ -352,6 +352,7 @@ type TxPool struct {
352352
subsidiesCheckerCache *utils.CheckerCache // Cache for subsidies check results
353353

354354
bundleCheckerFactory bundleCheckerFactory // Factory to create a bundle checker instance
355+
bundlesCheckerCache *utils.CheckerCache // Cache for bundle check results
355356
}
356357

357358
type txpoolResetRequest struct {
@@ -364,7 +365,7 @@ func NewTxPool(
364365
config TxPoolConfig,
365366
chainconfig *params.ChainConfig,
366367
chain StateReader) *TxPool {
367-
return newTxPool(config, chainconfig, chain, createSubsidiesChecker, newBundleChecker)
368+
return newTxPool(config, chainconfig, chain, createSubsidiesChecker, createBundleChecker)
368369
}
369370

370371
func newTxPool(
@@ -402,7 +403,7 @@ func newTxPool(
402403
subsidiesCheckerCache: utils.NewCheckerCache(-1), // use default size
403404

404405
bundleCheckerFactory: bundleCheckerFactory,
405-
// TODO: add a cache for bundle checker results if the checks are expensive
406+
bundlesCheckerCache: utils.NewCheckerCache(-1), // use default size
406407
}
407408
pool.locals = newAccountSet(pool.signer)
408409
for _, addr := range config.Locals {
@@ -1522,13 +1523,14 @@ func (pool *TxPool) createCachedSubsidiesChecker() utils.Checker {
15221523
))
15231524
}
15241525

1525-
func (pool *TxPool) createBundleChecker() bundleChecker {
1526-
return pool.bundleCheckerFactory(
1527-
pool.chain.CurrentRules(),
1528-
pool.chain,
1529-
pool.currentState,
1530-
pool.signer,
1531-
)
1526+
func (pool *TxPool) createBundleChecker() utils.Checker {
1527+
return pool.bundlesCheckerCache.Wrap(
1528+
pool.bundleCheckerFactory(
1529+
pool.chain.CurrentRules(),
1530+
pool.chain,
1531+
pool.currentState,
1532+
pool.signer,
1533+
))
15321534
}
15331535

15341536
// promoteExecutables moves transactions that have become processable from the

evmcore/tx_pool_subsidies_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestTxPool_SponsoredTransactionsAreIncludedInThePendingSet(t *testing.T) {
6363
factory := func(opera.Rules, StateReader, state.StateDB, types.Signer) utils.Checker {
6464
return subsidiesCheckerMock
6565
}
66-
pool := newTxPool(poolConfig, chainConfig, chain, factory, newBundleChecker)
66+
pool := newTxPool(poolConfig, chainConfig, chain, factory, createBundleChecker)
6767

6868
// transactions per sender
6969
const transactionsPerSender = 5

evmcore/tx_pool_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,6 +3426,8 @@ func testBundleCheckerFactory(
34263426
chain StateReader,
34273427
state state.StateDB,
34283428
signer types.Signer,
3429-
) bundleChecker {
3430-
return nil
3429+
) utils.Checker {
3430+
return utils.NewUnchachedChecker(func(tx *types.Transaction) bool {
3431+
panic("unexpected call to bundle features during legacy testing")
3432+
})
34313433
}

0 commit comments

Comments
 (0)