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): add new api LatestVersion #22305

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

cool-develope
Copy link
Contributor

@cool-develope cool-develope commented Oct 17, 2024

Description

Closes: #22259


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, you can find examples of the prefixes below:
  • 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.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

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

    • Introduced the LatestVersion method across various store structures, enhancing version management capabilities.
    • Updated LastBlockHeight method to reflect the most current application state.
  • Improvements

    • Changelog updated to document the addition of the LatestVersion method and previous version details.
  • Bug Fixes

    • Addressed a nil pointer panic when querying historical state.

Copy link
Contributor

coderabbitai bot commented Oct 17, 2024

📝 Walkthrough

Walkthrough

The pull request introduces modifications primarily to the LastBlockHeight method in the BaseApp struct, ensuring it utilizes the latest version from the commit multi-store. Additionally, a new LatestVersion method has been added across various store-related structs and interfaces, enhancing version management capabilities. The changelog has also been updated to reflect these changes. Overall, these updates aim to improve the efficiency of retrieving the last block height and managing version information within the application.

Changes

File Change Summary
baseapp/baseapp.go Updated LastBlockHeight method to use LatestVersion().
store/CHANGELOG.md Updated to include an entry for the new LatestVersion method in the Committer interface.
store/iavl/store.go Added LatestVersion method to Store struct.
store/mem/store.go Added LatestVersion method to Store struct.
store/rootmulti/dbadapter.go Added LatestVersion method to commitDBStoreAdapter struct, returning a hardcoded value of -1.
store/rootmulti/store.go Modified LatestVersion method to handle nil lastCommitInfo. Updated LastCommitID method.
store/transient/store.go Added LatestVersion method to Store struct.
store/types/store.go Added LatestVersion method to Committer and MultiStore interfaces.

Assessment against linked issues

Objective Addressed Explanation
Make the Committer interface have a GetLastVersion method (22259)
Make rootmulti store cache the LastCommitID (22259) No implementation for caching LastCommitID.

Possibly related PRs

  • feat(store/v2): add version exists #22235: The changes in this PR involve adding a "version exists" functionality in the store/v2/root/store.go, which relates to the LatestVersion method introduced in the main PR, as both are focused on version management within the store.

Suggested labels

C:server/v2, C:server/v2 cometbft

Suggested reviewers

  • kocubinski
  • lucaslopezf
  • tac0turtle
  • julienrbrt
  • hieuvubk

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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.

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

🧹 Outside diff range and nitpick comments (1)
store/rootmulti/store.go (1)

Line range hint 443-461: LGTM: Enhanced LastCommitID method with minor optimization suggestion

The changes to the LastCommitID method improve its robustness by properly handling cases where lastCommitInfo is nil or its CommitID hash is empty. This ensures that a valid CommitID structure is always returned.

A minor optimization could be made by pre-computing the empty hash:

var emptyHash = sha256.Sum256([]byte{})

func (rs *Store) LastCommitID() types.CommitID {
    // ... existing code ...
    appHash := emptyHash[:]
    // ... rest of the code ...
}

Also, consider adding a comment explaining why we're using an empty SHA256 hash in these cases for better code clarity.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 29ea9c8 and 5c729a6.

📒 Files selected for processing (8)
  • baseapp/baseapp.go (1 hunks)
  • store/CHANGELOG.md (1 hunks)
  • store/iavl/store.go (1 hunks)
  • store/mem/store.go (1 hunks)
  • store/rootmulti/dbadapter.go (1 hunks)
  • store/rootmulti/store.go (1 hunks)
  • store/transient/store.go (1 hunks)
  • store/types/store.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (8)
baseapp/baseapp.go (1)

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

store/CHANGELOG.md (1)

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

store/iavl/store.go (1)

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

store/mem/store.go (1)

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

store/rootmulti/dbadapter.go (1)

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

store/rootmulti/store.go (1)

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

store/transient/store.go (1)

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

store/types/store.go (1)

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

🔇 Additional comments (10)
store/rootmulti/dbadapter.go (1)

Line range hint 1-55: Overall structure and consistency look good

The new LatestVersion method fits well with the existing structure of the commitDBStoreAdapter. The file maintains consistency in terms of naming conventions and code organization, adhering to the Uber Go Style Guide.

store/transient/store.go (2)

Line range hint 1-58: Overall, the changes look good and align with the PR objectives.

The addition of the LatestVersion method to the Store struct completes the implementation of the Committer interface. This change is in line with the PR objectives to improve version management and potentially address performance issues.

The existing code remains unchanged, maintaining the overall structure and functionality of the Store class as a wrapper for a MemDB with Committer implementation.


45-48: LGTM. Consider adding a comment explaining the rationale.

The implementation of LatestVersion is correct and follows the Golang style guide. However, it might be helpful to add a comment explaining why it always returns 0 for a transient store.

Consider adding a comment like:

// LatestVersion implements Committer. It always returns 0 for a transient store
// as it doesn't persist data across commits.

Let's verify if there are any callers that might expect a non-zero version:

✅ Verification successful

Add a comment explaining why LatestVersion always returns 0

The LatestVersion method correctly always returns 0 for transient stores, as expected. To improve code clarity, consider adding a comment explaining this design choice.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for calls to LatestVersion() on Store or Committer types
rg --type go 'LatestVersion\(\)' -C 3

Length of output: 68860

store/CHANGELOG.md (1)

28-30: LGTM: Changelog entry is well-formatted and accurate.

The new changelog entry under the "Improvements" section accurately reflects the changes introduced in this PR. It follows the guidelines by including the module name, PR link, and a clear description of the improvement.

store/iavl/store.go (1)

149-152: LGTM: New method aligns with PR objectives

The new LatestVersion method is a well-implemented addition to the Store struct. It provides a direct way to retrieve the latest version of the IAVL tree, which aligns with the PR objectives of improving performance. The implementation is simple and efficient, leveraging the existing Version() method of the tree.

store/types/store.go (3)

Line range hint 191-193: LGTM: Consistent addition to MultiStore interface

The addition of LatestVersion() int64 to the MultiStore interface is consistent with the changes made to the Committer interface and aligns with the PR objectives.

Consider enhancing the comment to provide more context:

-	// LatestVersion returns the latest version in the store
+	// LatestVersion returns the latest version (height) of the store.
+	// This method is added to improve performance by avoiding unnecessary computations.

Ensure that all implementations of the MultiStore interface are updated to include this new method. Run the following command to find potential places that need updates:

#!/bin/bash
# Find all files that might implement the MultiStore interface
rg -t go "type \w+ struct" | rg -v "_test.go" | xargs -I {} rg -l "func \(\w+ \*?\w+\) (CacheMultiStore|GetStore|GetKVStore|TracingEnabled|SetTracer|SetTracingContext)" {} | xargs -I {} echo "Check file: {}"

24-24: Overall impact: Focused changes align with performance improvement goals

The additions of the LatestVersion() int64 method to both the Committer and MultiStore interfaces are well-contained and consistent. These changes align with the PR objectives of improving performance related to version retrieval without introducing breaking changes to the existing package structure.

Consider the following to ensure smooth integration of these changes:

  1. Update all implementations of Committer and MultiStore interfaces to include the new LatestVersion() method.
  2. Ensure that the new method is used in place of any existing inefficient version retrieval operations throughout the codebase.
  3. Add unit tests to verify the correct implementation and usage of the new LatestVersion() method.
  4. Update relevant documentation to reflect these changes and their performance implications.

To ensure comprehensive coverage of these changes, run the following command to find all files that might need updates or additional tests:

#!/bin/bash
# Find all files that might be affected by the new LatestVersion method
rg -t go "(Committer|MultiStore)" | rg -v "_test.go" | cut -d: -f1 | sort -u | xargs -I {} echo "Check file for updates and tests: {}"

Also applies to: 191-193


24-24: LGTM: New method aligns with performance improvement goals

The addition of LatestVersion() int64 to the Committer interface is a good step towards addressing the performance issues mentioned in the PR objectives. This method will allow for efficient retrieval of the latest version without unnecessary computations.

Ensure that all implementations of the Committer interface are updated to include this new method. Run the following command to find potential places that need updates:

store/rootmulti/store.go (1)

437-441: LGTM: Improved robustness in LatestVersion method

The changes to the LatestVersion method enhance its robustness by properly handling cases where lastCommitInfo is nil. This is a good improvement that ensures the method always returns a valid version, even when commit info hasn't been initialized yet.

baseapp/baseapp.go (1)

400-400: Approve the performance improvement in LastBlockHeight method.

The change from app.cms.LastCommitID().Version to app.cms.LatestVersion() addresses the performance issue mentioned in the PR objectives. This direct retrieval of the latest version from the commit multi-store should be more efficient.

To ensure this change doesn't have unintended consequences, please run the following verification:

Comment on lines +39 to +41
func (cdsa commitDBStoreAdapter) LatestVersion() int64 {
return -1
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consider revising the LatestVersion implementation

The current implementation of LatestVersion always returns -1, which may not accurately represent the latest version in all scenarios. This could lead to unexpected behavior or errors in code that relies on this method.

Consider the following suggestions:

  1. If -1 is intended to represent an uninitialized state, it might be better to return an error along with the version to indicate this special case.
  2. If possible, implement logic to return the actual latest version of the store.
  3. Add a comment explaining the rationale behind returning -1 if this is the intended behavior.

Here's a potential improvement:

func (cdsa commitDBStoreAdapter) LatestVersion() (int64, error) {
    // TODO: Implement logic to get the actual latest version
    return -1, errors.New("latest version not available")
}

This change would require updating the interface and other implementations as well.

@@ -59,4 +59,6 @@ func (s *Store) GetPruning() pruningtypes.PruningOptions {

func (s Store) LastCommitID() (id types.CommitID) { return }

func (s Store) LatestVersion() (version int64) { return }
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

⚠️ Potential issue

Standardize LatestVersion() Method Across Stores

The LatestVersion() method in store/mem/store.go currently returns an int64, whereas other store implementations return uint64. This inconsistency can lead to type mismatches and potential bugs.

To ensure consistency and prevent type-related issues:

  • Standardize the Return Type: Decide on a single return type (int64 or uint64) for the LatestVersion() method across all store implementations.
  • Implement the Method Correctly: Update the LatestVersion() method in store/mem/store.go to return the actual latest version instead of the zero value.
  • Review Method Naming: Ensure that all version-related methods (GetLatestVersion, LoadLatestVersion, etc.) are clearly defined and consistently named to reflect their functionality.
// LatestVersion returns the latest version of the store.
// This method is introduced to improve performance by avoiding
// repeated hash computations in GetLastBlockHeight.
func (s Store) LatestVersion() (version uint64) {
    // TODO: Implement the logic to return the latest version
    return 0
}
🔗 Analysis chain

Implement the LatestVersion() method

The LatestVersion() method has been added as per the PR objectives, but it currently has an empty implementation. This doesn't fulfill the intended purpose of returning the latest version of the store.

Please implement the method to return the actual latest version. Consider the following:

  1. How is the version information stored or tracked in the Store struct?
  2. Should this method read from a field, or calculate the version on-the-fly?

Also, add a comment explaining the purpose of this method. For example:

// LatestVersion returns the latest version of the store.
// This method is introduced to improve performance by avoiding
// repeated hash computations in GetLastBlockHeight.
func (s Store) LatestVersion() (version int64) {
    // TODO: Implement the logic to return the latest version
    return 0
}

To ensure this method is used correctly throughout the codebase, let's check its usage:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usages of LatestVersion method on Store
rg --type go 'Store.*LatestVersion\(\)'

Length of output: 3891

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]: Speedup baseapp.GetLastBlockHeight
6 participants