import { BlockNumber, BlockNumberSchema } from '@aztec/foundation/branded-types'; import type { PromiseWithResolvers } from '@aztec/foundation/promise'; import { z } from 'zod'; import type { BlockHash } from '../block/block_hash.js'; import type { SnapshotDataKeys } from '../snapshots/types.js'; import type { MerkleTreeReadOperations, MerkleTreeWriteOperations } from './merkle_tree_operations.js'; export type { SnapshotDataKeys }; /** * Defines the possible states of the world state synchronizer. */ export enum WorldStateRunningState { IDLE, SYNCHING, RUNNING, STOPPED, } export interface WorldStateSyncStatus { latestBlockNumber: BlockNumber; latestBlockHash: string; finalizedBlockNumber: BlockNumber; oldestHistoricBlockNumber: BlockNumber; treesAreSynched: boolean; } /** * Defines the status of the world state synchronizer. */ export interface WorldStateSynchronizerStatus { /** * The current state of the world state synchronizer. */ state: WorldStateRunningState; /** * The block numbers that the world state synchronizer is synced to. */ syncSummary: WorldStateSyncStatus; } /** Provides writeable forks of the world state at a given block number. */ export interface ForkMerkleTreeOperations { /** * Forks the world state at the given block number, defaulting to the latest one. * @param block - The block number to fork at. * @param opts - Optional parameters: * - closeDelayMs: number of milliseconds to wait before closing the fork on dispose. */ fork(block?: BlockNumber, opts?: { closeDelayMs?: number }): Promise; /** Backups the db to the target path. */ backupTo(dstPath: string, compact?: boolean): Promise, string>>; } export interface ReadonlyWorldStateAccess { /** Returns an instance of MerkleTreeAdminOperations that will not include uncommitted data. */ getCommitted(): MerkleTreeReadOperations; /** Gets a handle that allows reading the state as it was at the given block number. */ getSnapshot(blockNumber: number): MerkleTreeReadOperations; } /** Defines the interface for a world state synchronizer. */ export interface WorldStateSynchronizer extends ReadonlyWorldStateAccess, ForkMerkleTreeOperations { /** Starts the synchronizer. */ start(): Promise>; /** Returns the current status of the synchronizer. */ status(): Promise; /** Stops the synchronizer and its database. */ stop(): Promise; /** Stops the synchronizer from syncing, but keeps the database online. */ stopSync(): Promise; /** Resumes synching after a stopSync call. */ resumeSync(): void; /** * Forces an immediate sync to an optionally provided minimum block number. * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it. * @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection). * @returns A promise that resolves with the block number the world state was synced to */ syncImmediate(minBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise; /** Deletes the db */ clear(): Promise; } export const WorldStateSyncStatusSchema: z.ZodType = z.object({ finalizedBlockNumber: BlockNumberSchema, latestBlockNumber: BlockNumberSchema, latestBlockHash: z.string(), oldestHistoricBlockNumber: BlockNumberSchema, treesAreSynched: z.boolean(), });