Cosmos EVM 实现了与 Cosmos SDK 兼容的以太坊式费用计算。本文概述了 gas 的计算方式、费用的提供方式,以及费用市场(EIP-1559)如何对交易进行优先级排序。 参考资料:Cosmos SDK Gas,Ethereum Gas

Gas 与费用基础

Gas 是衡量交易计算强度的单位,也就是执行交易所需的工作量。复杂的多步骤交易比简单转账需要更多 gas。 费用 是为 gas 支付的成本:
总费用 = Gas × Gas Price

费用处理

Cosmos SDK 费用

用户需要指定两个字段:
  1. GasLimit:执行 gas 的上限(GasWanted)
  2. Fees 或 GasPrice 二选一,用于指定或计算交易费用
节点会先完整扣除费用,然后再执行交易。如果 GasLimit 不足,交易会失败并回滚,且不会退还费用。 验证者会指定自己的 min-gas-prices 用于交易筛选。费用不足的交易可能会被延迟处理,或者直接失败。

以太坊费用(EIP-1559)

引入 EIP-1559(London Hard Fork)后,GasPrice 被拆分为两个部分:
Gas Price = Base Fee + Priority Fee
  • BaseFee:根据区块大小自动计算得出(在以太坊上会被销毁,在 Cosmos EVM 上会被分配)
  • PriorityFee:支付给提议者的小费,用于激励其打包交易
用户需要指定:
  • max_fee_per_gas:GasPrice 的总上限
  • max_priority_fee_per_gas:PriorityFee 的最大值
  • gas_limit:gas 消耗上限
执行交易未使用到的多余 gas 会被退还。 参考:EIP-1559

Cosmos EVM 实现

从根本上说,Cosmos EVM 是一条 Cosmos SDK 链,通过模块提供 EVM 兼容性。所有 EVM 交易都会被编码为 Cosmos SDK 交易,并更新由 Cosmos SDK 管理的状态。

Fee Market 模块

在 EVM 层支持 EIP-1559 的 gas 计算:
  • 跟踪每个区块中提供的 gas,用于计算后续交易的 base fee
  • 对于 EVM 交易,绕过本地 min-gas-prices,改为应用 EIP-1559 费用逻辑
  • gas price 必须同时高于全局 min-gas-price 和当前区块的 BaseFee
与以太坊的关键差异:
  • BaseFee 会分配给验证者和委托人,而不是被销毁
  • BaseFee 受全局 min-gas-price 的下限约束(当前为 0,可通过治理调整)

EVM Gas 退款

Cosmos EVM 会为 EVM 交易退还一部分未使用的 gas(默认至少 50%),以尽量贴近以太坊的行为。该比例可以调整。

区块处理时间线

  1. EndBlock:FeeMarket 模块跟踪区块交易的总 TransientGasWanted,用于下一块的 BaseFee
  2. 交易接收:节点接收并传播交易,并按费用价格进行优先级排序
  3. BeginBlock:
    • FeeMarket 根据前一个区块的 GasWanted 计算 BaseFee(代码)
    • Distribution 模块分配前一个区块的费用奖励
  4. 交易处理:
    • AnteHandler 验证交易,检查费用是否高于最小值和 BaseFee,并将费用扣除到 fee_collector 模块
    • 对于 Cosmos 交易:执行并消耗 gas
    • 对于以太坊交易:在 gas 隔离环境下执行,计算已用 gas,并退还剩余部分
  5. EndBlock:保存当前区块的 GasWanted,供下一块使用

Gas 机制

Cosmos Gas 计量

  • GasMeter:跟踪状态转换过程中消耗的 gas(每笔交易都会重置)
  • BlockGasMeter:跟踪区块中消耗的 gas,并强制执行预定义限制(通过治理设置)
Gas 会按字节计价。由于 Cosmos 使用 Big.Int 类型(动态大小),较大的参数值会比较小的参数值消耗更多 gas。

匹配 EVM 的 Gas 消耗

EVM 会为每个 OPCODE 使用一张 gas table,而 Cosmos 则使用 GasConfig,为数据库操作定义固定成本和按字节计算的成本。 为了匹配 EVM 的 gas 消耗,Cosmos EVM 会:
  • 忽略 SDK 的 gas 消耗逻辑
  • 通过 gas limit - EVM 剩余 gas - refund 计算已消耗 gas
  • 将交易 GasMeter 重置为 0,并手动设置为 EVM 执行得到的 gasUsed 值
实现:x/vm/keeper/state_transition.go

AnteHandler

在交易执行前执行基础检查:
  • 签名校验、交易字段校验、交易费用检查
  • 验证用户余额是否足以支付交易成本(金额 + 费用)
  • 检查 gas limit 是否大于等于计算得到的 intrinsic gas

Gas 估算

以太坊的 eth_estimateGas 通过 EstimateGas 查询 API 实现。它会:
  • 基于当前区块和状态执行交易
  • 通过二分查找找到最优 gas 值
  • 使用缓存上下文避免持久化状态变更
实现:x/vm/keeper/grpc_query.go 对于 Cosmos 交易,请使用交易模拟。

零费用交易

在 Cosmos 中,最小 gas price 并不会由 AnteHandler 强制执行,而是由验证者各自指定自己的 min-gas-prices。这意味着,如果验证者接受,非 EVM 交易可以使用 0 费用。 EVM 交易不能为 0 费用,因为 EVM 天然要求 gas(在 ValidateBasic 和自定义 AnteHandler 中验证)。

跨链 Gas 与费用

在跨链场景下(例如,从链 A 向 Cosmos EVM 发起 IBC 转账,但用户没有原生代币),Cosmos SDK 的 Tips 允许用户:
  • 签署一笔带 tip 且不带 fees 的交易
  • 将其发送给费用中继者,由后者使用原生货币代付费用
  • 由中继者收取 tip 作为报酬(类似中介兑换行为)

相关文档


Cosmos EVM implements Ethereum-type fee calculation compatible with the Cosmos SDK. This overview explains gas calculation, fee provision, and how the fee market (EIP-1559) prioritizes transactions. References: Cosmos SDK Gas, Ethereum Gas

Gas and Fees Fundamentals

Gas is a unit measuring computational intensity of a transaction - the work required to execute it. Complex, multi-step transactions require more gas than simple transfers. Fees are the cost paid for gas:
Total Fees = Gas × Gas Price

Fee Handling

Cosmos SDK Fees

Users specify two fields:
  1. GasLimit - upper bound on execution gas (GasWanted)
  2. One of Fees or GasPrice to specify or calculate transaction fees
The node consumes fees entirely, then executes the transaction. If GasLimit is insufficient, the transaction fails and rolls back without refunding fees. Validators specify their min-gas-prices for transaction selection. Transactions with insufficient fees encounter delays or fail.

Ethereum Fees (EIP-1559)

With EIP-1559 (London Hard Fork), GasPrice split into two components:
Gas Price = Base Fee + Priority Fee
  • BaseFee: Calculated automatically based on block size (burned on Ethereum, distributed on Cosmos EVM)
  • PriorityFee: Tip to proposer for transaction inclusion
Users specify:
  • max_fee_per_gas: Total GasPrice maximum
  • max_priority_fee_per_gas: Maximum PriorityFee
  • gas_limit: Gas consumption limit
Surplus gas not required for execution is refunded. Reference: EIP-1559

Cosmos EVM Implementation

Fundamentally, Cosmos EVM is a Cosmos SDK chain enabling EVM compatibility via a module. All EVM transactions are encoded as Cosmos SDK transactions and update Cosmos SDK-managed state.

Fee Market Module

Supports EIP-1559 gas calculation on the EVM layer:
  • Tracks gas supplied per block to calculate base fee for future transactions
  • For EVM transactions, bypasses local min-gas-prices and applies EIP-1559 fee logic
  • Gas price must exceed both global min-gas-price and block’s BaseFee
Key differences from Ethereum:
  • BaseFee is distributed to validators/delegators (not burned)
  • BaseFee is lower-bounded by global min-gas-price (currently zero, adjustable via governance)

EVM Gas Refunds

Cosmos EVM refunds a fraction (at least 50% by default) of unused gas for EVM transactions to approximate Ethereum behavior. This is adjustable.

Block Processing Timeline

  1. EndBlock: FeeMarket module tracks total TransientGasWanted from block transactions for next block’s BaseFee
  2. Transaction Reception: Nodes receive and gossip transactions, prioritized by fee price
  3. BeginBlock:
    • FeeMarket calculates BaseFee (code) using previous block’s GasWanted
    • Distribution module distributes previous block’s fee rewards
  4. Transaction Processing:
    • AnteHandler validates transaction, verifies fees > minimum values and BaseFee, deducts fees to fee_collector module
    • For Cosmos transactions: Execute and consume gas
    • For Ethereum transactions: Execute with gas isolation, calculate gas used, refund surplus
  5. EndBlock: Store block’s GasWanted for next block

Gas Mechanics

Cosmos Gas Metering

  • GasMeter: Tracks gas consumed during state transitions (reset per transaction)
  • BlockGasMeter: Tracks gas consumed in block, enforces predefined limit (set via governance)
Gas is priced per-byte. Since Cosmos uses Big.Int types (dynamically sized), larger parameter values are more gas-intensive than smaller ones.

Matching EVM Gas Consumption

The EVM uses a gas table for each OPCODE, whereas Cosmos uses a GasConfig with flat and per-byte costs for database operations. To match EVM gas consumption, Cosmos EVM:
  • Ignores SDK gas consumption logic
  • Calculates gas consumed by subtracting EVM leftover gas + refund from gas limit
  • Resets transaction GasMeter to 0 and manually sets it to gasUsed value from EVM execution
Implementation: x/vm/keeper/state_transition.go

AnteHandler

Performs basic checks before transaction execution:
  • Signature verification, transaction field validation, transaction fees
  • Verifies user has sufficient balance for tx cost (amount + fees)
  • Checks gas limit >= computed intrinsic gas

Gas Estimation

Ethereum’s eth_estimateGas is implemented via EstimateGas query API. It:
  • Applies transaction against current block/state
  • Performs binary search to find optimal gas value
  • Uses cache context to avoid persisting state changes
Implementation: x/vm/keeper/grpc_query.go For Cosmos transactions, use transaction simulation.

Zero-Fee Transactions

In Cosmos, minimum gas price is not enforced by AnteHandler - validators specify their own min-gas-prices. This allows 0-fee transactions for non-EVM transactions if validators accept them. EVM transactions cannot have 0 fees as gas is inherently required by the EVM (validated in ValidateBasic and custom AnteHandler).

Cross-Chain Gas and Fees

For cross-chain scenarios (e.g., IBC transfer from Chain A to Cosmos EVM without native tokens), Cosmos SDK Tips allow users to:
  • Sign transaction with tip and no fees
  • Send to fee relayer who covers fees in native currency
  • Receive tip in payment (intermediary exchange behavior)