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(gov): migrate gov module to v8 #735

Merged
merged 1 commit into from
Oct 12, 2024
Merged

Conversation

zakir-code
Copy link
Contributor

@zakir-code zakir-code commented Oct 12, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced testing functionality for the upgrade process, including checks for governance parameters.
    • New functionality to manage deprecated store keys and remove old parameters during upgrades.
  • Bug Fixes

    • Improved validation of key deletions in the staking context.
  • Refactor

    • Streamlined the logic for checking key deletions and updated function signatures for clarity.
  • Chores

    • Removed deprecated variables related to base parameters to clean up the codebase.

Copy link

coderabbitai bot commented Oct 12, 2024

Walkthrough

The pull request introduces several enhancements to the testing and governance functionalities within the application. Key modifications include the addition of new functions for checking governance parameters and simplifying key deletion logic in the upgrade tests. Additionally, new functions are implemented in the upgrade handler to manage governance parameters during upgrades, while deprecated key management is introduced in the migrations. The changes also involve the removal of outdated variables from the governance types.

Changes

File Change Summary
app/upgrade_test.go - Added imports for distributiontypes, govtypes, fxgovv8, and fxgovtypes.
- Added checkGovCustomParams function.
- Updated checkStakingMigrationDelete to call checkKeysIsDelete.
- Added checkKeysIsDelete function.
app/upgrades/v8/upgrade.go - Added imports for storetypes and govtypes.
- Updated CreateUpgradeHandler to call migrationGovCustomParam.
- Added migrationGovCustomParam function.
x/gov/migrations/v8/migrate.go - Introduced migrate.go with deprecated key prefixes.
- Added GetRemovedStoreKeys and DeleteOldParamsStore functions.
x/gov/types/params.go - Removed FxBaseParamsKeyPrefix and FxEGFParamsKey variables.
x/staking/migrations/v8/migrate.go - Renamed GetRemovedValidatorStoreKeys to GetRemovedStoreKeys.
- Marked ValidatorOperatorKey, ConsensusPubKey, and ConsensusProcessKey as deprecated.

Possibly related PRs

🐇 In the code we hop and play,
With governance in a bright new way.
Old keys are gone, new checks in place,
Upgrades now run with a joyful grace.
Testing's strong, our path is clear,
Hopping forward, we have no fear! 🐇


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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

Copy link

@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

🧹 Outside diff range and nitpick comments (6)
x/staking/migrations/v8/migrate.go (3)

Line range hint 8-14: Consider removing deprecated variables and updating GetRemovedStoreKeys

The variables ValidatorOperatorKey, ConsensusPubKey, and ConsensusProcessKey are marked as deprecated and scheduled for removal in v8. However, they are still being used in the GetRemovedStoreKeys function.

Consider removing these variables entirely and updating the GetRemovedStoreKeys function to return the byte slices directly. This would look like:

func GetRemovedStoreKeys() [][]byte {
    return [][]byte{
        {0x91}, // ValidatorOperatorKey
        {0x92}, // ConsensusPubKey
        {0x93}, // ConsensusProcessKey
    }
}

This change would complete the deprecation process and remove the potential for these variables to be used elsewhere in the codebase.


17-20: Approve function renaming and suggest documentation improvement

The renaming of GetRemovedValidatorStoreKeys to GetRemovedStoreKeys is appropriate, especially if these keys are not specific to validators.

Consider adding a brief comment to explain the purpose of this function and the nature of the keys it returns. For example:

// GetRemovedStoreKeys returns a list of deprecated store keys that should be removed during migration.
func GetRemovedStoreKeys() [][]byte {
    // ... (existing implementation)
}

This would improve the function's documentation and make its purpose clearer to other developers.


Line range hint 22-33: Approve function update and suggest error handling improvement

The update to use the renamed GetRemovedStoreKeys function is correct, and the overall logic for deleting keys is sound and efficient.

Consider adding error handling for the iterator creation and closing. Here's a suggested improvement:

func DeleteMigrationValidatorStore(
    ctx sdk.Context,
    storeKey storetypes.StoreKey,
) error {
    store := ctx.KVStore(storeKey)
    removeKeys := GetRemovedStoreKeys()
    for _, key := range removeKeys {
        iterator := storetypes.KVStorePrefixIterator(store, key)
        defer iterator.Close()
        for ; iterator.Valid(); iterator.Next() {
            store.Delete(iterator.Key())
        }
        if err := iterator.Error(); err != nil {
            return fmt.Errorf("error iterating over keys: %w", err)
        }
    }
    return nil
}

This change adds proper cleanup with defer iterator.Close() and checks for any errors that might have occurred during iteration.

app/upgrade_test.go (2)

102-109: LGTM: Comprehensive governance parameter verification added.

The checkGovCustomParams function effectively verifies the custom parameters for the MsgCommunityPoolSpend message type and checks for the deletion of removed store keys. This addition ensures that governance-related changes are correctly applied during the upgrade.

Consider adding a comment explaining the purpose of the EGFCustomParamDepositRatio and other default values for better code readability.


Line range hint 115-123: Approve with suggestion: Generic key deletion check function added.

The checkKeysIsDelete function is a valuable addition that improves code reusability by providing a generic way to check for key deletions. However, there's a potential resource management issue:

The defer iterator.Close() statement is inside the loop, which may lead to resource leaks if multiple iterations occur. Consider moving the iterator creation and closure outside the loop:

func checkKeysIsDelete(t *testing.T, kvStore storetypes.KVStore, keys [][]byte) {
	require.Greater(t, len(keys), 0)
	for _, removeKey := range keys {
		iterator := storetypes.KVStorePrefixIterator(kvStore, removeKey)
		hasKey := false
		for ; iterator.Valid(); iterator.Next() {
			hasKey = true
			break
		}
		iterator.Close()
		require.False(t, hasKey, "key is not deleted, prefix:%x", removeKey)
	}
}

This change ensures that each iterator is properly closed after its use, preventing potential resource leaks.

app/upgrades/v8/upgrade.go (1)

42-46: Consistent Comment Formatting

For improved readability, consider adding a space after the numbering in the comments.

Apply this diff to adjust the comments:

 // 1. delete fxParams key
+// 1. Delete fxParams key

 // 2. init custom params
+// 2. Init custom params
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8c2c51a and fe0823a.

📒 Files selected for processing (5)
  • app/upgrade_test.go (3 hunks)
  • app/upgrades/v8/upgrade.go (2 hunks)
  • x/gov/migrations/v8/migrate.go (1 hunks)
  • x/gov/types/params.go (0 hunks)
  • x/staking/migrations/v8/migrate.go (2 hunks)
💤 Files with no reviewable changes (1)
  • x/gov/types/params.go
🧰 Additional context used
🔇 Additional comments (7)
x/gov/migrations/v8/migrate.go (2)

1-7: LGTM: Package declaration and imports are appropriate.

The package name v8 aligns with the migration version, and the imports are relevant to the functionality provided in this file.


15-17: LGTM: GetRemovedStoreKeys function is correct and concise.

The function correctly returns a slice containing the deprecated key prefixes. Its simplicity makes it easy to understand and maintain.

app/upgrade_test.go (3)

17-18: LGTM: New imports added for enhanced functionality.

The new import statements for distribution, governance, and custom types are appropriate for the changes made in this file. They provide the necessary types and functions for the new governance and upgrade checks.

Also applies to: 27-28


98-100: LGTM: Enhanced upgrade verification with governance parameter check.

The addition of checkGovCustomParams to the checkAppUpgrade function improves the upgrade verification process by including checks for governance custom parameters. This ensures that the governance module is correctly configured after the upgrade.


112-113: LGTM: Improved code reusability in staking migration check.

The refactoring of checkStakingMigrationDelete to use the new checkKeysIsDelete function improves code reusability and maintainability. This change simplifies the staking migration check while keeping the functionality intact.

app/upgrades/v8/upgrade.go (2)

6-15: Imports are Correct and Necessary

The added imports for storetypes, govtypes, and the keeper packages are appropriate and necessary for the new functionality introduced in the code.


31-34: Proper Placement of migrationGovCustomParam Call

Integrating migrationGovCustomParam after the validator migrations and before committing the context ensures that governance parameters are correctly handled during the upgrade process.

Comment on lines +8 to +13
var (
// Deprecated: do not use, remove in v8
FxBaseParamsKeyPrefix = []byte("0x90")
// Deprecated: do not use, remove in v8
FxEGFParamsKey = []byte("0x91")
)
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor deprecated key declarations and clarify deprecation timeline.

  1. Consider using const instead of var for these values, as they appear to be constants.
  2. The deprecation comments state "remove in v8", but this file is already in the v8 package. Please clarify the deprecation timeline.
  3. Add comments explaining the significance of the byte values 0x90 and 0x91.

Here's a suggested refactoring:

const (
	// FxBaseParamsKeyPrefix is the deprecated key prefix for base parameters.
	// It will be removed in the next major version.
	// The value 0x90 represents [explain significance].
	FxBaseParamsKeyPrefix = []byte{0x90}

	// FxEGFParamsKey is the deprecated key for EGF parameters.
	// It will be removed in the next major version.
	// The value 0x91 represents [explain significance].
	FxEGFParamsKey = []byte{0x91}
)

Comment on lines +19 to +31
func DeleteOldParamsStore(
ctx sdk.Context,
storeKey storetypes.StoreKey,
) {
store := ctx.KVStore(storeKey)
removeKeys := GetRemovedStoreKeys()
for _, key := range removeKeys {
iterator := storetypes.KVStorePrefixIterator(store, key)
for ; iterator.Valid(); iterator.Next() {
store.Delete(iterator.Key())
}
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Close iterator and consider minor optimization in DeleteOldParamsStore.

The function correctly deletes deprecated entries, but there are two points to address:

  1. The iterator is not being closed, which could lead to resource leaks.
  2. A minor optimization can be made by moving the store retrieval inside the loop.

Here's a suggested refactoring:

func DeleteOldParamsStore(
	ctx sdk.Context,
	storeKey storetypes.StoreKey,
) {
	removeKeys := GetRemovedStoreKeys()
	for _, key := range removeKeys {
		store := ctx.KVStore(storeKey)
		iterator := storetypes.KVStorePrefixIterator(store, key)
		defer iterator.Close()
		for ; iterator.Valid(); iterator.Next() {
			store.Delete(iterator.Key())
		}
	}
}

This change ensures that the iterator is properly closed and slightly optimizes the function by moving the store retrieval inside the loop, ensuring we always work with the most up-to-date store state.

Comment on lines +41 to +47
func migrationGovCustomParam(ctx sdk.Context, keeper *keeper.Keeper, storeKey *storetypes.KVStoreKey) error {
// 1. delete fxParams key
fxgovv8.DeleteOldParamsStore(ctx, storeKey)

// 2. init custom params
return keeper.InitCustomParams(ctx)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle Potential Errors from DeleteOldParamsStore Function

The fxgovv8.DeleteOldParamsStore(ctx, storeKey) function might return an error that is currently not being handled. To enhance robustness, consider checking and handling any potential errors from this function.

Apply this diff to handle the error:

 func migrationGovCustomParam(ctx sdk.Context, keeper *keeper.Keeper, storeKey *storetypes.KVStoreKey) error {
     // 1. delete fxParams key
-    fxgovv8.DeleteOldParamsStore(ctx, storeKey)
+    if err := fxgovv8.DeleteOldParamsStore(ctx, storeKey); err != nil {
+        return err
+    }

     // 2. init custom params
     return keeper.InitCustomParams(ctx)
 }
📝 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 migrationGovCustomParam(ctx sdk.Context, keeper *keeper.Keeper, storeKey *storetypes.KVStoreKey) error {
// 1. delete fxParams key
fxgovv8.DeleteOldParamsStore(ctx, storeKey)
// 2. init custom params
return keeper.InitCustomParams(ctx)
}
func migrationGovCustomParam(ctx sdk.Context, keeper *keeper.Keeper, storeKey *storetypes.KVStoreKey) error {
// 1. delete fxParams key
if err := fxgovv8.DeleteOldParamsStore(ctx, storeKey); err != nil {
return err
}
// 2. init custom params
return keeper.InitCustomParams(ctx)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants