/
createStoreSync.ts
292 lines (261 loc) · 8.99 KB
/
createStoreSync.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { StoreConfig, storeEventsAbi } from "@latticexyz/store";
import { Hex, TransactionReceiptNotFoundError } from "viem";
import {
StorageAdapter,
StorageAdapterBlock,
StoreEventsLog,
SyncFilter,
SyncOptions,
SyncResult,
internalTableIds,
} from "./common";
import { createBlockStream } from "@latticexyz/block-logs-stream";
import {
filter,
map,
tap,
from,
concat,
concatMap,
share,
firstValueFrom,
defer,
of,
catchError,
shareReplay,
combineLatest,
scan,
identity,
} from "rxjs";
import { debug as parentDebug } from "./debug";
import { SyncStep } from "./SyncStep";
import { bigIntMax, chunk, isDefined, waitForIdle } from "@latticexyz/common/utils";
import { getSnapshot } from "./getSnapshot";
import { fetchAndStoreLogs } from "./fetchAndStoreLogs";
const debug = parentDebug.extend("createStoreSync");
const defaultFilters: SyncFilter[] = internalTableIds.map((tableId) => ({ tableId }));
type CreateStoreSyncOptions<TConfig extends StoreConfig = StoreConfig> = SyncOptions<TConfig> & {
storageAdapter: StorageAdapter;
onProgress?: (opts: {
step: SyncStep;
percentage: number;
latestBlockNumber: bigint;
lastBlockNumberProcessed: bigint;
message: string;
}) => void;
};
export async function createStoreSync<TConfig extends StoreConfig = StoreConfig>({
storageAdapter,
onProgress,
publicClient,
address,
filters: initialFilters = [],
tableIds = [],
startBlock: initialStartBlock = 0n,
maxBlockRange,
initialState,
initialBlockLogs,
indexerUrl,
}: CreateStoreSyncOptions<TConfig>): Promise<SyncResult> {
const filters: SyncFilter[] =
initialFilters.length || tableIds.length
? [...initialFilters, ...tableIds.map((tableId) => ({ tableId })), ...defaultFilters]
: [];
const logFilter = filters.length
? (log: StoreEventsLog): boolean =>
filters.some(
(filter) =>
filter.tableId === log.args.tableId &&
(filter.key0 == null || filter.key0 === log.args.keyTuple[0]) &&
(filter.key1 == null || filter.key1 === log.args.keyTuple[1])
)
: undefined;
const initialBlockLogs$ = defer(async (): Promise<StorageAdapterBlock | undefined> => {
const chainId = publicClient.chain?.id ?? (await publicClient.getChainId());
onProgress?.({
step: SyncStep.SNAPSHOT,
percentage: 0,
latestBlockNumber: 0n,
lastBlockNumberProcessed: 0n,
message: "Getting snapshot",
});
const snapshot = await getSnapshot({
chainId,
address,
filters,
initialState,
initialBlockLogs,
indexerUrl,
});
onProgress?.({
step: SyncStep.SNAPSHOT,
percentage: 100,
latestBlockNumber: 0n,
lastBlockNumberProcessed: 0n,
message: "Got snapshot",
});
return snapshot;
}).pipe(
catchError((error) => {
debug("error getting snapshot", error);
onProgress?.({
step: SyncStep.SNAPSHOT,
percentage: 100,
latestBlockNumber: 0n,
lastBlockNumberProcessed: initialStartBlock,
message: "Failed to get snapshot",
});
return of(undefined);
}),
shareReplay(1)
);
const storedInitialBlockLogs$ = initialBlockLogs$.pipe(
filter(isDefined),
concatMap(async ({ blockNumber, logs }) => {
debug("hydrating", logs.length, "logs to block", blockNumber);
onProgress?.({
step: SyncStep.SNAPSHOT,
percentage: 0,
latestBlockNumber: 0n,
lastBlockNumberProcessed: blockNumber,
message: "Hydrating from snapshot",
});
// Split snapshot operations into chunks so we can update the progress callback (and ultimately render visual progress for the user).
// This isn't ideal if we want to e.g. batch load these into a DB in a single DB tx, but we'll take it.
//
// Split into 50 equal chunks (for better `onProgress` updates) but only if we have 100+ items per chunk
const chunkSize = Math.max(100, Math.floor(logs.length / 50));
const chunks = Array.from(chunk(logs, chunkSize));
for (const [i, chunk] of chunks.entries()) {
await storageAdapter({ blockNumber, logs: chunk });
onProgress?.({
step: SyncStep.SNAPSHOT,
percentage: ((i + 1) / chunks.length) * 100,
latestBlockNumber: 0n,
lastBlockNumberProcessed: blockNumber,
message: "Hydrating from snapshot",
});
// RECS is a synchronous API so hydrating in a loop like this blocks downstream render cycles
// that would display the percentage climbing up to 100.
// We wait for idle callback here to give rendering a chance to complete.
await waitForIdle();
}
onProgress?.({
step: SyncStep.SNAPSHOT,
percentage: 100,
latestBlockNumber: 0n,
lastBlockNumberProcessed: blockNumber,
message: "Hydrated from snapshot",
});
return { blockNumber, logs };
}),
shareReplay(1)
);
const startBlock$ = initialBlockLogs$.pipe(
map((block) => bigIntMax(block?.blockNumber ?? 0n, initialStartBlock)),
// TODO: if start block is still 0, find via deploy event
tap((startBlock) => debug("starting sync from block", startBlock))
);
const latestBlock$ = createBlockStream({ publicClient, blockTag: "latest" }).pipe(shareReplay(1));
const latestBlockNumber$ = latestBlock$.pipe(
map((block) => block.number),
tap((blockNumber) => {
debug("latest block number", blockNumber);
}),
shareReplay(1)
);
let startBlock: bigint | null = null;
let endBlock: bigint | null = null;
let lastBlockNumberProcessed: bigint | null = null;
const storedBlock$ = combineLatest([startBlock$, latestBlockNumber$]).pipe(
map(([startBlock, endBlock]) => ({ startBlock, endBlock })),
tap((range) => {
startBlock = range.startBlock;
endBlock = range.endBlock;
}),
concatMap((range) => {
const storedBlocks = fetchAndStoreLogs({
publicClient,
address,
events: storeEventsAbi,
maxBlockRange,
fromBlock: lastBlockNumberProcessed
? bigIntMax(range.startBlock, lastBlockNumberProcessed + 1n)
: range.startBlock,
toBlock: range.endBlock,
storageAdapter,
logFilter,
});
return from(storedBlocks);
}),
tap(({ blockNumber, logs }) => {
debug("stored", logs.length, "logs for block", blockNumber);
lastBlockNumberProcessed = blockNumber;
if (startBlock != null && endBlock != null) {
if (blockNumber < endBlock) {
const totalBlocks = endBlock - startBlock;
const processedBlocks = lastBlockNumberProcessed - startBlock;
onProgress?.({
step: SyncStep.RPC,
percentage: Number((processedBlocks * 1000n) / totalBlocks) / 10,
latestBlockNumber: endBlock,
lastBlockNumberProcessed,
message: "Hydrating from RPC",
});
} else {
onProgress?.({
step: SyncStep.LIVE,
percentage: 100,
latestBlockNumber: endBlock,
lastBlockNumberProcessed,
message: "All caught up!",
});
}
}
}),
share()
);
const storedBlockLogs$ = concat(storedInitialBlockLogs$, storedBlock$).pipe(share());
// keep 10 blocks worth processed transactions in memory
const recentBlocksWindow = 10;
// most recent block first, for ease of pulling the first one off the array
const recentBlocks$ = storedBlockLogs$.pipe(
scan<StorageAdapterBlock, StorageAdapterBlock[]>(
(recentBlocks, block) => [block, ...recentBlocks].slice(0, recentBlocksWindow),
[]
),
filter((recentBlocks) => recentBlocks.length > 0),
shareReplay(1)
);
// TODO: move to its own file so we can test it, have its own debug instance, etc.
async function waitForTransaction(tx: Hex): Promise<void> {
debug("waiting for tx", tx);
// This currently blocks for async call on each block processed
// We could potentially speed this up a tiny bit by racing to see if 1) tx exists in processed block or 2) fetch tx receipt for latest block processed
const hasTransaction$ = recentBlocks$.pipe(
concatMap(async (blocks) => {
const txs = blocks.flatMap((block) => block.logs.map((op) => op.transactionHash).filter(isDefined));
if (txs.includes(tx)) return true;
try {
const lastBlock = blocks[0];
debug("fetching tx receipt for block", lastBlock.blockNumber);
const receipt = await publicClient.getTransactionReceipt({ hash: tx });
return lastBlock.blockNumber >= receipt.blockNumber;
} catch (error) {
if (error instanceof TransactionReceiptNotFoundError) {
return false;
}
throw error;
}
}),
tap((result) => debug("has tx?", tx, result))
);
await firstValueFrom(hasTransaction$.pipe(filter(identity)));
}
return {
latestBlock$,
latestBlockNumber$,
storedBlockLogs$,
waitForTransaction,
};
}