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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions protocol/x/subaccounts/keeper/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,13 @@ func (k Keeper) UpdateSubaccounts(
}

// Apply the updates to perpetual positions.
success, err = UpdatePerpetualPositions(
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)
Comment on lines +286 to +292
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
+ }


// Apply all updates, including a subaccount update event in the Indexer block message
// per update and emit a cometbft event for each settled funding payment.
Expand Down Expand Up @@ -433,10 +427,7 @@ func (k Keeper) getSettledSubaccount(
// division result always rounds towards negative infinity.
totalNetSettlementPpm.Div(totalNetSettlementPpm, lib.BigIntOneMillion()),
)
err = newSubaccount.SetUsdcAssetPosition(newUsdcPosition)
if err != nil {
return types.Subaccount{}, nil, err
}
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
+ }

return newSubaccount, fundingPayments, nil
}

Expand Down
8 changes: 0 additions & 8 deletions protocol/x/subaccounts/keeper/subaccount_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ func getUpdatedPerpetualPositions(
func UpdatePerpetualPositions(
settledUpdates []settledUpdate,
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.

) {
// Apply the updates.
for i, u := range settledUpdates {
Expand Down Expand Up @@ -164,16 +161,12 @@ func UpdatePerpetualPositions(

settledUpdates[i].SettledSubaccount.PerpetualPositions = perpetualPositions
}
return true, nil
}

// For each settledUpdate in settledUpdates, updates its SettledSubaccount.AssetPositions
// to reflect settledUpdate.AssetUpdates.
func UpdateAssetPositions(
settledUpdates []settledUpdate,
) (
success bool,
err error,
) {
// Apply the updates.
for i, u := range settledUpdates {
Expand Down Expand Up @@ -226,5 +219,4 @@ func UpdateAssetPositions(

settledUpdates[i].SettledSubaccount.AssetPositions = assetPositions
}
return true, nil
}
33 changes: 16 additions & 17 deletions protocol/x/subaccounts/types/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,25 @@ func (m *Subaccount) GetUsdcPosition() *big.Int {
}

// SetUsdcAssetPosition sets the balance of the USDC asset position to `newUsdcPosition`.
// If the absolute value of `newUsdcPosition` cannot be represented in a uint64,
// an error is returned.
func (m *Subaccount) SetUsdcAssetPosition(newUsdcPosition *big.Int) error {
if m != nil {
usdcAssetPosition := m.getUsdcAssetPosition()
if newUsdcPosition == nil || newUsdcPosition.Sign() == 0 {
if usdcAssetPosition != nil {
m.AssetPositions = m.AssetPositions[1:]
}
} else {
if usdcAssetPosition == nil {
usdcAssetPosition = &AssetPosition{
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.

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.

}

usdcAssetPosition := m.getUsdcAssetPosition()
if newUsdcPosition == nil || newUsdcPosition.Sign() == 0 {
lucas-dydx marked this conversation as resolved.
Show resolved Hide resolved
if usdcAssetPosition != nil {
m.AssetPositions = m.AssetPositions[1:]
}
} else {
if usdcAssetPosition == nil {
usdcAssetPosition = &AssetPosition{
AssetId: assettypes.AssetUsdc.Id,
}
usdcAssetPosition.Quantums = dtypes.NewIntFromBigInt(newUsdcPosition)
m.AssetPositions = append([]*AssetPosition{usdcAssetPosition}, m.AssetPositions...)
}
usdcAssetPosition.Quantums = dtypes.NewIntFromBigInt(newUsdcPosition)
}
return nil
}

func (m *Subaccount) getUsdcAssetPosition() *AssetPosition {
Expand Down
8 changes: 1 addition & 7 deletions protocol/x/subaccounts/types/subaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ func TestSetSubaccountQuoteBalance(t *testing.T) {
subaccount *types.Subaccount
newQuoteBalance *big.Int
expectedAssetPositions []*types.AssetPosition
expectedError error
}{
"can set nil subaccount": {
subaccount: nil,
Expand Down Expand Up @@ -311,12 +310,7 @@ func TestSetSubaccountQuoteBalance(t *testing.T) {
// Run tests.
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := tc.subaccount.SetUsdcAssetPosition(tc.newQuoteBalance)
if tc.expectedError == nil {
require.NoError(t, err)
} else {
require.ErrorIs(t, tc.expectedError, err)
}
tc.subaccount.SetUsdcAssetPosition(tc.newQuoteBalance)
if tc.subaccount != nil {
require.Equal(
t,
Expand Down