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: verify delegation and reward data after upgrade #943

Merged
merged 1 commit into from
Jan 22, 2025

Conversation

zakir-code
Copy link
Contributor

@zakir-code zakir-code commented Jan 22, 2025

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced upgrade testing framework with improved delegation and reward data tracking
    • Added new utility function for swapping decimal coin denominations
  • Improvements

    • Updated distribution migration process to incorporate staking keeper
    • Refined error handling in upgrade-related functions
  • Dependency Updates

    • Updated zerolog dependency from indirect to direct requirement

These changes improve the system's upgrade process, testing capabilities, and dependency management.

Copy link

coderabbitai bot commented Jan 22, 2025

Walkthrough

This pull request introduces comprehensive changes to the upgrade testing and migration processes across multiple files. The modifications primarily focus on enhancing the upgrade test framework in app/upgrade_test.go, improving distribution migration logic in app/upgrades/v8/distribution.go, and adding a new coin denomination swap utility function in types/constant.go. The changes aim to strengthen the robustness of upgrade tests, particularly around delegation and reward data, and refine the migration process for distribution-related functionalities.

Changes

File Change Summary
app/upgrade_test.go - Added imports for distributionkeeper, stakingkeeper, and zerolog
- Modified beforeUpgrade function signature to accept *testing.T
- Introduced delegatesAndRewards function to capture delegation and reward data
- Updated BeforeUpgradeData struct with new map fields
app/upgrades/v8/distribution.go - Updated migrateDistribution and migrateDelegatorStartingInfos to include stakingKeeper
- Introduced validatorMap for caching validators
- Removed swapFXDecCoinsToDefaultCoin function
- Enhanced error handling in delegation information retrieval
app/upgrades/v8/upgrade.go - Modified migrateModulesData to pass StakingKeeper to migrateDistribution
go.mod - Changed github.com/rs/zerolog from indirect to direct dependency
types/constant.go - Added SwapDecCoins function for converting legacy decimal coins

Possibly related PRs

Poem

🐰 Hop, hop, upgrade we go!
Testing paths both high and low
Delegations checked with care
Migrations beyond compare
CodeRabbit's leap of testing might! 🚀

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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: 1

🧹 Nitpick comments (6)
app/upgrades/v8/distribution.go (2)

17-17: Update Function Documentation to Reflect New Parameter

The migrateDistribution function now includes an additional parameter stakingKeeper *fxstakingkeeper.Keeper. Please update the function's documentation to reflect this change, providing a clear explanation of the purpose and usage of the new parameter.


71-71: Add a Check for Zero Stake Values

After calculating info.Stake with validator.TokensFromSharesTruncated, consider adding a check to handle cases where the stake might be zero. This can prevent potential issues in downstream processes that expect a positive stake value.

Optionally, you could add:

	if info.Stake.IsZero() {
		// Handle zero stake scenario if necessary
	}
types/constant.go (1)

122-138: Add Documentation for the SwapDecCoins Function

Consider adding a comment to the newly introduced SwapDecCoins function to explain its purpose and usage. This improves code readability and helps other developers understand how and when to use this function.

Example:

// SwapDecCoins swaps the denomination of all DecCoins from LegacyFXDenom to DefaultDenom
// and adjusts their amounts using SwapDecAmount. It returns the resulting DecCoins.
func SwapDecCoins(decCoins sdk.DecCoins) sdk.DecCoins {
    // Function implementation...
}
app/upgrade_test.go (3)

25-28: Organize Imports for Clarity

For better readability, consider grouping standard library imports, third-party imports, and project-specific imports separately. This makes the import section cleaner and easier to navigate.


180-207: Handle Errors Appropriately in delegatesAndRewards Function

In the delegatesAndRewards function, errors within the iteration are being checked using require.NoError(t, err), which will fail the test immediately on error but won't stop the iteration. Consider modifying the error handling to capture the error and stop the iteration to prevent unnecessary continued processing after a failure.

Example:

err := myApp.StakingKeeper.IterateAllDelegations(ctx, func(del stakingtypes.Delegation) (stop bool) {
	// ...
	if err != nil {
		return true // Stop iteration on error
	}
	// ...
	return false
})
if err != nil {
	require.NoError(t, err)
}

253-253: Clarify Why Assertion Is Commented Out

At line 253~, the assertion comparing rewards before and after the upgrade is commented out:

// assert.EqualValues(t, beforeRewards.String(), afterRewards.String(), delegateKey)

Please either uncomment this assertion if it is necessary for the test, or add a comment explaining why it is being skipped. This ensures that the test code remains clear and maintainable.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f25d103 and affb27a.

📒 Files selected for processing (5)
  • app/upgrade_test.go (6 hunks)
  • app/upgrades/v8/distribution.go (6 hunks)
  • app/upgrades/v8/upgrade.go (1 hunks)
  • go.mod (1 hunks)
  • types/constant.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Mergify Merge Protections
  • GitHub Check: Summary
🔇 Additional comments (6)
app/upgrades/v8/distribution.go (2)

27-27: Verify Consistent Use of Staking Keeper in Function Calls

The migrateDistribution function now passes stakingKeeper to migrateDelegatorStartingInfos. Ensure that all calls to migrateDistribution and related functions consistently use the correct stakingKeeper instance to prevent potential issues.


39-46: Correctly Update Denominations in Historical Rewards

The changes within migrateValidatorHistoricalRewards appropriately update the denominations from LegacyFXDenom to DefaultDenom. The logic ensures that all rewards are correctly mapped to the new denomination.

app/upgrade_test.go (2)

58-58: Update beforeUpgrade Function Signature

The beforeUpgrade function now accepts t *testing.T as a parameter, which allows for the use of testing utilities within the function. This is a good practice for helper functions in tests.


161-167: Include Delegation Data in BeforeUpgradeData Struct

Adding Delegates and DelegateRewards to the BeforeUpgradeData struct is beneficial for capturing and comparing delegation state before and after the upgrade. This enhances the comprehensiveness of the upgrade tests.

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

231-231: LGTM! Adding StakingKeeper enhances delegation data verification.

The addition of StakingKeeper to migrateDistribution aligns with the PR's objective of fixing delegation and reward data verification during upgrades.

Run the following script to verify the migration logic:

✅ Verification successful

Verification confirms proper keeper usage in migration

The implementation in app/upgrades/v8/distribution.go shows correct usage of both keepers, with StakingKeeper specifically utilized for delegator data migration via migrateDelegatorStartingInfos, while DistrKeeper handles all reward-related migrations.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of migrateDistribution function.

# Test: Search for the function implementation to ensure it properly uses both keepers.
ast-grep --pattern $'func migrateDistribution(ctx sdk.Context, stakingKeeper *stakingkeeper.Keeper, distrKeeper *distributionkeeper.Keeper) error {
  $$$
}'

Length of output: 157


Script:

#!/bin/bash
# Search with a more generic pattern
ast-grep --pattern 'func migrateDistribution($$$) error'

# Backup with ripgrep
echo "=== Function definition ==="
rg "func migrateDistribution" -A 10

echo "=== Function usage ==="
rg "migrateDistribution\(" -A 2

Length of output: 2882

go.mod (1)

76-76: LGTM! Zerolog is now a direct dependency.

The removal of the // indirect comment correctly reflects that zerolog is now directly used in the codebase, supporting enhanced logging during upgrade testing.

Comment on lines +54 to +71
func migrateDelegatorStartingInfos(ctx sdk.Context, stakingKeeper *fxstakingkeeper.Keeper, distrKeeper distributionkeeper.Keeper) error {
var err error
validatorMap := make(map[string]stakingtypes.Validator)
distrKeeper.IterateDelegatorStartingInfos(ctx, func(valAddr sdk.ValAddress, addr sdk.AccAddress, info types.DelegatorStartingInfo) bool {
info.Stake = fxtypes.SwapDecAmount(info.Stake)
var delegation stakingtypes.Delegation
delegation, err = stakingKeeper.GetDelegation(ctx, addr, valAddr)
if err != nil {
return true
}
validator, ok := validatorMap[delegation.ValidatorAddress]
if !ok {
validator, err = stakingKeeper.GetValidator(ctx, valAddr)
if err != nil {
return true
}
validatorMap[delegation.ValidatorAddress] = validator
}
info.Stake = validator.TokensFromSharesTruncated(delegation.Shares)
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure Proper Error Handling and Propagation

In the migrateDelegatorStartingInfos function, errors within the iterator callback are not being captured in the err variable. Specifically:

  • At line 60~, if stakingKeeper.GetDelegation returns an error, the error is not assigned to err.
  • At line 66~, if stakingKeeper.GetValidator returns an error, the error is not assigned to err.

As a result, the function may return nil even when an error has occurred during iteration.

Apply this diff to capture and propagate errors correctly:

  		delegation, err = stakingKeeper.GetDelegation(ctx, addr, valAddr)
  		if err != nil {
+ 			err = fmt.Errorf("failed to get delegation for delegator %s and validator %s: %w", addr.String(), valAddr.String(), err)
  			return true
  		}
  			validator, err = stakingKeeper.GetValidator(ctx, valAddr)
  			if err != nil {
+ 				err = fmt.Errorf("failed to get validator %s: %w", valAddr.String(), err)
  				return true
  			}

This ensures that any errors encountered are properly returned and can be handled upstream.

📝 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 migrateDelegatorStartingInfos(ctx sdk.Context, stakingKeeper *fxstakingkeeper.Keeper, distrKeeper distributionkeeper.Keeper) error {
var err error
validatorMap := make(map[string]stakingtypes.Validator)
distrKeeper.IterateDelegatorStartingInfos(ctx, func(valAddr sdk.ValAddress, addr sdk.AccAddress, info types.DelegatorStartingInfo) bool {
info.Stake = fxtypes.SwapDecAmount(info.Stake)
var delegation stakingtypes.Delegation
delegation, err = stakingKeeper.GetDelegation(ctx, addr, valAddr)
if err != nil {
return true
}
validator, ok := validatorMap[delegation.ValidatorAddress]
if !ok {
validator, err = stakingKeeper.GetValidator(ctx, valAddr)
if err != nil {
return true
}
validatorMap[delegation.ValidatorAddress] = validator
}
info.Stake = validator.TokensFromSharesTruncated(delegation.Shares)
func migrateDelegatorStartingInfos(ctx sdk.Context, stakingKeeper *fxstakingkeeper.Keeper, distrKeeper distributionkeeper.Keeper) error {
var err error
validatorMap := make(map[string]stakingtypes.Validator)
distrKeeper.IterateDelegatorStartingInfos(ctx, func(valAddr sdk.ValAddress, addr sdk.AccAddress, info types.DelegatorStartingInfo) bool {
var delegation stakingtypes.Delegation
delegation, err = stakingKeeper.GetDelegation(ctx, addr, valAddr)
if err != nil {
err = fmt.Errorf("failed to get delegation for delegator %s and validator %s: %w", addr.String(), valAddr.String(), err)
return true
}
validator, ok := validatorMap[delegation.ValidatorAddress]
if !ok {
validator, err = stakingKeeper.GetValidator(ctx, valAddr)
if err != nil {
err = fmt.Errorf("failed to get validator %s: %w", valAddr.String(), err)
return true
}
validatorMap[delegation.ValidatorAddress] = validator
}
info.Stake = validator.TokensFromSharesTruncated(delegation.Shares)

@fx0x55 fx0x55 merged commit 3890139 into main Jan 22, 2025
13 checks passed
@fx0x55 fx0x55 deleted the fx0x55/check-delegate branch January 22, 2025 14:20
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