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(store/v2): implement the feature to upgrade the store keys #20453

Merged
merged 46 commits into from
Aug 5, 2024

Conversation

cool-develope
Copy link
Contributor

@cool-develope cool-develope commented May 25, 2024

Description

Closes: #19784


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • New "Upgrades" section in documentation detailing the LoadVersionAndUpgrade API for managing store keys.
    • Introduced methods in MetadataStore for better management of versioned keys, particularly for removed keys.
  • Bug Fixes

    • Updated dependency versions, potentially enhancing stability and functionality.
  • Documentation

    • Significant updates to README, improving clarity on upgrade processes, key management, and the responsibilities of root.Store.

@cool-develope cool-develope requested a review from a team as a code owner May 25, 2024 14:36
@cool-develope cool-develope marked this pull request as draft May 25, 2024 14:37
Copy link
Contributor

coderabbitai bot commented May 25, 2024

Warning

Rate limit exceeded

@cool-develope has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 16 minutes and 54 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 f50b207 and 65bce60.

Walkthrough

Walkthrough

The recent changes enhance store/v2 by enabling store upgrades, including the addition and removal of store keys, along with data migration functionalities. Key improvements introduce new interfaces and methods to streamline key-value store management and systematic handling of upgrades across different store components. Additionally, dependency updates ensure compatibility and stability.

Changes

Files Change Summaries
store/v2/go.mod, runtime/v2/go.mod, server/v2/go.mod, server/v2/cometbft/go.mod Updated dependency versions for cosmossdk.io/core and github.com/cosmos/iavl; added new dependency on golang.org/x/exp in store/v2.
store/v2/README.md Enhanced documentation on root.Store, introduced "Upgrades" section, and clarified pruning processes.
store/v2/commitment/metadata.go Introduced new methods in MetadataStore for managing versions and handling removed store keys.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant KVStore
    participant UpgradeStore
    participant Database
    User->>KVStore: Request Store Upgrade
    KVStore->>UpgradeStore: LoadVersionAndUpgrade(upgrades)
    UpgradeStore->>Database: MigrateStoreKey(oldKey, newKey)
    Database-->>UpgradeStore: Key Migration Status
    UpgradeStore-->>KVStore: Migration Complete
    KVStore-->>User: Confirm Upgrade
Loading

Assessment against linked issues

Objective (Issue #) Addressed Explanation
Implement Mounter interface to create the new commit tree (19784)
Add store keys (19784)
Remove store keys (19784)
Prune module state related to removed module (19784) While migration and pruning methods are implemented, specifics on module state pruning are unclear.

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
Contributor

@cool-develope your pull request is missing a changelog!

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

Outside diff range and nitpick comments (1)
store/v2/commitment/store.go (1)

Line range hint 124-179: The refactoring of LoadVersion to support store upgrades and rollbacks is comprehensive. Consider adding detailed comments within the method to explain the handling of store keys and the conditions under which versions are rolled back or initialized.

// LoadVersion updates the store to the target version. If the target version is less than the latest, it rolls back to that version.
// It also handles upgrades by setting initial versions for new or renamed store keys.
func (c *CommitStore) LoadVersion(targetVersion uint64, upgrades *corestore.StoreUpgrades) error {
  // existing code...
}
Review Details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits Files that changed from the base of the PR and between 5930ee3 and 34f301a.
Files selected for processing (3)
  • store/v2/commitment/store.go (4 hunks)
  • store/v2/database.go (2 hunks)
  • store/v2/root/store.go (2 hunks)
Additional Context Used
Path-based Instructions (3)
store/v2/database.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/commitment/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/root/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (5)
store/v2/database.go (1)

59-59: The updated comment for SetInitialVersion provides clearer information on its functionality.

store/v2/commitment/store.go (3)

9-9: The addition of the sort package supports the new sorting functionality in LoadVersion, ensuring deterministic iteration order for store upgrades.


148-155: The sorting logic ensures that store keys are processed in a consistent order when applying upgrades, which is crucial for maintaining state consistency across versions.


497-497: The modification to call LoadVersion at the end of the Restore method helps ensure the store is correctly updated post-restore. Verify that this change does not introduce performance issues or affect data integrity.

store/v2/root/store.go (1)

218-218: The update to LoadLatestVersion to call loadVersion directly simplifies the version loading process and maintains consistency with other version loading methods.

store/v2/root/store.go Outdated Show resolved Hide resolved
store/v2/root/store.go Fixed Show fixed Hide fixed
Copy link
Contributor

@alpe alpe left a comment

Choose a reason for hiding this comment

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

Good start. I don't have the full picture but rename and delete seem to not be fully implemented, yet.

store/v2/commitment/store.go Show resolved Hide resolved
store/v2/commitment/store.go Outdated Show resolved Hide resolved
@cool-develope cool-develope marked this pull request as ready for review June 3, 2024 17:54
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.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 34f301a and 787c2f1.

Files selected for processing (9)
  • core/store/upgrade.go (1 hunks)
  • store/v2/commitment/iavl/tree.go (1 hunks)
  • store/v2/commitment/store.go (5 hunks)
  • store/v2/commitment/store_test_suite.go (2 hunks)
  • store/v2/commitment/tree.go (2 hunks)
  • store/v2/database.go (3 hunks)
  • store/v2/pruning/manager.go (2 hunks)
  • store/v2/root/store.go (3 hunks)
  • store/v2/root/upgrade_test.go (1 hunks)
Additional context used
Path-based instructions (9)
core/store/upgrade.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/commitment/tree.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/database.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/pruning/manager.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/commitment/iavl/tree.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/root/upgrade_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

store/v2/commitment/store_test_suite.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/root/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/commitment/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

golangci-lint
store/v2/pruning/manager.go

13-13: undefined: store


15-15: undefined: store


17-17: undefined: store


19-19: undefined: store


23-23: undefined: store

store/v2/commitment/iavl/tree.go

19-19: undefined: store


29-29: undefined: Config


30-30: undefined: iavl.AsyncPruningOption


116-116: t.tree.SetCommitting undefined (type *"github.com/cosmos/iavl".MutableTree has no field or method SetCommitting)


118-118: t.tree.UnsetCommitting undefined (type *"github.com/cosmos/iavl".MutableTree has no field or method UnsetCommitting)


133-133: undefined: Exporter


145-145: undefined: Importer

store/v2/root/upgrade_test.go

24-24: undefined: store


28-28: undefined: MigrateStoreTestSuite


37-37: undefined: storeKeys


49-49: undefined: New


57-57: undefined: storeKeys


73-73: undefined: storeKeys


89-89: undefined: New

store/v2/commitment/store_test_suite.go

29-29: undefined: CommitStore


129-129: undefined: store

store/v2/root/store.go

33-33: undefined: store


36-36: undefined: store


22-22: undefined: store


67-67: undefined: store


68-68: undefined: store


72-72: undefined: store


130-130: undefined: store


134-134: undefined: store


166-166: undefined: store


115-115: undefined: NewReaderMap


127-127: undefined: NewReaderMap


177-177: undefined: store


182-182: undefined: store


191-191: undefined: store


197-197: undefined: store


206-206: undefined: store

store/v2/commitment/store.go

46-46: undefined: Tree


32-32: undefined: store


34-34: undefined: store


35-35: undefined: store


50-50: undefined: Tree


491-491: undefined: ErrorExportDone


523-523: undefined: Importer


525-525: storeKey declared and not used


8-8: "math" imported and not used

Additional comments not posted (10)
core/store/upgrade.go (1)

44-55: The implementation of IsRenamed method is correct and aligns with the PR objectives to handle store key upgrades.

store/v2/commitment/tree.go (1)

61-66: The KVStoreGetter interface and its method GetKVStoreWithBatch are correctly implemented and essential for the upgrade functionality.

store/v2/database.go (1)

40-40: Ensure all implementations of the Committer interface are updated to include the new upgrades parameter in the LoadVersion method.

store/v2/pruning/manager.go (1)

76-118: The PruneKVStores method is correctly implemented and handles the pruning of KVStores based on the batch flush threshold efficiently.

store/v2/commitment/iavl/tree.go (1)

17-40: The IavlTree struct correctly implements the required interfaces and provides essential functionality for the upgrade process.

Tools
golangci-lint

19-19: undefined: store


29-29: undefined: Config


30-30: undefined: iavl.AsyncPruningOption

store/v2/root/upgrade_test.go (1)

20-125: The UpgradeStoreTestSuite is well-implemented and provides comprehensive test coverage for the store upgrade functionality.

Tools
golangci-lint

24-24: undefined: store


28-28: undefined: MigrateStoreTestSuite


37-37: undefined: storeKeys


49-49: undefined: New


57-57: undefined: storeKeys


73-73: undefined: storeKeys


89-89: undefined: New

store/v2/commitment/store_test_suite.go (2)

22-22: New constant storeKey3 added for testing.

This constant is likely used to simulate a scenario with an additional store key, which is relevant for the new upgrade tests.


171-229: New test function TestStore_Upgrades added.

This function tests the upgrade process by simulating store operations before and after applying store upgrades. It checks for correct behavior when adding, renaming, and deleting store keys. The test is comprehensive and covers the critical functionality introduced in this PR.

store/v2/root/store.go (1)

224-224: Updated LoadVersionAndUpgrade and loadVersion methods to handle store upgrades.

These changes are crucial for enabling the store to handle version upgrades and apply necessary modifications to the store keys as specified by the upgrades. The implementation checks for migration status and correctly integrates the pruning logic for deleted and renamed keys.

Also applies to: 233-233, 237-271, 273-276

store/v2/commitment/store.go (1)

Line range hint 120-223: Updated LoadVersion method and added migrateKVStore method to handle store upgrades.

The LoadVersion method now supports rolling back or creating new commit info based on the target version, which is essential for handling snapshots and upgrades. The migrateKVStore method provides the functionality to migrate data between keys, which is necessary for handling renamed keys during upgrades.

Also applies to: 225-282

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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 787c2f1 and cc5803d.

Files selected for processing (1)
  • store/v2/root/upgrade_test.go (1 hunks)
Additional context used
Path-based instructions (1)
store/v2/root/upgrade_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

golangci-lint
store/v2/root/upgrade_test.go

25-25: undefined: store


29-29: undefined: MigrateStoreTestSuite


38-38: undefined: storeKeys


50-50: undefined: New


58-58: undefined: storeKeys


74-74: undefined: storeKeys


90-90: undefined: New

Additional comments not posted (3)
store/v2/root/upgrade_test.go (3)

32-66: Review of SetupTest method.

The method correctly sets up the test environment, including logging and database connections. It also commits a changeset, which is a good practice for testing state changes.

Tools
golangci-lint

38-38: undefined: storeKeys


50-50: undefined: New


58-58: undefined: storeKeys


68-92: Review of loadWithUpgrades method.

The method correctly handles the loading of the store with upgrades, including handling renamed and added store keys.

Tools
golangci-lint

74-74: undefined: storeKeys


90-90: undefined: New


94-126: Review of TestLoadVersionAndUpgrade method.

The method correctly tests the loading of the store version with upgrades, including handling new, renamed, and deleted store keys.

store/v2/root/upgrade_test.go Show resolved Hide resolved
store/v2/root/upgrade_test.go Outdated Show resolved Hide resolved
store/v2/root/upgrade_test.go Show resolved Hide resolved
store/v2/root/upgrade_test.go Outdated Show resolved Hide resolved
store/v2/commitment/store.go Fixed Show resolved Hide resolved
store/v2/database.go Outdated Show resolved Hide resolved
Copy link
Contributor

@alpe alpe left a comment

Choose a reason for hiding this comment

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

Nice updates. 🏇
I added some comments but I have only a very limited understanding of the original code. I would also need some help to understand your test design.

store/v2/commitment/store.go Fixed Show resolved Hide resolved
store/v2/commitment/store.go Outdated Show resolved Hide resolved
store/v2/commitment/store.go Outdated Show resolved Hide resolved
store/v2/commitment/store.go Outdated Show resolved Hide resolved
store/v2/commitment/store.go Show resolved Hide resolved
store/v2/pruning/manager.go Outdated Show resolved Hide resolved
store/v2/pruning/manager.go Outdated Show resolved Hide resolved
store/v2/commitment/store_test_suite.go Show resolved Hide resolved
store/v2/root/upgrade_test.go Outdated Show resolved Hide resolved
core/store/upgrade.go Outdated Show resolved Hide resolved
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: 3

Outside diff range and nitpick comments (1)
store/v2/storage/storage_test_suite.go (1)

643-694: Consider adding more detailed comments to the TestUpgradable function to explain the purpose of each major step.

This will improve the maintainability of the test code, especially for new developers or when revisiting this code after some time.

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cc5803d and 52a0148.

Files selected for processing (10)
  • store/v2/commitment/store.go (5 hunks)
  • store/v2/database.go (4 hunks)
  • store/v2/root/store.go (3 hunks)
  • store/v2/storage/pebbledb/db.go (4 hunks)
  • store/v2/storage/rocksdb/db.go (2 hunks)
  • store/v2/storage/rocksdb/db_test.go (1 hunks)
  • store/v2/storage/rocksdb/iterator.go (1 hunks)
  • store/v2/storage/sqlite/db.go (2 hunks)
  • store/v2/storage/storage_test_suite.go (1 hunks)
  • store/v2/storage/store.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • store/v2/storage/rocksdb/db_test.go
Additional context used
Path-based instructions (9)
store/v2/storage/rocksdb/iterator.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/database.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/storage/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/storage/rocksdb/db.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/storage/sqlite/db.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/storage/pebbledb/db.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/root/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/commitment/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/storage/storage_test_suite.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (23)
store/v2/storage/rocksdb/iterator.go (1)

127-129: The implementation of the Timestamp method looks correct and adheres to the expected behavior for accessing timestamp data from a RocksDB iterator.

store/v2/database.go (1)

28-35: The addition of the UpgradableDatabase interface with methods for pruning and migrating store keys is well-defined and aligns with the objectives of enabling seamless store modifications.

store/v2/storage/store.go (2)

21-21: The implementation of the UpgradableDatabase interface in the StorageStore class is correctly done.


141-162: The methods PruneStoreKey and MigrateStoreKey are well-implemented, with appropriate checks for the database's capability to handle these operations. This ensures that the operations are only attempted on databases that support them.

store/v2/storage/rocksdb/db.go (2)

31-32: The implementation of the UpgradableDatabase interface in the Database class is correctly done.


202-239: The methods PruneStoreKey and MigrateStoreKey are well-implemented. The PruneStoreKey method correctly does nothing, as pruning in RocksDB is handled by compaction. The MigrateStoreKey method includes comprehensive logic for copying keys, which is essential for data migration.

store/v2/storage/sqlite/db.go (3)

47-48: The implementation of the UpgradableDatabase interface is correctly declared.


246-262: The PruneStoreKey method correctly implements the functionality to prune a specific store key. Ensure proper transaction handling and error management.


264-283: The MigrateStoreKey method correctly implements the functionality to migrate data from one store key to another. Ensure transactional integrity and error handling are maintained throughout the operation.

store/v2/storage/pebbledb/db.go (3)

35-36: The implementation of the UpgradableDatabase interface is correctly declared.


328-351: The PruneStoreKey method correctly implements the functionality to prune a specific store key. Ensure proper batch handling and error management.


353-382: The MigrateStoreKey method correctly implements the functionality to migrate data from one store key to another. Ensure batch operations are handled correctly and efficiently.

store/v2/root/store.go (3)

224-224: The LoadLatestVersion method correctly delegates to loadVersion with no upgrades specified. This is appropriate for loading the latest version without modifications.


233-233: The LoadVersion method correctly delegates to loadVersion with no upgrades specified. This is consistent with the expected behavior when loading a specific version.


236-288: The LoadVersionAndUpgrade method correctly implements the functionality to load a specific version and apply upgrades. Ensure that the migration check and the handling of store key pruning are correctly implemented.

store/v2/commitment/store.go (6)

9-9: Added import "sort" to support deterministic iteration over keys during upgrades.


28-28: Introduced a new constant batchFlushThreshold set to 64KB, likely used to manage batch operations efficiently.


35-35: Implemented interfaces store.Committer, snapshots.CommitSnapshotter, store.PausablePruner, and store.KVStoreGetter to enhance the functionality of CommitStore.


239-296: Added migrateKVStore method to migrate data from an old key to a new key. This method ensures data consistency during key migration by using batch operations and handling large data migrations efficiently with batchFlushThreshold.


630-638: Implemented GetKVStoreWithBatch to retrieve a KVStoreWithBatch instance from the multiTrees map if available. This method supports the new interface store.KVStoreGetter.


Line range hint 133-237: Updated LoadVersion method to handle upgrades and rollback scenarios. The method now supports loading a specific version with potential store upgrades and handles different cases like rollback, snapshot restore, and store key modifications (add, delete, rename).

Verification successful

The rg output shows extensive usage of storeKey across various files, including tests and core implementation files. The LoadVersion method in store/v2/commitment/store.go handles added, deleted, and renamed store keys. To ensure these modifications are correctly managed in other parts of the codebase, we need to verify the handling of renamed and deleted store keys in the core implementation files.

Let's focus on verifying the handling of renamed and deleted store keys in the core implementation files, especially in the context of the LoadVersion method.


The search results confirm that the handling of renamed and deleted store keys is implemented correctly in the core implementation files. Specifically, the LoadVersion method in store/v2/commitment/store.go includes logic to manage renamed and deleted store keys, and this handling is consistent across other parts of the codebase.

  • The RenamedFrom function is used to check if a store key has been renamed and to migrate the data accordingly.
  • The IsDeleted function is used to check if a store key has been deleted and to remove the corresponding tree.

These functions are utilized in the LoadVersion method and other relevant parts of the codebase, ensuring that store key modifications are handled correctly.

  • store/v2/commitment/store.go
  • store/types/store.go
  • store/rootmulti/store.go
  • core/store/upgrade.go
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that all store keys modified by upgrades are handled correctly in other parts of the codebase.

# Test: Search for usage of modified store keys. Expect: Proper handling of renamed and deleted keys.
rg --type go $'storeKey'

Length of output: 47311



Script:

#!/bin/bash
# Description: Verify handling of renamed and deleted store keys in core implementation files.

# Test: Search for handling of renamed store keys.
rg --type go 'RenamedFrom' --context 5

# Test: Search for handling of deleted store keys.
rg --type go 'IsDeleted' --context 5

Length of output: 6730

store/v2/storage/storage_test_suite.go (2)

669-671: Verify the behavior of PruneStoreKey method in different scenarios.

This script will help ensure that PruneStoreKey is tested under various conditions, which is crucial for understanding its behavior in edge cases and ensuring robustness.


684-685: Check the error handling for MigrateStoreKey.

This script will help confirm that error handling for MigrateStoreKey is comprehensive and covers various failure scenarios, which is essential for maintaining data integrity during migrations.

Verification successful

The extracted implementations of the MigrateStoreKey method across different files show that error handling is indeed present within the method itself. Each implementation checks for errors at various stages and returns appropriate error messages.

However, the rg command to extract the usage of MigrateStoreKey failed due to a regex parse error. To ensure comprehensive verification, we need to correct this and re-run the script to check how errors are handled when MigrateStoreKey is called.


The corrected rg command output shows the usage of the MigrateStoreKey method in various files, including store/v2/storage/storage_test_suite.go. The error handling within the MigrateStoreKey method itself appears to be robust, as it checks for errors at multiple stages and returns appropriate error messages.

However, the test suite usage (store/v2/storage/storage_test_suite.go) only checks for no errors using s.Require().NoError(err), which is a standard practice in tests to ensure the method does not return an error under expected conditions.

Given the comprehensive error handling within the method implementations and the standard testing practices observed, the error handling for MigrateStoreKey appears to be adequately covered.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that error handling for MigrateStoreKey is robust across different scenarios.

# Test: Search for the method usage and related error handling tests. Expect: Comprehensive error handling tests.
rg --type go $'MigrateStoreKey'

Length of output: 937



Script:

#!/bin/bash
# Extract the implementation of the MigrateStoreKey method in different files
ast-grep --lang go --pattern $'func (db *Database) MigrateStoreKey(fromStoreKey, toStoreKey []byte) error {
  $$$
}' --json

ast-grep --lang go --pattern $'func (ss *StorageStore) MigrateStoreKey(oldStoreKey, newStoreKey []byte) error {
  $$$
}' --json

# Extract the usage of the MigrateStoreKey method to check for error handling
rg --type go 'MigrateStoreKey(' --context 5

Length of output: 7394



Script:

#!/bin/bash
# Correct the regex pattern and re-run the search for the usage of MigrateStoreKey to check for error handling
rg --type go 'MigrateStoreKey\(' --context 5

Length of output: 4401

store/v2/root/store.go Outdated Show resolved Hide resolved
store/v2/storage/storage_test_suite.go Show resolved Hide resolved
store/v2/database.go Outdated Show resolved Hide resolved
store/v2/root/store.go Dismissed Show dismissed Hide dismissed
@cool-develope
Copy link
Contributor Author

could we get a diagram on how this feature works. we can put it in the repo as well

sequenceDiagram
    participant S as Store
    participant SS as StateStorage
    participant SC as StateCommitment
    alt SC is a UpgradeableStore
        S->>SC: LoadVersionAndUpgrade
        SC->>SC: Mount new store keys
        SC->>SC: Prune removed store keys
    end
    SC->>S: LoadVersion Result
    alt SS is a UpgradableDatabase
        S->>SS: PruneStoreKeys
    end
Loading

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

Outside diff range, codebase verification and nitpick comments (3)
store/v2/commitment/metadata.go (1)

113-126: Add logging for debugging purposes.

Adding logging can help in debugging issues related to removed store keys.

+ log.Printf("Flushing removed store keys for version %d: %v", version, storeKeys)
store/v2/commitment/store.go (1)

253-268: Enhance error messages in the GetProof method.

Consider providing more detailed error messages to improve debugging.

- return nil, fmt.Errorf("store %s not found: %w", storeKey, err)
+ return nil, fmt.Errorf("store %s not found while retrieving proof: %w", storeKey, err)
store/v2/root/store.go (1)

275-290: LGTM!

The loadVersion method correctly handles the presence of upgrades and applies them if necessary. The changes are clean and maintain the DRY principle.

Consider adding detailed comments to explain how upgrades are handled within this method for better clarity.

+ // loadVersion loads the specified version and applies any store upgrades if provided.
+ // If upgrades are provided, it checks if the state commitment supports upgrades and applies them.
Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 77d0533 and be3e5af.

Files ignored due to path filters (2)
  • simapp/v2/go.sum is excluded by !**/*.sum
  • store/v2/go.sum is excluded by !**/*.sum
Files selected for processing (9)
  • simapp/v2/go.mod (2 hunks)
  • store/v2/README.md (2 hunks)
  • store/v2/commitment/iavl/tree_test.go (4 hunks)
  • store/v2/commitment/metadata.go (3 hunks)
  • store/v2/commitment/store.go (9 hunks)
  • store/v2/commitment/store_test_suite.go (3 hunks)
  • store/v2/db/wrapper.go (1 hunks)
  • store/v2/go.mod (4 hunks)
  • store/v2/root/store.go (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • store/v2/commitment/store_test_suite.go
  • store/v2/go.mod
Additional context used
Path-based instructions (6)
store/v2/db/wrapper.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/README.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

store/v2/commitment/iavl/tree_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

store/v2/commitment/metadata.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/commitment/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/root/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

LanguageTool
store/v2/README.md

[uncategorized] ~56-~56: This expression is ususally spelled with a hyphen
Context: ...nd SC layers. This means pruning can be implementation specific, such as being synchronous or asynchron...

(SPECIFIC_HYPHEN)

Markdownlint
store/v2/README.md

63-63: null
Files should end with a single newline character

(MD047, single-trailing-newline)

Additional comments not posted (20)
store/v2/db/wrapper.go (4)

22-23: LGTM! Integration with corestore package.

The return type change from idb.Iterator to corestore.Iterator is consistent with the integration of the corestore package.


27-28: LGTM! Integration with corestore package.

The return type change from idb.Iterator to corestore.Iterator is consistent with the integration of the corestore package.


32-33: LGTM! Integration with corestore package.

The return type change from idb.Batch to corestore.Batch is consistent with the integration of the corestore package.


37-38: LGTM! Integration with corestore package.

The return type change from idb.Batch to corestore.Batch is consistent with the integration of the corestore package.

store/v2/README.md (4)

23-43: Well-documented upgrade process.

The LoadVersionAndUpgrade API section is clear and informative. The sequence diagram effectively illustrates the process.


45-47: Clear explanation of the pruning process.

The section accurately describes how pruning marks store keys as pruned without immediate data removal.


55-57: Clear explanation of pruning responsibility.

The section accurately describes that pruning is the responsibility of the underlying SS and SC layers.

Tools
LanguageTool

[uncategorized] ~56-~56: This expression is ususally spelled with a hyphen
Context: ...nd SC layers. This means pruning can be implementation specific, such as being synchronous or asynchron...

(SPECIFIC_HYPHEN)


61-63: Clear test coverage expectations.

The section outlines clear expectations for test coverage of logical components.

Tools
Markdownlint

63-63: null
Files should end with a single newline character

(MD047, single-trailing-newline)

store/v2/commitment/iavl/tree_test.go (1)

48-49: Improved error handling in tests.

The updates to error handling during version retrieval and other operations improve the robustness of the test suite.

Also applies to: 59-61, 68-70, 112-114

store/v2/commitment/metadata.go (1)

15-15: LGTM!

The constant removedStoreKeyPrefix is correctly defined.

store/v2/commitment/store.go (6)

327-337: LGTM!

The method pruneRemovedStoreKeys is correctly implemented.


510-517: LGTM!

The method Close is correctly implemented.


Line range hint 298-306: LGTM!

The method Get is correctly implemented.


306-325: LGTM!

The method Prune is correctly implemented.


Line range hint 340-398: LGTM!

The method Snapshot is correctly implemented.


Line range hint 401-478: LGTM!

The method Restore is correctly implemented.

simapp/v2/go.mod (2)

96-96: Verify the version update for github.com/cosmos/iavl.

Ensure that the update to v1.3.0 does not introduce breaking changes.


8-8: Verify the version update for cosmossdk.io/core.

Ensure that the update to v0.12.1-0.20240725072823-6a2d039e1212 does not introduce breaking changes.

store/v2/root/store.go (2)

231-231: LGTM!

The LoadLatestVersion method correctly delegates to loadVersion with the new signature. The changes are clean and maintain the DRY principle.


240-240: LGTM!

The LoadVersion method correctly delegates to loadVersion with the new signature. The changes are clean and maintain the DRY principle.

store/v2/root/store.go Show resolved Hide resolved
store/v2/commitment/iavl/tree_test.go Show resolved Hide resolved
store/v2/commitment/metadata.go Show resolved Hide resolved
store/v2/commitment/metadata.go Outdated Show resolved Hide resolved
store/v2/commitment/store.go Show resolved Hide resolved
store/v2/README.md Outdated Show resolved Hide resolved
func (m *MetadataStore) setLatestVersion(version uint64) error {
var buf bytes.Buffer
buf.Grow(encoding.EncodeUvarintSize(version))
if err := encoding.EncodeUvarint(&buf, version); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

not really relevant to this PR, but why are we using varint encoding? Version is a 64 bit uint so isn't a static 8 byte little endian encoded int fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

afaik, it is introduced to optimize the data

}

// NewCommitStore creates a new CommitStore instance.
func NewCommitStore(trees map[string]Tree, db corestore.KVStoreWithBatch, logger corelog.Logger) (*CommitStore, error) {
func NewCommitStore(trees map[string]Tree, db corestore.KVStoreWithBatch, mountTreeFn MountTreeFn, logger corelog.Logger) (*CommitStore, error) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this API change.. mountTreeFn MountTreeFn in this constructor feels off from a DevEx perspective. Can it be defined internally in CommitStore?

if internal.IsMemoryStoreKey(key) {
trees[key] = mem.New()
return mem.New(), nil
} else {
switch storeOpts.SCType {
Copy link
Member

Choose a reason for hiding this comment

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

storeOpts.SCType appears to be the only value of importance bound within this closure when passed to NewCommitStore. If mountTreeFn is strictly required for that constructor I suggest passing the SC type instead since this iteration of store/v2 only supports one type of SC tree anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the main problem is that the Commitment is an abstraction layer, and has no idea of how to construct the tree.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we can rethink abstraction lines a bit?

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.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between be3e5af and 66d86ec.

Files ignored due to path filters (2)
  • simapp/v2/go.sum is excluded by !**/*.sum
  • store/v2/go.sum is excluded by !**/*.sum
Files selected for processing (3)
  • simapp/v2/go.mod (2 hunks)
  • store/v2/README.md (2 hunks)
  • store/v2/go.mod (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • simapp/v2/go.mod
Additional context used
Path-based instructions (1)
store/v2/README.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

LanguageTool
store/v2/README.md

[uncategorized] ~56-~56: This expression is ususally spelled with a hyphen
Context: ...nd SC layers. This means pruning can be implementation specific, such as being synchronous or asynchron...

(SPECIFIC_HYPHEN)

Markdownlint
store/v2/README.md

63-63: null
Files should end with a single newline character

(MD047, single-trailing-newline)

Additional comments not posted (5)
store/v2/README.md (2)

23-27: LGTM! Verify the accuracy of the diagram.

The description of the LoadVersionAndUpgrade API is clear and concise. The diagram helps illustrate the process.

Ensure the accuracy of the sequence diagram to match the actual implementation.


29-43: LGTM! Verify the accuracy of the diagram.

The sequence diagram is a helpful visual aid for understanding the upgrade process.

Ensure the accuracy of the sequence diagram to match the actual implementation.

store/v2/go.mod (3)

12-12: LGTM! Verify the compatibility of the new version.

The version of github.com/cosmos/iavl has been updated. Ensure it does not introduce breaking changes.

Verify the compatibility of the new version with the existing codebase.


21-21: LGTM! Verify the compatibility of the new dependency.

The golang.org/x/exp dependency has been added. Ensure it does not introduce compatibility issues.

Verify the compatibility of the new dependency with the existing codebase.


35-35: LGTM! Verify the compatibility of the new version.

The version of github.com/emicklei/dot has been updated. Ensure it does not introduce breaking changes.

Verify the compatibility of the new version with the existing codebase.

store/v2/README.md Show resolved Hide resolved
store/v2/README.md Show resolved Hide resolved
@github-actions github-actions bot added C:server/v2 Issues related to server/v2 C:server/v2 cometbft labels Aug 1, 2024
@cool-develope
Copy link
Contributor Author

reverted the bump iavl to v1.3.0, there are a lot of deps conflicts between v2 modules, I will figure out in the next PR

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.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 66d86ec and f50b207.

Files ignored due to path filters (3)
  • runtime/v2/go.sum is excluded by !**/*.sum
  • server/v2/cometbft/go.sum is excluded by !**/*.sum
  • server/v2/go.sum is excluded by !**/*.sum
Files selected for processing (4)
  • runtime/v2/go.mod (2 hunks)
  • server/v2/cometbft/go.mod (2 hunks)
  • server/v2/go.mod (2 hunks)
  • store/v2/commitment/metadata.go (3 hunks)
Files skipped from review due to trivial changes (1)
  • server/v2/cometbft/go.mod
Files skipped from review as they are similar to previous changes (1)
  • store/v2/commitment/metadata.go
Additional comments not posted (5)
runtime/v2/go.mod (2)

18-18: Dependency Update Approved: cosmossdk.io/core

The version update for cosmossdk.io/core from v0.12.1-0.20231114100755-569e3ff6a0d7 to v0.12.1-0.20240725072823-6a2d039e1212 is approved.


47-47: Dependency Update Approved: github.com/cosmos/iavl

The version update for github.com/cosmos/iavl from v1.2.1-0.20240725141113-7adc688cf179 to v1.2.1-0.20240731145221-594b181f427e is approved.

server/v2/go.mod (3)

18-18: Dependency Update Approved: cosmossdk.io/core

The version update for cosmossdk.io/core from v0.12.1-0.20231114100755-569e3ff6a0d7 to v0.12.1-0.20240725072823-6a2d039e1212 is approved.


59-59: Dependency Update Approved: github.com/cosmos/iavl

The version update for github.com/cosmos/iavl from v1.2.1-0.20240725141113-7adc688cf179 to v1.2.1-0.20240731145221-594b181f427e is approved.


62-62: Dependency Update Approved: github.com/emicklei/dot

The version update for github.com/emicklei/dot from v1.6.1 to v1.6.2 is approved.


for _, storeKey := range storeKeys {
key := []byte(fmt.Sprintf("%s%s", encoding.BuildPrefixWithVersion(removedStoreKeyPrefix, version), storeKey))
if err := batch.Set(key, []byte{}); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I think if a StoreInfo (below)

StoreInfo struct {
Name []byte
CommitID CommitID
Structure string
}

was stored here instead of packing all metadata into the key we would gain two key advantages:

  1. ability to preload (mount) deleted trees at construction time (i.e. in factory)
  2. maintain support for multiple SC types

In particular this PR makes (2) impossible since mountTreeFn is singular.

Copy link
Member

@kocubinski kocubinski left a comment

Choose a reason for hiding this comment

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

LGTM

@cool-develope cool-develope added this pull request to the merge queue Aug 5, 2024
Merged via the queue into main with commit 6d4097b Aug 5, 2024
75 checks passed
@cool-develope cool-develope deleted the store/upgrade branch August 5, 2024 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature(store/v2)]: upgrades
4 participants