|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "path/filepath" |
| 6 | + |
| 7 | + "github.com/holiman/uint256" |
| 8 | + "github.com/spf13/cast" |
| 9 | + |
| 10 | + srvflags "github.com/cosmos/evm/server/flags" |
| 11 | + |
| 12 | + "cosmossdk.io/log" |
| 13 | + |
| 14 | + "github.com/cosmos/cosmos-sdk/client/flags" |
| 15 | + sdkserver "github.com/cosmos/cosmos-sdk/server" |
| 16 | + servertypes "github.com/cosmos/cosmos-sdk/server/types" |
| 17 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 18 | + genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" |
| 19 | +) |
| 20 | + |
| 21 | +// GetBlockGasLimit reads the genesis json file using AppGenesisFromFile |
| 22 | +// to extract the consensus block gas limit before InitChain is called. |
| 23 | +func GetBlockGasLimit(appOpts servertypes.AppOptions, logger log.Logger) uint64 { |
| 24 | + homeDir := cast.ToString(appOpts.Get(flags.FlagHome)) |
| 25 | + if homeDir == "" { |
| 26 | + logger.Error("home directory not found in app options, using zero block gas limit") |
| 27 | + return math.MaxUint64 |
| 28 | + } |
| 29 | + genesisPath := filepath.Join(homeDir, "config", "genesis.json") |
| 30 | + |
| 31 | + appGenesis, err := genutiltypes.AppGenesisFromFile(genesisPath) |
| 32 | + if err != nil { |
| 33 | + logger.Error("failed to load genesis using SDK AppGenesisFromFile, using zero block gas limit", "path", genesisPath, "error", err) |
| 34 | + return 0 |
| 35 | + } |
| 36 | + genDoc, err := appGenesis.ToGenesisDoc() |
| 37 | + if err != nil { |
| 38 | + logger.Error("failed to convert AppGenesis to GenesisDoc, using zero block gas limit", "path", genesisPath, "error", err) |
| 39 | + return 0 |
| 40 | + } |
| 41 | + |
| 42 | + if genDoc.ConsensusParams == nil { |
| 43 | + logger.Error("consensus parameters not found in genesis (nil), using zero block gas limit") |
| 44 | + return 0 |
| 45 | + } |
| 46 | + |
| 47 | + maxGas := genDoc.ConsensusParams.Block.MaxGas |
| 48 | + if maxGas == -1 { |
| 49 | + logger.Warn("genesis max_gas is unlimited (-1), using max uint64") |
| 50 | + return math.MaxUint64 |
| 51 | + } |
| 52 | + if maxGas < -1 { |
| 53 | + logger.Error("invalid max_gas value in genesis, using zero block gas limit") |
| 54 | + return 0 |
| 55 | + } |
| 56 | + blockGasLimit := uint64(maxGas) // #nosec G115 -- maxGas >= 0 checked above |
| 57 | + |
| 58 | + logger.Debug( |
| 59 | + "extracted block gas limit from genesis using SDK AppGenesisFromFile", |
| 60 | + "genesis_path", genesisPath, |
| 61 | + "max_gas", maxGas, |
| 62 | + "block_gas_limit", blockGasLimit, |
| 63 | + ) |
| 64 | + |
| 65 | + return blockGasLimit |
| 66 | +} |
| 67 | + |
| 68 | +// GetMinGasPrices reads the min gas prices from the app options, set from app.toml |
| 69 | +// This is currently not used, but is kept in case this is useful for the mempool, |
| 70 | +// in addition to the min tip flag |
| 71 | +func GetMinGasPrices(appOpts servertypes.AppOptions, logger log.Logger) sdk.DecCoins { |
| 72 | + minGasPricesStr := cast.ToString(appOpts.Get(sdkserver.FlagMinGasPrices)) |
| 73 | + minGasPrices, err := sdk.ParseDecCoins(minGasPricesStr) |
| 74 | + if err != nil { |
| 75 | + logger.With("error", err).Info("failed to parse min gas prices, using empty DecCoins") |
| 76 | + minGasPrices = sdk.DecCoins{} |
| 77 | + } |
| 78 | + |
| 79 | + return minGasPrices |
| 80 | +} |
| 81 | + |
| 82 | +// GetMinTip reads the min tip from the app options, set from app.toml |
| 83 | +// This field is also known as the minimum priority fee |
| 84 | +func GetMinTip(appOpts servertypes.AppOptions, logger log.Logger) *uint256.Int { |
| 85 | + minTipUint64 := cast.ToUint64(appOpts.Get(srvflags.EVMMinTip)) |
| 86 | + minTip := uint256.NewInt(minTipUint64) |
| 87 | + |
| 88 | + if minTip.Cmp(uint256.NewInt(0)) >= 0 { // zero or positive |
| 89 | + return minTip |
| 90 | + } |
| 91 | + |
| 92 | + logger.Error("invalid min tip value in app.toml or flag, falling back to nil", "min_tip", minTipUint64) |
| 93 | + return nil |
| 94 | +} |
0 commit comments