Skip to content

Commit 5ff2ec7

Browse files
authored
perf: reduce number of query context calls (#598)
* update blockchain context once per block * evmd tests pass but this is some pointer madness * no more pointer madness * changelog
1 parent dd7b4c7 commit 5ff2ec7

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [\#511](https://github.com/cosmos/evm/pull/511) Minor code cleanup for `AddPrecompileFn`.
2727
- [\#544](https://github.com/cosmos/evm/pull/544) Parse logs from the txResult.Data and avoid emitting EVM events to cosmos-sdk events.
2828
- [\#582](https://github.com/cosmos/evm/pull/582) Add block max-gas (from genesis.json) and new min-tip (from app.toml/flags) ingestion into mempool config
29+
- [\#598](https://github.com/cosmos/evm/pull/598) Reduce number of times CreateQueryContext in mempool.
2930

3031
### FEATURES
3132

mempool/blockchain.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Blockchain struct {
4141
zeroHeader *types.Header
4242
blockGasLimit uint64
4343
previousHeaderHash common.Hash
44+
latestCtx sdk.Context
4445
}
4546

4647
// newBlockchain creates a new Blockchain instance that bridges Cosmos SDK state with Ethereum mempools.
@@ -78,10 +79,8 @@ func (b Blockchain) Config() *params.ChainConfig {
7879
// including block height, timestamp, gas limits, and base fee (if London fork is active).
7980
// Returns a zero header as placeholder if the context is not yet available.
8081
func (b Blockchain) CurrentBlock() *types.Header {
81-
ctx, err := b.GetLatestCtx()
82-
// This should only error out on the first block.
82+
ctx, err := b.GetLatestContext()
8383
if err != nil {
84-
b.logger.Debug("failed to get latest context, returning zero header", "error", err)
8584
return b.zeroHeader
8685
}
8786

@@ -169,6 +168,12 @@ func (b Blockchain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event
169168

170169
// NotifyNewBlock sends a chain head event when a new block is finalized
171170
func (b *Blockchain) NotifyNewBlock() {
171+
latestCtx, err := b.newLatestContext()
172+
if err != nil {
173+
b.latestCtx = sdk.Context{}
174+
b.logger.Debug("failed to get latest context, notifying chain head", "error", err)
175+
}
176+
b.latestCtx = latestCtx
172177
header := b.CurrentBlock()
173178
headerHash := header.Hash()
174179

@@ -197,7 +202,7 @@ func (b Blockchain) StateAt(hash common.Hash) (vm.StateDB, error) {
197202
}
198203

199204
// Always get the latest context to avoid stale nonce state.
200-
ctx, err := b.GetLatestCtx()
205+
ctx, err := b.GetLatestContext()
201206
if err != nil {
202207
// If we can't get the latest context for blocks past 1, something is seriously wrong with the chain state
203208
return nil, fmt.Errorf("failed to get latest context for StateAt: %w", err)
@@ -210,9 +215,21 @@ func (b Blockchain) StateAt(hash common.Hash) (vm.StateDB, error) {
210215
return stateDB, nil
211216
}
212217

213-
// GetLatestCtx retrieves the most recent query context from the application.
218+
// GetLatestContext returns the latest context as updated by the block,
219+
// or attempts to retrieve it again if unavailable.
220+
func (b Blockchain) GetLatestContext() (sdk.Context, error) {
221+
b.logger.Debug("getting latest context")
222+
223+
if b.latestCtx.Context() != nil {
224+
return b.latestCtx, nil
225+
}
226+
227+
return b.newLatestContext()
228+
}
229+
230+
// newLatestContext retrieves the most recent query context from the application.
214231
// This provides access to the current blockchain state for transaction validation and execution.
215-
func (b Blockchain) GetLatestCtx() (sdk.Context, error) {
232+
func (b Blockchain) newLatestContext() (sdk.Context, error) {
216233
b.logger.Debug("getting latest context")
217234

218235
ctx, err := b.getCtxCallback(0, false)

mempool/mempool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func (m *ExperimentalEVMMempool) shouldRemoveFromEVMPool(tx sdk.Tx) bool {
346346

347347
// If it was a successful transaction or a sequence error, we let the mempool handle the cleaning.
348348
// If it was any other Cosmos or antehandler related issue, then we remove it.
349-
ctx, err := m.blockchain.GetLatestCtx()
349+
ctx, err := m.blockchain.GetLatestContext()
350350
if err != nil {
351351
m.logger.Debug("cannot get latest context for validation, keeping transaction", "error", err)
352352
return false // Cannot validate, keep transaction

0 commit comments

Comments
 (0)