@aztec/world-state 0.0.0-test.0

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 (74) hide show
  1. package/README.md +40 -0
  2. package/dest/index.d.ts +5 -0
  3. package/dest/index.d.ts.map +1 -0
  4. package/dest/index.js +4 -0
  5. package/dest/instrumentation/instrumentation.d.ts +22 -0
  6. package/dest/instrumentation/instrumentation.d.ts.map +1 -0
  7. package/dest/instrumentation/instrumentation.js +117 -0
  8. package/dest/native/fork_checkpoint.d.ts +10 -0
  9. package/dest/native/fork_checkpoint.d.ts.map +1 -0
  10. package/dest/native/fork_checkpoint.js +26 -0
  11. package/dest/native/index.d.ts +3 -0
  12. package/dest/native/index.d.ts.map +1 -0
  13. package/dest/native/index.js +2 -0
  14. package/dest/native/merkle_trees_facade.d.ts +42 -0
  15. package/dest/native/merkle_trees_facade.d.ts.map +1 -0
  16. package/dest/native/merkle_trees_facade.js +240 -0
  17. package/dest/native/message.d.ts +331 -0
  18. package/dest/native/message.d.ts.map +1 -0
  19. package/dest/native/message.js +192 -0
  20. package/dest/native/native_world_state.d.ts +57 -0
  21. package/dest/native/native_world_state.d.ts.map +1 -0
  22. package/dest/native/native_world_state.js +229 -0
  23. package/dest/native/native_world_state_instance.d.ts +33 -0
  24. package/dest/native/native_world_state_instance.d.ts.map +1 -0
  25. package/dest/native/native_world_state_instance.js +164 -0
  26. package/dest/native/world_state_ops_queue.d.ts +19 -0
  27. package/dest/native/world_state_ops_queue.d.ts.map +1 -0
  28. package/dest/native/world_state_ops_queue.js +146 -0
  29. package/dest/synchronizer/config.d.ts +23 -0
  30. package/dest/synchronizer/config.d.ts.map +1 -0
  31. package/dest/synchronizer/config.js +39 -0
  32. package/dest/synchronizer/factory.d.ts +12 -0
  33. package/dest/synchronizer/factory.d.ts.map +1 -0
  34. package/dest/synchronizer/factory.js +24 -0
  35. package/dest/synchronizer/index.d.ts +3 -0
  36. package/dest/synchronizer/index.d.ts.map +1 -0
  37. package/dest/synchronizer/index.js +2 -0
  38. package/dest/synchronizer/server_world_state_synchronizer.d.ts +79 -0
  39. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -0
  40. package/dest/synchronizer/server_world_state_synchronizer.js +277 -0
  41. package/dest/test/index.d.ts +2 -0
  42. package/dest/test/index.d.ts.map +1 -0
  43. package/dest/test/index.js +1 -0
  44. package/dest/test/utils.d.ts +19 -0
  45. package/dest/test/utils.d.ts.map +1 -0
  46. package/dest/test/utils.js +99 -0
  47. package/dest/testing.d.ts +10 -0
  48. package/dest/testing.d.ts.map +1 -0
  49. package/dest/testing.js +37 -0
  50. package/dest/world-state-db/index.d.ts +3 -0
  51. package/dest/world-state-db/index.d.ts.map +1 -0
  52. package/dest/world-state-db/index.js +1 -0
  53. package/dest/world-state-db/merkle_tree_db.d.ts +68 -0
  54. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -0
  55. package/dest/world-state-db/merkle_tree_db.js +17 -0
  56. package/package.json +98 -0
  57. package/src/index.ts +4 -0
  58. package/src/instrumentation/instrumentation.ts +174 -0
  59. package/src/native/fork_checkpoint.ts +30 -0
  60. package/src/native/index.ts +2 -0
  61. package/src/native/merkle_trees_facade.ts +331 -0
  62. package/src/native/message.ts +541 -0
  63. package/src/native/native_world_state.ts +317 -0
  64. package/src/native/native_world_state_instance.ts +238 -0
  65. package/src/native/world_state_ops_queue.ts +190 -0
  66. package/src/synchronizer/config.ts +68 -0
  67. package/src/synchronizer/factory.ts +53 -0
  68. package/src/synchronizer/index.ts +2 -0
  69. package/src/synchronizer/server_world_state_synchronizer.ts +344 -0
  70. package/src/test/index.ts +1 -0
  71. package/src/test/utils.ts +153 -0
  72. package/src/testing.ts +60 -0
  73. package/src/world-state-db/index.ts +3 -0
  74. package/src/world-state-db/merkle_tree_db.ts +79 -0
@@ -0,0 +1,190 @@
1
+ import { promiseWithResolvers } from '@aztec/foundation/promise';
2
+
3
+ import { WorldStateMessageType } from './message.js';
4
+
5
+ /**
6
+ * This is the implementation for queueing requests to the world state.
7
+ * Requests need to be queued for the world state to ensure that writes are correctly ordered
8
+ * and reads return the correct data.
9
+ * Due to the nature of the NAPI we can't really do this there.
10
+ *
11
+ * The rules for queueing are as follows:
12
+ *
13
+ * 1. Reads of committed state never need to be queued. LMDB uses MVCC to ensure readers see a consistent view of the DB.
14
+ * 2. Reads of uncommitted state can happen concurrently with other reads of uncommitted state on the same fork (or reads of committed state)
15
+ * 3. All writes require exclusive access to their respective fork
16
+ *
17
+ */
18
+
19
+ type WorldStateOp = {
20
+ requestId: number;
21
+ mutating: boolean;
22
+ request: () => Promise<any>;
23
+ promise: PromiseWithResolvers<any>;
24
+ };
25
+
26
+ // These are the set of message types that implement mutating operations
27
+ // Messages of these types require exclusive access to their given forks
28
+ export const MUTATING_MSG_TYPES = new Set([
29
+ WorldStateMessageType.APPEND_LEAVES,
30
+ WorldStateMessageType.BATCH_INSERT,
31
+ WorldStateMessageType.SEQUENTIAL_INSERT,
32
+ WorldStateMessageType.UPDATE_ARCHIVE,
33
+ WorldStateMessageType.COMMIT,
34
+ WorldStateMessageType.ROLLBACK,
35
+ WorldStateMessageType.SYNC_BLOCK,
36
+ WorldStateMessageType.CREATE_FORK,
37
+ WorldStateMessageType.DELETE_FORK,
38
+ WorldStateMessageType.FINALISE_BLOCKS,
39
+ WorldStateMessageType.UNWIND_BLOCKS,
40
+ WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
41
+ WorldStateMessageType.CREATE_CHECKPOINT,
42
+ WorldStateMessageType.COMMIT_CHECKPOINT,
43
+ WorldStateMessageType.REVERT_CHECKPOINT,
44
+ ]);
45
+
46
+ // This class implements the per-fork operation queue
47
+ export class WorldStateOpsQueue {
48
+ private requests: WorldStateOp[] = [];
49
+ private inFlightMutatingCount = 0;
50
+ private inFlightCount = 0;
51
+ private stopPromise?: Promise<void>;
52
+ private stopResolve?: () => void;
53
+ private requestId = 0;
54
+ private ops: Map<number, WorldStateOp> = new Map();
55
+
56
+ // The primary public api, this is where an operation is queued
57
+ // We return a promise that will ultimately be resolved/rejected with the response/error generated by the 'request' argument
58
+ public execute(request: () => Promise<any>, messageType: WorldStateMessageType, committedOnly: boolean) {
59
+ if (this.stopResolve !== undefined) {
60
+ throw new Error('Unable to send request to world state, queue already stopped');
61
+ }
62
+
63
+ const op: WorldStateOp = {
64
+ requestId: this.requestId++,
65
+ mutating: MUTATING_MSG_TYPES.has(messageType),
66
+ request,
67
+ promise: promiseWithResolvers(),
68
+ };
69
+ this.ops.set(op.requestId, op);
70
+
71
+ // Perform the appropriate action based upon the queueing rules
72
+ if (op.mutating) {
73
+ this.executeMutating(op);
74
+ } else if (committedOnly === false) {
75
+ this.executeNonMutatingUncommitted(op);
76
+ } else {
77
+ this.executeNonMutatingCommitted(op);
78
+ }
79
+ return op.promise.promise;
80
+ }
81
+
82
+ // Mutating requests need exclusive access
83
+ private executeMutating(op: WorldStateOp) {
84
+ // If nothing is in flight then we send the request immediately
85
+ // Otherwise add to the queue
86
+ if (this.inFlightCount === 0) {
87
+ this.sendEnqueuedRequest(op);
88
+ } else {
89
+ this.requests.push(op);
90
+ }
91
+ }
92
+
93
+ // Non mutating requests including uncommitted state
94
+ private executeNonMutatingUncommitted(op: WorldStateOp) {
95
+ // If there are no mutating requests in flight and there is nothing queued
96
+ // then send the request immediately
97
+ // If a mutating request is in flight then we must wait
98
+ // If a mutating request is not in flight but something is queued then it must be a mutating request
99
+ if (this.inFlightMutatingCount == 0 && this.requests.length == 0) {
100
+ this.sendEnqueuedRequest(op);
101
+ } else {
102
+ this.requests.push(op);
103
+ }
104
+ }
105
+
106
+ private executeNonMutatingCommitted(op: WorldStateOp) {
107
+ // This is a non-mutating request for committed data
108
+ // It can always be sent
109
+ op.request()
110
+ .then(op.promise.resolve, op.promise.reject)
111
+ .finally(() => {
112
+ this.ops.delete(op.requestId);
113
+ });
114
+ }
115
+
116
+ private checkAndEnqueue(completedOp: WorldStateOp) {
117
+ // As request has completed
118
+ // First we decrements the relevant in flight counters
119
+ if (completedOp.mutating) {
120
+ --this.inFlightMutatingCount;
121
+ }
122
+ --this.inFlightCount;
123
+
124
+ // If there are still requests in flight then do nothing further
125
+ if (this.inFlightCount != 0) {
126
+ return;
127
+ }
128
+
129
+ // No requests in flight, send next queued requests
130
+ // We loop and send:
131
+ // 1 mutating request if it is next in the queue
132
+ // As many non-mutating requests as we encounter until
133
+ // we exhaust the queue or we reach a mutating request
134
+ while (this.requests.length > 0) {
135
+ const next = this.requests[0];
136
+ if (next.mutating) {
137
+ if (this.inFlightCount == 0) {
138
+ // send the mutating request
139
+ this.requests.shift();
140
+ this.sendEnqueuedRequest(next);
141
+ }
142
+ // this request is mutating, we need to stop here
143
+ break;
144
+ } else {
145
+ // not mutating, send and go round again
146
+ this.requests.shift();
147
+ this.sendEnqueuedRequest(next);
148
+ }
149
+ }
150
+
151
+ // If the queue is empty, there is nothing in flight and we have been told to stop, then resolve the stop promise
152
+ if (this.inFlightCount == 0 && this.stopResolve !== undefined) {
153
+ this.stopResolve();
154
+ }
155
+ }
156
+
157
+ private sendEnqueuedRequest(op: WorldStateOp) {
158
+ // Here we increment the in flight counts before sending
159
+ ++this.inFlightCount;
160
+ if (op.mutating) {
161
+ ++this.inFlightMutatingCount;
162
+ }
163
+
164
+ // Make the request and pass the response/error through to the stored promise
165
+ op.request()
166
+ .then(op.promise.resolve, op.promise.reject)
167
+ .finally(() => {
168
+ this.checkAndEnqueue(op);
169
+ this.ops.delete(op.requestId);
170
+ });
171
+ }
172
+
173
+ public stop() {
174
+ // If there is already a stop promise then return it
175
+ if (this.stopPromise) {
176
+ return this.stopPromise;
177
+ }
178
+
179
+ // Otherwise create a new one and capture the resolve method
180
+ this.stopPromise = new Promise(resolve => {
181
+ this.stopResolve = resolve;
182
+ });
183
+
184
+ // If no outstanding requests then immediately resolve the promise
185
+ if (this.requests.length == 0 && this.inFlightCount == 0 && this.stopResolve !== undefined) {
186
+ this.stopResolve();
187
+ }
188
+ return this.stopPromise;
189
+ }
190
+ }
@@ -0,0 +1,68 @@
1
+ import {
2
+ type ConfigMappingsType,
3
+ booleanConfigHelper,
4
+ getConfigFromMappings,
5
+ numberConfigHelper,
6
+ } from '@aztec/foundation/config';
7
+
8
+ /** World State synchronizer configuration values. */
9
+ export interface WorldStateConfig {
10
+ /** The frequency in which to check. */
11
+ worldStateBlockCheckIntervalMS: number;
12
+
13
+ /** Whether to follow only the proven chain. */
14
+ worldStateProvenBlocksOnly: boolean;
15
+
16
+ /** Size of the batch for each get-blocks request from the synchronizer to the archiver. */
17
+ worldStateBlockRequestBatchSize?: number;
18
+
19
+ /** The map size to be provided to LMDB for each world state tree DB, optional, will inherit from the general dataStoreMapSizeKB if not specified*/
20
+ worldStateDbMapSizeKb?: number;
21
+
22
+ /** Optional directory for the world state DB, if unspecified will default to the general data directory */
23
+ worldStateDataDirectory?: string;
24
+
25
+ /** The number of historic blocks to maintain */
26
+ worldStateBlockHistory: number;
27
+ }
28
+
29
+ export const worldStateConfigMappings: ConfigMappingsType<WorldStateConfig> = {
30
+ worldStateBlockCheckIntervalMS: {
31
+ env: 'WS_BLOCK_CHECK_INTERVAL_MS',
32
+ parseEnv: (val: string) => +val,
33
+ defaultValue: 100,
34
+ description: 'The frequency in which to check.',
35
+ },
36
+ worldStateProvenBlocksOnly: {
37
+ env: 'WS_PROVEN_BLOCKS_ONLY',
38
+ description: 'Whether to follow only the proven chain.',
39
+ ...booleanConfigHelper(),
40
+ },
41
+ worldStateBlockRequestBatchSize: {
42
+ env: 'WS_BLOCK_REQUEST_BATCH_SIZE',
43
+ parseEnv: (val: string | undefined) => (val ? +val : undefined),
44
+ description: 'Size of the batch for each get-blocks request from the synchronizer to the archiver.',
45
+ },
46
+ worldStateDbMapSizeKb: {
47
+ env: 'WS_DB_MAP_SIZE_KB',
48
+ parseEnv: (val: string | undefined) => (val ? +val : undefined),
49
+ description: 'The maximum possible size of the world state DB',
50
+ },
51
+ worldStateDataDirectory: {
52
+ env: 'WS_DATA_DIRECTORY',
53
+ description: 'Optional directory for the world state database',
54
+ },
55
+ worldStateBlockHistory: {
56
+ env: 'WS_NUM_HISTORIC_BLOCKS',
57
+ description: 'The number of historic blocks to maintain. Values less than 1 mean all history is maintained',
58
+ ...numberConfigHelper(64),
59
+ },
60
+ };
61
+
62
+ /**
63
+ * Returns the configuration values for the world state synchronizer.
64
+ * @returns The configuration values for the world state synchronizer.
65
+ */
66
+ export function getWorldStateConfigFromEnv(): WorldStateConfig {
67
+ return getConfigFromMappings<WorldStateConfig>(worldStateConfigMappings);
68
+ }
@@ -0,0 +1,53 @@
1
+ import type { DataStoreConfig } from '@aztec/kv-store/config';
2
+ import type { L2BlockSource } from '@aztec/stdlib/block';
3
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
4
+ import type { PublicDataTreeLeaf } from '@aztec/stdlib/trees';
5
+ import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
6
+
7
+ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
8
+ import { NativeWorldStateService } from '../native/native_world_state.js';
9
+ import type { WorldStateConfig } from './config.js';
10
+ import { ServerWorldStateSynchronizer } from './server_world_state_synchronizer.js';
11
+
12
+ export async function createWorldStateSynchronizer(
13
+ config: WorldStateConfig & DataStoreConfig,
14
+ l2BlockSource: L2BlockSource & L1ToL2MessageSource,
15
+ prefilledPublicData: PublicDataTreeLeaf[] = [],
16
+ client: TelemetryClient = getTelemetryClient(),
17
+ ) {
18
+ const instrumentation = new WorldStateInstrumentation(client);
19
+ const merkleTrees = await createWorldState(config, prefilledPublicData, instrumentation);
20
+ return new ServerWorldStateSynchronizer(merkleTrees, l2BlockSource, config, instrumentation);
21
+ }
22
+
23
+ export async function createWorldState(
24
+ config: WorldStateConfig & DataStoreConfig,
25
+ prefilledPublicData: PublicDataTreeLeaf[] = [],
26
+ instrumentation: WorldStateInstrumentation = new WorldStateInstrumentation(getTelemetryClient()),
27
+ ) {
28
+ const newConfig = {
29
+ dataDirectory: config.worldStateDataDirectory ?? config.dataDirectory,
30
+ dataStoreMapSizeKB: config.worldStateDbMapSizeKb ?? config.dataStoreMapSizeKB,
31
+ } as DataStoreConfig;
32
+
33
+ if (!config.l1Contracts?.rollupAddress) {
34
+ throw new Error('Rollup address is required to create a world state synchronizer.');
35
+ }
36
+
37
+ // If a data directory is provided in config, then create a persistent store.
38
+ const merkleTrees = newConfig.dataDirectory
39
+ ? await NativeWorldStateService.new(
40
+ config.l1Contracts.rollupAddress,
41
+ newConfig.dataDirectory,
42
+ newConfig.dataStoreMapSizeKB,
43
+ prefilledPublicData,
44
+ instrumentation,
45
+ )
46
+ : await NativeWorldStateService.tmp(
47
+ config.l1Contracts.rollupAddress,
48
+ !['true', '1'].includes(process.env.DEBUG_WORLD_STATE!),
49
+ prefilledPublicData,
50
+ );
51
+
52
+ return merkleTrees;
53
+ }
@@ -0,0 +1,2 @@
1
+ export * from './server_world_state_synchronizer.js';
2
+ export * from './factory.js';
@@ -0,0 +1,344 @@
1
+ import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
2
+ import type { Fr } from '@aztec/foundation/fields';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import { promiseWithResolvers } from '@aztec/foundation/promise';
5
+ import { elapsed } from '@aztec/foundation/timer';
6
+ import { MerkleTreeCalculator } from '@aztec/foundation/trees';
7
+ import { SHA256Trunc } from '@aztec/merkle-tree';
8
+ import type {
9
+ L2Block,
10
+ L2BlockId,
11
+ L2BlockSource,
12
+ L2BlockStream,
13
+ L2BlockStreamEvent,
14
+ L2BlockStreamEventHandler,
15
+ L2BlockStreamLocalDataProvider,
16
+ L2Tips,
17
+ } from '@aztec/stdlib/block';
18
+ import {
19
+ WorldStateRunningState,
20
+ type WorldStateSyncStatus,
21
+ type WorldStateSynchronizer,
22
+ type WorldStateSynchronizerStatus,
23
+ } from '@aztec/stdlib/interfaces/server';
24
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
25
+ import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
26
+ import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
27
+ import { TraceableL2BlockStream, getTelemetryClient } from '@aztec/telemetry-client';
28
+
29
+ import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
30
+ import type { WorldStateStatusFull } from '../native/message.js';
31
+ import type { MerkleTreeAdminDatabase } from '../world-state-db/merkle_tree_db.js';
32
+ import type { WorldStateConfig } from './config.js';
33
+
34
+ /**
35
+ * Synchronizes the world state with the L2 blocks from a L2BlockSource via a block stream.
36
+ * The synchronizer will download the L2 blocks from the L2BlockSource and update the merkle trees.
37
+ * Handles chain reorgs via the L2BlockStream.
38
+ */
39
+ export class ServerWorldStateSynchronizer
40
+ implements WorldStateSynchronizer, L2BlockStreamLocalDataProvider, L2BlockStreamEventHandler
41
+ {
42
+ private readonly merkleTreeCommitted: MerkleTreeReadOperations;
43
+
44
+ private latestBlockNumberAtStart = 0;
45
+ private historyToKeep: number | undefined;
46
+ private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
47
+ private latestBlockHashQuery: { blockNumber: number; hash: string | undefined } | undefined = undefined;
48
+
49
+ private syncPromise = promiseWithResolvers<void>();
50
+ protected blockStream: L2BlockStream | undefined;
51
+
52
+ constructor(
53
+ private readonly merkleTreeDb: MerkleTreeAdminDatabase,
54
+ private readonly l2BlockSource: L2BlockSource & L1ToL2MessageSource,
55
+ private readonly config: WorldStateConfig,
56
+ private instrumentation = new WorldStateInstrumentation(getTelemetryClient()),
57
+ private readonly log = createLogger('world_state'),
58
+ ) {
59
+ this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
60
+ this.historyToKeep = config.worldStateBlockHistory < 1 ? undefined : config.worldStateBlockHistory;
61
+ this.log.info(
62
+ `Created world state synchroniser with block history of ${
63
+ this.historyToKeep === undefined ? 'infinity' : this.historyToKeep
64
+ }`,
65
+ );
66
+ }
67
+
68
+ public getCommitted(): MerkleTreeReadOperations {
69
+ return this.merkleTreeDb.getCommitted();
70
+ }
71
+
72
+ public getSnapshot(blockNumber: number): MerkleTreeReadOperations {
73
+ return this.merkleTreeDb.getSnapshot(blockNumber);
74
+ }
75
+
76
+ public fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
77
+ return this.merkleTreeDb.fork(blockNumber);
78
+ }
79
+
80
+ public async start() {
81
+ if (this.currentState === WorldStateRunningState.STOPPED) {
82
+ throw new Error('Synchronizer already stopped');
83
+ }
84
+ if (this.currentState !== WorldStateRunningState.IDLE) {
85
+ return this.syncPromise;
86
+ }
87
+
88
+ // Get the current latest block number
89
+ this.latestBlockNumberAtStart = await (this.config.worldStateProvenBlocksOnly
90
+ ? this.l2BlockSource.getProvenBlockNumber()
91
+ : this.l2BlockSource.getBlockNumber());
92
+
93
+ const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
94
+
95
+ if (blockToDownloadFrom <= this.latestBlockNumberAtStart) {
96
+ // If there are blocks to be retrieved, go to a synching state
97
+ this.setCurrentState(WorldStateRunningState.SYNCHING);
98
+ this.log.verbose(`Starting sync from ${blockToDownloadFrom} to latest block ${this.latestBlockNumberAtStart}`);
99
+ } else {
100
+ // If no blocks to be retrieved, go straight to running
101
+ this.setCurrentState(WorldStateRunningState.RUNNING);
102
+ this.syncPromise.resolve();
103
+ this.log.debug(`Next block ${blockToDownloadFrom} already beyond latest block ${this.latestBlockNumberAtStart}`);
104
+ }
105
+
106
+ this.blockStream = this.createBlockStream();
107
+ this.blockStream.start();
108
+ this.log.info(`Started world state synchronizer from block ${blockToDownloadFrom}`);
109
+ return this.syncPromise.promise;
110
+ }
111
+
112
+ protected createBlockStream(): L2BlockStream {
113
+ const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
114
+ const logger = createLogger('world-state:block_stream');
115
+ return new TraceableL2BlockStream(this.l2BlockSource, this, this, tracer, 'WorldStateL2BlockStream', logger, {
116
+ proven: this.config.worldStateProvenBlocksOnly,
117
+ pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
118
+ batchSize: this.config.worldStateBlockRequestBatchSize,
119
+ });
120
+ }
121
+
122
+ public async stop() {
123
+ this.log.debug('Stopping block stream...');
124
+ await this.blockStream?.stop();
125
+ this.log.debug('Stopping merkle trees...');
126
+ await this.merkleTreeDb.close();
127
+ this.setCurrentState(WorldStateRunningState.STOPPED);
128
+ this.log.info(`Stopped world state synchronizer`);
129
+ }
130
+
131
+ public async status(): Promise<WorldStateSynchronizerStatus> {
132
+ const summary = await this.merkleTreeDb.getStatusSummary();
133
+ const status: WorldStateSyncStatus = {
134
+ latestBlockNumber: Number(summary.unfinalisedBlockNumber),
135
+ latestBlockHash: (await this.getL2BlockHash(Number(summary.unfinalisedBlockNumber))) ?? '',
136
+ finalisedBlockNumber: Number(summary.finalisedBlockNumber),
137
+ oldestHistoricBlockNumber: Number(summary.oldestHistoricalBlock),
138
+ treesAreSynched: summary.treesAreSynched,
139
+ };
140
+ return {
141
+ syncSummary: status,
142
+ state: this.currentState,
143
+ };
144
+ }
145
+
146
+ public async getLatestBlockNumber() {
147
+ return (await this.getL2Tips()).latest.number;
148
+ }
149
+
150
+ /**
151
+ * Forces an immediate sync.
152
+ * @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.
153
+ * @returns A promise that resolves with the block number the world state was synced to
154
+ */
155
+ public async syncImmediate(targetBlockNumber?: number): Promise<number> {
156
+ if (this.currentState !== WorldStateRunningState.RUNNING || this.blockStream === undefined) {
157
+ throw new Error(`World State is not running. Unable to perform sync.`);
158
+ }
159
+
160
+ // If we have been given a block number to sync to and we have reached that number then return
161
+ const currentBlockNumber = await this.getLatestBlockNumber();
162
+ if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
163
+ return currentBlockNumber;
164
+ }
165
+ this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
166
+
167
+ // Force the block stream to sync against the archiver now
168
+ await this.blockStream.sync();
169
+
170
+ // If we have been given a block number to sync to and we have not reached that number then fail
171
+ const updatedBlockNumber = await this.getLatestBlockNumber();
172
+ if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
173
+ throw new Error(`Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`);
174
+ }
175
+
176
+ return updatedBlockNumber;
177
+ }
178
+
179
+ /** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */
180
+ public async getL2BlockHash(number: number): Promise<string | undefined> {
181
+ if (number === 0) {
182
+ return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
183
+ }
184
+ if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
185
+ this.latestBlockHashQuery = {
186
+ hash: await this.merkleTreeCommitted
187
+ .getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number))
188
+ .then(leaf => leaf?.toString()),
189
+ blockNumber: number,
190
+ };
191
+ }
192
+ return this.latestBlockHashQuery.hash;
193
+ }
194
+
195
+ /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
196
+ public async getL2Tips(): Promise<L2Tips> {
197
+ const status = await this.merkleTreeDb.getStatusSummary();
198
+ const unfinalisedBlockHash = await this.getL2BlockHash(Number(status.unfinalisedBlockNumber));
199
+ const latestBlockId: L2BlockId = { number: Number(status.unfinalisedBlockNumber), hash: unfinalisedBlockHash! };
200
+
201
+ return {
202
+ latest: latestBlockId,
203
+ finalized: { number: Number(status.finalisedBlockNumber), hash: '' },
204
+ proven: { number: Number(status.finalisedBlockNumber), hash: '' }, // TODO(palla/reorg): Using finalised as proven for now
205
+ };
206
+ }
207
+
208
+ /** Handles an event emitted by the block stream. */
209
+ public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
210
+ try {
211
+ switch (event.type) {
212
+ case 'blocks-added':
213
+ await this.handleL2Blocks(event.blocks);
214
+ break;
215
+ case 'chain-pruned':
216
+ await this.handleChainPruned(event.blockNumber);
217
+ break;
218
+ case 'chain-proven':
219
+ await this.handleChainProven(event.blockNumber);
220
+ break;
221
+ case 'chain-finalized':
222
+ await this.handleChainFinalized(event.blockNumber);
223
+ break;
224
+ }
225
+ } catch (err) {
226
+ this.log.error('Error processing block stream', err);
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Handles a list of L2 blocks (i.e. Inserts the new note hashes into the merkle tree).
232
+ * @param l2Blocks - The L2 blocks to handle.
233
+ * @returns Whether the block handled was produced by this same node.
234
+ */
235
+ private async handleL2Blocks(l2Blocks: L2Block[]) {
236
+ this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
237
+
238
+ const messagePromises = l2Blocks.map(block => this.l2BlockSource.getL1ToL2Messages(BigInt(block.number)));
239
+ const l1ToL2Messages: Fr[][] = await Promise.all(messagePromises);
240
+ let updateStatus: WorldStateStatusFull | undefined = undefined;
241
+
242
+ for (let i = 0; i < l2Blocks.length; i++) {
243
+ const [duration, result] = await elapsed(() => this.handleL2Block(l2Blocks[i], l1ToL2Messages[i]));
244
+ this.log.verbose(`World state updated with L2 block ${l2Blocks[i].number}`, {
245
+ eventName: 'l2-block-handled',
246
+ duration,
247
+ unfinalisedBlockNumber: result.summary.unfinalisedBlockNumber,
248
+ finalisedBlockNumber: result.summary.finalisedBlockNumber,
249
+ oldestHistoricBlock: result.summary.oldestHistoricalBlock,
250
+ ...l2Blocks[i].getStats(),
251
+ } satisfies L2BlockHandledStats);
252
+ updateStatus = result;
253
+ }
254
+ if (!updateStatus) {
255
+ return;
256
+ }
257
+ this.instrumentation.updateWorldStateMetrics(updateStatus);
258
+ }
259
+
260
+ /**
261
+ * Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
262
+ * @param l2Block - The L2 block to handle.
263
+ * @param l1ToL2Messages - The L1 to L2 messages for the block.
264
+ * @returns Whether the block handled was produced by this same node.
265
+ */
266
+ private async handleL2Block(l2Block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
267
+ // First we check that the L1 to L2 messages hash to the block inHash.
268
+ // Note that we cannot optimize this check by checking the root of the subtree after inserting the messages
269
+ // to the real L1_TO_L2_MESSAGE_TREE (like we do in merkleTreeDb.handleL2BlockAndMessages(...)) because that
270
+ // tree uses pedersen and we don't have access to the converted root.
271
+ await this.verifyMessagesHashToInHash(l1ToL2Messages, l2Block.header.contentCommitment.inHash);
272
+
273
+ // If the above check succeeds, we can proceed to handle the block.
274
+ this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
275
+ blockNumber: l2Block.number,
276
+ blockHash: await l2Block.hash().then(h => h.toString()),
277
+ l1ToL2Messages: l1ToL2Messages.map(msg => msg.toString()),
278
+ });
279
+ const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
280
+
281
+ if (this.currentState === WorldStateRunningState.SYNCHING && l2Block.number >= this.latestBlockNumberAtStart) {
282
+ this.setCurrentState(WorldStateRunningState.RUNNING);
283
+ this.syncPromise.resolve();
284
+ }
285
+
286
+ return result;
287
+ }
288
+
289
+ private async handleChainFinalized(blockNumber: number) {
290
+ this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
291
+ const summary = await this.merkleTreeDb.setFinalised(BigInt(blockNumber));
292
+ if (this.historyToKeep === undefined) {
293
+ return;
294
+ }
295
+ const newHistoricBlock = summary.finalisedBlockNumber - BigInt(this.historyToKeep) + 1n;
296
+ if (newHistoricBlock <= 1) {
297
+ return;
298
+ }
299
+ this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
300
+ const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
301
+ this.log.debug(`World state summary `, status.summary);
302
+ }
303
+
304
+ private handleChainProven(blockNumber: number) {
305
+ this.log.debug(`Proven chain is now at block ${blockNumber}`);
306
+ return Promise.resolve();
307
+ }
308
+
309
+ private async handleChainPruned(blockNumber: number) {
310
+ this.log.warn(`Chain pruned to block ${blockNumber}`);
311
+ const status = await this.merkleTreeDb.unwindBlocks(BigInt(blockNumber));
312
+ this.latestBlockHashQuery = undefined;
313
+ this.instrumentation.updateWorldStateMetrics(status);
314
+ }
315
+
316
+ /**
317
+ * Method to set the value of the current state.
318
+ * @param newState - New state value.
319
+ */
320
+ private setCurrentState(newState: WorldStateRunningState) {
321
+ this.currentState = newState;
322
+ this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
323
+ }
324
+
325
+ /**
326
+ * Verifies that the L1 to L2 messages hash to the block inHash.
327
+ * @param l1ToL2Messages - The L1 to L2 messages for the block.
328
+ * @param inHash - The inHash of the block.
329
+ * @throws If the L1 to L2 messages do not hash to the block inHash.
330
+ */
331
+ protected async verifyMessagesHashToInHash(l1ToL2Messages: Fr[], inHash: Buffer) {
332
+ const treeCalculator = await MerkleTreeCalculator.create(
333
+ L1_TO_L2_MSG_SUBTREE_HEIGHT,
334
+ Buffer.alloc(32),
335
+ (lhs, rhs) => Promise.resolve(new SHA256Trunc().hash(lhs, rhs)),
336
+ );
337
+
338
+ const root = await treeCalculator.computeTreeRoot(l1ToL2Messages.map(msg => msg.toBuffer()));
339
+
340
+ if (!root.equals(inHash)) {
341
+ throw new Error('Obtained L1 to L2 messages failed to be hashed to the block inHash');
342
+ }
343
+ }
344
+ }
@@ -0,0 +1 @@
1
+ export * from './utils.js';