diff --git a/jsonrpc/backend/backend.go b/jsonrpc/backend/backend.go index d7245881..ae2f91e2 100644 --- a/jsonrpc/backend/backend.go +++ b/jsonrpc/backend/backend.go @@ -12,6 +12,7 @@ import ( lrucache "github.com/hashicorp/golang-lru/v2" "cosmossdk.io/log" + "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/server" @@ -52,7 +53,8 @@ type JSONRPCBackend struct { svrCtx *server.Context clientCtx client.Context - cfg config.JSONRPCConfig + cfg config.JSONRPCConfig + gasMultiplier math.LegacyDec } type txQueueItem struct { @@ -84,6 +86,14 @@ func NewJSONRPCBackend( if cfg.LogCacheSize == 0 { cfg.LogCacheSize = config.DefaultLogCacheSize } + if cfg.GasMultiplier == "" { + cfg.GasMultiplier = config.DefaultGasMultiplier + } + + gasMultiplier, err := math.LegacyNewDecFromStr(cfg.GasMultiplier) + if err != nil { + return nil, err + } queuedTxHashes := new(sync.Map) queuedTxs, err := lrucache.NewWithEvict(cfg.QueuedTransactionCap, func(_ string, txCache txQueueItem) { @@ -118,7 +128,9 @@ func NewJSONRPCBackend( ctx: ctx, svrCtx: svrCtx, clientCtx: clientCtx, - cfg: cfg, + + cfg: cfg, + gasMultiplier: gasMultiplier, } // start fee fetcher diff --git a/jsonrpc/backend/gas.go b/jsonrpc/backend/gas.go index a0700cce..4eaffce3 100644 --- a/jsonrpc/backend/gas.go +++ b/jsonrpc/backend/gas.go @@ -90,7 +90,9 @@ func (b *JSONRPCBackend) EstimateGas(args rpctypes.TransactionArgs, blockNrOrHas return hexutil.Uint64(0), err } - return hexutil.Uint64(gasInfo.GasUsed), nil + // apply gas multiplier + gasUsed := b.gasMultiplier.MulInt(math.NewIntFromUint64(gasInfo.GasUsed)).TruncateInt().Uint64() + return hexutil.Uint64(gasUsed), nil } func (b *JSONRPCBackend) GasPrice() (*hexutil.Big, error) { diff --git a/jsonrpc/backend/gas_test.go b/jsonrpc/backend/gas_test.go index 1ee496c3..306725ba 100644 --- a/jsonrpc/backend/gas_test.go +++ b/jsonrpc/backend/gas_test.go @@ -84,6 +84,5 @@ func Test_EstimateGas(t *testing.T) { Nonce: nil, }, nil, nil) require.NoError(t, err) - require.Greater(t, gasEstimated, uint64(10_000)) - require.Less(t, uint64(gasEstimated), uint64(finalizeRes.TxResults[1].GasUsed)) + require.Greater(t, uint64(gasEstimated), uint64(finalizeRes.TxResults[1].GasUsed)) } diff --git a/jsonrpc/config/config.go b/jsonrpc/config/config.go index ed9f2543..a91796eb 100644 --- a/jsonrpc/config/config.go +++ b/jsonrpc/config/config.go @@ -42,6 +42,8 @@ const ( DefaultFilterTimeout = 5 * time.Minute // DefaultLogCacheSize is the maximum number of cached blocks. DefaultLogCacheSize = 32 + // DefaultGasMultiplier is the default gas multiplier for the EVM state transition. + DefaultGasMultiplier = "1.4" ) var ( @@ -67,6 +69,7 @@ const ( flagJSONRPCFeeHistoryMaxBlocks = "json-rpc.fee-history-max-blocks" flagJSONRPCFilterTimeout = "json-rpc.filter-timeout" flagJSONRPCLogCacheSize = "json-rpc.log-cache-size" + flagJSONRPCGasMultiplier = "json-rpc.gas-multiplier" ) // JSONRPCConfig defines configuration for the EVM RPC server. @@ -104,6 +107,8 @@ type JSONRPCConfig struct { FilterTimeout time.Duration `mapstructure:"filter-timeout"` // LogCacheSize is the maximum number of cached blocks. LogCacheSize int `mapstructure:"log-cache-size"` + // GasMultiplier is the gas multiplier for the EVM state transition. + GasMultiplier string `mapstructure:"gas-multiplier"` } // DefaultJSONRPCConfig returns a default configuration for the EVM RPC server. @@ -132,6 +137,8 @@ func DefaultJSONRPCConfig() JSONRPCConfig { FilterTimeout: DefaultFilterTimeout, LogCacheSize: DefaultLogCacheSize, + + GasMultiplier: DefaultGasMultiplier, } } @@ -153,6 +160,7 @@ func AddConfigFlags(startCmd *cobra.Command) { startCmd.Flags().Int(flagJSONRPCFeeHistoryMaxBlocks, DefaultFeeHistoryMaxBlocks, "Maximum number of blocks used to lookup the fee history") startCmd.Flags().Duration(flagJSONRPCFilterTimeout, DefaultFilterTimeout, "Duration how long filters stay active") startCmd.Flags().Int(flagJSONRPCLogCacheSize, DefaultLogCacheSize, "Maximum number of cached blocks for the log filter") + startCmd.Flags().String(flagJSONRPCGasMultiplier, DefaultGasMultiplier, "Gas multiplier for the EVM state transition") } // GetConfig load config values from the app options @@ -174,6 +182,7 @@ func GetConfig(appOpts servertypes.AppOptions) JSONRPCConfig { FeeHistoryMaxBlocks: cast.ToInt(appOpts.Get(flagJSONRPCFeeHistoryMaxBlocks)), FilterTimeout: cast.ToDuration(appOpts.Get(flagJSONRPCFilterTimeout)), LogCacheSize: cast.ToInt(appOpts.Get(flagJSONRPCLogCacheSize)), + GasMultiplier: cast.ToString(appOpts.Get(flagJSONRPCGasMultiplier)), } } @@ -235,4 +244,7 @@ filter-timeout = "{{ .JSONRPCConfig.FilterTimeout }}" # LogCacheSize is the maximum number of cached blocks for the log filter. log-cache-size = {{ .JSONRPCConfig.LogCacheSize }} + +# GasMultiplier is the gas multiplier for the EVM state transition. +gas-multiplier = "{{ .JSONRPCConfig.GasMultiplier }}" `