Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [\#511](https://github.com/cosmos/evm/pull/511) Minor code cleanup for `AddPrecompileFn`.
- [\#544](https://github.com/cosmos/evm/pull/544) Parse logs from the txResult.Data and avoid emitting EVM events to cosmos-sdk events.
- [\#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
- [\#598](https://github.com/cosmos/evm/pull/598) Reduce number of times CreateQueryContext in mempool.

### FEATURES

Expand Down
29 changes: 23 additions & 6 deletions mempool/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Blockchain struct {
zeroHeader *types.Header
blockGasLimit uint64
previousHeaderHash common.Hash
latestCtx sdk.Context
}

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

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

// NotifyNewBlock sends a chain head event when a new block is finalized
func (b *Blockchain) NotifyNewBlock() {
latestCtx, err := b.newLatestContext()
if err != nil {
b.latestCtx = sdk.Context{}
b.logger.Debug("failed to get latest context, notifying chain head", "error", err)
}
b.latestCtx = latestCtx
header := b.CurrentBlock()
headerHash := header.Hash()

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

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

// GetLatestCtx retrieves the most recent query context from the application.
// GetLatestContext returns the latest context as updated by the block,
// or attempts to retrieve it again if unavailable.
func (b Blockchain) GetLatestContext() (sdk.Context, error) {
b.logger.Debug("getting latest context")

if b.latestCtx.Context() != nil {
return b.latestCtx, nil
}

return b.newLatestContext()
}

// newLatestContext retrieves the most recent query context from the application.
// This provides access to the current blockchain state for transaction validation and execution.
func (b Blockchain) GetLatestCtx() (sdk.Context, error) {
func (b Blockchain) newLatestContext() (sdk.Context, error) {
b.logger.Debug("getting latest context")

ctx, err := b.getCtxCallback(0, false)
Expand Down
2 changes: 1 addition & 1 deletion mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func (m *ExperimentalEVMMempool) shouldRemoveFromEVMPool(tx sdk.Tx) bool {

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