交易生命周期
交易生命周期通过 CometBFT 共识经历一个双阶段流程:CheckTx 阶段
- 交易路由:Ante handler 识别交易类型(
ante/ante.go:18-71) - EVM 验证:MonoDecorator 使用 go-ethereum 的
txpool.ValidateTransaction()统一执行验证(ante/evm/mono_decorator.go:99+) - 交易过滤:使用
PendingFilter,支持可配置的MinTip、基础费和交易类型过滤(mempool/mempool.go:455-461) - Nonce 间隙处理:未来 nonce 的交易会通过
InsertInvalidNonce()在本地排队(mempool/check_tx.go:21-23) - 加入内存池:有效交易会被加入 CometBFT mempool 并通过 P2P 广播
DeliverTx 阶段
- 消息解包:
msg.AsTransaction()从MsgEthereumTx中提取以太坊交易(x/vm/keeper/msg_server.go:32) - 状态转换:
ApplyTransaction()在 gas 隔离的环境中执行 EVM 逻辑(x/vm/keeper/state_transition.go:165-199) - 缓存上下文:隔离执行,并具备回滚能力
- 状态提交:成功的交易会提交到 Cosmos SDK store
关于详细的交易流转与 mempool 行为,参见 Mempool 文档 和 Cosmos SDK 生命周期。
交易类型
Cosmos EVM 支持两种交易类型:1. Cosmos 交易
这类交易由上下文中保存的元数据和sdk.Msg 组成,通过 Protobuf Msg 服务 触发状态变更。
Cosmos 交易可以包含多个 sdk.Msg,并包括:
Msgs:消息数组GasLimit:gas 计算选项FeeAmount:愿意支付的最高手续费TimeoutHeight:区块高度有效期Signatures:签名者签名数组Memo:备注或说明
2. Ethereum 交易
由 EOA(外部拥有账户)发起、用于改变 EVM 状态的操作。必须广播到整个网络。 Ethereum 交易类别:- 常规交易:账户到账户
- 合约部署:没有
to地址,合约代码位于data字段 - 合约执行:与已部署的智能合约交互
recipient,signature,nonce,valuedata:用于合约部署或调用的任意数据gasLimit,maxPriorityFeePerGas,maxFeePerGas
支持的 Ethereum 交易类型
Cosmos EVM 支持AcceptedTxType 中定义的类型(ante/evm/mono_decorator.go:23-27):
- Legacy Transactions(EIP-155):链 ID 保护
- Access List Transactions(EIP-2930):预声明的存储访问
- Dynamic Fee Transactions(EIP-1559):基础费 + 优先费模型
- Set Code Transactions(EIP-7702):带授权列表的账户代码分配
注意:默认不支持未受保护的旧式交易。
MsgEthereumTx 包装器
Cosmos EVM 将 Ethereum 交易封装在MsgEthereumTx 中(x/vm/types/tx.pb.go:36-43):
From:用于签名校验的 Ethereum 签名者地址字节Raw:完整的 Ethereum 交易数据
sdk.Msg 和 sdk.Tx 接口,从而支持:
- 直接集成 go-ethereum:使用
txpool.ValidateTransaction(),而不是 SDK ante handlers - Gas 隔离:EVM 执行使用无限 gas meter,绕过 SDK 的 gas 消耗逻辑(
x/vm/keeper/state_transition.go:153-164) - 经济性校验:
CheckSenderBalance()将账户余额与交易成本进行比较(x/vm/keeper/fees.go:24-39) - 单消息约束:每笔交易只能包含一条 EVM 消息(
ante/evm/mono_decorator.go:88-91)
EVM 执行集成
Cosmos EVM 创建了一个执行环境,用于衔接 Ethereum 与 Cosmos SDK 的状态管理: 区块上下文映射(x/vm/keeper/state_transition.go:46-57):
- CometBFT 区块提议者 → EVM coinbase 地址
- 区块高度 → EVM 区块号
- 区块时间戳 → EVM timestamp 操作码
- 通过 EIP-2935 合约访问历史区块
x/vm/keeper/state_transition.go:67-78):
- 通过 EVM 操作码拦截器限制合约创建与执行
- 为
CREATE、CREATE2和CALL操作提供基于策略的权限控制
x/vm/keeper/state_transition.go:125):
- 提供兼容 Ethereum 的 bloom 过滤器用于日志过滤
- 同时输出两种交易哈希以实现跨生态兼容
- 通过
crypto.CreateAddress()生成合约地址
交易排序与优先级
交易按有效小费排序:- Ethereum:
gas_tip_cap或min(gas_tip_cap, gas_fee_cap - base_fee) - Cosmos:
(fee_amount / gas_limit) - base_fee
- Ethereum 交易:支持 nonce 间隙,可本地排队并自动提升
- Cosmos 交易:要求 nonce 严格连续
关于详细的 mempool 行为,参见 Mempool 架构。
交易回执
Cosmos EVM 生成兼容 Ethereum 的回执,并集成 Cosmos SDK 事件(x/vm/keeper/state_transition.go:125-146):
- Bloom Filter 计算:创建交易级和区块级 bloom 过滤器
- Gas 对账:对齐 EVM gas 使用量与 SDK gas meter 状态
- 双事件发出:同时包含 Ethereum 交易哈希和 CometBFT 交易哈希(
x/vm/keeper/msg_server.go:77-80)
transactionHash,transactionIndex,blockHash,blockNumberfrom,to,cumulativeGasUsed,effectiveGasPrice,gasUsedcontractAddress,logs,logsBloomtype,status
IBC 集成
Cosmos EVM 通过 IBC 提供跨链能力,且可从 EVM 智能合约中访问:- ICS20 预编译:用于跨链代币转移的直接接口
- Cosmos SDK 模块访问:智能合约可通过预编译合约与 bank、staking、distribution 和 governance 模块交互
相关文档
- Mempool - 交易流转与 mempool 架构
- Gas and Fees - 手续费计算与 gas 计量
- Accounts - 账户结构与地址格式
- x/vm Module - EVM 实现细节
A transaction refers to an action initiated by an account that changes blockchain state. Transactions are broadcasted to the network where validators validate, execute, and propagate the resulting state change. Transactions consume computational resources (gas) requiring associated fees. Additionally, transactions must be signed using the sender’s private key to prove authenticity.
Transaction Lifecycle
The transaction lifecycle involves a dual-phase process through CometBFT consensus:CheckTx Phase
- Transaction routing: Ante handler identifies transaction type (
ante/ante.go:18-71) - EVM validation: MonoDecorator consolidates validation using go-ethereum’s
txpool.ValidateTransaction()(ante/evm/mono_decorator.go:99+) - Transaction filtering: Uses
PendingFilterwith configurableMinTip, base fee, and transaction type filters (mempool/mempool.go:455-461) - Nonce gap handling: Transactions with future nonces queued locally via
InsertInvalidNonce()(mempool/check_tx.go:21-23) - Mempool addition: Valid transactions added to CometBFT mempool for P2P broadcast
DeliverTx Phase
- Message unwrapping:
msg.AsTransaction()extracts Ethereum transaction fromMsgEthereumTx(x/vm/keeper/msg_server.go:32) - State transition:
ApplyTransaction()executes EVM logic with gas isolation (x/vm/keeper/state_transition.go:165-199) - Cache contexts: Isolated execution with rollback capability
- State commitment: Successful transactions committed to Cosmos SDK store
For detailed transaction flow and mempool behavior, see Mempool documentation and Cosmos SDK lifecycle.
Transaction Types
Cosmos EVM supports two transaction types:1. Cosmos Transactions
Transactions comprised of metadata held in contexts andsdk.Msgs that trigger state changes through Protobuf Msg services.
Cosmos transactions can have multiple sdk.Msgs and include:
Msgs: Array of messagesGasLimit: Gas calculation optionFeeAmount: Maximum fee willing to payTimeoutHeight: Block height validitySignatures: Array of signer signaturesMemo: Note or comment
2. Ethereum Transactions
Actions initiated by EOAs (externally-owned accounts) that transform EVM state. Must be broadcasted to the entire network. Ethereum transaction categories:- Regular transactions: Account to account
- Contract deployment: No
toaddress, contract code indatafield - Contract execution: Interact with deployed smart contract
recipient,signature,nonce,valuedata: Arbitrary data for contract deployment/callsgasLimit,maxPriorityFeePerGas,maxFeePerGas
Supported Ethereum Transaction Types
Cosmos EVM supports types defined inAcceptedTxType (ante/evm/mono_decorator.go:23-27):
- Legacy Transactions (EIP-155): Chain ID protection
- Access List Transactions (EIP-2930): Pre-declared storage access
- Dynamic Fee Transactions (EIP-1559): Base fee + priority fee model
- Set Code Transactions (EIP-7702): Account code assignment with authorization list
Note: Unprotected legacy transactions are not supported by default.
MsgEthereumTx Wrapper
Cosmos EVM wraps Ethereum transactions inMsgEthereumTx (x/vm/types/tx.pb.go:36-43):
From: Ethereum signer address bytes for signature verificationRaw: Complete Ethereum transaction data
sdk.Msg and sdk.Tx interfaces, enabling:
- Direct go-ethereum integration: Uses
txpool.ValidateTransaction()instead of SDK ante handlers - Gas isolation: EVM execution uses infinite gas meter, bypassing SDK gas consumption (
x/vm/keeper/state_transition.go:153-164) - Economic validation:
CheckSenderBalance()compares account balance with transaction cost (x/vm/keeper/fees.go:24-39) - Single message constraint: Only one EVM message per transaction (
ante/evm/mono_decorator.go:88-91)
EVM Execution Integration
Cosmos EVM creates an execution environment bridging Ethereum and Cosmos SDK state management: Block Context Mapping (x/vm/keeper/state_transition.go:46-57):
- CometBFT block proposer → EVM coinbase address
- Block height → EVM block number
- Block timestamp → EVM timestamp opcode
- Historical block access via EIP-2935 contract
x/vm/keeper/state_transition.go:67-78):
- Restrict contract creation and execution via EVM opcode interceptors
- Policy-based permissions for
CREATE,CREATE2, andCALLoperations
x/vm/keeper/state_transition.go:125):
- Ethereum-compatible bloom filters for log filtering
- Dual transaction hash emission for cross-ecosystem compatibility
- Contract address generation via
crypto.CreateAddress()
Transaction Ordering and Prioritization
Transactions are ordered by effective tips:- Ethereum:
gas_tip_capormin(gas_tip_cap, gas_fee_cap - base_fee) - Cosmos:
(fee_amount / gas_limit) - base_fee
- Ethereum transactions: Support nonce gaps with local queuing and automatic promotion
- Cosmos transactions: Require sequential nonces
For detailed mempool behavior, see Mempool Architecture.
Transaction Receipts
Cosmos EVM generates Ethereum-compatible receipts with Cosmos SDK event integration (x/vm/keeper/state_transition.go:125-146):
- Bloom Filter Computation: Creates transaction and block-level bloom filters
- Gas Reconciliation: Reconciles EVM gas usage with SDK gas meter state
- Dual Event Emission: Contains both Ethereum transaction hash and CometBFT transaction hash (
x/vm/keeper/msg_server.go:77-80)
transactionHash,transactionIndex,blockHash,blockNumberfrom,to,cumulativeGasUsed,effectiveGasPrice,gasUsedcontractAddress,logs,logsBloomtype,status
IBC Integration
Cosmos EVM enables cross-chain functionality through IBC accessible from EVM smart contracts:- ICS20 Precompile: Direct interface for cross-chain token transfers
- Cosmos SDK Module Access: Smart contracts interact with bank, staking, distribution, and governance modules through precompiled contracts
Related Documentation
- Mempool - Transaction flow and mempool architecture
- Gas and Fees - Fee calculation and gas metering
- Accounts - Account structure and addressing
- x/vm Module - EVM implementation details