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

[OTE-823] Fix FNS onchain events staging + retrieval logic #2318

Merged
merged 2 commits into from
Sep 26, 2024

Conversation

teddyding
Copy link
Contributor

@teddyding teddyding commented Sep 23, 2024

Changelist

Fix FNS onchain events staging + retrieval logic.

Test Plan

Tested by running FNS + load-testing locally.

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

  • New Features

    • Introduced a new method Precommit for enhanced ABCI Precommit logic in the clob module.
    • Updated streaming manager to utilize a new codec for improved event serialization and deserialization.
  • Bug Fixes

    • Removed restrictions allowing gRPC streaming to work with optimistic execution enabled.
  • Tests

    • Updated test cases to reflect changes in flag validation, allowing optimistic execution with gRPC streaming.

Copy link

linear bot commented Sep 23, 2024

@teddyding teddyding changed the title [OTE-823] Fix onchain events staging + retrieval logic [OTE-823] Fix FNS onchain events staging + retrieval logic Sep 23, 2024
Copy link
Contributor

coderabbitai bot commented Sep 23, 2024

Walkthrough

The pull request introduces several changes across multiple files. Key modifications include the addition of a cdc parameter to the getFullNodeStreamingManagerFromOptions function, the removal of a restriction on enabling gRPC streaming with optimistic execution, and the introduction of a new Precommit method in the AppModule struct. The changes enhance the codec functionality, update flag validation tests, and modernize the streaming manager's event handling.

Changes

File Change Summary
protocol/app/app.go Added a cdc parameter to the getFullNodeStreamingManagerFromOptions function signature.
protocol/app/flags/flags.go Removed a conditional check in the Validate method that restricted enabling gRPC streaming when optimistic execution is enabled.
protocol/app/flags/flags_test.go Updated the TestValidate function to reflect the change in behavior regarding optimistic execution and gRPC streaming; added new fields to the flags structure.
protocol/streaming/full_node_streaming_manager.go Introduced a cdc field in FullNodeStreamingManagerImpl and updated the constructor to accept this parameter; modified event marshaling methods to use the new codec; renamed getStagedFinalizeBlockEvents to getStagedFinalizeBlockEventsFromStore.
protocol/x/clob/module.go Added a new Precommit method to the AppModule struct for executing ABCI Precommit logic.

Possibly related PRs

Suggested labels

feature:optimistic_execution

Suggested reviewers

  • jonfung-dydx
  • dydxwill

🐰 In the meadow where rabbits play,
New features hop in, brightening the day!
With codecs added and flags set free,
Streaming flows like a dance, oh so glee!
Let’s celebrate changes, both big and small,
In our coding burrow, we’re having a ball! 🌼✨


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.

@@ -163,10 +163,6 @@ func (f *Flags) Validate() error {

// Grpc streaming
if f.GrpcStreamingEnabled {
if f.OptimisticExecutionEnabled {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously added to prevent both flags from being accidentally set true

@@ -177,6 +177,16 @@ func (am AppModule) PreBlock(ctx context.Context) (appmodule.ResponsePreBlock, e
}, nil
}

// BeginBlock executes all ABCI BeginBlock logic respective to the clob module.
func (am AppModule) Precommit(ctx context.Context) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Callback was not previously implemented, so ClobModule.Precommit was not being run

@teddyding teddyding marked this pull request as ready for review September 25, 2024 20:49
@teddyding teddyding requested a review from a team as a code owner September 25, 2024 20:49
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

🧹 Outside diff range and nitpick comments (1)
protocol/x/clob/module.go (1)

181-188: LGTM! Consider adding a comment explaining the purpose of the Precommit method.

The implementation of the Precommit method looks good and follows the established patterns in the module. It correctly implements the appmodule.HasPrecommit interface and uses telemetry for performance monitoring.

Consider adding a brief comment explaining the purpose of the Precommit method and its role in the ABCI lifecycle. This would improve code documentation and help future developers understand its significance. For example:

// Precommit executes all ABCI Precommit logic respective to the clob module.
// This method is called after the block is committed but before the state is finalized.
func (am AppModule) Precommit(ctx context.Context) error {
    // ... (existing implementation)
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9144e78 and 245d14d.

📒 Files selected for processing (5)
  • protocol/app/app.go (1 hunks)
  • protocol/app/flags/flags.go (0 hunks)
  • protocol/app/flags/flags_test.go (1 hunks)
  • protocol/streaming/full_node_streaming_manager.go (8 hunks)
  • protocol/x/clob/module.go (1 hunks)
💤 Files not reviewed due to no reviewable changes (1)
  • protocol/app/flags/flags.go
🔇 Additional comments not posted (4)
protocol/x/clob/module.go (1)

Line range hint 19-26: Verify interface implementation for appmodule.HasPrecommit

The addition of the Precommit method suggests that the AppModule should now implement the appmodule.HasPrecommit interface. Please ensure that this interface is properly added to the list of implemented interfaces at the beginning of the file.

Run the following script to check if the appmodule.HasPrecommit interface is properly implemented:

If the script doesn't find the appmodule.HasPrecommit interface, please add it to the list of implemented interfaces.

protocol/app/flags/flags_test.go (1)

116-125: Fix typo in test case name and clarify behavior change.

  1. There's a typo in the test case name: "canbe" should be "can be".

  2. This test case implies a significant change in behavior, allowing optimistic execution to be enabled alongside gRPC streaming. Could you please provide more context on this change?

    • Was this previously not allowed? If so, why?
    • What are the implications of enabling both optimistic execution and gRPC streaming?
    • Are there any potential risks or performance considerations we should be aware of?
  3. The flag values seem consistent with other test cases. However, it would be helpful to understand the reasoning behind the specific values chosen for GrpcStreamingMaxBatchSize, GrpcStreamingFlushIntervalMs, and GrpcStreamingMaxChannelBufferSize.

To ensure this change doesn't introduce unintended side effects, please run the following script:

This will help us identify if there are any other parts of the codebase that need to be updated to reflect this new behavior.

protocol/streaming/full_node_streaming_manager.go (2)

903-903: Assess the security implications of using an infinite gas meter.

In line 903, the context is set with an infinite gas meter using ctx = ctx.WithGasMeter(ante_types.NewFreeInfiniteGasMeter()). While this allows unmetered operations, ensure that it doesn't introduce security risks like potential denial-of-service attacks due to unbounded computation.

Review the necessity of an infinite gas meter in this context and consider implementing limits or safeguards if appropriate.


432-435: ⚠️ Potential issue

Ensure all calls to 'getStagedFinalizeBlockEventsFromStore' pass the new 'cdc' parameter.

The function getStagedFinalizeBlockEventsFromStore now requires a cdc codec.BinaryCodec parameter (lines 432-435). Check that all invocations of this function are updated to include the cdc argument.

Execute the following script to find and review all calls to getStagedFinalizeBlockEventsFromStore:

#!/bin/bash
# Description: Verify all calls to 'getStagedFinalizeBlockEventsFromStore' include the 'cdc' parameter.

# Find all calls to 'getStagedFinalizeBlockEventsFromStore'
rg --type go 'getStagedFinalizeBlockEventsFromStore\('

# Manually inspect each call to ensure 'cdc' is passed

@@ -2095,6 +2095,7 @@ func getFullNodeStreamingManagerFromOptions(
appFlags.GrpcStreamingMaxChannelBufferSize,
appFlags.FullNodeStreamingSnapshotInterval,
streamingManagerTransientStoreKey,
cdc,
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

Remove unused cdc parameter from NewFullNodeStreamingManager.

The cdc parameter is currently not utilized within the NewFullNodeStreamingManager function. If it's not required, please remove it to simplify the function signature and maintain code clarity.

🔗 Analysis chain

LGTM. Verify usage of new parameter in NewFullNodeStreamingManager.

The addition of the cdc parameter to NewFullNodeStreamingManager looks good. This change likely introduces codec functionality for serialization/deserialization in the streaming manager.

Please ensure that the NewFullNodeStreamingManager function properly utilizes the new cdc parameter. Run the following script to verify:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the NewFullNodeStreamingManager function signature and usage of cdc parameter

# Check the function signature
echo "Checking NewFullNodeStreamingManager function signature:"
rg --type go "func NewFullNodeStreamingManager\(.*codec\.Codec.*\)" ./protocol

# Check for usage of cdc within the function
echo "Checking usage of cdc parameter within NewFullNodeStreamingManager:"
rg --type go -A 10 "func NewFullNodeStreamingManager\(" ./protocol | rg "cdc"

Length of output: 502


Script:

#!/bin/bash
# Description: Verify all usages of the cdc parameter within NewFullNodeStreamingManager and the codebase

# Check for any usage of cdc within the NewFullNodeStreamingManager function
echo "Searching for cdc usage within NewFullNodeStreamingManager:"
rg --type go "func NewFullNodeStreamingManager\(" ./protocol | rg -A 20 "cdc"

# Search for any external usage of the cdc parameter in the codebase
echo "Searching for external usages of cdc parameter:"
rg --type go "\bcdc\b" ./protocol

Length of output: 91461

stagedEvent := clobtypes.StagedFinalizeBlockEvent{
Event: &clobtypes.StagedFinalizeBlockEvent_SubaccountUpdate{
SubaccountUpdate: &subaccountUpdate,
},
}
sm.stageFinalizeBlockEvent(
ctx,
clobtypes.Amino.MustMarshal(stagedEvent),
sm.cdc.MustMarshal(&stagedEvent),
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Replace 'MustMarshal' with error-handling 'Marshal' to prevent panics.

In StageFinalizeBlockSubaccountUpdate (line 406) and StageFinalizeBlockFill (line 428), sm.cdc.MustMarshal(&stagedEvent) is used. MustMarshal will panic if an error occurs during marshaling. It's safer to use Marshal with proper error handling to improve robustness.

Apply this change to handle marshaling errors gracefully:

- sm.cdc.MustMarshal(&stagedEvent)
+ marshaledEvent, err := sm.cdc.Marshal(&stagedEvent)
+ if err != nil {
+     sm.logger.Error("Failed to marshal staged event", "error", err)
+     return // Handle the error appropriately
+ }
+ sm.stageFinalizeBlockEvent(ctx, marshaledEvent)

Also applies to: 428-428

count := getStagedEventsCount(store)
events := make([]clobtypes.StagedFinalizeBlockEvent, count)
store = prefix.NewStore(store, []byte(StagedEventsKeyPrefix))
for i := uint32(0); i < count; i++ {
var event clobtypes.StagedFinalizeBlockEvent
bytes := store.Get(lib.Uint32ToKey(i))
clobtypes.Amino.MustUnmarshal(bytes, &event)
cdc.MustUnmarshal(bytes, &event)
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Handle errors from 'Unmarshal' instead of using 'MustUnmarshal'.

At line 442, cdc.MustUnmarshal(bytes, &event) is called. MustUnmarshal panics on error, which can cause the entire application to crash. It's better to use Unmarshal with error checking to make the code more resilient.

Modify the code to handle unmarshaling errors:

- cdc.MustUnmarshal(bytes, &event)
+ if err := cdc.Unmarshal(bytes, &event); err != nil {
+     sm.logger.Error("Failed to unmarshal staged event", "error", err)
+     continue // or handle the error as appropriate
+ }
📝 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
cdc.MustUnmarshal(bytes, &event)
if err := cdc.Unmarshal(bytes, &event); err != nil {
sm.logger.Error("Failed to unmarshal staged event", "error", err)
continue // or handle the error as appropriate
}

@@ -27,6 +28,7 @@
type FullNodeStreamingManagerImpl struct {
sync.Mutex

cdc codec.BinaryCodec
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

Update all calls to NewFullNodeStreamingManager to include the cdc parameter.

  • File: protocol/app/app.go
    • Line: (exact line number not provided)

Ensure that the cdc parameter is passed when instantiating NewFullNodeStreamingManager in all instances.

🔗 Analysis chain

Update all usages to accommodate the new 'cdc' parameter.

The cdc field of type codec.BinaryCodec has been added to the FullNodeStreamingManagerImpl struct (line 31). The constructor NewFullNodeStreamingManager now requires a cdc parameter (line 100), which is assigned during initialization (line 119). Ensure that:

  • All calls to NewFullNodeStreamingManager include the new cdc parameter.
  • Any instances where FullNodeStreamingManagerImpl is directly instantiated are updated to initialize the cdc field appropriately.

Run the following script to verify that all calls to NewFullNodeStreamingManager have been updated with the cdc parameter:

Also applies to: 100-100, 119-119

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all calls to 'NewFullNodeStreamingManager' include the 'cdc' parameter.

# Search for calls to 'NewFullNodeStreamingManager' lacking the 'cdc' parameter
rg --type go 'NewFullNodeStreamingManager\(' -A 5 | grep -v 'cdc'

# Alternatively, list all calls to inspect them manually
rg --type go -A 5 'NewFullNodeStreamingManager\('

Length of output: 1246

@@ -113,14 +113,17 @@ func TestValidate(t *testing.T) {
OptimisticExecutionEnabled: true,
},
},
"failure - optimistic execution cannot be enabled with gRPC streaming": {
"success - optimistic execution canbe enabled with gRPC streaming": {
Copy link
Contributor

Choose a reason for hiding this comment

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

typo

@teddyding teddyding merged commit 3019562 into main Sep 26, 2024
22 checks passed
@teddyding teddyding deleted the td/stage-fns-fix branch September 26, 2024 15:43
@teddyding
Copy link
Contributor Author

@Mergifyio backport release/protocol/v7.x

Copy link
Contributor

mergify bot commented Sep 26, 2024

backport release/protocol/v7.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Sep 26, 2024
teddyding added a commit that referenced this pull request Sep 26, 2024
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