From 57a5260830401c9ad93196a895a50b0fc4a86183 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Thu, 3 Aug 2023 08:51:56 -0700 Subject: [PATCH] feat(store-sync): add block numbers to SyncProgress (#1228) --- .changeset/giant-masks-carry.md | 5 +++++ .../src/recs/defineInternalComponents.ts | 2 ++ packages/store-sync/src/recs/syncToRecs.ts | 20 ++++++++++++++----- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 .changeset/giant-masks-carry.md diff --git a/.changeset/giant-masks-carry.md b/.changeset/giant-masks-carry.md new file mode 100644 index 0000000000..a29cb17c25 --- /dev/null +++ b/.changeset/giant-masks-carry.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/store-sync": patch +--- + +Adds `latestBlockNumber` and `lastBlockNumberProcessed` to internal `SyncProgress` component diff --git a/packages/store-sync/src/recs/defineInternalComponents.ts b/packages/store-sync/src/recs/defineInternalComponents.ts index f7d3ed6b31..785fddc85f 100644 --- a/packages/store-sync/src/recs/defineInternalComponents.ts +++ b/packages/store-sync/src/recs/defineInternalComponents.ts @@ -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: {} }, diff --git a/packages/store-sync/src/recs/syncToRecs.ts b/packages/store-sync/src/recs/syncToRecs.ts index 3a57f643ef..0245f6bfef 100644 --- a/packages/store-sync/src/recs/syncToRecs.ts +++ b/packages/store-sync/src/recs/syncToRecs.ts @@ -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); @@ -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, }); } } @@ -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, }); } @@ -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, }); } } @@ -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)