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-446] include vault shares in genesis state #1774

Merged
merged 4 commits into from
Jun 26, 2024
Merged

[TRA-446] include vault shares in genesis state #1774

merged 4 commits into from
Jun 26, 2024

Conversation

tqin7
Copy link
Contributor

@tqin7 tqin7 commented Jun 25, 2024

Changelist

include vault total / owner shares in genesis state

besides general benefits of exporting state, this enables a network to automatically start with existing vaults (e.g. for testing purposes)

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.

Summary by CodeRabbit

  • New Features

    • Introduced Vault entity with fields for total shares, owner shares, and ID.
    • Added functionality to handle vaults and owner shares in the genesis state.
  • Bug Fixes

    • Ensured validation checks for vault parameters to avoid inconsistencies in ownership and shares.
  • Tests

    • Added tests to verify the retrieval and accuracy of owner shares for a vault.
  • Documentation

    • Updated genesis state JSON files to include empty vaults array for clarity and initialization purposes.

Copy link

linear bot commented Jun 25, 2024

Copy link
Contributor

coderabbitai bot commented Jun 25, 2024

Walkthrough

The changes introduce and integrate a new Vault entity within the dydxprotocol.vault package, expanding the capabilities to manage vaults and their corresponding shares. This includes updates to several files to handle initialization, exporting, validation, and share management of vaults. Additional testing functions ensure the functionality works as intended.

Changes

Files & Paths Change Summary
indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/genesis.ts Added Vault entity, updated encoding/decoding logic, and included vaults field in GenesisState.
proto/dydxprotocol/vault/genesis.proto Updated to include new Vault message types and fields.
protocol/x/vault/genesis.go Added logic for initializing and exporting vault parameters and shares in GenesisState.
protocol/x/vault/keeper/shares.go Added function to retrieve all owner shares of a vault.
protocol/x/vault/keeper/shares_test.go Added tests for GetAllOwnerShares function.
protocol/x/vault/keeper/vault.go Updated to retrieve all vaults, including total and owner shares.
protocol/x/vault/types/errors.go Added new error variables for invalid owner and mismatched shares.
protocol/x/vault/types/genesis.go Enhanced validation logic for GenesisState related to shares and ownership.
protocol/app/testdata/default_genesis_state.json Added an empty vaults array in the JSON structure.
protocol/scripts/genesis/sample_pregenesis.json Included an empty vaults array in the JSON structure.
protocol/testing/containertest/preupgrade_genesis.json Added an empty vaults array in the JSON object.
protocol/testutil/constants/genesis.go Added vaults array in the GenesisState constant JSON structure.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant GenesisState
    participant Vault
    participant Keeper
    participant Store

    User->>+GenesisState: Initialize GenesisState
    GenesisState-->>-Vault: Create Vault Entities
    GenesisState->>Keeper: Call InitGenesis
    Keeper->>Store: Store Vaults and Shares

    User->>Keeper: Retrieve All Vaults
    Keeper->>Store: Retrieve Vaults and Shares
    Store-->>Keeper: Return Vault and Shares
    Keeper-->>User: Return All Vaults and Shares

    User->>Keeper: Retrieve All Owner Shares
    Keeper->>Store: Retrieve Owner Shares
    Store-->>Keeper: Return Owner Shares
    Keeper-->>User: Return All Owner Shares
Loading

Poem

In the vaults where treasures lie,
Shares are split, the code's our guide.
Keeper counts with careful eye,
Data flows like a steady tide.
Errors caught, tests flew by.
The genesis takes a ride,
Into a future vast and wide. 🌟🔐


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 Configration 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
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.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d36863c and 677ac92.

Files ignored due to path filters (1)
  • protocol/x/vault/types/genesis.pb.go is excluded by !**/*.pb.go
Files selected for processing (8)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/genesis.ts (4 hunks)
  • proto/dydxprotocol/vault/genesis.proto (1 hunks)
  • protocol/x/vault/genesis.go (1 hunks)
  • protocol/x/vault/keeper/shares.go (1 hunks)
  • protocol/x/vault/keeper/shares_test.go (1 hunks)
  • protocol/x/vault/keeper/vault.go (1 hunks)
  • protocol/x/vault/types/errors.go (1 hunks)
  • protocol/x/vault/types/genesis.go (1 hunks)
Additional comments not posted (13)
proto/dydxprotocol/vault/genesis.proto (2)

16-26: Review the new Vault message definition.

The Vault message definition correctly captures the vault's ID, total shares, and owner shares as per the PR objectives. Ensure that the field numbers are consistent and do not overlap with existing fields in other messages within the same file to maintain protobuf backward compatibility.


6-7: Ensure proper usage of new imports.

You've added imports for query.proto and vault.proto. Verify that these are indeed necessary and correctly utilized in this file to avoid unnecessary dependencies.

Verification successful

Ensure proper usage of new imports.

The imports for query.proto and vault.proto are indeed utilized in the genesis.proto file, as evidenced by the presence of Vault and vaults in the protobuf definitions.

  • vault.proto: The term Vault and vaults are used in the protobuf definitions.

No further action is needed regarding these imports.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the imported files are used in the protobuf definitions.

# Test: Search for `query` and `vault` usage in the file. Expect: At least one usage of each.
rg --type proto --files-with-matches $'query|vault' proto/dydxprotocol/vault/genesis.proto

Length of output: 121



Script:

#!/bin/bash
# Description: Verify that the imported files are used in the protobuf definitions.

# Test: Search for `query` and `vault` usage in the file. Expect: At least one usage of each.
rg 'query|vault' proto/dydxprotocol/vault/genesis.proto

Length of output: 588

protocol/x/vault/genesis.go (1)

34-39: Review data export logic in ExportGenesis.

The logic to export vault parameters and vaults themselves in ExportGenesis seems correct. However, ensure this exported data is in the expected format and that all necessary data fields are included.

Verification successful

Review data export logic in ExportGenesis.

The ExportGenesis function correctly retrieves and exports vault parameters and vaults. The Params and Vault types include all necessary fields to ensure the data is in the expected format.

  • Params in protocol/x/vault/types/params.pb.go includes fields like Layers, SpreadMinPpm, SpreadBufferPpm, SkewFactorPpm, OrderSizePctPpm, OrderExpirationSeconds, and ActivationThresholdQuoteQuantums.
  • Vault in protocol/x/vault/types/genesis.pb.go includes fields like VaultId, TotalShares, and OwnerShares.

No further issues were found with the export logic.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the exported genesis state format.

# Test: Check the exported data format by invoking the function with a mock context. Expect: Correctly formatted output.
echo "Mock test to verify exported genesis state format."

Length of output: 110



Script:

#!/bin/bash
# Search for the ExportGenesis function implementation in the codebase
ast-grep --lang go --pattern 'func ExportGenesis($_, $_) $_' 

Length of output: 8117



Script:

#!/bin/bash
# Search for the GetParams method implementation in the codebase
ast-grep --lang go --pattern 'func (k Keeper) GetParams($_) $_' 

# Search for the GetAllVaults method implementation in the codebase
ast-grep --lang go --pattern 'func (k Keeper) GetAllVaults($_) $_' 

Length of output: 4071



Script:

#!/bin/bash
# Search for the Params type definition in the codebase
ast-grep --lang go --pattern 'type Params struct $_' 

# Search for the Vault type definition in the codebase
ast-grep --lang go --pattern 'type Vault struct $_' 

Length of output: 798



Script:

#!/bin/bash
# Search for the Params type definition in the codebase
ast-grep --lang go --pattern 'type Params struct {$$$}' 

# Search for the Vault type definition in the codebase
ast-grep --lang go --pattern 'type Vault struct {$$$}' 

Length of output: 7633

protocol/x/vault/types/genesis.go (1)

18-39: Enhance validation logic in GenesisState.Validate.

The validation logic correctly checks for non-negative shares and consistency between total and owner shares. However, consider adding more detailed comments to explain each validation step for better maintainability.

+ // Check if total shares are non-negative.
+ // Ensure each owner's shares are non-negative and sum up to total shares.
+ // Validate that the owner field is not empty.
protocol/x/vault/types/errors.go (1)

78-87: Validate new error definitions.

The new error definitions ErrInvalidOwner and ErrMismatchedTotalAndOwnerShares are appropriate and well-defined. Ensure that these errors are used consistently throughout the module wherever relevant conditions are checked.

protocol/x/vault/keeper/vault.go (1)

103-126: Review modifications in GetAllVaults.

The modifications to GetAllVaults are correctly implemented to include total shares and owner shares. Ensure that the function handles potential errors during data retrieval and aggregation, such as possible nil values or data inconsistencies.

indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/genesis.ts (5)

13-13: Approved the addition of vaults field in the GenesisState interfaces.

The addition of the vaults field in both GenesisState and GenesisStateSDKType interfaces is consistent with the PR's objective to include vault shares in the genesis state. This change is crucial for initializing the network with predefined vault states.

Also applies to: 22-22


26-46: Review of newly defined Vault and VaultSDKType interfaces.

The interfaces are well-defined with appropriate fields for vault ID, total shares, and owner shares. This aligns with the PR’s objectives and seems correctly implemented. However, consider adding comments for each field to enhance code readability and maintainability.

+ /** The unique identifier of the vault. */
  vaultId?: VaultId;
+ /** The total number of shares available in the vault. */
  totalShares?: NumShares;
+ /** The shares owned by each owner within the vault. */
  ownerShares: OwnerShare[];

62-64: Review of encoding and decoding logic for Vault within GenesisState.

The encoding and decoding functions for Vault within GenesisState have been correctly implemented. These functions are critical for ensuring the correct serialization and deserialization of vault data in the genesis state.

Also applies to: 82-84


98-99: Review of fromPartial implementation for GenesisState.

The implementation of fromPartial for GenesisState correctly handles optional fields and ensures that the vaults array is populated correctly if provided. This method aids in creating partial instances of GenesisState, which is useful in scenarios where not all data is available.


104-163: Review of Vault utility functions and methods.

The utility functions for creating base Vault instances and encoding/decoding them are well-implemented. These functions are essential for handling Vault data throughout the application. The use of protobuf for serialization ensures consistency and reliability in data handling.

protocol/x/vault/keeper/shares.go (1)

104-123: Review of the GetAllOwnerShares function.

The implementation of GetAllOwnerShares is robust, leveraging the getVaultOwnerSharesStore to fetch all owner shares associated with a given vault. The use of an iterator to traverse owner shares is efficient, and the proper handling of the iterator's closure via defer is commendable. This function is crucial for retrieving comprehensive ownership data, which aligns with the PR's objectives.

protocol/x/vault/keeper/shares_test.go (1)

135-168: Review of TestGetAllOwnerShares unit test.

The unit test TestGetAllOwnerShares is well-structured, covering scenarios with no owner shares and multiple owner shares. The use of ElementsMatch to verify the correctness of the retrieved data is appropriate, ensuring that the order of elements does not affect the test outcome. This test effectively validates the functionality of the GetAllOwnerShares method.

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.

Actionable comments posted: 0

Outside diff range and nitpick comments (2)
protocol/testutil/constants/genesis.go (1)

Line range hint 342-342: Potential security risk: Generic API key detected.

A potential API key was detected in the file. Review this to ensure that no sensitive keys are hardcoded in the source code. If this is a placeholder or example, consider removing it or using environment variables to handle sensitive data securely.

- "eth_address": "0xEf01c3A30eB57c91c40C52E996d29c202ae72193"
+ "eth_address": "<REPLACE_WITH_ENV_VAR>"
protocol/scripts/genesis/sample_pregenesis.json (1)

Line range hint 538-538: Potential Security Risk: Exposed Generic API Key

It appears that an API key might be exposed in the configuration, which can lead to security vulnerabilities if this file is accessible publicly or within a broader internal network.

- "eth_address": "0xsampleaddress",
+ "eth_address": "<REDACTED>",

Please ensure that sensitive keys are stored securely and not hard-coded in public or shared files.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 677ac92 and 869860c.

Files selected for processing (4)
  • protocol/app/testdata/default_genesis_state.json (1 hunks)
  • protocol/scripts/genesis/sample_pregenesis.json (1 hunks)
  • protocol/testing/containertest/preupgrade_genesis.json (1 hunks)
  • protocol/testutil/constants/genesis.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • protocol/app/testdata/default_genesis_state.json
Additional context used
Gitleaks
protocol/testutil/constants/genesis.go

342-342: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key)

protocol/scripts/genesis/sample_pregenesis.json

538-538: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key)

protocol/testing/containertest/preupgrade_genesis.json

675-675: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key)

Additional comments not posted (3)
protocol/testutil/constants/genesis.go (1)

1514-1515: Ensure proper initialization of the vaults array.

The empty vaults array has been added as per the PR's objective to include vault shares in the genesis state. Ensure that this array is properly populated during the initialization or migration scripts to reflect the intended functionality.

protocol/scripts/genesis/sample_pregenesis.json (1)

1829-1829: Ensure initialization of vaults array for genesis state.

The addition of an empty "vaults" array is consistent with the PR's objective to prepare the genesis state for including vault shares. This setup allows for easy addition of vault data in future configurations or during runtime initialization.

protocol/testing/containertest/preupgrade_genesis.json (1)

2319-2319: Ensure the vaults array initialization aligns with the intended genesis state configuration.

The addition of an empty vaults array is consistent with the PR's objective to include vault shares in the genesis state for testing purposes. This setup allows for future modifications to be made easily, such as adding specific vault data during tests.

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.

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 869860c and 223e0da.

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 cd16b21 into main Jun 26, 2024
34 checks passed
@tqin7 tqin7 deleted the tq/tra-446 branch June 26, 2024 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants