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

fix swap claim bug for multi-fees #1421

Merged
merged 9 commits into from
Jul 5, 2024
Merged

fix swap claim bug for multi-fees #1421

merged 9 commits into from
Jul 5, 2024

Conversation

TalDerei
Copy link
Contributor

@TalDerei TalDerei commented Jul 5, 2024

references #1407

@TalDerei TalDerei self-assigned this Jul 5, 2024
Copy link

changeset-bot bot commented Jul 5, 2024

🦋 Changeset detected

Latest commit: af561e9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
minifront Minor
@penumbra-zone/wasm Major
@penumbra-zone/storage Major
@penumbra-zone/types Major
@penumbra-zone/services Major
@repo/ui Minor
@penumbra-zone/perspective Major
@penumbra-zone/query Major
node-status Patch
@penumbra-zone/crypto-web Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@TalDerei TalDerei requested review from turbocrime and a team July 5, 2024 01:59
packages/services/src/view-service/fees.ts Outdated Show resolved Hide resolved
packages/services/src/view-service/fees.test.ts Outdated Show resolved Hide resolved
packages/services/src/view-service/fees.ts Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you write a test for swap claims as well?

Copy link
Contributor Author

@TalDerei TalDerei Jul 5, 2024

Choose a reason for hiding this comment

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

logic has changed with the inclusion of indexDB, so we should change these tests accordingly

Comment on lines 18 to 23
if (swapCommitment) {
const swaps = await indexedDb.getSwapByCommitment(swapCommitment);
if (swaps?.swap?.tradingPair?.asset1) {
return swaps.swap.tradingPair.asset1;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this is saying "If a user is making a swap claim, attempt to find the assetIn they originally swapped with for an alt fee".

But given swap claims happen in different blocks than swaps, isn't it possible the user doesn't have that original asset anymore? Like what if they swapped the entire balance of it out? Is this logic we can't really depend on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as discussed, the original asset value balances are irrelevant since the swap pre-pays the swap claim. The assetId however must be consistent between the swap / swap claim and can be retrieved by querying the swap table in the database or by calling the SwapByCommitment service implementation using the swap commitment as the key.

@@ -14,11 +14,13 @@ export const transactionPlanner: Impl['transactionPlanner'] = async (req, ctx) =
const { indexedDb } = await services.getWalletServices();

// Query IndexedDB directly to check for the existence of staking token
const nativeToken = await indexedDb.hasStakingAssetBalance(req.source);
const nativeToken = await indexedDb.hasStakingAssetBalance();
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the thinking around removing source here? Is the expectation that paying fees should not be scoped to one account and can be paid by any of your accounts? Is this consistent with the core repo? At the moment, in minifront, we've kinda been scoping all activity to a single account in a tx.

Copy link
Contributor Author

@TalDerei TalDerei Jul 5, 2024

Choose a reason for hiding this comment

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

for some reason I was noticing stateless check failures. When performing a swap, the AddressIndex in hasStakingAssetBalance properly corresponds to the account performing the trade, and returns the proper boolean. For swap claims, AddressIndex is an undefined value. Removing this allowed the swap claim to proceed. cc @Valentine1898

Screenshot 2024-07-05 at 8 26 08 AM

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 the issue is that source should always be set, and we should not create transaction plans that are not linked to any account (address index)

Copy link
Contributor

@Valentine1898 Valentine1898 Jul 5, 2024

Choose a reason for hiding this comment

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

The fact that hasStakingAssetBalance() now doesn't take AddressIndex into account leads to a bug where the planner thinks the user has a native token for the commission but can't spend it. This bug will be reproduced quite often

@TalDerei
Copy link
Contributor Author

TalDerei commented Jul 5, 2024

reproduced edge case discovered during pairing:

  1. Swap GM --> GN
  2. acquire UM
  3. swap claim never hits extractAltFee
Screenshot 2024-07-05 at 8 53 35 AM

Comment on lines +23 to +24
const outputAsset = request.outputs.map(o => o.value?.assetId).find(Boolean);
expect(outputAsset!.equals(umAssetId)).toBeTruthy();
Copy link
Contributor

Choose a reason for hiding this comment

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

🚨 This (and the three changed tests below) are no longer testing extractAltFee(), since they don't call it. They should be changed to actually call extractAltFee(), or else they're essentially useless tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we're passing in an indexDB now, not sure how we'd simulate this in the test since MockIndexDB has different interface?

Copy link
Contributor

Choose a reason for hiding this comment

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

Un-resolving this because this is a pretty important issue: the tests here are literally not testing anything. It's the equivalent of writing an add() function, then writing a test like this:

it('adds', () => {
  const result = 2 + 2;
  expect(result).toBe(4);
})

In this test, we haven't called the add() function, so if we break the add() function later, the tests won't catch it.

Happy to pair on updating MockedIndexedDB if that would help!

Copy link
Contributor

Choose a reason for hiding this comment

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

Talked offline. Given this blocks deployment, we want to move forward. However, we should absolutely follow up with this with a separate PR.

@TalDerei
Copy link
Contributor Author

TalDerei commented Jul 5, 2024

@grod220 The swapClaims inside extractAltFee now properly retrieves the asset ID from the claim fee rather than from the pair inputs, which resolves several canonical ordering bugs. Additionally, extra checks have been implemented to address the swap claim edge case.

@TalDerei TalDerei requested a review from grod220 July 5, 2024 18:00
@Valentine1898 Valentine1898 self-requested a review July 5, 2024 20:56
Copy link
Contributor

@Valentine1898 Valentine1898 left a comment

Choose a reason for hiding this comment

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

LGTM!

Just one suggestion, I think planner should throw an error if the client has not passed source to TransactionPlannerRequest

@grod220
Copy link
Contributor

grod220 commented Jul 5, 2024

Pairing together w/ @TalDerei, merging!

@grod220 grod220 merged commit 28a48d7 into main Jul 5, 2024
6 checks passed
@grod220 grod220 deleted the swap-claim-bug branch July 5, 2024 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants