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

[TRA-115] decommission vaults at the beginning of a block #1264

Merged
merged 4 commits into from
Mar 28, 2024
Merged

Conversation

tqin7
Copy link
Contributor

@tqin7 tqin7 commented Mar 27, 2024

Changelist

in beginblock, decommission a vault if its total shares is positive and equity is non-positive

Test Plan

unit tests

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Copy link

linear bot commented Mar 27, 2024

Copy link

coderabbitai bot commented Mar 27, 2024

Warning

Rate Limit Exceeded

@tqin7 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 8 minutes and 55 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between c150cc8 and 8c8216e.

Walkthrough

The recent updates streamline vault management by automating vault decommissioning at each block's start using the new BeginBlocker function. These changes encompass calculating vault equity, defining decommission conditions, and executing the decommission process by removing shares. Tests validate the correct implementation of vault decommissioning procedures.

Changes

File(s) Change Summary
protocol/x/vault/abci.go Added BeginBlocker function for vault decommissioning.
protocol/x/vault/keeper/vault.go Introduces vault management methods, including equity calculation and decommissioning processes.
protocol/x/vault/keeper/vault_test.go Includes tests for vault decommissioning, covering share setting, vault decommission, and share deletion.
protocol/x/vault/module.go Implemented BeginBlock method to execute ABCI logic for the vault module.

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-tests 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 tests 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 tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

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

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8f5edc7 and 38df410.
Files selected for processing (4)
  • protocol/x/vault/abci.go (1 hunks)
  • protocol/x/vault/keeper/vault.go (1 hunks)
  • protocol/x/vault/keeper/vault_test.go (1 hunks)
  • protocol/x/vault/module.go (2 hunks)
Additional comments: 7
protocol/x/vault/abci.go (1)
  • 8-13: The implementation of BeginBlocker is concise and follows the expected pattern for Cosmos SDK modules. It's crucial to ensure that the DecommissionVaults method in the keeper is thoroughly tested, given its impact on the system state at the beginning of each block.
protocol/x/vault/keeper/vault.go (3)
  • 14-28: Consider adding documentation to the GetVaultEquity method to explain its purpose and the meaning of the returned equity value. This can improve code readability and maintainability.
  • 31-64: While the logic for decommissioning vaults based on their equity is sound, consider refining the error handling strategy. Instead of logging errors directly within the loop, it might be beneficial to aggregate errors and handle them after the loop completes. Additionally, assess the performance implications of iterating through all vaults, especially in scenarios with a large number of vaults.
  • 67-83: The DecommissionVault method is implemented correctly. Consider adding documentation to explain its purpose, especially the implications of deleting total and owner shares for a vault. This can aid in understanding the method's impact on the system state.
protocol/x/vault/keeper/vault_test.go (2)
  • 17-90: The test TestDecomissionVaults effectively covers the decommissioning logic. However, consider correcting the typo in the test name to TestDecommissionVaults. Additionally, adding more detailed comments explaining the test setup and the expected outcomes would enhance readability and maintainability.
  • 92-134: Similar to the previous comment, correct the typo in TestDecomissionVault to TestDecommissionVault. Enhancing the comments to explain the test setup and assertions in more detail would also be beneficial for future maintainability.
protocol/x/vault/module.go (1)
  • 151-158: The implementation of the BeginBlock method is correct and follows the expected pattern for integrating ABCI BeginBlock logic in Cosmos SDK modules. Ensure that telemetry is correctly configured to capture the metrics as intended.

@tqin7 tqin7 changed the title decommission vaults at the beginning of a block [TRA-115] decommission vaults at the beginning of a block Mar 28, 2024
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8f5edc7 and b4ec84f.
Files selected for processing (4)
  • protocol/x/vault/abci.go (1 hunks)
  • protocol/x/vault/keeper/vault.go (1 hunks)
  • protocol/x/vault/keeper/vault_test.go (1 hunks)
  • protocol/x/vault/module.go (2 hunks)
Files skipped from review as they are similar to previous changes (4)
  • protocol/x/vault/abci.go
  • protocol/x/vault/keeper/vault.go
  • protocol/x/vault/keeper/vault_test.go
  • protocol/x/vault/module.go

}

// DecommissionVaults decommissions all vaults with positive shares and non-positive equity.
func (k Keeper) DecommissionVaults(
Copy link
Contributor

@Christopher-Li Christopher-Li Mar 28, 2024

Choose a reason for hiding this comment

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

nit: can we clarify this function name? It sounds like DecomissionVaults would decomission all of the vaults, maybe DecommissionInvalidVaults

}
}

// DecommissionVault decommissions a vault by deleting its tota shares and owner shares.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// DecommissionVault decommissions a vault by deleting its tota shares and owner shares.
// DecommissionVault decommissions a vault by deleting its total shares and owner shares.

Comment on lines +42 to +46
// Skip if TotalShares is non-positive.
totalSharesRat, err := totalShares.ToBigRat()
if err != nil || totalSharesRat.Sign() <= 0 {
continue
}
Copy link
Contributor

Choose a reason for hiding this comment

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

is it positive for totalShares to be negative? How could that happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

negative shouldn't happen, but just added this check here to be future-proof

Copy link
Contributor

Choose a reason for hiding this comment

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

Curious, will this have to be updated when withdrawal logic is added? It's possible for a vault to have 0 shares and 0 equity in that case right? Non-blocking as that isn't part of Phase 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah 0 shares is possible. will think about that

}

// DecommissionVault decommissions a vault by deleting its tota shares and owner shares.
func (k Keeper) DecommissionVault(
Copy link
Contributor

Choose a reason for hiding this comment

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

iiuc, if a user attempts to deposit in this vault before it's decommissioned, it will be recreated in the same block its decommissioned and a new vault will be created right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep! correct

Copy link
Contributor

@vincentwschau vincentwschau Mar 28, 2024

Choose a reason for hiding this comment

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

Is that the case? If a user deposits to the vault before begin block (when decomissioning occurs), then it would have happened in the previous block. What re-creates it in the new block?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

deposit tx is processed after begin block. so if a vault is ready to be decommissioned, begin block would decommission it and then when deposit tx is processed, it will be created again.

if a user deposits before begin block, then that deposit tx is processed in the previous block

Comment on lines 78 to 89
// Check that total shares and owner shares are not deleted for vault 0.
got, exists := k.GetTotalShares(ctx, vault0)
require.Equal(t, true, exists)
require.Equal(t, shares, got)
got, exists = k.GetOwnerShares(ctx, vault0, constants.Alice_Num0.Owner)
require.Equal(t, true, exists)
require.Equal(t, shares, got)
// Check that total shares and owner shares are deleted for vault 1.
_, exists = k.GetTotalShares(ctx, vault1)
require.Equal(t, false, exists)
_, exists = k.GetOwnerShares(ctx, vault1, constants.Bob_Num0.Owner)
require.Equal(t, false, exists)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: shouldn't this be multiple separate test cases?

Comment on lines 97 to 98
// Decomission a non-existent vault.
k.DecommissionVault(ctx, constants.Vault_Clob_0)
Copy link
Contributor

Choose a reason for hiding this comment

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

what are you testing with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

making sure that the function doesn't panic on non-existent vaults

@@ -0,0 +1,135 @@
package keeper_test
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking nit: Both tests here can be broken up into separate test cases w/ descriptions which would be more helpful for someone running the test if anything fails.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b4ec84f and 12e85cc.
Files selected for processing (3)
  • protocol/x/vault/abci.go (1 hunks)
  • protocol/x/vault/keeper/vault.go (1 hunks)
  • protocol/x/vault/keeper/vault_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • protocol/x/vault/abci.go
  • protocol/x/vault/keeper/vault.go
  • protocol/x/vault/keeper/vault_test.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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 12e85cc and c150cc8.
Files selected for processing (1)
  • protocol/x/vault/keeper/vault_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/x/vault/keeper/vault_test.go

@tqin7 tqin7 merged commit 1eeee45 into main Mar 28, 2024
16 checks passed
@tqin7 tqin7 deleted the tq/tra-115 branch March 28, 2024 16:39
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.

None yet

3 participants