Skip to content

Commit 9840241

Browse files
authored
feat: making SyncDataProvider throw before sync (#13151)
1 parent 6307ba0 commit 9840241

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

yarn-project/pxe/src/pxe_oracle_interface/pxe_oracle_interface.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,10 +649,7 @@ export class PXEOracleInterface implements ExecutionDataProvider {
649649
// in time of the locally synced state.
650650
// Note that while this technically results in historical queries, we perform it at the latest locally synced block
651651
// number which *should* be recent enough to be available, even for non-archive nodes.
652-
const syncedBlockNumber = (await this.syncDataProvider.getBlockNumber())!;
653-
if (syncedBlockNumber === undefined) {
654-
throw new Error(`Attempted to deliver a note with an unsynchronized PXE - this should never happen`);
655-
}
652+
const syncedBlockNumber = await this.syncDataProvider.getBlockNumber();
656653

657654
// By computing siloed and unique note hashes ourselves we prevent contracts from interfering with the note storage
658655
// of other contracts, which would constitute a security breach.

yarn-project/pxe/src/storage/sync_data_provider/sync_data_provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ export class SyncDataProvider implements DataProvider {
1616
await this.#synchronizedHeader.set(header.toBuffer());
1717
}
1818

19-
async getBlockNumber(): Promise<number | undefined> {
19+
async getBlockNumber(): Promise<number> {
2020
const headerBuffer = await this.#synchronizedHeader.getAsync();
2121
if (!headerBuffer) {
22-
return undefined;
22+
throw new Error(`Trying to get block number with a not-yet-synchronized PXE - this should never happen`);
2323
}
2424

2525
return Number(BlockHeader.fromBuffer(headerBuffer).globalVariables.blockNumber.toBigInt());

yarn-project/pxe/src/synchronizer/synchronizer.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
21
import { type Logger, createLogger } from '@aztec/foundation/log';
32
import type { L2TipsKVStore } from '@aztec/kv-store/stores';
43
import { L2BlockStream, type L2BlockStreamEvent, type L2BlockStreamEventHandler } from '@aztec/stdlib/block';
@@ -10,13 +9,11 @@ import type { SyncDataProvider } from '../storage/sync_data_provider/sync_data_p
109
import type { TaggingDataProvider } from '../storage/tagging_data_provider/tagging_data_provider.js';
1110

1211
/**
13-
* The Synchronizer class manages the synchronization with the aztec node, allowing PXE to retrieve the
14-
* latest block header and handle reorgs.
15-
* It provides methods to trigger a sync and get the block number we are syncec to
16-
* details, and fetch transactions by hash.
12+
* The Synchronizer class orchestrates synchronization between the PXE and Aztec node, maintaining an up-to-date
13+
* view of the L2 chain state. It handles block header retrieval, chain reorganizations, and provides an interface
14+
* for querying sync status.
1715
*/
1816
export class Synchronizer implements L2BlockStreamEventHandler {
19-
private initialSyncBlockNumber = INITIAL_L2_BLOCK_NUM - 1;
2017
private log: Logger;
2118
private isSyncing: Promise<void> | undefined;
2219
protected readonly blockStream: L2BlockStream;
@@ -80,7 +77,7 @@ export class Synchronizer implements L2BlockStreamEventHandler {
8077
}
8178

8279
/**
83-
* Syncs PXE and the node by dowloading the metadata of the latest blocks, allowing simulations to use
80+
* Syncs PXE and the node by downloading the metadata of the latest blocks, allowing simulations to use
8481
* recent data (e.g. notes), and handling any reorgs that might have occurred.
8582
*/
8683
public async sync() {
@@ -115,7 +112,7 @@ export class Synchronizer implements L2BlockStreamEventHandler {
115112
await this.blockStream.sync();
116113
}
117114

118-
public async getSynchedBlockNumber() {
119-
return (await this.syncDataProvider.getBlockNumber()) ?? this.initialSyncBlockNumber;
115+
public getSynchedBlockNumber() {
116+
return this.syncDataProvider.getBlockNumber();
120117
}
121118
}

0 commit comments

Comments
 (0)