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

Improve ErrorOutOfGas handling in predicate execution #694

Merged
merged 5 commits into from
Jul 22, 2024

Conversation

bdeneux
Copy link
Contributor

@bdeneux bdeneux commented Jul 4, 2024

Fixe #692, see issue description for complete details.

Note: I was forced to move QueryInterpreter utility method into the keeper since using the common ResourceGas predicate error coming from keeper package lead to a cycle import between utils and prolog packages.

EDIT: Now all OutOfGas errors that occurs in the interpreter or at predicate cost will return an GRPC error instead of a predicate prolog error.

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling logic for gas consumption errors, ensuring more accurate error messages and context.
    • Enhanced test cases to cover additional scenarios related to bank balances and gas-related errors.
  • Refactor

    • Streamlined error handling by switching from if statements to switch statements for better code readability and maintainability.
    • Removed redundant logic and unused functions to clean up the codebase.

Copy link
Contributor

coderabbitai bot commented Jul 4, 2024

Walkthrough

The changes across various files focus on enhancing error handling related to gas management and bank operations, improving test case clarity, and removing obsolete functions. These updates aim to boost the reliability and maintainability of the codebase while providing more context in error messages.

Changes

File Path Change Summary
x/logic/keeper/grpc_query_ask_test.go Added imports for Cosmos SDK types and bank types, updated test cases with new queries and errors, and included a new test case for bank balances.
x/logic/keeper/interpreter.go Added import for sdkerrors, modified error handling in newInterpreter using a switch statement, and specifically addressed out-of-gas errors.
x/logic/prolog/error.go Removed imports related to store types, eliminated the AtomResourceGas and ResourceGas functions.
x/logic/util/prolog.go Added imports for error handling and SDK types, and modified logic to include checks for out-of-gas errors in solutions.

Sequence Diagram(s)

Not applicable for these changes as they are too varied and do not introduce new features or control flow modifications.

Poem

In the code, where logic weaves,
Errors now are caught with ease,
Gas is handled with a care,
Bank balances checked, oh so fair.
Out-of-gas, no more fear,
Error handling, crystal clear.
Coding changes, oh so bright,
Bringing clarity, day and night.


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

codecov bot commented Jul 4, 2024

Codecov Report

Attention: Patch coverage is 30.00000% with 14 lines in your changes missing coverage. Please review.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #694      +/-   ##
==========================================
- Coverage   54.35%   54.23%   -0.12%     
==========================================
  Files          74       74              
  Lines        2896     2906      +10     
==========================================
+ Hits         1574     1576       +2     
- Misses       1226     1234       +8     
  Partials       96       96              
Files Coverage Δ
x/logic/prolog/error.go 0.00% <ø> (ø)
x/logic/keeper/interpreter.go 93.10% <75.00%> (-0.95%) ⬇️
x/logic/util/prolog.go 0.00% <0.00%> (ø)

@bdeneux bdeneux marked this pull request as ready for review July 4, 2024 14:55
@bdeneux bdeneux force-pushed the fix/out-of-gas-catch branch 2 times, most recently from 9d478fe to f1c1abd Compare July 4, 2024 14:58
Copy link
Contributor

@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: 1

Outside diff range and nitpick comments (1)
x/logic/keeper/interpreter.go (1)

92-95: Consider initializing env explicitly.

While env is used later in the function, it might be beneficial to explicitly initialize it to avoid potential issues.

- var env *engine.Env
+ env := engine.NewEnv()
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fb341ff and f1c1abd.

Files selected for processing (2)
  • x/logic/keeper/grpc_query_ask_test.go (3 hunks)
  • x/logic/keeper/interpreter.go (4 hunks)
Additional context used
GitHub Check: codecov/patch
x/logic/keeper/interpreter.go

[warning] 107-107: x/logic/keeper/interpreter.go#L107
Added line #L107 was not covered by tests


[warning] 244-244: x/logic/keeper/interpreter.go#L244
Added line #L244 was not covered by tests


[warning] 257-257: x/logic/keeper/interpreter.go#L257
Added line #L257 was not covered by tests

Additional comments not posted (12)
x/logic/keeper/interpreter.go (9)

33-33: LGTM!

The new constant defaultEnvCap is a useful addition for setting a default environment capacity.


86-90: Ensure proper error handling for parsing queries.

The error handling for parsing queries seems appropriate. Ensure that all possible parsing errors are covered.


96-101: LGTM!

The logic for handling solution limits and appending environments is sound.


103-108: Ensure comprehensive test coverage for envsToResults.

The function envsToResults is crucial for processing results. Ensure that it is thoroughly tested.

Tools
GitHub Check: codecov/patch

[warning] 107-107: x/logic/keeper/interpreter.go#L107
Added line #L107 was not covered by tests


125-129: LGTM!

The final result struct construction and return logic are appropriate.


226-230: LGTM!

The parsedVarsToVars function correctly maps parsed variables to their string representations.


232-251: Ensure comprehensive test coverage for envsToResults.

The function envsToResults is crucial for processing results. Ensure that it is thoroughly tested.

Tools
GitHub Check: codecov/patch

[warning] 244-244: x/logic/keeper/interpreter.go#L244
Added line #L244 was not covered by tests


253-265: LGTM!

The scanExpression function correctly scans expressions and returns substitutions.

Tools
GitHub Check: codecov/patch

[warning] 257-257: x/logic/keeper/interpreter.go#L257
Added line #L257 was not covered by tests


267-272: LGTM!

The isBound function correctly checks if a parsed variable is bound in the given environment.

x/logic/keeper/grpc_query_ask_test.go (3)

23-23: LGTM!

The new import bankypes is necessary for the new test case for a bank query.


142-149: LGTM!

The new test case for a bank query correctly tests the handling of bank balances and gas consumption.


338-342: LGTM!

The updated mock interaction for bankKeeper correctly simulates gas consumption for the GetAccountsBalances method.

x/logic/keeper/interpreter.go Outdated Show resolved Hide resolved
@bdeneux bdeneux requested review from ccamel and amimart July 4, 2024 15:21
@bdeneux bdeneux self-assigned this Jul 4, 2024
@bdeneux bdeneux linked an issue Jul 4, 2024 that may be closed by this pull request
@amimart
Copy link
Member

amimart commented Jul 5, 2024

@bdeneux Thanks!

I wonder why we've integrated the out of gas as a prolog error, it creates a discrepancy in the way the ask query answers: either as a gRPC error or a success containing a prolog error. Moreover, out of gas errors having a sense at the sdk level this feels a bit wrong to me.

@ccamel I'd be really interested in your insights on that, I may be missing something :)

Copy link
Member

@amimart amimart left a comment

Choose a reason for hiding this comment

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

If you don't mind, as mentioned in the discussion we'll change the behaviour to always return a gRPC error instead of a prolog error in the case of out of gas.

@bdeneux bdeneux force-pushed the fix/out-of-gas-catch branch from f1c1abd to fedbafb Compare July 18, 2024 14:22
@bdeneux bdeneux force-pushed the fix/out-of-gas-catch branch from fedbafb to fdc9db9 Compare July 18, 2024 14:23
Copy link
Contributor

@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, codebase verification and nitpick comments (1)
x/logic/keeper/interpreter.go (1)

112-116: Ensure test coverage for new error handling logic.

The changes improve error handling by specifically addressing out-of-gas errors. However, the new error handling logic is not covered by tests. Please add test cases to cover this new logic.

Analysis chain

Ensure test coverage for new error handling logic.

The changes improve error handling by specifically addressing out-of-gas errors. However, the new error handling logic is not covered by tests.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Ensure test coverage for new error handling logic.

# Test: Search for test cases covering the new error handling logic. Expect: Test cases covering the new error handling logic.
rg --type go -A 5 $'storetypes.ErrorOutOfGas'

Length of output: 874

Tools
GitHub Check: codecov/patch

[warning] 115-116: x/logic/keeper/interpreter.go#L115-L116
Added lines #L115 - L116 were not covered by tests

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f1c1abd and 594c1c4.

Files selected for processing (4)
  • x/logic/keeper/grpc_query_ask_test.go (3 hunks)
  • x/logic/keeper/interpreter.go (2 hunks)
  • x/logic/prolog/error.go (3 hunks)
  • x/logic/util/prolog.go (2 hunks)
Additional context used
GitHub Check: codecov/patch
x/logic/util/prolog.go

[warning] 57-60: x/logic/util/prolog.go#L57-L60
Added lines #L57 - L60 were not covered by tests

x/logic/keeper/interpreter.go

[warning] 115-116: x/logic/keeper/interpreter.go#L115-L116
Added lines #L115 - L116 were not covered by tests

Additional comments not posted (13)
x/logic/util/prolog.go (4)

8-8: Import addition approved.

The import for errorsmod is necessary for wrapping errors with additional context.


10-10: Import addition approved.

The import for sdk is necessary for accessing SDK-specific types and functions.


11-11: Import addition approved.

The import for sdkerrors is necessary for handling SDK-specific errors.


57-61: Ensure test coverage for new error handling logic.

The changes improve error handling by specifically addressing out-of-gas errors. However, the new error handling logic is not covered by tests.

Tools
GitHub Check: codecov/patch

[warning] 57-60: x/logic/util/prolog.go#L57-L60
Added lines #L57 - L60 were not covered by tests

x/logic/prolog/error.go (3)

5-5: Import removal approved.

The import for cosmossdk.io/store/types is no longer necessary as the related components are removed.


5-5: Function removal approved.

The function AtomResourceGas is no longer necessary as the related components are removed.


5-5: Function removal approved.

The function ResourceGas is no longer necessary as the related components are removed.

x/logic/keeper/interpreter.go (1)

10-10: Import addition approved.

The import for sdkerrors is necessary for handling SDK-specific errors.

x/logic/keeper/grpc_query_ask_test.go (5)

20-20: Import addition approved.

The import for sdk is necessary for accessing SDK-specific types and functions.


23-23: Import addition approved.

The import for bankypes is necessary for accessing bank-specific types and functions.


142-146: Test case addition approved.

The new test case covers the error handling logic for bank balances and gas consumption. Ensure that the test case passes successfully.


153-153: Test case addition approved.

The new test case covers the error handling logic for block height and gas consumption. Ensure that the test case passes successfully.


332-336: Test case addition approved.

The new test case logic for gas consumption related to bank operations is necessary for covering the new error handling logic. Ensure that the test case passes successfully.

@bdeneux bdeneux force-pushed the fix/out-of-gas-catch branch from 594c1c4 to f117c7c Compare July 19, 2024 05:51
@bdeneux bdeneux requested a review from amimart July 19, 2024 05:55
Copy link
Contributor

@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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 594c1c4 and f117c7c.

Files selected for processing (2)
  • x/logic/keeper/interpreter.go (2 hunks)
  • x/logic/util/prolog.go (2 hunks)
Additional context used
GitHub Check: codecov/patch
x/logic/util/prolog.go

[warning] 56-62: x/logic/util/prolog.go#L56-L62
Added lines #L56 - L62 were not covered by tests

x/logic/keeper/interpreter.go

[warning] 114-115: x/logic/keeper/interpreter.go#L114-L115
Added lines #L114 - L115 were not covered by tests

Additional comments not posted (6)
x/logic/util/prolog.go (4)

12-12: LGTM! Import for errorsmod is necessary.

The import errorsmod from cosmossdk.io/errors is correctly added for enhanced error handling.


15-15: LGTM! Import for sdk is necessary.

The import sdk from github.com/cosmos/cosmos-sdk/types is correctly added for using SDK types.


16-16: LGTM! Import for sdkerrors is necessary.

The import sdkerrors from github.com/cosmos/cosmos-sdk/types/errors is correctly added for handling SDK-specific errors.


56-63: Enhanced error handling for sdkerrors.ErrOutOfGas.

The error handling logic is improved to handle out-of-gas scenarios more robustly using a switch statement.

However, ensure that these changes are covered by tests.

Tools
GitHub Check: codecov/patch

[warning] 56-62: x/logic/util/prolog.go#L56-L62
Added lines #L56 - L62 were not covered by tests

x/logic/keeper/interpreter.go (2)

19-19: LGTM! Import for sdkerrors is necessary.

The import sdkerrors from github.com/cosmos/cosmos-sdk/types/errors is correctly added for handling SDK-specific errors.


111-115: Enhanced error handling for storetypes.ErrorOutOfGas.

The error handling logic is improved to handle out-of-gas scenarios more robustly using a switch statement.

However, ensure that these changes are covered by tests.

Tools
GitHub Check: codecov/patch

[warning] 114-115: x/logic/keeper/interpreter.go#L114-L115
Added lines #L114 - L115 were not covered by tests

Copy link
Member

@amimart amimart left a comment

Choose a reason for hiding this comment

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

Thanks, I think we're almost there :)

x/logic/keeper/grpc_query_ask_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@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: 6

Outside diff range, codebase verification and nitpick comments (1)
x/logic/util/prolog.go (1)

Line range hint 75-98:
Add test coverage for converting environments to results.

The conversion of environments to results is not covered by tests.

Do you want me to generate the unit testing code or open a GitHub issue to track this task?

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f117c7c and c242379.

Files selected for processing (3)
  • x/logic/keeper/grpc_query_ask_test.go (3 hunks)
  • x/logic/keeper/interpreter.go (2 hunks)
  • x/logic/util/prolog.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/logic/keeper/grpc_query_ask_test.go
Additional context used
GitHub Check: codecov/patch
x/logic/util/prolog.go

[warning] 31-31: x/logic/util/prolog.go#L31
Added line #L31 was not covered by tests


[warning] 49-49: x/logic/util/prolog.go#L49
Added line #L49 was not covered by tests


[warning] 55-55: x/logic/util/prolog.go#L55
Added line #L55 was not covered by tests


[warning] 57-65: x/logic/util/prolog.go#L57-L65
Added lines #L57 - L65 were not covered by tests

x/logic/keeper/interpreter.go

[warning] 115-116: x/logic/keeper/interpreter.go#L115-L116
Added lines #L115 - L116 were not covered by tests

Additional comments not posted (2)
x/logic/util/prolog.go (1)

Line range hint 71-73:
LGTM!

The function correctly maps parsed variables to their string names.

x/logic/keeper/interpreter.go (1)

Line range hint 76-78:
LGTM!

The function correctly delegates the query execution to util.QueryInterpreter.

x/logic/util/prolog.go Show resolved Hide resolved
x/logic/util/prolog.go Show resolved Hide resolved
x/logic/util/prolog.go Show resolved Hide resolved
x/logic/util/prolog.go Show resolved Hide resolved
x/logic/keeper/interpreter.go Show resolved Hide resolved
x/logic/keeper/interpreter.go Show resolved Hide resolved
@bdeneux bdeneux requested a review from amimart July 22, 2024 09:43
Copy link
Member

@amimart amimart left a comment

Choose a reason for hiding this comment

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

Well done this looks good! Thanks 😉

Copy link
Member

@ccamel ccamel left a comment

Choose a reason for hiding this comment

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

Lgtm! Thx 🙏

@bdeneux bdeneux merged commit a55301b into main Jul 22, 2024
19 of 20 checks passed
@bdeneux bdeneux deleted the fix/out-of-gas-catch branch July 22, 2024 13:15
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.

🧠 Logic: Lack of information when ErrorOutOfGas in interpreter
3 participants