Skip to content

Commit

Permalink
feat(store-sync): add block numbers to SyncProgress (#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Aug 3, 2023
1 parent 7fb0dad commit 57a5260
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-masks-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store-sync": patch
---

Adds `latestBlockNumber` and `lastBlockNumberProcessed` to internal `SyncProgress` component
2 changes: 2 additions & 0 deletions packages/store-sync/src/recs/defineInternalComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function defineInternalComponents(world: World) {
step: Type.String,
message: Type.String,
percentage: Type.Number,
latestBlockNumber: Type.BigInt,
lastBlockNumberProcessed: Type.BigInt,
},
{
metadata: { keySchema: {}, valueSchema: {} },
Expand Down
20 changes: 15 additions & 5 deletions packages/store-sync/src/recs/syncToRecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export async function syncToRecs<
step: SyncStep.SNAPSHOT,
message: `Hydrating from snapshot to block ${initialState.blockNumber}`,
percentage: 0,
latestBlockNumber: 0n,
lastBlockNumberProcessed: initialState.blockNumber,
});

const componentList = Object.values(components);
Expand All @@ -136,6 +138,8 @@ export async function syncToRecs<
step: SyncStep.SNAPSHOT,
message: `Hydrating from snapshot to block ${initialState.blockNumber}`,
percentage: (recordsProcessed / numRecords) * 100,
latestBlockNumber: 0n,
lastBlockNumberProcessed: initialState.blockNumber,
});
}
}
Expand All @@ -146,6 +150,8 @@ export async function syncToRecs<
step: SyncStep.SNAPSHOT,
message: `Hydrating from snapshot to block ${initialState.blockNumber}`,
percentage: (recordsProcessed / numRecords) * 100,
latestBlockNumber: 0n,
lastBlockNumberProcessed: initialState.blockNumber,
});
}

Expand Down Expand Up @@ -177,28 +183,32 @@ export async function syncToRecs<
share()
);

let latestBlockNumberProcessed: bigint | null = null;
let lastBlockNumberProcessed: bigint | null = null;
const blockStorageOperations$ = blockLogs$.pipe(
concatMap(blockLogsToStorage(recsStorage({ components, config }))),
tap(({ blockNumber, operations }) => {
debug("stored", operations.length, "operations for block", blockNumber);
latestBlockNumberProcessed = blockNumber;
lastBlockNumberProcessed = blockNumber;

if (
latestBlockNumber != null &&
getComponentValue(components.SyncProgress, singletonEntity)?.step !== SyncStep.LIVE
) {
if (blockNumber < latestBlockNumber) {
if (lastBlockNumberProcessed < latestBlockNumber) {
setComponent(components.SyncProgress, singletonEntity, {
step: SyncStep.RPC,
message: `Hydrating from RPC to block ${latestBlockNumber}`,
percentage: (Number(blockNumber) / Number(latestBlockNumber)) * 100,
percentage: (Number(lastBlockNumberProcessed) / Number(latestBlockNumber)) * 100,
latestBlockNumber,
lastBlockNumberProcessed,
});
} else {
setComponent(components.SyncProgress, singletonEntity, {
step: SyncStep.LIVE,
message: `All caught up!`,
percentage: 100,
latestBlockNumber,
lastBlockNumberProcessed,
});
}
}
Expand All @@ -217,7 +227,7 @@ export async function syncToRecs<
const receipt = await publicClient.waitForTransactionReceipt({ hash: tx });

// If we haven't processed a block yet or we haven't processed the block for the tx, wait for it
if (latestBlockNumberProcessed == null || latestBlockNumberProcessed < receipt.blockNumber) {
if (lastBlockNumberProcessed == null || lastBlockNumberProcessed < receipt.blockNumber) {
await firstValueFrom(
blockStorageOperations$.pipe(
filter(({ blockNumber }) => blockNumber != null && blockNumber >= receipt.blockNumber)
Expand Down

0 comments on commit 57a5260

Please sign in to comment.