交易是指由账户发起、会改变区块链状态的操作。交易会被广播到网络中,由验证者对其进行验证、执行,并传播由此产生的状态变更。 交易会消耗计算资源(gas),因此需要支付相应费用。此外,交易必须使用发送方的私钥签名,以证明其真实性。

交易生命周期

交易生命周期通过 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 字段
  • 合约执行:与已部署的智能合约交互
Ethereum 交易字段:
  • recipient, signature, nonce, value
  • data:用于合约部署或调用的任意数据
  • gasLimit, maxPriorityFeePerGas, maxFeePerGas
参考:Ethereum 交易

支持的 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 接口,从而支持:

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
小费越高,优先级越高,与交易类型无关。 Nonce 处理:
  • 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, blockNumber
  • from, to, cumulativeGasUsed, effectiveGasPrice, gasUsed
  • contractAddress, logs, logsBloom
  • type, status

IBC 集成

Cosmos EVM 通过 IBC 提供跨链能力,且可从 EVM 智能合约中访问:
  • ICS20 预编译:用于跨链代币转移的直接接口
  • Cosmos SDK 模块访问:智能合约可通过预编译合约与 bank、staking、distribution 和 governance 模块交互

相关文档


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 PendingFilter with configurable MinTip, 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 from MsgEthereumTx (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 and sdk.Msgs that trigger state changes through Protobuf Msg services. Cosmos transactions can have multiple sdk.Msgs and include:
  • Msgs: Array of messages
  • GasLimit: Gas calculation option
  • FeeAmount: Maximum fee willing to pay
  • TimeoutHeight: Block height validity
  • Signatures: Array of signer signatures
  • Memo: 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 to address, contract code in data field
  • Contract execution: Interact with deployed smart contract
Ethereum transaction fields:
  • recipient, signature, nonce, value
  • data: Arbitrary data for contract deployment/calls
  • gasLimit, maxPriorityFeePerGas, maxFeePerGas
Reference: Ethereum transactions

Supported Ethereum Transaction Types

Cosmos EVM supports types defined in AcceptedTxType (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 in MsgEthereumTx (x/vm/types/tx.pb.go:36-43):
  • From: Ethereum signer address bytes for signature verification
  • Raw: Complete Ethereum transaction data
This wrapper implements both sdk.Msg and sdk.Tx interfaces, enabling:

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
Access Control Hooks (x/vm/keeper/state_transition.go:67-78):
  • Restrict contract creation and execution via EVM opcode interceptors
  • Policy-based permissions for CREATE, CREATE2, and CALL operations
Receipt Generation (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_cap or min(gas_tip_cap, gas_fee_cap - base_fee)
  • Cosmos: (fee_amount / gas_limit) - base_fee
Higher tips = higher priority, regardless of transaction type. Nonce Handling:
  • 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)
Receipt fields include:
  • transactionHash, transactionIndex, blockHash, blockNumber
  • from, to, cumulativeGasUsed, effectiveGasPrice, gasUsed
  • contractAddress, logs, logsBloom
  • type, 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