Skip to content

Commit

Permalink
feat: add more granularity to initial sync state report (#146)
Browse files Browse the repository at this point in the history
* feat: add more granularity to initial sync state report

* test(network): fix tests
  • Loading branch information
alvrs authored Sep 15, 2022
1 parent 8516220 commit d4ba338
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
8 changes: 6 additions & 2 deletions packages/network/src/workers/SyncWorker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ describe("Sync.worker", () => {
expect(syncUtils.fetchStateInBlockRange).toHaveBeenLastCalledWith(
expect.anything(),
cacheBlockNumber,
currentBlockNumber
currentBlockNumber,
expect.anything(),
expect.anything()
);

// Expect output to contain the events from the the gap state
Expand Down Expand Up @@ -357,7 +359,9 @@ describe("Sync.worker", () => {
expect(syncUtils.fetchStateInBlockRange).toHaveBeenLastCalledWith(
expect.anything(),
cacheBlockNumber,
firstLiveBlockNumber
firstLiveBlockNumber,
expect.anything(),
expect.anything()
);

// Expect output to contain the events from the cache and the gap state
Expand Down
8 changes: 7 additions & 1 deletion packages/network/src/workers/SyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ export class SyncWorker<Cm extends Components> implements DoWork<SyncWorkerConfi
`Fetching state from block ${initialState.blockNumber} to ${streamStartBlockNumber}`,
80
);
const gapState = await fetchStateInBlockRange(fetchWorldEvents, initialState.blockNumber, streamStartBlockNumber);
const gapState = await fetchStateInBlockRange(
fetchWorldEvents,
initialState.blockNumber,
streamStartBlockNumber,
50,
this.setLoadingState.bind(this)
);
console.log(
`[SyncWorker] got ${gapState.state.size} items from block range ${initialState.blockNumber} -> ${streamStartBlockNumber}`
);
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/workers/syncUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("syncUtils", () => {

const fetchWorldEvents = jest.fn(() => Promise.resolve([event, event]));

const state = await fetchStateInBlockRange(fetchWorldEvents, 42, 6969, 100);
const state = await fetchStateInBlockRange(fetchWorldEvents, 42, 6969, 100, () => void 0);

expect(fetchWorldEvents).toHaveBeenCalledTimes(Math.ceil((6969 - 42) / 100));
expect(fetchWorldEvents).toHaveBeenCalledWith(42, 141);
Expand Down
13 changes: 12 additions & 1 deletion packages/network/src/workers/syncUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CacheStore, createCacheStore, storeEvent } from "./CacheStore";
import { abi as ComponentAbi } from "@latticexyz/solecs/abi/Component.json";
import { abi as WorldAbi } from "@latticexyz/solecs/abi/World.json";
import { Component, World } from "@latticexyz/solecs/types/ethers-contracts";
import { SyncState } from "./constants";
import { ECSStreamBlockBundleReply } from "@latticexyz/services/protobuf/ts/ecs-stream/ecs-stream";

/**
Expand Down Expand Up @@ -215,7 +216,8 @@ export async function fetchStateInBlockRange(
fetchWorldEvents: ReturnType<typeof createFetchWorldEventsInBlockRange>,
fromBlockNumber: number,
toBlockNumber: number,
interval = 50
interval = 50,
setLoadingState?: (state: SyncState, msg: string, percentage: number) => void
): Promise<CacheStore> {
const cacheStore = createCacheStore();
const delta = toBlockNumber - fromBlockNumber;
Expand All @@ -226,6 +228,15 @@ export async function fetchStateInBlockRange(
const from = steps[i];
const to = i === steps.length - 1 ? toBlockNumber : steps[i + 1] - 1;
const events = await fetchWorldEvents(from, to);

if (setLoadingState) {
setLoadingState(
SyncState.INITIAL,
`Fetching state from block ${fromBlockNumber} to ${toBlockNumber} (${i * interval}/${delta})`,
80
);
}

console.log(`[SyncWorker] initial sync fetched ${events.length} events from block range ${from} -> ${to}`);

for (const event of events) {
Expand Down

0 comments on commit d4ba338

Please sign in to comment.