feat(evm): add native precompile runtime base#336
Merged
phenix3443 merged 1 commit intoJul 14, 2026
Conversation
Add x/evm/precompiles/base, a thin wrapper over cosmos/evm's precompiles/common native runtime, so moca precompiles can stop hand-rolling GetCacheContext/Snapshot/RevertToSnapshot/commit templates and share one snapshot, gas-metering and balance-sync implementation. base.Precompile embeds cosmos/evm's common.Precompile and the ABI, and: - RunPrecompile rejects native value up front (moca precompiles are not payable) then delegates to RunNativeAction (official snapshot/gas/balance model, error->revert) - Dispatch is a pass-through to SetupABI (method dispatch + read-only write protection + arg unpacking) - WithBalanceHandler opt-in wires cosmos/evm's bank-event -> StateDB sync Helper unit tests cover constructor wiring and dispatch/read-only behavior. No existing precompile is migrated in this change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Subtask 1 of the precompile → cosmos/evm native-mode migration: add
x/evm/precompiles/base, a thin shared runtime skeleton over cosmos/evm'sprecompiles/common. This lets moca precompiles stop hand-rollingGetCacheContext / Snapshot / RevertToSnapshot / committemplates (currently duplicated across all 11) and share one snapshot / gas-metering / balance-sync implementation.API (
base.Precompile)Embeds cosmos/evm's
common.Precompile+ the precompile ABI:RunPrecompile(evm, contract, readonly, exec)— rejects native value up front (moca precompiles are not payable), then delegates toRunNativeAction(the official snapshot / gas / balance-handler model, which converts action errors into EVM reverts).Dispatch(contract, readonly, isTx)— pass-through toSetupABI: method dispatch by ID, read-only write protection, argument unpacking.WithBalanceHandler(bankKeeper)— opt-inBalanceHandlerFactoryfor state-changing precompiles that move native balances.Intended per-precompile usage:
Design notes
Execute(ctx, stateDB, contract, readonly), but moca handlers emit logs viaevm(evm.StateDB.AddLog), matching cosmos/evm's own precompiles which pass the EVM, not the StateDB. SoExecuteFntakesevm(StateDB is reachable viaevm.StateDB).KvGasConfig(like cosmos/evm's own precompiles); moca keeps charging a flat per-method cost via each precompile'sRequiredGas, so the base must not double-charge store gas.Tests
Helper-level unit tests (no EVM needed): constructor wiring (address / empty gas / balance-handler opt-in) and
Dispatchbehavior (routes tx/query methods, unpacks args, rejects tx methods in read-only calls). The full runtime path (RunPrecompile→RunNativeAction) is exercised end-to-end by the bank migration (subtask 2), which runs throughEvmKeeper.CallEVMWithData.go test ./x/evm/precompiles/base -count=1→ ok.precompile-integration.