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

Move memstore hydration initialized flag to memstore #2681

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

Conversation

jayy04
Copy link
Contributor

@jayy04 jayy04 commented Jan 10, 2025

Changelist

[Describe or list the changes made in this PR]

Test Plan

[Describe how this PR was tested (if applicable)]

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • Refactor

    • Streamlined memory store initialization management by consolidating fields and updating method names.
    • Introduced new methods for checking and setting memory store initialization state.
  • New Features

    • Added a new key for tracking memory store initialization status.
  • Tests

    • Updated tests to validate memory store initialization status instead of expecting a panic on re-initialization.

These changes improve the management and testing of memory store initialization, enhancing overall robustness and clarity.

@jayy04 jayy04 requested a review from a team as a code owner January 10, 2025 21:59
Copy link
Contributor

coderabbitai bot commented Jan 10, 2025

Walkthrough

The changes modify the CLOB (Central Limit Order Book) module's keeper implementation, focusing on how the memory store initialization state is managed. Instead of using multiple struct fields to track initialization, the implementation now uses a single atomic boolean field, inMemStructuresInitialized, along with context-based methods to get and set the initialization state. Two new methods, GetMemstoreInitialized and SetMemstoreInitialized, have been added to the Keeper, along with a corresponding constant KeyMemstoreInitialized in the keys file to support this new approach.

Changes

File Change Summary
protocol/x/clob/keeper/keeper.go - Removed initialized and memStoreInitialized fields
- Added GetMemstoreInitialized method
- Added SetMemstoreInitialized method
- Updated IsInitialized to IsInMemStructuresInitialized
- Modified initialization logic to use the new field and methods
protocol/x/clob/types/keys.go - Added KeyMemstoreInitialized constant for state management
protocol/x/clob/keeper/keeper_test.go - Updated TestInitMemStore_OnlyAllowedOnce to verify initialization state instead of expecting a panic
protocol/mocks/ClobKeeper.go - Updated IsInitialized to IsInMemStructuresInitialized and modified comments for clarity
protocol/x/clob/ante/clob.go - Updated AnteHandle method to check IsInMemStructuresInitialized instead of IsInitialized
protocol/x/clob/ante/clob_test.go - Updated mock behavior to use IsInMemStructuresInitialized
protocol/x/clob/types/clob_keeper.go - Updated IsInitialized method to IsInMemStructuresInitialized

Poem

🐰 A Rabbit's Ode to Initialization
In keeper's realm, a field did fade away,
Context now holds what structs once did sway.
Memstore's state, a key so bright and clear,
Initialization dances without fear! 🔑✨
A cleaner path, our code now takes flight.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6dcdcae and b4629a9.

📒 Files selected for processing (5)
  • protocol/mocks/ClobKeeper.go (3 hunks)
  • protocol/x/clob/ante/clob.go (1 hunks)
  • protocol/x/clob/ante/clob_test.go (1 hunks)
  • protocol/x/clob/keeper/keeper.go (5 hunks)
  • protocol/x/clob/types/clob_keeper.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • protocol/mocks/ClobKeeper.go
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: test-sim-import-export
  • GitHub Check: test-sim-multi-seed-short
  • GitHub Check: test-sim-after-import
  • GitHub Check: unit-end-to-end-and-integration
  • GitHub Check: test-race
  • GitHub Check: test-coverage-upload
  • GitHub Check: liveness-test
  • GitHub Check: container-tests
  • GitHub Check: Summary
🔇 Additional comments (8)
protocol/x/clob/types/clob_keeper.go (1)

16-16: LGTM! The new method name better reflects its purpose.

The renaming from IsInitialized() to IsInMemStructuresInitialized() improves clarity by explicitly indicating that it checks the initialization state of in-memory structures.

protocol/x/clob/ante/clob.go (1)

68-68: LGTM! The change is consistent with the interface update.

The method call has been updated to use IsInMemStructuresInitialized(), maintaining consistency with the interface changes.

protocol/x/clob/ante/clob_test.go (1)

56-56: LGTM! The mock setup is updated correctly.

The mock expectation has been updated to use IsInMemStructuresInitialized(), maintaining consistency with the interface changes.

protocol/x/clob/keeper/keeper.go (5)

56-56: Consider using a more concise field name.

Based on past review feedback, consider renaming inMemStructuresInitialized to inMemStructsInitialized for better conciseness while maintaining clarity.


105-126: LGTM! The initialization is correct and well-documented.

The inMemStructuresInitialized field is properly initialized with an atomic boolean, and the comment clarifies that it's false by default.


180-182: LGTM! The implementation is thread-safe and well-documented.

The method correctly uses atomic operations to check the initialization state, and both the name and comment clearly indicate its purpose.


187-192: LGTM! The error handling is improved.

The code now gracefully handles re-initialization attempts by returning early instead of panicking, and the comments clearly explain the initialization process and its implications.


312-323: LGTM! The memstore initialization methods are well-implemented.

The new methods correctly use the key-value store to track initialization state, following standard patterns and maintaining good encapsulation.

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@jayy04 jayy04 force-pushed the jy/memstore-hydration-fix branch from 863751f to 1c35383 Compare January 10, 2025 22:00
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

🧹 Nitpick comments (1)
protocol/x/clob/keeper/keeper.go (1)

312-323: Consider using a constant for the initialization flag value.

While the implementation is correct, consider defining a constant for the byte value 1 used in SetMemstoreInitialized. This would make the code more maintainable and self-documenting.

 // In types/keys.go
+const (
+    MemstoreInitializedValue byte = 1
+)

 // In keeper.go
 func (k Keeper) SetMemstoreInitialized(ctx sdk.Context) {
     store := ctx.KVStore(k.memKey)
     store.Set(
         []byte(types.KeyMemstoreInitialized),
-        []byte{1},
+        []byte{types.MemstoreInitializedValue},
     )
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b2674f5 and 1c35383.

📒 Files selected for processing (2)
  • protocol/x/clob/keeper/keeper.go (4 hunks)
  • protocol/x/clob/types/keys.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: test-sim-multi-seed-short
  • GitHub Check: test-race
  • GitHub Check: Summary
🔇 Additional comments (4)
protocol/x/clob/types/keys.go (1)

82-84: LGTM! Well-placed constant definition.

The new constant follows the existing naming conventions and is appropriately placed in the Memstore section.

protocol/x/clob/keeper/keeper.go (3)

56-56: LGTM! Correct retention of the initialized field.

The initialized field is correctly retained as it serves a different purpose than the memstore initialization flag.


187-191: LGTM! Clear initialization flow.

The initialization flow is maintained while properly utilizing the new memstore-based flag system.


256-263: LGTM! Proper initialization guard and flag setting.

The initialization check and flag setting ensure atomic initialization and prevent double initialization of the memstore.

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

🧹 Nitpick comments (1)
protocol/x/clob/keeper/keeper.go (1)

312-324: Consider adding error handling for store operations.

While the implementation is clean and focused, it might be worth adding error handling for store operations to handle potential edge cases.

Consider this implementation:

 func (k Keeper) SetMemstoreInitialized(ctx sdk.Context) {
     store := ctx.KVStore(k.memKey)
+    if store == nil {
+        panic("memstore is nil")
+    }
     store.Set(
         []byte(types.KeyMemstoreInitialized),
         []byte{1},
     )
 }

 func (k Keeper) GetMemstoreInitialized(ctx sdk.Context) bool {
     store := ctx.KVStore(k.memKey)
+    if store == nil {
+        panic("memstore is nil")
+    }
     return store.Has([]byte(types.KeyMemstoreInitialized))
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b2674f5 and 1c35383.

📒 Files selected for processing (2)
  • protocol/x/clob/keeper/keeper.go (4 hunks)
  • protocol/x/clob/types/keys.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test-race
  • GitHub Check: Summary
🔇 Additional comments (4)
protocol/x/clob/types/keys.go (1)

82-84: LGTM!

The new constant KeyMemstoreInitialized is well-placed in the "Memstore" section and follows the established naming conventions. The purpose is clearly documented in the comment.

protocol/x/clob/keeper/keeper.go (3)

56-56: LGTM!

The initialized field using atomic.Bool ensures thread-safe access to the initialization state.


187-189: LGTM!

The initialization flow is well-structured with clear separation of concerns.


256-263: LGTM!

The initialization check and flag setting in the memstore follow a clear and consistent pattern.

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

🧹 Nitpick comments (1)
protocol/x/clob/keeper/keeper_test.go (1)

Line range hint 26-34: Consider adding edge case tests for memstore initialization.

While the basic initialization and stateful order count cases are covered, consider adding tests for:

  1. Initialization with empty state
  2. Initialization after chain halt/resume
  3. Concurrent initialization attempts

Would you like me to provide example test implementations for these scenarios?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1c35383 and 6dcdcae.

📒 Files selected for processing (1)
  • protocol/x/clob/keeper/keeper_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: test-sim-multi-seed-short
  • GitHub Check: test-sim-import-export
  • GitHub Check: test-sim-after-import
  • GitHub Check: unit-end-to-end-and-integration
  • GitHub Check: test-race
  • GitHub Check: test-coverage-upload
  • GitHub Check: liveness-test
  • GitHub Check: container-tests
  • GitHub Check: Summary

require.Panics(t, func() {
ks.ClobKeeper.InitMemStore(ks.Ctx)
})
require.True(t, ks.ClobKeeper.GetMemstoreInitialized(ks.Ctx))
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

Test doesn't fully validate "OnlyAllowedOnce" behavior.

The test name suggests it should verify that initialization is only allowed once, but it only checks if the first initialization succeeds. It should also verify that subsequent initialization attempts are properly handled.

Consider updating the test to verify both cases:

 func TestInitMemStore_OnlyAllowedOnce(t *testing.T) {
     memClob := memclob.NewMemClobPriceTimePriority(false)
     ks := keepertest.NewClobKeepersTestContextWithUninitializedMemStore(
         t,
         memClob,
         &mocks.BankKeeper{},
         &mocks.IndexerEventManager{})

     // First initialization should succeed
     ks.ClobKeeper.InitMemStore(ks.Ctx)
     require.True(t, ks.ClobKeeper.GetMemstoreInitialized(ks.Ctx))

+    // Second initialization should not be allowed
+    secondCtx := ks.Ctx.WithBlockHeight(ks.Ctx.BlockHeight() + 1)
+    ks.ClobKeeper.InitMemStore(secondCtx)
+    
+    // Verify the initialization flag wasn't affected by second attempt
+    require.True(t, ks.ClobKeeper.GetMemstoreInitialized(secondCtx))
 }

Committable suggestion skipped: line range outside the PR's diff.

@@ -54,8 +53,7 @@ type (
streamingManager streamingtypes.FullNodeStreamingManager
finalizeBlockEventStager finalizeblock.EventStager[*types.ClobStagedFinalizeBlockEvent]

initialized *atomic.Bool
memStoreInitialized *atomic.Bool
initialized *atomic.Bool
Copy link
Contributor

@teddyding teddyding Jan 11, 2025

Choose a reason for hiding this comment

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

nit: rename to inMemStructsInitialized or sth similar to disambiguate with memStoreInitialized

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants