Gas 与费用基础
Gas 是衡量交易计算强度的单位,也就是执行交易所需的工作量。复杂的多步骤交易比简单转账需要更多 gas。 费用 是为 gas 支付的成本:费用处理
Cosmos SDK 费用
用户需要指定两个字段:GasLimit:执行 gas 的上限(GasWanted)Fees或GasPrice二选一,用于指定或计算交易费用
GasLimit 不足,交易会失败并回滚,且不会退还费用。
验证者会指定自己的 min-gas-prices 用于交易筛选。费用不足的交易可能会被延迟处理,或者直接失败。
以太坊费用(EIP-1559)
引入 EIP-1559(London Hard Fork)后,GasPrice 被拆分为两个部分:
- BaseFee:根据区块大小自动计算得出(在以太坊上会被销毁,在 Cosmos EVM 上会被分配)
- PriorityFee:支付给提议者的小费,用于激励其打包交易
max_fee_per_gas:GasPrice的总上限max_priority_fee_per_gas:PriorityFee的最大值gas_limit:gas 消耗上限
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%),以尽量贴近以太坊的行为。该比例可以调整。区块处理时间线
- EndBlock:FeeMarket 模块跟踪区块交易的总
TransientGasWanted,用于下一块的BaseFee - 交易接收:节点接收并传播交易,并按费用价格进行优先级排序
- BeginBlock:
- FeeMarket 根据前一个区块的
GasWanted计算BaseFee(代码) - Distribution 模块分配前一个区块的费用奖励
- FeeMarket 根据前一个区块的
- 交易处理:
AnteHandler验证交易,检查费用是否高于最小值和BaseFee,并将费用扣除到fee_collector模块- 对于 Cosmos 交易:执行并消耗 gas
- 对于以太坊交易:在 gas 隔离环境下执行,计算已用 gas,并退还剩余部分
- EndBlock:保存当前区块的
GasWanted,供下一块使用
Gas 机制
Cosmos Gas 计量
GasMeter:跟踪状态转换过程中消耗的 gas(每笔交易都会重置)BlockGasMeter:跟踪区块中消耗的 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 作为报酬(类似中介兑换行为)
相关文档
- EIP-1559 费用市场 - 费用市场机制详解
- 性能优化 - Gas 估算优化
- x/feemarket 模块 - 费用市场实现
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:Fee Handling
Cosmos SDK Fees
Users specify two fields:GasLimit- upper bound on execution gas (GasWanted)- One of
FeesorGasPriceto specify or calculate transaction fees
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:
- BaseFee: Calculated automatically based on block size (burned on Ethereum, distributed on Cosmos EVM)
- PriorityFee: Tip to proposer for transaction inclusion
max_fee_per_gas: TotalGasPricemaximummax_priority_fee_per_gas: MaximumPriorityFeegas_limit: Gas consumption limit
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-pricesand applies EIP-1559 fee logic - Gas price must exceed both global
min-gas-priceand block’sBaseFee
BaseFeeis distributed to validators/delegators (not burned)BaseFeeis lower-bounded by globalmin-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
- EndBlock: FeeMarket module tracks total
TransientGasWantedfrom block transactions for next block’sBaseFee - Transaction Reception: Nodes receive and gossip transactions, prioritized by fee price
- BeginBlock:
- FeeMarket calculates
BaseFee(code) using previous block’sGasWanted - Distribution module distributes previous block’s fee rewards
- FeeMarket calculates
- Transaction Processing:
AnteHandlervalidates transaction, verifies fees > minimum values andBaseFee, deducts fees tofee_collectormodule- For Cosmos transactions: Execute and consume gas
- For Ethereum transactions: Execute with gas isolation, calculate gas used, refund surplus
- EndBlock: Store block’s
GasWantedfor 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)
Matching EVM Gas Consumption
The EVM uses a gas table for each OPCODE, whereas Cosmos uses aGasConfig 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
GasMeterto 0 and manually sets it togasUsedvalue from EVM execution
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’seth_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
x/vm/keeper/grpc_query.go
For Cosmos transactions, use transaction simulation.
Zero-Fee Transactions
In Cosmos, minimum gas price is not enforced byAnteHandler - 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 SDKTips 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)
Related Documentation
- EIP-1559 Fee Market - Detailed fee market mechanics
- Performance Optimization - Gas estimation optimizations
- x/feemarket Module - Fee market implementation