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 20 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.
    • Added migration functionality for legacy genesis files to current format.
    • Enhanced the application state export to include consensus validators.
  • 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.
  • Chores

    • Updated test files to ensure migration logic is functioning correctly.

Copy link
Contributor

coderabbitai bot commented Sep 12, 2024

Walkthrough

Walkthrough

The changes introduce a new JSONPubKey interface for handling public keys in JSON format, modify validator types to use sdk.GenesisValidator instead of cmttypes.GenesisValidator, and streamline the export and migration processes for genesis files. The modifications enhance the handling of public keys and validators across various files, ensuring better integration with the Cosmos SDK and maintaining compatibility with legacy 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.
server/types/app.go Changed Validators type from cmttypes.GenesisValidator to sdk.GenesisValidator, indicating a shift in dependency for validator management.
simapp/export.go Simplified ExportAppStateAndValidators by directly assigning validators from staking.WriteValidators without intermediate conversions.
tests/e2e/genutil/export_test.go Updated setupApp to initialize the Validators field in ConsensusGenesis from nil to an empty slice of sdk.GenesisValidator.
types/genesis.go Removed genesis.go file that defined a custom GenesisValidator structure, indicating a refactor in genesis state management.
types/staking.go Introduced a new GenesisValidator struct with fields for address, public key, power, and name, utilizing cryptotypes.JSONPubKey for public key representation.
x/genutil/migration/migration.go Added functionality for migrating legacy genesis files to a new format with a Migrator struct and methods for reading and transforming legacy data.
x/genutil/migration/migration_test.go Implemented unit tests for the migration process, ensuring legacy data is correctly converted to the new format.
x/genutil/types/genesis.go Updated to use sdk.GenesisValidator instead of cmttypes.GenesisValidator, modifying related functions for genesis handling.
x/genutil/utils.go Changed the ExportGenesisFileWithTime function to accept []sdk.GenesisValidator instead of []cmttypes.GenesisValidator.
x/genutil/v2/cli/export.go Enhanced ExportCmd to include the Validators field in the appGenesis structure.
x/genutil/v2/types.go Added Validators field to the ExportedApp struct for exporting validator information.
x/staking/genesis.go Removed custom GenesisValidator struct and updated WriteValidators function to return sdk.GenesisValidator, improving standardization in validator handling.

Possibly related PRs

Suggested reviewers

  • kocubinski
  • aaronc
  • julienrbrt
  • facundomedica
  • sontrinh16
  • testinginprod

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: 13

Outside diff range and nitpick comments (2)
x/genutil/migration/migration.go (2)

35-38: Align struct tags with JSON formatting conventions

Ensure that the JSON struct tags follow consistent formatting, especially regarding omitempty usage.

Review the struct tags for legacyConsensusGenesis:

 type legacyConsensusGenesis struct {
 	Validators []cmttypes.GenesisValidator `json:"validators,omitempty"`
-	Params     *cmttypes.ConsensusParams   `json:"params,omitempty"`
+	Params     *cmttypes.ConsensusParams   `json:"params,omitempty"`
 }

While there might not be a change here, double-check that the tags are correctly applied and consistent with other structs.


68-68: Simplify slice initialization

Instead of initializing vals with an empty slice literal, you can declare it using var for consistency and to follow Go idioms.

Apply this diff to declare the slice:

-		vals := []sdk.GenesisValidator{}
+		var vals []sdk.GenesisValidator
Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c9b90ad and 80a92ae.

Files selected for processing (15)
  • crypto/types/types.go (1 hunks)
  • server/types/app.go (2 hunks)
  • simapp/export.go (1 hunks)
  • tests/e2e/genutil/export_test.go (2 hunks)
  • types/genesis.go (0 hunks)
  • types/staking.go (1 hunks)
  • x/genutil/migration/migration.go (1 hunks)
  • x/genutil/migration/migration_test.go (1 hunks)
  • x/genutil/migration/testdata/old_app_genesis.json (1 hunks)
  • x/genutil/types/genesis.go (5 hunks)
  • x/genutil/types/testdata/app_genesis.json (1 hunks)
  • x/genutil/utils.go (2 hunks)
  • x/genutil/v2/cli/export.go (1 hunks)
  • x/genutil/v2/types.go (2 hunks)
  • x/staking/genesis.go (2 hunks)
Files not reviewed due to no reviewable changes (1)
  • types/genesis.go
Additional context used
Path-based instructions (12)
crypto/types/types.go (1)

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

server/types/app.go (1)

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

simapp/export.go (1)

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

tests/e2e/genutil/export_test.go (3)

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


Pattern tests/**/*: "Assess the integration and e2e test code assessing sufficient code coverage for the changes associated in the pull request"


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

types/staking.go (1)

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

x/genutil/migration/migration.go (1)

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

x/genutil/migration/migration_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"

x/genutil/types/genesis.go (1)

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

x/genutil/utils.go (1)

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

x/genutil/v2/cli/export.go (1)

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

x/genutil/v2/types.go (1)

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

x/staking/genesis.go (1)

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

Gitleaks
x/genutil/migration/testdata/old_app_genesis.json

1-1: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

x/genutil/types/testdata/app_genesis.json

1-1: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

Additional comments not posted (22)
x/genutil/v2/types.go (2)

6-7: LGTM!

The import statement for the sdk package is valid and necessary to use the sdk.GenesisValidator type in the ExportedApp struct.


25-26: Excellent addition!

The new Validators field of type []sdk.GenesisValidator in the ExportedApp struct is a valuable enhancement. It allows the struct to include an exported validator set, which is crucial for applications that need to maintain or export the state of validators alongside the app's state and height.

This change expands the data model to accommodate more comprehensive state information relevant to blockchain applications, particularly in the context of the Cosmos SDK.

x/genutil/migration/migration_test.go (1)

16-48: Excellent test coverage for the migration functionality!

The test function TestMigration provides comprehensive coverage for the migration of an old genesis file to a new format. It follows best practices by:

  1. Setting up a clean temporary directory for the migration process.
  2. Verifying that the old genesis file cannot be read with the new format before migration.
  3. Executing the migration process.
  4. Verifying that the new genesis file reflects the data from the old genesis file after migration.
  5. Making comprehensive assertions on the application hash and validator addresses.

The test ensures the reliability of the migration process and helps maintain the integrity of the data during transitions.

crypto/types/types.go (1)

8-14: LGTM!

The new JSONPubKey interface is a well-designed addition that enhances the functionality for handling public keys parsed from JSON files. The interface methods are clearly defined and serve specific purposes, aligning with the existing PubKey interface.

The code follows the Uber Go Style Guide and does not introduce any breaking changes to the existing codebase.

x/staking/genesis.go (3)

12-12: LGTM!

The import statement follows the Uber Golang style guide and is necessary for the changes in this file.


Line range hint 17-50: Excellent refactoring!

The changes in the WriteValidators function align with the goal of leveraging SDK types and utilities. Key improvements include:

  • Using sdk.GenesisValidator instead of a custom type enhances compatibility with the Cosmos SDK.
  • Utilizing cryptocodec.ToCmtPubKeyInterface for public key conversion ensures a standardized approach.
  • Populating the Address field using Bytes() method guarantees the correct format is used.

The code follows the Uber Golang style guide and improves the overall integration with the Cosmos SDK.


30-34: Great use of cryptocodec.ToCmtPubKeyInterface!

The conversion of the public key to the cryptotypes.PubKey interface using cryptocodec.ToCmtPubKeyInterface aligns with the goal of adopting a standardized approach for handling public keys. The error handling is performed correctly and follows the Uber Golang style guide.

This change contributes to the overall refactoring effort and improves maintainability and interoperability with other components of the Cosmos SDK.

x/genutil/v2/cli/export.go (1)

79-79: LGTM! The change ensures that the exported genesis state includes the consensus validators.

Assigning the Validators field from the exported structure to the Consensus field of the appGenesis structure is a crucial step in exporting the complete application state. By including the consensus validators in the exported genesis, this change ensures that the application can properly initialize its consensus mechanism during startup.

This modification aligns with the expected behavior of the export command and enhances the functionality of the application.

server/types/app.go (1)

20-20: LGTM!

The import statement is valid, and the package path is correct. The sdk alias is commonly used and follows the convention.

x/genutil/utils.go (2)

21-21: LGTM!

The import statement is valid and necessary for using the sdk.GenesisValidator type.


38-38: Verify the usage of the function in the codebase.

The change in the validators parameter type from []cmttypes.GenesisValidator to []sdk.GenesisValidator is valid and aligns with the import of the sdk package. This change promotes better integration with the Cosmos SDK while maintaining compatibility with the legacy structures.

However, it's important to verify that all the callers of this function are now passing the validators using the sdk.GenesisValidator type to avoid any type mismatches.

Run the following script to verify the function usage:

Verification successful

Verification successful, but additional manual review recommended.

The change in the validators parameter type from []cmttypes.GenesisValidator to []sdk.GenesisValidator in the ExportGenesisFileWithTime function does not break any existing usage found in the codebase. All observed calls to this function pass nil for the validators parameter, which is compatible with both types.

However, we couldn't find any instances where a non-nil validators slice is passed to the function. This means we can't directly verify the correct usage of sdk.GenesisValidator.

To ensure complete compatibility:

  • Conduct a thorough manual review of the codebase to confirm no usages of cmttypes.GenesisValidator remain for this function.
  • Update any documentation or comments related to this function to reflect the new parameter type.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `ExportGenesisFileWithTime` pass validators using `sdk.GenesisValidator` type.

# Test: Search for the function usage. Expect: Only occurrences with `sdk.GenesisValidator` type for validators.
rg --type go -A 5 $'ExportGenesisFileWithTime'

Length of output: 2217

types/staking.go (1)

103-108: LGTM!

The GenesisValidator struct is well-defined with appropriate field types:

  • Address as ConsAddress represents the validator's consensus address.
  • PubKey as cryptotypes.JSONPubKey allows flexibility in handling different key types.
  • Power as int64 captures the validator's voting power.
  • Name as string provides a human-readable identifier.

The struct provides a clear and structured representation of an initial validator's essential attributes, ensuring compatibility with the SDK's types system.

tests/e2e/genutil/export_test.go (2)

34-34: LGTM!

The import statement for the sdk package is valid and follows the correct syntax and conventions.


188-188: Improvement: Initializing Validators field with an empty slice.

Changing the Validators field from nil to an empty slice of sdk.GenesisValidator is a good practice. It explicitly initializes the validators list as empty during the application setup, preventing potential nil pointer dereference issues.

Using the sdk.GenesisValidator type ensures compatibility with the Cosmos SDK types for genesis validators.

simapp/export.go (1)

47-47: LGTM!

The change simplifies the code by directly assigning the validators returned from staking.WriteValidators to the Validators field of the ExportedApp struct. This eliminates the need for manual conversion and construction of a new list of cmttypes.GenesisValidator objects, reducing potential points of failure while maintaining the overall functionality.

x/genutil/types/genesis.go (6)

122-132: LGTM!

The conversion from cmttypes.GenesisValidator to sdk.GenesisValidator is implemented correctly. The relevant fields are mapped appropriately, ensuring that the validators are in the expected format for the SDK.


143-143: LGTM!

The assignment of the converted validators to the Consensus.Validators field of the AppGenesis struct is correct and consistent with the conversion logic.


176-190: LGTM!

The conversion from sdk.GenesisValidator back to cmttypes.GenesisValidator in the ToGenesisDoc method is implemented correctly. The relevant fields are mapped appropriately, ensuring that the validators are in the expected format for the CometBFT genesis.

The additional check to set the validators to nil if the slice is empty is a good practice to maintain consistency with the expected structure.


205-206: LGTM!

The modification of the ConsensusGenesis struct to use []sdk.GenesisValidator instead of []cmttypes.GenesisValidator is consistent with the overall refactoring to use SDK types for the genesis validator. It ensures that the ConsensusGenesis struct aligns with the AppGenesis struct and the SDK's expectations.


211-211: LGTM!

The modification of the NewConsensusGenesis function to accept []sdk.GenesisValidator instead of []cmttypes.GenesisValidator is consistent with the change made to the ConsensusGenesis struct. It ensures that the function signature aligns with the updated struct definition.


272-272: LGTM!

The assignment of the validator's address based on the public key, when the address is empty, is correct. It ensures that the validator's address is populated consistently based on the public key, handling cases where the address might be missing in the genesis file.

x/genutil/migration/migration.go (1)

25-33: Ensure consistent usage of JSON struct tags

In the legacyAppGenesis struct, verify that JSON struct tags are correctly and consistently applied, and that field names match the expected JSON keys.

Run the following script to check the struct tags:

Verification successful

JSON struct tags are correctly and consistently applied

The verification process confirms that all fields in the legacyAppGenesis struct have corresponding JSON tags. The field names and JSON keys are consistent, following the convention of using CamelCase for Go field names and snake_case for JSON keys. Optional fields (AppState and Consensus) appropriately use the "omitempty" option.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that struct field names and JSON tags are consistent.

# Expected: All struct fields have matching JSON tags.

ast-grep --lang go --pattern $'type $_ struct {
  $_$: $_$ `json:"$_$"`$$$
}' x/genutil/migration/migration.go

Length of output: 111


Script:

#!/bin/bash
# Display the content of the file
echo "File content:"
cat x/genutil/migration/migration.go

echo -e "\n\nStruct definition:"
grep -n "type legacyAppGenesis struct" -A 10 x/genutil/migration/migration.go

echo -e "\n\nJSON tags:"
grep -n '`json:"' x/genutil/migration/migration.go

Length of output: 4760

@@ -76,7 +76,7 @@ type (
// AppState is the application state as JSON.
AppState json.RawMessage
// Validators is the exported validator set.
Validators []cmttypes.GenesisValidator
Validators []sdk.GenesisValidator
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Update remaining cmttypes.GenesisValidator usages for consistency

The verification process has identified several instances where cmttypes.GenesisValidator is still being used in the codebase. These occurrences need to be addressed to maintain consistency with the change to sdk.GenesisValidator in the ExportedApp struct:

  • x/genutil/types/genesis.go: Two instances of cmttypes.GenesisValidator usage
  • x/genutil/migration/migration.go: One instance in a struct field definition

Action items:

  • Update these instances to use sdk.GenesisValidator instead of cmttypes.GenesisValidator.
  • Carefully review the affected files to ensure proper migration and maintain functionality.
  • Test thoroughly after making these changes to verify that validator-related operations still work as expected.
Analysis chain

Verify compatibility with existing code.

The change from cmttypes.GenesisValidator to sdk.GenesisValidator aligns with the shift towards using Cosmos SDK types for the Validators structure. This modification is likely part of a larger refactor to integrate more closely with the Cosmos SDK.

However, it's important to ensure that existing code relying on the cmttypes.GenesisValidator type is updated to handle the new sdk.GenesisValidator type to maintain compatibility and functionality related to validator management.

Run the following script to identify instances of cmttypes.GenesisValidator usage in the codebase:

If the script returns instances of cmttypes.GenesisValidator usage outside of test files or comments, update the code to handle the new sdk.GenesisValidator type accordingly.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for usage of `cmttypes.GenesisValidator` in Go files.

# Test: Search for the type usage. Expect: Only occurrences in test files or comments.
rg --type go $'cmttypes\.GenesisValidator'

Length of output: 287

Comment on lines +40 to +42
// NewMigrator takes in 2 file path one for the current genesis file
// and the other are the directory where the new genesis file will live.
// If you want to replace old genesis file the both path could be the same.
Copy link
Contributor

Choose a reason for hiding this comment

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

Improve comment clarity and grammar for NewMigrator function

The comments for NewMigrator contain grammatical errors and can be rephrased for better understanding. According to the Go documentation conventions, comments should be complete sentences starting with the element's name.

Apply this diff to enhance the comment:

-// NewMigrator takes in 2 file path one for the current genesis file
-// and the other are the directory where the new genesis file will live.
-// If you want to replace old genesis file the both path could be the same.
+// NewMigrator takes in two file paths: one for the current genesis file
+// and the other for the directory where the new genesis file will be saved.
+// If you want to replace the old genesis file, both paths can be the same.
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
// NewMigrator takes in 2 file path one for the current genesis file
// and the other are the directory where the new genesis file will live.
// If you want to replace old genesis file the both path could be the same.
// NewMigrator takes in two file paths: one for the current genesis file
// and the other for the directory where the new genesis file will be saved.
// If you want to replace the old genesis file, both paths can be the same.

Comment on lines +95 to +98
err = newAg.ValidateAndComplete()
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.

Handle potential errors from newAg.ValidateAndComplete() gracefully

When calling newAg.ValidateAndComplete(), ensure that any validation errors are wrapped with context to aid in debugging.

Wrap the error with additional context:

 	err = newAg.ValidateAndComplete()
 	if err != nil {
-		return err
+		return fmt.Errorf("validation failed for new app genesis: %w", err)
 	}
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
err = newAg.ValidateAndComplete()
if err != nil {
return err
}
err = newAg.ValidateAndComplete()
if err != nil {
return fmt.Errorf("validation failed for new app genesis: %w", err)
}

Comment on lines +55 to +56
// MigrateGenesisFile migrate current genesis file content to match of the
// new genesis validator type.
Copy link
Contributor

Choose a reason for hiding this comment

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

Enhance comment clarity and grammar for MigrateGenesisFile method

The comment for MigrateGenesisFile contains grammatical errors and lacks clarity. It should be a clear, complete sentence that starts with the method name.

Apply this diff to improve the comment:

-// MigrateGenesisFile migrate current genesis file content to match of the
-// new genesis validator type.
+// MigrateGenesisFile migrates the current genesis file content to match the
+// new genesis validator type.
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
// MigrateGenesisFile migrate current genesis file content to match of the
// new genesis validator type.
// MigrateGenesisFile migrates the current genesis file content to match the
// new genesis validator type.

Comment on lines +103 to +104
// CometBFT Genesis Handling for JSON,
// this is necessary for json unmarshaling of legacyConsensusGenesis
Copy link
Contributor

Choose a reason for hiding this comment

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

Improve comment clarity for JSON handling functions

The comments preceding the MarshalJSON and UnmarshalJSON methods can be rephrased for clarity and to follow Go documentation conventions.

Apply this diff to enhance the comment:

-// CometBFT Genesis Handling for JSON,
-// this is necessary for json unmarshaling of legacyConsensusGenesis
+// Implement custom JSON marshalling for legacyConsensusGenesis.
+// This is necessary for proper JSON unmarshalling of CometBFT consensus genesis data.
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
// CometBFT Genesis Handling for JSON,
// this is necessary for json unmarshaling of legacyConsensusGenesis
// Implement custom JSON marshalling for legacyConsensusGenesis.
// This is necessary for proper JSON unmarshalling of CometBFT consensus genesis data.

Comment on lines +44 to +52
file, err := os.Open(filepath.Clean(filePath))
if err != nil {
return nil, err
}

return &Migrator{
filePath: savePath,
reader: file,
}, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Handle potential error when opening the input file

Currently, you're opening the input file but not deferring its closure, which could lead to resource leaks. Additionally, consider handling the scenario where os.Open fails.

Apply this diff to defer the file closure:

 func NewMigrator(filePath, savePath string) (*Migrator, error) {
 	file, err := os.Open(filepath.Clean(filePath))
 	if err != nil {
 		return nil, err
 	}
+	defer file.Close()
 
 	return &Migrator{
 		savePath: savePath,
 		reader:   file,
 	}, nil
 }

However, since you're returning the file as m.reader, deferring file.Close() here would close it before it's read. Instead, ensure that the file is properly closed after its usage, perhaps in the MigrateGenesisFile method.

Committable suggestion was skipped due to low confidence.

Comment on lines +62 to +67
if rs, ok := m.reader.(io.ReadSeeker); ok {
err = json.NewDecoder(rs).Decode(&ag)
if err != nil {
return fmt.Errorf("error unmarshalling legacy AppGenesis: %w", err)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Handle the case where m.reader is not an io.ReadSeeker

The current implementation proceeds only if m.reader is an io.ReadSeeker. If it's not, the method does nothing but still attempts to use newAg, which would be uninitialized, leading to unexpected behavior.

Consider returning an error if m.reader is not an io.ReadSeeker:

 	if rs, ok := m.reader.(io.ReadSeeker); ok {
 		err = json.NewDecoder(rs).Decode(&ag)
 		if err != nil {
 			return fmt.Errorf("error unmarshalling legacy AppGenesis: %w", err)
 		}
+	} else {
+		return fmt.Errorf("reader does not implement io.ReadSeeker")
 	}
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
if rs, ok := m.reader.(io.ReadSeeker); ok {
err = json.NewDecoder(rs).Decode(&ag)
if err != nil {
return fmt.Errorf("error unmarshalling legacy AppGenesis: %w", err)
}
if rs, ok := m.reader.(io.ReadSeeker); ok {
err = json.NewDecoder(rs).Decode(&ag)
if err != nil {
return fmt.Errorf("error unmarshalling legacy AppGenesis: %w", err)
}
} else {
return fmt.Errorf("reader does not implement io.ReadSeeker")
}

AppState: ag.AppState,
Consensus: &types.ConsensusGenesis{
Validators: vals,
Params: ag.Consensus.Params,
Copy link
Contributor

Choose a reason for hiding this comment

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

Check for nil ag.Consensus before accessing ag.Consensus.Params

Similar to the previous issue, if ag.Consensus is nil, accessing ag.Consensus.Params will result in a nil pointer dereference.

Add a nil check before assigning Params:

 			Consensus: &types.ConsensusGenesis{
 				Validators: vals,
-				Params:     ag.Consensus.Params,
+				Params:     ag.Consensus != nil ? ag.Consensus.Params : nil,
 			},

Alternatively, you can check and handle the nil case appropriately before constructing newAg.

Committable suggestion was skipped due to low confidence.

Comment on lines +71 to +75
Address: sdk.ConsAddress(cmtVal.Address).Bytes(),
PubKey: cmtVal.PubKey,
Power: cmtVal.Power,
Name: cmtVal.Name,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider initializing Address without unnecessary conversion

In the Address field assignment, converting to sdk.ConsAddress and then calling Bytes() may be redundant if cmtVal.Address is already in the correct format.

Review whether the conversion is necessary, and simplify if possible:

 			val := sdk.GenesisValidator{
-				Address: sdk.ConsAddress(cmtVal.Address).Bytes(),
+				Address: cmtVal.Address,
 				PubKey:  cmtVal.PubKey,
 				Power:   cmtVal.Power,
 				Name:    cmtVal.Name,
 			}

Ensure that this change aligns with the expected data types and does not introduce errors.

Committable suggestion was skipped due to low confidence.

@@ -0,0 +1 @@
{"app_name":"\u003cappd\u003e","app_version":"","genesis_time":"2023-02-20T11:08:30.588307671Z","chain_id":"demo","initial_height":48,"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"consensus":{"validators":[{"address":"D336F99AA5CF77503CDD8366E68A0DFE89B4124B","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"power":"1","name":"test"}],"params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_num_blocks":"100000","max_age_duration":"172800000000000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"},"synchrony":{"precision":"0","message_delay":"0"},"feature":{"vote_extensions_enable_height":"0","pbts_enable_height":"0"}}}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Potential Exposure of Sensitive Information

The old_app_genesis.json file appears to contain sensitive data such as private keys, account numbers, or API keys. Committing such information to the repository can lead to security vulnerabilities. Please ensure that all confidential data is sanitized or replaced with placeholder values before committing.

Tools
Gitleaks

1-1: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

Copy link
Member

Choose a reason for hiding this comment

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

As said in the standup, could you make it work with https://github.com/cosmos/cosmos-sdk/blob/main/x/genutil/client/cli/migrate.go#L21-L22 ?

If you wonder, migrations should look like this: https://github.com/cosmos/cosmos-sdk/tree/v0.50.9/x/genutil/migrations (so could you revert that here too in the package names?)

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:x/genutil genutil module issues C:x/staking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants