/
syncToRecs.ts
251 lines (226 loc) · 8.53 KB
/
syncToRecs.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
import { StoreConfig, storeEventsAbi } from "@latticexyz/store";
import { Address, Block, Chain, Hex, PublicClient, TransactionReceipt, Transport } from "viem";
import {
ComponentValue,
Entity,
Component as RecsComponent,
Schema as RecsSchema,
World as RecsWorld,
getComponentValue,
setComponent,
} from "@latticexyz/recs";
import { BlockLogs, Table } from "../common";
import { TableRecord } from "@latticexyz/store";
import {
createBlockStream,
isNonPendingBlock,
blockRangeToLogs,
groupLogsByBlockNumber,
} from "@latticexyz/block-logs-stream";
import { filter, map, tap, mergeMap, from, concatMap, Observable, share, firstValueFrom } from "rxjs";
import { BlockStorageOperations, blockLogsToStorage } from "../blockLogsToStorage";
import { recsStorage } from "./recsStorage";
import { hexKeyTupleToEntity } from "./hexKeyTupleToEntity";
import { debug } from "./debug";
import { defineInternalComponents } from "./defineInternalComponents";
import { getTableKey } from "./getTableKey";
import { StoreComponentMetadata, SyncStep } from "./common";
import { encodeEntity } from "./encodeEntity";
import { createIndexerClient } from "../trpc-indexer";
type SyncToRecsOptions<
TConfig extends StoreConfig = StoreConfig,
TComponents extends Record<string, RecsComponent<RecsSchema, StoreComponentMetadata>> = Record<
string,
RecsComponent<RecsSchema, StoreComponentMetadata>
>
> = {
world: RecsWorld;
config: TConfig;
address: Address;
// TODO: make this optional and return one if none provided (but will need chain ID at least)
publicClient: PublicClient;
// TODO: generate these from config and return instead?
components: TComponents;
indexerUrl?: string;
initialState?: {
blockNumber: bigint | null;
tables: (Table & { records: TableRecord[] })[];
};
};
type SyncToRecsResult<
TConfig extends StoreConfig = StoreConfig,
TComponents extends Record<string, RecsComponent<RecsSchema, StoreComponentMetadata>> = Record<
string,
RecsComponent<RecsSchema, StoreComponentMetadata>
>
> = {
// TODO: return publicClient?
components: TComponents & ReturnType<typeof defineInternalComponents>;
singletonEntity: Entity;
latestBlock$: Observable<Block>;
latestBlockNumber$: Observable<bigint>;
blockLogs$: Observable<BlockLogs>;
blockStorageOperations$: Observable<BlockStorageOperations<TConfig>>;
waitForTransaction: (tx: Hex) => Promise<{ receipt: TransactionReceipt }>;
destroy: () => void;
};
export async function syncToRecs<
TConfig extends StoreConfig = StoreConfig,
TComponents extends Record<string, RecsComponent<RecsSchema, StoreComponentMetadata>> = Record<
string,
RecsComponent<RecsSchema, StoreComponentMetadata>
>
>({
world,
config,
address,
publicClient,
components: initialComponents,
initialState,
indexerUrl,
}: SyncToRecsOptions<TConfig, TComponents>): Promise<SyncToRecsResult<TConfig, TComponents>> {
const components = {
...initialComponents,
...defineInternalComponents(world),
};
const singletonEntity = world.registerEntity({ id: hexKeyTupleToEntity([]) });
let startBlock = 0n;
if (indexerUrl != null && initialState == null) {
const indexer = createIndexerClient({ url: indexerUrl });
try {
const chainId = publicClient.chain?.id ?? (await publicClient.getChainId());
initialState = await indexer.findAll.query({ chainId, address });
} catch (error) {
debug("couldn't get initial state from indexer", error);
}
}
if (initialState != null && initialState.blockNumber != null) {
debug("hydrating from initial state to block", initialState.blockNumber);
startBlock = initialState.blockNumber + 1n;
setComponent(components.SyncProgress, singletonEntity, {
step: SyncStep.SNAPSHOT,
message: `Hydrating from snapshot to block ${initialState.blockNumber}`,
percentage: 0,
latestBlockNumber: 0n,
lastBlockNumberProcessed: initialState.blockNumber,
});
const componentList = Object.values(components);
const numRecords = initialState.tables.reduce((sum, table) => sum + table.records.length, 0);
const recordsPerSyncProgressUpdate = Math.floor(numRecords / 100);
let recordsProcessed = 0;
for (const table of initialState.tables) {
setComponent(components.TableMetadata, getTableKey(table) as Entity, { table });
const component = componentList.find((component) => component.id === table.tableId);
if (component == null) {
debug(`no component found for table ${table.namespace}:${table.name}, skipping initial state`);
continue;
}
for (const record of table.records) {
const entity = encodeEntity(table.keySchema, record.key);
setComponent(component, entity, record.value as ComponentValue);
recordsProcessed++;
if (recordsProcessed % recordsPerSyncProgressUpdate === 0) {
setComponent(components.SyncProgress, singletonEntity, {
step: SyncStep.SNAPSHOT,
message: `Hydrating from snapshot to block ${initialState.blockNumber}`,
percentage: (recordsProcessed / numRecords) * 100,
latestBlockNumber: 0n,
lastBlockNumberProcessed: initialState.blockNumber,
});
}
}
debug(`hydrated ${table.records.length} records for table ${table.namespace}:${table.name}`);
}
setComponent(components.SyncProgress, singletonEntity, {
step: SyncStep.SNAPSHOT,
message: `Hydrating from snapshot to block ${initialState.blockNumber}`,
percentage: (recordsProcessed / numRecords) * 100,
latestBlockNumber: 0n,
lastBlockNumberProcessed: initialState.blockNumber,
});
}
// TODO: if startBlock is still 0, find via deploy event
debug("starting sync from block", startBlock);
const latestBlock$ = createBlockStream({ publicClient, blockTag: "latest" }).pipe(share());
const latestBlockNumber$ = latestBlock$.pipe(
filter(isNonPendingBlock),
map((block) => block.number),
share()
);
let latestBlockNumber: bigint | null = null;
const blockLogs$ = latestBlockNumber$.pipe(
tap((blockNumber) => {
debug("latest block number", blockNumber);
latestBlockNumber = blockNumber;
}),
map((blockNumber) => ({ startBlock, endBlock: blockNumber })),
blockRangeToLogs({
publicClient,
address,
events: storeEventsAbi,
}),
mergeMap(({ toBlock, logs }) => from(groupLogsByBlockNumber(logs, toBlock))),
share()
);
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);
lastBlockNumberProcessed = blockNumber;
if (
latestBlockNumber != null &&
getComponentValue(components.SyncProgress, singletonEntity)?.step !== SyncStep.LIVE
) {
if (lastBlockNumberProcessed < latestBlockNumber) {
setComponent(components.SyncProgress, singletonEntity, {
step: SyncStep.RPC,
message: `Hydrating from RPC to block ${latestBlockNumber}`,
percentage: (Number(lastBlockNumberProcessed) / Number(latestBlockNumber)) * 100,
latestBlockNumber,
lastBlockNumberProcessed,
});
} else {
setComponent(components.SyncProgress, singletonEntity, {
step: SyncStep.LIVE,
message: `All caught up!`,
percentage: 100,
latestBlockNumber,
lastBlockNumberProcessed,
});
}
}
}),
share()
);
// Start the sync
const sub = blockStorageOperations$.subscribe();
world.registerDisposer(() => sub.unsubscribe());
async function waitForTransaction(tx: Hex): Promise<{
receipt: TransactionReceipt;
}> {
// Wait for tx to be mined
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 (lastBlockNumberProcessed == null || lastBlockNumberProcessed < receipt.blockNumber) {
await firstValueFrom(
blockStorageOperations$.pipe(
filter(({ blockNumber }) => blockNumber != null && blockNumber >= receipt.blockNumber)
)
);
}
return { receipt };
}
return {
components,
singletonEntity,
latestBlock$,
latestBlockNumber$,
blockLogs$,
blockStorageOperations$,
waitForTransaction,
destroy: (): void => {
world.dispose();
},
};
}