@aztec/world-state 0.0.1-commit.aada20e3 → 0.0.1-commit.b2a5d0dd1
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/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/merkle_trees_facade.d.ts +7 -6
- package/dest/native/merkle_trees_facade.d.ts.map +1 -1
- package/dest/native/merkle_trees_facade.js +17 -9
- package/dest/native/message.d.ts +13 -6
- package/dest/native/message.d.ts.map +1 -1
- package/dest/native/native_world_state.d.ts +7 -5
- package/dest/native/native_world_state.d.ts.map +1 -1
- package/dest/native/native_world_state.js +17 -12
- package/dest/native/native_world_state_instance.d.ts +4 -4
- package/dest/native/native_world_state_instance.d.ts.map +1 -1
- package/dest/native/native_world_state_instance.js +8 -7
- package/dest/native/world_state_ops_queue.js +5 -5
- package/dest/synchronizer/config.d.ts +3 -3
- package/dest/synchronizer/config.d.ts.map +1 -1
- package/dest/synchronizer/config.js +13 -10
- package/dest/synchronizer/factory.d.ts +5 -5
- package/dest/synchronizer/factory.d.ts.map +1 -1
- package/dest/synchronizer/factory.js +6 -5
- package/dest/synchronizer/server_world_state_synchronizer.d.ts +4 -4
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.js +86 -17
- 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 +1 -10
- package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
- package/package.json +9 -10
- package/src/native/fork_checkpoint.ts +19 -3
- package/src/native/merkle_trees_facade.ts +24 -12
- package/src/native/message.ts +14 -5
- package/src/native/native_world_state.ts +15 -21
- package/src/native/native_world_state_instance.ts +10 -6
- package/src/native/world_state_ops_queue.ts +5 -5
- package/src/synchronizer/config.ts +14 -12
- package/src/synchronizer/factory.ts +7 -7
- package/src/synchronizer/server_world_state_synchronizer.ts +97 -19
- package/src/testing.ts +8 -9
- package/src/world-state-db/merkle_tree_db.ts +0 -10
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
1
|
+
import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
3
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
7
|
import {
|
|
8
|
+
type BlockHash,
|
|
9
|
+
GENESIS_BLOCK_HEADER_HASH,
|
|
8
10
|
GENESIS_CHECKPOINT_HEADER_HASH,
|
|
9
11
|
type L2Block,
|
|
10
12
|
type L2BlockId,
|
|
@@ -64,7 +66,7 @@ export class ServerWorldStateSynchronizer
|
|
|
64
66
|
private readonly log: Logger = createLogger('world_state'),
|
|
65
67
|
) {
|
|
66
68
|
this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
|
|
67
|
-
this.historyToKeep = config.
|
|
69
|
+
this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
|
|
68
70
|
this.log.info(
|
|
69
71
|
`Created world state synchroniser with block history of ${
|
|
70
72
|
this.historyToKeep === undefined ? 'infinity' : this.historyToKeep
|
|
@@ -177,13 +179,10 @@ export class ServerWorldStateSynchronizer
|
|
|
177
179
|
/**
|
|
178
180
|
* Forces an immediate sync.
|
|
179
181
|
* @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
|
|
180
|
-
* @param
|
|
182
|
+
* @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
|
|
181
183
|
* @returns A promise that resolves with the block number the world state was synced to
|
|
182
184
|
*/
|
|
183
|
-
public async syncImmediate(
|
|
184
|
-
targetBlockNumber?: BlockNumber,
|
|
185
|
-
skipThrowIfTargetNotReached?: boolean,
|
|
186
|
-
): Promise<BlockNumber> {
|
|
185
|
+
public async syncImmediate(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber> {
|
|
187
186
|
if (this.currentState !== WorldStateRunningState.RUNNING) {
|
|
188
187
|
throw new Error(`World State is not running. Unable to perform sync.`);
|
|
189
188
|
}
|
|
@@ -195,7 +194,19 @@ export class ServerWorldStateSynchronizer
|
|
|
195
194
|
// If we have been given a block number to sync to and we have reached that number then return
|
|
196
195
|
const currentBlockNumber = await this.getLatestBlockNumber();
|
|
197
196
|
if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
|
|
198
|
-
|
|
197
|
+
if (blockHash === undefined) {
|
|
198
|
+
return currentBlockNumber;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// If a block hash was provided, verify we're on the expected fork
|
|
202
|
+
const currentHash = await this.getL2BlockHash(targetBlockNumber);
|
|
203
|
+
if (currentHash === blockHash.toString()) {
|
|
204
|
+
return currentBlockNumber;
|
|
205
|
+
}
|
|
206
|
+
// Hash mismatch: a reorg may have occurred, fall through to trigger sync
|
|
207
|
+
this.log.debug(
|
|
208
|
+
`World state block hash mismatch at ${targetBlockNumber} (expected ${blockHash}, got ${currentHash}). Triggering resync.`,
|
|
209
|
+
);
|
|
199
210
|
}
|
|
200
211
|
this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
|
|
201
212
|
|
|
@@ -213,7 +224,7 @@ export class ServerWorldStateSynchronizer
|
|
|
213
224
|
|
|
214
225
|
// If we have been given a block number to sync to and we have not reached that number then fail
|
|
215
226
|
const updatedBlockNumber = await this.getLatestBlockNumber();
|
|
216
|
-
if (
|
|
227
|
+
if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
|
|
217
228
|
throw new WorldStateSynchronizerError(
|
|
218
229
|
`Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`,
|
|
219
230
|
{
|
|
@@ -227,6 +238,24 @@ export class ServerWorldStateSynchronizer
|
|
|
227
238
|
);
|
|
228
239
|
}
|
|
229
240
|
|
|
241
|
+
// If a block hash was provided, verify we're on the expected fork after syncing, throw otherwise
|
|
242
|
+
if (blockHash !== undefined && targetBlockNumber !== undefined) {
|
|
243
|
+
const updatedHash = await this.getL2BlockHash(targetBlockNumber);
|
|
244
|
+
if (updatedHash !== blockHash.toString()) {
|
|
245
|
+
throw new WorldStateSynchronizerError(
|
|
246
|
+
`Block hash mismatch at block ${targetBlockNumber} (expected ${blockHash} but got ${updatedHash})`,
|
|
247
|
+
{
|
|
248
|
+
cause: {
|
|
249
|
+
reason: 'block_hash_mismatch',
|
|
250
|
+
targetBlockNumber,
|
|
251
|
+
expectedHash: blockHash.toString(),
|
|
252
|
+
actualHash: updatedHash,
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
230
259
|
return updatedBlockNumber;
|
|
231
260
|
}
|
|
232
261
|
|
|
@@ -263,15 +292,19 @@ export class ServerWorldStateSynchronizer
|
|
|
263
292
|
proposed: latestBlockId,
|
|
264
293
|
checkpointed: {
|
|
265
294
|
block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
|
|
266
|
-
checkpoint: { number:
|
|
295
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
296
|
+
},
|
|
297
|
+
proposedCheckpoint: {
|
|
298
|
+
block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
|
|
299
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
267
300
|
},
|
|
268
301
|
finalized: {
|
|
269
302
|
block: { number: status.finalizedBlockNumber, hash: finalizedBlockHash ?? '' },
|
|
270
|
-
checkpoint: { number:
|
|
303
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
271
304
|
},
|
|
272
305
|
proven: {
|
|
273
306
|
block: { number: provenBlockNumber, hash: provenBlockHash ?? '' },
|
|
274
|
-
checkpoint: { number:
|
|
307
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
275
308
|
},
|
|
276
309
|
};
|
|
277
310
|
}
|
|
@@ -360,16 +393,59 @@ export class ServerWorldStateSynchronizer
|
|
|
360
393
|
|
|
361
394
|
private async handleChainFinalized(blockNumber: BlockNumber) {
|
|
362
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
|
+
}
|
|
363
408
|
const summary = await this.merkleTreeDb.setFinalized(blockNumber);
|
|
364
409
|
if (this.historyToKeep === undefined) {
|
|
365
410
|
return;
|
|
366
411
|
}
|
|
367
|
-
|
|
368
|
-
|
|
412
|
+
// Get the checkpointed block for the finalized block number
|
|
413
|
+
const finalisedCheckpoint = await this.l2BlockSource.getCheckpointedBlock(summary.finalizedBlockNumber);
|
|
414
|
+
if (finalisedCheckpoint === undefined) {
|
|
415
|
+
this.log.warn(
|
|
416
|
+
`Failed to retrieve checkpointed block for finalized block number: ${summary.finalizedBlockNumber}`,
|
|
417
|
+
);
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
// Compute the required historic checkpoint number
|
|
421
|
+
const newHistoricCheckpointNumber = finalisedCheckpoint.checkpointNumber - this.historyToKeep + 1;
|
|
422
|
+
if (newHistoricCheckpointNumber <= 1) {
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
// Retrieve the historic checkpoint
|
|
426
|
+
const historicCheckpoints = await this.l2BlockSource.getCheckpoints(
|
|
427
|
+
CheckpointNumber(newHistoricCheckpointNumber),
|
|
428
|
+
1,
|
|
429
|
+
);
|
|
430
|
+
if (historicCheckpoints.length === 0 || historicCheckpoints[0] === undefined) {
|
|
431
|
+
this.log.warn(`Failed to retrieve checkpoint number ${newHistoricCheckpointNumber} from Archiver`);
|
|
369
432
|
return;
|
|
370
433
|
}
|
|
371
|
-
|
|
372
|
-
|
|
434
|
+
const historicCheckpoint = historicCheckpoints[0];
|
|
435
|
+
if (historicCheckpoint.checkpoint.blocks.length === 0 || historicCheckpoint.checkpoint.blocks[0] === undefined) {
|
|
436
|
+
this.log.warn(`Retrieved checkpoint number ${newHistoricCheckpointNumber} has no blocks!`);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
// Find the block at the start of the checkpoint and remove blocks up to this one
|
|
440
|
+
const newHistoricBlock = historicCheckpoint.checkpoint.blocks[0];
|
|
441
|
+
if (newHistoricBlock.number <= currentSummary.oldestHistoricalBlock) {
|
|
442
|
+
this.log.debug(
|
|
443
|
+
`Historic block ${newHistoricBlock.number} is not newer than oldest available ${currentSummary.oldestHistoricalBlock}. Skipping prune.`,
|
|
444
|
+
);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
|
|
448
|
+
const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock.number));
|
|
373
449
|
this.log.debug(`World state summary `, status.summary);
|
|
374
450
|
}
|
|
375
451
|
|
|
@@ -380,9 +456,11 @@ export class ServerWorldStateSynchronizer
|
|
|
380
456
|
}
|
|
381
457
|
|
|
382
458
|
private async handleChainPruned(blockNumber: BlockNumber) {
|
|
383
|
-
this.log.
|
|
459
|
+
this.log.info(`Chain pruned to block ${blockNumber}`);
|
|
384
460
|
const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
|
|
385
|
-
this.provenBlockNumber
|
|
461
|
+
if (this.provenBlockNumber !== undefined && this.provenBlockNumber > blockNumber) {
|
|
462
|
+
this.provenBlockNumber = undefined;
|
|
463
|
+
}
|
|
386
464
|
this.instrumentation.updateWorldStateMetrics(status);
|
|
387
465
|
}
|
|
388
466
|
|
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,14 +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 { IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
|
|
5
4
|
import type { L2Block } from '@aztec/stdlib/block';
|
|
6
5
|
import type {
|
|
7
6
|
ForkMerkleTreeOperations,
|
|
8
7
|
MerkleTreeReadOperations,
|
|
9
8
|
ReadonlyWorldStateAccess,
|
|
10
9
|
} from '@aztec/stdlib/interfaces/server';
|
|
11
|
-
import type { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
12
10
|
|
|
13
11
|
import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/message.js';
|
|
14
12
|
|
|
@@ -31,14 +29,6 @@ export const INITIAL_NULLIFIER_TREE_SIZE = 2 * MAX_NULLIFIERS_PER_TX;
|
|
|
31
29
|
|
|
32
30
|
export const INITIAL_PUBLIC_DATA_TREE_SIZE = 2 * MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
|
|
33
31
|
|
|
34
|
-
export type TreeSnapshots = {
|
|
35
|
-
[MerkleTreeId.NULLIFIER_TREE]: IndexedTreeSnapshot;
|
|
36
|
-
[MerkleTreeId.NOTE_HASH_TREE]: TreeSnapshot<Fr>;
|
|
37
|
-
[MerkleTreeId.PUBLIC_DATA_TREE]: IndexedTreeSnapshot;
|
|
38
|
-
[MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: TreeSnapshot<Fr>;
|
|
39
|
-
[MerkleTreeId.ARCHIVE]: TreeSnapshot<Fr>;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
32
|
export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, ReadonlyWorldStateAccess {
|
|
43
33
|
/**
|
|
44
34
|
* Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
|