@aztec-labs/world-state 6.0.0-rc.1 → 6.0.0-rc.2
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 +12 -1
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.js +102 -9
- package/package.json +9 -9
- package/src/synchronizer/server_world_state_synchronizer.ts +106 -9
|
@@ -29,6 +29,13 @@ export declare class ServerWorldStateSynchronizer implements WorldStateSynchroni
|
|
|
29
29
|
private syncPromise;
|
|
30
30
|
protected blockStream: EventDrivenL2BlockStream | undefined;
|
|
31
31
|
private provenBlockNumber;
|
|
32
|
+
private readonly mutationQueue;
|
|
33
|
+
private readonly pruningSleep;
|
|
34
|
+
private pruningCutoff;
|
|
35
|
+
private pruningPromise;
|
|
36
|
+
private pruningPaused;
|
|
37
|
+
private stopping;
|
|
38
|
+
private stopPromise;
|
|
32
39
|
constructor(merkleTreeDb: MerkleTreeAdminDatabase, l2BlockSource: L2BlockSource & L1ToL2MessageSource, config: WorldStateConfig, instrumentation?: WorldStateInstrumentation, log?: Logger);
|
|
33
40
|
getCommitted(): MerkleTreeReadOperations;
|
|
34
41
|
getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations;
|
|
@@ -41,6 +48,7 @@ export declare class ServerWorldStateSynchronizer implements WorldStateSynchroni
|
|
|
41
48
|
start(): Promise<void | import("@aztec-labs/foundation/promise").PromiseWithResolvers<void>>;
|
|
42
49
|
protected createBlockStream(): EventDrivenL2BlockStream;
|
|
43
50
|
stop(): Promise<void>;
|
|
51
|
+
private stopInternal;
|
|
44
52
|
status(): Promise<WorldStateSynchronizerStatus>;
|
|
45
53
|
getLatestBlockNumber(): Promise<BlockNumber>;
|
|
46
54
|
stopSync(): Promise<void>;
|
|
@@ -62,6 +70,7 @@ export declare class ServerWorldStateSynchronizer implements WorldStateSynchroni
|
|
|
62
70
|
getL2Tips(): Promise<LocalChainTips>;
|
|
63
71
|
/** Handles an event emitted by the block stream. */
|
|
64
72
|
handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void>;
|
|
73
|
+
private handleBlockStreamEventInternal;
|
|
65
74
|
private handleL2Blocks;
|
|
66
75
|
private getL1ToL2LeafCountBefore;
|
|
67
76
|
/**
|
|
@@ -79,5 +88,7 @@ export declare class ServerWorldStateSynchronizer implements WorldStateSynchroni
|
|
|
79
88
|
* @param newState - New state value.
|
|
80
89
|
*/
|
|
81
90
|
private setCurrentState;
|
|
91
|
+
private startPruning;
|
|
92
|
+
private pruneHistory;
|
|
82
93
|
}
|
|
83
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
94
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VydmVyX3dvcmxkX3N0YXRlX3N5bmNocm9uaXplci5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3N5bmNocm9uaXplci9zZXJ2ZXJfd29ybGRfc3RhdGVfc3luY2hyb25pemVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFBRSxXQUFXLEVBQW9CLE1BQU0sc0NBQXNDLENBQUM7QUFDckYsT0FBTyxLQUFLLEVBQUUsRUFBRSxFQUFFLE1BQU0scUNBQXFDLENBQUM7QUFDOUQsT0FBTyxFQUFFLEtBQUssTUFBTSxFQUFnQixNQUFNLDRCQUE0QixDQUFDO0FBS3ZFLE9BQU8sRUFDTCxLQUFLLFNBQVMsRUFDZCx3QkFBd0IsRUFDeEIsS0FBSyxPQUFPLEVBQ1osS0FBSyxhQUFhLEVBQ2xCLEtBQUssa0JBQWtCLEVBQ3ZCLEtBQUsseUJBQXlCLEVBQzlCLEtBQUssOEJBQThCLEVBQ25DLEtBQUssY0FBYyxFQUNwQixNQUFNLDBCQUEwQixDQUFDO0FBQ2xDLE9BQU8sRUFH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LEVBQUUsR0FBRyxPQUFPLENBQUMsb0JBQW9CLENBQUMsQ0FnQm5HO1lBRWEsb0JBQW9CO0lBdURsQyxPQUFPLENBQUMsaUJBQWlCO1lBTVgsaUJBQWlCO0lBUy9COzs7T0FHRztJQUNILE9BQU8sQ0FBQyxlQUFlO0lBS3ZCLE9BQU8sQ0FBQyxZQUFZO1lBT04sWUFBWTtDQXdDM0IifQ==
|
|
@@ -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;AACrF,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,qCAAqC,CAAC;AAC9D,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,4BAA4B,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;AACrF,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,w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YAwCd,wBAAwB;IAYtC;;;;;OAKG;IACH,UAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAgBnG;YAEa,oBAAoB;IAuDlC,OAAO,CAAC,iBAAiB;YAMX,iBAAiB;IAS/B;;;OAGG;IACH,OAAO,CAAC,eAAe;IAKvB,OAAO,CAAC,YAAY;YAON,YAAY;CAwC3B"}
|
|
@@ -2,6 +2,8 @@ import { INITIAL_L2_BLOCK_NUM } from '@aztec-labs/constants';
|
|
|
2
2
|
import { BlockNumber, CheckpointNumber } from '@aztec-labs/foundation/branded-types';
|
|
3
3
|
import { createLogger } from '@aztec-labs/foundation/log';
|
|
4
4
|
import { promiseWithResolvers } from '@aztec-labs/foundation/promise';
|
|
5
|
+
import { SerialQueue } from '@aztec-labs/foundation/queue';
|
|
6
|
+
import { InterruptibleSleep } from '@aztec-labs/foundation/sleep';
|
|
5
7
|
import { elapsed } from '@aztec-labs/foundation/timer';
|
|
6
8
|
import { EventDrivenL2BlockStream } from '@aztec-labs/stdlib/block';
|
|
7
9
|
import { WorldStateRunningState } from '@aztec-labs/stdlib/interfaces/server';
|
|
@@ -9,6 +11,8 @@ import { MerkleTreeId } from '@aztec-labs/stdlib/trees';
|
|
|
9
11
|
import { getTelemetryClient } from '@aztec-labs/telemetry-client';
|
|
10
12
|
import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
|
|
11
13
|
import { WorldStateSynchronizerError } from './errors.js';
|
|
14
|
+
// four checkpoint's worth of blocks
|
|
15
|
+
const MAX_BLOCK_PRUNE = 40;
|
|
12
16
|
/**
|
|
13
17
|
* Synchronizes the world state with the L2 blocks from a L2BlockSource via a block stream.
|
|
14
18
|
* The synchronizer will download the L2 blocks from the L2BlockSource and update the merkle trees.
|
|
@@ -28,6 +32,13 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
28
32
|
// WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
|
|
29
33
|
// store the proven block number here, in the synchronizer, so that we don't end up spamming the logs with 'chain-proved' events
|
|
30
34
|
provenBlockNumber;
|
|
35
|
+
mutationQueue;
|
|
36
|
+
pruningSleep;
|
|
37
|
+
pruningCutoff;
|
|
38
|
+
pruningPromise;
|
|
39
|
+
pruningPaused;
|
|
40
|
+
stopping;
|
|
41
|
+
stopPromise;
|
|
31
42
|
constructor(merkleTreeDb, l2BlockSource, config, instrumentation = new WorldStateInstrumentation(getTelemetryClient()), log = createLogger('world_state')){
|
|
32
43
|
this.merkleTreeDb = merkleTreeDb;
|
|
33
44
|
this.l2BlockSource = l2BlockSource;
|
|
@@ -37,7 +48,12 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
37
48
|
this.latestBlockNumberAtStart = BlockNumber.ZERO;
|
|
38
49
|
this.currentState = WorldStateRunningState.IDLE;
|
|
39
50
|
this.syncPromise = promiseWithResolvers();
|
|
51
|
+
this.mutationQueue = new SerialQueue();
|
|
52
|
+
this.pruningSleep = new InterruptibleSleep();
|
|
53
|
+
this.pruningPaused = false;
|
|
54
|
+
this.stopping = false;
|
|
40
55
|
this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
|
|
56
|
+
this.mutationQueue.start();
|
|
41
57
|
this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
|
|
42
58
|
this.log.info(`Created world state synchroniser with block history of ${this.historyToKeep === undefined ? 'infinity' : this.historyToKeep}`);
|
|
43
59
|
}
|
|
@@ -84,10 +100,25 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
84
100
|
return this.merkleTreeDb.fork(blockNumber, opts);
|
|
85
101
|
}
|
|
86
102
|
backupTo(dstPath, compact) {
|
|
87
|
-
return this.merkleTreeDb.backupTo(dstPath, compact);
|
|
103
|
+
return this.mutationQueue.put(()=>this.merkleTreeDb.backupTo(dstPath, compact));
|
|
88
104
|
}
|
|
89
|
-
clear() {
|
|
90
|
-
|
|
105
|
+
async clear() {
|
|
106
|
+
const wasPaused = this.pruningPaused;
|
|
107
|
+
await this.stopSync();
|
|
108
|
+
try {
|
|
109
|
+
await this.mutationQueue.put(async ()=>{
|
|
110
|
+
this.pruningCutoff = undefined;
|
|
111
|
+
await this.merkleTreeDb.clear();
|
|
112
|
+
});
|
|
113
|
+
} finally{
|
|
114
|
+
if (!wasPaused && !this.stopping) {
|
|
115
|
+
if (this.blockStream) {
|
|
116
|
+
this.resumeSync();
|
|
117
|
+
} else {
|
|
118
|
+
this.pruningPaused = false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
91
122
|
}
|
|
92
123
|
async start() {
|
|
93
124
|
if (this.currentState === WorldStateRunningState.STOPPED) {
|
|
@@ -112,6 +143,7 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
112
143
|
this.blockStream = this.createBlockStream();
|
|
113
144
|
this.blockStream.start();
|
|
114
145
|
this.log.info(`Started world state synchronizer from block ${blockToDownloadFrom}`);
|
|
146
|
+
this.startPruning();
|
|
115
147
|
return this.syncPromise.promise;
|
|
116
148
|
}
|
|
117
149
|
createBlockStream() {
|
|
@@ -122,11 +154,15 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
122
154
|
ignoreCheckpoints: true
|
|
123
155
|
});
|
|
124
156
|
}
|
|
125
|
-
|
|
126
|
-
this.
|
|
127
|
-
|
|
157
|
+
stop() {
|
|
158
|
+
return this.stopPromise ??= this.stopInternal();
|
|
159
|
+
}
|
|
160
|
+
async stopInternal() {
|
|
161
|
+
this.stopping = true;
|
|
162
|
+
await this.stopSync();
|
|
128
163
|
this.log.debug('Stopping merkle trees...');
|
|
129
|
-
await this.merkleTreeDb.close();
|
|
164
|
+
await this.mutationQueue.put(()=>this.merkleTreeDb.close());
|
|
165
|
+
await this.mutationQueue.end();
|
|
130
166
|
this.setCurrentState(WorldStateRunningState.STOPPED);
|
|
131
167
|
this.log.info(`Stopped world state synchronizer`);
|
|
132
168
|
}
|
|
@@ -149,15 +185,23 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
149
185
|
}
|
|
150
186
|
async stopSync() {
|
|
151
187
|
this.log.debug('Stopping sync...');
|
|
188
|
+
this.pruningPaused = true;
|
|
189
|
+
this.pruningSleep.interrupt();
|
|
152
190
|
await this.blockStream?.stop();
|
|
191
|
+
await this.pruningPromise;
|
|
153
192
|
this.log.info('Stopped sync');
|
|
154
193
|
}
|
|
155
194
|
resumeSync() {
|
|
195
|
+
if (this.stopping) {
|
|
196
|
+
throw new Error('Cannot resume sync after stopping');
|
|
197
|
+
}
|
|
156
198
|
if (!this.blockStream) {
|
|
157
199
|
throw new Error('Cannot resume sync as block stream is not initialized');
|
|
158
200
|
}
|
|
159
201
|
this.log.debug('Resuming sync...');
|
|
160
202
|
this.blockStream.start();
|
|
203
|
+
this.pruningPaused = false;
|
|
204
|
+
this.startPruning();
|
|
161
205
|
this.log.info('Resumed sync');
|
|
162
206
|
}
|
|
163
207
|
/**
|
|
@@ -266,6 +310,9 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
266
310
|
};
|
|
267
311
|
}
|
|
268
312
|
/** Handles an event emitted by the block stream. */ async handleBlockStreamEvent(event) {
|
|
313
|
+
await this.mutationQueue.put(()=>this.handleBlockStreamEventInternal(event));
|
|
314
|
+
}
|
|
315
|
+
async handleBlockStreamEventInternal(event) {
|
|
269
316
|
switch(event.type){
|
|
270
317
|
case 'blocks-added':
|
|
271
318
|
await this.handleL2Blocks(event.blocks);
|
|
@@ -408,8 +455,8 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
408
455
|
return;
|
|
409
456
|
}
|
|
410
457
|
this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
|
|
411
|
-
|
|
412
|
-
this.
|
|
458
|
+
this.pruningCutoff = BlockNumber(Math.max(this.pruningCutoff ?? 0, newHistoricBlock.number));
|
|
459
|
+
this.startPruning();
|
|
413
460
|
}
|
|
414
461
|
handleChainProven(blockNumber) {
|
|
415
462
|
this.provenBlockNumber = blockNumber;
|
|
@@ -431,4 +478,50 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
431
478
|
this.currentState = newState;
|
|
432
479
|
this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
|
|
433
480
|
}
|
|
481
|
+
startPruning() {
|
|
482
|
+
// allow only one active worker
|
|
483
|
+
if (this.pruningPromise === undefined && !this.pruningPaused && !this.stopping && this.pruningCutoff) {
|
|
484
|
+
this.pruningPromise = this.pruneHistory();
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
async pruneHistory() {
|
|
488
|
+
try {
|
|
489
|
+
while(!this.pruningPaused && this.pruningCutoff !== undefined){
|
|
490
|
+
// Yield between native requests so forks and block sync can acquire canonical exclusivity.
|
|
491
|
+
await this.pruningSleep.sleep(0);
|
|
492
|
+
try {
|
|
493
|
+
await this.mutationQueue.put(async ()=>{
|
|
494
|
+
if (this.pruningPaused || this.pruningCutoff === undefined) {
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
const summary = await this.merkleTreeDb.getStatusSummary();
|
|
498
|
+
const cutoff = Math.min(this.pruningCutoff, summary.finalizedBlockNumber);
|
|
499
|
+
if (cutoff <= summary.oldestHistoricalBlock) {
|
|
500
|
+
this.pruningCutoff = undefined;
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
// Native history starts at block 1 even when the summary still reports the genesis sentinel 0.
|
|
504
|
+
const nextBlock = BlockNumber(Math.min(cutoff, Math.max(1, summary.oldestHistoricalBlock) + MAX_BLOCK_PRUNE));
|
|
505
|
+
const status = await this.merkleTreeDb.removeHistoricalBlocks(nextBlock);
|
|
506
|
+
if (status.summary.oldestHistoricalBlock <= summary.oldestHistoricalBlock) {
|
|
507
|
+
throw new Error('Historical pruning did not advance the oldest block');
|
|
508
|
+
}
|
|
509
|
+
this.log.debug('Pruned world state history', {
|
|
510
|
+
blocksPruned: BlockNumber.diff(summary.oldestHistoricalBlock, status.summary.oldestHistoricalBlock),
|
|
511
|
+
...status.summary
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
} catch (err) {
|
|
515
|
+
this.log.error('Failed to prune historical world state', err, {
|
|
516
|
+
cutoff: this.pruningCutoff
|
|
517
|
+
});
|
|
518
|
+
if (!this.pruningPaused) {
|
|
519
|
+
await this.pruningSleep.sleep(Math.max(1000, this.config.worldStateBlockCheckIntervalMS));
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
} finally{
|
|
524
|
+
this.pruningPromise = undefined;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
434
527
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec-labs/world-state",
|
|
3
|
-
"version": "6.0.0-rc.
|
|
3
|
+
"version": "6.0.0-rc.2",
|
|
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-rc.
|
|
68
|
-
"@aztec-labs/constants": "6.0.0-rc.
|
|
69
|
-
"@aztec-labs/foundation": "6.0.0-rc.
|
|
70
|
-
"@aztec-labs/protocol-contracts": "6.0.0-rc.
|
|
71
|
-
"@aztec-labs/stdlib": "6.0.0-rc.
|
|
72
|
-
"@aztec-labs/telemetry-client": "6.0.0-rc.
|
|
67
|
+
"@aztec-foundation/wsdb": "6.0.0-rc.5",
|
|
68
|
+
"@aztec-labs/constants": "6.0.0-rc.2",
|
|
69
|
+
"@aztec-labs/foundation": "6.0.0-rc.2",
|
|
70
|
+
"@aztec-labs/protocol-contracts": "6.0.0-rc.2",
|
|
71
|
+
"@aztec-labs/stdlib": "6.0.0-rc.2",
|
|
72
|
+
"@aztec-labs/telemetry-client": "6.0.0-rc.2",
|
|
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-rc.
|
|
79
|
-
"@aztec-labs/archiver": "6.0.0-rc.
|
|
78
|
+
"@aztec-foundation/ipc-runtime": "6.0.0-rc.5",
|
|
79
|
+
"@aztec-labs/archiver": "6.0.0-rc.2",
|
|
80
80
|
"@jest/globals": "^30.0.0",
|
|
81
81
|
"@types/jest": "^30.0.0",
|
|
82
82
|
"@types/node": "^22.15.17",
|
|
@@ -3,6 +3,8 @@ import { BlockNumber, CheckpointNumber } from '@aztec-labs/foundation/branded-ty
|
|
|
3
3
|
import type { Fr } from '@aztec-labs/foundation/curves/bn254';
|
|
4
4
|
import { type Logger, createLogger } from '@aztec-labs/foundation/log';
|
|
5
5
|
import { promiseWithResolvers } from '@aztec-labs/foundation/promise';
|
|
6
|
+
import { SerialQueue } from '@aztec-labs/foundation/queue';
|
|
7
|
+
import { InterruptibleSleep } from '@aztec-labs/foundation/sleep';
|
|
6
8
|
import { elapsed } from '@aztec-labs/foundation/timer';
|
|
7
9
|
import {
|
|
8
10
|
type BlockHash,
|
|
@@ -34,6 +36,9 @@ import { WorldStateSynchronizerError } from './errors.js';
|
|
|
34
36
|
|
|
35
37
|
export type { SnapshotDataKeys };
|
|
36
38
|
|
|
39
|
+
// four checkpoint's worth of blocks
|
|
40
|
+
const MAX_BLOCK_PRUNE = 40;
|
|
41
|
+
|
|
37
42
|
/**
|
|
38
43
|
* Synchronizes the world state with the L2 blocks from a L2BlockSource via a block stream.
|
|
39
44
|
* The synchronizer will download the L2 blocks from the L2BlockSource and update the merkle trees.
|
|
@@ -55,6 +60,14 @@ export class ServerWorldStateSynchronizer
|
|
|
55
60
|
// store the proven block number here, in the synchronizer, so that we don't end up spamming the logs with 'chain-proved' events
|
|
56
61
|
private provenBlockNumber: BlockNumber | undefined;
|
|
57
62
|
|
|
63
|
+
private readonly mutationQueue = new SerialQueue();
|
|
64
|
+
private readonly pruningSleep = new InterruptibleSleep();
|
|
65
|
+
private pruningCutoff: BlockNumber | undefined;
|
|
66
|
+
private pruningPromise: Promise<void> | undefined;
|
|
67
|
+
private pruningPaused = false;
|
|
68
|
+
private stopping = false;
|
|
69
|
+
private stopPromise: Promise<void> | undefined;
|
|
70
|
+
|
|
58
71
|
constructor(
|
|
59
72
|
private readonly merkleTreeDb: MerkleTreeAdminDatabase,
|
|
60
73
|
private readonly l2BlockSource: L2BlockSource & L1ToL2MessageSource,
|
|
@@ -63,6 +76,7 @@ export class ServerWorldStateSynchronizer
|
|
|
63
76
|
private readonly log: Logger = createLogger('world_state'),
|
|
64
77
|
) {
|
|
65
78
|
this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
|
|
79
|
+
this.mutationQueue.start();
|
|
66
80
|
this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
|
|
67
81
|
this.log.info(
|
|
68
82
|
`Created world state synchroniser with block history of ${
|
|
@@ -124,11 +138,26 @@ export class ServerWorldStateSynchronizer
|
|
|
124
138
|
}
|
|
125
139
|
|
|
126
140
|
public backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>> {
|
|
127
|
-
return this.merkleTreeDb.backupTo(dstPath, compact);
|
|
141
|
+
return this.mutationQueue.put(() => this.merkleTreeDb.backupTo(dstPath, compact));
|
|
128
142
|
}
|
|
129
143
|
|
|
130
|
-
public clear(): Promise<void> {
|
|
131
|
-
|
|
144
|
+
public async clear(): Promise<void> {
|
|
145
|
+
const wasPaused = this.pruningPaused;
|
|
146
|
+
await this.stopSync();
|
|
147
|
+
try {
|
|
148
|
+
await this.mutationQueue.put(async () => {
|
|
149
|
+
this.pruningCutoff = undefined;
|
|
150
|
+
await this.merkleTreeDb.clear();
|
|
151
|
+
});
|
|
152
|
+
} finally {
|
|
153
|
+
if (!wasPaused && !this.stopping) {
|
|
154
|
+
if (this.blockStream) {
|
|
155
|
+
this.resumeSync();
|
|
156
|
+
} else {
|
|
157
|
+
this.pruningPaused = false;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
132
161
|
}
|
|
133
162
|
|
|
134
163
|
public async start() {
|
|
@@ -158,6 +187,9 @@ export class ServerWorldStateSynchronizer
|
|
|
158
187
|
this.blockStream = this.createBlockStream();
|
|
159
188
|
this.blockStream.start();
|
|
160
189
|
this.log.info(`Started world state synchronizer from block ${blockToDownloadFrom}`);
|
|
190
|
+
|
|
191
|
+
this.startPruning();
|
|
192
|
+
|
|
161
193
|
return this.syncPromise.promise;
|
|
162
194
|
}
|
|
163
195
|
|
|
@@ -170,11 +202,16 @@ export class ServerWorldStateSynchronizer
|
|
|
170
202
|
});
|
|
171
203
|
}
|
|
172
204
|
|
|
173
|
-
public
|
|
174
|
-
this.
|
|
175
|
-
|
|
205
|
+
public stop(): Promise<void> {
|
|
206
|
+
return (this.stopPromise ??= this.stopInternal());
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private async stopInternal() {
|
|
210
|
+
this.stopping = true;
|
|
211
|
+
await this.stopSync();
|
|
176
212
|
this.log.debug('Stopping merkle trees...');
|
|
177
|
-
await this.merkleTreeDb.close();
|
|
213
|
+
await this.mutationQueue.put(() => this.merkleTreeDb.close());
|
|
214
|
+
await this.mutationQueue.end();
|
|
178
215
|
this.setCurrentState(WorldStateRunningState.STOPPED);
|
|
179
216
|
this.log.info(`Stopped world state synchronizer`);
|
|
180
217
|
}
|
|
@@ -200,16 +237,24 @@ export class ServerWorldStateSynchronizer
|
|
|
200
237
|
|
|
201
238
|
public async stopSync() {
|
|
202
239
|
this.log.debug('Stopping sync...');
|
|
240
|
+
this.pruningPaused = true;
|
|
241
|
+
this.pruningSleep.interrupt();
|
|
203
242
|
await this.blockStream?.stop();
|
|
243
|
+
await this.pruningPromise;
|
|
204
244
|
this.log.info('Stopped sync');
|
|
205
245
|
}
|
|
206
246
|
|
|
207
247
|
public resumeSync() {
|
|
248
|
+
if (this.stopping) {
|
|
249
|
+
throw new Error('Cannot resume sync after stopping');
|
|
250
|
+
}
|
|
208
251
|
if (!this.blockStream) {
|
|
209
252
|
throw new Error('Cannot resume sync as block stream is not initialized');
|
|
210
253
|
}
|
|
211
254
|
this.log.debug('Resuming sync...');
|
|
212
255
|
this.blockStream.start();
|
|
256
|
+
this.pruningPaused = false;
|
|
257
|
+
this.startPruning();
|
|
213
258
|
this.log.info('Resumed sync');
|
|
214
259
|
}
|
|
215
260
|
|
|
@@ -336,6 +381,10 @@ export class ServerWorldStateSynchronizer
|
|
|
336
381
|
|
|
337
382
|
/** Handles an event emitted by the block stream. */
|
|
338
383
|
public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
|
|
384
|
+
await this.mutationQueue.put(() => this.handleBlockStreamEventInternal(event));
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
private async handleBlockStreamEventInternal(event: L2BlockStreamEvent): Promise<void> {
|
|
339
388
|
switch (event.type) {
|
|
340
389
|
case 'blocks-added':
|
|
341
390
|
await this.handleL2Blocks(event.blocks);
|
|
@@ -493,8 +542,8 @@ export class ServerWorldStateSynchronizer
|
|
|
493
542
|
return;
|
|
494
543
|
}
|
|
495
544
|
this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
|
|
496
|
-
|
|
497
|
-
this.
|
|
545
|
+
this.pruningCutoff = BlockNumber(Math.max(this.pruningCutoff ?? 0, newHistoricBlock.number));
|
|
546
|
+
this.startPruning();
|
|
498
547
|
}
|
|
499
548
|
|
|
500
549
|
private handleChainProven(blockNumber: BlockNumber) {
|
|
@@ -520,4 +569,52 @@ export class ServerWorldStateSynchronizer
|
|
|
520
569
|
this.currentState = newState;
|
|
521
570
|
this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
|
|
522
571
|
}
|
|
572
|
+
|
|
573
|
+
private startPruning() {
|
|
574
|
+
// allow only one active worker
|
|
575
|
+
if (this.pruningPromise === undefined && !this.pruningPaused && !this.stopping && this.pruningCutoff) {
|
|
576
|
+
this.pruningPromise = this.pruneHistory();
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
private async pruneHistory() {
|
|
581
|
+
try {
|
|
582
|
+
while (!this.pruningPaused && this.pruningCutoff !== undefined) {
|
|
583
|
+
// Yield between native requests so forks and block sync can acquire canonical exclusivity.
|
|
584
|
+
await this.pruningSleep.sleep(0);
|
|
585
|
+
try {
|
|
586
|
+
await this.mutationQueue.put(async () => {
|
|
587
|
+
if (this.pruningPaused || this.pruningCutoff === undefined) {
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
const summary = await this.merkleTreeDb.getStatusSummary();
|
|
591
|
+
const cutoff = Math.min(this.pruningCutoff, summary.finalizedBlockNumber);
|
|
592
|
+
if (cutoff <= summary.oldestHistoricalBlock) {
|
|
593
|
+
this.pruningCutoff = undefined;
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
// Native history starts at block 1 even when the summary still reports the genesis sentinel 0.
|
|
597
|
+
const nextBlock = BlockNumber(
|
|
598
|
+
Math.min(cutoff, Math.max(1, summary.oldestHistoricalBlock) + MAX_BLOCK_PRUNE),
|
|
599
|
+
);
|
|
600
|
+
const status = await this.merkleTreeDb.removeHistoricalBlocks(nextBlock);
|
|
601
|
+
if (status.summary.oldestHistoricalBlock <= summary.oldestHistoricalBlock) {
|
|
602
|
+
throw new Error('Historical pruning did not advance the oldest block');
|
|
603
|
+
}
|
|
604
|
+
this.log.debug('Pruned world state history', {
|
|
605
|
+
blocksPruned: BlockNumber.diff(summary.oldestHistoricalBlock, status.summary.oldestHistoricalBlock),
|
|
606
|
+
...status.summary,
|
|
607
|
+
});
|
|
608
|
+
});
|
|
609
|
+
} catch (err) {
|
|
610
|
+
this.log.error('Failed to prune historical world state', err, { cutoff: this.pruningCutoff });
|
|
611
|
+
if (!this.pruningPaused) {
|
|
612
|
+
await this.pruningSleep.sleep(Math.max(1000, this.config.worldStateBlockCheckIntervalMS));
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
} finally {
|
|
617
|
+
this.pruningPromise = undefined;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
523
620
|
}
|