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

Conversation

beer-1
Copy link
Collaborator

@beer-1 beer-1 commented Dec 3, 2024

Description

We have removed the default gas charge for fee deduction during simulation, as we've introduced a gas-free fee payment option when using the JSON-RPC endpoint. However, since Cosmos still applies charges only when the transaction is finalize mode, we've introduced a gas multiplier for the JSON-RPC endpoint to prevent out-of-gas issues.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Introduced a gas multiplier setting for enhanced gas estimation in the JSON-RPC backend.
    • Added a command-line flag to specify the gas multiplier when starting the server.
    • Updated configuration to include a default gas multiplier for EVM transactions.
  • Bug Fixes

    • Improved error handling for gas multiplier conversion.
  • Tests

    • Modified gas estimation tests to reflect changes in gas calculation logic.

@beer-1 beer-1 self-assigned this Dec 3, 2024
@beer-1 beer-1 requested a review from a team as a code owner December 3, 2024 08:01
Copy link

coderabbitai bot commented Dec 3, 2024

Walkthrough

The changes introduce a new gasMultiplier field to the JSONRPCBackend struct, enhancing gas estimation by incorporating a multiplier derived from configuration settings. The EstimateGas method is updated to use this multiplier when calculating gas usage. Additionally, the configuration management is improved with a new GasMultiplier field in the JSONRPCConfig struct and a command-line flag for runtime configuration. Tests for the EstimateGas method are adjusted to reflect the new logic. Overall, these modifications aim to provide more flexible and accurate gas estimation for EVM transactions.

Changes

File Change Summary
jsonrpc/backend/backend.go Added gasMultiplier math.LegacyDec to JSONRPCBackend struct; initialized in NewJSONRPCBackend; added error handling for conversion.
jsonrpc/backend/gas.go Modified EstimateGas method to apply gasMultiplier when calculating estimated gas usage.
jsonrpc/backend/gas_test.go Updated assertions in EstimateGas test by removing upper bound check on estimated gas.
jsonrpc/config/config.go Added DefaultGasMultiplier constant and GasMultiplier field to JSONRPCConfig; updated command-line flags and config retrieval.

Possibly related PRs

Suggested reviewers

  • djm07073

🐰 In the meadow, where bunnies play,
A gas multiplier brightens the day.
With new fields and flags, we hop with glee,
Estimating gas, as smooth as can be.
So let’s cheer for changes, both clever and spry,
For a faster EVM, oh my, oh my! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (4)
jsonrpc/backend/backend.go (1)

Line range hint 1-1: Consider architectural improvements

To enhance the gas multiplier feature:

  1. Add documentation explaining the purpose and impact of the gas multiplier
  2. Consider implementing a centralized configuration validation
  3. Add metrics/logging for monitoring gas estimation accuracy
  4. Consider making the multiplier configurable at runtime via an admin API
jsonrpc/config/config.go (3)

45-46: Consider documenting the rationale for the 1.4 multiplier value

While 1.4 seems like a reasonable default value for gas estimation buffer, it would be helpful to document why this specific value was chosen. This helps future maintainers understand the reasoning behind this constant.


110-111: Enhance field documentation with validation requirements

The field documentation should specify:

  1. Expected format (decimal string)
  2. Valid range of values
  3. Impact on gas estimation

Consider updating the comment to:

-// GasMultiplier is the gas multiplier for the EVM state transition.
+// GasMultiplier is the gas multiplier for the EVM state transition.
+// Must be a valid decimal string greater than 1.0 (e.g. "1.4").
+// This value is used to pad the gas estimation to prevent out-of-gas errors during actual execution.

247-249: Add validation warning in config template

The gas multiplier configuration in the template should include a warning about validation requirements and potential impacts.

Consider updating the template comment to:

-# GasMultiplier is the gas multiplier for the EVM state transition.
+# GasMultiplier is the gas multiplier for the EVM state transition.
+# Must be a valid decimal string greater than 1.0 (e.g. "1.4").
+# Warning: Setting this too low may cause transactions to fail with out-of-gas errors,
+# while setting it too high will cause users to over-provision gas.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between fbe8e17 and 51572dd.

📒 Files selected for processing (4)
  • jsonrpc/backend/backend.go (4 hunks)
  • jsonrpc/backend/gas.go (1 hunks)
  • jsonrpc/backend/gas_test.go (1 hunks)
  • jsonrpc/config/config.go (7 hunks)

jsonrpc/backend/gas_test.go Show resolved Hide resolved
jsonrpc/backend/gas.go Show resolved Hide resolved
jsonrpc/backend/backend.go Outdated Show resolved Hide resolved
jsonrpc/backend/backend.go Show resolved Hide resolved
jsonrpc/config/config.go Show resolved Hide resolved
Copy link

codecov bot commented Dec 3, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 8 lines in your changes missing coverage. Please review.

Project coverage is 56.81%. Comparing base (46ca792) to head (ff16b02).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
jsonrpc/backend/backend.go 40.00% 4 Missing and 2 partials ⚠️
jsonrpc/config/config.go 50.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #128      +/-   ##
==========================================
- Coverage   56.83%   56.81%   -0.02%     
==========================================
  Files         109      109              
  Lines        9930     9944      +14     
==========================================
+ Hits         5644     5650       +6     
- Misses       3473     3479       +6     
- Partials      813      815       +2     
Files with missing lines Coverage Δ
jsonrpc/backend/gas.go 49.46% <100.00%> (+0.54%) ⬆️
jsonrpc/config/config.go 42.42% <50.00%> (+0.48%) ⬆️
jsonrpc/backend/backend.go 70.40% <40.00%> (-2.88%) ⬇️

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
jsonrpc/config/config.go (3)

45-46: Document the rationale for the default gas multiplier value

The default value of 1.4 seems arbitrary. Consider adding a comment explaining why this specific value was chosen.

-// DefaultGasMultiplier is the default gas multiplier for the EVM state transition.
+// DefaultGasMultiplier is the default gas multiplier for the EVM state transition.
+// The value 1.4 provides a 40% buffer to account for potential gas estimation inaccuracies
+// and prevent out-of-gas errors during actual transaction execution.
 DefaultGasMultiplier = "1.4"

110-111: Add validation constraints to the GasMultiplier field documentation

The field documentation should specify the valid range of values.

-// GasMultiplier is the gas multiplier for the EVM state transition.
+// GasMultiplier is the gas multiplier for the EVM state transition.
+// Must be a decimal string representing a value >= 1.0
 GasMultiplier string `mapstructure:"gas-multiplier"`

247-249: Add validation constraints to the configuration template

The configuration template should document the valid range for the gas multiplier.

-# GasMultiplier is the gas multiplier for the EVM state transition.
+# GasMultiplier is the gas multiplier for the EVM state transition.
+# Must be a decimal string representing a value >= 1.0 (e.g., "1.4" for 40% buffer)
 gas-multiplier = "{{ .JSONRPCConfig.GasMultiplier }}"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 51572dd and ff16b02.

📒 Files selected for processing (2)
  • jsonrpc/backend/backend.go (4 hunks)
  • jsonrpc/config/config.go (7 hunks)
🔇 Additional comments (3)
jsonrpc/backend/backend.go (3)

131-133: LGTM: Proper initialization of gas multiplier

The initialization of the gas multiplier field in the struct is correct.


56-57: ⚠️ Potential issue

Add thread safety for gas multiplier access

The gasMultiplier field might be accessed concurrently but lacks synchronization mechanisms.

Add mutex protection:

 type JSONRPCBackend struct {
     // ... other fields ...
     cfg           config.JSONRPCConfig
     gasMultiplier math.LegacyDec
+    gasMultMutex  sync.RWMutex
 }

Likely invalid or redundant comment.


89-96: ⚠️ Potential issue

Enhance gas multiplier validation

The gas multiplier validation needs to be strengthened to ensure valid values.

Add validation for the multiplier value:

 if cfg.GasMultiplier == "" {
     cfg.GasMultiplier = config.DefaultGasMultiplier
 }

 gasMultiplier, err := math.LegacyNewDecFromStr(cfg.GasMultiplier)
 if err != nil {
     return nil, err
 }
+// Validate multiplier range
+if gasMultiplier.LT(math.LegacyOneDec()) {
+    return nil, fmt.Errorf("gas multiplier must be greater than or equal to 1.0, got %s", cfg.GasMultiplier)
+}

Likely invalid or redundant comment.

@beer-1 beer-1 merged commit 75b1285 into main Dec 3, 2024
9 of 10 checks passed
@beer-1 beer-1 deleted the feat/add-gas-multiplier-on-jsonrpc branch December 3, 2024 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants