@aztec/world-state 0.0.1-commit.03f7ef2 → 0.0.1-commit.04d373f
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/instrumentation/instrumentation.d.ts +1 -1
- package/dest/instrumentation/instrumentation.d.ts.map +1 -1
- package/dest/instrumentation/instrumentation.js +17 -41
- package/dest/native/fork_checkpoint.d.ts +7 -1
- package/dest/native/fork_checkpoint.d.ts.map +1 -1
- package/dest/native/fork_checkpoint.js +15 -3
- package/dest/native/ipc_world_state_instance.d.ts +50 -0
- package/dest/native/ipc_world_state_instance.d.ts.map +1 -0
- package/dest/native/ipc_world_state_instance.js +628 -0
- package/dest/native/merkle_trees_facade.d.ts +9 -6
- package/dest/native/merkle_trees_facade.d.ts.map +1 -1
- package/dest/native/merkle_trees_facade.js +47 -16
- package/dest/native/message.d.ts +13 -5
- package/dest/native/message.d.ts.map +1 -1
- package/dest/native/native_world_state.d.ts +34 -8
- package/dest/native/native_world_state.d.ts.map +1 -1
- package/dest/native/native_world_state.js +81 -27
- package/dest/native/native_world_state_instance.d.ts +6 -5
- package/dest/native/native_world_state_instance.d.ts.map +1 -1
- package/dest/native/native_world_state_instance.js +33 -26
- package/dest/native/world_state_ops_queue.js +5 -5
- package/dest/synchronizer/config.d.ts +3 -5
- package/dest/synchronizer/config.d.ts.map +1 -1
- package/dest/synchronizer/config.js +15 -18
- package/dest/synchronizer/factory.d.ts +6 -5
- package/dest/synchronizer/factory.d.ts.map +1 -1
- package/dest/synchronizer/factory.js +7 -6
- package/dest/synchronizer/server_world_state_synchronizer.d.ts +4 -5
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.js +139 -39
- package/dest/test/utils.d.ts +7 -7
- package/dest/test/utils.d.ts.map +1 -1
- package/dest/test/utils.js +5 -5
- package/dest/testing.d.ts +4 -3
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +10 -6
- package/dest/world-state-db/merkle_tree_db.d.ts +5 -14
- package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
- package/package.json +13 -12
- package/src/instrumentation/instrumentation.ts +17 -41
- package/src/native/fork_checkpoint.ts +19 -3
- package/src/native/ipc_world_state_instance.ts +717 -0
- package/src/native/merkle_trees_facade.ts +51 -17
- package/src/native/message.ts +14 -4
- package/src/native/native_world_state.ts +108 -32
- package/src/native/native_world_state_instance.ts +44 -32
- package/src/native/world_state_ops_queue.ts +5 -5
- package/src/synchronizer/config.ts +16 -23
- package/src/synchronizer/factory.ts +19 -11
- package/src/synchronizer/server_world_state_synchronizer.ts +142 -53
- package/src/test/utils.ts +6 -6
- package/src/testing.ts +8 -9
- package/src/world-state-db/merkle_tree_db.ts +8 -14
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type ConfigMappingsType,
|
|
3
|
-
booleanConfigHelper,
|
|
4
3
|
getConfigFromMappings,
|
|
5
4
|
numberConfigHelper,
|
|
5
|
+
optionalNumberConfigHelper,
|
|
6
6
|
} from '@aztec/foundation/config';
|
|
7
7
|
|
|
8
8
|
/** World State synchronizer configuration values. */
|
|
@@ -10,9 +10,6 @@ export interface WorldStateConfig {
|
|
|
10
10
|
/** The frequency in which to check. */
|
|
11
11
|
worldStateBlockCheckIntervalMS: number;
|
|
12
12
|
|
|
13
|
-
/** Whether to follow only the proven chain. */
|
|
14
|
-
worldStateProvenBlocksOnly: boolean;
|
|
15
|
-
|
|
16
13
|
/** Size of the batch for each get-blocks request from the synchronizer to the archiver. */
|
|
17
14
|
worldStateBlockRequestBatchSize?: number;
|
|
18
15
|
|
|
@@ -37,59 +34,53 @@ export interface WorldStateConfig {
|
|
|
37
34
|
/** Optional directory for the world state DB, if unspecified will default to the general data directory */
|
|
38
35
|
worldStateDataDirectory?: string;
|
|
39
36
|
|
|
40
|
-
/** The number of historic blocks to maintain */
|
|
41
|
-
|
|
37
|
+
/** The number of historic checkpoints worth of blocks to maintain */
|
|
38
|
+
worldStateCheckpointHistory: number;
|
|
42
39
|
}
|
|
43
40
|
|
|
44
41
|
export const worldStateConfigMappings: ConfigMappingsType<WorldStateConfig> = {
|
|
45
42
|
worldStateBlockCheckIntervalMS: {
|
|
46
43
|
env: 'WS_BLOCK_CHECK_INTERVAL_MS',
|
|
47
|
-
|
|
48
|
-
defaultValue: 100,
|
|
44
|
+
...numberConfigHelper(100),
|
|
49
45
|
description: 'The frequency in which to check.',
|
|
50
46
|
},
|
|
51
|
-
worldStateProvenBlocksOnly: {
|
|
52
|
-
env: 'WS_PROVEN_BLOCKS_ONLY',
|
|
53
|
-
description: 'Whether to follow only the proven chain.',
|
|
54
|
-
...booleanConfigHelper(),
|
|
55
|
-
},
|
|
56
47
|
worldStateBlockRequestBatchSize: {
|
|
57
48
|
env: 'WS_BLOCK_REQUEST_BATCH_SIZE',
|
|
58
|
-
|
|
49
|
+
...optionalNumberConfigHelper(),
|
|
59
50
|
description: 'Size of the batch for each get-blocks request from the synchronizer to the archiver.',
|
|
60
51
|
},
|
|
61
52
|
worldStateDbMapSizeKb: {
|
|
62
53
|
env: 'WS_DB_MAP_SIZE_KB',
|
|
63
|
-
|
|
54
|
+
...optionalNumberConfigHelper(),
|
|
64
55
|
description: 'The maximum possible size of the world state DB in KB. Overwrites the general dataStoreMapSizeKb.',
|
|
65
56
|
},
|
|
66
57
|
archiveTreeMapSizeKb: {
|
|
67
58
|
env: 'ARCHIVE_TREE_MAP_SIZE_KB',
|
|
68
|
-
|
|
59
|
+
...optionalNumberConfigHelper(),
|
|
69
60
|
description:
|
|
70
61
|
'The maximum possible size of the world state archive tree in KB. Overwrites the general worldStateDbMapSizeKb.',
|
|
71
62
|
},
|
|
72
63
|
nullifierTreeMapSizeKb: {
|
|
73
64
|
env: 'NULLIFIER_TREE_MAP_SIZE_KB',
|
|
74
|
-
|
|
65
|
+
...optionalNumberConfigHelper(),
|
|
75
66
|
description:
|
|
76
67
|
'The maximum possible size of the world state nullifier tree in KB. Overwrites the general worldStateDbMapSizeKb.',
|
|
77
68
|
},
|
|
78
69
|
noteHashTreeMapSizeKb: {
|
|
79
70
|
env: 'NOTE_HASH_TREE_MAP_SIZE_KB',
|
|
80
|
-
|
|
71
|
+
...optionalNumberConfigHelper(),
|
|
81
72
|
description:
|
|
82
73
|
'The maximum possible size of the world state note hash tree in KB. Overwrites the general worldStateDbMapSizeKb.',
|
|
83
74
|
},
|
|
84
75
|
messageTreeMapSizeKb: {
|
|
85
76
|
env: 'MESSAGE_TREE_MAP_SIZE_KB',
|
|
86
|
-
|
|
77
|
+
...optionalNumberConfigHelper(),
|
|
87
78
|
description:
|
|
88
79
|
'The maximum possible size of the world state message tree in KB. Overwrites the general worldStateDbMapSizeKb.',
|
|
89
80
|
},
|
|
90
81
|
publicDataTreeMapSizeKb: {
|
|
91
82
|
env: 'PUBLIC_DATA_TREE_MAP_SIZE_KB',
|
|
92
|
-
|
|
83
|
+
...optionalNumberConfigHelper(),
|
|
93
84
|
description:
|
|
94
85
|
'The maximum possible size of the world state public data tree in KB. Overwrites the general worldStateDbMapSizeKb.',
|
|
95
86
|
},
|
|
@@ -97,9 +88,11 @@ export const worldStateConfigMappings: ConfigMappingsType<WorldStateConfig> = {
|
|
|
97
88
|
env: 'WS_DATA_DIRECTORY',
|
|
98
89
|
description: 'Optional directory for the world state database',
|
|
99
90
|
},
|
|
100
|
-
|
|
101
|
-
env: '
|
|
102
|
-
description:
|
|
91
|
+
worldStateCheckpointHistory: {
|
|
92
|
+
env: 'WS_NUM_HISTORIC_CHECKPOINTS',
|
|
93
|
+
description:
|
|
94
|
+
'The number of historic checkpoints worth of blocks to maintain. Values less than 1 mean all history is maintained',
|
|
95
|
+
fallback: ['WS_NUM_HISTORIC_BLOCKS'],
|
|
103
96
|
...numberConfigHelper(64),
|
|
104
97
|
},
|
|
105
98
|
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { LoggerBindings } from '@aztec/foundation/log';
|
|
2
2
|
import type { L2BlockSource } from '@aztec/stdlib/block';
|
|
3
|
+
import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
|
|
3
4
|
import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
4
|
-
import type
|
|
5
|
+
import { EMPTY_GENESIS_DATA, type GenesisData, isGenesisData } from '@aztec/stdlib/world-state';
|
|
5
6
|
import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
|
|
6
7
|
|
|
7
8
|
import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
|
|
@@ -20,11 +21,14 @@ export interface WorldStateTreeMapSizes {
|
|
|
20
21
|
export async function createWorldStateSynchronizer(
|
|
21
22
|
config: WorldStateConfig & DataStoreConfig,
|
|
22
23
|
l2BlockSource: L2BlockSource & L1ToL2MessageSource,
|
|
23
|
-
|
|
24
|
+
genesisOrNativeWorldState: GenesisData | NativeWorldStateService,
|
|
24
25
|
client: TelemetryClient = getTelemetryClient(),
|
|
26
|
+
bindings?: LoggerBindings,
|
|
25
27
|
) {
|
|
26
28
|
const instrumentation = new WorldStateInstrumentation(client);
|
|
27
|
-
const merkleTrees =
|
|
29
|
+
const merkleTrees = isGenesisData(genesisOrNativeWorldState)
|
|
30
|
+
? await createWorldState(config, genesisOrNativeWorldState, instrumentation, bindings)
|
|
31
|
+
: genesisOrNativeWorldState;
|
|
28
32
|
return new ServerWorldStateSynchronizer(merkleTrees, l2BlockSource, config, instrumentation);
|
|
29
33
|
}
|
|
30
34
|
|
|
@@ -39,9 +43,10 @@ export async function createWorldState(
|
|
|
39
43
|
| 'messageTreeMapSizeKb'
|
|
40
44
|
| 'publicDataTreeMapSizeKb'
|
|
41
45
|
> &
|
|
42
|
-
Pick<DataStoreConfig, 'dataDirectory' | 'dataStoreMapSizeKb' | '
|
|
43
|
-
|
|
46
|
+
Pick<DataStoreConfig, 'dataDirectory' | 'dataStoreMapSizeKb' | 'rollupAddress'>,
|
|
47
|
+
genesis: GenesisData = EMPTY_GENESIS_DATA,
|
|
44
48
|
instrumentation: WorldStateInstrumentation = new WorldStateInstrumentation(getTelemetryClient()),
|
|
49
|
+
bindings?: LoggerBindings,
|
|
45
50
|
) {
|
|
46
51
|
const dataDirectory = config.worldStateDataDirectory ?? config.dataDirectory;
|
|
47
52
|
const dataStoreMapSizeKb = config.worldStateDbMapSizeKb ?? config.dataStoreMapSizeKb;
|
|
@@ -53,23 +58,26 @@ export async function createWorldState(
|
|
|
53
58
|
publicDataTreeMapSizeKb: config.publicDataTreeMapSizeKb ?? dataStoreMapSizeKb,
|
|
54
59
|
};
|
|
55
60
|
|
|
56
|
-
if (!config.
|
|
61
|
+
if (!config.rollupAddress) {
|
|
57
62
|
throw new Error('Rollup address is required to create a world state synchronizer.');
|
|
58
63
|
}
|
|
59
64
|
|
|
60
65
|
// If a data directory is provided in config, then create a persistent store.
|
|
61
66
|
const merkleTrees = dataDirectory
|
|
62
67
|
? await NativeWorldStateService.new(
|
|
63
|
-
config.
|
|
68
|
+
config.rollupAddress,
|
|
64
69
|
dataDirectory,
|
|
65
70
|
wsTreeMapSizes,
|
|
66
|
-
|
|
71
|
+
genesis,
|
|
67
72
|
instrumentation,
|
|
73
|
+
bindings,
|
|
68
74
|
)
|
|
69
75
|
: await NativeWorldStateService.tmp(
|
|
70
|
-
config.
|
|
76
|
+
config.rollupAddress,
|
|
71
77
|
!['true', '1'].includes(process.env.DEBUG_WORLD_STATE!),
|
|
72
|
-
|
|
78
|
+
genesis,
|
|
79
|
+
instrumentation,
|
|
80
|
+
bindings,
|
|
73
81
|
);
|
|
74
82
|
|
|
75
83
|
return merkleTrees;
|
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { INITIAL_CHECKPOINT_NUMBER } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
2
3
|
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
4
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
4
5
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
5
6
|
import { elapsed } from '@aztec/foundation/timer';
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
import {
|
|
8
|
+
type BlockHash,
|
|
9
|
+
GENESIS_CHECKPOINT_HEADER_HASH,
|
|
10
|
+
type L2Block,
|
|
11
|
+
type L2BlockId,
|
|
12
|
+
type L2BlockSource,
|
|
10
13
|
L2BlockStream,
|
|
11
|
-
L2BlockStreamEvent,
|
|
12
|
-
L2BlockStreamEventHandler,
|
|
13
|
-
L2BlockStreamLocalDataProvider,
|
|
14
|
-
L2Tips,
|
|
14
|
+
type L2BlockStreamEvent,
|
|
15
|
+
type L2BlockStreamEventHandler,
|
|
16
|
+
type L2BlockStreamLocalDataProvider,
|
|
17
|
+
type L2Tips,
|
|
15
18
|
} from '@aztec/stdlib/block';
|
|
16
19
|
import {
|
|
17
20
|
WorldStateRunningState,
|
|
@@ -23,7 +26,7 @@ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
|
23
26
|
import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
|
|
24
27
|
import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
|
|
25
28
|
import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
26
|
-
import {
|
|
29
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
27
30
|
|
|
28
31
|
import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
|
|
29
32
|
import type { WorldStateStatusFull } from '../native/message.js';
|
|
@@ -46,7 +49,6 @@ export class ServerWorldStateSynchronizer
|
|
|
46
49
|
private latestBlockNumberAtStart = BlockNumber.ZERO;
|
|
47
50
|
private historyToKeep: number | undefined;
|
|
48
51
|
private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
|
|
49
|
-
private latestBlockHashQuery: { blockNumber: BlockNumber; hash: string | undefined } | undefined = undefined;
|
|
50
52
|
|
|
51
53
|
private syncPromise = promiseWithResolvers<void>();
|
|
52
54
|
protected blockStream: L2BlockStream | undefined;
|
|
@@ -63,7 +65,7 @@ export class ServerWorldStateSynchronizer
|
|
|
63
65
|
private readonly log: Logger = createLogger('world_state'),
|
|
64
66
|
) {
|
|
65
67
|
this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
|
|
66
|
-
this.historyToKeep = config.
|
|
68
|
+
this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
|
|
67
69
|
this.log.info(
|
|
68
70
|
`Created world state synchroniser with block history of ${
|
|
69
71
|
this.historyToKeep === undefined ? 'infinity' : this.historyToKeep
|
|
@@ -100,11 +102,7 @@ export class ServerWorldStateSynchronizer
|
|
|
100
102
|
}
|
|
101
103
|
|
|
102
104
|
// Get the current latest block number
|
|
103
|
-
this.latestBlockNumberAtStart = BlockNumber(
|
|
104
|
-
await (this.config.worldStateProvenBlocksOnly
|
|
105
|
-
? this.l2BlockSource.getProvenBlockNumber()
|
|
106
|
-
: this.l2BlockSource.getBlockNumber()),
|
|
107
|
-
);
|
|
105
|
+
this.latestBlockNumberAtStart = BlockNumber(await this.l2BlockSource.getBlockNumber());
|
|
108
106
|
|
|
109
107
|
const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
|
|
110
108
|
|
|
@@ -126,12 +124,11 @@ export class ServerWorldStateSynchronizer
|
|
|
126
124
|
}
|
|
127
125
|
|
|
128
126
|
protected createBlockStream(): L2BlockStream {
|
|
129
|
-
const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
|
|
130
127
|
const logger = createLogger('world-state:block_stream');
|
|
131
|
-
return new
|
|
132
|
-
proven: this.config.worldStateProvenBlocksOnly,
|
|
128
|
+
return new L2BlockStream(this.l2BlockSource, this, this, logger, {
|
|
133
129
|
pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
|
|
134
130
|
batchSize: this.config.worldStateBlockRequestBatchSize,
|
|
131
|
+
ignoreCheckpoints: true,
|
|
135
132
|
});
|
|
136
133
|
}
|
|
137
134
|
|
|
@@ -160,7 +157,7 @@ export class ServerWorldStateSynchronizer
|
|
|
160
157
|
}
|
|
161
158
|
|
|
162
159
|
public async getLatestBlockNumber() {
|
|
163
|
-
return (await this.getL2Tips()).
|
|
160
|
+
return (await this.getL2Tips()).proposed.number;
|
|
164
161
|
}
|
|
165
162
|
|
|
166
163
|
public async stopSync() {
|
|
@@ -181,13 +178,10 @@ export class ServerWorldStateSynchronizer
|
|
|
181
178
|
/**
|
|
182
179
|
* Forces an immediate sync.
|
|
183
180
|
* @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
|
|
184
|
-
* @param
|
|
181
|
+
* @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
|
|
185
182
|
* @returns A promise that resolves with the block number the world state was synced to
|
|
186
183
|
*/
|
|
187
|
-
public async syncImmediate(
|
|
188
|
-
targetBlockNumber?: BlockNumber,
|
|
189
|
-
skipThrowIfTargetNotReached?: boolean,
|
|
190
|
-
): Promise<BlockNumber> {
|
|
184
|
+
public async syncImmediate(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber> {
|
|
191
185
|
if (this.currentState !== WorldStateRunningState.RUNNING) {
|
|
192
186
|
throw new Error(`World State is not running. Unable to perform sync.`);
|
|
193
187
|
}
|
|
@@ -199,7 +193,19 @@ export class ServerWorldStateSynchronizer
|
|
|
199
193
|
// If we have been given a block number to sync to and we have reached that number then return
|
|
200
194
|
const currentBlockNumber = await this.getLatestBlockNumber();
|
|
201
195
|
if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
|
|
202
|
-
|
|
196
|
+
if (blockHash === undefined) {
|
|
197
|
+
return currentBlockNumber;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// If a block hash was provided, verify we're on the expected fork
|
|
201
|
+
const currentHash = await this.getL2BlockHash(targetBlockNumber);
|
|
202
|
+
if (currentHash === blockHash.toString()) {
|
|
203
|
+
return currentBlockNumber;
|
|
204
|
+
}
|
|
205
|
+
// Hash mismatch: a reorg may have occurred, fall through to trigger sync
|
|
206
|
+
this.log.debug(
|
|
207
|
+
`World state block hash mismatch at ${targetBlockNumber} (expected ${blockHash}, got ${currentHash}). Triggering resync.`,
|
|
208
|
+
);
|
|
203
209
|
}
|
|
204
210
|
this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
|
|
205
211
|
|
|
@@ -217,7 +223,7 @@ export class ServerWorldStateSynchronizer
|
|
|
217
223
|
|
|
218
224
|
// If we have been given a block number to sync to and we have not reached that number then fail
|
|
219
225
|
const updatedBlockNumber = await this.getLatestBlockNumber();
|
|
220
|
-
if (
|
|
226
|
+
if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
|
|
221
227
|
throw new WorldStateSynchronizerError(
|
|
222
228
|
`Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`,
|
|
223
229
|
{
|
|
@@ -231,6 +237,24 @@ export class ServerWorldStateSynchronizer
|
|
|
231
237
|
);
|
|
232
238
|
}
|
|
233
239
|
|
|
240
|
+
// If a block hash was provided, verify we're on the expected fork after syncing, throw otherwise
|
|
241
|
+
if (blockHash !== undefined && targetBlockNumber !== undefined) {
|
|
242
|
+
const updatedHash = await this.getL2BlockHash(targetBlockNumber);
|
|
243
|
+
if (updatedHash !== blockHash.toString()) {
|
|
244
|
+
throw new WorldStateSynchronizerError(
|
|
245
|
+
`Block hash mismatch at block ${targetBlockNumber} (expected ${blockHash} but got ${updatedHash})`,
|
|
246
|
+
{
|
|
247
|
+
cause: {
|
|
248
|
+
reason: 'block_hash_mismatch',
|
|
249
|
+
targetBlockNumber,
|
|
250
|
+
expectedHash: blockHash.toString(),
|
|
251
|
+
actualHash: updatedHash,
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
234
258
|
return updatedBlockNumber;
|
|
235
259
|
}
|
|
236
260
|
|
|
@@ -239,27 +263,49 @@ export class ServerWorldStateSynchronizer
|
|
|
239
263
|
if (number === BlockNumber.ZERO) {
|
|
240
264
|
return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
|
|
241
265
|
}
|
|
242
|
-
|
|
243
|
-
this.latestBlockHashQuery = {
|
|
244
|
-
hash: await this.merkleTreeCommitted
|
|
245
|
-
.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number))
|
|
246
|
-
.then(leaf => leaf?.toString()),
|
|
247
|
-
blockNumber: number,
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
return this.latestBlockHashQuery.hash;
|
|
266
|
+
return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then(leaf => leaf?.toString());
|
|
251
267
|
}
|
|
252
268
|
|
|
253
269
|
/** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
|
|
254
270
|
public async getL2Tips(): Promise<L2Tips> {
|
|
255
271
|
const status = await this.merkleTreeDb.getStatusSummary();
|
|
256
|
-
const
|
|
272
|
+
const unfinalizedBlockHashPromise = this.getL2BlockHash(status.unfinalizedBlockNumber);
|
|
273
|
+
const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
|
|
274
|
+
|
|
275
|
+
const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
|
|
276
|
+
const provenBlockHashPromise =
|
|
277
|
+
this.provenBlockNumber === undefined ? finalizedBlockHashPromise : this.getL2BlockHash(this.provenBlockNumber);
|
|
278
|
+
|
|
279
|
+
const [unfinalizedBlockHash, finalizedBlockHash, provenBlockHash] = await Promise.all([
|
|
280
|
+
unfinalizedBlockHashPromise,
|
|
281
|
+
finalizedBlockHashPromise,
|
|
282
|
+
provenBlockHashPromise,
|
|
283
|
+
]);
|
|
257
284
|
const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
|
|
258
285
|
|
|
286
|
+
// World state doesn't track checkpointed blocks or checkpoints themselves.
|
|
287
|
+
// but we use a block stream so we need to provide 'local' L2Tips.
|
|
288
|
+
// We configure the block stream to ignore checkpoints and set checkpoint values to genesis here.
|
|
289
|
+
const genesisCheckpointHeaderHash = GENESIS_CHECKPOINT_HEADER_HASH.toString();
|
|
290
|
+
const initialBlockHash = (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
|
|
259
291
|
return {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
292
|
+
proposed: latestBlockId,
|
|
293
|
+
checkpointed: {
|
|
294
|
+
block: { number: BlockNumber.ZERO, hash: initialBlockHash },
|
|
295
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
296
|
+
},
|
|
297
|
+
proposedCheckpoint: {
|
|
298
|
+
block: { number: BlockNumber.ZERO, hash: initialBlockHash },
|
|
299
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
300
|
+
},
|
|
301
|
+
finalized: {
|
|
302
|
+
block: { number: status.finalizedBlockNumber, hash: finalizedBlockHash ?? '' },
|
|
303
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
304
|
+
},
|
|
305
|
+
proven: {
|
|
306
|
+
block: { number: provenBlockNumber, hash: provenBlockHash ?? '' },
|
|
307
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
308
|
+
},
|
|
263
309
|
};
|
|
264
310
|
}
|
|
265
311
|
|
|
@@ -267,7 +313,7 @@ export class ServerWorldStateSynchronizer
|
|
|
267
313
|
public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
|
|
268
314
|
switch (event.type) {
|
|
269
315
|
case 'blocks-added':
|
|
270
|
-
await this.handleL2Blocks(event.blocks
|
|
316
|
+
await this.handleL2Blocks(event.blocks);
|
|
271
317
|
break;
|
|
272
318
|
case 'chain-pruned':
|
|
273
319
|
await this.handleChainPruned(event.block.number);
|
|
@@ -286,8 +332,8 @@ export class ServerWorldStateSynchronizer
|
|
|
286
332
|
* @param l2Blocks - The L2 blocks to handle.
|
|
287
333
|
* @returns Whether the block handled was produced by this same node.
|
|
288
334
|
*/
|
|
289
|
-
private async handleL2Blocks(l2Blocks:
|
|
290
|
-
this.log.
|
|
335
|
+
private async handleL2Blocks(l2Blocks: L2Block[]) {
|
|
336
|
+
this.log.debug(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
|
|
291
337
|
|
|
292
338
|
// Fetch the L1->L2 messages for the first block in a checkpoint.
|
|
293
339
|
const messagesForBlocks = new Map<BlockNumber, Fr[]>();
|
|
@@ -327,11 +373,13 @@ export class ServerWorldStateSynchronizer
|
|
|
327
373
|
* @param l1ToL2Messages - The L1 to L2 messages for the block.
|
|
328
374
|
* @returns Whether the block handled was produced by this same node.
|
|
329
375
|
*/
|
|
330
|
-
private async handleL2Block(l2Block:
|
|
331
|
-
this.log.
|
|
376
|
+
private async handleL2Block(l2Block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
|
|
377
|
+
this.log.debug(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
|
|
332
378
|
blockNumber: l2Block.number,
|
|
333
379
|
blockHash: await l2Block.hash().then(h => h.toString()),
|
|
334
380
|
l1ToL2Messages: l1ToL2Messages.map(msg => msg.toString()),
|
|
381
|
+
blockHeader: l2Block.header.toInspect(),
|
|
382
|
+
blockStats: l2Block.getStats(),
|
|
335
383
|
});
|
|
336
384
|
const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
|
|
337
385
|
|
|
@@ -345,16 +393,56 @@ export class ServerWorldStateSynchronizer
|
|
|
345
393
|
|
|
346
394
|
private async handleChainFinalized(blockNumber: BlockNumber) {
|
|
347
395
|
this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
|
|
396
|
+
// If the finalized block number is older than the oldest available block in world state,
|
|
397
|
+
// skip entirely. The finalized block number can jump backwards (e.g. when the finalization
|
|
398
|
+
// heuristic changes) and try to read block data that has already been pruned. When this
|
|
399
|
+
// happens, there is nothing useful to do — the native world state is already finalized
|
|
400
|
+
// past this point and pruning has already happened.
|
|
401
|
+
const currentSummary = await this.merkleTreeDb.getStatusSummary();
|
|
402
|
+
if (blockNumber < currentSummary.oldestHistoricalBlock || blockNumber < 1) {
|
|
403
|
+
this.log.trace(
|
|
404
|
+
`Finalized block ${blockNumber} is older than the oldest available block ${currentSummary.oldestHistoricalBlock}. Skipping.`,
|
|
405
|
+
);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
348
408
|
const summary = await this.merkleTreeDb.setFinalized(blockNumber);
|
|
349
409
|
if (this.historyToKeep === undefined) {
|
|
350
410
|
return;
|
|
351
411
|
}
|
|
352
|
-
const
|
|
353
|
-
if (
|
|
412
|
+
const finalisedBlockData = await this.l2BlockSource.getBlockData({ number: summary.finalizedBlockNumber });
|
|
413
|
+
if (finalisedBlockData === undefined) {
|
|
414
|
+
this.log.warn(
|
|
415
|
+
`Failed to retrieve checkpointed block for finalized block number: ${summary.finalizedBlockNumber}`,
|
|
416
|
+
);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
// Compute the required historic checkpoint number
|
|
420
|
+
const newHistoricCheckpointNumber = finalisedBlockData.checkpointNumber - this.historyToKeep + 1;
|
|
421
|
+
if (newHistoricCheckpointNumber <= 1) {
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
// Retrieve the historic checkpoint
|
|
425
|
+
const historicCheckpoint = await this.l2BlockSource.getCheckpoint({
|
|
426
|
+
number: CheckpointNumber(newHistoricCheckpointNumber),
|
|
427
|
+
});
|
|
428
|
+
if (!historicCheckpoint) {
|
|
429
|
+
this.log.warn(`Failed to retrieve checkpoint number ${newHistoricCheckpointNumber} from Archiver`);
|
|
354
430
|
return;
|
|
355
431
|
}
|
|
356
|
-
|
|
357
|
-
|
|
432
|
+
if (historicCheckpoint.checkpoint.blocks.length === 0 || historicCheckpoint.checkpoint.blocks[0] === undefined) {
|
|
433
|
+
this.log.warn(`Retrieved checkpoint number ${newHistoricCheckpointNumber} has no blocks!`);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
// Find the block at the start of the checkpoint and remove blocks up to this one
|
|
437
|
+
const newHistoricBlock = historicCheckpoint.checkpoint.blocks[0];
|
|
438
|
+
if (newHistoricBlock.number <= currentSummary.oldestHistoricalBlock) {
|
|
439
|
+
this.log.debug(
|
|
440
|
+
`Historic block ${newHistoricBlock.number} is not newer than oldest available ${currentSummary.oldestHistoricalBlock}. Skipping prune.`,
|
|
441
|
+
);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
|
|
445
|
+
const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock.number));
|
|
358
446
|
this.log.debug(`World state summary `, status.summary);
|
|
359
447
|
}
|
|
360
448
|
|
|
@@ -365,10 +453,11 @@ export class ServerWorldStateSynchronizer
|
|
|
365
453
|
}
|
|
366
454
|
|
|
367
455
|
private async handleChainPruned(blockNumber: BlockNumber) {
|
|
368
|
-
this.log.
|
|
456
|
+
this.log.info(`Chain pruned to block ${blockNumber}`);
|
|
369
457
|
const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
|
|
370
|
-
this.
|
|
371
|
-
|
|
458
|
+
if (this.provenBlockNumber !== undefined && this.provenBlockNumber > blockNumber) {
|
|
459
|
+
this.provenBlockNumber = undefined;
|
|
460
|
+
}
|
|
372
461
|
this.instrumentation.updateWorldStateMetrics(status);
|
|
373
462
|
}
|
|
374
463
|
|
package/src/test/utils.ts
CHANGED
|
@@ -5,10 +5,10 @@ import {
|
|
|
5
5
|
NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
|
|
6
6
|
} from '@aztec/constants';
|
|
7
7
|
import { asyncMap } from '@aztec/foundation/async-map';
|
|
8
|
-
import { BlockNumber, type CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
8
|
+
import { BlockNumber, type CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
|
|
9
9
|
import { padArrayEnd } from '@aztec/foundation/collection';
|
|
10
10
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
11
|
-
import {
|
|
11
|
+
import { L2Block } from '@aztec/stdlib/block';
|
|
12
12
|
import type {
|
|
13
13
|
IndexedTreeId,
|
|
14
14
|
MerkleTreeReadOperations,
|
|
@@ -20,7 +20,7 @@ import { BlockHeader } from '@aztec/stdlib/tx';
|
|
|
20
20
|
|
|
21
21
|
import type { NativeWorldStateService } from '../native/native_world_state.js';
|
|
22
22
|
|
|
23
|
-
export async function updateBlockState(block:
|
|
23
|
+
export async function updateBlockState(block: L2Block, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations) {
|
|
24
24
|
const insertData = async (
|
|
25
25
|
treeId: IndexedTreeId,
|
|
26
26
|
data: Buffer[][],
|
|
@@ -76,8 +76,8 @@ export async function mockBlock(
|
|
|
76
76
|
numL1ToL2Messages: number = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
|
|
77
77
|
isFirstBlockInCheckpoint: boolean = true,
|
|
78
78
|
) {
|
|
79
|
-
const block = await
|
|
80
|
-
indexWithinCheckpoint: isFirstBlockInCheckpoint ? 0 : 1,
|
|
79
|
+
const block = await L2Block.random(blockNum, {
|
|
80
|
+
indexWithinCheckpoint: isFirstBlockInCheckpoint ? IndexWithinCheckpoint(0) : IndexWithinCheckpoint(1),
|
|
81
81
|
txsPerBlock: size,
|
|
82
82
|
txOptions: { maxEffects },
|
|
83
83
|
});
|
|
@@ -92,7 +92,7 @@ export async function mockBlock(
|
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
export async function mockEmptyBlock(blockNum: BlockNumber, fork: MerkleTreeWriteOperations) {
|
|
95
|
-
const l2Block =
|
|
95
|
+
const l2Block = L2Block.empty();
|
|
96
96
|
const l1ToL2Messages = Array(16).fill(0).map(Fr.zero);
|
|
97
97
|
|
|
98
98
|
l2Block.header.globalVariables.blockNumber = blockNum;
|
package/src/testing.ts
CHANGED
|
@@ -3,22 +3,19 @@ import { Fr } from '@aztec/foundation/curves/bn254';
|
|
|
3
3
|
import { computeFeePayerBalanceLeafSlot } from '@aztec/protocol-contracts/fee-juice';
|
|
4
4
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
5
5
|
import { MerkleTreeId, PublicDataTreeLeaf } from '@aztec/stdlib/trees';
|
|
6
|
+
import type { GenesisData } from '@aztec/stdlib/world-state';
|
|
6
7
|
|
|
7
8
|
import { NativeWorldStateService } from './native/index.js';
|
|
8
9
|
|
|
9
|
-
async function generateGenesisValues(
|
|
10
|
-
if (!prefilledPublicData.length) {
|
|
10
|
+
async function generateGenesisValues(genesis: GenesisData) {
|
|
11
|
+
if (!genesis.prefilledPublicData.length && genesis.genesisTimestamp === 0n) {
|
|
11
12
|
return {
|
|
12
13
|
genesisArchiveRoot: new Fr(GENESIS_ARCHIVE_ROOT),
|
|
13
14
|
};
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
// Create a temporary world state to compute the genesis values.
|
|
17
|
-
const ws = await NativeWorldStateService.tmp(
|
|
18
|
-
undefined /* rollupAddress */,
|
|
19
|
-
true /* cleanupTmpDir */,
|
|
20
|
-
prefilledPublicData,
|
|
21
|
-
);
|
|
18
|
+
const ws = await NativeWorldStateService.tmp(undefined /* rollupAddress */, true /* cleanupTmpDir */, genesis);
|
|
22
19
|
const genesisArchiveRoot = new Fr((await ws.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
|
|
23
20
|
await ws.close();
|
|
24
21
|
|
|
@@ -33,6 +30,7 @@ export async function getGenesisValues(
|
|
|
33
30
|
initialAccounts: AztecAddress[],
|
|
34
31
|
initialAccountFeeJuice = defaultInitialAccountFeeJuice,
|
|
35
32
|
genesisPublicData: PublicDataTreeLeaf[] = [],
|
|
33
|
+
genesisTimestamp: bigint = 0n,
|
|
36
34
|
) {
|
|
37
35
|
// Top up the accounts with fee juice.
|
|
38
36
|
let prefilledPublicData = await Promise.all(
|
|
@@ -46,11 +44,12 @@ export async function getGenesisValues(
|
|
|
46
44
|
|
|
47
45
|
prefilledPublicData.sort((a, b) => (b.slot.lt(a.slot) ? 1 : -1));
|
|
48
46
|
|
|
49
|
-
const
|
|
47
|
+
const genesis: GenesisData = { prefilledPublicData, genesisTimestamp };
|
|
48
|
+
const { genesisArchiveRoot } = await generateGenesisValues(genesis);
|
|
50
49
|
|
|
51
50
|
return {
|
|
52
51
|
genesisArchiveRoot,
|
|
53
|
-
|
|
52
|
+
genesis,
|
|
54
53
|
fundingNeeded: BigInt(initialAccounts.length) * initialAccountFeeJuice.toBigInt(),
|
|
55
54
|
};
|
|
56
55
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/constants';
|
|
2
2
|
import type { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
3
|
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
import type { L2Block } from '@aztec/stdlib/block';
|
|
5
|
+
import type {
|
|
6
|
+
ForkMerkleTreeOperations,
|
|
7
|
+
MerkleTreeReadOperations,
|
|
8
|
+
ReadonlyWorldStateAccess,
|
|
9
|
+
} from '@aztec/stdlib/interfaces/server';
|
|
8
10
|
|
|
9
11
|
import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/message.js';
|
|
10
12
|
|
|
@@ -27,21 +29,13 @@ export const INITIAL_NULLIFIER_TREE_SIZE = 2 * MAX_NULLIFIERS_PER_TX;
|
|
|
27
29
|
|
|
28
30
|
export const INITIAL_PUBLIC_DATA_TREE_SIZE = 2 * MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
|
|
29
31
|
|
|
30
|
-
export
|
|
31
|
-
[MerkleTreeId.NULLIFIER_TREE]: IndexedTreeSnapshot;
|
|
32
|
-
[MerkleTreeId.NOTE_HASH_TREE]: TreeSnapshot<Fr>;
|
|
33
|
-
[MerkleTreeId.PUBLIC_DATA_TREE]: IndexedTreeSnapshot;
|
|
34
|
-
[MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: TreeSnapshot<Fr>;
|
|
35
|
-
[MerkleTreeId.ARCHIVE]: TreeSnapshot<Fr>;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
|
|
32
|
+
export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, ReadonlyWorldStateAccess {
|
|
39
33
|
/**
|
|
40
34
|
* Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
|
|
41
35
|
* @param block - The L2 block to handle.
|
|
42
36
|
* @param l1ToL2Messages - The L1 to L2 messages for the block.
|
|
43
37
|
*/
|
|
44
|
-
handleL2BlockAndMessages(block:
|
|
38
|
+
handleL2BlockAndMessages(block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull>;
|
|
45
39
|
|
|
46
40
|
/**
|
|
47
41
|
* Gets a handle that allows reading the latest committed state
|