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: sync evm nonce with cosmos #54

Merged
merged 3 commits into from
Aug 29, 2024
Merged

Conversation

beer-1
Copy link
Member

@beer-1 beer-1 commented Aug 29, 2024

Description

Closes: #XXXX


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 an IncrementSequenceDecorator for managing transaction signer sequence numbers, enhancing security against replay attacks.
    • Added a code_hash field to ContractAccount for improved contract management.
  • Bug Fixes

    • Implemented stricter validation for transaction sequences to prevent nonce-related errors.
    • Enhanced error handling for invalid block numbers in transaction count retrieval.
  • Refactor

    • Streamlined account management by utilizing an accountKeeper, improving code readability and maintainability.
    • Removed unnecessary nonce management from EVM call functions to simplify transaction processing.
    • Refined criteria for identifying empty accounts by including sequence number checks.

@beer-1 beer-1 self-assigned this Aug 29, 2024
Copy link

coderabbitai bot commented Aug 29, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The changes involve multiple modifications across various files, focusing on enhancing transaction handling, account management, and contract functionalities. Key updates include the introduction of a new IncrementSequenceDecorator for managing signer sequence numbers, refinements in nonce management, and the addition of a code_hash field in contract accounts. Several functions were refactored to improve code organization and clarity, while some were removed to streamline processes.

Changes

Files Change Summary
app/ante/..., app/ante/... Updated decorator instantiation in NewAnteHandler and introduced IncrementSequenceDecorator for managing sequence numbers in transactions.
jsonrpc/backend/tx.go Enhanced transaction sequence validation and added safeguards for block number inputs in SendTx and GetTransactionCount.
proto/minievm/evm/v1/auth.proto Added bytes code_hash field to ContractAccount for contract verification.
x/evm/keeper/..., x/evm/keeper/... Removed nonce increment functionality from EVM calls and introduced a new increaseNonce method for managing account nonces during call operations.
x/evm/keeper/query_server.go Modified function call in Call method to streamline tracing behavior.
x/evm/state/keys.go Removed AccountKeyPrefix variable and associated accountKey function, simplifying key generation logic.
x/evm/state/statedb.go Refactored account management to utilize accountKeeper, enhancing abstraction and modularity in account operations.
x/evm/types/auth.go Updated NewContractAccountWithAddress to initialize CodeHash and refined IsEmptyAccount logic to include sequence number checks.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant MsgServer
    participant AccountKeeper
    participant StateDB

    User->>MsgServer: Call transaction
    MsgServer->>AccountKeeper: increaseNonce(caller)
    AccountKeeper-->>MsgServer: Nonce incremented
    MsgServer->>StateDB: Create or retrieve account
    StateDB-->>MsgServer: Account ready
    MsgServer->>User: Transaction processed
Loading

🐇 In the code, we hop and play,
With sequences in a brand new way.
Nonces managed, contracts shine,
Our little changes make it fine!
So let’s rejoice, with a hop and a cheer,
For every tweak brings us near! 🥕✨


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.

@beer-1 beer-1 marked this pull request as ready for review August 29, 2024 05:24
@beer-1 beer-1 requested a review from a team as a code owner August 29, 2024 05:24
Copy link

codecov bot commented Aug 29, 2024

Codecov Report

Attention: Patch coverage is 65.49296% with 49 lines in your changes missing coverage. Please review.

Project coverage is 28.38%. Comparing base (8d5d08b) to head (d73722d).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
x/evm/state/statedb.go 75.71% 6 Missing and 11 partials ⚠️
app/ante/sigverify.go 70.45% 8 Missing and 5 partials ⚠️
x/evm/keeper/msg_server.go 35.71% 6 Missing and 3 partials ⚠️
jsonrpc/backend/tx.go 0.00% 7 Missing ⚠️
x/evm/types/auth.go 0.00% 3 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #54      +/-   ##
==========================================
+ Coverage   28.35%   28.38%   +0.02%     
==========================================
  Files         114      113       -1     
  Lines       12194    12238      +44     
==========================================
+ Hits         3458     3474      +16     
- Misses       8236     8260      +24     
- Partials      500      504       +4     
Files with missing lines Coverage Δ
app/ante/ante.go 56.86% <100.00%> (ø)
x/evm/keeper/context.go 72.78% <100.00%> (-0.36%) ⬇️
x/evm/keeper/query_server.go 34.12% <100.00%> (ø)
x/evm/state/keys.go 100.00% <ø> (ø)
x/evm/types/auth.go 0.00% <0.00%> (ø)
jsonrpc/backend/tx.go 0.00% <0.00%> (ø)
x/evm/keeper/msg_server.go 45.83% <35.71%> (-1.27%) ⬇️
app/ante/sigverify.go 23.27% <70.45%> (+18.05%) ⬆️
x/evm/state/statedb.go 64.86% <75.71%> (+0.50%) ⬆️

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: 10

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8d5d08b and a7794c7.

Files ignored due to path filters (1)
  • x/evm/types/auth.pb.go is excluded by !**/*.pb.go
Files selected for processing (10)
  • app/ante/ante.go (1 hunks)
  • app/ante/sigverify.go (2 hunks)
  • jsonrpc/backend/tx.go (3 hunks)
  • proto/minievm/evm/v1/auth.proto (2 hunks)
  • x/evm/keeper/context.go (2 hunks)
  • x/evm/keeper/msg_server.go (2 hunks)
  • x/evm/keeper/query_server.go (1 hunks)
  • x/evm/state/keys.go (1 hunks)
  • x/evm/state/statedb.go (11 hunks)
  • x/evm/types/auth.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • x/evm/state/keys.go
Additional context used
GitHub Check: codecov/patch
x/evm/types/auth.go

[warning] 26-27: x/evm/types/auth.go#L26-L27
Added lines #L26 - L27 were not covered by tests


[warning] 71-71: x/evm/types/auth.go#L71
Added line #L71 was not covered by tests

x/evm/keeper/msg_server.go

[warning] 149-150: x/evm/keeper/msg_server.go#L149-L150
Added lines #L149 - L150 were not covered by tests


[warning] 152-153: x/evm/keeper/msg_server.go#L152-L153
Added lines #L152 - L153 were not covered by tests


[warning] 169-170: x/evm/keeper/msg_server.go#L169-L170
Added lines #L169 - L170 were not covered by tests

app/ante/sigverify.go

[warning] 225-229: app/ante/sigverify.go#L225-L229
Added lines #L225 - L229 were not covered by tests


[warning] 232-235: app/ante/sigverify.go#L232-L235
Added lines #L232 - L235 were not covered by tests


[warning] 237-240: app/ante/sigverify.go#L237-L240
Added lines #L237 - L240 were not covered by tests


[warning] 243-243: app/ante/sigverify.go#L243
Added line #L243 was not covered by tests


[warning] 248-260: app/ante/sigverify.go#L248-L260
Added lines #L248 - L260 were not covered by tests


[warning] 263-264: app/ante/sigverify.go#L263-L264
Added lines #L263 - L264 were not covered by tests


[warning] 266-271: app/ante/sigverify.go#L266-L271
Added lines #L266 - L271 were not covered by tests


[warning] 273-275: app/ante/sigverify.go#L273-L275
Added lines #L273 - L275 were not covered by tests


[warning] 278-278: app/ante/sigverify.go#L278
Added line #L278 was not covered by tests


[warning] 282-282: app/ante/sigverify.go#L282
Added line #L282 was not covered by tests

jsonrpc/backend/tx.go

[warning] 82-84: jsonrpc/backend/tx.go#L82-L84
Added lines #L82 - L84 were not covered by tests


[warning] 169-171: jsonrpc/backend/tx.go#L169-L171
Added lines #L169 - L171 were not covered by tests


[warning] 174-174: jsonrpc/backend/tx.go#L174
Added line #L174 was not covered by tests

x/evm/state/statedb.go

[warning] 290-292: x/evm/state/statedb.go#L290-L292
Added lines #L290 - L292 were not covered by tests


[warning] 363-363: x/evm/state/statedb.go#L363
Added line #L363 was not covered by tests


[warning] 401-402: x/evm/state/statedb.go#L401-L402
Added lines #L401 - L402 were not covered by tests

Additional comments not posted (8)
proto/minievm/evm/v1/auth.proto (2)

16-16: LGTM!

The addition of the code_hash field to the ContractAccount message enhances the structure by allowing it to store a hash of the contract's code. This is crucial for contract verification and management.

The code changes are approved.


25-30: LGTM!

The formatting adjustments in the ShorthandAccount message standardize the code style but do not affect functionality.

The code changes are approved.

app/ante/ante.go (1)

97-97: LGTM!

The modification in the instantiation of decorators within the NewAnteHandler function suggests a potential shift in the namespace or the implementation of the NewIncrementSequenceDecorator function. This alteration may indicate a refactoring effort aimed at improving code organization or clarity.

The code changes are approved.

x/evm/keeper/query_server.go (1)

86-86: Verify the correctness of the tracer variable usage.

The boolean argument false has been removed and the tracer variable is passed directly. Ensure that the tracer variable is correctly initialized and used in the EVMCallWithTracer method.

Run the following script to verify the initialization and usage of the tracer variable:

Verification successful

Tracer variable is correctly initialized and used.

The tracer variable is properly initialized using logger.NewJSONLogger and is of the correct type *tracing.Hooks. It is appropriately used in the EVMCallWithTracer method, confirming the correctness of the change. No issues were found with the modification.

  • Initialization: logger.NewJSONLogger from github.com/ethereum/go-ethereum/eth/tracers/logger.
  • Usage: Correctly passed to EVMCallWithTracer.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the initialization and usage of the `tracer` variable.

# Test: Search for the initialization and usage of the `tracer` variable. Expect: Correct initialization and usage.
rg --type go -A 5 $'var tracer *tracing.Hooks'

Length of output: 47


Script:

#!/bin/bash
# Search for the `tracer` variable in the `query_server.go` file to check its initialization.
rg --type go 'tracer' x/evm/keeper/query_server.go

# Search for the `EVMCallWithTracer` method to understand its parameters and usage.
rg --type go 'func EVMCallWithTracer' -A 10

# Search for any imports or type definitions related to `tracing.Hooks`.
rg --type go 'tracing.Hooks'

Length of output: 1449

jsonrpc/backend/tx.go (1)

12-12: LGTM!

The new import statement for github.com/ethereum/go-ethereum/core is necessary for the added functionality.

The code changes are approved.

x/evm/keeper/context.go (2)

229-229: LGTM!

The change simplifies the method interface, focusing on EVM calls without nonce management.

The code changes are approved.


233-233: LGTM!

The change indicates that nonce management is no longer handled within this context, simplifying the control flow.

The code changes are approved.

x/evm/state/statedb.go (1)

264-265: LGTM!

The change enhances the abstraction layer for account management, promoting better encapsulation.

The code changes are approved.

Comment on lines +26 to +27
BaseAccount: authtypes.NewBaseAccountWithAddress(addr),
CodeHash: []byte{},
Copy link

Choose a reason for hiding this comment

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

LGTM! But add tests.

The initialization of the CodeHash field enhances the ContractAccount structure by adding a property that may be used for further contract-related functionalities. However, the changes are not covered by tests.

The code changes are approved.

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

Tools
GitHub Check: codecov/patch

[warning] 26-27: x/evm/types/auth.go#L26-L27
Added lines #L26 - L27 were not covered by tests

@@ -67,5 +68,5 @@
_, isShorthandAccount := account.(ShorthandAccountI)
_, isContractAccount := account.(*ContractAccount)

return !isModuleAccount && !isShorthandAccount && !isContractAccount && account.GetPubKey() == nil
return !isModuleAccount && !isShorthandAccount && !isContractAccount && account.GetPubKey() == nil && account.GetSequence() == 0
Copy link

Choose a reason for hiding this comment

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

LGTM! But add tests.

The inclusion of the sequence check makes the criteria for determining whether an account is considered "empty" more stringent. However, the changes are not covered by tests.

The code changes are approved.

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

Tools
GitHub Check: codecov/patch

[warning] 71-71: x/evm/types/auth.go#L71
Added line #L71 was not covered by tests

Comment on lines +145 to +156
// increaseNonce increases the nonce of the given account.
func (ms *msgServerImpl) increaseNonce(ctx context.Context, caller sdk.AccAddress) error {
senderAcc := ms.accountKeeper.GetAccount(ctx, caller)
if senderAcc == nil {
senderAcc = ms.accountKeeper.NewAccountWithAddress(ctx, caller)
}
if err := senderAcc.SetSequence(senderAcc.GetSequence() + 1); err != nil {
return err
}
ms.accountKeeper.SetAccount(ctx, senderAcc)
return nil
}
Copy link

Choose a reason for hiding this comment

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

Add tests for the increaseNonce function.

The function correctly increments the nonce of the specified account. However, the function is not covered by tests.

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

Tools
GitHub Check: codecov/patch

[warning] 149-150: x/evm/keeper/msg_server.go#L149-L150
Added lines #L149 - L150 were not covered by tests


[warning] 152-153: x/evm/keeper/msg_server.go#L152-L153
Added lines #L152 - L153 were not covered by tests

Comment on lines +165 to +171
// increase nonce before execution like evm does
//
// NOTE: evm only increases nonce at Call not Create, so we should do the same.
if err := ms.increaseNonce(ctx, sender); err != nil {
return nil, err
}

Copy link

Choose a reason for hiding this comment

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

Add tests for the modified Call function.

The function correctly invokes increaseNonce before executing the main logic. However, the added lines are not covered by tests.

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

Tools
GitHub Check: codecov/patch

[warning] 169-170: x/evm/keeper/msg_server.go#L169-L170
Added lines #L169 - L170 were not covered by tests

app/ante/sigverify.go Show resolved Hide resolved
jsonrpc/backend/tx.go Show resolved Hide resolved
jsonrpc/backend/tx.go Show resolved Hide resolved
x/evm/state/statedb.go Show resolved Hide resolved
x/evm/state/statedb.go Show resolved Hide resolved
x/evm/state/statedb.go Show resolved Hide resolved
@beer-1 beer-1 merged commit a65d532 into main Aug 29, 2024
6 checks passed
@beer-1 beer-1 deleted the feat/sync-nonce-with-cosmos branch August 29, 2024 06:10
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.

1 participant