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(genutil): Use sdk types genesis validator #21678

Open
wants to merge 46 commits into
base: main
Choose a base branch
from

Conversation

sontrinh16
Copy link
Member

@sontrinh16 sontrinh16 commented Sep 12, 2024

Description

follow up to: #21382


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

    • Introduced a new interface for handling public keys from JSON data.
    • Enhanced the application state export to include consensus validators.
    • Added functionality for migrating legacy genesis files to a new format.
    • Integrated staking functionalities into the application.
    • Added new methods for querying application state based on specific versions.
    • Improved compatibility with the Cosmos SDK through standardized validator handling.
  • Bug Fixes

    • Improved robustness in handling validator initialization during tests.
  • Refactor

    • Simplified validator handling by replacing custom structures with standardized SDK types.
    • Streamlined the export process for genesis files.
    • Enhanced state management capabilities with new query functionality.

Copy link
Contributor

coderabbitai bot commented Sep 12, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes introduce a new JSONPubKey interface for handling public keys in JSON format, modify the ExportAppStateAndValidators function to include validator information, and replace a custom GenesisValidator struct with sdk.GenesisValidator. These modifications enhance public key management and validator handling across various files, ensuring better integration with the Cosmos SDK while maintaining compatibility with existing structures.

Changes

Files Change Summary
crypto/types/types.go Added a new JSONPubKey interface for JSON-parsed public keys, including methods for address retrieval, byte representation, signature verification, and key type identification.
crypto/codec/pubkey.go Introduced PubKeyToProto and PubKeyFromProto functions for converting between JSONPubkey and cryptotypes.PubKey, ensuring compatibility across different public key formats.
crypto/keys/jsonkey.go Added JSONPubkey type for representing public keys parsed from JSON, with methods for address computation and byte representation.
runtime/v2/app.go Added a Query method to the App struct for querying state at a specific version, enhancing state management capabilities.
simapp/v2/app_di.go Introduced StakingKeeper field in SimApp struct to manage staking functionalities, updating the initialization function accordingly.
simapp/v2/export.go Modified ExportAppStateAndValidators to include a new Validators field populated by current bonded validators, enhancing the export functionality.
types/staking.go Added GenesisValidator struct to represent initial validators with relevant attributes, enhancing the staking module's functionality.
x/genutil/migration/v052/migrate.go Introduced migration utilities for converting legacy genesis files to a new format, ensuring compatibility with updated application structures.
x/genutil/migration/v052/migrate_test.go Added unit tests for the migration of application genesis files, ensuring the integrity of the migration process.
x/genutil/types/genesis.go Updated to use sdk.GenesisValidator instead of cmttypes.GenesisValidator, enhancing compatibility with the SDK's expectations.
x/staking/genesis.go Removed custom GenesisValidator struct, replacing it with sdk.GenesisValidator, simplifying the code and improving integration with the Cosmos SDK.

Possibly related PRs

Suggested labels

C:x/auth, C:x/accounts, C:x/upgrade, C:x/mint, C:x/distribution, C:x/feegrant, C:x/protocolpool, C:x/nft, C:x/epochs

Suggested reviewers

  • sontrinh16
  • julienrbrt
  • testinginprod
  • facundomedica

📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6a81119 and 496627d.

📒 Files selected for processing (1)
  • simapp/v2/app_di.go (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • simapp/v2/app_di.go

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 C:x/staking C:x/genutil genutil module issues labels Sep 12, 2024
@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Sep 13, 2024
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.

If you can re-add the new GenesisValdiator struct in genutil/v2, then it will solve this as well: #21634

@sontrinh16
Copy link
Member Author

genutil/v2,

i will added it in, just need to fix it to work with json unmarshal

@github-actions github-actions bot added the C:CLI label Sep 17, 2024
@github-actions github-actions bot removed the C:CLI label Sep 18, 2024
@sontrinh16 sontrinh16 marked this pull request as ready for review September 19, 2024 08:35
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

🧹 Outside diff range and nitpick comments (1)
x/staking/genesis.go (1)

Line range hint 16-34: Approved: Good refactoring to use SDK types

The changes to the WriteValidators function look good. Using sdk.GenesisValidator instead of a custom type aligns well with the PR objective of refactoring to use SDK types. This change enhances compatibility with the Cosmos SDK and reduces the need for custom implementations.

Consider adding a comment explaining the use of sdk.ConsAddress(pk.Address()).Bytes() for setting the Address field, as it might not be immediately obvious to all developers why this conversion is necessary. For example:

vals = append(vals, sdk.GenesisValidator{
    // Convert the consensus public key address to bytes for SDK compatibility
    Address: sdk.ConsAddress(pk.Address()).Bytes(),
    PubKey:  pk,
    Power:   validator.GetConsensusPower(keeper.PowerReduction(ctx)),
    Name:    validator.GetMoniker(),
})

This addition would improve code clarity and maintainability.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a0d41c9 and b1924fd.

📒 Files selected for processing (3)
  • crypto/types/types.go (1 hunks)
  • simapp/v2/export.go (2 hunks)
  • x/staking/genesis.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crypto/types/types.go
  • simapp/v2/export.go
🧰 Additional context used
📓 Path-based instructions (1)
x/staking/genesis.go (1)

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

@sontrinh16
Copy link
Member Author

@julienrbrt seems i can completely remove cmtjson maybe because of the consensus params, maybe i introduced sdk types for it on a seperated PR

@@ -123,3 +124,11 @@ func (a *App[T]) GetAppManager() *appmanager.AppManager[T] {
func (a *App[T]) GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message {
return a.GRPCMethodsToMessageMap
}

func (a *App[T]) Query(ctx context.Context, gasLimit, version uint64, req transaction.Msg) (transaction.Msg, error) {
Copy link
Member Author

@sontrinh16 sontrinh16 Sep 30, 2024

Choose a reason for hiding this comment

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

i couldn't find other way to get bonded validator directly because of executionContext. Dont know if i missed something here

Copy link
Member

Choose a reason for hiding this comment

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

We can use the staking keeper directly and remove this, like in export v1.

Copy link
Member

Choose a reason for hiding this comment

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

can't we pass the correct context from the calling func?

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 (3)
crypto/keys/jsonkey.go (1)

10-15: Minor improvement: Fix grammatical error in comment.

The struct definition looks good and follows the Uber Golang style guide. However, there's a small grammatical error in the comment.

Consider updating the comment to fix the grammatical error:

-// JSONPubKey defines a public key that are parse from JSON file.
+// JSONPubKey defines a public key that is parsed from a JSON file.
simapp/v2/export.go (1)

30-58: LGTM: Validator query and processing logic is well-implemented.

The new code block effectively queries for bonded validators and converts them to GenesisValidator format. Error handling is generally good, but there's room for improvement.

Consider wrapping errors with additional context for better debugging. For example:

if err != nil {
    return v2.ExportedApp{}, fmt.Errorf("failed to get consensus public key: %w", err)
}

This approach provides more context about where the error occurred, making debugging easier.

runtime/v2/app.go (1)

128-134: LGTM: New Query method looks good, with a minor suggestion.

The new Query method is well-implemented and integrates nicely with the existing stf structure. It allows querying at specific blockchain heights, which is a valuable feature.

Consider wrapping the error returned from db.StateAt to provide more context:

 func (a *App[T]) Query(ctx context.Context, gasLimit, version uint64, req transaction.Msg) (transaction.Msg, error) {
 	state, err := a.db.StateAt(version)
 	if err != nil {
-		return nil, err
+		return nil, fmt.Errorf("failed to get state at version %d: %w", version, err)
 	}
 	return a.stf.Query(ctx, state, gasLimit, req)
 }

This change would make debugging easier by providing more information about the context of the error.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b1924fd and b4014f3.

📒 Files selected for processing (10)
  • crypto/codec/pubkey.go (1 hunks)
  • crypto/keys/jsonkey.go (1 hunks)
  • runtime/v2/app.go (2 hunks)
  • simapp/v2/app_di.go (3 hunks)
  • simapp/v2/export.go (2 hunks)
  • types/staking.go (2 hunks)
  • x/genutil/migration/v052/migrate.go (1 hunks)
  • x/genutil/migration/v052/migrate_test.go (1 hunks)
  • x/genutil/types/genesis.go (6 hunks)
  • x/staking/genesis.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (6)
  • simapp/v2/app_di.go
  • types/staking.go
  • x/genutil/migration/v052/migrate.go
  • x/genutil/migration/v052/migrate_test.go
  • x/genutil/types/genesis.go
  • x/staking/genesis.go
🧰 Additional context used
📓 Path-based instructions (4)
crypto/codec/pubkey.go (1)

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

crypto/keys/jsonkey.go (1)

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

runtime/v2/app.go (1)

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

simapp/v2/export.go (1)

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

🔇 Additional comments (7)
crypto/keys/jsonkey.go (2)

3-8: LGTM: Import section is well-organized.

The import section follows the Uber Golang style guide. All imported packages are used in the code, and they are correctly grouped.


39-41: LGTM: Bytes() method is correct and concise.

The Bytes() method is implemented correctly and follows the Uber Golang style guide. It simply returns the Value field, which is appropriate for its purpose.

simapp/v2/export.go (4)

5-8: LGTM: New imports are appropriate and correctly placed.

The added imports for "fmt" and "stakingtypes" are necessary for the new functionality and follow Go conventions.


9-10: LGTM: Crypto codec import is appropriate.

The added import for "cryptocodec" is necessary for handling public keys and follows Go conventions.


61-64: LGTM: Return statement updated correctly.

The Validators field is correctly added to the ExportedApp struct in the return statement, aligning with the updated function signature.


61-64: Verify ExportedApp struct definition in genutil/v2 package.

To ensure consistency with the changes made here, we need to verify that the ExportedApp struct in the x/genutil/v2/types.go file has been updated to include the Validators field.

Run the following script to verify the ExportedApp struct definition:

✅ Verification successful

ExportedApp struct definition verified successfully.

The ExportedApp struct in x/genutil/v2/types.go includes the Validators field as expected.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the ExportedApp struct definition in the genutil/v2 package

# Test: Search for the ExportedApp struct definition
rg --type go -A 10 'type ExportedApp struct' x/genutil/v2/

Length of output: 477

runtime/v2/app.go (1)

4-4: LGTM: Import statement added correctly.

The addition of the context package import is necessary for the new Query method and follows the Uber Go Style Guide for import grouping.

Comment on lines +17 to +37
func (pk JSONPubkey) Address() types.Address {
switch pk.KeyType {
case ed25519.PubKeyName:
ed25519 := ed25519.PubKey{
Key: pk.Value,
}
return ed25519.Address()
case secp256k1.PubKeyName:
secp256k1 := secp256k1.PubKey{
Key: pk.Value,
}
return secp256k1.Address()
case bls12_381.PubKeyName:
bls12_381 := bls12_381.PubKey{
Key: pk.Value,
}
return bls12_381.Address()
default:
return nil
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding error handling for unknown key types.

The Address() method implementation looks good overall and follows the Uber Golang style guide. However, it might be beneficial to add error handling for unknown key types.

Consider updating the Address() method to return an error for unknown key types:

-func (pk JSONPubkey) Address() types.Address {
+func (pk JSONPubkey) Address() (types.Address, error) {
 	switch pk.KeyType {
 	case ed25519.PubKeyName:
 		ed25519 := ed25519.PubKey{
 			Key: pk.Value,
 		}
-		return ed25519.Address()
+		return ed25519.Address(), nil
 	case secp256k1.PubKeyName:
 		secp256k1 := secp256k1.PubKey{
 			Key: pk.Value,
 		}
-		return secp256k1.Address()
+		return secp256k1.Address(), nil
 	case bls12_381.PubKeyName:
 		bls12_381 := bls12_381.PubKey{
 			Key: pk.Value,
 		}
-		return bls12_381.Address()
+		return bls12_381.Address(), nil
 	default:
-		return nil
+		return nil, fmt.Errorf("unknown key type: %s", pk.KeyType)
 	}
 }

This change would allow callers to handle unknown key types more gracefully.

📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (pk JSONPubkey) Address() types.Address {
switch pk.KeyType {
case ed25519.PubKeyName:
ed25519 := ed25519.PubKey{
Key: pk.Value,
}
return ed25519.Address()
case secp256k1.PubKeyName:
secp256k1 := secp256k1.PubKey{
Key: pk.Value,
}
return secp256k1.Address()
case bls12_381.PubKeyName:
bls12_381 := bls12_381.PubKey{
Key: pk.Value,
}
return bls12_381.Address()
default:
return nil
}
}
func (pk JSONPubkey) Address() (types.Address, error) {
switch pk.KeyType {
case ed25519.PubKeyName:
ed25519 := ed25519.PubKey{
Key: pk.Value,
}
return ed25519.Address(), nil
case secp256k1.PubKeyName:
secp256k1 := secp256k1.PubKey{
Key: pk.Value,
}
return secp256k1.Address(), nil
case bls12_381.PubKeyName:
bls12_381 := bls12_381.PubKey{
Key: pk.Value,
}
return bls12_381.Address(), nil
default:
return nil, fmt.Errorf("unknown key type: %s", pk.KeyType)
}
}

crypto/codec/pubkey.go Outdated Show resolved Hide resolved
crypto/codec/pubkey.go Outdated Show resolved Hide resolved
crypto/codec/pubkey.go Outdated Show resolved Hide resolved
Copy link
Contributor

@raynaudoe raynaudoe left a comment

Choose a reason for hiding this comment

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

LGTM! left a few comments

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! some nits

x/staking/genesis.go Outdated Show resolved Hide resolved
@@ -22,8 +27,39 @@ func (app *SimApp[T]) ExportAppStateAndValidators(jailAllowedAddrs []string) (v2
return v2.ExportedApp{}, err
}

// get the current bonded validators
resp, err := app.Query(ctx, 0, latestHeight, &stakingtypes.QueryValidatorsRequest{
Copy link
Member

Choose a reason for hiding this comment

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

Why not using the keeper directly?

Copy link
Member Author

Choose a reason for hiding this comment

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

hmmmm i tried but got panic: failed to get executionContext from context in simapp v2 test, seems like i have to use stf to set it in

@@ -123,3 +124,11 @@ func (a *App[T]) GetAppManager() *appmanager.AppManager[T] {
func (a *App[T]) GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message {
return a.GRPCMethodsToMessageMap
}

func (a *App[T]) Query(ctx context.Context, gasLimit, version uint64, req transaction.Msg) (transaction.Msg, error) {
Copy link
Member

Choose a reason for hiding this comment

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

We can use the staking keeper directly and remove this, like in export v1.

if !ok {
return v2.ExportedApp{}, fmt.Errorf("invalid response, expected QueryValidatorsResponse")
}

Copy link
Member

Choose a reason for hiding this comment

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

It looks like the input argument to this func jailAllowedAddrs []string is not used anywhere, should there be filtering done somewhere in this func?

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:CLI C:x/genutil genutil module issues C:x/staking S: DO NOT MERGE Status: DO NOT MERGE
Projects
None yet
Development

Successfully merging this pull request may close these issues.

server/v2: failed to restart node with exported genesis
9 participants