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

[refactor] Remove errors from subaccounts keeper functions that don't return errors #678

Merged
merged 3 commits into from
Oct 20, 2023

Conversation

lucas-dydx
Copy link
Contributor

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

Changelist

Remove errors from subaccounts keeper functions that don't return errors.

This is part of a larger subaccounts keeper refactor to simplify the code by removing returned errors when invariants are broken, and instead panicing earlier.

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.

m.AssetPositions = append([]*AssetPosition{usdcAssetPosition}, m.AssetPositions...)
func (m *Subaccount) SetUsdcAssetPosition(newUsdcPosition *big.Int) {
if m == nil {
return
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we panic / error log if this ever happens?

It doesn't seem like this function should silently fail if called with a nil pointer.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this was meant to be a convenience method for tests only. I don't think there is a reason to use this instead of UpdateAssetPositions. wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's currently used in subaccounts keeper here

Created a ticket here to remove it later.

protocol/x/subaccounts/types/subaccount.go Show resolved Hide resolved
@lucas-dydx lucas-dydx marked this pull request as ready for review October 20, 2023 15:48
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 20, 2023

Walkthrough

The changes primarily focus on simplifying the error handling and return values in the subaccounts module. The functions UpdatePerpetualPositions and UpdateAssetPositions no longer return any values, and the SetUsdcAssetPosition method has been simplified. The test cases have been updated to reflect these changes.

Changes

File Summary
.../keeper/subaccount.go The UpdateSubaccounts function now directly assigns the result of UpdatePerpetualPositions and UpdateAssetPositions to the success variable. The getSettledSubaccount function no longer returns an error and directly sets the usdcAssetPosition on the newSubaccount object.
.../keeper/subaccount_helper.go The err return value has been removed from UpdatePerpetualPositions and UpdateAssetPositions. The logic remains the same, but the return statements have been simplified.
.../types/subaccount.go The SetUsdcAssetPosition method of the Subaccount struct has been simplified. The error return type has been removed, and the method now returns void. The logic for setting the USDC asset position has been improved for clarity and simplicity.
.../types/subaccount_test.go The check for expected errors in the TestSetSubaccountQuoteBalance test function has been removed. The test now only calls the SetUsdcAssetPosition method on the subaccount object.

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.

CodeRabbit Commands

  • Enter @coderabbitai pause as a PR comment to pause the reviews on a PR.
  • Enter @coderabbitai resume as a PR comment to resume the paused reviews.
  • For on-demand reviews, enter @coderabbitai review as a PR comment to trigger a review. This is useful when reviews are automatic disabled at the repository level.
  • Enter @coderabbitai: ignore in the PR description to stop a specific PR from being reviewed.

Note these commands work only in the PR comment. For conversations with the bot, please use the review comments on code diffs or files.

@@ -106,7 +106,6 @@ func UpdatePerpetualPositions(
perpIdToFundingIndex map[uint32]dtypes.SerializableInt,
) (
success bool,
err 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.

This function doesn't return an error anymore, so removed it.

AssetId: assettypes.AssetUsdc.Id,
}
m.AssetPositions = append([]*AssetPosition{usdcAssetPosition}, m.AssetPositions...)
func (m *Subaccount) SetUsdcAssetPosition(newUsdcPosition *big.Int) {
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 function also doesn't return an error anymore, so removed it + refactored it to early return.

Let me know if anyone would prefer I remove the refactor logic to early return. This function has good test coverage though so I felt like the change is safe.

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: 2

Commits Files that changed from the base of the PR and between 98464cb and b2cc88f.
Files selected for processing (4)
  • protocol/x/subaccounts/keeper/subaccount.go (2 hunks)
  • protocol/x/subaccounts/keeper/subaccount_helper.go (4 hunks)
  • protocol/x/subaccounts/types/subaccount.go (1 hunks)
  • protocol/x/subaccounts/types/subaccount_test.go (2 hunks)
Files skipped from review due to trivial changes (2)
  • protocol/x/subaccounts/keeper/subaccount_helper.go
  • protocol/x/subaccounts/types/subaccount.go
Additional comments (Suppressed): 3
protocol/x/subaccounts/types/subaccount_test.go (2)
  • 205-210: The removal of expectedError from the test case struct indicates that the function SetUsdcAssetPosition no longer returns an error. Ensure that this change is intentional and that all error handling is properly managed upstream or within the function itself.

  • 310-316: The error check after calling SetUsdcAssetPosition has been removed. This is consistent with the change in the function's signature to no longer return an error. However, it's important to ensure that the function is now handling all possible error scenarios internally, or that these scenarios are being managed upstream.

protocol/x/subaccounts/keeper/subaccount.go (1)
  • 433-437: The SetUsdcAssetPosition method no longer returns an error. This change simplifies the code but also removes the ability to handle errors that might occur when setting the USDC asset position. Ensure that the SetUsdcAssetPosition method is designed to handle all possible errors internally, or consider adding error handling back to this method if necessary.

protocol/x/subaccounts/keeper/subaccount.go Outdated Show resolved Hide resolved
protocol/x/subaccounts/keeper/subaccount.go Outdated Show resolved Hide resolved
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

Commits Files that changed from the base of the PR and between b2cc88f and ca6354a.
Files selected for processing (2)
  • protocol/x/subaccounts/keeper/subaccount.go (2 hunks)
  • protocol/x/subaccounts/keeper/subaccount_helper.go (3 hunks)
Files skipped from review due to trivial changes (1)
  • protocol/x/subaccounts/keeper/subaccount_helper.go
Additional comments (Suppressed): 2
protocol/x/subaccounts/keeper/subaccount.go (2)
  • 286-292: The error handling has been removed from the UpdatePerpetualPositions and UpdateAssetPositions functions. Ensure that these functions are now designed to never fail, or that any errors they encounter are handled internally. If not, this could lead to silent failures and unexpected behavior.

  • 430-430: The error handling has been removed from the SetUsdcAssetPosition function. As with the previous comment, ensure that this function is now designed to never fail, or that any errors it encounters are handled internally. If not, this could lead to silent failures and unexpected behavior.

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: 2

Commits Files that changed from the base of the PR and between ca6354a and 90075e4.
Files selected for processing (1)
  • protocol/x/subaccounts/keeper/subaccount.go (2 hunks)

Comment on lines +286 to +292
UpdatePerpetualPositions(
settledUpdates,
perpIdToFundingIndex,
)
if !success || err != nil {
return success, successPerUpdate, err
}

// Apply the updates to asset positions.
success, err = UpdateAssetPositions(settledUpdates)
if !success || err != nil {
return success, successPerUpdate, err
}
UpdateAssetPositions(settledUpdates)
Copy link
Contributor

Choose a reason for hiding this comment

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

The error handling has been removed from the UpdatePerpetualPositions and UpdateAssetPositions functions. This could potentially lead to silent failures if these functions encounter an error. Consider adding error handling back in to ensure that any issues are properly caught and handled.

- UpdatePerpetualPositions(
-   settledUpdates,
-   perpIdToFundingIndex,
- )
- UpdateAssetPositions(settledUpdates)
+ success, err = UpdatePerpetualPositions(
+   settledUpdates,
+   perpIdToFundingIndex,
+ )
+ if !success || err != nil {
+   return success, successPerUpdate, err
+ }
+ success, err = UpdateAssetPositions(settledUpdates)
+ if !success || err != nil {
+   return success, successPerUpdate, err
+ }

return types.Subaccount{}, nil, err
}
// TODO(CLOB-993): Remove this function and use `UpdateAssetPositions` instead.
newSubaccount.SetUsdcAssetPosition(newUsdcPosition)
Copy link
Contributor

Choose a reason for hiding this comment

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

The SetUsdcAssetPosition method no longer returns an error. This could potentially lead to silent failures if this method encounters an error. Consider adding error handling back in to ensure that any issues are properly caught and handled.

- newSubaccount.SetUsdcAssetPosition(newUsdcPosition)
+ err = newSubaccount.SetUsdcAssetPosition(newUsdcPosition)
+ if err != nil {
+   return types.Subaccount{}, nil, err
+ }

@lucas-dydx lucas-dydx merged commit 31281e2 into main Oct 20, 2023
14 of 15 checks passed
@lucas-dydx lucas-dydx deleted the lucas-dydx/sub-keeper-tech-debt-pt1 branch October 20, 2023 21:05
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