Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce gas multiplier on jsonrpc endpoint #128

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
svrCtx *server.Context
clientCtx client.Context

cfg config.JSONRPCConfig
cfg config.JSONRPCConfig
gasMultiplier math.LegacyDec
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
}

type txQueueItem struct {
Expand Down Expand Up @@ -84,6 +86,14 @@
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(cfg.GasMultiplier)
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 @@
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
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
}

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))
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@
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 @@
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 @@
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 @@
FilterTimeout: DefaultFilterTimeout,

LogCacheSize: DefaultLogCacheSize,

GasMultiplier: DefaultGasMultiplier,
}
}

Expand All @@ -153,6 +160,7 @@
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 @@
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
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -235,4 +244,7 @@

# 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 }}"
`
Loading