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

fix(runtime/v2): return genesis val updates #22729

Merged
merged 7 commits into from
Dec 3, 2024
Merged

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Dec 3, 2024

Description

Closes: #21907

Tested locally and Import/Export works.


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 genesis initialization process to include validator updates.
    • Added interface implementation for handling ABCI genesis functionality.
  • Bug Fixes

    • Improved error handling related to validator updates during genesis initialization.
  • Tests

    • Updated tests to accommodate changes in function signatures and ensure expected behavior with new return types.

Copy link
Contributor

coderabbitai bot commented Dec 3, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request involve modifications to several methods related to the genesis initialization process in the application. Key updates include altering method signatures to return additional information, specifically a slice of ValidatorUpdate, alongside existing return types. The error handling logic has been enhanced to accommodate these changes, ensuring that validator updates are correctly managed during the initialization and end-block processes. Additionally, an interface implementation has been added to the AppModule struct to support ABCI genesis functionality.

Changes

File Change Summary
runtime/v2/builder.go Updated initGenesis method signature to return []appmodule.ValidatorUpdate along with existing types. Adjusted internal logic for error handling and populating validator updates.
runtime/v2/manager.go Modified InitGenesisJSON method to return []appmodulev2.ValidatorUpdate and updated error handling. Adjusted logic for managing validator updates.
server/v2/appmanager/appmanager.go Changed InitGenesis method signature to include valUpdates. Enhanced error handling for validator updates. Added comments for clarity.
server/v2/appmanager/genesis.go Updated InitGenesis function signature to return []appmodulev2.ValidatorUpdate.
server/v2/cometbft/abci_test.go Modified callback function signature in setUpConsensus to return []appmodulev2.ValidatorUpdate. Adjusted related methods for new return type.
x/genutil/module.go Added implementation of appmodulev2.HasABCIGenesis interface to AppModule.

Assessment against linked issues

Objective Addressed Explanation
Ensure initGenesis returns validator updates (#21907)
Improve error handling during genesis initialization (#21907)

Possibly related PRs

Suggested reviewers

  • facundomedica
  • kocubinski
  • sontrinh16
  • testinginprod
  • akhilkumarpilli

📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1489945 and 9bdfa7a.

📒 Files selected for processing (2)
  • runtime/v2/builder.go (1 hunks)
  • server/v2/cometbft/abci_test.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
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"

runtime/v2/builder.go (1)

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

🔇 Additional comments (4)
runtime/v2/builder.go (3)

131-131: LGTM: Function signature updated to handle validator updates

The change to return []appmodule.ValidatorUpdate aligns with the PR's objective to fix the validator set initialization issue.


135-135: LGTM: Error handling properly updated

Error handling has been correctly updated to return nil validator updates in error cases.

Also applies to: 140-140, 145-148


151-157: LGTM: Validator updates properly captured and returned

The implementation correctly captures and returns validator updates from InitGenesisJSON, addressing the core issue of empty validator sets after genesis initialization.

Also applies to: 161-161

server/v2/cometbft/abci_test.go (1)

704-707: ⚠️ Potential issue

Add test coverage for validator updates during genesis initialization.

The test implementation needs to be enhanced to verify the validator set initialization fix.

Apply this diff to add proper test coverage:

 func(ctx context.Context, src io.Reader, txHandler func(json.RawMessage) error) (store.WriterMap, []appmodulev2.ValidatorUpdate, error) {
     _, st, err := mockStore.StateLatest()
     require.NoError(t, err)
-    return branch.DefaultNewWriterMap(st), nil, nil
+    // Test with sample validator updates
+    validatorUpdates := []appmodulev2.ValidatorUpdate{
+        {
+            PubKey:  []byte("validator1"),
+            Power:   100,
+            Address: []byte("address1"),
+        },
+    }
+    return branch.DefaultNewWriterMap(st), validatorUpdates, nil
 }

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.

@github-actions github-actions bot added the C:x/genutil genutil module issues label Dec 3, 2024
@julienrbrt julienrbrt added backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release and removed C:x/genutil genutil module issues labels Dec 3, 2024
@julienrbrt julienrbrt marked this pull request as ready for review December 3, 2024 14:02
Copy link
Contributor

github-actions bot commented Dec 3, 2024

@julienrbrt your pull request is missing a changelog!

@github-actions github-actions bot added the C:x/genutil genutil module issues label Dec 3, 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.

Actionable comments posted: 3

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 538e1d1 and 4f611a2.

📒 Files selected for processing (6)
  • runtime/v2/builder.go (1 hunks)
  • runtime/v2/manager.go (2 hunks)
  • server/v2/appmanager/appmanager.go (3 hunks)
  • server/v2/appmanager/genesis.go (2 hunks)
  • server/v2/cometbft/abci_test.go (1 hunks)
  • x/genutil/module.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
server/v2/appmanager/genesis.go (1)

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

x/genutil/module.go (1)

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

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/appmanager/appmanager.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.

runtime/v2/manager.go (1)

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

🔇 Additional comments (5)
server/v2/appmanager/genesis.go (1)

25-25: Verify all callers handle the new return value in InitGenesis

The function signature of InitGenesis has been updated to return an additional value []appmodulev2.ValidatorUpdate. Ensure that all callers of this function have been updated to handle the new return value to prevent runtime errors.

Run the following script to find all usages of InitGenesis and check if they handle the new return value:

runtime/v2/builder.go (1)

131-131: Verify all callers handle the new return value in initGenesis

The function initGenesis now returns an additional value []appmodule.ValidatorUpdate. Ensure that all callers of this function are updated to handle the new return value to prevent runtime errors.

Run the following script to find all usages of initGenesis and check if they handle the new return value:

✅ Verification successful

Let me try a different approach to verify the callers of initGenesis.


Let me check the actual implementation of the caller to verify how it handles the return values.


All callers properly handle the new return value

The verification shows that there is only one caller of the initGenesis method in server/v2/appmanager/appmanager.go, and it correctly handles all three return values: genesisState, valUpdates, err := a.initGenesis(...). The other matches in the search results were function definitions, not calls. Therefore, no updates are needed in the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find function calls to `initGenesis` and verify the number of return variables.

# Search for function calls to `initGenesis` where only two return values are expected
ast-grep --lang go '$_ := $_.initGenesis($_, $_, $_);' \
  | grep -v '_, _, err :=' \
  | grep -v '_, err :=' \
  || echo "All callers handle the new return value."

Length of output: 310


Script:

#!/bin/bash
# Search for all occurrences of initGenesis function calls
rg -U "initGenesis\([^)]+\)" -A 2

Length of output: 1132


Script:

#!/bin/bash
# Get the context around the initGenesis call in appmanager.go
rg -U -B 2 -A 5 "genesisState, valUpdates, err := a\.initGenesis\(" server/v2/appmanager/appmanager.go

Length of output: 372

server/v2/cometbft/abci_test.go (1)

705-708: 🛠️ Refactor suggestion

Verify validator updates in InitChain tests.

The existing TestConsensus_InitChain_* test cases don't verify the handling of validator updates returned during genesis initialization.

Let's verify if there are any other test files covering this functionality:

Consider adding a new test case:

func TestConsensus_InitChain_With_ValidatorUpdates(t *testing.T) {
    c := setUpConsensus(t, 100_000, mempool.NoOpMempool[mock.Tx]{})
    
    resp, err := c.InitChain(context.Background(), &abciproto.InitChainRequest{
        Time:          time.Now(),
        ChainId:       "test",
        InitialHeight: 1,
    })
    require.NoError(t, err)
    require.NotNil(t, resp.Validators)
    require.NotEmpty(t, resp.Validators)
}
x/genutil/module.go (2)

Line range hint 1-150: Implementation looks good!

The existing code structure properly supports the new HasABCIGenesis interface implementation. The genesis-related methods, particularly InitGenesis, already handle validator updates correctly, which aligns well with the PR's objective of fixing validator set initialization.


22-22: Verify HasABCIGenesis interface implementation.

The addition of the HasABCIGenesis interface implementation aligns with the PR objectives to fix validator set initialization. Let's verify that all required methods are properly implemented.

✅ Verification successful

HasABCIGenesis interface implementation is correctly implemented

The AppModule type correctly implements all required methods of the HasABCIGenesis interface:

  • DefaultGenesis(): Returns default genesis state
  • ValidateGenesis(data json.RawMessage): Validates genesis state
  • InitGenesis(ctx context.Context, data json.RawMessage): Initializes genesis state
  • ExportGenesis(ctx context.Context): Exports genesis state
  • Base AppModule interface: Implemented through interface composition
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that AppModule implements all required methods of HasABCIGenesis

# Search for the HasABCIGenesis interface definition
ast-grep --pattern 'type HasABCIGenesis interface {
  $$$
}'

# Search for the corresponding method implementations in AppModule
ast-grep --pattern 'func (am AppModule) $_($$$) $$ {
  $$$
}'

Length of output: 651


Script:

#!/bin/bash
# Search for the required method implementations in the genutil module
rg -A 5 "func \(am AppModule\) (DefaultGenesis|ValidateGenesis|InitGenesis|ExportGenesis)" x/genutil/

# Also check if AppModule implements the base AppModule interface
ast-grep --pattern 'type AppModule interface {
  $$$
}'

Length of output: 2245

runtime/v2/manager.go Outdated Show resolved Hide resolved
server/v2/appmanager/appmanager.go Show resolved Hide resolved
server/v2/cometbft/abci_test.go 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: 2

🧹 Outside diff range and nitpick comments (1)
server/v2/appmanager/appmanager.go (1)

149-150: Improve error message for clarity

The error message when both valUpdates and blockResponse.ValidatorUpdates are set could be more descriptive.

Consider rephrasing the error message:

- return nil, nil, errors.New("validator updates returned from InitGenesis and genesis transactions, only one can be used")
+ return nil, nil, errors.New("conflict in validator updates: both InitGenesis and genesis transactions provided validator updates; only one source is allowed")
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 538e1d1 and 4f611a2.

📒 Files selected for processing (6)
  • runtime/v2/builder.go (1 hunks)
  • runtime/v2/manager.go (2 hunks)
  • server/v2/appmanager/appmanager.go (3 hunks)
  • server/v2/appmanager/genesis.go (2 hunks)
  • server/v2/cometbft/abci_test.go (1 hunks)
  • x/genutil/module.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
server/v2/appmanager/genesis.go (1)

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

x/genutil/module.go (1)

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

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"

runtime/v2/manager.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.

runtime/v2/builder.go (1)

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

🔇 Additional comments (5)
runtime/v2/manager.go (1)

Line range hint 144-194: Overall, the changes in InitGenesisJSON look good

The function signature update and the handling of validator updates are correctly implemented.

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

25-25: Function signature updated correctly

The InitGenesis function signature has been updated appropriately to include the []appmodulev2.ValidatorUpdate return value.

x/genutil/module.go (1)

22-22: Implemented appmodulev2.HasABCIGenesis interface correctly

The AppModule struct now correctly implements the HasABCIGenesis interface.

runtime/v2/builder.go (1)

131-160: Updated initGenesis method to include validator updates

The initGenesis method signature and return statements have been updated appropriately to include []appmodule.ValidatorUpdate. Error handling has been adjusted to ensure consistency.

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

145-155: Validator updates handling logic looks correct

The logic for handling validator updates from InitGenesis and genesis transactions is implemented correctly.

runtime/v2/manager.go Outdated Show resolved Hide resolved
server/v2/cometbft/abci_test.go Show resolved Hide resolved
Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

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

nice job!!

@julienrbrt julienrbrt added this pull request to the merge queue Dec 3, 2024
Merged via the queue into main with commit 7d11742 Dec 3, 2024
75 of 76 checks passed
@julienrbrt julienrbrt deleted the julien/valupdates-genesis branch December 3, 2024 21:32
mergify bot pushed a commit that referenced this pull request Dec 3, 2024
(cherry picked from commit 7d11742)

# Conflicts:
#	runtime/v2/builder.go
#	runtime/v2/manager.go
#	server/v2/appmanager/appmanager.go
#	server/v2/appmanager/genesis.go
julienrbrt added a commit that referenced this pull request Dec 3, 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 Issues related to server/v2 C:x/genutil genutil module issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: app v2: empty valset after init genesis
8 participants