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

Made StoreKey singleton #2018

Merged
merged 2 commits into from
Sep 24, 2024
Merged

Made StoreKey singleton #2018

merged 2 commits into from
Sep 24, 2024

Conversation

abi87
Copy link
Collaborator

@abi87 abi87 commented Sep 23, 2024

StoreKey is used as index in cosmos sdk DB code. Notably the whole struct is used as index, not its content; this means that if we call ProvideKVStoreKey twice (thus currently re-instantiating a key with the same content), the lookup by storeKey fails and its store may not be found, even if the store key content is the very same.
This is hardly an issue in beaconKit but it costed me some debugging time while trying wiring miniAvalanche in.
The change I propose here makes the storeKey a singleton and guards against nasty issue which would happen if ProveideKVStore is called multiple times.

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of the storeKey variable to ensure consistent access throughout the application.
  • Refactor

    • Simplified logic by replacing local instantiation of storeKey with a global instance, enhancing efficiency.

Copy link
Contributor

coderabbitai bot commented Sep 23, 2024

Walkthrough

The changes involve the declaration of the storeKey variable in the mod/node-core/pkg/components/depinject.go file. It has been modified from a local variable within the ProvideKVStoreKey function to a global singleton variable. This adjustment allows the function to return a pointer to the global storeKey, ensuring consistent usage of the same instance throughout the application.

Changes

File Change Summary
mod/node-core/pkg/components/depinject.go Added a global variable storeKey and updated the ProvideKVStoreKey function to return a pointer to this global variable instead of a local instance.

Poem

In the code where bunnies hop,
A singleton now takes the top.
With storeKey shining bright,
It simplifies our code's delight.
No more instances, just one key,
Hooray for changes, let’s all agree! 🐇✨


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.

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.

@abi87 abi87 self-assigned this Sep 23, 2024
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: ASSERTIVE

Commits

Files that changed from the base of the PR and between 89f7411 and d5da671.

Files selected for processing (1)
  • mod/node-core/pkg/components/depinject.go (1 hunks)
Additional comments not posted (2)
mod/node-core/pkg/components/depinject.go (2)

Line range hint 1-124: Overall, the changes look good and address the PR objectives.

The implementation of the singleton pattern for StoreKey should effectively resolve the issues mentioned in the PR objectives. The changes are minimal and focused, which reduces the risk of introducing new problems.

A few points to consider for future improvements:

  1. Document the rationale behind the singleton pattern in a comment, as suggested earlier.
  2. Consider adding unit tests to verify the singleton behavior of StoreKey.
  3. If not already done, update any documentation that might reference the old StoreKey implementation.

34-36: LGTM! Verify usage across the codebase.

The modification to ProvideKVStoreKey function effectively implements the singleton pattern for StoreKey. This change addresses the issue mentioned in the PR objectives where multiple calls to this function were causing problems.

To ensure this change doesn't introduce any unexpected behavior, let's verify the usage of ProvideKVStoreKey across the codebase:

This script will help us ensure that:

  1. All usages of ProvideKVStoreKey are still valid with the new implementation.
  2. There are no remaining direct instantiations of storetypes.NewKVStoreKey that should be replaced with ProvideKVStoreKey.
Verification successful

Verified! All usages of ProvideKVStoreKey are correctly updated, and there are no direct instantiations of storetypes.NewKVStoreKey remaining in the active codebase.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check usage of ProvideKVStoreKey across the codebase

# Search for function calls to ProvideKVStoreKey
echo "Searching for ProvideKVStoreKey usage:"
rg --type go "ProvideKVStoreKey\(\)" -C 3

# Search for any direct usage of storetypes.NewKVStoreKey
echo "\nChecking for any remaining direct usage of storetypes.NewKVStoreKey:"
rg --type go "storetypes\.NewKVStoreKey\(" -C 3

Length of output: 1792

Comment on lines +31 to +32
//nolint:gochecknoglobals // storeKey is a singleton.
var storeKey = storetypes.NewKVStoreKey("beacon")
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM! Consider adding a brief comment explaining the singleton pattern.

The introduction of a global storeKey variable aligns well with the PR objective of making StoreKey a singleton. This change should effectively prevent issues arising from multiple instantiations of the key.

Consider adding a brief comment explaining why the singleton pattern is used here. This can help future maintainers understand the rationale behind this design decision. For example:

 //nolint:gochecknoglobals // storeKey is a singleton.
+// Using a singleton pattern ensures that only one instance of the StoreKey is used
+// throughout the application, preventing issues with multiple instantiations.
 var storeKey = storetypes.NewKVStoreKey("beacon")
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
//nolint:gochecknoglobals // storeKey is a singleton.
var storeKey = storetypes.NewKVStoreKey("beacon")
//nolint:gochecknoglobals // storeKey is a singleton.
// Using a singleton pattern ensures that only one instance of the StoreKey is used
// throughout the application, preventing issues with multiple instantiations.
var storeKey = storetypes.NewKVStoreKey("beacon")

Copy link

codecov bot commented Sep 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 22.37%. Comparing base (3c142f7) to head (7454f3e).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2018   +/-   ##
=======================================
  Coverage   22.37%   22.37%           
=======================================
  Files         358      358           
  Lines       16066    16065    -1     
  Branches       13       13           
=======================================
  Hits         3594     3594           
+ Misses      12323    12322    -1     
  Partials      149      149           
Flag Coverage Δ
22.37% <ø> (+<0.01%) ⬆️
Files with missing lines Coverage Δ
mod/node-core/pkg/components/depinject.go 0.00% <ø> (ø)

@ocnc ocnc merged commit 767e5b9 into main Sep 24, 2024
16 checks passed
@ocnc ocnc deleted the storeKey_singleton branch September 24, 2024 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants