Skip to content

Commit

Permalink
(frontend-sdk, hasura-sync-adapters) fix available balance not updati…
Browse files Browse the repository at this point in the history
…ng (#588)

* always sync if committed index < endIndex

* fix edge case where adapter no emit

* get rid of console log, leave rest in

* changeset

* Remove logs

Signed-off-by: Daniel Park <[email protected]>

---------

Signed-off-by: Daniel Park <[email protected]>
Co-authored-by: Daniel Park <[email protected]>
  • Loading branch information
Sladuca and panieldark authored Nov 10, 2023
1 parent 57023ec commit 4167132
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-apples-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nocturne-xyz/hasura-sync-adapters": patch
---

always emit empty state diff if `latestCommittedMerkleIndex` is undefined and no sdk events
6 changes: 6 additions & 0 deletions .changeset/breezy-hats-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nocturne-xyz/frontend-sdk": patch
"@nocturne-xyz/client": patch
---

ensure frontend-sdk syncs when latestCommittedIndex != latestSyncedIndex
4 changes: 4 additions & 0 deletions packages/client/src/NocturneClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ export class NocturneClient {
return await this.db.latestSyncedMerkleIndex();
}

async getLatestCommittedMerkleIndex(): Promise<number | undefined> {
return await this.db.latestCommittedMerkleIndex();
}

async hasEnoughBalanceForOperationRequest(
opRequest: OperationRequest
): Promise<boolean> {
Expand Down
15 changes: 12 additions & 3 deletions packages/frontend-sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,17 +937,26 @@ export class NocturneSdk implements NocturneSdkApi {
const startIndex = (await this.getLatestSyncedMerkleIndex()) ?? 0;
let currentIndex = startIndex;

// if latestCommittedMerkleIndex from the client is different from that on-chain, then the client
// is behind and we need to sync at least once. However, we don't currently have a way to fetch
// the latest committed merkle index with a timelag, so for now we're going to assume the client needs
// to sync at least once if its `latestCommittedMerkleIndex` is different from the `endIndex` we fetched
// this should work fine, but it technically makes more queries than it needs to.
// TODO: add method to SDKSyncAdapter to fetch latest committed merkle index with a timelag
const latestCommittedMerkleIndex = await (await this.clientThunk()).getLatestCommittedMerkleIndex();
const minIterations = latestCommittedMerkleIndex !== endIndex ? 1 : 0;

const NUM_REFETCHES = 5;
const refetchEvery = Math.floor(
(endIndex - startIndex) / NUM_REFETCHES,
);

let count = 0;
while (currentIndex < endIndex) {
while (count < minIterations || currentIndex < endIndex) {
console.log("[sync] syncing", { currentIndex, endIndex, opts });
currentIndex = (await this.syncInner(opts)) ?? 0;

if (count % refetchEvery === 0) {
if (refetchEvery > 1 && count % refetchEvery === 0) {
endIndex = await fetchEndIndex();
}
count++;
Expand All @@ -956,7 +965,7 @@ export class NocturneSdk implements NocturneSdkApi {
((currentIndex - startIndex) / (endIndex - startIndex)) * 100;

this.syncProgressHandlers.forEach((handler) => handler(progress));
}
};
});
} catch (err) {
if (err == E_ALREADY_LOCKED) {
Expand Down
14 changes: 10 additions & 4 deletions sync/hasura-sync-adapters/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export class HasuraSdkSyncAdapter implements SDKSyncAdapter {
toBlock + 1
);

console.log("[HasuraSdkSyncAdapter]", {
events,
latestCommittedMerkleIndex,
newLatestCommittedMerkleIndex,
});

// if there are sdk events, produce the requisite diff.
// otherwise, see if latestCommittedMerkleIndex changed - if so, yield an empty state diff with the new merkle index
if (events.length > 0) {
Expand Down Expand Up @@ -151,12 +157,12 @@ export class HasuraSdkSyncAdapter implements SDKSyncAdapter {
);

// check the latest committed merkle index
// if it's bigger than the one from the last iteration,
// if it's bigger than the one from the last iteration OR it's the first iteration,
// then emit an empty diff with only the latest committed merkle index
if (
latestCommittedMerkleIndex &&
newLatestCommittedMerkleIndex &&
newLatestCommittedMerkleIndex > latestCommittedMerkleIndex
!latestCommittedMerkleIndex ||
(newLatestCommittedMerkleIndex &&
newLatestCommittedMerkleIndex > latestCommittedMerkleIndex)
) {
latestCommittedMerkleIndex = newLatestCommittedMerkleIndex;

Expand Down

0 comments on commit 4167132

Please sign in to comment.