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(core/coretesting): make memDB satisfy db.Db interface #22570

Merged
merged 3 commits into from
Nov 20, 2024

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Nov 20, 2024

Description

Closes: #22498


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 synchronous methods for setting and deleting key-value pairs in the database.
    • Added methods to print stored key-value pairs and retrieve database statistics.
  • Tests

    • Implemented type checks to ensure compatibility of MemDB with the expected database interface.
    • Updated module dependencies to include the latest version of cosmos-db.

Copy link
Contributor

coderabbitai bot commented Nov 20, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request introduce new methods to the MemDB struct in the core/testing/memdb.go file, including synchronous versions of existing methods and additional functionality for printing and retrieving database statistics. A new file, type_check.go, is added to the tests/integration package to perform compile-time type checks to ensure that coretesting.MemDB implements the db.DB interface.

Changes

File Change Summary
core/testing/memdb.go Added methods: SetSync, DeleteSync, Print, and Stats to MemDB struct.
tests/integration/type_check.go Introduced a variable to assert that coretesting.MemDB implements the db.DB interface.
tests/go.mod Updated module dependencies, added github.com/cosmos/cosmos-db, and removed an indirect requirement.

Assessment against linked issues

Objective Addressed Explanation
coretesting.MemDB is not db.DB: missing method DeleteSync (#22498)

Possibly related PRs

Suggested labels

C:x/accounts, C:x/accounts/base, C:x/accounts/lockup, C:x/accounts/multisig, C:x/authz, C:x/bank, C:x/distribution, C:x/gov, C:x/mint, C:x/nft, C:x/params, C:x/slashing, C:x/staking, C:x/upgrade

Suggested reviewers

  • tac0turtle
  • kocubinski
  • akhilkumarpilli
  • sontrinh16

📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between fbd4e03 and 5aee018.

📒 Files selected for processing (1)
  • tests/go.mod (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
tests/go.mod (1)

Pattern tests/**/*: "Assess the integration and e2e test code assessing sufficient code coverage for the changes associated in the pull request"

🔇 Additional comments (1)
tests/go.mod (1)

53-53: Verify the cosmos-db dependency version selection

The addition of github.com/cosmos/cosmos-db with a pre-release version (v1.0.3-0.20240911104526-ddc3f09bfc22) warrants verification:

  1. Consider if a stable release could be used instead of a pre-release version
  2. Ensure this specific version is required for the DB interface implementation
✅ Verification successful

Let me gather more information about the DB interface changes to understand if this specific pre-release version is necessary.


Let me check the actual usage in the integration test file and the latest stable version's interface.


Pre-release version appears necessary for interface compatibility

Based on the verification:

  • The pre-release version contains specific DB interface changes (commit message: "make a type alias in DB interface")
  • The integration test explicitly verifies that coretesting.MemDB implements db.DB interface
  • No security advisories were found for this package
  • The version is ahead of the latest stable release (v1.0.2), suggesting the interface changes are not yet in a stable release

The pre-release version seems justified as it contains necessary interface changes for type compatibility.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the cosmos-db dependency version and check for any security advisories

# Check available versions
echo "Available versions:"
gh api repos/cosmos/cosmos-db/tags --jq '.[].name'

# Check for security advisories
echo "Security advisories:"
gh api graphql -f query='
{
  securityVulnerabilities(first: 5, ecosystem: GO, package: "github.com/cosmos/cosmos-db") {
    nodes {
      advisory {
        summary
        severity
        publishedAt
      }
      vulnerableVersionRange
      firstPatchedVersion {
        identifier
      }
    }
  }
}'

# Check if the specific commit exists and its age
echo "Commit information:"
gh api repos/cosmos/cosmos-db/commits/ddc3f09bfc22 --jq '.commit.message'

Length of output: 767


Script:

#!/bin/bash
# Check the DB interface changes in the specific commit
gh api repos/cosmos/cosmos-db/commits/ddc3f09bfc22/files --jq '.[].patch'

# Check if the test files in the current repo use any new features from this commit
rg -l "github.com/cosmos/cosmos-db" tests/

Length of output: 437


Script:

#!/bin/bash
# Check how cosmos-db is used in the integration test
cat tests/integration/type_check.go

# Check the latest stable version's timestamp
gh api repos/cosmos/cosmos-db/git/refs/tags/v1.0.2 --jq '.object.url' | xargs gh api --jq '.committer.date'

Length of output: 526


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.

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no suggestions.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
core/testing/memdb.go (1)

316-325: Consider making Print more flexible and testable

While the implementation is thread-safe, consider these improvements:

  1. Allow customization of output destination (io.Writer) instead of direct stdout
  2. Return actual errors that might occur during writing
-func (db *MemDB) Print() error {
+func (db *MemDB) Print(w io.Writer) error {
   db.mtx.RLock()
   defer db.mtx.RUnlock()

   db.kv.tree.Ascend(item{}, func(i item) bool {
-    fmt.Printf("[%X]:\t[%X]\n", i.key, i.value)
+    if _, err := fmt.Fprintf(w, "[%X]:\t[%X]\n", i.key, i.value); err != nil {
+      return false
+    }
     return true
   })
   return nil
}
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 50c5963 and fbd4e03.

📒 Files selected for processing (2)
  • core/testing/memdb.go (2 hunks)
  • tests/integration/type_check.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • tests/integration/type_check.go
🧰 Additional context used
📓 Path-based instructions (1)
core/testing/memdb.go (1)

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

🔇 Additional comments (3)
core/testing/memdb.go (3)

294-296: LGTM: SetSync implementation is correct

The implementation appropriately delegates to Set method since memory operations are inherently synchronous. Thread safety is maintained through the existing mutex in Set.


304-306: LGTM: DeleteSync implementation is correct

The implementation appropriately delegates to Delete method since memory operations are inherently synchronous. Thread safety is maintained through the existing mutex in Delete.


327-335: LGTM: Stats implementation is clean and thread-safe

The implementation provides useful metadata about the database while maintaining thread safety through appropriate use of RLock.

@julienrbrt julienrbrt added this pull request to the merge queue Nov 20, 2024
Merged via the queue into main with commit d810b77 Nov 20, 2024
75 of 76 checks passed
@julienrbrt julienrbrt deleted the julien/coretesting branch November 20, 2024 15:41
alpe added a commit that referenced this pull request Nov 21, 2024
* main:
  build(deps): Bump cosmossdk.io/math from 1.3.0 to 1.4.0 (#22580)
  fix(server/v2/api/telemetry): enable global metrics  (#22571)
  refactor(server/v2/cometbft): add `codec.Codec` and clean-up APIs (#22566)
  feat(core/coretesting): make memDB satisfy db.Db interface (#22570)
  Merge commit from fork
  fix(server(/v2)): fix fallback genesis path (#22564)
  fix: only overwrite context chainID when necessary (#22568)
  docs(client): Update setFeeGranter and setFeePayer comments (#22526)
  fix(baseapp): populate header info in `NewUncachedContext` (#22557)
  build(deps): Bump buf.build/gen/go/cometbft/cometbft/protocolbuffers/go from 1.35.1-20240701160653-fedbb9acfd2f.1 to 1.35.2-20240701160653-fedbb9acfd2f.1 in /api (#22551)
  build(deps): Bump github.com/creachadair/atomicfile from 0.3.5 to 0.3.6 in /tools/confix (#22552)
  docs: Update reference of Approximation (#22550)
  fix(server/v2/comebft): wire missing services + fix simulation (#21964)
  ci: fix permissions for GITHUB_TOKEN on dependabot workflows (#22547)
  ci: fix permissions for GITHUB_TOKEN in spell check workflow (#22545)
  build(deps): Bump google.golang.org/protobuf from 1.35.1 to 1.35.2 (#22537)
  fix(cosmovisor): premature upgrade on restart (#22528)
  fix(store/v2/pebble): handle version 0 in keys (#22524)
  refactor(server/v2/telemetry): swap redirects (#22520)
  docs: Update content in  CODE_OF_CONDUCT.md (#22518)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: coretesting.MemDB is not db.DB: missing method DeleteSync
5 participants