@aztec/world-state 4.0.0-nightly.20250907 → 4.0.0-nightly.20260108
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/dest/index.d.ts +1 -1
- package/dest/instrumentation/instrumentation.d.ts +1 -1
- package/dest/instrumentation/instrumentation.d.ts.map +1 -1
- package/dest/native/bench_metrics.d.ts +1 -1
- package/dest/native/bench_metrics.d.ts.map +1 -1
- package/dest/native/fork_checkpoint.d.ts +1 -1
- package/dest/native/fork_checkpoint.d.ts.map +1 -1
- package/dest/native/index.d.ts +1 -1
- package/dest/native/merkle_trees_facade.d.ts +13 -5
- package/dest/native/merkle_trees_facade.d.ts.map +1 -1
- package/dest/native/merkle_trees_facade.js +29 -4
- package/dest/native/message.d.ts +13 -17
- package/dest/native/message.d.ts.map +1 -1
- package/dest/native/message.js +14 -20
- package/dest/native/native_world_state.d.ts +12 -9
- package/dest/native/native_world_state.d.ts.map +1 -1
- package/dest/native/native_world_state.js +17 -10
- package/dest/native/native_world_state_instance.d.ts +10 -1
- package/dest/native/native_world_state_instance.d.ts.map +1 -1
- package/dest/native/native_world_state_instance.js +21 -0
- package/dest/native/world_state_ops_queue.d.ts +1 -1
- package/dest/native/world_state_ops_queue.d.ts.map +1 -1
- package/dest/synchronizer/config.d.ts +2 -2
- package/dest/synchronizer/config.js +1 -1
- package/dest/synchronizer/errors.d.ts +1 -1
- package/dest/synchronizer/errors.d.ts.map +1 -1
- package/dest/synchronizer/factory.d.ts +2 -2
- package/dest/synchronizer/factory.js +6 -6
- package/dest/synchronizer/index.d.ts +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.d.ts +10 -26
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.js +37 -53
- package/dest/test/index.d.ts +1 -1
- package/dest/test/utils.d.ts +16 -9
- package/dest/test/utils.d.ts.map +1 -1
- package/dest/test/utils.js +56 -52
- package/dest/testing.d.ts +2 -2
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +1 -1
- package/dest/world-state-db/index.d.ts +1 -1
- package/dest/world-state-db/merkle_tree_db.d.ts +10 -9
- package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
- package/package.json +13 -12
- package/src/native/merkle_trees_facade.ts +34 -5
- package/src/native/message.ts +24 -39
- package/src/native/native_world_state.ts +44 -16
- package/src/native/native_world_state_instance.ts +28 -0
- package/src/synchronizer/config.ts +2 -2
- package/src/synchronizer/factory.ts +7 -7
- package/src/synchronizer/server_world_state_synchronizer.ts +69 -84
- package/src/test/utils.ts +89 -94
- package/src/testing.ts +1 -1
- package/src/world-state-db/merkle_tree_db.ts +13 -8
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
3
|
import { fromEntries, padArrayEnd } from '@aztec/foundation/collection';
|
|
4
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
5
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
4
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
5
6
|
import { tryRmDir } from '@aztec/foundation/fs';
|
|
6
7
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
7
|
-
import type {
|
|
8
|
+
import type { L2BlockNew } from '@aztec/stdlib/block';
|
|
8
9
|
import { DatabaseVersionManager } from '@aztec/stdlib/database-version';
|
|
9
10
|
import type {
|
|
10
11
|
IndexedTreeId,
|
|
@@ -14,6 +15,7 @@ import type {
|
|
|
14
15
|
import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
|
|
15
16
|
import { MerkleTreeId, NullifierLeaf, type NullifierLeafPreimage, PublicDataTreeLeaf } from '@aztec/stdlib/trees';
|
|
16
17
|
import { BlockHeader, PartialStateReference, StateReference } from '@aztec/stdlib/tx';
|
|
18
|
+
import { WorldStateRevision } from '@aztec/stdlib/world-state';
|
|
17
19
|
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
18
20
|
|
|
19
21
|
import assert from 'assert/strict';
|
|
@@ -33,7 +35,6 @@ import {
|
|
|
33
35
|
sanitizeFullStatus,
|
|
34
36
|
sanitizeSummary,
|
|
35
37
|
treeStateReferenceToSnapshot,
|
|
36
|
-
worldStateRevision,
|
|
37
38
|
} from './message.js';
|
|
38
39
|
import { NativeWorldState } from './native_world_state_instance.js';
|
|
39
40
|
|
|
@@ -147,32 +148,59 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
147
148
|
}
|
|
148
149
|
|
|
149
150
|
public getCommitted(): MerkleTreeReadOperations {
|
|
150
|
-
return new MerkleTreesFacade(this.instance, this.initialHeader!,
|
|
151
|
+
return new MerkleTreesFacade(this.instance, this.initialHeader!, WorldStateRevision.empty());
|
|
151
152
|
}
|
|
152
153
|
|
|
153
|
-
public getSnapshot(blockNumber:
|
|
154
|
-
return new MerkleTreesFacade(
|
|
154
|
+
public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
|
|
155
|
+
return new MerkleTreesFacade(
|
|
156
|
+
this.instance,
|
|
157
|
+
this.initialHeader!,
|
|
158
|
+
new WorldStateRevision(/*forkId=*/ 0, /* blockNumber=*/ blockNumber, /* includeUncommitted=*/ false),
|
|
159
|
+
);
|
|
155
160
|
}
|
|
156
161
|
|
|
157
|
-
public async fork(
|
|
162
|
+
public async fork(
|
|
163
|
+
blockNumber?: BlockNumber,
|
|
164
|
+
opts: { closeDelayMs?: number } = {},
|
|
165
|
+
): Promise<MerkleTreeWriteOperations> {
|
|
158
166
|
const resp = await this.instance.call(WorldStateMessageType.CREATE_FORK, {
|
|
159
167
|
latest: blockNumber === undefined,
|
|
160
|
-
blockNumber: blockNumber ??
|
|
168
|
+
blockNumber: blockNumber ?? BlockNumber.ZERO,
|
|
161
169
|
canonical: true,
|
|
162
170
|
});
|
|
163
|
-
return new MerkleTreesForkFacade(
|
|
171
|
+
return new MerkleTreesForkFacade(
|
|
172
|
+
this.instance,
|
|
173
|
+
this.initialHeader!,
|
|
174
|
+
new WorldStateRevision(
|
|
175
|
+
/*forkId=*/ resp.forkId,
|
|
176
|
+
/* blockNumber=*/ BlockNumber.ZERO,
|
|
177
|
+
/* includeUncommitted=*/ true,
|
|
178
|
+
),
|
|
179
|
+
opts,
|
|
180
|
+
);
|
|
164
181
|
}
|
|
165
182
|
|
|
166
183
|
public getInitialHeader(): BlockHeader {
|
|
167
184
|
return this.initialHeader!;
|
|
168
185
|
}
|
|
169
186
|
|
|
170
|
-
public async handleL2BlockAndMessages(l2Block:
|
|
171
|
-
|
|
187
|
+
public async handleL2BlockAndMessages(l2Block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
|
|
188
|
+
const isFirstBlock = l2Block.indexWithinCheckpoint === 0;
|
|
189
|
+
if (!isFirstBlock && l1ToL2Messages.length > 0) {
|
|
190
|
+
throw new Error(
|
|
191
|
+
`L1 to L2 messages must be empty for non-first blocks, but got ${l1ToL2Messages.length} messages for block ${l2Block.number}.`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// We have to pad the given l1 to l2 messages, and the note hashes and nullifiers within tx effects, because that's
|
|
196
|
+
// how the trees are built by circuits.
|
|
197
|
+
const paddedL1ToL2Messages = isFirstBlock
|
|
198
|
+
? padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP)
|
|
199
|
+
: [];
|
|
200
|
+
|
|
172
201
|
const paddedNoteHashes = l2Block.body.txEffects.flatMap(txEffect =>
|
|
173
202
|
padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
|
|
174
203
|
);
|
|
175
|
-
const paddedL1ToL2Messages = padArrayEnd(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
|
|
176
204
|
|
|
177
205
|
const paddedNullifiers = l2Block.body.txEffects
|
|
178
206
|
.flatMap(txEffect => padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX))
|
|
@@ -192,7 +220,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
192
220
|
WorldStateMessageType.SYNC_BLOCK,
|
|
193
221
|
{
|
|
194
222
|
blockNumber: l2Block.number,
|
|
195
|
-
blockHeaderHash: await l2Block.
|
|
223
|
+
blockHeaderHash: await l2Block.hash(),
|
|
196
224
|
paddedL1ToL2Messages: paddedL1ToL2Messages.map(serializeLeaf),
|
|
197
225
|
paddedNoteHashes: paddedNoteHashes.map(serializeLeaf),
|
|
198
226
|
paddedNullifiers: paddedNullifiers.map(serializeLeaf),
|
|
@@ -240,7 +268,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
240
268
|
* @param toBlockNumber The block number that is now the tip of the finalized chain
|
|
241
269
|
* @returns The new WorldStateStatus
|
|
242
270
|
*/
|
|
243
|
-
public async setFinalized(toBlockNumber:
|
|
271
|
+
public async setFinalized(toBlockNumber: BlockNumber) {
|
|
244
272
|
try {
|
|
245
273
|
await this.instance.call(
|
|
246
274
|
WorldStateMessageType.FINALIZE_BLOCKS,
|
|
@@ -263,7 +291,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
263
291
|
* @param toBlockNumber The block number of the new oldest historical block
|
|
264
292
|
* @returns The new WorldStateStatus
|
|
265
293
|
*/
|
|
266
|
-
public async removeHistoricalBlocks(toBlockNumber:
|
|
294
|
+
public async removeHistoricalBlocks(toBlockNumber: BlockNumber) {
|
|
267
295
|
try {
|
|
268
296
|
return await this.instance.call(
|
|
269
297
|
WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
|
|
@@ -285,7 +313,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
285
313
|
* @param toBlockNumber The block number of the new tip of the pending chain,
|
|
286
314
|
* @returns The new WorldStateStatus
|
|
287
315
|
*/
|
|
288
|
-
public async unwindBlocks(toBlockNumber:
|
|
316
|
+
public async unwindBlocks(toBlockNumber: BlockNumber) {
|
|
289
317
|
try {
|
|
290
318
|
return await this.instance.call(
|
|
291
319
|
WorldStateMessageType.UNWIND_BLOCKS,
|
|
@@ -36,6 +36,8 @@ export interface NativeWorldStateInstance {
|
|
|
36
36
|
messageType: T,
|
|
37
37
|
body: WorldStateRequest[T] & WorldStateRequestCategories,
|
|
38
38
|
): Promise<WorldStateResponse[T]>;
|
|
39
|
+
// TODO(dbanks12): this returns any type, but we should strongly type it
|
|
40
|
+
getHandle(): any;
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
/**
|
|
@@ -107,6 +109,32 @@ export class NativeWorldState implements NativeWorldStateInstance {
|
|
|
107
109
|
);
|
|
108
110
|
}
|
|
109
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Gets the native WorldState handle from the underlying native instance.
|
|
114
|
+
* We call the getHandle() method on the native WorldState to get a NAPI External
|
|
115
|
+
* that wraps the underlying C++ WorldState pointer.
|
|
116
|
+
* @returns The NAPI External handle to the native WorldState instance,since
|
|
117
|
+
* the NAPI external type is opaque, we return any (we could also use an opaque symbol type)
|
|
118
|
+
*/
|
|
119
|
+
public getHandle(): any {
|
|
120
|
+
const worldStateWrapper = (this.instance as any).dest;
|
|
121
|
+
|
|
122
|
+
if (!worldStateWrapper) {
|
|
123
|
+
throw new Error('No WorldStateWrapper found');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (typeof worldStateWrapper.getHandle !== 'function') {
|
|
127
|
+
throw new Error('WorldStateWrapper does not have getHandle method');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Call getHandle() to get the NAPI External
|
|
131
|
+
try {
|
|
132
|
+
return worldStateWrapper.getHandle();
|
|
133
|
+
} catch (error) {
|
|
134
|
+
this.log.error('Failed to get native WorldState handle', error);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
110
138
|
/**
|
|
111
139
|
* Sends a message to the native instance and returns the response.
|
|
112
140
|
* @param messageType - The type of message to send
|
|
@@ -16,7 +16,7 @@ export interface WorldStateConfig {
|
|
|
16
16
|
/** Size of the batch for each get-blocks request from the synchronizer to the archiver. */
|
|
17
17
|
worldStateBlockRequestBatchSize?: number;
|
|
18
18
|
|
|
19
|
-
/** The map size to be provided to LMDB for each world state tree DB, optional, will inherit from the general
|
|
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
20
|
worldStateDbMapSizeKb?: number;
|
|
21
21
|
|
|
22
22
|
/** The map size to be provided to LMDB for each world state archive tree, optional, will inherit from the general worldStateDbMapSizeKb if not specified*/
|
|
@@ -61,7 +61,7 @@ export const worldStateConfigMappings: ConfigMappingsType<WorldStateConfig> = {
|
|
|
61
61
|
worldStateDbMapSizeKb: {
|
|
62
62
|
env: 'WS_DB_MAP_SIZE_KB',
|
|
63
63
|
parseEnv: (val: string | undefined) => (val ? +val : undefined),
|
|
64
|
-
description: 'The maximum possible size of the world state DB in KB. Overwrites the general
|
|
64
|
+
description: 'The maximum possible size of the world state DB in KB. Overwrites the general dataStoreMapSizeKb.',
|
|
65
65
|
},
|
|
66
66
|
archiveTreeMapSizeKb: {
|
|
67
67
|
env: 'ARCHIVE_TREE_MAP_SIZE_KB',
|
|
@@ -39,18 +39,18 @@ export async function createWorldState(
|
|
|
39
39
|
| 'messageTreeMapSizeKb'
|
|
40
40
|
| 'publicDataTreeMapSizeKb'
|
|
41
41
|
> &
|
|
42
|
-
Pick<DataStoreConfig, 'dataDirectory' | '
|
|
42
|
+
Pick<DataStoreConfig, 'dataDirectory' | 'dataStoreMapSizeKb' | 'l1Contracts'>,
|
|
43
43
|
prefilledPublicData: PublicDataTreeLeaf[] = [],
|
|
44
44
|
instrumentation: WorldStateInstrumentation = new WorldStateInstrumentation(getTelemetryClient()),
|
|
45
45
|
) {
|
|
46
46
|
const dataDirectory = config.worldStateDataDirectory ?? config.dataDirectory;
|
|
47
|
-
const
|
|
47
|
+
const dataStoreMapSizeKb = config.worldStateDbMapSizeKb ?? config.dataStoreMapSizeKb;
|
|
48
48
|
const wsTreeMapSizes: WorldStateTreeMapSizes = {
|
|
49
|
-
archiveTreeMapSizeKb: config.archiveTreeMapSizeKb ??
|
|
50
|
-
nullifierTreeMapSizeKb: config.nullifierTreeMapSizeKb ??
|
|
51
|
-
noteHashTreeMapSizeKb: config.noteHashTreeMapSizeKb ??
|
|
52
|
-
messageTreeMapSizeKb: config.messageTreeMapSizeKb ??
|
|
53
|
-
publicDataTreeMapSizeKb: config.publicDataTreeMapSizeKb ??
|
|
49
|
+
archiveTreeMapSizeKb: config.archiveTreeMapSizeKb ?? dataStoreMapSizeKb,
|
|
50
|
+
nullifierTreeMapSizeKb: config.nullifierTreeMapSizeKb ?? dataStoreMapSizeKb,
|
|
51
|
+
noteHashTreeMapSizeKb: config.noteHashTreeMapSizeKb ?? dataStoreMapSizeKb,
|
|
52
|
+
messageTreeMapSizeKb: config.messageTreeMapSizeKb ?? dataStoreMapSizeKb,
|
|
53
|
+
publicDataTreeMapSizeKb: config.publicDataTreeMapSizeKb ?? dataStoreMapSizeKb,
|
|
54
54
|
};
|
|
55
55
|
|
|
56
56
|
if (!config.l1Contracts?.rollupAddress) {
|
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type { Fr } from '@aztec/foundation/fields';
|
|
1
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
3
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
5
4
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
6
5
|
import { elapsed } from '@aztec/foundation/timer';
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
L2BlockSource,
|
|
6
|
+
import {
|
|
7
|
+
type L2BlockId,
|
|
8
|
+
type L2BlockNew,
|
|
9
|
+
type L2BlockSource,
|
|
12
10
|
L2BlockStream,
|
|
13
|
-
L2BlockStreamEvent,
|
|
14
|
-
L2BlockStreamEventHandler,
|
|
15
|
-
L2BlockStreamLocalDataProvider,
|
|
16
|
-
L2Tips,
|
|
11
|
+
type L2BlockStreamEvent,
|
|
12
|
+
type L2BlockStreamEventHandler,
|
|
13
|
+
type L2BlockStreamLocalDataProvider,
|
|
14
|
+
type L2Tips,
|
|
17
15
|
} from '@aztec/stdlib/block';
|
|
18
16
|
import {
|
|
19
17
|
WorldStateRunningState,
|
|
@@ -25,7 +23,7 @@ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
|
25
23
|
import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
|
|
26
24
|
import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
|
|
27
25
|
import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
28
|
-
import {
|
|
26
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
29
27
|
|
|
30
28
|
import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
|
|
31
29
|
import type { WorldStateStatusFull } from '../native/message.js';
|
|
@@ -45,17 +43,17 @@ export class ServerWorldStateSynchronizer
|
|
|
45
43
|
{
|
|
46
44
|
private readonly merkleTreeCommitted: MerkleTreeReadOperations;
|
|
47
45
|
|
|
48
|
-
private latestBlockNumberAtStart =
|
|
46
|
+
private latestBlockNumberAtStart = BlockNumber.ZERO;
|
|
49
47
|
private historyToKeep: number | undefined;
|
|
50
48
|
private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
|
|
51
|
-
private latestBlockHashQuery: { blockNumber:
|
|
49
|
+
private latestBlockHashQuery: { blockNumber: BlockNumber; hash: string | undefined } | undefined = undefined;
|
|
52
50
|
|
|
53
51
|
private syncPromise = promiseWithResolvers<void>();
|
|
54
52
|
protected blockStream: L2BlockStream | undefined;
|
|
55
53
|
|
|
56
54
|
// WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
|
|
57
55
|
// store the proven block number here, in the synchronizer, so that we don't end up spamming the logs with 'chain-proved' events
|
|
58
|
-
private provenBlockNumber:
|
|
56
|
+
private provenBlockNumber: BlockNumber | undefined;
|
|
59
57
|
|
|
60
58
|
constructor(
|
|
61
59
|
private readonly merkleTreeDb: MerkleTreeAdminDatabase,
|
|
@@ -77,12 +75,12 @@ export class ServerWorldStateSynchronizer
|
|
|
77
75
|
return this.merkleTreeDb.getCommitted();
|
|
78
76
|
}
|
|
79
77
|
|
|
80
|
-
public getSnapshot(blockNumber:
|
|
78
|
+
public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
|
|
81
79
|
return this.merkleTreeDb.getSnapshot(blockNumber);
|
|
82
80
|
}
|
|
83
81
|
|
|
84
|
-
public fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
|
|
85
|
-
return this.merkleTreeDb.fork(blockNumber);
|
|
82
|
+
public fork(blockNumber?: BlockNumber, opts?: { closeDelayMs?: number }): Promise<MerkleTreeWriteOperations> {
|
|
83
|
+
return this.merkleTreeDb.fork(blockNumber, opts);
|
|
86
84
|
}
|
|
87
85
|
|
|
88
86
|
public backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>> {
|
|
@@ -102,9 +100,11 @@ export class ServerWorldStateSynchronizer
|
|
|
102
100
|
}
|
|
103
101
|
|
|
104
102
|
// Get the current latest block number
|
|
105
|
-
this.latestBlockNumberAtStart =
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
this.latestBlockNumberAtStart = BlockNumber(
|
|
104
|
+
await (this.config.worldStateProvenBlocksOnly
|
|
105
|
+
? this.l2BlockSource.getProvenBlockNumber()
|
|
106
|
+
: this.l2BlockSource.getBlockNumber()),
|
|
107
|
+
);
|
|
108
108
|
|
|
109
109
|
const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
|
|
110
110
|
|
|
@@ -126,9 +126,8 @@ export class ServerWorldStateSynchronizer
|
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
protected createBlockStream(): L2BlockStream {
|
|
129
|
-
const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
|
|
130
129
|
const logger = createLogger('world-state:block_stream');
|
|
131
|
-
return new
|
|
130
|
+
return new L2BlockStream(this.l2BlockSource, this, this, logger, {
|
|
132
131
|
proven: this.config.worldStateProvenBlocksOnly,
|
|
133
132
|
pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
|
|
134
133
|
batchSize: this.config.worldStateBlockRequestBatchSize,
|
|
@@ -147,10 +146,10 @@ export class ServerWorldStateSynchronizer
|
|
|
147
146
|
public async status(): Promise<WorldStateSynchronizerStatus> {
|
|
148
147
|
const summary = await this.merkleTreeDb.getStatusSummary();
|
|
149
148
|
const status: WorldStateSyncStatus = {
|
|
150
|
-
latestBlockNumber:
|
|
151
|
-
latestBlockHash: (await this.getL2BlockHash(
|
|
152
|
-
finalizedBlockNumber:
|
|
153
|
-
oldestHistoricBlockNumber:
|
|
149
|
+
latestBlockNumber: summary.unfinalizedBlockNumber,
|
|
150
|
+
latestBlockHash: (await this.getL2BlockHash(summary.unfinalizedBlockNumber)) ?? '',
|
|
151
|
+
finalizedBlockNumber: summary.finalizedBlockNumber,
|
|
152
|
+
oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
|
|
154
153
|
treesAreSynched: summary.treesAreSynched,
|
|
155
154
|
};
|
|
156
155
|
return {
|
|
@@ -184,7 +183,10 @@ export class ServerWorldStateSynchronizer
|
|
|
184
183
|
* @param skipThrowIfTargetNotReached - Whether to skip throwing if the target block number is not reached.
|
|
185
184
|
* @returns A promise that resolves with the block number the world state was synced to
|
|
186
185
|
*/
|
|
187
|
-
public async syncImmediate(
|
|
186
|
+
public async syncImmediate(
|
|
187
|
+
targetBlockNumber?: BlockNumber,
|
|
188
|
+
skipThrowIfTargetNotReached?: boolean,
|
|
189
|
+
): Promise<BlockNumber> {
|
|
188
190
|
if (this.currentState !== WorldStateRunningState.RUNNING) {
|
|
189
191
|
throw new Error(`World State is not running. Unable to perform sync.`);
|
|
190
192
|
}
|
|
@@ -202,7 +204,7 @@ export class ServerWorldStateSynchronizer
|
|
|
202
204
|
|
|
203
205
|
// If the archiver is behind the target block, force an archiver sync
|
|
204
206
|
if (targetBlockNumber) {
|
|
205
|
-
const archiverLatestBlock = await this.l2BlockSource.getBlockNumber();
|
|
207
|
+
const archiverLatestBlock = BlockNumber(await this.l2BlockSource.getBlockNumber());
|
|
206
208
|
if (archiverLatestBlock < targetBlockNumber) {
|
|
207
209
|
this.log.debug(`Archiver is at ${archiverLatestBlock} behind target block ${targetBlockNumber}.`);
|
|
208
210
|
await this.l2BlockSource.syncImmediate();
|
|
@@ -232,8 +234,8 @@ export class ServerWorldStateSynchronizer
|
|
|
232
234
|
}
|
|
233
235
|
|
|
234
236
|
/** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */
|
|
235
|
-
public async getL2BlockHash(number:
|
|
236
|
-
if (number ===
|
|
237
|
+
public async getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
|
|
238
|
+
if (number === BlockNumber.ZERO) {
|
|
237
239
|
return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
|
|
238
240
|
}
|
|
239
241
|
if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
|
|
@@ -250,13 +252,13 @@ export class ServerWorldStateSynchronizer
|
|
|
250
252
|
/** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
|
|
251
253
|
public async getL2Tips(): Promise<L2Tips> {
|
|
252
254
|
const status = await this.merkleTreeDb.getStatusSummary();
|
|
253
|
-
const unfinalizedBlockHash = await this.getL2BlockHash(
|
|
254
|
-
const latestBlockId: L2BlockId = { number:
|
|
255
|
+
const unfinalizedBlockHash = await this.getL2BlockHash(status.unfinalizedBlockNumber);
|
|
256
|
+
const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
|
|
255
257
|
|
|
256
258
|
return {
|
|
257
259
|
latest: latestBlockId,
|
|
258
|
-
finalized: { number:
|
|
259
|
-
proven: { number:
|
|
260
|
+
finalized: { number: status.finalizedBlockNumber, hash: '' },
|
|
261
|
+
proven: { number: this.provenBlockNumber ?? status.finalizedBlockNumber, hash: '' }, // TODO(palla/reorg): Using finalized as proven for now
|
|
260
262
|
};
|
|
261
263
|
}
|
|
262
264
|
|
|
@@ -264,7 +266,7 @@ export class ServerWorldStateSynchronizer
|
|
|
264
266
|
public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
|
|
265
267
|
switch (event.type) {
|
|
266
268
|
case 'blocks-added':
|
|
267
|
-
await this.handleL2Blocks(event.blocks.map(b => b.block));
|
|
269
|
+
await this.handleL2Blocks(event.blocks.map(b => b.block.toL2Block()));
|
|
268
270
|
break;
|
|
269
271
|
case 'chain-pruned':
|
|
270
272
|
await this.handleChainPruned(event.block.number);
|
|
@@ -283,22 +285,32 @@ export class ServerWorldStateSynchronizer
|
|
|
283
285
|
* @param l2Blocks - The L2 blocks to handle.
|
|
284
286
|
* @returns Whether the block handled was produced by this same node.
|
|
285
287
|
*/
|
|
286
|
-
private async handleL2Blocks(l2Blocks:
|
|
288
|
+
private async handleL2Blocks(l2Blocks: L2BlockNew[]) {
|
|
287
289
|
this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
|
|
288
290
|
|
|
289
|
-
|
|
290
|
-
const
|
|
291
|
-
|
|
291
|
+
// Fetch the L1->L2 messages for the first block in a checkpoint.
|
|
292
|
+
const messagesForBlocks = new Map<BlockNumber, Fr[]>();
|
|
293
|
+
await Promise.all(
|
|
294
|
+
l2Blocks
|
|
295
|
+
.filter(b => b.indexWithinCheckpoint === 0)
|
|
296
|
+
.map(async block => {
|
|
297
|
+
const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
|
|
298
|
+
messagesForBlocks.set(block.number, l1ToL2Messages);
|
|
299
|
+
}),
|
|
300
|
+
);
|
|
292
301
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
302
|
+
let updateStatus: WorldStateStatusFull | undefined = undefined;
|
|
303
|
+
for (const block of l2Blocks) {
|
|
304
|
+
const [duration, result] = await elapsed(() =>
|
|
305
|
+
this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []),
|
|
306
|
+
);
|
|
307
|
+
this.log.info(`World state updated with L2 block ${block.number}`, {
|
|
296
308
|
eventName: 'l2-block-handled',
|
|
297
309
|
duration,
|
|
298
|
-
unfinalizedBlockNumber: result.summary.unfinalizedBlockNumber,
|
|
299
|
-
finalizedBlockNumber: result.summary.finalizedBlockNumber,
|
|
300
|
-
oldestHistoricBlock: result.summary.oldestHistoricalBlock,
|
|
301
|
-
...
|
|
310
|
+
unfinalizedBlockNumber: BigInt(result.summary.unfinalizedBlockNumber),
|
|
311
|
+
finalizedBlockNumber: BigInt(result.summary.finalizedBlockNumber),
|
|
312
|
+
oldestHistoricBlock: BigInt(result.summary.oldestHistoricalBlock),
|
|
313
|
+
...block.getStats(),
|
|
302
314
|
} satisfies L2BlockHandledStats);
|
|
303
315
|
updateStatus = result;
|
|
304
316
|
}
|
|
@@ -314,14 +326,7 @@ export class ServerWorldStateSynchronizer
|
|
|
314
326
|
* @param l1ToL2Messages - The L1 to L2 messages for the block.
|
|
315
327
|
* @returns Whether the block handled was produced by this same node.
|
|
316
328
|
*/
|
|
317
|
-
private async handleL2Block(l2Block:
|
|
318
|
-
// First we check that the L1 to L2 messages hash to the block inHash.
|
|
319
|
-
// Note that we cannot optimize this check by checking the root of the subtree after inserting the messages
|
|
320
|
-
// to the real L1_TO_L2_MESSAGE_TREE (like we do in merkleTreeDb.handleL2BlockAndMessages(...)) because that
|
|
321
|
-
// tree uses pedersen and we don't have access to the converted root.
|
|
322
|
-
await this.verifyMessagesHashToInHash(l1ToL2Messages, l2Block.header.contentCommitment.inHash);
|
|
323
|
-
|
|
324
|
-
// If the above check succeeds, we can proceed to handle the block.
|
|
329
|
+
private async handleL2Block(l2Block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
|
|
325
330
|
this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
|
|
326
331
|
blockNumber: l2Block.number,
|
|
327
332
|
blockHash: await l2Block.hash().then(h => h.toString()),
|
|
@@ -337,30 +342,30 @@ export class ServerWorldStateSynchronizer
|
|
|
337
342
|
return result;
|
|
338
343
|
}
|
|
339
344
|
|
|
340
|
-
private async handleChainFinalized(blockNumber:
|
|
345
|
+
private async handleChainFinalized(blockNumber: BlockNumber) {
|
|
341
346
|
this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
|
|
342
|
-
const summary = await this.merkleTreeDb.setFinalized(
|
|
347
|
+
const summary = await this.merkleTreeDb.setFinalized(blockNumber);
|
|
343
348
|
if (this.historyToKeep === undefined) {
|
|
344
349
|
return;
|
|
345
350
|
}
|
|
346
|
-
const newHistoricBlock = summary.finalizedBlockNumber -
|
|
351
|
+
const newHistoricBlock = summary.finalizedBlockNumber - this.historyToKeep + 1;
|
|
347
352
|
if (newHistoricBlock <= 1) {
|
|
348
353
|
return;
|
|
349
354
|
}
|
|
350
355
|
this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
|
|
351
|
-
const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
|
|
356
|
+
const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock));
|
|
352
357
|
this.log.debug(`World state summary `, status.summary);
|
|
353
358
|
}
|
|
354
359
|
|
|
355
|
-
private handleChainProven(blockNumber:
|
|
356
|
-
this.provenBlockNumber =
|
|
360
|
+
private handleChainProven(blockNumber: BlockNumber) {
|
|
361
|
+
this.provenBlockNumber = blockNumber;
|
|
357
362
|
this.log.debug(`Proven chain is now at block ${blockNumber}`);
|
|
358
363
|
return Promise.resolve();
|
|
359
364
|
}
|
|
360
365
|
|
|
361
|
-
private async handleChainPruned(blockNumber:
|
|
366
|
+
private async handleChainPruned(blockNumber: BlockNumber) {
|
|
362
367
|
this.log.warn(`Chain pruned to block ${blockNumber}`);
|
|
363
|
-
const status = await this.merkleTreeDb.unwindBlocks(
|
|
368
|
+
const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
|
|
364
369
|
this.latestBlockHashQuery = undefined;
|
|
365
370
|
this.provenBlockNumber = undefined;
|
|
366
371
|
this.instrumentation.updateWorldStateMetrics(status);
|
|
@@ -374,24 +379,4 @@ export class ServerWorldStateSynchronizer
|
|
|
374
379
|
this.currentState = newState;
|
|
375
380
|
this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
|
|
376
381
|
}
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* Verifies that the L1 to L2 messages hash to the block inHash.
|
|
380
|
-
* @param l1ToL2Messages - The L1 to L2 messages for the block.
|
|
381
|
-
* @param inHash - The inHash of the block.
|
|
382
|
-
* @throws If the L1 to L2 messages do not hash to the block inHash.
|
|
383
|
-
*/
|
|
384
|
-
protected async verifyMessagesHashToInHash(l1ToL2Messages: Fr[], inHash: Fr) {
|
|
385
|
-
const treeCalculator = await MerkleTreeCalculator.create(
|
|
386
|
-
L1_TO_L2_MSG_SUBTREE_HEIGHT,
|
|
387
|
-
Buffer.alloc(32),
|
|
388
|
-
(lhs, rhs) => Promise.resolve(new SHA256Trunc().hash(lhs, rhs)),
|
|
389
|
-
);
|
|
390
|
-
|
|
391
|
-
const root = await treeCalculator.computeTreeRoot(l1ToL2Messages.map(msg => msg.toBuffer()));
|
|
392
|
-
|
|
393
|
-
if (!root.equals(inHash.toBuffer())) {
|
|
394
|
-
throw new Error('Obtained L1 to L2 messages failed to be hashed to the block inHash');
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
382
|
}
|