-
Notifications
You must be signed in to change notification settings - Fork 58
fix(tokens): confirm portfolio token balances with certified calls #8048
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
Open
yhabib
wants to merge
4
commits into
main
Choose a base branch
from
fix/portfolio-balances-certified
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5dbe914
fix(tokens): confirm portfolio token balances with certified calls
yhabib 3982d45
test(portfolio): add an e2e spec for certified balance calls
yhabib 3ff1a44
fix(portfolio): address Copilot review comments
yhabib d8d4ac7
docs(portfolio): correct the query-first claim in balance sync JSDoc
yhabib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
frontend/src/lib/services/icrc-accounts-balance.services.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { syncAccounts } from "$lib/services/icrc-accounts.services"; | ||
| import { toastsError } from "$lib/stores/toasts.store"; | ||
| import type { UniverseCanisterIdText } from "$lib/types/universe"; | ||
| import { Principal } from "@icp-sdk/core/principal"; | ||
|
|
||
| /** | ||
| * Load Icrc accounts balances and token. | ||
| * | ||
| * The query answer shows first and the certified answer replaces it. If the | ||
| * certified answer arrives first, the query answer is skipped. | ||
| * | ||
| * @param {universeIds: UniverseCanisterIdText[]} params | ||
| * @param {UniverseCanisterIdText[]} params.universeIds The Icrc environment for which the balances should be loaded. | ||
| */ | ||
| export const syncIcrcAccountsBalances = async ({ | ||
| universeIds, | ||
| }: { | ||
| universeIds: UniverseCanisterIdText[]; | ||
| }): Promise<void> => { | ||
| const results = await Promise.allSettled( | ||
| universeIds.map((universeId) => | ||
| syncAccounts({ | ||
| ledgerCanisterId: Principal.fromText(universeId), | ||
| }) | ||
| ) | ||
| ); | ||
|
|
||
| const error: boolean = | ||
| results.find(({ status }) => status === "rejected") !== undefined; | ||
| if (error) { | ||
| toastsError({ labelKey: "error.accounts_load" }); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
frontend/src/lib/services/wallet-uncertified-accounts.services.ts
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
frontend/src/tests/e2e/portfolio-certified-balances.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { AppPo } from "$tests/page-objects/App.page-object"; | ||
| import { PlaywrightPageObjectElement } from "$tests/page-objects/playwright.page-object"; | ||
| import { signInWithNewUser, step } from "$tests/utils/e2e.test-utils"; | ||
| import { expect, test, type Request } from "@playwright/test"; | ||
|
|
||
| // The Portfolio page must confirm every token balance with a certified update | ||
| // call. Before the fix it loaded every SNS and ck token balance with a query | ||
| // call only, so one malicious replica could show a forged balance. | ||
| // | ||
| // The IC HTTP interface uses one path per call type: | ||
| // /api/v2/canister/<canisterId>/query -> uncertified query call | ||
| // /api/v3/canister/<canisterId>/call -> certified update call | ||
| // | ||
| // The CBOR request body carries the method name as a plain text string, so the | ||
| // test selects the balance calls by searching the raw body for | ||
| // "icrc1_balance_of". No CBOR parser is needed. | ||
|
|
||
| const CALL_PATH_PATTERN = /^\/api\/v\d+\/canister\/([^/]+)\/(query|call)$/; | ||
|
|
||
| const BALANCE_METHOD = "icrc1_balance_of"; | ||
|
|
||
| test("Portfolio balances are confirmed with certified update calls", async ({ | ||
| page, | ||
| context, | ||
| }) => { | ||
| const queriedLedgers = new Set<string>(); | ||
| const updatedLedgers = new Set<string>(); | ||
|
|
||
| page.on("request", (request: Request) => { | ||
| if (request.method() !== "POST") { | ||
| return; | ||
| } | ||
|
|
||
| const match = CALL_PATH_PATTERN.exec(new URL(request.url()).pathname); | ||
| if (match === null) { | ||
| return; | ||
| } | ||
|
|
||
| const body = request.postDataBuffer(); | ||
| if (body === null) { | ||
| return; | ||
| } | ||
| if (!body.toString("latin1").includes(BALANCE_METHOD)) { | ||
| return; | ||
| } | ||
|
|
||
| const [, canisterId, callType] = match; | ||
| if (callType === "query") { | ||
| queriedLedgers.add(canisterId); | ||
| } else { | ||
| updatedLedgers.add(canisterId); | ||
| } | ||
| }); | ||
|
|
||
| await page.goto("/"); | ||
| await expect(page).toHaveTitle("Portfolio | Network Nervous System"); | ||
|
|
||
| await signInWithNewUser({ page, context }); | ||
|
|
||
| const pageElement = PlaywrightPageObjectElement.fromPage(page); | ||
| const appPo = new AppPo(pageElement); | ||
| const portfolioPagePo = appPo.getPortfolioPo().getPortfolioPagePo(); | ||
|
|
||
| await step("Wait for the Portfolio page to load the balances"); | ||
| await portfolioPagePo.getTotalAssetsCardPo().waitForLoaded(); | ||
|
|
||
| await step("The Portfolio page reads at least one ledger balance"); | ||
| await expect | ||
| .poll(() => queriedLedgers.size, { timeout: 60_000 }) | ||
| .toBeGreaterThan(0); | ||
|
|
||
| await step( | ||
| "Every ledger that got a balance query also gets a balance update" | ||
| ); | ||
| // On main this list keeps every queried ledger, because the Portfolio page | ||
| // sends no update call at all. | ||
| await expect | ||
| .poll( | ||
| () => [...queriedLedgers].filter((id) => !updatedLedgers.has(id)).sort(), | ||
| { timeout: 60_000 } | ||
| ) | ||
| .toEqual([]); | ||
|
|
||
| await step("No ledger gets an update call without a query call"); | ||
| // The query answer must still show first, so an update call alone would mean | ||
| // the page waits for the certified answer to render a balance. | ||
| expect( | ||
| [...updatedLedgers].filter((id) => !queriedLedgers.has(id)).sort() | ||
| ).toEqual([]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.