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

refactor(core): re-add handlers #21575

Merged
merged 11 commits into from
Sep 9, 2024
Merged

refactor(core): re-add handlers #21575

merged 11 commits into from
Sep 9, 2024

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Sep 6, 2024

Description

ref: #21176

I removed getting msg name directly from core.
I tried to use reflection to get it automatically, but it didn't work: ea34ede
So modules would just pass their message name alongside their implementation instead.


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

Summary by CodeRabbit

  • New Features

    • Enhanced message handling system with new helper functions for improved type safety in handler registration.
    • Streamlined registration process for message handlers, reducing potential runtime errors.
    • Updated interfaces for clearer and more consistent handler registration across different message types.
    • Improved error logging for better debugging context in service registration.
  • Documentation

    • Added detailed usage examples in comments to assist developers in implementing the new handlers effectively.

Copy link
Contributor

coderabbitai bot commented Sep 6, 2024

Walkthrough

Walkthrough

The changes introduce new helper functions for registering message handlers in the appmodulev2 package, enhancing type safety and simplifying the registration process. The interfaces for PreMsgRouter, MsgRouter, PostMsgRouter, and QueryRouter have been updated to accept handlers directly, eliminating the need for message names. Additionally, error handling and logging improvements have been made in the runtime/v2/manager.go file, and method names in the MsgRouterBuilder struct have been modified for clarity.

Changes

File Path Change Summary
core/appmodule/v2/handlers.go - Updated PreMsgRouter, MsgRouter, PostMsgRouter, and QueryRouter interfaces to register handlers directly.
- Introduced new functions: RegisterMsgPreHandler, RegisterMsgHandler, RegisterPostMsgHandler, and RegisterQueryHandler for improved type safety and clarity.
runtime/v2/manager.go - Refactored error handling in registerServices, registerQueryHandlers, and registerMsgHandlers functions for improved readability and logging.
server/v2/stf/stf_router.go - Renamed message registration methods in MsgRouterBuilder for clarity: RegisterGlobalPreHandler to RegisterGlobalPreMsgHandler, etc.

Possibly related PRs

Suggested labels

C:x/bank, C:server/v2 cometbft, C:indexer/postgres, C:x/bank/v2


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 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
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.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e9d72f5 and ee88f1d.

Files selected for processing (1)
  • core/appmodule/v2/handlers.go (2 hunks)
Additional context used
Path-based instructions (1)
core/appmodule/v2/handlers.go (1)

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

Additional comments not posted (8)
core/appmodule/v2/handlers.go (8)

24-24: Refactor: Simplified handler registration.

The change to RegisterPreHandler by removing the message name simplifies the interface and reduces the dependency on specific message names at registration time. This aligns with the PR's goal to abstract away details like message names from the core components.


49-61: Enhancement: Type-safe registration for PreMsgHandlers.

The introduction of RegisterMsgPreHandler using generics enhances type safety by ensuring that the handler is associated with the correct message type at compile time. This prevents runtime type errors and improves the robustness of the message handling system.


65-65: Refactor: Streamlined MsgRouter interface.

The MsgRouter interface has been simplified by changing Register to RegisterMsgHandler, which now directly accepts a handler without a message name. This change supports the PR's objective to internalize message name handling and reduce external dependencies.


87-99: Enhancement: Type-safe registration for MsgHandlers.

RegisterMsgHandler function utilizes generics to ensure that handlers are correctly typed, enhancing the safety and maintainability of the code. This approach also simplifies the registration process by abstracting away the need to manually handle type assertions.


105-105: Refactor: Simplified PostMsgRouter interface.

The modification to RegisterPostMsgHandler by removing the message name parameter simplifies the interface, making it consistent with other handler registration methods in this refactor.


129-145: Enhancement: Type-safe registration for PostMsgHandlers.

The RegisterPostMsgHandler function introduces type safety for post-message handling, ensuring that both the request and response types are correctly handled. This method prevents type mismatches and enhances the reliability of the post-processing logic.


149-149: Refactor: Streamlined QueryRouter interface.

The change to RegisterQueryHandler by removing the query name parameter aligns with the overall PR goal of simplifying and internalizing the handling of names within the system, thus reducing potential errors and dependencies.


171-183: Enhancement: Type-safe registration for QueryHandlers.

The introduction of RegisterQueryHandler using generics ensures that query handlers are type-safe, reducing the risk of runtime errors due to type mismatches. This change improves the maintainability and robustness of the query handling system.

@julienrbrt julienrbrt marked this pull request as draft September 6, 2024 13:07
@github-actions github-actions bot added C:server/v2 Issues related to server/v2 C:server/v2 stf labels Sep 6, 2024
server/v2/stf/stf_router.go Fixed Show fixed Hide fixed
server/v2/stf/stf_router.go Fixed Show fixed Hide fixed
@julienrbrt julienrbrt marked this pull request as ready for review September 6, 2024 14:42
Copy link
Contributor

@hieuvubk hieuvubk left a comment

Choose a reason for hiding this comment

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

LGTM
One question: We gonna put DenomInterface logic under the Handlers right?

core/appmodule/v2/handlers.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: 0

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ee88f1d and 1d89400.

Files selected for processing (3)
  • core/appmodule/v2/handlers.go (2 hunks)
  • runtime/v2/manager.go (3 hunks)
  • server/v2/stf/stf_router.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • server/v2/stf/stf_router.go
Additional context used
Path-based instructions (2)
core/appmodule/v2/handlers.go (1)

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

runtime/v2/manager.go (1)

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

Additional comments not posted (9)
core/appmodule/v2/handlers.go (6)

20-27: Interface update enhances clarity.

The renaming of RegisterPreHandler to RegisterPreMsgHandler in the PreMsgRouter interface improves clarity and consistency with other naming conventions in the system.


35-62: Excellent use of generics and documentation.

The RegisterMsgPreHandler function effectively uses generics to ensure type safety, which is crucial for preventing runtime type errors. The inclusion of a detailed example in the comments is helpful for developers implementing this function.


65-71: Interface update enhances clarity.

The renaming of RegisterPostHandler to RegisterPostMsgHandler in the PostMsgRouter interface improves clarity and consistency with other naming conventions in the system.


79-110: Excellent use of generics and documentation.

The RegisterPostMsgHandler function effectively uses generics to ensure type safety, which is crucial for preventing runtime type errors. The inclusion of a detailed example in the comments is helpful for developers implementing this function.


113-116: Interface update enhances clarity.

The renaming of Register to RegisterHandler in the MsgRouter interface improves clarity and consistency with other naming conventions in the system.


131-167: Excellent use of generics and documentation.

The RegisterHandler function effectively uses generics to ensure type safety, which is crucial for preventing runtime type errors. The inclusion of a detailed example in the comments is helpful for developers implementing this function.

runtime/v2/manager.go (3)

607-614: Improved error handling and logging.

The refactoring of error handling in the registerServices function enhances readability and provides more context in the error messages, which aids in debugging.


662-662: Enhanced error messaging for clarity.

The inclusion of the service name and method name in the error messages within the registerQueryHandlers function provides clearer context, which is beneficial for debugging.


683-683: Enhanced error messaging for clarity.

The inclusion of the service name and method name in the error messages within the registerMsgHandlers function provides clearer context, which is beneficial for debugging.

// PreMsgRouter. Example usage:
// ```go
//
// func (h Handlers) BeforeSend(ctx context.Context, req *types.MsgSend) (*types.QueryBalanceResponse, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Response type here is invalid

// }
//
// func (m Module) RegisterPreMsgHandlers(router appmodule.PreMsgRouter) {
// handlers := keeper.NewHandlers(m.keeper)
Copy link
Contributor

Choose a reason for hiding this comment

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

a way to use it. it's to simply define before send on the keeper and handle it via keeper, if we wanto to do it msg server style and have a wrapper like handlers then that is also fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like to think of the keeper as something only for state (basically, just holding public/private collections), and the business logic is somewhere else.

Copy link
Contributor

@testinginprod testinginprod left a comment

Choose a reason for hiding this comment

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

left few nits but looks good

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.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1d89400 and da06273.

Files selected for processing (1)
  • core/appmodule/v2/handlers.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • core/appmodule/v2/handlers.go

@julienrbrt julienrbrt added this pull request to the merge queue Sep 9, 2024
Merged via the queue into main with commit eeeb5b8 Sep 9, 2024
72 of 73 checks passed
@julienrbrt julienrbrt deleted the julien/core-handlers branch September 9, 2024 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:server/v2 stf C:server/v2 Issues related to server/v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants