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(server): Allow calling back into the application struct in PostSetup. #19455

Merged
merged 9 commits into from
Feb 26, 2024

Conversation

itsdevbear
Copy link
Contributor

@itsdevbear itsdevbear commented Feb 16, 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
  • 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.

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
    • Enhanced extensibility across the platform by introducing generic types for application structures, improving type safety and reusability.
    • Improved configuration handling within test functions.
  • Refactor
    • Updated various function signatures to include generic types, streamlining future developments and extensions.
  • Documentation
    • Updated CHANGELOG to document the new callback feature in the server module's PostSetup.

@itsdevbear itsdevbear requested a review from a team as a code owner February 16, 2024 17:30
Copy link
Contributor

coderabbitai bot commented Feb 16, 2024

Walkthrough

Walkthrough

The update introduces generics to enhance type safety and extensibility across various components of the codebase. It applies a generic type T to function signatures and structures related to application setup, server commands, and snapshot handling. This change aims to make the code more flexible and reusable by allowing these components to work with any type that satisfies the required interface, thereby improving the overall architecture and maintainability.

Changes

Files Change Summary
client/pruning/main.go Added types import; modified Cmd function to include generic type T.
server/cmt_cmds.go, server/rollback.go Updated function signatures to include generic type T for types.Application.
server/start.go Introduced generic type T to StartCmdOptions and related functions for enhanced extensibility.
server/types/app.go Replaced AppCreator function signature with a generic type T.
server/util.go, server/util_test.go Utilized generic type T for appCreator parameter; updated imports and function calls accordingly.
CHANGELOG.md Documented the ability for application struct callbacks in PostSetup within the server module.
client/snapshot/cmd.go, client/snapshot/export.go, client/snapshot/restore.go Updated function signatures to include a generic type T for servertypes.Application.

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-tests for this file.
  • 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 tests 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 from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

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

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@github-actions github-actions bot added the C:CLI label Feb 16, 2024
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.

Review Status

Actionable comments generated: 3

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 43df8d8 and b6fc778.
Files selected for processing (10)
  • client/pruning/main.go (2 hunks)
  • client/snapshot/cmd.go (1 hunks)
  • client/snapshot/export.go (1 hunks)
  • client/snapshot/restore.go (1 hunks)
  • server/cmt_cmds.go (1 hunks)
  • server/rollback.go (1 hunks)
  • server/start.go (10 hunks)
  • server/types/app.go (1 hunks)
  • server/util.go (2 hunks)
  • server/util_test.go (9 hunks)
Additional comments: 24
client/snapshot/cmd.go (1)
  • 11-11: The introduction of the generic type T for types.Application in the Cmd function signature is a significant improvement, enhancing type safety and flexibility. This change allows for more generic and reusable code structures, aligning with the PR's objectives to improve the flexibility and extensibility of the application's initialization process.
client/snapshot/restore.go (1)
  • 18-18: The update to include a generic type T in the RestoreSnapshotCmd function signature is well-implemented, enhancing type safety and flexibility. This change is consistent with the PR's goal of making the application's setup process more adaptable and promotes better code reusability.
client/snapshot/export.go (1)
  • 14-14: The modification to include a generic type T in the ExportSnapshotCmd function signature is correctly implemented, enhancing type safety and flexibility. This change aligns with the PR's objectives and promotes better code reusability and adaptability.
server/rollback.go (1)
  • 13-13: The update to include a generic type T in the NewRollbackCmd function signature is well-implemented, enhancing type safety and flexibility. This change is consistent with the PR's goal of making the application's setup process more adaptable and promotes better code reusability.
server/types/app.go (1)
  • 69-69: The modification to include a generic type T in the AppCreator type declaration is correctly implemented, enhancing type safety and flexibility. This change aligns with the PR's objectives and promotes better code reusability and adaptability in the application's initialization process.
client/pruning/main.go (1)
  • 26-26: The update to include a generic type T in the Cmd function signature for pruning is well-implemented, enhancing type safety and flexibility. This change is consistent with the PR's goal of making the application's setup process more adaptable and promotes better code reusability.
server/cmt_cmds.go (1)
  • 359-359: The introduction of the generic type T in the BootstrapStateCmd function signature is a significant improvement, enhancing type safety and flexibility. This change allows for more generic and reusable code structures, aligning with the PR's objectives to improve the flexibility and extensibility of the application's initialization process.
server/util_test.go (8)
  • 54-54: The usage of server.StartCmd[types.Application](nil) introduces a generic type for types.Application, enhancing type safety and flexibility. This change aligns with the PR's objective to improve the server component's extensibility. Ensure that the nil argument passed here is intended and that any required initialization for the Application type is handled appropriately elsewhere in the codebase.
  • 133-133: Similar to the previous comment, the usage of server.StartCmd[types.Application](nil) here is consistent with the PR's goal. It's crucial to verify that passing nil as the argument does not lead to unintended behavior during the execution of these tests.
  • 176-176: Again, the use of server.StartCmd[types.Application](nil) is observed. As before, ensure that this usage is consistent with the intended design and that the nil argument does not cause issues.
  • 198-198: The repeated use of server.StartCmd[types.Application](nil) in test functions demonstrates a consistent approach to utilizing generic types across the test suite. It's important to maintain this consistency and ensure that the nil argument is appropriate for the tests' context.
  • 228-228: The usage of server.StartCmd[types.Application](nil) here is in line with the PR's objectives. As with previous instances, confirm that the nil argument is suitable and does not compromise the tests' integrity.
  • 318-318: The introduction of server.StartCmd[types.Application](nil) in the newPrecedenceCommon function further emphasizes the PR's focus on enhancing type safety and flexibility through generics. This consistent application of generics across the test suite is commendable.
  • 435-435: The use of server.StartCmd[types.Application](nil) in this context is consistent with the pattern observed throughout the file. As always, ensure that the implications of the nil argument are fully considered and that it aligns with the test's requirements.
  • 475-475: The final observed usage of server.StartCmd[types.Application](nil) in this file. This change is part of the broader effort to introduce generics and enhance the flexibility of the server component. As with other instances, the use of nil should be carefully evaluated to ensure it meets the test's needs.
server/util.go (2)
  • 327-327: The introduction of a generic type T in the AddCommands function signature enhances type safety and flexibility. This change allows the function to accept any type that implements the types.Application interface, making the code more reusable and adaptable to different application types. The implementation appears correct and follows best practices for generic programming in Go.
  • 358-358: Similarly, the use of a generic type T in the AddTestnetCreatorCommand function signature is a positive change. It increases the function's flexibility by allowing it to handle various types of AppCreator, which is beneficial for type safety and code reusability. The modification is implemented correctly and aligns with best practices for leveraging generics in Go.
server/start.go (7)
  • 111-119: The introduction of the generic type T in StartCmdOptions is a significant enhancement for flexibility. However, ensure that all implementations using StartCmdOptions are updated to leverage generics properly. This change aligns well with the PR's goal of enhancing the server component's extensibility.
  • 126-127: The StartCmd function now accepts a generic type, which is a good use of Go's type parameters feature introduced in Go 1.18. This change allows for more type-safe and flexible application structures.
  • 132-132: In StartCmdWithOptions, the check for nil DBOpener and its default assignment is a good practice. It ensures that there's always a database opener function available, preventing potential nil pointer dereferences.
  • 221-232: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [229-292]

The separation of startStandAlone and startInProcess functions for different modes of operation is a good architectural decision. It enhances readability and maintainability. Ensure that all new configurations and flags introduced are documented and tested.

  • 339-339: The conditional execution of PostSetup in startInProcess is a crucial addition for extending the initialization process. It's important to ensure that all implementations of PostSetup handle errors gracefully and do not leave the application in an inconsistent state if an error occurs.
  • 600-608: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [590-605]

The startApp function's adaptation to support generics and the introduction of testnetify logic are significant changes. Ensure comprehensive testing around these changes, especially the testnetify logic, to prevent any unintended side effects on the application's state.

  • 936-936: Adding start node flags with the introduction of generics is handled well. It's important to ensure that all new flags introduced are documented and that their usage is clear to end-users.

@@ -20,6 +20,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/server/types"
Copy link
Contributor

@coderabbitai coderabbitai bot Feb 16, 2024

Choose a reason for hiding this comment

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

The import statement for types is duplicated as both github.com/cosmos/cosmos-sdk/server/types and servertypes "github.com/cosmos/cosmos-sdk/server/types". This redundancy could be eliminated to improve code clarity and maintainability.

-	"github.com/cosmos/cosmos-sdk/server/types"

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
"github.com/cosmos/cosmos-sdk/server/types"

Copy link
Member

Choose a reason for hiding this comment

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

Good point from the bot

Copy link
Contributor

Choose a reason for hiding this comment

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

@julienrbrt: Thank you for acknowledging the point! If there's anything more I can help with, please let me know.

\ (^-^) /

Comment on lines 201 to 213
return cmd
}

func start(svrCtx *Context, clientCtx client.Context, appCreator types.AppCreator, withCmt bool, opts StartCmdOptions) error {
func start[T types.Application](svrCtx *Context, clientCtx client.Context, appCreator types.AppCreator[T], withCmt bool, opts StartCmdOptions[T]) error {
svrCfg, err := getAndValidateConfig(svrCtx)
if err != nil {
return err
}

app, appCleanupFn, err := startApp(svrCtx, appCreator, opts)
app, appCleanupFn, err := startApp[T](svrCtx, appCreator, opts)
if err != nil {
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [204-226]

The start function's adaptation to use generics is well-implemented. However, consider adding error handling for the startStandAlone and startInProcess calls to ensure any errors during the startup process are appropriately managed.

- return startStandAlone[T](svrCtx, svrCfg, clientCtx, app, metrics, opts)
+ if err := startStandAlone[T](svrCtx, svrCfg, clientCtx, app, metrics, opts); err != nil {
+     return err
+ }

- return startInProcess[T](svrCtx, svrCfg, clientCtx, app, metrics, opts)
+ if err := startInProcess[T](svrCtx, svrCfg, clientCtx, app, metrics, opts); err != nil {
+     return err
+ }

Comment on lines 715 to 721

// testnetify modifies both state and blockStore, allowing the provided operator address and local validator key to control the network
// that the state in the data folder represents. The chainID of the local genesis file is modified to match the provided chainID.
func testnetify(ctx *Context, home string, testnetAppCreator types.AppCreator, db dbm.DB, traceWriter io.WriteCloser) (types.Application, error) {
func testnetify[T types.Application](ctx *Context, home string, testnetAppCreator types.AppCreator[T], db dbm.DB, traceWriter io.WriteCloser) (*T, error) {
config := ctx.Config

newChainID, ok := ctx.Viper.Get(KeyNewChainID).(string)
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [625-932]

The InPlaceTestnetCreator and testnetify functions introduce complex logic for creating a testnet from the current state. It's crucial to ensure that these functions are well-documented and that their impact on the application's state is clearly understood by developers. Consider adding more inline comments explaining the steps involved, especially in the testnetify function, to improve maintainability.

+ // Explanation of the logic and its impact on the application's state

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

utACK!

nit: changelog entry

Curious, how do you plan on using post-setup? What do you plan on executing in that handler?

server/start.go Fixed 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.

Review Status

Actionable comments generated: 1

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between b6fc778 and 1a0ec34.
Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • client/pruning/main.go (1 hunks)
  • server/start.go (11 hunks)
Files skipped from review as they are similar to previous changes (2)
  • client/pruning/main.go
  • server/start.go

CHANGELOG.md Outdated Show resolved Hide resolved
@@ -271,6 +273,12 @@ func startStandAlone(svrCtx *Context, svrCfg serverconfig.Config, clientCtx clie
return err
}

if opts.PostSetup != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if opts.PostSetup != nil {
if opts.PostSetupStandalone != nil {

Copy link
Member

Choose a reason for hiding this comment

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

gently ping on this one

Copy link
Member

Choose a reason for hiding this comment

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

@itsdevbear, it should still be committed as you've forced pushed your last commit, which removed this.

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.

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 1a0ec34 and a05b089.
Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • server/start.go (11 hunks)
Files skipped from review as they are similar to previous changes (2)
  • CHANGELOG.md
  • server/start.go

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.

utACK.
Linter won't be happy unless you remove those duplicated imports tho.

@itsdevbear
Copy link
Contributor Author

fixed linter

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.

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 1a0ec34 and babd6c7.
Files selected for processing (5)
  • CHANGELOG.md (1 hunks)
  • client/snapshot/cmd.go (1 hunks)
  • client/snapshot/export.go (1 hunks)
  • client/snapshot/restore.go (1 hunks)
  • server/start.go (11 hunks)
Files skipped from review as they are similar to previous changes (5)
  • CHANGELOG.md
  • client/snapshot/cmd.go
  • client/snapshot/export.go
  • client/snapshot/restore.go
  • server/start.go

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.

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 1a0ec34 and ca37db5.
Files selected for processing (6)
  • CHANGELOG.md (1 hunks)
  • client/snapshot/cmd.go (1 hunks)
  • client/snapshot/export.go (1 hunks)
  • client/snapshot/restore.go (1 hunks)
  • server/start.go (11 hunks)
  • server/util_test.go (8 hunks)
Files skipped from review as they are similar to previous changes (6)
  • CHANGELOG.md
  • client/snapshot/cmd.go
  • client/snapshot/export.go
  • client/snapshot/restore.go
  • server/start.go
  • server/util_test.go

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.

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between ca37db5 and 74f2d5e.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.md

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.

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 74f2d5e and aed949b.
Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • server/start.go (11 hunks)
  • server/util_test.go (8 hunks)
Files skipped from review as they are similar to previous changes (3)
  • CHANGELOG.md
  • server/start.go
  • server/util_test.go

@tac0turtle tac0turtle added this pull request to the merge queue Feb 26, 2024
Merged via the queue into cosmos:main with commit 23359a0 Feb 26, 2024
57 of 59 checks passed
@tac0turtle tac0turtle deleted the app-in-post-setup branch February 26, 2024 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants