@aztec/world-state 0.0.1-commit.24de95ac → 0.0.1-commit.3469e52
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/instrumentation/instrumentation.js +11 -42
- 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 +12 -6
- package/dest/native/merkle_trees_facade.d.ts.map +1 -1
- package/dest/native/merkle_trees_facade.js +39 -8
- package/dest/native/message.d.ts +12 -11
- package/dest/native/message.d.ts.map +1 -1
- package/dest/native/message.js +14 -13
- 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 +14 -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 +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 +1 -1
- package/dest/synchronizer/index.d.ts +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.d.ts +10 -27
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.js +79 -68
- 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 -11
- package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
- package/package.json +13 -12
- package/src/instrumentation/instrumentation.ts +10 -42
- package/src/native/merkle_trees_facade.ts +42 -8
- package/src/native/message.ts +23 -22
- package/src/native/native_world_state.ts +33 -21
- package/src/native/native_world_state_instance.ts +28 -0
- package/src/synchronizer/server_world_state_synchronizer.ts +100 -97
- package/src/test/utils.ts +89 -94
- package/src/testing.ts +1 -1
- package/src/world-state-db/merkle_tree_db.ts +13 -10
|
@@ -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,
|
|
@@ -150,7 +151,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
150
151
|
return new MerkleTreesFacade(this.instance, this.initialHeader!, WorldStateRevision.empty());
|
|
151
152
|
}
|
|
152
153
|
|
|
153
|
-
public getSnapshot(blockNumber:
|
|
154
|
+
public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
|
|
154
155
|
return new MerkleTreesFacade(
|
|
155
156
|
this.instance,
|
|
156
157
|
this.initialHeader!,
|
|
@@ -158,16 +159,24 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
158
159
|
);
|
|
159
160
|
}
|
|
160
161
|
|
|
161
|
-
public async fork(
|
|
162
|
+
public async fork(
|
|
163
|
+
blockNumber?: BlockNumber,
|
|
164
|
+
opts: { closeDelayMs?: number } = {},
|
|
165
|
+
): Promise<MerkleTreeWriteOperations> {
|
|
162
166
|
const resp = await this.instance.call(WorldStateMessageType.CREATE_FORK, {
|
|
163
167
|
latest: blockNumber === undefined,
|
|
164
|
-
blockNumber: blockNumber ??
|
|
168
|
+
blockNumber: blockNumber ?? BlockNumber.ZERO,
|
|
165
169
|
canonical: true,
|
|
166
170
|
});
|
|
167
171
|
return new MerkleTreesForkFacade(
|
|
168
172
|
this.instance,
|
|
169
173
|
this.initialHeader!,
|
|
170
|
-
new WorldStateRevision(
|
|
174
|
+
new WorldStateRevision(
|
|
175
|
+
/*forkId=*/ resp.forkId,
|
|
176
|
+
/* blockNumber=*/ BlockNumber.ZERO,
|
|
177
|
+
/* includeUncommitted=*/ true,
|
|
178
|
+
),
|
|
179
|
+
opts,
|
|
171
180
|
);
|
|
172
181
|
}
|
|
173
182
|
|
|
@@ -175,21 +184,24 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
175
184
|
return this.initialHeader!;
|
|
176
185
|
}
|
|
177
186
|
|
|
178
|
-
public async handleL2BlockAndMessages(
|
|
179
|
-
l2Block
|
|
180
|
-
l1ToL2Messages
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
);
|
|
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.
|
|
189
197
|
const paddedL1ToL2Messages = isFirstBlock
|
|
190
198
|
? padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP)
|
|
191
199
|
: [];
|
|
192
200
|
|
|
201
|
+
const paddedNoteHashes = l2Block.body.txEffects.flatMap(txEffect =>
|
|
202
|
+
padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
|
|
203
|
+
);
|
|
204
|
+
|
|
193
205
|
const paddedNullifiers = l2Block.body.txEffects
|
|
194
206
|
.flatMap(txEffect => padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX))
|
|
195
207
|
.map(nullifier => new NullifierLeaf(nullifier));
|
|
@@ -208,7 +220,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
208
220
|
WorldStateMessageType.SYNC_BLOCK,
|
|
209
221
|
{
|
|
210
222
|
blockNumber: l2Block.number,
|
|
211
|
-
blockHeaderHash: await l2Block.
|
|
223
|
+
blockHeaderHash: await l2Block.hash(),
|
|
212
224
|
paddedL1ToL2Messages: paddedL1ToL2Messages.map(serializeLeaf),
|
|
213
225
|
paddedNoteHashes: paddedNoteHashes.map(serializeLeaf),
|
|
214
226
|
paddedNullifiers: paddedNullifiers.map(serializeLeaf),
|
|
@@ -256,7 +268,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
256
268
|
* @param toBlockNumber The block number that is now the tip of the finalized chain
|
|
257
269
|
* @returns The new WorldStateStatus
|
|
258
270
|
*/
|
|
259
|
-
public async setFinalized(toBlockNumber:
|
|
271
|
+
public async setFinalized(toBlockNumber: BlockNumber) {
|
|
260
272
|
try {
|
|
261
273
|
await this.instance.call(
|
|
262
274
|
WorldStateMessageType.FINALIZE_BLOCKS,
|
|
@@ -279,7 +291,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
279
291
|
* @param toBlockNumber The block number of the new oldest historical block
|
|
280
292
|
* @returns The new WorldStateStatus
|
|
281
293
|
*/
|
|
282
|
-
public async removeHistoricalBlocks(toBlockNumber:
|
|
294
|
+
public async removeHistoricalBlocks(toBlockNumber: BlockNumber) {
|
|
283
295
|
try {
|
|
284
296
|
return await this.instance.call(
|
|
285
297
|
WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
|
|
@@ -301,7 +313,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
301
313
|
* @param toBlockNumber The block number of the new tip of the pending chain,
|
|
302
314
|
* @returns The new WorldStateStatus
|
|
303
315
|
*/
|
|
304
|
-
public async unwindBlocks(toBlockNumber:
|
|
316
|
+
public async unwindBlocks(toBlockNumber: BlockNumber) {
|
|
305
317
|
try {
|
|
306
318
|
return await this.instance.call(
|
|
307
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
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type { Fr } from '@aztec/foundation/
|
|
1
|
+
import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM, INITIAL_L2_CHECKPOINT_NUM } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
4
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
5
5
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
6
6
|
import { elapsed } from '@aztec/foundation/timer';
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
L2BlockSource,
|
|
7
|
+
import {
|
|
8
|
+
GENESIS_CHECKPOINT_HEADER_HASH,
|
|
9
|
+
type L2BlockId,
|
|
10
|
+
type L2BlockNew,
|
|
11
|
+
type L2BlockSource,
|
|
12
12
|
L2BlockStream,
|
|
13
|
-
L2BlockStreamEvent,
|
|
14
|
-
L2BlockStreamEventHandler,
|
|
15
|
-
L2BlockStreamLocalDataProvider,
|
|
16
|
-
L2Tips,
|
|
13
|
+
type L2BlockStreamEvent,
|
|
14
|
+
type L2BlockStreamEventHandler,
|
|
15
|
+
type L2BlockStreamLocalDataProvider,
|
|
16
|
+
type L2Tips,
|
|
17
17
|
} from '@aztec/stdlib/block';
|
|
18
18
|
import {
|
|
19
19
|
WorldStateRunningState,
|
|
@@ -25,7 +25,7 @@ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
|
25
25
|
import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
|
|
26
26
|
import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
|
|
27
27
|
import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
28
|
-
import {
|
|
28
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
29
29
|
|
|
30
30
|
import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
|
|
31
31
|
import type { WorldStateStatusFull } from '../native/message.js';
|
|
@@ -45,17 +45,16 @@ export class ServerWorldStateSynchronizer
|
|
|
45
45
|
{
|
|
46
46
|
private readonly merkleTreeCommitted: MerkleTreeReadOperations;
|
|
47
47
|
|
|
48
|
-
private latestBlockNumberAtStart =
|
|
48
|
+
private latestBlockNumberAtStart = BlockNumber.ZERO;
|
|
49
49
|
private historyToKeep: number | undefined;
|
|
50
50
|
private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
|
|
51
|
-
private latestBlockHashQuery: { blockNumber: number; hash: string | undefined } | undefined = undefined;
|
|
52
51
|
|
|
53
52
|
private syncPromise = promiseWithResolvers<void>();
|
|
54
53
|
protected blockStream: L2BlockStream | undefined;
|
|
55
54
|
|
|
56
55
|
// WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
|
|
57
56
|
// 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:
|
|
57
|
+
private provenBlockNumber: BlockNumber | undefined;
|
|
59
58
|
|
|
60
59
|
constructor(
|
|
61
60
|
private readonly merkleTreeDb: MerkleTreeAdminDatabase,
|
|
@@ -77,12 +76,12 @@ export class ServerWorldStateSynchronizer
|
|
|
77
76
|
return this.merkleTreeDb.getCommitted();
|
|
78
77
|
}
|
|
79
78
|
|
|
80
|
-
public getSnapshot(blockNumber:
|
|
79
|
+
public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
|
|
81
80
|
return this.merkleTreeDb.getSnapshot(blockNumber);
|
|
82
81
|
}
|
|
83
82
|
|
|
84
|
-
public fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
|
|
85
|
-
return this.merkleTreeDb.fork(blockNumber);
|
|
83
|
+
public fork(blockNumber?: BlockNumber, opts?: { closeDelayMs?: number }): Promise<MerkleTreeWriteOperations> {
|
|
84
|
+
return this.merkleTreeDb.fork(blockNumber, opts);
|
|
86
85
|
}
|
|
87
86
|
|
|
88
87
|
public backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>> {
|
|
@@ -102,9 +101,11 @@ export class ServerWorldStateSynchronizer
|
|
|
102
101
|
}
|
|
103
102
|
|
|
104
103
|
// Get the current latest block number
|
|
105
|
-
this.latestBlockNumberAtStart =
|
|
106
|
-
|
|
107
|
-
|
|
104
|
+
this.latestBlockNumberAtStart = BlockNumber(
|
|
105
|
+
await (this.config.worldStateProvenBlocksOnly
|
|
106
|
+
? this.l2BlockSource.getProvenBlockNumber()
|
|
107
|
+
: this.l2BlockSource.getBlockNumber()),
|
|
108
|
+
);
|
|
108
109
|
|
|
109
110
|
const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
|
|
110
111
|
|
|
@@ -126,12 +127,12 @@ export class ServerWorldStateSynchronizer
|
|
|
126
127
|
}
|
|
127
128
|
|
|
128
129
|
protected createBlockStream(): L2BlockStream {
|
|
129
|
-
const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
|
|
130
130
|
const logger = createLogger('world-state:block_stream');
|
|
131
|
-
return new
|
|
131
|
+
return new L2BlockStream(this.l2BlockSource, this, this, logger, {
|
|
132
132
|
proven: this.config.worldStateProvenBlocksOnly,
|
|
133
133
|
pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
|
|
134
134
|
batchSize: this.config.worldStateBlockRequestBatchSize,
|
|
135
|
+
ignoreCheckpoints: true,
|
|
135
136
|
});
|
|
136
137
|
}
|
|
137
138
|
|
|
@@ -147,10 +148,10 @@ export class ServerWorldStateSynchronizer
|
|
|
147
148
|
public async status(): Promise<WorldStateSynchronizerStatus> {
|
|
148
149
|
const summary = await this.merkleTreeDb.getStatusSummary();
|
|
149
150
|
const status: WorldStateSyncStatus = {
|
|
150
|
-
latestBlockNumber:
|
|
151
|
-
latestBlockHash: (await this.getL2BlockHash(
|
|
152
|
-
finalizedBlockNumber:
|
|
153
|
-
oldestHistoricBlockNumber:
|
|
151
|
+
latestBlockNumber: summary.unfinalizedBlockNumber,
|
|
152
|
+
latestBlockHash: (await this.getL2BlockHash(summary.unfinalizedBlockNumber)) ?? '',
|
|
153
|
+
finalizedBlockNumber: summary.finalizedBlockNumber,
|
|
154
|
+
oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
|
|
154
155
|
treesAreSynched: summary.treesAreSynched,
|
|
155
156
|
};
|
|
156
157
|
return {
|
|
@@ -160,7 +161,7 @@ export class ServerWorldStateSynchronizer
|
|
|
160
161
|
}
|
|
161
162
|
|
|
162
163
|
public async getLatestBlockNumber() {
|
|
163
|
-
return (await this.getL2Tips()).
|
|
164
|
+
return (await this.getL2Tips()).proposed.number;
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
public async stopSync() {
|
|
@@ -184,7 +185,10 @@ export class ServerWorldStateSynchronizer
|
|
|
184
185
|
* @param skipThrowIfTargetNotReached - Whether to skip throwing if the target block number is not reached.
|
|
185
186
|
* @returns A promise that resolves with the block number the world state was synced to
|
|
186
187
|
*/
|
|
187
|
-
public async syncImmediate(
|
|
188
|
+
public async syncImmediate(
|
|
189
|
+
targetBlockNumber?: BlockNumber,
|
|
190
|
+
skipThrowIfTargetNotReached?: boolean,
|
|
191
|
+
): Promise<BlockNumber> {
|
|
188
192
|
if (this.currentState !== WorldStateRunningState.RUNNING) {
|
|
189
193
|
throw new Error(`World State is not running. Unable to perform sync.`);
|
|
190
194
|
}
|
|
@@ -202,7 +206,7 @@ export class ServerWorldStateSynchronizer
|
|
|
202
206
|
|
|
203
207
|
// If the archiver is behind the target block, force an archiver sync
|
|
204
208
|
if (targetBlockNumber) {
|
|
205
|
-
const archiverLatestBlock = await this.l2BlockSource.getBlockNumber();
|
|
209
|
+
const archiverLatestBlock = BlockNumber(await this.l2BlockSource.getBlockNumber());
|
|
206
210
|
if (archiverLatestBlock < targetBlockNumber) {
|
|
207
211
|
this.log.debug(`Archiver is at ${archiverLatestBlock} behind target block ${targetBlockNumber}.`);
|
|
208
212
|
await this.l2BlockSource.syncImmediate();
|
|
@@ -232,31 +236,48 @@ export class ServerWorldStateSynchronizer
|
|
|
232
236
|
}
|
|
233
237
|
|
|
234
238
|
/** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */
|
|
235
|
-
public async getL2BlockHash(number:
|
|
236
|
-
if (number ===
|
|
239
|
+
public async getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
|
|
240
|
+
if (number === BlockNumber.ZERO) {
|
|
237
241
|
return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
|
|
238
242
|
}
|
|
239
|
-
|
|
240
|
-
this.latestBlockHashQuery = {
|
|
241
|
-
hash: await this.merkleTreeCommitted
|
|
242
|
-
.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number))
|
|
243
|
-
.then(leaf => leaf?.toString()),
|
|
244
|
-
blockNumber: number,
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
return this.latestBlockHashQuery.hash;
|
|
243
|
+
return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then(leaf => leaf?.toString());
|
|
248
244
|
}
|
|
249
245
|
|
|
250
246
|
/** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
|
|
251
247
|
public async getL2Tips(): Promise<L2Tips> {
|
|
252
248
|
const status = await this.merkleTreeDb.getStatusSummary();
|
|
253
|
-
const
|
|
254
|
-
const
|
|
255
|
-
|
|
249
|
+
const unfinalizedBlockHashPromise = this.getL2BlockHash(status.unfinalizedBlockNumber);
|
|
250
|
+
const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
|
|
251
|
+
|
|
252
|
+
const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
|
|
253
|
+
const provenBlockHashPromise =
|
|
254
|
+
this.provenBlockNumber === undefined ? finalizedBlockHashPromise : this.getL2BlockHash(this.provenBlockNumber);
|
|
255
|
+
|
|
256
|
+
const [unfinalizedBlockHash, finalizedBlockHash, provenBlockHash] = await Promise.all([
|
|
257
|
+
unfinalizedBlockHashPromise,
|
|
258
|
+
finalizedBlockHashPromise,
|
|
259
|
+
provenBlockHashPromise,
|
|
260
|
+
]);
|
|
261
|
+
const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
|
|
262
|
+
|
|
263
|
+
// World state doesn't track checkpointed blocks or checkpoints themselves.
|
|
264
|
+
// but we use a block stream so we need to provide 'local' L2Tips.
|
|
265
|
+
// We configure the block stream to ignore checkpoints and set checkpoint values to genesis here.
|
|
266
|
+
const genesisCheckpointHeaderHash = GENESIS_CHECKPOINT_HEADER_HASH.toString();
|
|
256
267
|
return {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
268
|
+
proposed: latestBlockId,
|
|
269
|
+
checkpointed: {
|
|
270
|
+
block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
|
|
271
|
+
checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
|
|
272
|
+
},
|
|
273
|
+
finalized: {
|
|
274
|
+
block: { number: status.finalizedBlockNumber, hash: finalizedBlockHash ?? '' },
|
|
275
|
+
checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
|
|
276
|
+
},
|
|
277
|
+
proven: {
|
|
278
|
+
block: { number: provenBlockNumber, hash: provenBlockHash ?? '' },
|
|
279
|
+
checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
|
|
280
|
+
},
|
|
260
281
|
};
|
|
261
282
|
}
|
|
262
283
|
|
|
@@ -264,7 +285,7 @@ export class ServerWorldStateSynchronizer
|
|
|
264
285
|
public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
|
|
265
286
|
switch (event.type) {
|
|
266
287
|
case 'blocks-added':
|
|
267
|
-
await this.handleL2Blocks(event.blocks
|
|
288
|
+
await this.handleL2Blocks(event.blocks);
|
|
268
289
|
break;
|
|
269
290
|
case 'chain-pruned':
|
|
270
291
|
await this.handleChainPruned(event.block.number);
|
|
@@ -283,22 +304,32 @@ export class ServerWorldStateSynchronizer
|
|
|
283
304
|
* @param l2Blocks - The L2 blocks to handle.
|
|
284
305
|
* @returns Whether the block handled was produced by this same node.
|
|
285
306
|
*/
|
|
286
|
-
private async handleL2Blocks(l2Blocks:
|
|
307
|
+
private async handleL2Blocks(l2Blocks: L2BlockNew[]) {
|
|
287
308
|
this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
|
|
288
309
|
|
|
289
|
-
|
|
290
|
-
const
|
|
291
|
-
|
|
310
|
+
// Fetch the L1->L2 messages for the first block in a checkpoint.
|
|
311
|
+
const messagesForBlocks = new Map<BlockNumber, Fr[]>();
|
|
312
|
+
await Promise.all(
|
|
313
|
+
l2Blocks
|
|
314
|
+
.filter(b => b.indexWithinCheckpoint === 0)
|
|
315
|
+
.map(async block => {
|
|
316
|
+
const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
|
|
317
|
+
messagesForBlocks.set(block.number, l1ToL2Messages);
|
|
318
|
+
}),
|
|
319
|
+
);
|
|
292
320
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
321
|
+
let updateStatus: WorldStateStatusFull | undefined = undefined;
|
|
322
|
+
for (const block of l2Blocks) {
|
|
323
|
+
const [duration, result] = await elapsed(() =>
|
|
324
|
+
this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []),
|
|
325
|
+
);
|
|
326
|
+
this.log.info(`World state updated with L2 block ${block.number}`, {
|
|
296
327
|
eventName: 'l2-block-handled',
|
|
297
328
|
duration,
|
|
298
|
-
unfinalizedBlockNumber: result.summary.unfinalizedBlockNumber,
|
|
299
|
-
finalizedBlockNumber: result.summary.finalizedBlockNumber,
|
|
300
|
-
oldestHistoricBlock: result.summary.oldestHistoricalBlock,
|
|
301
|
-
...
|
|
329
|
+
unfinalizedBlockNumber: BigInt(result.summary.unfinalizedBlockNumber),
|
|
330
|
+
finalizedBlockNumber: BigInt(result.summary.finalizedBlockNumber),
|
|
331
|
+
oldestHistoricBlock: BigInt(result.summary.oldestHistoricalBlock),
|
|
332
|
+
...block.getStats(),
|
|
302
333
|
} satisfies L2BlockHandledStats);
|
|
303
334
|
updateStatus = result;
|
|
304
335
|
}
|
|
@@ -314,14 +345,7 @@ export class ServerWorldStateSynchronizer
|
|
|
314
345
|
* @param l1ToL2Messages - The L1 to L2 messages for the block.
|
|
315
346
|
* @returns Whether the block handled was produced by this same node.
|
|
316
347
|
*/
|
|
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.
|
|
348
|
+
private async handleL2Block(l2Block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
|
|
325
349
|
this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
|
|
326
350
|
blockNumber: l2Block.number,
|
|
327
351
|
blockHash: await l2Block.hash().then(h => h.toString()),
|
|
@@ -337,31 +361,30 @@ export class ServerWorldStateSynchronizer
|
|
|
337
361
|
return result;
|
|
338
362
|
}
|
|
339
363
|
|
|
340
|
-
private async handleChainFinalized(blockNumber:
|
|
364
|
+
private async handleChainFinalized(blockNumber: BlockNumber) {
|
|
341
365
|
this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
|
|
342
|
-
const summary = await this.merkleTreeDb.setFinalized(
|
|
366
|
+
const summary = await this.merkleTreeDb.setFinalized(blockNumber);
|
|
343
367
|
if (this.historyToKeep === undefined) {
|
|
344
368
|
return;
|
|
345
369
|
}
|
|
346
|
-
const newHistoricBlock = summary.finalizedBlockNumber -
|
|
370
|
+
const newHistoricBlock = summary.finalizedBlockNumber - this.historyToKeep + 1;
|
|
347
371
|
if (newHistoricBlock <= 1) {
|
|
348
372
|
return;
|
|
349
373
|
}
|
|
350
374
|
this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
|
|
351
|
-
const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
|
|
375
|
+
const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock));
|
|
352
376
|
this.log.debug(`World state summary `, status.summary);
|
|
353
377
|
}
|
|
354
378
|
|
|
355
|
-
private handleChainProven(blockNumber:
|
|
356
|
-
this.provenBlockNumber =
|
|
379
|
+
private handleChainProven(blockNumber: BlockNumber) {
|
|
380
|
+
this.provenBlockNumber = blockNumber;
|
|
357
381
|
this.log.debug(`Proven chain is now at block ${blockNumber}`);
|
|
358
382
|
return Promise.resolve();
|
|
359
383
|
}
|
|
360
384
|
|
|
361
|
-
private async handleChainPruned(blockNumber:
|
|
385
|
+
private async handleChainPruned(blockNumber: BlockNumber) {
|
|
362
386
|
this.log.warn(`Chain pruned to block ${blockNumber}`);
|
|
363
|
-
const status = await this.merkleTreeDb.unwindBlocks(
|
|
364
|
-
this.latestBlockHashQuery = undefined;
|
|
387
|
+
const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
|
|
365
388
|
this.provenBlockNumber = undefined;
|
|
366
389
|
this.instrumentation.updateWorldStateMetrics(status);
|
|
367
390
|
}
|
|
@@ -374,24 +397,4 @@ export class ServerWorldStateSynchronizer
|
|
|
374
397
|
this.currentState = newState;
|
|
375
398
|
this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
|
|
376
399
|
}
|
|
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
400
|
}
|