From 88a63cbad7a131c0a46469b9e04901fed40ef270 Mon Sep 17 00:00:00 2001 From: tycho garen Date: Wed, 2 Mar 2022 07:38:46 -0500 Subject: [PATCH] wip wip --- internal/state/execution.go | 7 +++---- internal/state/tx_filter.go | 32 +++++++++++++++++++++----------- node/node_test.go | 3 --- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/internal/state/execution.go b/internal/state/execution.go index cdd6e009b..f06c06e44 100644 --- a/internal/state/execution.go +++ b/internal/state/execution.go @@ -253,7 +253,7 @@ func (blockExec *BlockExecutor) ApplyBlock( } // Lock mempool, commit app state, update mempoool. - appHash, retainHeight, err := blockExec.Commit(ctx, state, block, abciResponses.FinalizeBlock.Txs) + appHash, retainHeight, err := blockExec.Commit(ctx, block, abciResponses.FinalizeBlock.Txs) if err != nil { return state, fmt.Errorf("commit failed for application: %w", err) } @@ -324,7 +324,6 @@ func (blockExec *BlockExecutor) VerifyVoteExtension(ctx context.Context, vote *t // state before new txs are run in the mempool, lest they be invalid. func (blockExec *BlockExecutor) Commit( ctx context.Context, - state State, block *types.Block, deliverTxResponses []*abci.ResponseDeliverTx, ) ([]byte, int64, error) { @@ -360,8 +359,8 @@ func (blockExec *BlockExecutor) Commit( block.Height, block.Txs, deliverTxResponses, - TxPreCheck(state), - TxPostCheck(state), + TxPreCheck(blockExec.store), + TxPostCheck(blockExec.store), ) return res.Data, res.RetainHeight, err diff --git a/internal/state/tx_filter.go b/internal/state/tx_filter.go index f2950dbff..62605c08d 100644 --- a/internal/state/tx_filter.go +++ b/internal/state/tx_filter.go @@ -22,15 +22,23 @@ func cachingStateFetcher(store Store) 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) + if time.Since(last) < ttl && cache.ChainID != "" { + return cache, nil } + cache, err = store.Load() + if err != nil { + return State{}, err + } + last = time.Now() + // at least 100ms but maybe as much as that+ + // a block interval. This might end up being + // too much, but it replaces a mechanism that + // cached these values for the entire runtime + // of the process + ttl = (100 * time.Millisecond) + cache.LastBlockTime.Sub(last) + + return cache, nil } } @@ -38,9 +46,10 @@ func cachingStateFetcher(store Store) func() (State, error) { // 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(store Store) mempool.PreCheckFunc { + fetch := cachingStateFetcher(store) + return func(tx types.Tx) error { - // TODO: this should probably be cached at some level. - state, err := store.Load() + state, err := fetch() if err != nil { return err } @@ -55,9 +64,10 @@ func TxPreCheck(store Store) mempool.PreCheckFunc { // 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(store Store) mempool.PostCheckFunc { + fetch := cachingStateFetcher(store) + return func(tx types.Tx, resp *abci.ResponseCheckTx) error { - // TODO: this should probably be cached at some level. - state, err := store.Load() + state, err := fetch() if err != nil { return err } diff --git a/node/node_test.go b/node/node_test.go index 41cb1b6a9..5fbf80e00 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -292,7 +292,6 @@ func TestCreateProposalBlock(t *testing.T) { logger.With("module", "mempool"), cfg.Mempool, proxyApp.Mempool(), - state.LastBlockHeight, ) // Make EvidencePool @@ -392,7 +391,6 @@ func TestMaxTxsProposalBlockSize(t *testing.T) { logger.With("module", "mempool"), cfg.Mempool, proxyApp.Mempool(), - state.LastBlockHeight, ) // fill the mempool with one txs just below the maximum size @@ -457,7 +455,6 @@ func TestMaxProposalBlockSize(t *testing.T) { logger.With("module", "mempool"), cfg.Mempool, proxyApp.Mempool(), - state.LastBlockHeight, ) // fill the mempool with one txs just below the maximum size