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-571] Add vault table and vault event version/subtype. #2263

Merged
merged 2 commits into from
Sep 17, 2024

Conversation

vincentwschau
Copy link
Contributor

@vincentwschau vincentwschau commented Sep 16, 2024

Changelist

Add subtype for vault event + vault table in Indexer.

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 a vault management system with default configurations and CRUD operations.
    • Added support for upsert events related to vaults, enhancing event handling capabilities.
  • Bug Fixes

    • Improved vault retrieval and upsert functionalities, ensuring data integrity.
  • Documentation

    • Expanded type definitions and interfaces for vaults, enhancing clarity and type safety.
  • Tests

    • Added comprehensive unit tests for vault management functionalities, ensuring robust performance.

@vincentwschau vincentwschau requested a review from a team as a code owner September 16, 2024 18:25
Copy link

linear bot commented Sep 16, 2024

Copy link
Contributor

coderabbitai bot commented Sep 16, 2024

Walkthrough

The changes introduce a comprehensive vault management system within a PostgreSQL-based application. This includes new constants, a migration for creating a vaults table, a model for vaults, and CRUD operations for vault records. Additionally, the implementation includes unit tests for the VaultTable module and enhancements to event handling with the addition of an upsert vault event subtype.

Changes

File Path Change Summary
.../helpers/constants.ts Added constants defaultVaultAddress and defaultVault for default vault configurations.
.../stores/vault-table.test.ts Introduced unit tests for VaultTable, covering creation, retrieval, and upsert operations for vaults.
.../migrations/migration_files/20240912180829_create_vaults_table.ts Created a new vaults table with specified columns and types.
.../helpers/db-helpers.ts Added 'vaults' to the layer1Tables array for database operations.
.../models/vault-model.ts Introduced VaultModel class defining the structure and validation rules for vault entities.
.../stores/vault-table.ts Implemented findAll, create, and upsert functions for vault operations in the database.
.../types/db-model-types.ts Added VaultFromDatabase interface to define vault entity structure in the database.
.../types/index.ts Exported all types from vault-types module for broader access.
.../types/query-types.ts Introduced VaultQueryConfig interface for enhanced querying capabilities for vaults.
.../types/vault-types.ts Added VaultCreateObject interface, VaultStatus enum, and VaultColumns enum for vault-related data.
.../events/constants.go Added SubtypeUpsertVault constant and UpsertVaultEventVersion for event handling related to vaults.

Possibly related PRs

Suggested labels

proto

Poem

🐰 In the land of vaults, so shiny and bright,
New constants and tables bring joy to the night.
With tests all around, our code is so neat,
Upsert events dance, oh what a treat!
Let's hop through the changes, with cheer in our hearts,
For vaults are the magic, where adventure starts! 🌟


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 67a75fd and 8bbc6a6.

Files selected for processing (1)
  • indexer/packages/postgres/tests/db/migrations.test.ts (1 hunks)
Additional comments not posted (1)
indexer/packages/postgres/__tests__/db/migrations.test.ts (1)

15-15: LGTM! Re-enabling the migration tests is a positive step.

The change from describe.skip to describe indicates that the migration is now considered stable and ready for testing. This is a good practice as it ensures that the migration's functionality and correctness are validated before integration.

Some additional points to consider:

  • Ensure that the migration has been thoroughly tested manually before enabling the automated tests.
  • Verify that the test cases cover all the critical aspects of the migration, including edge cases and potential failure scenarios.
  • Monitor the test results closely after re-enabling the tests to identify any unexpected failures or issues.

Overall, this change reflects a positive shift in the development process and demonstrates a commitment to maintaining code quality and reliability.


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 generate interesting stats about this repository and render them as a table.
    -- @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 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
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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 521ed1b and 67a75fd.

Files selected for processing (11)
  • indexer/packages/postgres/tests/helpers/constants.ts (2 hunks)
  • indexer/packages/postgres/tests/stores/vault-table.test.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240912180829_create_vaults_table.ts (1 hunks)
  • indexer/packages/postgres/src/helpers/db-helpers.ts (1 hunks)
  • indexer/packages/postgres/src/models/vault-model.ts (1 hunks)
  • indexer/packages/postgres/src/stores/vault-table.ts (1 hunks)
  • indexer/packages/postgres/src/types/db-model-types.ts (2 hunks)
  • indexer/packages/postgres/src/types/index.ts (1 hunks)
  • indexer/packages/postgres/src/types/query-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/vault-types.ts (1 hunks)
  • protocol/indexer/events/constants.go (3 hunks)
Additional comments not posted (27)
indexer/packages/postgres/src/types/vault-types.ts (3)

3-9: LGTM!

The VaultCreateObject interface provides a clear and well-structured definition for creating a vault. The property names are descriptive, and their types are appropriately defined. The use of the VaultStatus enum for the status property ensures type safety and clarity. Additionally, the IsoString type is correctly used for the createdAt and updatedAt properties to represent ISO 8601 formatted date strings.


11-16: LGTM!

The VaultStatus enum provides a clear and well-defined set of possible states for a vault. The state names are descriptive and follow a consistent naming convention (uppercase with underscores). These states likely represent different operational modes or conditions of the vault, enhancing the clarity and maintainability of the codebase.


18-24: LGTM!

The VaultColumns enum is a helpful addition that provides string constants for the property names of the VaultCreateObject interface. Using an enum for column names is a good practice for ensuring consistency and avoiding typos when referencing these properties in other parts of the codebase, such as database queries or data manipulation. This enhances the maintainability and reliability of the code.

indexer/packages/postgres/src/db/migrations/migration_files/20240912180829_create_vaults_table.ts (2)

3-16: LGTM!

The up migration function correctly creates the vaults table with appropriate columns and constraints. The chosen column types and the status enum values align well with the intended purpose of storing vault information.

Including the createdAt and updatedAt timestamps is a good practice for tracking vault creation and modification.


18-20: LGTM!

The down migration function correctly drops the vaults table, which is the appropriate way to revert the migration created by the up function.

indexer/packages/postgres/src/models/vault-model.ts (9)

1-3: LGTM!

The imports are relevant and necessary for the class implementation.


5-13: LGTM!

The VaultModel class correctly extends the BaseModel class. The static methods tableName and idColumn are properly defined.


15-33: LGTM!

The jsonSchema method correctly defines the structure and validation rules for the vaults. The required properties, types, and formats are properly specified.


35-36: LGTM!

The address property is correctly defined as a non-nullable string type.


37-38: LGTM!

The clobPairId property is correctly defined as a non-nullable string type.


39-40: LGTM!

The status property is correctly defined as a non-nullable VaultStatus type.


41-42: LGTM!

The createdAt property is correctly defined as a non-nullable IsoString type.


43-44: LGTM!

The updatedAt property is correctly defined as a non-nullable IsoString type.


5-44: LGTM!

The VaultModel class is well-structured and follows the necessary conventions for a data model. The static methods and properties are properly defined and typed. The JSON schema correctly defines the structure and validation rules for the vaults. The class provides a clear and consistent representation of the vaults data.

indexer/packages/postgres/src/types/index.ts (1)

35-35: LGTM!

The export statement follows the existing pattern and makes the vault types accessible to other parts of the codebase. This change enhances the module's interface by incorporating additional types related to vaults.

Note: Ensure that the exported vault types are used correctly and consistently throughout the codebase to maintain type safety and avoid any potential issues.

protocol/indexer/events/constants.go (2)

23-23: LGTM!

The addition of the new constant SubtypeUpsertVault is in line with the PR objective and follows the existing naming convention.


43-43: LGTM!

The addition of the new constant UpsertVaultEventVersion and the inclusion of SubtypeUpsertVault in the OnChainEventSubtypes array are consistent with the PR objective and the addition of the new subtype. The initial version is set correctly to 1.

Also applies to: 61-61

indexer/packages/postgres/__tests__/stores/vault-table.test.ts (2)

27-50: LGTM!

The test case is well-structured and covers the essential aspects of the findAll method. It ensures that all created vaults are retrieved correctly with the expected data.


52-80: LGTM!

The test case thoroughly verifies the upsert method by:

  1. Creating a vault and verifying its data.
  2. Upserting the vault with updated status and verifying the changes.

It ensures that the upsert method updates the vault data correctly while keeping the other data intact.

indexer/packages/postgres/src/stores/vault-table.ts (3)

21-77: LGTM!

The findAll function is well-structured and follows best practices for querying a database using an ORM. The use of optional filters and pagination allows for flexibility in querying the database. The function correctly verifies the presence of required fields before constructing the base query, and the base query is modified according to the provided filters and sorting options, which is a good practice. The default sorting order is set to clobPairId in ascending order, which is a reasonable default. The function returns all matching records, which is the expected behavior.


79-88: LGTM!

The create function is a simple wrapper around the Objection.js insert method. It correctly spreads the vaultToCreate object into the insert method, which is a good practice. The function returns the created vault record, which is the expected behavior.


90-100: LGTM!

The upsert function is a simple wrapper around the Objection.js upsert method. It correctly spreads the vaultToUpsert object into the upsert method, which is a good practice. The function returns the first vault from the resulting array of records, which is the expected behavior.

indexer/packages/postgres/src/helpers/db-helpers.ts (1)

34-34: LGTM!

The addition of the 'vaults' entry to the layer1Tables array is consistent with the PR objective of creating a vault table within the Indexer. This change ensures that the vaults table is included when deleting data using the dropData or clearData functions.

indexer/packages/postgres/src/types/db-model-types.ts (1)

305-311: LGTM!

The new VaultFromDatabase interface provides a clear and well-structured schema for vault entities in the database. The included properties are relevant and properly typed.

Exporting the interface makes it accessible for use in other parts of the codebase, enabling consistent handling of vault data across the system.

The changes enhance the data model without altering existing functionality, effectively accommodating new requirements related to vault management.

indexer/packages/postgres/src/types/query-types.ts (1)

348-352: LGTM!

The new VaultQueryConfig interface follows the existing pattern and enhances the querying capabilities for vaults. The optional properties allow for flexible filtering based on multiple addresses, CLOB pair IDs, and statuses.

indexer/packages/postgres/__tests__/helpers/constants.ts (2)

63-64: LGTM!

The import statement for VaultCreateObject and VaultStatus types is correctly placed and will likely be used in the subsequent code changes.


999-1007: LGTM!

The new constants defaultVaultAddress and defaultVault are implemented correctly:

  • The constants follow the naming convention and are exported for use in tests.
  • The defaultVault object is created with reasonable default values, including the correct usage of the VaultStatus enum.

These additions will be helpful for testing vault-related functionality.

Comment on lines +23 to +25
it('Successfully creates a vault', async () => {
await VaultTable.create(defaultVault);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Add assertions to verify the vault creation.

The test case ensures that the create method can be called without errors. However, it does not verify that the vault is actually created with the correct data.

Consider adding assertions to verify that the created vault matches the input data:

  it('Successfully creates a vault', async () => {
    await VaultTable.create(defaultVault);
+   const createdVault = await VaultTable.findAll({}, [], { readReplica: true });
+   expect(createdVault.length).toEqual(1);
+   expect(createdVault[0]).toEqual(expect.objectContaining(defaultVault));
  });
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
it('Successfully creates a vault', async () => {
await VaultTable.create(defaultVault);
});
it('Successfully creates a vault', async () => {
await VaultTable.create(defaultVault);
const createdVault = await VaultTable.findAll({}, [], { readReplica: true });
expect(createdVault.length).toEqual(1);
expect(createdVault[0]).toEqual(expect.objectContaining(defaultVault));
});

Copy link
Contributor

@tqin7 tqin7 left a comment

Choose a reason for hiding this comment

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

does indexer need to know about decommissioning of a vault? protocol decommissions a vault if it is deactivated and has non-positive equity

@@ -0,0 +1,20 @@
import * as Knex from 'knex';

export async function up(knex: Knex): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

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

for my understanding, this logic is run when indexer is upgraded?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

@vincentwschau
Copy link
Contributor Author

Indexer does not need to know about decommissioning of a vault, as the subaccount / balances for the vault will still exist.

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