Skip to content

Commit

Permalink
introduce gas multiplier
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Dec 3, 2024
1 parent fbe8e17 commit 51572dd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
16 changes: 14 additions & 2 deletions jsonrpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -84,6 +86,14 @@ func NewJSONRPCBackend(
if cfg.LogCacheSize == 0 {
cfg.LogCacheSize = config.DefaultLogCacheSize
}
if cfg.GasMultiplier == "" {
cfg.GasMultiplier = config.DefaultGasMultiplier
}

Check warning on line 91 in jsonrpc/backend/backend.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/backend.go#L90-L91

Added lines #L90 - L91 were not covered by tests

gasMultiplier, err := math.LegacyNewDecFromStr(config.DefaultGasMultiplier)
if err != nil {
return nil, err
}

Check warning on line 96 in jsonrpc/backend/backend.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/backend.go#L95-L96

Added lines #L95 - L96 were not covered by tests

queuedTxHashes := new(sync.Map)
queuedTxs, err := lrucache.NewWithEvict(cfg.QueuedTransactionCap, func(_ string, txCache txQueueItem) {
Expand Down Expand Up @@ -118,7 +128,9 @@ func NewJSONRPCBackend(
ctx: ctx,
svrCtx: svrCtx,
clientCtx: clientCtx,
cfg: cfg,

cfg: cfg,
gasMultiplier: gasMultiplier,
}

// start fee fetcher
Expand Down
4 changes: 3 additions & 1 deletion jsonrpc/backend/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions jsonrpc/backend/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
12 changes: 12 additions & 0 deletions jsonrpc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -132,6 +137,8 @@ func DefaultJSONRPCConfig() JSONRPCConfig {
FilterTimeout: DefaultFilterTimeout,

LogCacheSize: DefaultLogCacheSize,

GasMultiplier: DefaultGasMultiplier,
}
}

Expand All @@ -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")

Check warning on line 163 in jsonrpc/config/config.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/config/config.go#L163

Added line #L163 was not covered by tests
}

// GetConfig load config values from the app options
Expand All @@ -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)),

Check warning on line 185 in jsonrpc/config/config.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/config/config.go#L185

Added line #L185 was not covered by tests
}
}

Expand Down Expand Up @@ -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 }}
`

0 comments on commit 51572dd

Please sign in to comment.