From c66136d5a06ce52e97a417691e994e24f1b524cd Mon Sep 17 00:00:00 2001 From: tycho garen Date: Tue, 1 Mar 2022 15:18:34 -0500 Subject: [PATCH] wip --- internal/mempool/mempool.go | 3 +- internal/mempool/mempool_test.go | 2 +- internal/state/tx_filter.go | 60 +++++++++++++++++++++++++++----- node/node.go | 2 +- node/setup.go | 7 ++-- 5 files changed, 58 insertions(+), 16 deletions(-) diff --git a/internal/mempool/mempool.go b/internal/mempool/mempool.go index 21429721d..26f039de4 100644 --- a/internal/mempool/mempool.go +++ b/internal/mempool/mempool.go @@ -94,7 +94,6 @@ func NewTxMempool( logger log.Logger, cfg *config.MempoolConfig, proxyAppConn proxy.AppConnMempool, - height int64, options ...TxMempoolOption, ) *TxMempool { @@ -102,7 +101,7 @@ func NewTxMempool( logger: logger, config: cfg, proxyAppConn: proxyAppConn, - height: height, + height: -1, cache: NopTxCache{}, metrics: NopMetrics(), txStore: NewTxStore(), diff --git a/internal/mempool/mempool_test.go b/internal/mempool/mempool_test.go index e2cf12e07..68eb5731b 100644 --- a/internal/mempool/mempool_test.go +++ b/internal/mempool/mempool_test.go @@ -95,7 +95,7 @@ func setup(ctx context.Context, t testing.TB, cacheSize int, options ...TxMempoo appConnMem.Wait() }) - return NewTxMempool(logger.With("test", t.Name()), cfg.Mempool, appConnMem, 0, options...) + return NewTxMempool(logger.With("test", t.Name()), cfg.Mempool, appConnMem, options...) } func checkTxs(ctx context.Context, t *testing.T, txmp *TxMempool, numTxs int, peerID uint16) []testTx { diff --git a/internal/state/tx_filter.go b/internal/state/tx_filter.go index 871e08ae6..f2950dbff 100644 --- a/internal/state/tx_filter.go +++ b/internal/state/tx_filter.go @@ -1,22 +1,66 @@ package state import ( + "sync" + "time" + + abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/internal/mempool" "github.com/tendermint/tendermint/types" ) +func cachingStateFetcher(store Store) func() (State, error) { + var ( + last time.Time + mutex = &sync.Mutex{} + ttl time.Duration + cache State + err error + ) + + return func() (State, error) { + mutex.Lock() + defer mutex.Unlock() + + if cache.ChainID == "" { // is nil + cache, err = store.Load() + if err != nil { + return State{}, err + } + last = time.Now() + ttl = 100*time.Millisecond + (cache.LastBlockTime.Add(last) * 2) + } + + } + +} + // TxPreCheck returns a function to filter transactions before processing. // The function limits the size of a transaction to the block's maximum data size. -func TxPreCheck(state State) mempool.PreCheckFunc { - maxDataBytes := types.MaxDataBytesNoEvidence( - state.ConsensusParams.Block.MaxBytes, - state.Validators.Size(), - ) - return mempool.PreCheckMaxBytes(maxDataBytes) +func TxPreCheck(store Store) mempool.PreCheckFunc { + return func(tx types.Tx) error { + // TODO: this should probably be cached at some level. + state, err := store.Load() + if err != nil { + return err + } + maxDataBytes := types.MaxDataBytesNoEvidence( + state.ConsensusParams.Block.MaxBytes, + state.Validators.Size(), + ) + return mempool.PreCheckMaxBytes(maxDataBytes)(tx) + } } // TxPostCheck returns a function to filter transactions after processing. // The function limits the gas wanted by a transaction to the block's maximum total gas. -func TxPostCheck(state State) mempool.PostCheckFunc { - return mempool.PostCheckMaxGas(state.ConsensusParams.Block.MaxGas) +func TxPostCheck(store Store) mempool.PostCheckFunc { + return func(tx types.Tx, resp *abci.ResponseCheckTx) error { + // TODO: this should probably be cached at some level. + state, err := store.Load() + if err != nil { + return err + } + return mempool.PostCheckMaxGas(state.ConsensusParams.Block.MaxGas)(tx, resp) + } } diff --git a/node/node.go b/node/node.go index 7d7b75170..eaaf16ba1 100644 --- a/node/node.go +++ b/node/node.go @@ -272,7 +272,7 @@ func makeNode( } mpReactor, mp, err := createMempoolReactor(ctx, - cfg, proxyApp, state, nodeMetrics.mempool, peerManager, router, logger, + cfg, proxyApp, stateStore, nodeMetrics.mempool, peerManager, router, logger, ) if err != nil { return nil, combineCloseError(err, makeCloser(closers)) diff --git a/node/setup.go b/node/setup.go index e880cd5c4..93973e0d1 100644 --- a/node/setup.go +++ b/node/setup.go @@ -172,7 +172,7 @@ func createMempoolReactor( ctx context.Context, cfg *config.Config, proxyApp proxy.AppConns, - state sm.State, + store sm.Store, memplMetrics *mempool.Metrics, peerManager *p2p.PeerManager, router *p2p.Router, @@ -184,10 +184,9 @@ func createMempoolReactor( logger, cfg.Mempool, proxyApp.Mempool(), - state.LastBlockHeight, mempool.WithMetrics(memplMetrics), - mempool.WithPreCheck(sm.TxPreCheck(state)), - mempool.WithPostCheck(sm.TxPostCheck(state)), + mempool.WithPreCheck(sm.TxPreCheck(store)), + mempool.WithPostCheck(sm.TxPostCheck(store)), ) reactor, err := mempool.NewReactor(