@aztec-labs/world-state 6.0.0 → 6.1.0-nightly.20261023
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/synchronizer/server_world_state_synchronizer.d.ts +11 -2
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.js +52 -11
- package/package.json +9 -9
- package/src/synchronizer/server_world_state_synchronizer.ts +60 -11
|
@@ -34,6 +34,8 @@ export declare class ServerWorldStateSynchronizer implements WorldStateSynchroni
|
|
|
34
34
|
private pruningPaused;
|
|
35
35
|
private stopping;
|
|
36
36
|
private stopPromise;
|
|
37
|
+
private readonly blockHashCache;
|
|
38
|
+
private blockHashCacheGeneration;
|
|
37
39
|
constructor(merkleTreeDb: MerkleTreeAdminDatabase, l2BlockSource: L2BlockSource & L1ToL2MessageSource, config: WorldStateConfig, instrumentation?: WorldStateInstrumentation, log?: Logger);
|
|
38
40
|
getCommitted(): MerkleTreeReadOperations;
|
|
39
41
|
getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations;
|
|
@@ -58,8 +60,15 @@ export declare class ServerWorldStateSynchronizer implements WorldStateSynchroni
|
|
|
58
60
|
* @returns A promise that resolves with the block number the world state was synced to
|
|
59
61
|
*/
|
|
60
62
|
syncImmediate(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber>;
|
|
61
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* Returns the L2 block hash for a given number, served from memory when it was read before. Used by the
|
|
65
|
+
* L2BlockStream for detecting reorgs.
|
|
66
|
+
*/
|
|
62
67
|
getL2BlockHash(number: BlockNumber): Promise<string | undefined>;
|
|
68
|
+
private readL2BlockHash;
|
|
69
|
+
private readAndCacheL2BlockHash;
|
|
70
|
+
private invalidateBlockHashCache;
|
|
71
|
+
private withBlockHashCacheInvalidation;
|
|
63
72
|
/**
|
|
64
73
|
* Returns the proposed, proven, and finalized block tips of the chain. World state drives its block stream with
|
|
65
74
|
* `ignoreCheckpoints`, so it does not track checkpointed blocks or checkpoints and omits `checkpointed` from the tips
|
|
@@ -89,4 +98,4 @@ export declare class ServerWorldStateSynchronizer implements WorldStateSynchroni
|
|
|
89
98
|
private startPruning;
|
|
90
99
|
private pruneHistory;
|
|
91
100
|
}
|
|
92
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
101
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VydmVyX3dvcmxkX3N0YXRlX3N5bmNocm9uaXplci5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3N5bmNocm9uaXplci9zZXJ2ZXJfd29ybGRfc3RhdGVfc3luY2hyb25pemVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFBRSxXQUFXLEVBQW9CLE1BQU0sc0NBQXNDLENBQUM7QUFFckYsT0FBTyxLQUFLLEVBQUUsRUFBRSxFQUFFLE1BQU0scUNBQXFDLENBQUM7QUFDOUQsT0FBTyxFQUFFLEtBQUssTUFBTSxFQUFnQixNQUFNLDRCQUE0QixDQUFDO0FBS3ZFLE9BQU8sRUFDTCxLQUFLLFNBQVMsRUFDZCx3QkFBd0IsRUFDeEIsS0FBSyxPQUFPLEVBQ1osS0FBSyxhQUFhLEVBQ2xCLEtBQUssa0JBQWtCLEVBQ3ZCLEtBQUsseUJBQXlCLEVBQzlCLEtBQUssOEJBQThCLEVBQ25DLEtBQUssY0FBYyxFQUNwQixNQUFNLDBCQUEwQixDQUFDO0FBQ2xDLE9BQU8sRUFHTCxLQUFLLHNCQUFzQixFQUMzQixLQUFLLDRCQUE0QixFQUNsQyxNQUFNLHNDQUFzQyxDQUFDO0FBQzlDLE9BQU8sS0FBSyxFQUFFLG1CQUFtQixFQUFFLE1BQU0sOEJBQThCLENBQUM7QUFDeEUsT0FBTyxLQUFLLEVBQUUsZ0J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1lBTVgsaUJBQWlCO0lBUy9COzs7T0FHRztJQUNILE9BQU8sQ0FBQyxlQUFlO0lBS3ZCLE9BQU8sQ0FBQyxZQUFZO1lBT04sWUFBWTtDQTBDM0IifQ==
|
|
@@ -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,EAAE,WAAW,EAAoB,MAAM,sCAAsC,CAAC;
|
|
1
|
+
{"version":3,"file":"server_world_state_synchronizer.d.ts","sourceRoot":"","sources":["../../src/synchronizer/server_world_state_synchronizer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAoB,MAAM,sCAAsC,CAAC;AAErF,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,qCAAqC,CAAC;AAC9D,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,4BAA4B,CAAC;AAKvE,OAAO,EACL,KAAK,SAAS,EACd,wBAAwB,EACxB,KAAK,OAAO,EACZ,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,yBAAyB,EAC9B,KAAK,8BAA8B,EACnC,KAAK,cAAc,EACpB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAGL,KAAK,sBAAsB,EAC3B,KAAK,4BAA4B,EAClC,MAAM,sCAAsC,CAAC;AAC9C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACxE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAErE,OAAO,EAAgB,KAAK,wBAAwB,EAAE,KAAK,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAGvH,OAAO,EAAE,yBAAyB,EAAE,MAAM,uCAAuC,CAAC;AAClF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACjE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,qCAAqC,CAAC;AACnF,O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C,OAAO,CAAC,iBAAiB;YAMX,iBAAiB;IAS/B;;;OAGG;IACH,OAAO,CAAC,eAAe;IAKvB,OAAO,CAAC,YAAY;YAON,YAAY;CA0C3B"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec-labs/constants';
|
|
2
2
|
import { BlockNumber, CheckpointNumber } from '@aztec-labs/foundation/branded-types';
|
|
3
|
+
import { LruMap } from '@aztec-labs/foundation/collection';
|
|
3
4
|
import { createLogger } from '@aztec-labs/foundation/log';
|
|
4
5
|
import { promiseWithResolvers } from '@aztec-labs/foundation/promise';
|
|
5
6
|
import { SerialQueue } from '@aztec-labs/foundation/queue';
|
|
@@ -14,6 +15,8 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
14
15
|
import { validateWorldStateHistory } from './history.js';
|
|
15
16
|
// four checkpoint's worth of blocks
|
|
16
17
|
const MAX_BLOCK_PRUNE = 40;
|
|
18
|
+
// The block stream only reads hashes near the tips, so a small bound is enough to keep the cache from growing unbounded.
|
|
19
|
+
const MAX_CACHED_BLOCK_HASHES = 64;
|
|
17
20
|
/**
|
|
18
21
|
* Synchronizes the world state with the L2 blocks from a L2BlockSource via a block stream.
|
|
19
22
|
* The synchronizer will download the L2 blocks from the L2BlockSource and update the merkle trees.
|
|
@@ -38,6 +41,11 @@ const MAX_BLOCK_PRUNE = 40;
|
|
|
38
41
|
pruningPaused;
|
|
39
42
|
stopping;
|
|
40
43
|
stopPromise;
|
|
44
|
+
// Archive hashes the block stream reads for its tips and reorg checks, keyed by block number. Committed archive leaves
|
|
45
|
+
// only change through this class (unwind, clear, history pruning), each of which bumps the generation and empties the
|
|
46
|
+
// cache, so a read that overlaps one of those mutations is never cached.
|
|
47
|
+
blockHashCache;
|
|
48
|
+
blockHashCacheGeneration;
|
|
41
49
|
constructor(merkleTreeDb, l2BlockSource, config, instrumentation = new WorldStateInstrumentation(getTelemetryClient()), log = createLogger('world_state')){
|
|
42
50
|
this.merkleTreeDb = merkleTreeDb;
|
|
43
51
|
this.l2BlockSource = l2BlockSource;
|
|
@@ -51,6 +59,8 @@ const MAX_BLOCK_PRUNE = 40;
|
|
|
51
59
|
this.pruningSleep = new InterruptibleSleep();
|
|
52
60
|
this.pruningPaused = false;
|
|
53
61
|
this.stopping = false;
|
|
62
|
+
this.blockHashCache = new LruMap(MAX_CACHED_BLOCK_HASHES);
|
|
63
|
+
this.blockHashCacheGeneration = 0;
|
|
54
64
|
this.mutationQueue.start();
|
|
55
65
|
this.log.info('Created world state synchronizer', {
|
|
56
66
|
historyRetention: config.worldStateHistoryRetention
|
|
@@ -107,7 +117,7 @@ const MAX_BLOCK_PRUNE = 40;
|
|
|
107
117
|
try {
|
|
108
118
|
await this.mutationQueue.put(async ()=>{
|
|
109
119
|
this.pruningCutoff = undefined;
|
|
110
|
-
await this.merkleTreeDb.clear();
|
|
120
|
+
await this.withBlockHashCacheInvalidation(()=>this.merkleTreeDb.clear());
|
|
111
121
|
});
|
|
112
122
|
} finally{
|
|
113
123
|
if (!wasPaused && !this.stopping) {
|
|
@@ -178,7 +188,7 @@ const MAX_BLOCK_PRUNE = 40;
|
|
|
178
188
|
const summary = await this.merkleTreeDb.getStatusSummary();
|
|
179
189
|
const status = {
|
|
180
190
|
latestBlockNumber: summary.unfinalizedBlockNumber,
|
|
181
|
-
latestBlockHash: await this.
|
|
191
|
+
latestBlockHash: await this.readL2BlockHash(summary.unfinalizedBlockNumber) ?? '',
|
|
182
192
|
finalizedBlockNumber: summary.finalizedBlockNumber,
|
|
183
193
|
oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
|
|
184
194
|
treesAreSynched: summary.treesAreSynched
|
|
@@ -231,7 +241,7 @@ const MAX_BLOCK_PRUNE = 40;
|
|
|
231
241
|
return currentBlockNumber;
|
|
232
242
|
}
|
|
233
243
|
// If a block hash was provided, verify we're on the expected fork
|
|
234
|
-
const currentHash = await this.
|
|
244
|
+
const currentHash = await this.readL2BlockHash(targetBlockNumber);
|
|
235
245
|
if (currentHash === blockHash.toString()) {
|
|
236
246
|
return currentBlockNumber;
|
|
237
247
|
}
|
|
@@ -263,7 +273,7 @@ const MAX_BLOCK_PRUNE = 40;
|
|
|
263
273
|
}
|
|
264
274
|
// If a block hash was provided, verify we're on the expected fork after syncing, throw otherwise
|
|
265
275
|
if (blockHash !== undefined && targetBlockNumber !== undefined) {
|
|
266
|
-
const updatedHash = await this.
|
|
276
|
+
const updatedHash = await this.readL2BlockHash(targetBlockNumber);
|
|
267
277
|
if (updatedHash !== blockHash.toString()) {
|
|
268
278
|
throw new WorldStateSynchronizerError(`Block hash mismatch at block ${targetBlockNumber} (expected ${blockHash} but got ${updatedHash})`, {
|
|
269
279
|
cause: {
|
|
@@ -277,12 +287,40 @@ const MAX_BLOCK_PRUNE = 40;
|
|
|
277
287
|
}
|
|
278
288
|
return updatedBlockNumber;
|
|
279
289
|
}
|
|
280
|
-
/**
|
|
290
|
+
/**
|
|
291
|
+
* Returns the L2 block hash for a given number, served from memory when it was read before. Used by the
|
|
292
|
+
* L2BlockStream for detecting reorgs.
|
|
293
|
+
*/ async getL2BlockHash(number) {
|
|
294
|
+
return this.blockHashCache.get(number) ?? await this.readAndCacheL2BlockHash(number);
|
|
295
|
+
}
|
|
296
|
+
/** Reads the L2 block hash for a given number from the committed archive tree. */ async readL2BlockHash(number) {
|
|
281
297
|
// Block 0 is read from the archive as well (its first leaf is the initial header hash, which init asserts) rather
|
|
282
|
-
// than from the in-memory initial header, so
|
|
283
|
-
//
|
|
298
|
+
// than from the in-memory initial header, so a node idling at genesis still reaches wsdb on every block stream poll
|
|
299
|
+
// (see getL2Tips). The committed view is fetched per call because clear() replaces the instance.
|
|
284
300
|
const leaf = await this.merkleTreeDb.getCommitted().getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number));
|
|
285
|
-
|
|
301
|
+
// Native wsdb returns zero for unwritten leaves; even the genesis archive leaf is nonzero.
|
|
302
|
+
return leaf && !leaf.isZero() ? leaf.toString() : undefined;
|
|
303
|
+
}
|
|
304
|
+
async readAndCacheL2BlockHash(number) {
|
|
305
|
+
const generation = this.blockHashCacheGeneration;
|
|
306
|
+
const hash = await this.readL2BlockHash(number);
|
|
307
|
+
// A missing leaf is not cached: the block may be added later without any invalidation.
|
|
308
|
+
if (hash !== undefined && generation === this.blockHashCacheGeneration) {
|
|
309
|
+
this.blockHashCache.set(number, hash);
|
|
310
|
+
}
|
|
311
|
+
return hash;
|
|
312
|
+
}
|
|
313
|
+
invalidateBlockHashCache() {
|
|
314
|
+
this.blockHashCacheGeneration++;
|
|
315
|
+
this.blockHashCache.clear();
|
|
316
|
+
}
|
|
317
|
+
/** Runs a mutation that can change committed archive leaves, invalidating the block hash cache around it. */ async withBlockHashCacheInvalidation(fn) {
|
|
318
|
+
this.invalidateBlockHashCache();
|
|
319
|
+
try {
|
|
320
|
+
return await fn();
|
|
321
|
+
} finally{
|
|
322
|
+
this.invalidateBlockHashCache();
|
|
323
|
+
}
|
|
286
324
|
}
|
|
287
325
|
/**
|
|
288
326
|
* Returns the proposed, proven, and finalized block tips of the chain. World state drives its block stream with
|
|
@@ -290,7 +328,10 @@ const MAX_BLOCK_PRUNE = 40;
|
|
|
290
328
|
* it reports.
|
|
291
329
|
*/ async getL2Tips() {
|
|
292
330
|
const status = await this.merkleTreeDb.getStatusSummary();
|
|
293
|
-
|
|
331
|
+
// The status summary is cached in memory, so the proposed tip hash is always read from wsdb: that keeps one native
|
|
332
|
+
// round trip per block stream poll, so the node notices when wsdb dies. It also refreshes the cache entry that the
|
|
333
|
+
// walk-back reads right after. The finalized and proven hashes come from the cache.
|
|
334
|
+
const unfinalizedBlockHashPromise = this.readAndCacheL2BlockHash(status.unfinalizedBlockNumber);
|
|
294
335
|
const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
|
|
295
336
|
const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
|
|
296
337
|
const provenBlockHashPromise = this.provenBlockNumber === undefined ? finalizedBlockHashPromise : this.getL2BlockHash(this.provenBlockNumber);
|
|
@@ -475,7 +516,7 @@ const MAX_BLOCK_PRUNE = 40;
|
|
|
475
516
|
}
|
|
476
517
|
async handleChainPruned(blockNumber) {
|
|
477
518
|
this.log.info(`Chain pruned to block ${blockNumber}`);
|
|
478
|
-
const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
|
|
519
|
+
const status = await this.withBlockHashCacheInvalidation(()=>this.merkleTreeDb.unwindBlocks(blockNumber));
|
|
479
520
|
if (this.provenBlockNumber !== undefined && this.provenBlockNumber > blockNumber) {
|
|
480
521
|
this.provenBlockNumber = undefined;
|
|
481
522
|
}
|
|
@@ -512,7 +553,7 @@ const MAX_BLOCK_PRUNE = 40;
|
|
|
512
553
|
}
|
|
513
554
|
// Native history starts at block 1 even when the summary still reports the genesis sentinel 0.
|
|
514
555
|
const nextBlock = BlockNumber(Math.min(cutoff, Math.max(1, summary.oldestHistoricalBlock) + MAX_BLOCK_PRUNE));
|
|
515
|
-
const status = await this.merkleTreeDb.removeHistoricalBlocks(nextBlock);
|
|
556
|
+
const status = await this.withBlockHashCacheInvalidation(()=>this.merkleTreeDb.removeHistoricalBlocks(nextBlock));
|
|
516
557
|
if (status.summary.oldestHistoricalBlock <= summary.oldestHistoricalBlock) {
|
|
517
558
|
throw new Error('Historical pruning did not advance the oldest block');
|
|
518
559
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec-labs/world-state",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.1.0-nightly.20261023",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/index.js",
|
|
@@ -64,19 +64,19 @@
|
|
|
64
64
|
]
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@aztec-foundation/wsdb": "6.0.0",
|
|
68
|
-
"@aztec-labs/constants": "6.0.
|
|
69
|
-
"@aztec-labs/foundation": "6.0.
|
|
70
|
-
"@aztec-labs/protocol-contracts": "6.0.
|
|
71
|
-
"@aztec-labs/stdlib": "6.0.
|
|
72
|
-
"@aztec-labs/telemetry-client": "6.0.
|
|
67
|
+
"@aztec-foundation/wsdb": "6.0.0-rc.7",
|
|
68
|
+
"@aztec-labs/constants": "6.1.0-nightly.20261023",
|
|
69
|
+
"@aztec-labs/foundation": "6.1.0-nightly.20261023",
|
|
70
|
+
"@aztec-labs/protocol-contracts": "6.1.0-nightly.20261023",
|
|
71
|
+
"@aztec-labs/stdlib": "6.1.0-nightly.20261023",
|
|
72
|
+
"@aztec-labs/telemetry-client": "6.1.0-nightly.20261023",
|
|
73
73
|
"msgpackr": "^1.11.2",
|
|
74
74
|
"tslib": "^2.4.0",
|
|
75
75
|
"zod": "~4.4.3"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@aztec-foundation/ipc-runtime": "6.0.0",
|
|
79
|
-
"@aztec-labs/archiver": "6.0.
|
|
78
|
+
"@aztec-foundation/ipc-runtime": "6.0.0-rc.7",
|
|
79
|
+
"@aztec-labs/archiver": "6.1.0-nightly.20261023",
|
|
80
80
|
"@jest/globals": "^30.0.0",
|
|
81
81
|
"@types/jest": "^30.0.0",
|
|
82
82
|
"@types/node": "^22.15.17",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec-labs/constants';
|
|
2
2
|
import { BlockNumber, CheckpointNumber } from '@aztec-labs/foundation/branded-types';
|
|
3
|
+
import { LruMap } from '@aztec-labs/foundation/collection';
|
|
3
4
|
import type { Fr } from '@aztec-labs/foundation/curves/bn254';
|
|
4
5
|
import { type Logger, createLogger } from '@aztec-labs/foundation/log';
|
|
5
6
|
import { promiseWithResolvers } from '@aztec-labs/foundation/promise';
|
|
@@ -40,6 +41,9 @@ export type { SnapshotDataKeys };
|
|
|
40
41
|
// four checkpoint's worth of blocks
|
|
41
42
|
const MAX_BLOCK_PRUNE = 40;
|
|
42
43
|
|
|
44
|
+
// The block stream only reads hashes near the tips, so a small bound is enough to keep the cache from growing unbounded.
|
|
45
|
+
const MAX_CACHED_BLOCK_HASHES = 64;
|
|
46
|
+
|
|
43
47
|
/**
|
|
44
48
|
* Synchronizes the world state with the L2 blocks from a L2BlockSource via a block stream.
|
|
45
49
|
* The synchronizer will download the L2 blocks from the L2BlockSource and update the merkle trees.
|
|
@@ -66,6 +70,12 @@ export class ServerWorldStateSynchronizer
|
|
|
66
70
|
private stopping = false;
|
|
67
71
|
private stopPromise: Promise<void> | undefined;
|
|
68
72
|
|
|
73
|
+
// Archive hashes the block stream reads for its tips and reorg checks, keyed by block number. Committed archive leaves
|
|
74
|
+
// only change through this class (unwind, clear, history pruning), each of which bumps the generation and empties the
|
|
75
|
+
// cache, so a read that overlaps one of those mutations is never cached.
|
|
76
|
+
private readonly blockHashCache = new LruMap<number, string>(MAX_CACHED_BLOCK_HASHES);
|
|
77
|
+
private blockHashCacheGeneration = 0;
|
|
78
|
+
|
|
69
79
|
constructor(
|
|
70
80
|
private readonly merkleTreeDb: MerkleTreeAdminDatabase,
|
|
71
81
|
private readonly l2BlockSource: L2BlockSource & L1ToL2MessageSource,
|
|
@@ -139,7 +149,7 @@ export class ServerWorldStateSynchronizer
|
|
|
139
149
|
try {
|
|
140
150
|
await this.mutationQueue.put(async () => {
|
|
141
151
|
this.pruningCutoff = undefined;
|
|
142
|
-
await this.merkleTreeDb.clear();
|
|
152
|
+
await this.withBlockHashCacheInvalidation(() => this.merkleTreeDb.clear());
|
|
143
153
|
});
|
|
144
154
|
} finally {
|
|
145
155
|
if (!wasPaused && !this.stopping) {
|
|
@@ -222,7 +232,7 @@ export class ServerWorldStateSynchronizer
|
|
|
222
232
|
const summary = await this.merkleTreeDb.getStatusSummary();
|
|
223
233
|
const status: WorldStateSyncStatus = {
|
|
224
234
|
latestBlockNumber: summary.unfinalizedBlockNumber,
|
|
225
|
-
latestBlockHash: (await this.
|
|
235
|
+
latestBlockHash: (await this.readL2BlockHash(summary.unfinalizedBlockNumber)) ?? '',
|
|
226
236
|
finalizedBlockNumber: summary.finalizedBlockNumber,
|
|
227
237
|
oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
|
|
228
238
|
treesAreSynched: summary.treesAreSynched,
|
|
@@ -283,7 +293,7 @@ export class ServerWorldStateSynchronizer
|
|
|
283
293
|
}
|
|
284
294
|
|
|
285
295
|
// If a block hash was provided, verify we're on the expected fork
|
|
286
|
-
const currentHash = await this.
|
|
296
|
+
const currentHash = await this.readL2BlockHash(targetBlockNumber);
|
|
287
297
|
if (currentHash === blockHash.toString()) {
|
|
288
298
|
return currentBlockNumber;
|
|
289
299
|
}
|
|
@@ -324,7 +334,7 @@ export class ServerWorldStateSynchronizer
|
|
|
324
334
|
|
|
325
335
|
// If a block hash was provided, verify we're on the expected fork after syncing, throw otherwise
|
|
326
336
|
if (blockHash !== undefined && targetBlockNumber !== undefined) {
|
|
327
|
-
const updatedHash = await this.
|
|
337
|
+
const updatedHash = await this.readL2BlockHash(targetBlockNumber);
|
|
328
338
|
if (updatedHash !== blockHash.toString()) {
|
|
329
339
|
throw new WorldStateSynchronizerError(
|
|
330
340
|
`Block hash mismatch at block ${targetBlockNumber} (expected ${blockHash} but got ${updatedHash})`,
|
|
@@ -343,13 +353,47 @@ export class ServerWorldStateSynchronizer
|
|
|
343
353
|
return updatedBlockNumber;
|
|
344
354
|
}
|
|
345
355
|
|
|
346
|
-
/**
|
|
356
|
+
/**
|
|
357
|
+
* Returns the L2 block hash for a given number, served from memory when it was read before. Used by the
|
|
358
|
+
* L2BlockStream for detecting reorgs.
|
|
359
|
+
*/
|
|
347
360
|
public async getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
|
|
361
|
+
return this.blockHashCache.get(number) ?? (await this.readAndCacheL2BlockHash(number));
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Reads the L2 block hash for a given number from the committed archive tree. */
|
|
365
|
+
private async readL2BlockHash(number: BlockNumber): Promise<string | undefined> {
|
|
348
366
|
// Block 0 is read from the archive as well (its first leaf is the initial header hash, which init asserts) rather
|
|
349
|
-
// than from the in-memory initial header, so
|
|
350
|
-
//
|
|
367
|
+
// than from the in-memory initial header, so a node idling at genesis still reaches wsdb on every block stream poll
|
|
368
|
+
// (see getL2Tips). The committed view is fetched per call because clear() replaces the instance.
|
|
351
369
|
const leaf = await this.merkleTreeDb.getCommitted().getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number));
|
|
352
|
-
|
|
370
|
+
// Native wsdb returns zero for unwritten leaves; even the genesis archive leaf is nonzero.
|
|
371
|
+
return leaf && !leaf.isZero() ? leaf.toString() : undefined;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
private async readAndCacheL2BlockHash(number: BlockNumber): Promise<string | undefined> {
|
|
375
|
+
const generation = this.blockHashCacheGeneration;
|
|
376
|
+
const hash = await this.readL2BlockHash(number);
|
|
377
|
+
// A missing leaf is not cached: the block may be added later without any invalidation.
|
|
378
|
+
if (hash !== undefined && generation === this.blockHashCacheGeneration) {
|
|
379
|
+
this.blockHashCache.set(number, hash);
|
|
380
|
+
}
|
|
381
|
+
return hash;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private invalidateBlockHashCache() {
|
|
385
|
+
this.blockHashCacheGeneration++;
|
|
386
|
+
this.blockHashCache.clear();
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** Runs a mutation that can change committed archive leaves, invalidating the block hash cache around it. */
|
|
390
|
+
private async withBlockHashCacheInvalidation<T>(fn: () => Promise<T>): Promise<T> {
|
|
391
|
+
this.invalidateBlockHashCache();
|
|
392
|
+
try {
|
|
393
|
+
return await fn();
|
|
394
|
+
} finally {
|
|
395
|
+
this.invalidateBlockHashCache();
|
|
396
|
+
}
|
|
353
397
|
}
|
|
354
398
|
|
|
355
399
|
/**
|
|
@@ -359,7 +403,10 @@ export class ServerWorldStateSynchronizer
|
|
|
359
403
|
*/
|
|
360
404
|
public async getL2Tips(): Promise<LocalChainTips> {
|
|
361
405
|
const status = await this.merkleTreeDb.getStatusSummary();
|
|
362
|
-
|
|
406
|
+
// The status summary is cached in memory, so the proposed tip hash is always read from wsdb: that keeps one native
|
|
407
|
+
// round trip per block stream poll, so the node notices when wsdb dies. It also refreshes the cache entry that the
|
|
408
|
+
// walk-back reads right after. The finalized and proven hashes come from the cache.
|
|
409
|
+
const unfinalizedBlockHashPromise = this.readAndCacheL2BlockHash(status.unfinalizedBlockNumber);
|
|
363
410
|
const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
|
|
364
411
|
|
|
365
412
|
const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
|
|
@@ -561,7 +608,7 @@ export class ServerWorldStateSynchronizer
|
|
|
561
608
|
|
|
562
609
|
private async handleChainPruned(blockNumber: BlockNumber) {
|
|
563
610
|
this.log.info(`Chain pruned to block ${blockNumber}`);
|
|
564
|
-
const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
|
|
611
|
+
const status = await this.withBlockHashCacheInvalidation(() => this.merkleTreeDb.unwindBlocks(blockNumber));
|
|
565
612
|
if (this.provenBlockNumber !== undefined && this.provenBlockNumber > blockNumber) {
|
|
566
613
|
this.provenBlockNumber = undefined;
|
|
567
614
|
}
|
|
@@ -604,7 +651,9 @@ export class ServerWorldStateSynchronizer
|
|
|
604
651
|
const nextBlock = BlockNumber(
|
|
605
652
|
Math.min(cutoff, Math.max(1, summary.oldestHistoricalBlock) + MAX_BLOCK_PRUNE),
|
|
606
653
|
);
|
|
607
|
-
const status = await this.
|
|
654
|
+
const status = await this.withBlockHashCacheInvalidation(() =>
|
|
655
|
+
this.merkleTreeDb.removeHistoricalBlocks(nextBlock),
|
|
656
|
+
);
|
|
608
657
|
if (status.summary.oldestHistoricalBlock <= summary.oldestHistoricalBlock) {
|
|
609
658
|
throw new Error('Historical pruning did not advance the oldest block');
|
|
610
659
|
}
|