gcx/blockchain/genex-chain/x/evm/keeper/gas.go

118 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package keeper — EVM Gas 费覆盖逻辑
//
// Genex Chain 的 Gas 策略:
// - 当前阶段平台全额补贴min_gas_price = 0
// - Gas 参数可通过 Governance 合约动态调整(无需硬分叉)
// - 后期可开启 EIP-1559 费用市场或固定 Gas 费
package keeper
import (
"math/big"
)
// GasConfig 定义 Gas 费配置
type GasConfig struct {
// MinGasPrice 最低 Gas 价格(当前为 0平台补贴
MinGasPrice *big.Int `json:"min_gas_price"`
// MaxGasPerBlock 每区块最大 Gas
MaxGasPerBlock uint64 `json:"max_gas_per_block"`
// EnableEIP1559 是否启用 EIP-1559 动态费用
EnableEIP1559 bool `json:"enable_eip1559"`
// BaseFeeChangeDenominator EIP-1559 基础费变化分母
BaseFeeChangeDenominator uint64 `json:"base_fee_change_denominator"`
// ElasticityMultiplier 弹性乘数
ElasticityMultiplier uint64 `json:"elasticity_multiplier"`
}
// DefaultGasConfig 返回默认 Gas 配置(平台全额补贴)
func DefaultGasConfig() GasConfig {
return GasConfig{
MinGasPrice: big.NewInt(0), // 免费 Gas
MaxGasPerBlock: 100_000_000, // 1 亿 Gas 上限
EnableEIP1559: false, // 当前不启用 EIP-1559
BaseFeeChangeDenominator: 8,
ElasticityMultiplier: 2,
}
}
// EVMKeeper 管理 EVM 模块状态,包含 Gas 费逻辑
type EVMKeeper struct {
gasConfig GasConfig
}
// NewEVMKeeper 创建 EVMKeeper 实例
func NewEVMKeeper(config GasConfig) *EVMKeeper {
return &EVMKeeper{
gasConfig: config,
}
}
// GetBaseFee 返回当前基础 Gas 费
// 当前阶段:返回 0平台全额补贴用户无需支付 Gas
// 后期可通过 Governance 合约调整 MinGasPrice 参数开启收费
func (k *EVMKeeper) GetBaseFee() *big.Int {
if k.gasConfig.MinGasPrice == nil || k.gasConfig.MinGasPrice.Sign() == 0 {
return big.NewInt(0) // 免费 Gas
}
if k.gasConfig.EnableEIP1559 {
return k.calculateEIP1559BaseFee()
}
return new(big.Int).Set(k.gasConfig.MinGasPrice)
}
// GetMinGasPrice 获取最低 Gas 价格
func (k *EVMKeeper) GetMinGasPrice() *big.Int {
if k.gasConfig.MinGasPrice == nil {
return big.NewInt(0)
}
return new(big.Int).Set(k.gasConfig.MinGasPrice)
}
// SetMinGasPrice 设置最低 Gas 价格(由 Governance 合约通过提案调用)
func (k *EVMKeeper) SetMinGasPrice(price *big.Int) {
k.gasConfig.MinGasPrice = new(big.Int).Set(price)
}
// IsGasFree 检查当前是否为免费 Gas 模式
func (k *EVMKeeper) IsGasFree() bool {
return k.gasConfig.MinGasPrice == nil || k.gasConfig.MinGasPrice.Sign() == 0
}
// GetMaxGasPerBlock 获取每区块最大 Gas 限制
func (k *EVMKeeper) GetMaxGasPerBlock() uint64 {
return k.gasConfig.MaxGasPerBlock
}
// SetMaxGasPerBlock 设置每区块最大 Gas由 Governance 调整)
func (k *EVMKeeper) SetMaxGasPerBlock(maxGas uint64) {
k.gasConfig.MaxGasPerBlock = maxGas
}
// EnableEIP1559 启用/禁用 EIP-1559由 Governance 控制)
func (k *EVMKeeper) EnableEIP1559(enable bool) {
k.gasConfig.EnableEIP1559 = enable
}
// calculateEIP1559BaseFee 计算 EIP-1559 动态基础费
// 预留接口,当前阶段不启用
func (k *EVMKeeper) calculateEIP1559BaseFee() *big.Int {
// EIP-1559 基础费计算逻辑
// 生产环境中应根据上一区块的 Gas 使用率动态调整
// 当前返回最低价格
return new(big.Int).Set(k.gasConfig.MinGasPrice)
}
// GetGasConfig 获取当前 Gas 配置(用于 API 查询)
func (k *EVMKeeper) GetGasConfig() GasConfig {
return k.gasConfig
}
// EstimateGasCost 估算交易 Gas 费用
// 当前阶段始终返回 0平台补贴
func (k *EVMKeeper) EstimateGasCost(gasUsed uint64) *big.Int {
baseFee := k.GetBaseFee()
return new(big.Int).Mul(baseFee, new(big.Int).SetUint64(gasUsed))
}