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

feat: add v2.0 upgrade handler #75

Merged
merged 3 commits into from
Jul 24, 2024
Merged

Conversation

sheldonleedev
Copy link
Collaborator

@sheldonleedev sheldonleedev commented Jul 23, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a more dynamic and modular upgrade handling system, enhancing the application's upgrade management capabilities.
    • Added detailed migration processes to ensure agents are effectively transitioned from the Bitcoin staking module to the new agent management system.
    • Implemented a robust admin management feature with error handling for existing admin addresses.
  • Bug Fixes

    • Enhanced error handling during upgrade processes for better clarity and reliability.
  • Documentation

    • Updated documentation to reflect new upgrade structures and the addition of the SetAdmin method with its error handling.
  • Chores

    • Organized and streamlined the codebase for improved maintainability and clarity in upgrade management.

Copy link

coderabbitai bot commented Jul 23, 2024

Walkthrough

The recent changes significantly enhance the upgrade management system within the application, transitioning from a static list of upgrade plans to a dynamic UpgradeRouter. This shift improves modularity and flexibility, while new keeper interfaces expand application capabilities. Structured migration functions facilitate smoother transitions during upgrades, and enhanced administrative controls ensure better management of admin addresses, improving error handling and user management.

Changes

Files Change Summary
app/upgrade.go, app/upgrades/types.go Replaced static upgrade plans with a dynamic UpgradeRouter, enhancing upgrade handling. Introduced new keeper interfaces and updated the AppKeepers struct to include them. Added methods for better upgrade management and improved error handling.
app/upgrades/v200/migrations.go Introduced a migration function to transfer agent data from the Bitcoin staking module to the agent management system, ensuring relevant data is preserved during upgrades.
app/upgrades/v200/upgrades.go Implemented the upgrade mechanism for version 2.0, defining an Upgrade struct and an upgrade handler for systematic execution of migrations and initializations across modules, ensuring smooth transitions between versions.
x/agent/keeper/agent.go Added SetAdmin method to the keeper for managing admin addresses with error checks, enhancing administrative controls.
x/agent/types/errors.go Introduced a new error variable, ErrAdminExists, to improve error handling related to admin management, providing a specific case for scenarios involving user permissions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant UpgradeRouter
    participant AppKeepers
    participant AgentKeeper
    
    User->>UpgradeRouter: Initiate Upgrade
    UpgradeRouter->>AppKeepers: Retrieve Current State
    AppKeepers->>AgentKeeper: Migrate Agents
    AgentKeeper-->>AppKeepers: Confirm Migration
    AppKeepers-->>UpgradeRouter: Migration Complete
    UpgradeRouter-->>User: Upgrade Successful
Loading

🐰 In the meadow where upgrades bloom,
A rabbit hops, dispelling gloom.
With routers new and admin rights,
We leap to new, exciting heights!
So cheer for code, both swift and bright,
For upgrades bring us pure delight! 🌼


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

Outside diff range, codebase verification and nitpick comments (1)
app/upgrades/v200/migarations.go (1)

13-28: Consider adding logging for better traceability.

Adding logging can help trace the migration process and identify issues.

+	log.Printf("Migrating receiver: %s", receiver.Name)
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 203a109 and e20bed4.

Files selected for processing (6)
  • app/upgrade.go (2 hunks)
  • app/upgrades/types.go (3 hunks)
  • app/upgrades/v200/migarations.go (1 hunks)
  • app/upgrades/v200/upgrades.go (1 hunks)
  • x/agent/keeper/agent.go (1 hunks)
  • x/agent/types/errors.go (1 hunks)
Additional comments not posted (22)
app/upgrades/v200/migarations.go (1)

23-24: Verify the removal of receivers.

The TODO comment indicates uncertainty about the removal of params.Receivers from the BTC staking module.

Ensure that the Receivers field is correctly removed from the BTC staking module parameters.

x/agent/types/errors.go (1)

15-15: LGTM! The new error variable is consistent and enhances error handling.

The new error variable ErrAdminExists is correctly registered and follows the existing pattern.

app/upgrade.go (3)

14-17: LGTM! The initialization block is correct.

The initialization block correctly registers the v2.0 upgrade plan with the UpgradeRouter.


20-22: LGTM! The method promotes a clearer logic flow.

The RegisterUpgradePlans method correctly calls setupUpgradeStoreLoaders and setupUpgradeHandlers.


57-67: LGTM! The method aligns with the new dynamic routing system.

The setupUpgradeHandlers method correctly sets up upgrade handlers using the UpgradeRouter.

app/upgrades/v200/upgrades.go (8)

1-15: LGTM! The package declaration and import statements are correctly structured.

The imports are relevant to the functionality and follow standard practices.


17-28: LGTM! The Upgrade struct is correctly defined and initialized.

The struct initialization sets up the upgrade handler and store upgrades appropriately.


30-34: LGTM! The upgradeHandlerConstructor function signature is correct.

The function parameters and return type are appropriate for an upgrade handler.


35-38: Ensure correct module versioning.

The function sets the consensus version for the agent and plan modules. Verify that these versions are correct and necessary for the upgrade.


50-53: LGTM! The agent module initialization is correct.

The function migrates agents from BTCStaking to the agent module and handles errors appropriately.


55-62: LGTM! The plan module initialization is correct.

The function sets the plan parameters and handles errors appropriately.


64-66: LGTM! The token module initialization is correct.

The function sets the token parameters using the default values.


68-69: LGTM! The function returns the result of running migrations.

The function returns the updated version map and any errors from running migrations.

app/upgrades/types.go (6)

Line range hint 1-17:
LGTM! The package declaration and import statements are correctly structured.

The imports are relevant to the functionality and follow standard practices.


43-54: LGTM! The AppKeepers struct is correctly updated.

The struct definition includes all necessary keepers and follows standard practices.


59-61: LGTM! The UpgradeRouter struct is correctly defined.

The struct definition is appropriate for managing upgrades.


63-65: LGTM! The NewUpgradeRouter function is correctly implemented.

The function initializes a new UpgradeRouter instance.


75-77: LGTM! The Routers function is correctly implemented.

The function returns the map of registered upgrades.


79-81: LGTM! The UpgradeInfo function is correctly implemented.

The function returns the upgrade information for a given plan name.

x/agent/keeper/agent.go (3)

47-54: Ensure correct admin address validation.

The SetAdmin method checks if the sender is the current admin. Verify that the error handling and admin setting logic are correct.


Line range hint 55-62:
LGTM! The GetAdmin method is correctly implemented.

The method retrieves and returns the admin address from the store.


Line range hint 111-113:
LGTM! The setAdmin method is correctly implemented.

The method sets the admin address in the store.

Comment on lines +15 to +21
app.AgentKeeper.AddAgent(
ctx,
receiver.Name,
receiver.Addr, receiver.EthAddr,
"",
"",
)
Copy link

Choose a reason for hiding this comment

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

Handle errors from AddAgent.

The function does not handle potential errors from AddAgent. This could lead to silent failures.

-		app.AgentKeeper.AddAgent(
+		if err := app.AgentKeeper.AddAgent(
		    ctx,
		    receiver.Name,
		    receiver.Addr, receiver.EthAddr,
		    "",
		    "",
		); err != nil {
		    return 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
app.AgentKeeper.AddAgent(
ctx,
receiver.Name,
receiver.Addr, receiver.EthAddr,
"",
"",
)
if err := app.AgentKeeper.AddAgent(
ctx,
receiver.Name,
receiver.Addr, receiver.EthAddr,
"",
"",
); err != nil {
return err
}

Comment on lines +38 to +55
// configure store loader that checks if version == upgradeHeight and applies store upgrades
func (app *LorenzoApp) setupUpgradeStoreLoaders() {
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
app.Logger().Info("not found upgrade plan", "planName", planName, "err", err.Error())
panic(fmt.Sprintf("failed to read upgrade info from disk %s", err))
}

if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
return
}

if upgradeInfo.Name == planName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
// this configures a no-op upgrade handler for the planName upgrade
app.UpgradeKeeper.SetUpgradeHandler(planName, upgradeHandler)
// configure store loader that checks if version+1 == upgradeHeight and applies store upgrades
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, upgrades))
app.SetStoreLoader(
upgradetypes.UpgradeStoreLoader(
upgradeInfo.Height,
router.UpgradeInfo(upgradeInfo.Name).StoreUpgrades,
),
)
}
Copy link

Choose a reason for hiding this comment

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

Consider using a less aggressive error handling strategy.

Using panic for error handling might be too aggressive. Consider using error logging or returning an error.

-		panic(fmt.Sprintf("failed to read upgrade info from disk %s", err))
+		log.Fatalf("failed to read upgrade info from disk %s", 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
// configure store loader that checks if version == upgradeHeight and applies store upgrades
func (app *LorenzoApp) setupUpgradeStoreLoaders() {
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
app.Logger().Info("not found upgrade plan", "planName", planName, "err", err.Error())
panic(fmt.Sprintf("failed to read upgrade info from disk %s", err))
}
if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
return
}
if upgradeInfo.Name == planName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
// this configures a no-op upgrade handler for the planName upgrade
app.UpgradeKeeper.SetUpgradeHandler(planName, upgradeHandler)
// configure store loader that checks if version+1 == upgradeHeight and applies store upgrades
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, upgrades))
app.SetStoreLoader(
upgradetypes.UpgradeStoreLoader(
upgradeInfo.Height,
router.UpgradeInfo(upgradeInfo.Name).StoreUpgrades,
),
)
}
// configure store loader that checks if version == upgradeHeight and applies store upgrades
func (app *LorenzoApp) setupUpgradeStoreLoaders() {
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
log.Fatalf("failed to read upgrade info from disk %s", err)
}
if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
return
}
app.SetStoreLoader(
upgradetypes.UpgradeStoreLoader(
upgradeInfo.Height,
router.UpgradeInfo(upgradeInfo.Name).StoreUpgrades,
),
)
}

Comment on lines +67 to +73
func (r *UpgradeRouter) Register(u Upgrade) *UpgradeRouter {
if _, has := r.mu[u.UpgradeName]; has {
panic(u.UpgradeName + " already registered")
}
r.mu[u.UpgradeName] = u
return r
}
Copy link

Choose a reason for hiding this comment

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

Handle duplicate registration appropriately.

The Register function panics if an upgrade is already registered. Consider handling this case more gracefully, such as logging an error or returning an error.

-    panic(u.UpgradeName + " already registered")
+    return fmt.Errorf("%s already registered", u.UpgradeName)
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 (r *UpgradeRouter) Register(u Upgrade) *UpgradeRouter {
if _, has := r.mu[u.UpgradeName]; has {
panic(u.UpgradeName + " already registered")
}
r.mu[u.UpgradeName] = u
return r
}
func (r *UpgradeRouter) Register(u Upgrade) *UpgradeRouter {
if _, has := r.mu[u.UpgradeName]; has {
return fmt.Errorf("%s already registered", u.UpgradeName)
}
r.mu[u.UpgradeName] = u
return r
}

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e20bed4 and 0f7044a.

Files selected for processing (1)
  • app/upgrades/v200/upgrades.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/upgrades/v200/upgrades.go

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0f7044a and 10a61ab.

Files selected for processing (1)
  • app/upgrades/v200/upgrades.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/upgrades/v200/upgrades.go

@github-actions github-actions bot merged commit 375912d into main Jul 24, 2024
8 checks passed
@github-actions github-actions bot deleted the sheldon/upgrade-plan-v2.0 branch July 24, 2024 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants