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(stf): remove RunWithCtx #21739

Merged
merged 19 commits into from
Sep 19, 2024
Merged

refactor(stf): remove RunWithCtx #21739

merged 19 commits into from
Sep 19, 2024

Conversation

kocubinski
Copy link
Member

@kocubinski kocubinski commented Sep 15, 2024

Description

This resolves a TODO by removing stf.RunWithCtx. Instead some indirection with services is used to create a bespoke genesis context (with support for fewer services).

Tested so far with:

simdv2 start # from genesis
simdv2 genesis export

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

    • Enhanced the InitGenesis function to provide a detailed state representation alongside error reporting.
    • Integrated default service bindings into the application's dependency injection configuration for improved service management.
  • Bug Fixes

    • Removed the RunWithCtx function to simplify the STF[T] struct interface and reduce potential confusion.
  • Chores

    • Redirected module paths to local versions for easier development and testing.

Copy link
Contributor

coderabbitai bot commented Sep 15, 2024

Walkthrough

Walkthrough

The changes involve modifying the InitGenesis function in abci_test.go to return both a store.WriterMap and an error, enhancing the handling of store state during initialization. The NewSimApp function in app_di.go has been updated to include runtime.DefaultServiceBindings(), improving service management. Additionally, the go.mod files in both simapp/v2 and runtime/v2 have been adjusted to replace the cosmossdk.io/core module with a local path, facilitating local development. The RunWithCtx function has been removed from the STF[T] struct, simplifying the interface.

Changes

File Change Summary
server/v2/cometbft/abci_test.go Modified InitGenesis function signature to return store.WriterMap and error, enhancing state management during genesis initialization.
simapp/v2/app_di.go Updated NewSimApp to include runtime.DefaultServiceBindings() for improved service management in the application's dependency injection configuration.
simapp/v2/go.mod, runtime/v2/go.mod Added a replacement for cosmossdk.io/core to a local path ../../core, enabling local development with a modified version of the core package.
server/v2/stf/stf.go Removed RunWithCtx function from STF[T] struct to simplify the interface and reduce potential confusion.

Possibly related PRs


Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e19452b and 284ba57.

Files ignored due to path filters (2)
  • runtime/v2/go.sum is excluded by !**/*.sum
  • simapp/v2/go.sum is excluded by !**/*.sum
Files selected for processing (3)
  • runtime/v2/go.mod (1 hunks)
  • server/v2/stf/stf.go (0 hunks)
  • simapp/v2/go.mod (1 hunks)
Files not reviewed due to no reviewable changes (1)
  • server/v2/stf/stf.go
Files skipped from review as they are similar to previous changes (2)
  • runtime/v2/go.mod
  • simapp/v2/go.mod

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.

runtime/v2/genesis.go Fixed Show fixed Hide fixed
runtime/v2/genesis.go Fixed Show fixed Hide fixed
runtime/v2/genesis.go Fixed Show fixed Hide fixed
runtime/v2/services/genesis.go Fixed Show fixed Hide fixed
runtime/v2/services/genesis.go Fixed Show fixed Hide fixed
runtime/v2/services/genesis.go Fixed Show fixed Hide fixed
@kocubinski kocubinski marked this pull request as ready for review September 16, 2024 14:41
return g.state, nil
}

type GenesisKVStoreServie struct {
Copy link
Member

Choose a reason for hiding this comment

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

typo.

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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f348d84 and 9fe1020.

Files ignored due to path filters (2)
  • runtime/v2/go.sum is excluded by !**/*.sum
  • simapp/v2/go.sum is excluded by !**/*.sum
Files selected for processing (13)
  • core/header/service.go (3 hunks)
  • core/store/service.go (2 hunks)
  • runtime/v2/builder.go (3 hunks)
  • runtime/v2/go.mod (1 hunks)
  • runtime/v2/module.go (5 hunks)
  • runtime/v2/services/genesis.go (1 hunks)
  • server/v2/appmanager/appmanager.go (3 hunks)
  • server/v2/appmanager/genesis.go (1 hunks)
  • server/v2/appmanager/types.go (0 hunks)
  • server/v2/stf/stf.go (0 hunks)
  • simapp/v2/app_di.go (1 hunks)
  • simapp/v2/go.mod (1 hunks)
  • simapp/v2/simdv2/cmd/root_di.go (1 hunks)
Files not reviewed due to no reviewable changes (2)
  • server/v2/appmanager/types.go
  • server/v2/stf/stf.go
Files skipped from review due to trivial changes (1)
  • core/header/service.go
Additional context used
Path-based instructions (8)
server/v2/appmanager/genesis.go (1)

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

core/store/service.go (1)

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

runtime/v2/services/genesis.go (1)

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

simapp/v2/simdv2/cmd/root_di.go (1)

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

server/v2/appmanager/appmanager.go (1)

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

simapp/v2/app_di.go (1)

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

runtime/v2/module.go (1)

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

runtime/v2/builder.go (1)

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

Additional comments not posted (26)
server/v2/appmanager/genesis.go (1)

14-27: LGTM!

The changes to the InitGenesis function signature and comments are well-aligned with the Uber Go Style Guide and improve the functionality and documentation of the function.

Please ensure that all callers of the InitGenesis function are updated to handle the returned store.WriterMap and error. You can use the following script to verify the usage of InitGenesis:

Verification successful

To properly verify the changes and their impact, we need to gather more specific information about the usage of this InitGenesis function. Let's start by searching for its direct callers.


Let's broaden our search to gather more information about the usage of InitGenesis and related concepts.


To further verify the changes and their impact, let's examine the AppManager's InitGenesis method more closely.


LGTM! The changes to InitGenesis are well-implemented and consistent.

The modifications to the InitGenesis function in server/v2/appmanager/genesis.go have been correctly implemented. The function now returns a store.WriterMap along with the *server.BlockResponse and error. This change is consistently applied in the AppManager's InitGenesis method.

Key points:

  1. The new signature is correctly used and propagated.
  2. Error handling is comprehensive throughout the implementation.
  3. The store.WriterMap (genesisState) is properly utilized to represent the genesis state.
  4. While there are no direct callers of appmanager.InitGenesis, it's being indirectly called through the CometBFT ABCI interface, maintaining compatibility.

The changes appear to be part of a larger refactoring to improve state management during genesis initialization. The implementation is sound and should integrate well with the rest of the system.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all callers of `InitGenesis` handle the returned `store.WriterMap` and error.

# Test: Search for the function usage. Expect: Callers should handle the returned values.
rg --type go -A 5 $'InitGenesis'

Length of output: 102909


Script:

#!/bin/bash
# Search for direct callers of InitGenesis in the appmanager package
rg --type go -A 5 "appmanager\.InitGenesis"

Length of output: 45


Script:

#!/bin/bash
# Search for any usage of InitGenesis in the appmanager package
echo "Searching for InitGenesis in appmanager package:"
rg --type go -A 5 "InitGenesis" server/v2/appmanager/

# Search for store.WriterMap usage
echo "\nSearching for store.WriterMap usage:"
rg --type go -A 5 "store\.WriterMap"

# Look for recent changes in error handling related to genesis initialization
echo "\nSearching for error handling changes in genesis initialization:"
rg --type go -A 5 "InitGenesis.*error"

Length of output: 35830


Script:

#!/bin/bash
# Check the AppManager's InitGenesis method
echo "Examining AppManager's InitGenesis method:"
rg --type go -A 20 "func \(a AppManager\[T\]\) InitGenesis" server/v2/appmanager/appmanager.go

# Look for potential indirect callers of InitGenesis
echo "\nSearching for potential indirect callers of InitGenesis:"
rg --type go -A 5 "InitGenesis\(" --glob !server/v2/appmanager/genesis.go

Length of output: 61017

core/store/service.go (2)

13-13: LGTM!

The new KVStoreServiceFactory function type provides a flexible way to create instances of KVStoreService based on a byte slice input. The naming and syntax are correct.


23-23: LGTM!

The new MemoryStoreServiceFactory function type provides a flexible way to create instances of MemoryStoreService based on a byte slice input. The naming and syntax are correct.

runtime/v2/services/genesis.go (5)

21-25: LGTM!

The NewGenesisContext function correctly initializes the genesisContext struct with the provided state.


27-37: LGTM!

The Run method correctly executes the provided function within the context of the genesisContext and handles the return values appropriately.


44-52: LGTM!

The NewGenesisKVService function correctly initializes the GenesisKVStoreServie struct with the provided actor and execution service.


76-82: LGTM!

The HeaderInfo method correctly returns header information based on the provided context. If the genesisContext is found, it returns an empty header.Info struct, otherwise, it delegates the call to the underlying executionService.


84-88: LGTM!

The NewGenesisHeaderService function correctly initializes the GenesisHeaderService struct with the provided execution service.

runtime/v2/go.mod (1)

8-8: LGTM!

The addition of the replacement directive for cosmossdk.io/core is a good change. It allows the module to be sourced from a local directory, which can facilitate local development and testing.

The change is unlikely to have any negative impact on the module's functionality and may improve the development workflow.

simapp/v2/simdv2/cmd/root_di.go (1)

40-40: LGTM! The addition of runtime.DefaultServiceBindings() enhances the dependency injection setup.

This change integrates default service bindings into the dependency injection configuration, which may streamline service management and improve the modularity and maintainability of the codebase. It aligns with the PR objective of refactoring the codebase.

Potential benefits:

  • Standardizes service instantiation and resolution
  • Improves modularity and maintainability
  • Simplifies the dependency injection setup
server/v2/appmanager/appmanager.go (4)

47-58: LGTM!

The changes to the InitGenesis method simplify the initialization process by directly calling initGenesis with the necessary parameters. The error handling for decoding the genesis transaction remains intact, ensuring that any issues during this process are still reported accurately. The changes enhance clarity and reduce complexity.


86-87: LGTM!

The changes to the ExportGenesis method reduce unnecessary complexity by directly checking if the exportGenesis function is set before attempting to call it. The error handling is straightforward, as it directly returns an error if the function is not defined. The changes improve the readability of the code.


90-90: LGTM!

The changes to the ExportGenesis method simplify the export process by directly returning the result of the exportGenesis call if the function is defined. The error handling is handled by the calling function. The changes enhance the maintainability and readability of the code.


159-161: LGTM!

The QueryWithState method provides a way to process a query with a temporary or uncommitted state, which can be useful in certain scenarios. The method uses the Query method of the stf field to execute the query, ensuring consistency with other query methods. The method is well-documented, explaining its purpose and use case.

simapp/v2/app_di.go (1)

74-74: LGTM!

The addition of runtime.DefaultServiceBindings() to the appConfig initialization process is a good change. It integrates default service bindings into the application's dependency injection configuration, which may enhance the application's ability to manage service dependencies more effectively.

runtime/v2/module.go (6)

179-181: LGTM!

The introduction of factory parameters for service instantiation enhances the modularity and flexibility of the ProvideEnvironment function. This change allows for easier testing and promotes better separation of concerns.


203-203: LGTM!

Instantiating the kvService using the kvFactory aligns with the factory-based approach and promotes flexibility and testability.


207-207: LGTM!

Instantiating the memKvService using the memFactory aligns with the factory-based approach and promotes flexibility and testability.


215-215: LGTM!

Instantiating the HeaderService using the headerFactory aligns with the factory-based approach and promotes flexibility and testability.


226-227: LGTM!

Renaming the wrapper parameter to builder improves the clarity of the parameter's purpose and enhances code readability.


240-259: LGTM!

The introduction of the DefaultServiceBindings function streamlines the service binding process and enhances the clarity of service dependencies within the application. This change aligns with the overall refactoring goal of improving modularity and configurability.

runtime/v2/builder.go (4)

162-193: Excellent enhancements to the InitGenesis function!

The changes significantly improve the genesis initialization process:

  • The state check ensures genesis is only initialized on a zero state, preventing inconsistencies.
  • The use of GenesisContext provides a more structured approach to handling the genesis state.
  • The improved error handling provides clearer error reporting for various failure points.

Great work on enhancing the robustness and clarity of the genesis initialization process!


196-199: Good addition of the state check in the ExportGenesis function.

The state check ensures that the latest state is retrieved before attempting to export the genesis data. This change aligns well with the modifications made to the InitGenesis function and enhances the consistency and reliability of the genesis export process.


200-206: Good use of GenesisContext in the ExportGenesis function.

The utilization of GenesisContext allows for a more organized execution of the export logic. This change aligns well with the modifications made to the InitGenesis function and provides a consistent and structured approach to handling the genesis state during both initialization and export processes.


6-6: LGTM!

The import of the errors package aligns with the improved error handling in the InitGenesis function and does not introduce any issues.

simapp/v2/go.mod (1)

289-289: LGTM! The new replacement directive for cosmossdk.io/core facilitates local development.

The addition of the replacement directive for cosmossdk.io/core allows the simapp/v2 module to utilize a local version of the core package instead of fetching it from the original repository. This change facilitates local development and testing by enabling developers to work with a modified version of the core package without needing to publish changes to a remote repository.

The other existing replacements remain unchanged, indicating that the integration with other parts of the Cosmos SDK ecosystem continues as before.

runtime/v2/services/genesis.go Outdated Show resolved Hide resolved
runtime/v2/services/genesis.go Dismissed Show dismissed Hide dismissed
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)
server/v2/cometbft/abci_test.go (1)

689-690: Add a TODO comment to track the missing implementation.

Since the actual genesis initialization logic is missing, consider adding a TODO comment to track this pending work:

InitGenesis: func(ctx context.Context, src io.Reader, txHandler func(json.RawMessage) error) (store.WriterMap, error) {
    // TODO: Implement the genesis initialization logic
    return nil, nil 
},
Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 57ec239 and c20649d.

Files selected for processing (1)
  • server/v2/cometbft/abci_test.go (1 hunks)
Additional context used
Path-based instructions (1)
server/v2/cometbft/abci_test.go (2)

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


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

server/v2/cometbft/abci_test.go Outdated Show resolved Hide resolved
runtime/v2/services/genesis.go Dismissed Show dismissed Hide dismissed
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.

ACK!

Can we check if we can make genesis export concurrent with this method? (Basically checking if the module manager changes from #21554 are working out of the box with this)

@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Sep 16, 2024
@julienrbrt
Copy link
Member

Because of the CometBFT server changes and simapp/v2 changes adding the backport label

@julienrbrt julienrbrt self-requested a review September 16, 2024 19:39
},
ExportGenesis: func(ctx context.Context, version uint64) ([]byte, error) {
genesisJson, err := a.app.moduleManager.ExportGenesisForModules(ctx)
_, state, err := a.app.db.StateLatest()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_, state, err := a.app.db.StateLatest()
state, err := a.app.db.StateAt(version)

Copy link
Member

Choose a reason for hiding this comment

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

This looked valid @kocubinski btw.

@akhilkumarpilli
Copy link
Contributor

@kocubinski, I attempted to use genesisCtx instead of RunWithCtx, basing your changes on my branch from this PR to enable concurrent export of genesis. However, I encountered the concurrent map write issue again.

@kocubinski
Copy link
Member Author

@kocubinski, I attempted to use genesisCtx instead of RunWithCtx, basing your changes on my branch from this PR to enable concurrent export of genesis. However, I encountered the concurrent map write issue again.

Can you share a link to the code you ran and tested? I see #21554 is still using stf.RunWithCtx

@kocubinski kocubinski added this pull request to the merge queue Sep 19, 2024
Merged via the queue into main with commit d6364b8 Sep 19, 2024
74 checks passed
@kocubinski kocubinski deleted the kocu/rm-stf-run-with-ctx branch September 19, 2024 21:45
mergify bot pushed a commit that referenced this pull request Sep 19, 2024
(cherry picked from commit d6364b8)

# Conflicts:
#	core/header/service.go
#	core/store/service.go
#	runtime/v2/builder.go
#	runtime/v2/go.mod
#	runtime/v2/go.sum
#	runtime/v2/module.go
#	server/v2/appmanager/appmanager.go
#	server/v2/appmanager/genesis.go
#	server/v2/appmanager/types.go
#	server/v2/stf/stf.go
#	simapp/v2/go.mod
#	simapp/v2/go.sum
julienrbrt added a commit that referenced this pull request Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:server/v2 appmanager C:server/v2 cometbft 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