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

Initial PrepareCheckState audit #695

Merged
merged 5 commits into from
Oct 25, 2023

Conversation

lucas-dydx
Copy link
Contributor

@lucas-dydx lucas-dydx commented Oct 24, 2023

Changelist

Small PrepareCheckState fixes from audit (nothing major that needs to be backported to the release).

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 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.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 24, 2023

Walkthrough

The changes primarily focus on improving error handling, logging, and code clarity in the clob and memclob modules. Key modifications include better propagation of errors in the liquidation process, enhanced logging for missing conditional orders, and renaming of variables for better readability. The logic for handling short-term orders and purging invalid memclob state has also been refined.

Changes

File Path Summary
protocol/x/clob/keeper/liquidations.go Added error handling in PlacePerpetualLiquidation function to ensure proper propagation of errors.
protocol/x/clob/keeper/orders.go Enhanced error logging for missing conditional orders in the state.
protocol/x/clob/memclob/memclob.go Multiple changes including renaming of variables for clarity, refined logic for handling short-term orders, and improved purging of invalid memclob state. Added error logging for missing fill amount in short-term orders.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.json

@@ -271,6 +271,9 @@ func (k Keeper) PlacePerpetualLiquidation(
ctx,
liquidationOrder,
)
if err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IIRC we were previously ignoring this error and re-assigning it below

@@ -454,6 +454,17 @@ func (k Keeper) PlaceStatefulOrdersFromLastBlock(

orderPlacement, exists := k.GetLongTermOrderPlacement(ctx, orderId)
if !exists {
// Error log if this is a conditional orders and it doesn't exist in state, since it
// can't have been canceled.
if orderId.IsConditionalOrder() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Conditional orders should always exist in state, so error logging here if they don't exist

Copy link
Contributor

Choose a reason for hiding this comment

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

Also might be good to assert IsConditionalOrderTriggered is True. Since the conditional order has to be triggered at this point in time. It is possible to read out from untriggered state inGetLongTermOrderPlacement.

Copy link
Contributor

Choose a reason for hiding this comment

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

^ should definitely assert that the conditional order is triggered

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This validation is performed in PlaceConditionalOrdersTriggeredInLastBlock so I'm going to skip this for now if that's fine! Let me know if anyone disagrees.

@@ -1004,7 +1004,7 @@ func (m *MemClobPriceTimePriority) ReplayOperations(
}

// Log an error if there are two Order Removals for the same OrderId
if _, found := placedPreexistingStatefulOrderIds[orderId]; found {
if _, found := placedOrderRemovalOrderIds[orderId]; found {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check was previously the exact same as the above check, which was an oversight

existingOrder, found := m.openOrders.getOrder(ctx, otpOrderId)
if found && existingOrder.GetOrderHash() == otpOrderHash {
m.mustRemoveOrder(ctx, otpOrderId)
} else if otpOrderId.IsShortTermOrder() {
} else {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This shouldn't be necessary since all of these operations should be short-term orders

@lucas-dydx lucas-dydx marked this pull request as ready for review October 24, 2023 21:32
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a612627 and b225ed8.
Files selected for processing (3)
  • protocol/x/clob/keeper/liquidations.go (1 hunks)
  • protocol/x/clob/keeper/orders.go (1 hunks)
  • protocol/x/clob/memclob/memclob.go (5 hunks)
Files skipped from review due to trivial changes (1)
  • protocol/x/clob/keeper/orders.go
Additional comments: 6
protocol/x/clob/keeper/liquidations.go (1)
  • 274-276: The addition of error handling here is a good practice. It ensures that any error that occurs during the placement of the liquidation order is properly propagated and can be handled upstream. This can help in debugging and maintaining the code.
protocol/x/clob/memclob/memclob.go (5)
  • 1007-1007: The change from placedPreexistingStatefulOrderIds to placedOrderRemovalOrderIds is noted. Ensure that the new map placedOrderRemovalOrderIds is properly initialized and populated before this check. Also, verify that this change does not affect other parts of the code that might still be using placedPreexistingStatefulOrderIds.

  • 1135-1140: The condition otpOrderId.IsShortTermOrder() has been removed in the new hunk. This might lead to unintended removal of non-short-term orders from ShortTermOrderTxBytes. If this is intentional, ensure that the implications of this change are well understood and tested.

  • 1168-1168: The parameter fullyFilledOrderIds has been replaced with filledOrderIds. Ensure that this change is reflected in all calls to PurgeInvalidMemclobState and that filledOrderIds can include both partially and fully filled orders, as the function name suggests purging of invalid state, not just fully filled orders.

  • 1186-1188: The loop now iterates over filledOrderIds instead of fullyFilledOrderIds. Ensure that RemoveOrderIfFilled can handle both partially and fully filled orders, as the change in parameter name suggests.

  • 2006-2013: The addition of error logging when an order that should be partially or fully filled has no fill amount is a good practice. It will help in debugging and identifying issues faster.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b225ed8 and 49fea6d.
Files selected for processing (2)
  • protocol/x/clob/keeper/orders.go (1 hunks)
  • protocol/x/clob/memclob/memclob.go (5 hunks)
Additional comments: 3
protocol/x/clob/memclob/memclob.go (2)
  • 1135-1140: The condition for removing the order hash from ShortTermOrderTxBytes has been changed. Previously, it was removed if the order was a Short-Term order. Now, it's removed if the order is not found or the order hash doesn't match. This change could potentially lead to different behavior. Please verify if this is the intended behavior.

1168:
The parameter fullyFilledOrderIds has been renamed to filledOrderIds. Ensure that all calls to this function throughout the codebase have been updated to match the new parameter name.

1186:
The loop iterating over filledOrderIds (previously fullyFilledOrderIds) and calling RemoveOrderIfFilled remains the same, only the parameter name has changed. Ensure that the logic within RemoveOrderIfFilled is compatible with this change.

  • 2006-2016: Error logging has been added when a filled Short-Term order ID has no fill amount. This is a good practice as it helps in debugging and understanding the system's state. However, ensure that this logging does not introduce any performance issues, especially if the number of Short-Term orders is large.
protocol/x/clob/keeper/orders.go (1)
  • 457-467: The addition of an error log when a conditional order doesn't exist in the state is a good practice for debugging and understanding the system's behavior. However, it's important to ensure that this logging doesn't introduce performance issues, especially if the number of non-existing conditional orders is high. Consider adding a rate limit to your logging or making this log optional based on a debug flag.
+ if orderId.IsConditionalOrder() {
+   k.Logger(ctx).Error(
+     fmt.Sprintf(
+       "PlaceStatefulOrdersFromLastBlock: Triggered conditional Order ID %+v doesn't exist in state",
+       orderId,
+     ),
+   )
+ }
Committable suggestion (Beta)
if orderId.IsConditionalOrder() {
  if debugFlag {
    k.Logger(ctx).Error(
      fmt.Sprintf(
        "PlaceStatefulOrdersFromLastBlock: Triggered conditional Order ID %+v doesn't exist in state",
        orderId,
      ),
    )
  }
}

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 49fea6d and 92dcd43.
Files selected for processing (1)
  • protocol/x/clob/keeper/liquidations.go (1 hunks)
Additional comments: 1
protocol/x/clob/keeper/liquidations.go (1)
  • 274-276: The error handling here is a good addition. It ensures that any error that occurs during the execution of PlacePerpetualLiquidation is propagated up the call stack and handled appropriately. This is a good practice as it allows for better debugging and error management.

Comment on lines 2006 to 2008
// If there is no fill amount for this order, return early. Note this is only valid if the
// order is a stateful order that was fully-filled or partially-filled and expired / canceled /
// removed in the last block.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// If there is no fill amount for this order, return early. Note this is only valid if the
// order is a stateful order that was fully-filled or partially-filled and expired / canceled /
// removed in the last block.
// If there is no fill amount for this order, return early. Note this is only valid if the
// order is a stateful order that was fully-filled or partially-filled and expired / canceled /
// removed in the last block. For short-term orders, their fill amounts are stored past expiration,
// so this return is invalid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, lmk what you think!

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 92dcd43 and 0e2cd1d.
Files selected for processing (1)
  • protocol/x/clob/memclob/memclob.go (5 hunks)
Additional comments: 5
protocol/x/clob/memclob/memclob.go (5)
  • 1007-1007: The variable placedOrderRemovalOrderIds is used instead of placedPreexistingStatefulOrderIds to check for duplicate order removals. Ensure that this change is intentional and that the new variable is correctly populated elsewhere in the code.

  • 1135-1140: The condition to remove the order hash from ShortTermOrderTxBytes has been simplified. Previously, it was only removed if the order was a Short-Term order. Now, it's removed for all cases where the order is not found or the order hash doesn't match. This change could potentially affect the behavior of the system if there are cases where non-Short-Term orders are involved. Please verify this change.

  • 1168-1168: The parameter fullyFilledOrderIds has been renamed to filledOrderIds. Ensure that all calls to this function throughout the codebase have been updated to match the new parameter name.

  • 1186-1186: The loop variable fullyFilledOrderIds has been renamed to filledOrderIds. Ensure that this variable is correctly populated elsewhere in the code.

  • 2006-2017: Added error logging for the case where a filled Short-Term order ID has no fill amount. This is a good practice as it helps in identifying potential issues in the system.

@lucas-dydx lucas-dydx merged commit 8933f1f into main Oct 25, 2023
@lucas-dydx lucas-dydx deleted the lucas-dydx/prepare-check-state-audit branch October 25, 2023 20:04
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