ponder 0.8.2 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/sync/index.ts CHANGED
@@ -6,7 +6,6 @@ import {
6
6
  createHistoricalSync,
7
7
  } from "@/sync-historical/index.js";
8
8
  import {
9
- type BlockWithEventData,
10
9
  type RealtimeSync,
11
10
  type RealtimeSyncEvent,
12
11
  createRealtimeSync,
@@ -219,21 +218,23 @@ type CreateSyncParameters = {
219
218
  };
220
219
 
221
220
  export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
222
- const localSyncContext = new Map<
221
+ const perNetworkSync = new Map<
223
222
  Network,
224
223
  {
225
224
  requestQueue: RequestQueue;
226
225
  syncProgress: SyncProgress;
227
226
  historicalSync: HistoricalSync;
228
227
  realtimeSync: RealtimeSync;
229
- unfinalizedEventData: BlockWithEventData[];
228
+ unfinalizedBlocks: (Omit<
229
+ Extract<RealtimeSyncEvent, { type: "block" }>,
230
+ "type"
231
+ > & { events: RawEvent[] })[];
230
232
  }
231
233
  >();
232
234
  const status: Status = {};
233
235
  let isKilled = false;
234
236
  // Realtime events across all chains that can't be passed to the parent function
235
237
  // because the overall checkpoint hasn't caught up to the events yet.
236
- let pendingEvents: RawEvent[] = [];
237
238
 
238
239
  // Instantiate `localSyncData` and `status`
239
240
  await Promise.all(
@@ -322,12 +323,12 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
322
323
  0,
323
324
  );
324
325
 
325
- localSyncContext.set(network, {
326
+ perNetworkSync.set(network, {
326
327
  requestQueue,
327
328
  syncProgress,
328
329
  historicalSync,
329
330
  realtimeSync,
330
- unfinalizedEventData: [],
331
+ unfinalizedBlocks: [],
331
332
  });
332
333
  status[network.name] = { block: null, ready: false };
333
334
  }),
@@ -339,7 +340,7 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
339
340
  const getOmnichainCheckpoint = (
340
341
  tag: "start" | "end" | "current" | "finalized",
341
342
  ): string | undefined => {
342
- const checkpoints = Array.from(localSyncContext.entries()).map(
343
+ const checkpoints = Array.from(perNetworkSync.entries()).map(
343
344
  ([network, { syncProgress }]) =>
344
345
  getChainCheckpoint({ syncProgress, network, tag }),
345
346
  );
@@ -389,7 +390,7 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
389
390
  checkpoint: string;
390
391
  network: Network;
391
392
  }) => {
392
- const localBlock = localSyncContext
393
+ const localBlock = perNetworkSync
393
394
  .get(network)!
394
395
  .realtimeSync.unfinalizedBlocks.findLast(
395
396
  (block) =>
@@ -437,7 +438,7 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
437
438
  let showLogs = true;
438
439
  while (true) {
439
440
  const syncGenerator = mergeAsyncGenerators(
440
- Array.from(localSyncContext.entries()).map(
441
+ Array.from(perNetworkSync.entries()).map(
441
442
  ([network, { syncProgress, historicalSync }]) =>
442
443
  localHistoricalSyncGenerator({
443
444
  common: args.common,
@@ -459,7 +460,7 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
459
460
  * It is an invariant that `latestBlock` will eventually be defined.
460
461
  */
461
462
  if (
462
- Array.from(localSyncContext.values()).some(
463
+ Array.from(perNetworkSync.values()).some(
463
464
  ({ syncProgress }) => syncProgress.current === undefined,
464
465
  )
465
466
  ) {
@@ -546,7 +547,7 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
546
547
 
547
548
  /** `true` if all networks have synced all known finalized blocks. */
548
549
  const allHistoricalSyncExhaustive = Array.from(
549
- localSyncContext.values(),
550
+ perNetworkSync.values(),
550
551
  ).every(({ syncProgress }) => {
551
552
  if (isSyncEnd(syncProgress)) return true;
552
553
 
@@ -566,7 +567,7 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
566
567
  latestFinalizedFetch = Date.now();
567
568
 
568
569
  await Promise.all(
569
- Array.from(localSyncContext.entries()).map(
570
+ Array.from(perNetworkSync.entries()).map(
570
571
  async ([network, { requestQueue, syncProgress }]) => {
571
572
  args.common.logger.debug({
572
573
  service: "sync",
@@ -611,8 +612,8 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
611
612
  network,
612
613
  event,
613
614
  }: { network: Network; event: RealtimeSyncEvent }) => {
614
- const { syncProgress, realtimeSync, unfinalizedEventData } =
615
- localSyncContext.get(network)!;
615
+ const { syncProgress, realtimeSync, unfinalizedBlocks } =
616
+ perNetworkSync.get(network)!;
616
617
 
617
618
  switch (event.type) {
618
619
  /**
@@ -630,17 +631,7 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
630
631
  hexToNumber(syncProgress.current.number),
631
632
  );
632
633
 
633
- const blockWithEventData = {
634
- block: event.block,
635
- filters: event.filters,
636
- logs: event.logs,
637
- factoryLogs: event.factoryLogs,
638
- traces: event.traces,
639
- transactions: event.transactions,
640
- transactionReceipts: event.transactionReceipts,
641
- };
642
-
643
- unfinalizedEventData.push(blockWithEventData);
634
+ const blockWithEventData = event;
644
635
 
645
636
  const events = buildEvents({
646
637
  sources: args.sources,
@@ -650,19 +641,27 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
650
641
  unfinalizedChildAddresses: realtimeSync.unfinalizedChildAddresses,
651
642
  });
652
643
 
653
- pendingEvents.push(...events);
644
+ unfinalizedBlocks.push({ ...blockWithEventData, events });
654
645
 
655
646
  if (to > from) {
656
647
  for (const network of args.networks) {
657
648
  updateRealtimeStatus({ checkpoint: to, network });
658
649
  }
659
650
 
660
- const events = pendingEvents
661
- .filter(({ checkpoint }) => checkpoint <= to)
662
- .sort((a, b) => (a.checkpoint < b.checkpoint ? -1 : 1));
651
+ const pendingEvents: RawEvent[] = [];
663
652
 
664
- pendingEvents = pendingEvents.filter(
665
- ({ checkpoint }) => checkpoint > to,
653
+ for (const { unfinalizedBlocks } of perNetworkSync.values()) {
654
+ for (const { events } of unfinalizedBlocks) {
655
+ for (const event of events) {
656
+ if (event.checkpoint > from && event.checkpoint <= to) {
657
+ pendingEvents.push(event);
658
+ }
659
+ }
660
+ }
661
+ }
662
+
663
+ const events = pendingEvents.sort((a, b) =>
664
+ a.checkpoint < b.checkpoint ? -1 : 1,
666
665
  );
667
666
 
668
667
  args
@@ -704,17 +703,6 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
704
703
  args.onRealtimeEvent({ type: "finalize", checkpoint });
705
704
  }
706
705
 
707
- const finalizedEventData = unfinalizedEventData.filter(
708
- (ued) =>
709
- hexToNumber(ued.block.number) <= hexToNumber(event.block.number),
710
- );
711
-
712
- localSyncContext.get(network)!.unfinalizedEventData =
713
- unfinalizedEventData.filter(
714
- (ued) =>
715
- hexToNumber(ued.block.number) > hexToNumber(event.block.number),
716
- );
717
-
718
706
  if (
719
707
  getChainCheckpoint({ syncProgress, network, tag: "finalized" })! >
720
708
  getOmnichainCheckpoint("current")!
@@ -723,57 +711,68 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
723
711
  service: "sync",
724
712
  msg: `Finalized block for '${network.name}' has surpassed overall indexing checkpoint`,
725
713
  });
714
+ // exit early because we need to keep `unfinalizedBlocks.events`
715
+ return;
726
716
  }
727
717
 
718
+ const finalizedBlocks = unfinalizedBlocks.filter(
719
+ ({ block }) =>
720
+ hexToNumber(block.number) <= hexToNumber(event.block.number),
721
+ );
722
+
723
+ perNetworkSync.get(network)!.unfinalizedBlocks =
724
+ unfinalizedBlocks.filter(
725
+ ({ block }) =>
726
+ hexToNumber(block.number) > hexToNumber(event.block.number),
727
+ );
728
+
728
729
  // Add finalized blocks, logs, transactions, receipts, and traces to the sync-store.
729
730
 
730
731
  await Promise.all([
731
732
  args.syncStore.insertBlocks({
732
- blocks: finalizedEventData
733
- .filter(({ filters }) => filters.size > 0)
733
+ blocks: finalizedBlocks
734
+ .filter(({ hasMatchedFilter }) => hasMatchedFilter)
734
735
  .map(({ block }) => block),
735
736
  chainId: network.chainId,
736
737
  }),
737
738
  args.syncStore.insertLogs({
738
- logs: finalizedEventData.flatMap(({ logs, block }) =>
739
+ logs: finalizedBlocks.flatMap(({ logs, block }) =>
739
740
  logs.map((log) => ({ log, block })),
740
741
  ),
741
742
  shouldUpdateCheckpoint: true,
742
743
  chainId: network.chainId,
743
744
  }),
744
745
  args.syncStore.insertLogs({
745
- logs: finalizedEventData.flatMap(({ factoryLogs }) =>
746
+ logs: finalizedBlocks.flatMap(({ factoryLogs }) =>
746
747
  factoryLogs.map((log) => ({ log })),
747
748
  ),
748
749
  shouldUpdateCheckpoint: false,
749
750
  chainId: network.chainId,
750
751
  }),
751
752
  args.syncStore.insertTransactions({
752
- transactions: finalizedEventData.flatMap(
753
- ({ transactions, block }) =>
754
- transactions.map((transaction) => ({
755
- transaction,
756
- block,
757
- })),
753
+ transactions: finalizedBlocks.flatMap(({ transactions, block }) =>
754
+ transactions.map((transaction) => ({
755
+ transaction,
756
+ block,
757
+ })),
758
758
  ),
759
759
  chainId: network.chainId,
760
760
  }),
761
761
  args.syncStore.insertTransactionReceipts({
762
- transactionReceipts: finalizedEventData.flatMap(
762
+ transactionReceipts: finalizedBlocks.flatMap(
763
763
  ({ transactionReceipts }) => transactionReceipts,
764
764
  ),
765
765
  chainId: network.chainId,
766
766
  }),
767
767
  args.syncStore.insertTraces({
768
- traces: finalizedEventData.flatMap(
769
- ({ traces, block, transactions }) =>
770
- traces.map((trace) => ({
771
- trace,
772
- block,
773
- transaction: transactions.find(
774
- (t) => t.hash === trace.transactionHash,
775
- )!,
776
- })),
768
+ traces: finalizedBlocks.flatMap(({ traces, block, transactions }) =>
769
+ traces.map((trace) => ({
770
+ trace,
771
+ block,
772
+ transaction: transactions.find(
773
+ (t) => t.hash === trace.transactionHash,
774
+ )!,
775
+ })),
777
776
  ),
778
777
  chainId: network.chainId,
779
778
  }),
@@ -824,21 +823,12 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
824
823
  hexToNumber(syncProgress.current.number),
825
824
  );
826
825
 
827
- localSyncContext.get(network)!.unfinalizedEventData =
828
- unfinalizedEventData.filter(
829
- (led) =>
830
- hexToNumber(led.block.number) <= hexToNumber(event.block.number),
826
+ perNetworkSync.get(network)!.unfinalizedBlocks =
827
+ unfinalizedBlocks.filter(
828
+ ({ block }) =>
829
+ hexToNumber(block.number) <= hexToNumber(event.block.number),
831
830
  );
832
831
 
833
- const reorgedHashes = new Set<Hash>();
834
- for (const b of event.reorgedBlocks) {
835
- reorgedHashes.add(b.hash);
836
- }
837
-
838
- pendingEvents = pendingEvents.filter(
839
- (e) => reorgedHashes.has(e.block.hash) === false,
840
- );
841
-
842
832
  await args.syncStore.pruneRpcRequestResult({
843
833
  blocks: event.reorgedBlocks,
844
834
  chainId: network.chainId,
@@ -858,7 +848,7 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
858
848
  getEvents,
859
849
  async startRealtime() {
860
850
  for (const network of args.networks) {
861
- const { syncProgress, realtimeSync } = localSyncContext.get(network)!;
851
+ const { syncProgress, realtimeSync } = perNetworkSync.get(network)!;
862
852
 
863
853
  status[network.name]!.block = {
864
854
  number: hexToNumber(syncProgress.current!.number),
@@ -907,14 +897,14 @@ export const createSync = async (args: CreateSyncParameters): Promise<Sync> => {
907
897
  return status;
908
898
  },
909
899
  getCachedTransport(network) {
910
- const { requestQueue } = localSyncContext.get(network)!;
900
+ const { requestQueue } = perNetworkSync.get(network)!;
911
901
  return cachedTransport({ requestQueue, syncStore: args.syncStore });
912
902
  },
913
903
  async kill() {
914
904
  isKilled = true;
915
905
  const promises: Promise<void>[] = [];
916
906
  for (const network of args.networks) {
917
- const { historicalSync, realtimeSync } = localSyncContext.get(network)!;
907
+ const { historicalSync, realtimeSync } = perNetworkSync.get(network)!;
918
908
  historicalSync.kill();
919
909
  promises.push(realtimeSync.kill());
920
910
  }
@@ -66,7 +66,7 @@ type CreateRealtimeSyncParameters = {
66
66
 
67
67
  export type BlockWithEventData = {
68
68
  block: SyncBlock;
69
- filters: Set<Filter>;
69
+ // filters: Set<Filter>;
70
70
  logs: SyncLog[];
71
71
  factoryLogs: SyncLog[];
72
72
  traces: SyncTrace[];
@@ -77,6 +77,7 @@ export type BlockWithEventData = {
77
77
  export type RealtimeSyncEvent =
78
78
  | ({
79
79
  type: "block";
80
+ hasMatchedFilter: boolean;
80
81
  } & BlockWithEventData)
81
82
  | {
82
83
  type: "finalize";
@@ -111,7 +112,7 @@ export const createRealtimeSync = (
111
112
  * `parentHash` => `hash`.
112
113
  */
113
114
  let unfinalizedBlocks: LightBlock[] = [];
114
- let queue: Queue<void, Omit<BlockWithEventData, "filters">>;
115
+ let queue: Queue<void, BlockWithEventData>;
115
116
  let consecutiveErrors = 0;
116
117
  let interval: NodeJS.Timeout | undefined;
117
118
 
@@ -186,7 +187,7 @@ export const createRealtimeSync = (
186
187
  traces,
187
188
  transactions,
188
189
  transactionReceipts,
189
- }: Omit<BlockWithEventData, "filters">) => {
190
+ }: BlockWithEventData) => {
190
191
  args.common.logger.debug({
191
192
  service: "realtime",
192
193
  msg: `Started syncing '${args.network.name}' block ${hexToNumber(block.number)}`,
@@ -400,7 +401,7 @@ export const createRealtimeSync = (
400
401
 
401
402
  await args.onEvent({
402
403
  type: "block",
403
- filters: matchedFilters,
404
+ hasMatchedFilter: matchedFilters.size > 0,
404
405
  block,
405
406
  factoryLogs,
406
407
  logs,
@@ -584,7 +585,7 @@ export const createRealtimeSync = (
584
585
  */
585
586
  const fetchBlockEventData = async (
586
587
  block: SyncBlock,
587
- ): Promise<Omit<BlockWithEventData, "filters">> => {
588
+ ): Promise<BlockWithEventData> => {
588
589
  ////////
589
590
  // Logs
590
591
  ////////