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-116] add x/vault total shares store #1179

Merged
merged 3 commits into from
Mar 15, 2024
Merged

[TRA-116] add x/vault total shares store #1179

merged 3 commits into from
Mar 15, 2024

Conversation

tqin7
Copy link
Contributor

@tqin7 tqin7 commented Mar 15, 2024

Changelist

Add TotalShares store (and getter and setter methods)

Test Plan

added unit tests in this PR

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 15, 2024

Copy link
Contributor

coderabbitai bot commented Mar 15, 2024

Walkthrough

The recent update introduces and enhances vault management functionalities within a blockchain protocol. It defines constants for vault types, adds and tests methods for managing and tracking the total shares of vaults, and improves documentation. Key developments include the introduction of a new constant for share management, the ability to convert vault IDs to state keys for easier access, and a systematic enhancement of the protocol's vault handling capabilities.

Changes

Files Change Summaries
.../testutil/constants/vault.go Defines constants for vault types Vault_Clob_0 and Vault_Clob_1.
.../vault/keeper/shares.go Adds functionality for managing total shares of a vault.
.../vault/keeper/shares_test.go Introduces tests for managing vault total shares.
.../vault/types/keys.go Adds comments and a new TotalSharesKeyPrefix constant.
.../vault/types/keys_test.go Introduces tests for module and state keys in the vault package.
.../vault/types/vault_id.go Adds a method ToStateKey() to convert VaultId to a byte slice.
.../vault/types/vault_id_test.go Introduces a test for converting vault IDs to state keys.
.../vault/types/errors.go Defines an error related to negative shares in 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
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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dd41d0a and 283092d.
Files selected for processing (7)
  • protocol/testutil/constants/vault.go (1 hunks)
  • protocol/x/vault/keeper/shares.go (1 hunks)
  • protocol/x/vault/keeper/shares_test.go (1 hunks)
  • protocol/x/vault/types/keys.go (1 hunks)
  • protocol/x/vault/types/keys_test.go (1 hunks)
  • protocol/x/vault/types/vault_id.go (1 hunks)
  • protocol/x/vault/types/vault_id_test.go (1 hunks)
Additional comments: 6
protocol/x/vault/types/keys.go (1)
  • 3-16: The addition of TotalSharesKeyPrefix and the comments for clarity are well-implemented. These changes enhance the readability and maintainability of the code.
protocol/testutil/constants/vault.go (1)
  • 1-16: The constants Vault_Clob_0 and Vault_Clob_1 are defined correctly, using appropriate types and following a consistent naming convention. These changes facilitate testing and implementation of vault-related features.
protocol/x/vault/types/keys_test.go (1)
  • 1-17: The tests introduced for verifying the correctness of module and state keys are well-structured and follow best practices for unit testing in Go. These tests ensure the reliability and correctness of the constants defined in the code.
protocol/x/vault/types/vault_id_test.go (1)
  • 1-16: The tests for the ToStateKey method are correctly implemented, ensuring that the method behaves as expected. However, if the ToStateKey method is updated to return an error (as suggested in the review of vault_id.go), these tests will need to be updated accordingly to handle the error.
protocol/x/vault/keeper/shares.go (1)
  • 9-34: The GetTotalShares and SetTotalShares methods are correctly implemented, providing a robust mechanism for managing total shares of a vault. The use of prefix.NewStore and the codec for marshaling and unmarshaling data are appropriate and align with best practices for interacting with the store in Cosmos SDK modules.
protocol/x/vault/keeper/shares_test.go (1)
  • 1-45: The tests for the getter and setter methods for total shares of a vault are well-structured and comprehensive, covering a variety of scenarios to ensure the correctness and reliability of the implementation. These tests are crucial for maintaining the robustness of the vault functionality within the protocol.

protocol/x/vault/types/vault_id.go Show resolved Hide resolved
var (
Vault_Clob_0 = vaulttypes.VaultId{
Type: vaulttypes.VaultType_VAULT_TYPE_CLOB,
Number: 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this field named Number instead of like Id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think Id is a bit confusing as VaultId already has Id in the name. For example, see subaccountId


b := store.Get(vaultId.ToStateKey())
if b == nil {
return val, false
Copy link
Contributor

Choose a reason for hiding this comment

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

how is val being returned here without being initialized? Is val being in the return of this function also initialize it? Does that also mean exists is initialized?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's correct. See here for more details.

ctx sdk.Context,
vaultId types.VaultId,
) (val types.NumShares, exists bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.TotalSharesKeyPrefix))
Copy link
Contributor

Choose a reason for hiding this comment

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

This is finding the existing keyvalue store that is associated with this keeper? Why do we use this instead of just storing this KV store directly in the keeper in x/vault/keeper.go?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We usually create a separate store (within KVStore) for each "functionality"

}

// SetTotalShares sets TotalShares for a vault.
func (k Keeper) SetTotalShares(
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if instead of having SetTotalShares, we should have incrementTotalShares, decrementTotalShares, or resetTotalShares(setting total shares to 0). It seems like we will never set totalShares to an arbitrary number, so SetTotalShares would require additional arithmetic.

nit: Should we panic on totalShares being a negative number?

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 we can build these increment decrement on top of Get and Set for sure.

As for verifying whether totalShares is non-negative, perhaps we can add that later when building out MsgDepositToVault?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

chatted with Vincent offline about this check on negative. will change this to return error if negative

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 283092d and 5614707.
Files selected for processing (3)
  • protocol/x/vault/keeper/shares.go (1 hunks)
  • protocol/x/vault/keeper/shares_test.go (1 hunks)
  • protocol/x/vault/types/errors.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • protocol/x/vault/keeper/shares.go
  • protocol/x/vault/keeper/shares_test.go
Additional comments: 1
protocol/x/vault/types/errors.go (1)
  • 7-12: The addition of ErrNegativeShares is well-defined and follows best practices for error handling within Cosmos SDK modules. The error message is clear and appropriately describes the condition being checked. Ensure that the error code 1 aligns with the existing error code sequence within the module to avoid conflicts.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

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

@tqin7 tqin7 merged commit 434bf65 into main Mar 15, 2024
17 checks passed
@tqin7 tqin7 deleted the tq/tra-116 branch March 15, 2024 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants