@apibara/protocol 2.1.0-beta.51 → 2.1.0-beta.53

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/dist/index.cjs CHANGED
@@ -6,7 +6,7 @@ const assert = require('node:assert');
6
6
  const consola = require('consola');
7
7
  const niceGrpc = require('nice-grpc');
8
8
  require('protobufjs/minimal.js');
9
- const rpc_index = require('./shared/protocol.54f17699.cjs');
9
+ const rpc_index = require('./shared/protocol.27cb7b14.cjs');
10
10
  require('viem');
11
11
  require('long');
12
12
 
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { s as stream } from './shared/protocol.0e734e33.cjs';
2
2
  export { B as Bytes, b as BytesFromUint8Array, w as Client, u as ClientCallOptions, x as CreateClientOptions, C as Cursor, e as CursorFromBytes, c as CursorProto, q as Data, g as DataFinality, h as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, k as Duration, j as DurationCodec, F as Finalize, G as GrpcClient, H as Heartbeat, I as Invalidate, R as ResponseWithoutData, S as StatusRequest, f as StatusResponse, o as StdErr, m as StdOut, t as StreamConfig, A as StreamDataIterable, v as StreamDataOptions, l as StreamDataRequest, r as StreamDataResponse, p as SystemMessage, T as TimeoutError, z as createAuthenticatedClient, y as createClient, d as createCursor, i as isCursor, n as normalizeCursor } from './shared/protocol.0e734e33.cjs';
3
3
  import _m0 from 'protobufjs/minimal.js';
4
- export { i as rpc } from './shared/protocol.7aa4aab6.cjs';
4
+ export { i as rpc } from './shared/protocol.d99fa7ff.cjs';
5
5
  export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
6
6
  import './codec.cjs';
7
7
  import 'nice-grpc-common';
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { s as stream } from './shared/protocol.21e66b9e.mjs';
2
2
  export { B as Bytes, b as BytesFromUint8Array, w as Client, u as ClientCallOptions, x as CreateClientOptions, C as Cursor, e as CursorFromBytes, c as CursorProto, q as Data, g as DataFinality, h as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, k as Duration, j as DurationCodec, F as Finalize, G as GrpcClient, H as Heartbeat, I as Invalidate, R as ResponseWithoutData, S as StatusRequest, f as StatusResponse, o as StdErr, m as StdOut, t as StreamConfig, A as StreamDataIterable, v as StreamDataOptions, l as StreamDataRequest, r as StreamDataResponse, p as SystemMessage, T as TimeoutError, z as createAuthenticatedClient, y as createClient, d as createCursor, i as isCursor, n as normalizeCursor } from './shared/protocol.21e66b9e.mjs';
3
3
  import _m0 from 'protobufjs/minimal.js';
4
- export { i as rpc } from './shared/protocol.6ab8d6dd.mjs';
4
+ export { i as rpc } from './shared/protocol.e53aa437.mjs';
5
5
  export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
6
6
  import './codec.mjs';
7
7
  import 'nice-grpc-common';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { s as stream } from './shared/protocol.8fb09325.js';
2
2
  export { B as Bytes, b as BytesFromUint8Array, w as Client, u as ClientCallOptions, x as CreateClientOptions, C as Cursor, e as CursorFromBytes, c as CursorProto, q as Data, g as DataFinality, h as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, k as Duration, j as DurationCodec, F as Finalize, G as GrpcClient, H as Heartbeat, I as Invalidate, R as ResponseWithoutData, S as StatusRequest, f as StatusResponse, o as StdErr, m as StdOut, t as StreamConfig, A as StreamDataIterable, v as StreamDataOptions, l as StreamDataRequest, r as StreamDataResponse, p as SystemMessage, T as TimeoutError, z as createAuthenticatedClient, y as createClient, d as createCursor, i as isCursor, n as normalizeCursor } from './shared/protocol.8fb09325.js';
3
3
  import _m0 from 'protobufjs/minimal.js';
4
- export { i as rpc } from './shared/protocol.8407f25e.js';
4
+ export { i as rpc } from './shared/protocol.f900827e.js';
5
5
  export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
6
6
  import './codec.js';
7
7
  import 'nice-grpc-common';
package/dist/index.mjs CHANGED
@@ -6,7 +6,7 @@ import consola from 'consola';
6
6
  import { createChannel, createClient as createClient$1, Metadata } from 'nice-grpc';
7
7
  export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
8
8
  import 'protobufjs/minimal.js';
9
- export { i as rpc } from './shared/protocol.bde61588.mjs';
9
+ export { i as rpc } from './shared/protocol.05fab89c.mjs';
10
10
  import 'viem';
11
11
  import 'long';
12
12
 
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const rpc_index = require('../shared/protocol.54f17699.cjs');
3
+ const rpc_index = require('../shared/protocol.27cb7b14.cjs');
4
4
  require('viem');
5
5
 
6
6
 
@@ -1,4 +1,4 @@
1
- export { B as BlockInfo, c as FetchBlockByNumberArgs, d as FetchBlockByNumberResult, F as FetchBlockRangeArgs, b as FetchBlockRangeResult, a as FetchBlockResult, e as FetchCursorArgs, f as RpcClient, h as RpcDataStream, R as RpcStreamConfig, V as ValidateFilterResult, g as createRpcClient } from '../shared/protocol.7aa4aab6.cjs';
1
+ export { B as BlockInfo, c as FetchBlockByNumberArgs, d as FetchBlockByNumberResult, F as FetchBlockRangeArgs, b as FetchBlockRangeResult, a as FetchBlockResult, e as FetchCursorArgs, f as FetchCursorRangeArgs, g as RpcClient, j as RpcDataStream, R as RpcStreamConfig, V as ValidateFilterResult, h as createRpcClient } from '../shared/protocol.d99fa7ff.cjs';
2
2
  import '../shared/protocol.0e734e33.cjs';
3
3
  import '../codec.cjs';
4
4
  import 'nice-grpc-common';
@@ -1,4 +1,4 @@
1
- export { B as BlockInfo, c as FetchBlockByNumberArgs, d as FetchBlockByNumberResult, F as FetchBlockRangeArgs, b as FetchBlockRangeResult, a as FetchBlockResult, e as FetchCursorArgs, f as RpcClient, h as RpcDataStream, R as RpcStreamConfig, V as ValidateFilterResult, g as createRpcClient } from '../shared/protocol.6ab8d6dd.mjs';
1
+ export { B as BlockInfo, c as FetchBlockByNumberArgs, d as FetchBlockByNumberResult, F as FetchBlockRangeArgs, b as FetchBlockRangeResult, a as FetchBlockResult, e as FetchCursorArgs, f as FetchCursorRangeArgs, g as RpcClient, j as RpcDataStream, R as RpcStreamConfig, V as ValidateFilterResult, h as createRpcClient } from '../shared/protocol.e53aa437.mjs';
2
2
  import '../shared/protocol.21e66b9e.mjs';
3
3
  import '../codec.mjs';
4
4
  import 'nice-grpc-common';
@@ -1,4 +1,4 @@
1
- export { B as BlockInfo, c as FetchBlockByNumberArgs, d as FetchBlockByNumberResult, F as FetchBlockRangeArgs, b as FetchBlockRangeResult, a as FetchBlockResult, e as FetchCursorArgs, f as RpcClient, h as RpcDataStream, R as RpcStreamConfig, V as ValidateFilterResult, g as createRpcClient } from '../shared/protocol.8407f25e.js';
1
+ export { B as BlockInfo, c as FetchBlockByNumberArgs, d as FetchBlockByNumberResult, F as FetchBlockRangeArgs, b as FetchBlockRangeResult, a as FetchBlockResult, e as FetchCursorArgs, f as FetchCursorRangeArgs, g as RpcClient, j as RpcDataStream, R as RpcStreamConfig, V as ValidateFilterResult, h as createRpcClient } from '../shared/protocol.f900827e.js';
2
2
  import '../shared/protocol.8fb09325.js';
3
3
  import '../codec.js';
4
4
  import 'nice-grpc-common';
@@ -1,3 +1,3 @@
1
- export { a as RpcClient, b as RpcDataStream, R as RpcStreamConfig, c as createRpcClient } from '../shared/protocol.bde61588.mjs';
1
+ export { a as RpcClient, b as RpcDataStream, R as RpcStreamConfig, c as createRpcClient } from '../shared/protocol.05fab89c.mjs';
2
2
  import 'viem';
3
3
  //# sourceMappingURL=index.mjs.map
@@ -28,14 +28,20 @@ var __privateSet = (obj, member, value, setter) => {
28
28
  setter ? setter.call(obj, value) : member.set(obj, value);
29
29
  return value;
30
30
  };
31
- var _finalized, _head, _canonical;
31
+ var _finalized, _head, _canonical, _batchSize;
32
32
  class ChainTracker {
33
- constructor({ head, finalized }) {
33
+ constructor({
34
+ head,
35
+ finalized,
36
+ batchSize
37
+ }) {
34
38
  __privateAdd(this, _finalized, void 0);
35
39
  __privateAdd(this, _head, void 0);
36
40
  __privateAdd(this, _canonical, void 0);
41
+ __privateAdd(this, _batchSize, void 0);
37
42
  __privateSet(this, _finalized, finalized);
38
43
  __privateSet(this, _head, head);
44
+ __privateSet(this, _batchSize, batchSize);
39
45
  __privateSet(this, _canonical, /* @__PURE__ */ new Map([
40
46
  [finalized.blockNumber, finalized],
41
47
  [head.blockNumber, head]
@@ -85,7 +91,8 @@ class ChainTracker {
85
91
  }
86
92
  async updateHead({
87
93
  newHead,
88
- fetchCursorByHash
94
+ fetchCursorByHash,
95
+ fetchCursorRange
89
96
  }) {
90
97
  if (newHead.blockNumber === __privateGet(this, _head).blockNumber && newHead.blockHash === __privateGet(this, _head).blockHash) {
91
98
  return { status: "unchanged" };
@@ -135,46 +142,43 @@ class ChainTracker {
135
142
  }
136
143
  throw new Error("Cannot reconcile new head with canonical chain.");
137
144
  }
138
- let current = newHead;
139
- let reorgDetected = false;
140
- const blocksToApply = [newHead];
145
+ let currentBlockNumber = __privateGet(this, _head).blockNumber + 1n;
141
146
  while (true) {
142
- const parent = await fetchCursorByHash(current.parentBlockHash);
143
- if (!parent) {
144
- throw new Error(
145
- "Cannot reconcile new head with canonical chain: failed to fetch parent"
146
- );
147
+ let endBlockNumber = currentBlockNumber + __privateGet(this, _batchSize);
148
+ if (endBlockNumber > newHead.blockNumber) {
149
+ endBlockNumber = newHead.blockNumber;
147
150
  }
148
- if (parent.blockNumber === __privateGet(this, _head).blockNumber) {
149
- if (parent.blockHash === __privateGet(this, _head).blockHash) {
150
- break;
151
- }
152
- const headParent = __privateGet(this, _canonical).get(__privateGet(this, _head).blockNumber - 1n);
153
- if (!headParent) {
151
+ const missing = await fetchCursorRange({
152
+ startBlockNumber: currentBlockNumber,
153
+ endBlockNumber
154
+ });
155
+ for (const block of missing) {
156
+ const canonicalParent = __privateGet(this, _canonical).get(block.blockNumber - 1n);
157
+ if (!canonicalParent || canonicalParent.blockHash !== block.parentBlockHash) {
154
158
  throw new Error(
155
- "Cannot reconcile new head with canonical chain: missing parent in canonical chain"
159
+ "Chain reorganization detected. Recovery not implemented"
156
160
  );
157
161
  }
158
- __privateGet(this, _canonical).delete(__privateGet(this, _head).blockNumber);
159
- __privateSet(this, _head, headParent);
160
- reorgDetected = true;
162
+ __privateGet(this, _canonical).set(block.blockNumber, block);
161
163
  }
162
- blocksToApply.push(parent);
163
- current = parent;
164
- }
165
- for (const block of blocksToApply.reverse()) {
166
- __privateGet(this, _canonical).set(block.blockNumber, block);
164
+ if (endBlockNumber === newHead.blockNumber) {
165
+ break;
166
+ }
167
+ currentBlockNumber = endBlockNumber + 1n;
167
168
  }
168
- const previousHead = __privateGet(this, _head);
169
169
  __privateSet(this, _head, newHead);
170
- if (reorgDetected) {
171
- return {
172
- status: "reorg",
173
- cursor: blockInfoToCursor(previousHead)
174
- };
175
- }
176
170
  return { status: "success" };
177
171
  }
172
+ isCanonical({ orderKey, uniqueKey }) {
173
+ if (!uniqueKey) {
174
+ return true;
175
+ }
176
+ const block = __privateGet(this, _canonical).get(orderKey);
177
+ if (!block) {
178
+ return true;
179
+ }
180
+ return block.blockHash === uniqueKey;
181
+ }
178
182
  async initializeStartingCursor({
179
183
  cursor,
180
184
  fetchCursor
@@ -228,11 +232,13 @@ class ChainTracker {
228
232
  _finalized = new WeakMap();
229
233
  _head = new WeakMap();
230
234
  _canonical = new WeakMap();
235
+ _batchSize = new WeakMap();
231
236
  function createChainTracker({
232
237
  head,
233
- finalized
238
+ finalized,
239
+ batchSize
234
240
  }) {
235
- return new ChainTracker({ finalized, head });
241
+ return new ChainTracker({ finalized, head, batchSize });
236
242
  }
237
243
 
238
244
  var __defProp = Object.defineProperty;
@@ -273,7 +279,8 @@ class RpcDataStream {
273
279
  }
274
280
  const chainTracker = createChainTracker({
275
281
  head,
276
- finalized
282
+ finalized,
283
+ batchSize: 20n
277
284
  });
278
285
  let cursor;
279
286
  if (this.request.startingCursor) {
@@ -293,6 +300,7 @@ class RpcDataStream {
293
300
  cursor,
294
301
  lastHeartbeat: Date.now(),
295
302
  lastFinalizedRefresh: Date.now(),
303
+ lastHeadRefresh: Date.now(),
296
304
  lastBackfillMessage: Date.now(),
297
305
  chainTracker,
298
306
  config: this.config,
@@ -330,7 +338,7 @@ async function* dataStreamLoop(state) {
330
338
  if (isAtHead(state)) {
331
339
  yield* waitForHeadChange(state);
332
340
  } else {
333
- yield* produceNextBlock(state);
341
+ yield* produceLiveBlocks(state);
334
342
  }
335
343
  }
336
344
  }
@@ -341,7 +349,7 @@ async function* backfillFinalizedBlocks(state) {
341
349
  const force = shouldForceBackfill(state);
342
350
  const filterData = await config.fetchBlockRange({
343
351
  startBlock: cursor.orderKey + 1n,
344
- finalizedBlock: finalized.orderKey,
352
+ maxBlock: finalized.orderKey,
345
353
  force,
346
354
  filter
347
355
  });
@@ -370,43 +378,59 @@ async function* backfillFinalizedBlocks(state) {
370
378
  state.cursor = { orderKey: filterData.endBlock };
371
379
  }
372
380
  }
373
- async function* produceNextBlock(state) {
374
- const currentBlockHash = state.cursor.uniqueKey;
375
- if (currentBlockHash === void 0) {
376
- throw new Error("Live production phase without cursor's hash.");
377
- }
378
- const result = await state.config.fetchBlockByNumber({
379
- blockNumber: state.cursor.orderKey + 1n,
380
- isAtHead: isAtHead(state),
381
- expectedParentBlockHash: currentBlockHash,
382
- filter: state.filter
383
- });
384
- if (result.status === "reorg") {
385
- throw new Error("Reorg not implemented");
381
+ async function* produceLiveBlocks(state) {
382
+ const { config, cursor, chainTracker, filter } = state;
383
+ if (shouldRefreshHead(state)) {
384
+ const maybeNewHead = await config.fetchCursor({ blockTag: "latest" });
385
+ if (maybeNewHead === null) {
386
+ throw new Error("Failed to fetch the latest block");
387
+ }
388
+ const result = await chainTracker.updateHead({
389
+ newHead: maybeNewHead,
390
+ fetchCursorByHash: (blockHash) => config.fetchCursor({ blockHash }),
391
+ fetchCursorRange: (args) => config.fetchCursorRange(args)
392
+ });
393
+ state.lastHeadRefresh = Date.now();
394
+ if (result.status === "reorg") {
395
+ const { cursor: cursor2 } = result;
396
+ if (cursor2.orderKey < state.cursor.orderKey) {
397
+ state.cursor = cursor2;
398
+ yield {
399
+ _tag: "invalidate",
400
+ invalidate: { cursor: cursor2 }
401
+ };
402
+ return;
403
+ }
404
+ }
386
405
  }
387
- const { data, blockInfo } = result;
388
- state.cursor = {
389
- orderKey: blockInfo.blockNumber,
390
- uniqueKey: blockInfo.blockHash
391
- };
392
- const { status: headUpdateStatus } = state.chainTracker.addToCanonicalChain({
393
- blockInfo
406
+ const head = chainTracker.head();
407
+ const filterData = await config.fetchBlockRange({
408
+ startBlock: cursor.orderKey + 1n,
409
+ maxBlock: head.orderKey,
410
+ force: false,
411
+ filter
394
412
  });
395
- if (headUpdateStatus !== "success") {
396
- throw new Error("Failed to update head. Would cause reorg.");
397
- }
398
- if (data.block !== null) {
399
- state.lastHeartbeat = Date.now();
400
- const production = isAtHead(state) ? "live" : "backfill";
401
- yield {
402
- _tag: "data",
403
- data: {
404
- cursor: data.cursor,
405
- endCursor: data.endCursor,
406
- data: [data.block],
407
- finality: "accepted",
408
- production
409
- }
413
+ for (const { cursor: cursor2, endCursor, block } of filterData.data) {
414
+ if (!chainTracker.isCanonical(endCursor)) {
415
+ throw new Error("Trying to process non-canonical block");
416
+ }
417
+ if (block !== null) {
418
+ state.lastHeartbeat = Date.now();
419
+ const production = isAtHead(state) ? "live" : "backfill";
420
+ yield {
421
+ _tag: "data",
422
+ data: {
423
+ cursor: cursor2,
424
+ endCursor,
425
+ data: [block],
426
+ finality: "accepted",
427
+ production
428
+ }
429
+ };
430
+ }
431
+ state.cursor = {
432
+ orderKey: endCursor.orderKey,
433
+ uniqueKey: endCursor.uniqueKey
410
434
  };
411
435
  }
412
436
  }
@@ -425,7 +449,8 @@ async function* waitForHeadChange(state) {
425
449
  }
426
450
  const result = await chainTracker.updateHead({
427
451
  newHead: maybeNewHead,
428
- fetchCursorByHash: (blockHash) => config.fetchCursor({ blockHash })
452
+ fetchCursorByHash: (blockHash) => config.fetchCursor({ blockHash }),
453
+ fetchCursorRange: (args) => config.fetchCursorRange(args)
429
454
  });
430
455
  switch (result.status) {
431
456
  case "unchanged": {
@@ -478,6 +503,11 @@ function shouldRefreshFinalized(state) {
478
503
  const now = Date.now();
479
504
  return now - lastFinalizedRefresh >= config.finalizedRefreshIntervalMs();
480
505
  }
506
+ function shouldRefreshHead(state) {
507
+ const { lastHeadRefresh, config } = state;
508
+ const now = Date.now();
509
+ return now - lastHeadRefresh >= config.headRefreshIntervalMs();
510
+ }
481
511
  function isAtHead(state) {
482
512
  const head = state.chainTracker.head();
483
513
  return state.cursor.orderKey === head.orderKey;
@@ -527,4 +557,4 @@ const index = {
527
557
  };
528
558
 
529
559
  export { RpcStreamConfig as R, RpcClient as a, RpcDataStream as b, createRpcClient as c, index as i };
530
- //# sourceMappingURL=protocol.bde61588.mjs.map
560
+ //# sourceMappingURL=protocol.05fab89c.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.05fab89c.mjs","sources":["../../src/rpc/config.ts","../../src/rpc/helpers.ts","../../src/rpc/chain-tracker.ts","../../src/rpc/data-stream.ts","../../src/rpc/client.ts"],"sourcesContent":["import type { Bytes, Cursor } from \"../common\";\n\nexport type FetchBlockRangeArgs<TFilter> = {\n startBlock: bigint;\n maxBlock: bigint;\n force: boolean;\n filter: TFilter;\n};\n\nexport type FetchBlockRangeResult<TBlock> = {\n startBlock: bigint;\n endBlock: bigint;\n data: FetchBlockResult<TBlock>[];\n};\n\nexport type FetchBlockResult<TBlock> = {\n block: TBlock | null;\n cursor: Cursor | undefined;\n endCursor: Cursor;\n};\n\nexport type BlockInfo = {\n blockNumber: bigint;\n blockHash: Bytes;\n parentBlockHash: Bytes;\n};\n\nexport type FetchBlockByNumberArgs<TFilter> = {\n blockNumber: bigint;\n expectedParentBlockHash: Bytes;\n isAtHead: boolean;\n filter: TFilter;\n};\n\nexport type FetchBlockByNumberResult<TBlock> =\n | {\n status: \"success\";\n data: FetchBlockResult<TBlock>;\n blockInfo: BlockInfo;\n }\n | {\n status: \"reorg\";\n blockInfo: BlockInfo;\n };\n\nexport type FetchCursorRangeArgs = {\n startBlockNumber: bigint;\n endBlockNumber: bigint;\n};\n\nexport type FetchCursorArgs =\n | {\n blockTag: \"latest\" | \"finalized\";\n blockNumber?: undefined;\n blockHash?: undefined;\n }\n | {\n blockTag?: undefined;\n blockNumber: bigint;\n blockHash?: undefined;\n }\n | {\n blockTag?: undefined;\n blockNumber?: undefined;\n blockHash: Bytes;\n };\n\nexport type ValidateFilterResult =\n | {\n valid: true;\n error?: undefined;\n }\n | {\n valid: false;\n error: string;\n };\n\nexport abstract class RpcStreamConfig<TFilter, TBlock> {\n abstract headRefreshIntervalMs(): number;\n abstract finalizedRefreshIntervalMs(): number;\n\n abstract fetchCursorRange(args: FetchCursorRangeArgs): Promise<BlockInfo[]>;\n abstract fetchCursor(args: FetchCursorArgs): Promise<BlockInfo | null>;\n\n abstract validateFilter(filter: TFilter): ValidateFilterResult;\n\n abstract fetchBlockRange(\n args: FetchBlockRangeArgs<TFilter>,\n ): Promise<FetchBlockRangeResult<TBlock>>;\n\n abstract fetchBlockByNumber(\n args: FetchBlockByNumberArgs<TFilter>,\n ): Promise<FetchBlockByNumberResult<TBlock>>;\n}\n","import type { Cursor } from \"../common\";\nimport type { BlockInfo } from \"./config\";\n\nexport function blockInfoToCursor(blockInfo: BlockInfo): Cursor {\n return {\n orderKey: blockInfo.blockNumber,\n uniqueKey: blockInfo.blockHash,\n };\n}\n","import { fromHex } from \"viem\";\nimport type { Bytes, Cursor } from \"../common\";\nimport type { BlockInfo, FetchCursorRangeArgs } from \"./config\";\nimport { blockInfoToCursor } from \"./helpers\";\n\ntype UpdateHeadArgs = {\n newHead: BlockInfo;\n fetchCursorByHash: (hash: Bytes) => Promise<BlockInfo | null>;\n fetchCursorRange: (args: FetchCursorRangeArgs) => Promise<BlockInfo[]>;\n};\n\ntype UpdateHeadResult =\n | {\n status: \"unchanged\";\n }\n | {\n status: \"success\";\n }\n | {\n status: \"reorg\";\n cursor: Cursor;\n };\n\nexport class ChainTracker {\n #finalized: BlockInfo;\n #head: BlockInfo;\n #canonical: Map<bigint, BlockInfo>;\n #batchSize: bigint;\n\n constructor({\n head,\n finalized,\n batchSize,\n }: {\n finalized: BlockInfo;\n head: BlockInfo;\n batchSize: bigint;\n }) {\n this.#finalized = finalized;\n this.#head = head;\n this.#batchSize = batchSize;\n\n this.#canonical = new Map([\n [finalized.blockNumber, finalized],\n [head.blockNumber, head],\n ]);\n }\n\n head(): Cursor {\n return blockInfoToCursor(this.#head);\n }\n\n finalized(): Cursor {\n return blockInfoToCursor(this.#finalized);\n }\n\n updateFinalized(newFinalized: BlockInfo) {\n // console.debug(\n // `updateFinalized: new=${newFinalized.blockNumber} old=${this.#finalized.blockNumber}`,\n // );\n\n if (newFinalized.blockNumber < this.#finalized.blockNumber) {\n throw new Error(\"Finalized cursor moved backwards\");\n }\n\n if (newFinalized.blockNumber === this.#finalized.blockNumber) {\n if (newFinalized.blockHash !== this.#finalized.blockHash) {\n throw new Error(\"Received a different finalized cursor\");\n }\n\n return false;\n }\n\n // Delete all blocks that are now finalized.\n for (\n let bn = this.#finalized.blockNumber;\n bn < newFinalized.blockNumber;\n bn++\n ) {\n this.#canonical.delete(bn);\n }\n\n this.#canonical.set(newFinalized.blockNumber, newFinalized);\n this.#finalized = newFinalized;\n\n return true;\n }\n\n addToCanonicalChain({ blockInfo }: { blockInfo: BlockInfo }) {\n // console.debug(`addToCanonicalChain: block=${blockInfo.blockNumber}`);\n\n const existing = this.#canonical.get(blockInfo.blockNumber);\n\n if (existing) {\n if (existing.blockHash !== blockInfo.blockHash) {\n throw new Error(\n `Block already exists in canonical chain: previous ${existing.blockHash}, new ${blockInfo.blockHash}`,\n );\n }\n }\n\n const parent = this.#canonical.get(blockInfo.blockNumber - 1n);\n if (!parent) {\n throw new Error(\"Parent block not in canonical chain\");\n }\n\n if (parent.blockHash !== blockInfo.parentBlockHash) {\n throw new Error(\"Parent block hash mismatch.\");\n }\n\n this.#canonical.set(blockInfo.blockNumber, blockInfo);\n\n // console.log(\"Canon updated: \", canonical);\n\n return { status: \"success\" };\n }\n\n async updateHead({\n newHead,\n fetchCursorByHash,\n fetchCursorRange,\n }: UpdateHeadArgs): Promise<UpdateHeadResult> {\n // console.debug(\n // `updateHead: new=${newHead.blockNumber} old=${this.#head.blockNumber}`,\n // );\n\n // No changes to the chain.\n if (\n newHead.blockNumber === this.#head.blockNumber &&\n newHead.blockHash === this.#head.blockHash\n ) {\n return { status: \"unchanged\" };\n }\n\n // Most common case: the new head is the block after the current head.\n if (\n newHead.blockNumber === this.#head.blockNumber + 1n &&\n newHead.parentBlockHash === this.#head.blockHash\n ) {\n this.#canonical.set(newHead.blockNumber, newHead);\n this.#head = newHead;\n return { status: \"success\" };\n }\n\n // The new chain is not longer.\n if (newHead.blockNumber <= this.#head.blockNumber) {\n // console.log(\"head=\", this.#head, \"newhead=\", newHead);\n let currentNewHead = newHead;\n // Delete all blocks from canonical chain after the new head.\n for (\n let bn = newHead.blockNumber + 1n;\n bn <= this.#head.blockNumber;\n bn++\n ) {\n this.#canonical.delete(bn);\n }\n\n // Check if the chain was simply shrunk to this block.\n const existing = this.#canonical.get(currentNewHead.blockNumber);\n if (existing && existing.blockHash === currentNewHead.blockHash) {\n this.#head = existing;\n return {\n status: \"reorg\",\n cursor: blockInfoToCursor(existing),\n };\n }\n\n while (currentNewHead.blockNumber > this.#finalized.blockNumber) {\n this.#canonical.delete(currentNewHead.blockNumber);\n\n const canonicalParent = this.#canonical.get(\n currentNewHead.blockNumber - 1n,\n );\n\n if (!canonicalParent) {\n throw new Error(\n \"Cannot reconcile new head with canonical chain: missing parent in canonical chain\",\n );\n }\n\n // We found the common ancestor.\n if (canonicalParent.blockHash === currentNewHead.parentBlockHash) {\n this.#head = canonicalParent;\n return {\n status: \"reorg\",\n cursor: blockInfoToCursor(canonicalParent),\n };\n }\n\n const parent = await fetchCursorByHash(currentNewHead.parentBlockHash);\n\n if (!parent) {\n throw new Error(\n \"Cannot reconcile new head with canonical chain: failed to fetch parent\",\n );\n }\n\n currentNewHead = parent;\n }\n\n throw new Error(\"Cannot reconcile new head with canonical chain.\");\n }\n\n // In all other cases we need to \"join\" the new head with the existing chain.\n // The new chain is longer and we need the missing blocks.\n // This may result in reorgs.\n\n // console.log(\n // `Moving from ${this.#head.blockNumber} to ${newHead.blockNumber} (${newHead.blockNumber - this.#head.blockNumber} blocks)`,\n // );\n\n let currentBlockNumber = this.#head.blockNumber + 1n;\n\n while (true) {\n let endBlockNumber = currentBlockNumber + this.#batchSize;\n if (endBlockNumber > newHead.blockNumber) {\n endBlockNumber = newHead.blockNumber;\n }\n\n const missing = await fetchCursorRange({\n startBlockNumber: currentBlockNumber,\n endBlockNumber,\n });\n\n for (const block of missing) {\n const canonicalParent = this.#canonical.get(block.blockNumber - 1n);\n if (\n !canonicalParent ||\n canonicalParent.blockHash !== block.parentBlockHash\n ) {\n throw new Error(\n \"Chain reorganization detected. Recovery not implemented\",\n );\n }\n\n this.#canonical.set(block.blockNumber, block);\n\n // console.log(`Applied block ${block.blockNumber}`);\n }\n\n if (endBlockNumber === newHead.blockNumber) {\n break;\n }\n\n currentBlockNumber = endBlockNumber + 1n;\n }\n\n /*\n while (true) {\n const parent = await fetchCursorByHash(current.parentBlockHash);\n\n if (!parent) {\n throw new Error(\n \"Cannot reconcile new head with canonical chain: failed to fetch parent\",\n );\n }\n\n if (parent.blockNumber === this.#head.blockNumber) {\n if (parent.blockHash === this.#head.blockHash) {\n break;\n }\n\n const headParent = this.#canonical.get(this.#head.blockNumber - 1n);\n if (!headParent) {\n throw new Error(\n \"Cannot reconcile new head with canonical chain: missing parent in canonical chain\",\n );\n }\n\n // Update current head.\n this.#canonical.delete(this.#head.blockNumber);\n this.#head = headParent;\n reorgDetected = true;\n }\n\n blocksToApply.push(parent);\n current = parent;\n }\n */\n // throw new Error(\"FUCK IT\");\n\n // for (const block of blocksToApply.reverse()) {\n // this.#canonical.set(block.blockNumber, block);\n // }\n\n // const previousHead = this.#head;\n this.#head = newHead;\n\n // if (reorgDetected) {\n // return {\n // status: \"reorg\",\n // cursor: blockInfoToCursor(previousHead),\n // };\n // }\n\n return { status: \"success\" };\n }\n\n isCanonical({ orderKey, uniqueKey }: Cursor) {\n if (!uniqueKey) {\n return true;\n }\n\n const block = this.#canonical.get(orderKey);\n if (!block) {\n return true;\n }\n\n return block.blockHash === uniqueKey;\n }\n\n async initializeStartingCursor({\n cursor,\n fetchCursor,\n }: {\n cursor: Cursor;\n fetchCursor: (blockNumber: bigint) => Promise<BlockInfo | null>;\n }): Promise<\n | { canonical: true; reason?: undefined; fullCursor: Cursor }\n | { canonical: false; reason: string; fullCursor?: undefined }\n > {\n const head = this.head();\n const finalized = this.finalized();\n\n if (cursor.orderKey > head.orderKey) {\n return { canonical: false, reason: \"cursor is ahead of head\" };\n }\n\n const expectedInfo = await fetchCursor(cursor.orderKey);\n if (!cursor.uniqueKey) {\n if (expectedInfo === null) {\n throw new Error(\"Failed to initialize canonical cursor\");\n }\n\n if (expectedInfo.blockNumber > finalized.orderKey) {\n this.#canonical.set(expectedInfo.blockNumber, expectedInfo);\n }\n\n return { canonical: true, fullCursor: blockInfoToCursor(expectedInfo) };\n }\n\n if (expectedInfo === null) {\n return {\n canonical: false,\n reason: \"expected block does not exist\",\n };\n }\n\n const expectedCursor = blockInfoToCursor(expectedInfo);\n\n // These two checks are redundant, but they are kept to avoid issues with bad config implementations.\n if (!expectedCursor.uniqueKey) {\n return {\n canonical: false,\n reason: \"expected cursor has no unique key (hash)\",\n };\n }\n\n if (expectedCursor.orderKey !== cursor.orderKey) {\n return {\n canonical: false,\n reason: \"cursor order key does not match expected order key\",\n };\n }\n\n if (\n fromHex(expectedCursor.uniqueKey, \"bigint\") !==\n fromHex(cursor.uniqueKey, \"bigint\")\n ) {\n return {\n canonical: false,\n reason: `cursor hash does not match expected hash: ${cursor.uniqueKey} !== ${expectedCursor.uniqueKey}`,\n };\n }\n\n if (expectedInfo.blockNumber > finalized.orderKey) {\n this.#canonical.set(expectedInfo.blockNumber, expectedInfo);\n }\n\n return { canonical: true, fullCursor: expectedCursor };\n }\n}\n\nexport function createChainTracker({\n head,\n finalized,\n batchSize,\n}: {\n head: BlockInfo;\n finalized: BlockInfo;\n batchSize: bigint;\n}): ChainTracker {\n return new ChainTracker({ finalized, head, batchSize });\n}\n","import type { StreamDataOptions } from \"../client\";\nimport type { Cursor } from \"../common\";\nimport type { StreamDataRequest, StreamDataResponse } from \"../stream\";\nimport { type ChainTracker, createChainTracker } from \"./chain-tracker\";\nimport type { RpcStreamConfig } from \"./config\";\nimport { blockInfoToCursor } from \"./helpers\";\n\nconst DEFAULT_HEARTBEAT_INTERVAL_MS = 30_000;\n\ntype State<TFilter, TBlock> = {\n // The network-specific config.\n config: RpcStreamConfig<TFilter, TBlock>;\n // The current cursor, that is the last block that was filtered.\n cursor: Cursor;\n // When the finalized block was last refreshed.\n lastFinalizedRefresh: number;\n // When the head was last refreshed.\n lastHeadRefresh: number;\n // When the last heartbeat was sent.\n lastHeartbeat: number;\n // When the last backfill message was sent.\n lastBackfillMessage: number;\n // Track the chain's state.\n chainTracker: ChainTracker;\n // Heartbeat interval in milliseconds.\n heartbeatIntervalMs: number;\n // The request filter.\n filter: TFilter;\n // The request options.\n options?: StreamDataOptions;\n};\n\nexport class RpcDataStream<TFilter, TBlock> {\n private heartbeatIntervalMs: number;\n\n constructor(\n private config: RpcStreamConfig<TFilter, TBlock>,\n private request: StreamDataRequest<TFilter>,\n private options?: StreamDataOptions,\n ) {\n this.heartbeatIntervalMs = request.heartbeatInterval\n ? Number(request.heartbeatInterval.seconds) * 1000\n : DEFAULT_HEARTBEAT_INTERVAL_MS;\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<StreamDataResponse<TBlock>> {\n const startingState = await this.initialize();\n yield* dataStreamLoop(startingState);\n }\n\n private async initialize(): Promise<State<TFilter, TBlock>> {\n if (this.request.filter.length === 0) {\n throw new Error(\"Request.filter: empty.\");\n }\n\n if (this.request.filter.length > 1) {\n throw new Error(\"Request.filter: only one filter is supported.\");\n }\n\n const [head, finalized] = await Promise.all([\n this.config.fetchCursor({ blockTag: \"latest\" }),\n this.config.fetchCursor({ blockTag: \"finalized\" }),\n ]);\n\n if (finalized === null) {\n throw new Error(\"EvmRpcStream requires a finalized block\");\n }\n\n if (head === null) {\n throw new Error(\"EvmRpcStream requires a chain with blocks.\");\n }\n\n const chainTracker = createChainTracker({\n head,\n finalized,\n batchSize: 20n,\n });\n\n let cursor: Cursor;\n if (this.request.startingCursor) {\n cursor = this.request.startingCursor;\n\n const { canonical, reason, fullCursor } =\n await chainTracker.initializeStartingCursor({\n cursor,\n fetchCursor: (blockNumber) =>\n this.config.fetchCursor({ blockNumber }),\n });\n\n if (!canonical) {\n throw new Error(`Starting cursor is not canonical: ${reason}`);\n }\n\n cursor = fullCursor;\n } else {\n cursor = { orderKey: -1n };\n }\n\n return {\n cursor,\n lastHeartbeat: Date.now(),\n lastFinalizedRefresh: Date.now(),\n lastHeadRefresh: Date.now(),\n lastBackfillMessage: Date.now(),\n chainTracker,\n config: this.config,\n heartbeatIntervalMs: this.heartbeatIntervalMs,\n filter: this.request.filter[0],\n options: this.options,\n };\n }\n}\n\nasync function* dataStreamLoop<TFilter, TBlock>(\n state: State<TFilter, TBlock>,\n): AsyncGenerator<StreamDataResponse<TBlock>> {\n while (shouldContinue(state)) {\n const { cursor, chainTracker } = state;\n\n // Always check for heartbeats first to ensure we don't miss any.\n if (shouldSendHeartbeat(state)) {\n state.lastHeartbeat = Date.now();\n yield { _tag: \"heartbeat\" };\n }\n\n if (shouldRefreshFinalized(state)) {\n const finalizedInfo = await state.config.fetchCursor({\n blockTag: \"finalized\",\n });\n\n if (finalizedInfo === null) {\n throw new Error(\"Failed to fetch finalized cursor\");\n }\n\n const finalized = blockInfoToCursor(finalizedInfo);\n const finalizedChanged =\n state.chainTracker.updateFinalized(finalizedInfo);\n\n // Only send finalized if it's needed.\n if (finalizedChanged && state.cursor.orderKey > finalized.orderKey) {\n yield { _tag: \"finalize\", finalize: { cursor: finalized } };\n }\n\n state.lastFinalizedRefresh = Date.now();\n }\n\n const finalized = chainTracker.finalized();\n\n // console.debug(\n // `RpcLoop: c=${cursor.orderKey} f=${finalized.orderKey} h=${chainTracker.head().orderKey}`,\n // );\n\n if (cursor.orderKey < finalized.orderKey) {\n yield* backfillFinalizedBlocks(state);\n } else {\n // If we're at the head, wait for a change.\n //\n // We don't want to produce a block immediately, but re-run the loop so\n // that it's like any other iteration.\n if (isAtHead(state)) {\n yield* waitForHeadChange(state);\n } else {\n yield* produceLiveBlocks(state);\n }\n }\n }\n}\n\nasync function* backfillFinalizedBlocks<TFilter, TBlock>(\n state: State<TFilter, TBlock>,\n): AsyncGenerator<StreamDataResponse<TBlock>> {\n const { cursor, chainTracker, config, filter } = state;\n const finalized = chainTracker.finalized();\n\n // While backfilling we want to regularly send some blocks (even if empty) so\n // that the client can store the cursor.\n const force = shouldForceBackfill(state);\n\n const filterData = await config.fetchBlockRange({\n startBlock: cursor.orderKey + 1n,\n maxBlock: finalized.orderKey,\n force,\n filter,\n });\n\n if (filterData.endBlock > finalized.orderKey) {\n throw new Error(\n \"Network-specific stream returned invalid data, crossing the finalized block.\",\n );\n }\n\n for (const data of filterData.data) {\n state.lastHeartbeat = Date.now();\n state.lastBackfillMessage = Date.now();\n yield {\n _tag: \"data\",\n data: {\n cursor: data.cursor,\n endCursor: data.endCursor,\n data: [data.block],\n finality: \"finalized\",\n production: \"backfill\",\n },\n };\n }\n\n // Notice that we check that filteredData.endBlock <= finalized.orderKey above.\n if (filterData.endBlock === finalized.orderKey) {\n // Prepare for transition to non-finalized data.\n state.cursor = finalized;\n } else {\n state.cursor = { orderKey: filterData.endBlock };\n }\n}\n\n// This is a generator to possibly produce data for live blocks.\n//\n// It's a generator because it's not guaranteed to produce data for the next block.\nasync function* produceLiveBlocks<TFilter, TBlock>(\n state: State<TFilter, TBlock>,\n): AsyncGenerator<StreamDataResponse<TBlock>> {\n const { config, cursor, chainTracker, filter } = state;\n\n if (shouldRefreshHead(state)) {\n const maybeNewHead = await config.fetchCursor({ blockTag: \"latest\" });\n if (maybeNewHead === null) {\n throw new Error(\"Failed to fetch the latest block\");\n }\n\n const result = await chainTracker.updateHead({\n newHead: maybeNewHead,\n fetchCursorByHash: (blockHash) => config.fetchCursor({ blockHash }),\n fetchCursorRange: (args) => config.fetchCursorRange(args),\n });\n\n state.lastHeadRefresh = Date.now();\n\n if (result.status === \"reorg\") {\n const { cursor } = result;\n // Only handle reorgs if they involve blocks already processed.\n if (cursor.orderKey < state.cursor.orderKey) {\n state.cursor = cursor;\n\n yield {\n _tag: \"invalidate\",\n invalidate: { cursor },\n };\n\n return;\n }\n }\n }\n\n const head = chainTracker.head();\n\n const filterData = await config.fetchBlockRange({\n startBlock: cursor.orderKey + 1n,\n maxBlock: head.orderKey,\n force: false,\n filter,\n });\n\n for (const { cursor, endCursor, block } of filterData.data) {\n if (!chainTracker.isCanonical(endCursor)) {\n throw new Error(\"Trying to process non-canonical block\");\n }\n\n if (block !== null) {\n state.lastHeartbeat = Date.now();\n const production = isAtHead(state) ? \"live\" : \"backfill\";\n\n yield {\n _tag: \"data\",\n data: {\n cursor,\n endCursor,\n data: [block],\n finality: \"accepted\",\n production,\n },\n };\n }\n\n state.cursor = {\n orderKey: endCursor.orderKey,\n uniqueKey: endCursor.uniqueKey,\n };\n }\n}\n\nasync function* waitForHeadChange<TBlock>(\n state: State<unknown, TBlock>,\n): AsyncGenerator<StreamDataResponse<TBlock>> {\n const { chainTracker, config } = state;\n\n const heartbeatDeadline = state.lastHeartbeat + state.heartbeatIntervalMs;\n const finalizedRefreshDeadline =\n state.lastFinalizedRefresh + config.finalizedRefreshIntervalMs();\n\n while (true) {\n const now = Date.now();\n // Allow the outer loop to send the heartbeat message or refresh finalized blocks.\n if (now >= heartbeatDeadline || now >= finalizedRefreshDeadline) {\n return;\n }\n\n const maybeNewHead = await config.fetchCursor({ blockTag: \"latest\" });\n\n if (maybeNewHead === null) {\n throw new Error(\"Failed to fetch the latest block\");\n }\n\n const result = await chainTracker.updateHead({\n newHead: maybeNewHead,\n fetchCursorByHash: (blockHash) => config.fetchCursor({ blockHash }),\n fetchCursorRange: (args) => config.fetchCursorRange(args),\n });\n\n switch (result.status) {\n case \"unchanged\": {\n const heartbeatTimeout = heartbeatDeadline - now;\n const finalizedTimeout = finalizedRefreshDeadline - now;\n\n // Wait until whatever happens next.\n await sleep(\n Math.min(\n heartbeatTimeout,\n finalizedTimeout,\n config.headRefreshIntervalMs(),\n ),\n );\n\n break;\n }\n case \"reorg\": {\n const { cursor } = result;\n // Only handle reorgs if they involve blocks already processed.\n if (cursor.orderKey < state.cursor.orderKey) {\n state.cursor = cursor;\n\n yield {\n _tag: \"invalidate\",\n invalidate: { cursor },\n };\n }\n\n break;\n }\n case \"success\": {\n // Chain grew without any issues. Go back to the top-level loop to produce data.\n return;\n }\n }\n }\n}\n\nfunction shouldSendHeartbeat(state: State<unknown, unknown>): boolean {\n const { heartbeatIntervalMs, lastHeartbeat } = state;\n const now = Date.now();\n return now - lastHeartbeat >= heartbeatIntervalMs;\n}\n\nfunction shouldForceBackfill(state: State<unknown, unknown>): boolean {\n const { lastBackfillMessage, heartbeatIntervalMs } = state;\n const now = Date.now();\n return now - lastBackfillMessage >= heartbeatIntervalMs;\n}\n\nfunction shouldContinue(state: State<unknown, unknown>): boolean {\n const { endingCursor } = state.options || {};\n if (endingCursor === undefined) return true;\n\n return state.cursor.orderKey < endingCursor.orderKey;\n}\n\nfunction shouldRefreshFinalized(state: State<unknown, unknown>): boolean {\n const { lastFinalizedRefresh, config } = state;\n const now = Date.now();\n return now - lastFinalizedRefresh >= config.finalizedRefreshIntervalMs();\n}\n\nfunction shouldRefreshHead(state: State<unknown, unknown>): boolean {\n const { lastHeadRefresh, config } = state;\n const now = Date.now();\n return now - lastHeadRefresh >= config.headRefreshIntervalMs();\n}\n\nfunction isAtHead(state: State<unknown, unknown>): boolean {\n const head = state.chainTracker.head();\n return state.cursor.orderKey === head.orderKey;\n}\n\nfunction sleep(duration: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, duration));\n}\n","import type { Client, ClientCallOptions, StreamDataOptions } from \"../client\";\nimport type { Cursor } from \"../common\";\nimport type { StatusRequest, StatusResponse } from \"../status\";\nimport type { StreamDataRequest, StreamDataResponse } from \"../stream\";\nimport type { RpcStreamConfig } from \"./config\";\nimport { RpcDataStream } from \"./data-stream\";\nimport { blockInfoToCursor } from \"./helpers\";\n\nexport class RpcClient<TFilter, TBlock> implements Client<TFilter, TBlock> {\n constructor(private config: RpcStreamConfig<TFilter, TBlock>) {}\n\n async status(\n _request?: StatusRequest,\n _options?: ClientCallOptions,\n ): Promise<StatusResponse> {\n const [currentHead, finalized] = await Promise.all([\n this.config.fetchCursor({ blockTag: \"latest\" }),\n this.config.fetchCursor({ blockTag: \"finalized\" }),\n ]);\n\n const starting: Cursor = { orderKey: 0n };\n\n return {\n currentHead: currentHead ? blockInfoToCursor(currentHead) : undefined,\n lastIngested: currentHead ? blockInfoToCursor(currentHead) : undefined,\n finalized: finalized ? blockInfoToCursor(finalized) : undefined,\n starting,\n };\n }\n\n streamData(\n request: StreamDataRequest<TFilter>,\n options?: StreamDataOptions,\n ): AsyncIterable<StreamDataResponse<TBlock>> {\n const index = 0;\n for (const filter of request.filter) {\n const { valid, error } = this.config.validateFilter(filter);\n if (!valid) {\n throw new Error(`Filter at position ${index} is invalid: ${error}`);\n }\n }\n\n return new RpcDataStream(this.config, request, options);\n }\n}\n\nexport function createRpcClient<TFilter, TBlock>(\n config: RpcStreamConfig<TFilter, TBlock>,\n): RpcClient<TFilter, TBlock> {\n return new RpcClient(config);\n}\n"],"names":["finalized","cursor"],"mappings":";;AA6EO,MAAe,eAAiC,CAAA;AAgBvD;;AC1FO,SAAS,kBAAkB,SAA8B,EAAA;AAC9D,EAAO,OAAA;AAAA,IACL,UAAU,SAAU,CAAA,WAAA;AAAA,IACpB,WAAW,SAAU,CAAA,SAAA;A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