@aztec/world-state 0.0.0-test.0 → 0.0.1-commit.001888fc

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.
Files changed (65) hide show
  1. package/dest/index.d.ts +1 -1
  2. package/dest/instrumentation/instrumentation.d.ts +6 -4
  3. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  4. package/dest/instrumentation/instrumentation.js +25 -41
  5. package/dest/native/bench_metrics.d.ts +23 -0
  6. package/dest/native/bench_metrics.d.ts.map +1 -0
  7. package/dest/native/bench_metrics.js +81 -0
  8. package/dest/native/fork_checkpoint.d.ts +7 -1
  9. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  10. package/dest/native/fork_checkpoint.js +15 -3
  11. package/dest/native/index.d.ts +1 -1
  12. package/dest/native/merkle_trees_facade.d.ts +20 -8
  13. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  14. package/dest/native/merkle_trees_facade.js +80 -15
  15. package/dest/native/message.d.ts +83 -53
  16. package/dest/native/message.d.ts.map +1 -1
  17. package/dest/native/message.js +61 -61
  18. package/dest/native/native_world_state.d.ts +27 -19
  19. package/dest/native/native_world_state.d.ts.map +1 -1
  20. package/dest/native/native_world_state.js +103 -41
  21. package/dest/native/native_world_state_instance.d.ts +20 -4
  22. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  23. package/dest/native/native_world_state_instance.js +43 -4
  24. package/dest/native/world_state_ops_queue.d.ts +1 -1
  25. package/dest/native/world_state_ops_queue.d.ts.map +1 -1
  26. package/dest/native/world_state_ops_queue.js +1 -1
  27. package/dest/synchronizer/config.d.ts +14 -6
  28. package/dest/synchronizer/config.d.ts.map +1 -1
  29. package/dest/synchronizer/config.js +33 -10
  30. package/dest/synchronizer/errors.d.ts +4 -0
  31. package/dest/synchronizer/errors.d.ts.map +1 -0
  32. package/dest/synchronizer/errors.js +5 -0
  33. package/dest/synchronizer/factory.d.ts +12 -4
  34. package/dest/synchronizer/factory.d.ts.map +1 -1
  35. package/dest/synchronizer/factory.js +13 -8
  36. package/dest/synchronizer/index.d.ts +1 -1
  37. package/dest/synchronizer/server_world_state_synchronizer.d.ts +21 -31
  38. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  39. package/dest/synchronizer/server_world_state_synchronizer.js +191 -96
  40. package/dest/test/index.d.ts +1 -1
  41. package/dest/test/utils.d.ts +12 -5
  42. package/dest/test/utils.d.ts.map +1 -1
  43. package/dest/test/utils.js +54 -47
  44. package/dest/testing.d.ts +3 -3
  45. package/dest/testing.d.ts.map +1 -1
  46. package/dest/testing.js +7 -11
  47. package/dest/world-state-db/index.d.ts +1 -1
  48. package/dest/world-state-db/merkle_tree_db.d.ts +12 -18
  49. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  50. package/package.json +23 -24
  51. package/src/instrumentation/instrumentation.ts +31 -43
  52. package/src/native/bench_metrics.ts +91 -0
  53. package/src/native/fork_checkpoint.ts +19 -3
  54. package/src/native/merkle_trees_facade.ts +92 -20
  55. package/src/native/message.ts +105 -75
  56. package/src/native/native_world_state.ts +132 -52
  57. package/src/native/native_world_state_instance.ts +63 -10
  58. package/src/native/world_state_ops_queue.ts +1 -1
  59. package/src/synchronizer/config.ts +55 -21
  60. package/src/synchronizer/errors.ts +5 -0
  61. package/src/synchronizer/factory.ts +39 -10
  62. package/src/synchronizer/server_world_state_synchronizer.ts +227 -121
  63. package/src/test/utils.ts +92 -82
  64. package/src/testing.ts +4 -8
  65. package/src/world-state-db/merkle_tree_db.ts +16 -18
@@ -2,23 +2,28 @@ import { getTelemetryClient } from '@aztec/telemetry-client';
2
2
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
3
3
  import { NativeWorldStateService } from '../native/native_world_state.js';
4
4
  import { ServerWorldStateSynchronizer } from './server_world_state_synchronizer.js';
5
- export async function createWorldStateSynchronizer(config, l2BlockSource, prefilledPublicData = [], client = getTelemetryClient()) {
5
+ export async function createWorldStateSynchronizer(config, l2BlockSource, prefilledPublicData = [], client = getTelemetryClient(), bindings) {
6
6
  const instrumentation = new WorldStateInstrumentation(client);
7
- const merkleTrees = await createWorldState(config, prefilledPublicData, instrumentation);
7
+ const merkleTrees = await createWorldState(config, prefilledPublicData, instrumentation, bindings);
8
8
  return new ServerWorldStateSynchronizer(merkleTrees, l2BlockSource, config, instrumentation);
9
9
  }
10
- export async function createWorldState(config, prefilledPublicData = [], instrumentation = new WorldStateInstrumentation(getTelemetryClient())) {
11
- const newConfig = {
12
- dataDirectory: config.worldStateDataDirectory ?? config.dataDirectory,
13
- dataStoreMapSizeKB: config.worldStateDbMapSizeKb ?? config.dataStoreMapSizeKB
10
+ export async function createWorldState(config, prefilledPublicData = [], instrumentation = new WorldStateInstrumentation(getTelemetryClient()), bindings) {
11
+ const dataDirectory = config.worldStateDataDirectory ?? config.dataDirectory;
12
+ const dataStoreMapSizeKb = config.worldStateDbMapSizeKb ?? config.dataStoreMapSizeKb;
13
+ const wsTreeMapSizes = {
14
+ archiveTreeMapSizeKb: config.archiveTreeMapSizeKb ?? dataStoreMapSizeKb,
15
+ nullifierTreeMapSizeKb: config.nullifierTreeMapSizeKb ?? dataStoreMapSizeKb,
16
+ noteHashTreeMapSizeKb: config.noteHashTreeMapSizeKb ?? dataStoreMapSizeKb,
17
+ messageTreeMapSizeKb: config.messageTreeMapSizeKb ?? dataStoreMapSizeKb,
18
+ publicDataTreeMapSizeKb: config.publicDataTreeMapSizeKb ?? dataStoreMapSizeKb
14
19
  };
15
20
  if (!config.l1Contracts?.rollupAddress) {
16
21
  throw new Error('Rollup address is required to create a world state synchronizer.');
17
22
  }
18
23
  // If a data directory is provided in config, then create a persistent store.
19
- const merkleTrees = newConfig.dataDirectory ? await NativeWorldStateService.new(config.l1Contracts.rollupAddress, newConfig.dataDirectory, newConfig.dataStoreMapSizeKB, prefilledPublicData, instrumentation) : await NativeWorldStateService.tmp(config.l1Contracts.rollupAddress, ![
24
+ const merkleTrees = dataDirectory ? await NativeWorldStateService.new(config.l1Contracts.rollupAddress, dataDirectory, wsTreeMapSizes, prefilledPublicData, instrumentation, bindings) : await NativeWorldStateService.tmp(config.l1Contracts.rollupAddress, ![
20
25
  'true',
21
26
  '1'
22
- ].includes(process.env.DEBUG_WORLD_STATE), prefilledPublicData);
27
+ ].includes(process.env.DEBUG_WORLD_STATE), prefilledPublicData, instrumentation, bindings);
23
28
  return merkleTrees;
24
29
  }
@@ -1,3 +1,3 @@
1
1
  export * from './server_world_state_synchronizer.js';
2
2
  export * from './factory.js';
3
- //# sourceMappingURL=index.d.ts.map
3
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zeW5jaHJvbml6ZXIvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsY0FBYyxzQ0FBc0MsQ0FBQztBQUNyRCxjQUFjLGNBQWMsQ0FBQyJ9
@@ -1,13 +1,14 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- /// <reference types="node" resolution-mode="require"/>
3
- import type { Fr } from '@aztec/foundation/fields';
4
- import type { L2BlockSource, L2BlockStream, L2BlockStreamEvent, L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider, L2Tips } from '@aztec/stdlib/block';
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { type Logger } from '@aztec/foundation/log';
3
+ import { type BlockHash, type L2BlockSource, L2BlockStream, type L2BlockStreamEvent, type L2BlockStreamEventHandler, type L2BlockStreamLocalDataProvider, type L2Tips } from '@aztec/stdlib/block';
5
4
  import { type WorldStateSynchronizer, type WorldStateSynchronizerStatus } from '@aztec/stdlib/interfaces/server';
6
5
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
6
+ import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
7
7
  import { type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
8
8
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
9
9
  import type { MerkleTreeAdminDatabase } from '../world-state-db/merkle_tree_db.js';
10
10
  import type { WorldStateConfig } from './config.js';
11
+ export type { SnapshotDataKeys };
11
12
  /**
12
13
  * Synchronizes the world state with the L2 blocks from a L2BlockSource via a block stream.
13
14
  * The synchronizer will download the L2 blocks from the L2BlockSource and update the merkle trees.
@@ -23,42 +24,38 @@ export declare class ServerWorldStateSynchronizer implements WorldStateSynchroni
23
24
  private latestBlockNumberAtStart;
24
25
  private historyToKeep;
25
26
  private currentState;
26
- private latestBlockHashQuery;
27
27
  private syncPromise;
28
28
  protected blockStream: L2BlockStream | undefined;
29
- constructor(merkleTreeDb: MerkleTreeAdminDatabase, l2BlockSource: L2BlockSource & L1ToL2MessageSource, config: WorldStateConfig, instrumentation?: WorldStateInstrumentation, log?: import("@aztec/foundation/log").Logger);
29
+ private provenBlockNumber;
30
+ constructor(merkleTreeDb: MerkleTreeAdminDatabase, l2BlockSource: L2BlockSource & L1ToL2MessageSource, config: WorldStateConfig, instrumentation?: WorldStateInstrumentation, log?: Logger);
30
31
  getCommitted(): MerkleTreeReadOperations;
31
- getSnapshot(blockNumber: number): MerkleTreeReadOperations;
32
- fork(blockNumber?: number): Promise<MerkleTreeWriteOperations>;
32
+ getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations;
33
+ fork(blockNumber?: BlockNumber, opts?: {
34
+ closeDelayMs?: number;
35
+ }): Promise<MerkleTreeWriteOperations>;
36
+ backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>>;
37
+ clear(): Promise<void>;
33
38
  start(): Promise<void | import("@aztec/foundation/promise").PromiseWithResolvers<void>>;
34
39
  protected createBlockStream(): L2BlockStream;
35
40
  stop(): Promise<void>;
36
41
  status(): Promise<WorldStateSynchronizerStatus>;
37
- getLatestBlockNumber(): Promise<number>;
42
+ getLatestBlockNumber(): Promise<BlockNumber>;
43
+ stopSync(): Promise<void>;
44
+ resumeSync(): void;
38
45
  /**
39
46
  * Forces an immediate sync.
40
- * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it. Throws if cannot be reached.
47
+ * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
48
+ * @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
41
49
  * @returns A promise that resolves with the block number the world state was synced to
42
50
  */
43
- syncImmediate(targetBlockNumber?: number): Promise<number>;
51
+ syncImmediate(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber>;
44
52
  /** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */
45
- getL2BlockHash(number: number): Promise<string | undefined>;
53
+ getL2BlockHash(number: BlockNumber): Promise<string | undefined>;
46
54
  /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
47
55
  getL2Tips(): Promise<L2Tips>;
48
56
  /** Handles an event emitted by the block stream. */
49
57
  handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void>;
50
- /**
51
- * Handles a list of L2 blocks (i.e. Inserts the new note hashes into the merkle tree).
52
- * @param l2Blocks - The L2 blocks to handle.
53
- * @returns Whether the block handled was produced by this same node.
54
- */
55
58
  private handleL2Blocks;
56
- /**
57
- * Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
58
- * @param l2Block - The L2 block to handle.
59
- * @param l1ToL2Messages - The L1 to L2 messages for the block.
60
- * @returns Whether the block handled was produced by this same node.
61
- */
62
59
  private handleL2Block;
63
60
  private handleChainFinalized;
64
61
  private handleChainProven;
@@ -68,12 +65,5 @@ export declare class ServerWorldStateSynchronizer implements WorldStateSynchroni
68
65
  * @param newState - New state value.
69
66
  */
70
67
  private setCurrentState;
71
- /**
72
- * Verifies that the L1 to L2 messages hash to the block inHash.
73
- * @param l1ToL2Messages - The L1 to L2 messages for the block.
74
- * @param inHash - The inHash of the block.
75
- * @throws If the L1 to L2 messages do not hash to the block inHash.
76
- */
77
- protected verifyMessagesHashToInHash(l1ToL2Messages: Fr[], inHash: Buffer): Promise<void>;
78
68
  }
79
- //# sourceMappingURL=server_world_state_synchronizer.d.ts.map
69
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VydmVyX3dvcmxkX3N0YXRlX3N5bmNocm9uaXplci5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3N5bmNocm9uaXplci9zZXJ2ZXJfd29ybGRfc3RhdGVfc3luY2hyb25pemVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFBRSxXQUFXLEVBQW9CLE1BQU0saUNBQWlDLENBQUM7QUFFaEYsT0FBTyxFQUFFLEtBQUssTUFBTSxFQUFnQixNQUFNLHVCQUF1QixDQUFDO0FBR2xFLE9BQU8sRUFDTCxLQUFLLFNBQVMsRUFJZCxLQUFLLGFBQWEsRUFDbEIsYUFBYSxFQUNiLEtBQUssa0JBQWtCLEVBQ3ZCLEtBQUsseUJBQXlCLEVBQzlCLEtBQUssOEJBQThCLEVBQ25DLEtBQUssTUFBTSxFQUNaLE1BQU0scUJBQXFCLENBQUM7QUFDN0IsT0FBTyxFQUdMLEtBQUssc0JBQXNCLEVBQzNCLEtBQUssNEJBQTRCLEVBQ2xDLE1BQU0saUNBQWlDLENBQUM7QUFDekMsT0FBTyxLQUFLLEVBQUUsbUJBQW1CLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUNuRSxPQUFPLEtBQUssRUFBRSxnQkFBZ0IsRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBRWhFLE9BQU8sRUFBZ0IsS0FBSyx3QkFBd0IsRUFBRSxLQUFLLHlCQUF5QixFQUFFLE1BQU0scUJBQXFCLENBQUM7QUFHbEgsT0FBTyxFQUFFLHlCQUF5QixFQUFFLE1BQU0sd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0JBQWtCLEdBQUcsT0FBTyxDQUFDLElBQUksQ0FBQyxDQWU1RTtZQU9hLGNBQWM7WUF5Q2QsYUFBYTtZQWtCYixvQkFBb0I7SUF3Q2xDLE9BQU8sQ0FBQyxpQkFBaUI7WUFNWCxpQkFBaUI7SUFPL0I7OztPQUdHO0lBQ0gsT0FBTyxDQUFDLGVBQWU7Q0FJeEIifQ==
@@ -1 +1 @@
1
- {"version":3,"file":"server_world_state_synchronizer.d.ts","sourceRoot":"","sources":["../../src/synchronizer/server_world_state_synchronizer.ts"],"names":[],"mappings":";;AACA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAMnD,OAAO,KAAK,EAGV,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,yBAAyB,EACzB,8BAA8B,EAC9B,MAAM,EACP,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAGL,KAAK,sBAAsB,EAC3B,KAAK,4BAA4B,EAClC,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAEnE,OAAO,EAAgB,KAAK,wBAAwB,EAAE,KAAK,yBAAyB,EAAE,MAAM,qBAAqB,CAAC;AAGlH,OAAO,EAAE,yBAAyB,EAAE,MAAM,uCAAuC,CAAC;AAElF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,qCAAqC,CAAC;AACnF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;GAIG;AACH,qBAAa,4BACX,YAAW,sBAAsB,EAAE,8BAA8B,EAAE,yBAAyB;IAa1F,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,eAAe;IACvB,OAAO,CAAC,QAAQ,CAAC,GAAG;IAftB,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA2B;IAE/D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,oDAAoD;IACvC,sBAAsB,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAe5E;YAOa,cAAc;YAyCd,aAAa;YAkBb,oBAAoB;IAwClC,OAAO,CAAC,iBAAiB;YAMX,iBAAiB;IAO/B;;;OAGG;IACH,OAAO,CAAC,eAAe;CAIxB"}
@@ -1,13 +1,14 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
1
+ import { GENESIS_BLOCK_HEADER_HASH, INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
3
  import { createLogger } from '@aztec/foundation/log';
3
4
  import { promiseWithResolvers } from '@aztec/foundation/promise';
4
5
  import { elapsed } from '@aztec/foundation/timer';
5
- import { MerkleTreeCalculator } from '@aztec/foundation/trees';
6
- import { SHA256Trunc } from '@aztec/merkle-tree';
6
+ import { GENESIS_CHECKPOINT_HEADER_HASH, L2BlockStream } from '@aztec/stdlib/block';
7
7
  import { WorldStateRunningState } from '@aztec/stdlib/interfaces/server';
8
8
  import { MerkleTreeId } from '@aztec/stdlib/trees';
9
- import { TraceableL2BlockStream, getTelemetryClient } from '@aztec/telemetry-client';
9
+ import { getTelemetryClient } from '@aztec/telemetry-client';
10
10
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
11
+ import { WorldStateSynchronizerError } from './errors.js';
11
12
  /**
12
13
  * Synchronizes the world state with the L2 blocks from a L2BlockSource via a block stream.
13
14
  * The synchronizer will download the L2 blocks from the L2BlockSource and update the merkle trees.
@@ -22,21 +23,22 @@ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js
22
23
  latestBlockNumberAtStart;
23
24
  historyToKeep;
24
25
  currentState;
25
- latestBlockHashQuery;
26
26
  syncPromise;
27
27
  blockStream;
28
+ // WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
29
+ // store the proven block number here, in the synchronizer, so that we don't end up spamming the logs with 'chain-proved' events
30
+ provenBlockNumber;
28
31
  constructor(merkleTreeDb, l2BlockSource, config, instrumentation = new WorldStateInstrumentation(getTelemetryClient()), log = createLogger('world_state')){
29
32
  this.merkleTreeDb = merkleTreeDb;
30
33
  this.l2BlockSource = l2BlockSource;
31
34
  this.config = config;
32
35
  this.instrumentation = instrumentation;
33
36
  this.log = log;
34
- this.latestBlockNumberAtStart = 0;
37
+ this.latestBlockNumberAtStart = BlockNumber.ZERO;
35
38
  this.currentState = WorldStateRunningState.IDLE;
36
- this.latestBlockHashQuery = undefined;
37
39
  this.syncPromise = promiseWithResolvers();
38
40
  this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
39
- this.historyToKeep = config.worldStateBlockHistory < 1 ? undefined : config.worldStateBlockHistory;
41
+ this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
40
42
  this.log.info(`Created world state synchroniser with block history of ${this.historyToKeep === undefined ? 'infinity' : this.historyToKeep}`);
41
43
  }
42
44
  getCommitted() {
@@ -45,8 +47,14 @@ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js
45
47
  getSnapshot(blockNumber) {
46
48
  return this.merkleTreeDb.getSnapshot(blockNumber);
47
49
  }
48
- fork(blockNumber) {
49
- return this.merkleTreeDb.fork(blockNumber);
50
+ fork(blockNumber, opts) {
51
+ return this.merkleTreeDb.fork(blockNumber, opts);
52
+ }
53
+ backupTo(dstPath, compact) {
54
+ return this.merkleTreeDb.backupTo(dstPath, compact);
55
+ }
56
+ clear() {
57
+ return this.merkleTreeDb.clear();
50
58
  }
51
59
  async start() {
52
60
  if (this.currentState === WorldStateRunningState.STOPPED) {
@@ -56,7 +64,7 @@ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js
56
64
  return this.syncPromise;
57
65
  }
58
66
  // Get the current latest block number
59
- this.latestBlockNumberAtStart = await (this.config.worldStateProvenBlocksOnly ? this.l2BlockSource.getProvenBlockNumber() : this.l2BlockSource.getBlockNumber());
67
+ this.latestBlockNumberAtStart = BlockNumber(await this.l2BlockSource.getBlockNumber());
60
68
  const blockToDownloadFrom = await this.getLatestBlockNumber() + 1;
61
69
  if (blockToDownloadFrom <= this.latestBlockNumberAtStart) {
62
70
  // If there are blocks to be retrieved, go to a synching state
@@ -74,12 +82,11 @@ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js
74
82
  return this.syncPromise.promise;
75
83
  }
76
84
  createBlockStream() {
77
- const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
78
85
  const logger = createLogger('world-state:block_stream');
79
- return new TraceableL2BlockStream(this.l2BlockSource, this, this, tracer, 'WorldStateL2BlockStream', logger, {
80
- proven: this.config.worldStateProvenBlocksOnly,
86
+ return new L2BlockStream(this.l2BlockSource, this, this, logger, {
81
87
  pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
82
- batchSize: this.config.worldStateBlockRequestBatchSize
88
+ batchSize: this.config.worldStateBlockRequestBatchSize,
89
+ ignoreCheckpoints: true
83
90
  });
84
91
  }
85
92
  async stop() {
@@ -93,10 +100,10 @@ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js
93
100
  async status() {
94
101
  const summary = await this.merkleTreeDb.getStatusSummary();
95
102
  const status = {
96
- latestBlockNumber: Number(summary.unfinalisedBlockNumber),
97
- latestBlockHash: await this.getL2BlockHash(Number(summary.unfinalisedBlockNumber)) ?? '',
98
- finalisedBlockNumber: Number(summary.finalisedBlockNumber),
99
- oldestHistoricBlockNumber: Number(summary.oldestHistoricalBlock),
103
+ latestBlockNumber: summary.unfinalizedBlockNumber,
104
+ latestBlockHash: await this.getL2BlockHash(summary.unfinalizedBlockNumber) ?? '',
105
+ finalizedBlockNumber: summary.finalizedBlockNumber,
106
+ oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
100
107
  treesAreSynched: summary.treesAreSynched
101
108
  };
102
109
  return {
@@ -105,80 +112,159 @@ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js
105
112
  };
106
113
  }
107
114
  async getLatestBlockNumber() {
108
- return (await this.getL2Tips()).latest.number;
115
+ return (await this.getL2Tips()).proposed.number;
116
+ }
117
+ async stopSync() {
118
+ this.log.debug('Stopping sync...');
119
+ await this.blockStream?.stop();
120
+ this.log.info('Stopped sync');
121
+ }
122
+ resumeSync() {
123
+ if (!this.blockStream) {
124
+ throw new Error('Cannot resume sync as block stream is not initialized');
125
+ }
126
+ this.log.debug('Resuming sync...');
127
+ this.blockStream.start();
128
+ this.log.info('Resumed sync');
109
129
  }
110
130
  /**
111
131
  * Forces an immediate sync.
112
- * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it. Throws if cannot be reached.
132
+ * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
133
+ * @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
113
134
  * @returns A promise that resolves with the block number the world state was synced to
114
- */ async syncImmediate(targetBlockNumber) {
115
- if (this.currentState !== WorldStateRunningState.RUNNING || this.blockStream === undefined) {
135
+ */ async syncImmediate(targetBlockNumber, blockHash) {
136
+ if (this.currentState !== WorldStateRunningState.RUNNING) {
116
137
  throw new Error(`World State is not running. Unable to perform sync.`);
117
138
  }
139
+ if (this.blockStream === undefined) {
140
+ throw new Error('Block stream is not initialized. Unable to perform sync.');
141
+ }
118
142
  // If we have been given a block number to sync to and we have reached that number then return
119
143
  const currentBlockNumber = await this.getLatestBlockNumber();
120
144
  if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
121
- return currentBlockNumber;
145
+ if (blockHash === undefined) {
146
+ return currentBlockNumber;
147
+ }
148
+ // If a block hash was provided, verify we're on the expected fork
149
+ const currentHash = await this.getL2BlockHash(targetBlockNumber);
150
+ if (currentHash === blockHash.toString()) {
151
+ return currentBlockNumber;
152
+ }
153
+ // Hash mismatch: a reorg may have occurred, fall through to trigger sync
154
+ this.log.debug(`World state block hash mismatch at ${targetBlockNumber} (expected ${blockHash}, got ${currentHash}). Triggering resync.`);
122
155
  }
123
156
  this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
157
+ // If the archiver is behind the target block, force an archiver sync
158
+ if (targetBlockNumber) {
159
+ const archiverLatestBlock = BlockNumber(await this.l2BlockSource.getBlockNumber());
160
+ if (archiverLatestBlock < targetBlockNumber) {
161
+ this.log.debug(`Archiver is at ${archiverLatestBlock} behind target block ${targetBlockNumber}.`);
162
+ await this.l2BlockSource.syncImmediate();
163
+ }
164
+ }
124
165
  // Force the block stream to sync against the archiver now
125
166
  await this.blockStream.sync();
126
167
  // If we have been given a block number to sync to and we have not reached that number then fail
127
168
  const updatedBlockNumber = await this.getLatestBlockNumber();
128
169
  if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
129
- throw new Error(`Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`);
170
+ throw new WorldStateSynchronizerError(`Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`, {
171
+ cause: {
172
+ reason: 'block_not_available',
173
+ previousBlockNumber: currentBlockNumber,
174
+ updatedBlockNumber,
175
+ targetBlockNumber
176
+ }
177
+ });
178
+ }
179
+ // If a block hash was provided, verify we're on the expected fork after syncing, throw otherwise
180
+ if (blockHash !== undefined && targetBlockNumber !== undefined) {
181
+ const updatedHash = await this.getL2BlockHash(targetBlockNumber);
182
+ if (updatedHash !== blockHash.toString()) {
183
+ throw new WorldStateSynchronizerError(`Block hash mismatch at block ${targetBlockNumber} (expected ${blockHash} but got ${updatedHash})`, {
184
+ cause: {
185
+ reason: 'block_hash_mismatch',
186
+ targetBlockNumber,
187
+ expectedHash: blockHash.toString(),
188
+ actualHash: updatedHash
189
+ }
190
+ });
191
+ }
130
192
  }
131
193
  return updatedBlockNumber;
132
194
  }
133
195
  /** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */ async getL2BlockHash(number) {
134
- if (number === 0) {
196
+ if (number === BlockNumber.ZERO) {
135
197
  return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
136
198
  }
137
- if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
138
- this.latestBlockHashQuery = {
139
- hash: await this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then((leaf)=>leaf?.toString()),
140
- blockNumber: number
141
- };
142
- }
143
- return this.latestBlockHashQuery.hash;
199
+ return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then((leaf)=>leaf?.toString());
144
200
  }
145
201
  /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */ async getL2Tips() {
146
202
  const status = await this.merkleTreeDb.getStatusSummary();
147
- const unfinalisedBlockHash = await this.getL2BlockHash(Number(status.unfinalisedBlockNumber));
203
+ const unfinalizedBlockHashPromise = this.getL2BlockHash(status.unfinalizedBlockNumber);
204
+ const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
205
+ const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
206
+ const provenBlockHashPromise = this.provenBlockNumber === undefined ? finalizedBlockHashPromise : this.getL2BlockHash(this.provenBlockNumber);
207
+ const [unfinalizedBlockHash, finalizedBlockHash, provenBlockHash] = await Promise.all([
208
+ unfinalizedBlockHashPromise,
209
+ finalizedBlockHashPromise,
210
+ provenBlockHashPromise
211
+ ]);
148
212
  const latestBlockId = {
149
- number: Number(status.unfinalisedBlockNumber),
150
- hash: unfinalisedBlockHash
213
+ number: status.unfinalizedBlockNumber,
214
+ hash: unfinalizedBlockHash
151
215
  };
216
+ // World state doesn't track checkpointed blocks or checkpoints themselves.
217
+ // but we use a block stream so we need to provide 'local' L2Tips.
218
+ // We configure the block stream to ignore checkpoints and set checkpoint values to genesis here.
219
+ const genesisCheckpointHeaderHash = GENESIS_CHECKPOINT_HEADER_HASH.toString();
152
220
  return {
153
- latest: latestBlockId,
221
+ proposed: latestBlockId,
222
+ checkpointed: {
223
+ block: {
224
+ number: INITIAL_L2_BLOCK_NUM,
225
+ hash: GENESIS_BLOCK_HEADER_HASH.toString()
226
+ },
227
+ checkpoint: {
228
+ number: INITIAL_CHECKPOINT_NUMBER,
229
+ hash: genesisCheckpointHeaderHash
230
+ }
231
+ },
154
232
  finalized: {
155
- number: Number(status.finalisedBlockNumber),
156
- hash: ''
233
+ block: {
234
+ number: status.finalizedBlockNumber,
235
+ hash: finalizedBlockHash ?? ''
236
+ },
237
+ checkpoint: {
238
+ number: INITIAL_CHECKPOINT_NUMBER,
239
+ hash: genesisCheckpointHeaderHash
240
+ }
157
241
  },
158
242
  proven: {
159
- number: Number(status.finalisedBlockNumber),
160
- hash: ''
243
+ block: {
244
+ number: provenBlockNumber,
245
+ hash: provenBlockHash ?? ''
246
+ },
247
+ checkpoint: {
248
+ number: INITIAL_CHECKPOINT_NUMBER,
249
+ hash: genesisCheckpointHeaderHash
250
+ }
161
251
  }
162
252
  };
163
253
  }
164
254
  /** Handles an event emitted by the block stream. */ async handleBlockStreamEvent(event) {
165
- try {
166
- switch(event.type){
167
- case 'blocks-added':
168
- await this.handleL2Blocks(event.blocks);
169
- break;
170
- case 'chain-pruned':
171
- await this.handleChainPruned(event.blockNumber);
172
- break;
173
- case 'chain-proven':
174
- await this.handleChainProven(event.blockNumber);
175
- break;
176
- case 'chain-finalized':
177
- await this.handleChainFinalized(event.blockNumber);
178
- break;
179
- }
180
- } catch (err) {
181
- this.log.error('Error processing block stream', err);
255
+ switch(event.type){
256
+ case 'blocks-added':
257
+ await this.handleL2Blocks(event.blocks);
258
+ break;
259
+ case 'chain-pruned':
260
+ await this.handleChainPruned(event.block.number);
261
+ break;
262
+ case 'chain-proven':
263
+ await this.handleChainProven(event.block.number);
264
+ break;
265
+ case 'chain-finalized':
266
+ await this.handleChainFinalized(event.block.number);
267
+ break;
182
268
  }
183
269
  }
184
270
  /**
@@ -186,19 +272,23 @@ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js
186
272
  * @param l2Blocks - The L2 blocks to handle.
187
273
  * @returns Whether the block handled was produced by this same node.
188
274
  */ async handleL2Blocks(l2Blocks) {
189
- this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1).number}`);
190
- const messagePromises = l2Blocks.map((block)=>this.l2BlockSource.getL1ToL2Messages(BigInt(block.number)));
191
- const l1ToL2Messages = await Promise.all(messagePromises);
275
+ this.log.debug(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1).number}`);
276
+ // Fetch the L1->L2 messages for the first block in a checkpoint.
277
+ const messagesForBlocks = new Map();
278
+ await Promise.all(l2Blocks.filter((b)=>b.indexWithinCheckpoint === 0).map(async (block)=>{
279
+ const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
280
+ messagesForBlocks.set(block.number, l1ToL2Messages);
281
+ }));
192
282
  let updateStatus = undefined;
193
- for(let i = 0; i < l2Blocks.length; i++){
194
- const [duration, result] = await elapsed(()=>this.handleL2Block(l2Blocks[i], l1ToL2Messages[i]));
195
- this.log.verbose(`World state updated with L2 block ${l2Blocks[i].number}`, {
283
+ for (const block of l2Blocks){
284
+ const [duration, result] = await elapsed(()=>this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []));
285
+ this.log.info(`World state updated with L2 block ${block.number}`, {
196
286
  eventName: 'l2-block-handled',
197
287
  duration,
198
- unfinalisedBlockNumber: result.summary.unfinalisedBlockNumber,
199
- finalisedBlockNumber: result.summary.finalisedBlockNumber,
200
- oldestHistoricBlock: result.summary.oldestHistoricalBlock,
201
- ...l2Blocks[i].getStats()
288
+ unfinalizedBlockNumber: BigInt(result.summary.unfinalizedBlockNumber),
289
+ finalizedBlockNumber: BigInt(result.summary.finalizedBlockNumber),
290
+ oldestHistoricBlock: BigInt(result.summary.oldestHistoricalBlock),
291
+ ...block.getStats()
202
292
  });
203
293
  updateStatus = result;
204
294
  }
@@ -213,16 +303,12 @@ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js
213
303
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
214
304
  * @returns Whether the block handled was produced by this same node.
215
305
  */ async handleL2Block(l2Block, l1ToL2Messages) {
216
- // First we check that the L1 to L2 messages hash to the block inHash.
217
- // Note that we cannot optimize this check by checking the root of the subtree after inserting the messages
218
- // to the real L1_TO_L2_MESSAGE_TREE (like we do in merkleTreeDb.handleL2BlockAndMessages(...)) because that
219
- // tree uses pedersen and we don't have access to the converted root.
220
- await this.verifyMessagesHashToInHash(l1ToL2Messages, l2Block.header.contentCommitment.inHash);
221
- // If the above check succeeds, we can proceed to handle the block.
222
- this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
306
+ this.log.debug(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
223
307
  blockNumber: l2Block.number,
224
308
  blockHash: await l2Block.hash().then((h)=>h.toString()),
225
- l1ToL2Messages: l1ToL2Messages.map((msg)=>msg.toString())
309
+ l1ToL2Messages: l1ToL2Messages.map((msg)=>msg.toString()),
310
+ blockHeader: l2Block.header.toInspect(),
311
+ blockStats: l2Block.getStats()
226
312
  });
227
313
  const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
228
314
  if (this.currentState === WorldStateRunningState.SYNCHING && l2Block.number >= this.latestBlockNumberAtStart) {
@@ -233,26 +319,47 @@ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js
233
319
  }
234
320
  async handleChainFinalized(blockNumber) {
235
321
  this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
236
- const summary = await this.merkleTreeDb.setFinalised(BigInt(blockNumber));
322
+ const summary = await this.merkleTreeDb.setFinalized(blockNumber);
237
323
  if (this.historyToKeep === undefined) {
238
324
  return;
239
325
  }
240
- const newHistoricBlock = summary.finalisedBlockNumber - BigInt(this.historyToKeep) + 1n;
241
- if (newHistoricBlock <= 1) {
326
+ // Get the checkpointed block for the finalized block number
327
+ const finalisedCheckpoint = await this.l2BlockSource.getCheckpointedBlock(summary.finalizedBlockNumber);
328
+ if (finalisedCheckpoint === undefined) {
329
+ this.log.warn(`Failed to retrieve checkpointed block for finalized block number: ${summary.finalizedBlockNumber}`);
330
+ return;
331
+ }
332
+ // Compute the required historic checkpoint number
333
+ const newHistoricCheckpointNumber = finalisedCheckpoint.checkpointNumber - this.historyToKeep + 1;
334
+ if (newHistoricCheckpointNumber <= 1) {
335
+ return;
336
+ }
337
+ // Retrieve the historic checkpoint
338
+ const historicCheckpoints = await this.l2BlockSource.getCheckpoints(CheckpointNumber(newHistoricCheckpointNumber), 1);
339
+ if (historicCheckpoints.length === 0 || historicCheckpoints[0] === undefined) {
340
+ this.log.warn(`Failed to retrieve checkpoint number ${newHistoricCheckpointNumber} from Archiver`);
341
+ return;
342
+ }
343
+ const historicCheckpoint = historicCheckpoints[0];
344
+ if (historicCheckpoint.checkpoint.blocks.length === 0 || historicCheckpoint.checkpoint.blocks[0] === undefined) {
345
+ this.log.warn(`Retrieved checkpoint number ${newHistoricCheckpointNumber} has no blocks!`);
242
346
  return;
243
347
  }
244
- this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
245
- const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
348
+ // Find the block at the start of the checkpoint and remove blocks up to this one
349
+ const newHistoricBlock = historicCheckpoint.checkpoint.blocks[0];
350
+ this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
351
+ const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock.number));
246
352
  this.log.debug(`World state summary `, status.summary);
247
353
  }
248
354
  handleChainProven(blockNumber) {
355
+ this.provenBlockNumber = blockNumber;
249
356
  this.log.debug(`Proven chain is now at block ${blockNumber}`);
250
357
  return Promise.resolve();
251
358
  }
252
359
  async handleChainPruned(blockNumber) {
253
- this.log.warn(`Chain pruned to block ${blockNumber}`);
254
- const status = await this.merkleTreeDb.unwindBlocks(BigInt(blockNumber));
255
- this.latestBlockHashQuery = undefined;
360
+ this.log.info(`Chain pruned to block ${blockNumber}`);
361
+ const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
362
+ this.provenBlockNumber = undefined;
256
363
  this.instrumentation.updateWorldStateMetrics(status);
257
364
  }
258
365
  /**
@@ -262,16 +369,4 @@ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js
262
369
  this.currentState = newState;
263
370
  this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
264
371
  }
265
- /**
266
- * Verifies that the L1 to L2 messages hash to the block inHash.
267
- * @param l1ToL2Messages - The L1 to L2 messages for the block.
268
- * @param inHash - The inHash of the block.
269
- * @throws If the L1 to L2 messages do not hash to the block inHash.
270
- */ async verifyMessagesHashToInHash(l1ToL2Messages, inHash) {
271
- const treeCalculator = await MerkleTreeCalculator.create(L1_TO_L2_MSG_SUBTREE_HEIGHT, Buffer.alloc(32), (lhs, rhs)=>Promise.resolve(new SHA256Trunc().hash(lhs, rhs)));
272
- const root = await treeCalculator.computeTreeRoot(l1ToL2Messages.map((msg)=>msg.toBuffer()));
273
- if (!root.equals(inHash)) {
274
- throw new Error('Obtained L1 to L2 messages failed to be hashed to the block inHash');
275
- }
276
- }
277
372
  }
@@ -1,2 +1,2 @@
1
1
  export * from './utils.js';
2
- //# sourceMappingURL=index.d.ts.map
2
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy90ZXN0L2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQWMsWUFBWSxDQUFDIn0=
@@ -1,19 +1,26 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { BlockNumber, type CheckpointNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
3
  import { L2Block } from '@aztec/stdlib/block';
3
4
  import type { MerkleTreeReadOperations, MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server';
5
+ import { mockCheckpointAndMessages } from '@aztec/stdlib/testing';
4
6
  import type { NativeWorldStateService } from '../native/native_world_state.js';
5
- export declare function mockBlock(blockNum: number, size: number, fork: MerkleTreeWriteOperations): Promise<{
7
+ export declare function updateBlockState(block: L2Block, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations): Promise<void>;
8
+ export declare function mockBlock(blockNum: BlockNumber, size: number, fork: MerkleTreeWriteOperations, maxEffects?: number | undefined, numL1ToL2Messages?: number, isFirstBlockInCheckpoint?: boolean): Promise<{
6
9
  block: L2Block;
7
10
  messages: Fr[];
8
11
  }>;
9
- export declare function mockEmptyBlock(blockNum: number, fork: MerkleTreeWriteOperations): Promise<{
12
+ export declare function mockEmptyBlock(blockNum: BlockNumber, fork: MerkleTreeWriteOperations): Promise<{
10
13
  block: L2Block;
11
14
  messages: Fr[];
12
15
  }>;
13
- export declare function mockBlocks(from: number, count: number, numTxs: number, worldState: NativeWorldStateService): Promise<{
16
+ export declare function mockBlocks(from: BlockNumber, count: number, numTxs: number, worldState: NativeWorldStateService): Promise<{
14
17
  blocks: L2Block[];
15
18
  messages: Fr[][];
16
19
  }>;
20
+ export declare function mockCheckpoint(checkpointNumber: CheckpointNumber, fork: MerkleTreeWriteOperations, options?: Partial<Parameters<typeof mockCheckpointAndMessages>[1]>): Promise<{
21
+ checkpoint: import("@aztec/stdlib/checkpoint").Checkpoint;
22
+ messages: Fr[];
23
+ }>;
17
24
  export declare function assertSameState(forkA: MerkleTreeReadOperations, forkB: MerkleTreeReadOperations): Promise<void>;
18
25
  export declare function compareChains(left: MerkleTreeReadOperations, right: MerkleTreeReadOperations): Promise<void>;
19
- //# sourceMappingURL=utils.d.ts.map
26
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuZC50cyIsInNvdXJjZVJvb3QiO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