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(bank/v2): Add MsgSend handler #21736

Merged
merged 33 commits into from
Sep 23, 2024
Merged

feat(bank/v2): Add MsgSend handler #21736

merged 33 commits into from
Sep 23, 2024

Conversation

hieuvubk
Copy link
Contributor

@hieuvubk hieuvubk commented Sep 15, 2024

Description

Closes: #21705

  • regist msg & query handler in runtime/v2
  • MsgSend handler, cmd
  • QueryBalance handler, cmd
  • InitGenesis with Balances and Supply
  • init bankv2 gen file in simapp/v2 testnet
  • systemtests

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 (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • 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.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

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

    • Streamlined service registration process, enhancing adaptability for different module types.
    • Introduced a system test for the bank module, validating transaction sending functionality in version 2 of the application.
    • Added support for handling Genesis state of the new bank module version.
    • Expanded message handling capabilities with new transaction and query command structures.
    • Implemented CLI functionality for querying account balances and sending funds directly from the command line.
  • Bug Fixes

    • Improved error handling in service registration and transaction operations.
  • Documentation

    • Updated documentation to reflect changes in service registration and the addition of new tests for better user guidance.

Copy link
Contributor

coderabbitai bot commented Sep 15, 2024

Walkthrough

Walkthrough

The changes in this pull request enhance the service registration process within the Cosmos SDK's banking module. Key modifications include the simplification of the RegisterServices method to accept a broader interface, allowing for improved error handling and flexibility. Additionally, a new system test for the bank module's version 2 functionality has been introduced, validating transaction sending capabilities and ensuring the module's robustness. Other updates include enhancements to message handling and query processing within the bank module.

Changes

File Path Change Summary
runtime/v2/manager.go Simplified RegisterServices method to accept appmodulev2.AppModule, enhancing service registration and error handling.
tests/systemtests/bankv2_test.go Introduced a system test for the bank module's v2 functionality, validating transaction sending capabilities.
simapp/v2/simdv2/cmd/testnet.go Added logic for handling the Genesis state of the new bank module version, including a helper function to convert v1 state to v2 format.
x/bank/v2/module.go Enhanced AppModule by adding message handlers for MsgSend and MsgMint, and introduced methods for query decoders and command structures.
x/bank/v2/types/msgs.go Introduced MsgSend structure for coin transfers, including a constructor function and interface implementation.
x/bank/v2/client/cli/query.go Implemented CLI functionality for querying bank module data, including commands to retrieve account balances.
x/bank/v2/client/cli/tx.go Implemented CLI functionality for handling bank transactions, specifically for sending funds between accounts.

Possibly related issues

  • [Epic]: Bank/v2 #17579: The changes aim to create a new bank module that is extendable and efficient, aligning with the objectives of reducing the scope and improving performance as mentioned in this issue.

Possibly related PRs

  • refactor(core): re-add handlers #21575: The changes in this PR involve modifications to the runtime/v2/manager.go file, specifically enhancing the message handling system, which is directly related to the changes made in the main PR regarding service registration and message handling.
  • feat(bank/v2): Send function #21606: This PR introduces enhancements to the bank module's keeper, including the SendCoins function, which aligns with the main PR's focus on improving service registration and handling within the banking module.
  • refactor(x/bank/v2): migrate to handlers #21659: This PR migrates the message and query servers in the bank module to a handler-based architecture, which is relevant to the main PR's changes in service registration and handling mechanisms.

Suggested labels

C:server/v2

Suggested reviewers

  • julienrbrt
  • akhilkumarpilli

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.

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
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

Preliminary review, looks already good but I have a few comments

x/bank/v2/types/msgs.go Outdated Show resolved Hide resolved
@@ -102,6 +104,12 @@ func (am AppModule) RegisterMsgHandlers(router appmodulev2.MsgRouter) {
errs = errors.Join(errs, err)
}

if err := appmodulev2.RegisterHandler(
Copy link
Member

Choose a reason for hiding this comment

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

@testinginprod, imho we should improve the API to register multiple messages and handlers in that method. Otherwise it is imho way too verbose.

like

appmodulev2.RegisterHandlers(router, "cosmos.bank.v2.MsgSend": handlers.SendHandler, "cosmos.bank.v2.MsgUpdateParams": handlers.ParamsHandler)

It will need to be a bit less type safe I think, but still a better UX.


var FlagSplit = "split"

// NewTxCmd returns a root CLI command handler for all x/bank transaction commands.
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a TODO to remove and link the gRPC / AutoCLI issue ?

message MsgSend {
option (cosmos.msg.v1.signer) = "from_address";
option (amino.name) = "cosmos-sdk/MsgSend";
Copy link
Member

Choose a reason for hiding this comment

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

To delete

@@ -480,6 +480,14 @@ func (m *MM[T]) RegisterServices(app *App[T]) error {
if module, ok := module.(appmodulev2.HasPostMsgHandlers); ok {
module.RegisterPostMsgHandlers(app.msgRouterBuilder)
}

if module, ok := module.(appmodulev2.HasMsgHandlers); ok {
Copy link
Member

Choose a reason for hiding this comment

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

We should add a check in registerService to avoid the double registration.

}

if c.err != nil {
app.logger.Warn("error registering services", "error", c.err)
}

// TODO: query regist by RegisterQueryHandlers not in grpcQueryDecoders
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do some hacky here cause RegisterQueryHandlers not add query decoder to grpcQueryDecoders so I have to add again.
Must be some way better but idk yet.

if errs != nil {
panic(errs)
}
}

// GetQueryDecoders registers the query handlers for the bank module.
func (am AppModule) GetQueryDecoders() map[string]func() gogoproto.Message {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

dito ref

Comment on lines +621 to +623
decoderMap := module.GetQueryDecoders()
for path, decoder := range decoderMap {
app.GRPCMethodsToMessageMap[path] = decoder

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
@@ -5,11 +5,14 @@
"encoding/json"
"errors"
"fmt"
"reflect"

Check notice

Code scanning / CodeQL

Sensitive package import Note

Certain system packages contain functions which may be a possible source of non-determinism
x/bank/v2/types/msgs.go Outdated Show resolved Hide resolved
x/bank/v2/module.go Outdated Show resolved Hide resolved
Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

lgtm!, mostly docs missing

x/bank/v2/module.go Outdated Show resolved Hide resolved
x/bank/v2/module.go Show resolved Hide resolved
@@ -402,6 +403,11 @@ func initGenFiles[T transaction.Tx](
}
appGenState[banktypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&bankGenState)

var bankV2GenState bankv2types.GenesisState
clientCtx.Codec.MustUnmarshalJSON(appGenState[bankv2types.ModuleName], &bankV2GenState)
Copy link
Member

@julienrbrt julienrbrt Sep 23, 2024

Choose a reason for hiding this comment

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

I like the fallback tbh for now, maybe we should check if len(bankV2GenState.Balances) > 0 and if not, we use bank v1 genesis?

@@ -504,3 +510,13 @@ func writeFile(name, dir string, contents []byte) error {

return os.WriteFile(file, contents, 0o600)
}

func getBankV2GenesisFromV1(v1GenesisState banktypes.GenesisState) bankv2types.GenesisState {
Copy link
Member

Choose a reason for hiding this comment

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

let's add a comment on the rationale of doing this (basically, to be deleted after genesis migration command addition)

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

Outside diff range and nitpick comments (1)
x/bank/v2/module.go (1)

174-178: LGTM! Address the TODO comment in a follow-up task.

The GetTxCmd method is a good addition that provides access to the transaction commands for the bank/v2 module.

The TODO comment suggests that this method might be replaced by autocli in the future. To ensure this doesn't get overlooked:

  1. Consider creating a follow-up task or issue to track the migration to autocli.
  2. Add a more descriptive comment explaining why autocli will be used and any potential blockers for the migration.

Would you like me to create a GitHub issue to track this TODO?

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1fa11c9 and 829c03c.

Files selected for processing (3)
  • simapp/v2/simdv2/cmd/testnet.go (3 hunks)
  • x/bank/v2/module.go (3 hunks)
  • x/bank/v2/types/msgs.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • simapp/v2/simdv2/cmd/testnet.go
  • x/bank/v2/types/msgs.go
Additional context used
Path-based instructions (1)
x/bank/v2/module.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (1)
x/bank/v2/module.go (1)

8-8: Address the security alert regarding the use of the "reflect" package.

The addition of new import statements is necessary for the added functionality. However, there's a security alert from GitHub Advanced Security about the import of the "reflect" package.

The use of the "reflect" package might introduce non-determinism, which could be a concern in blockchain applications. Consider the following:

  1. Evaluate if the use of reflection is absolutely necessary. If possible, replace it with type-safe alternatives.
  2. If reflection cannot be avoided, ensure its usage is limited and well-documented.
  3. Add comments explaining why reflection is needed and any potential risks.

To verify the usage of the "reflect" package:

This script will help identify where reflection is used and if there are any existing comments explaining its usage.

Also applies to: 11-11

x/bank/v2/module.go Show resolved Hide resolved
x/bank/v2/module.go Show resolved Hide resolved
x/bank/v2/module.go Show resolved Hide resolved
x/bank/v2/module.go Show resolved Hide resolved
Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

nice work!

Copy link
Contributor

@akhilkumarpilli akhilkumarpilli left a comment

Choose a reason for hiding this comment

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

lgtm!

@julienrbrt
Copy link
Member

@hieuvubk could you fix the conflicts and let's merge 🚀

@hieuvubk hieuvubk enabled auto-merge September 23, 2024 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create handlers and commands for send and mint
3 participants