Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions barretenberg/cpp/src/barretenberg/world_state/world_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,16 @@ WorldStateStatusFull WorldState::remove_historical_blocks(const index_t& toBlock
WorldStateRevision revision{ .forkId = CANONICAL_FORK_ID, .blockNumber = 0, .includeUncommitted = false };
TreeMetaResponse archive_state = get_tree_info(revision, MerkleTreeId::ARCHIVE);
if (toBlockNumber <= archive_state.meta.oldestHistoricBlock) {
throw std::runtime_error("Unable to remove historical block, block not found");
throw std::runtime_error(format("Unable to remove historical blocks to block number ",
toBlockNumber,
", blocks not found. Current oldest block: ",
archive_state.meta.oldestHistoricBlock));
}
WorldStateStatusFull status;
for (index_t blockNumber = archive_state.meta.oldestHistoricBlock; blockNumber < toBlockNumber; blockNumber++) {
if (!remove_historical_block(blockNumber, status)) {
throw std::runtime_error("Failed to remove historical block");
throw std::runtime_error(format(
"Failed to remove historical block ", blockNumber, " when removing blocks up to ", toBlockNumber));
}
}
populate_status_summary(status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ describe('L1Publisher integration', () => {
worldStateBlockCheckIntervalMS: 10000,
worldStateProvenBlocksOnly: false,
worldStateDbMapSizeKb: 10 * 1024 * 1024,
worldStateBlockHistory: 0,
};
worldStateSynchronizer = new ServerWorldStateSynchronizer(
builderDb,
Expand Down
1 change: 1 addition & 0 deletions yarn-project/foundation/src/config/env_var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export type EnvVar =
| 'SEQ_VIEM_POLLING_INTERVAL_MS'
| 'WS_DB_MAP_SIZE_KB'
| 'WS_DATA_DIRECTORY'
| 'WS_NUM_HISTORIC_BLOCKS'
| 'ETHEREUM_SLOT_DURATION'
| 'AZTEC_SLOT_DURATION'
| 'AZTEC_EPOCH_DURATION'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,11 @@ describe('NativeWorldState', () => {
}

//can't prune what has already been pruned
for (let i = 0; i < highestPrunedBlockNumber; i++) {
for (let i = 0; i <= highestPrunedBlockNumber; i++) {
await expect(ws.removeHistoricalBlocks(BigInt(i + 1))).rejects.toThrow(
'Unable to remove historical block, block not found',
`Unable to remove historical blocks to block number ${BigInt(
i + 1,
)}, blocks not found. Current oldest block: ${highestPrunedBlockNumber + 1}`,
);
}
});
Expand Down
15 changes: 14 additions & 1 deletion yarn-project/world-state/src/synchronizer/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type ConfigMappingsType, booleanConfigHelper, getConfigFromMappings } from '@aztec/foundation/config';
import {
type ConfigMappingsType,
booleanConfigHelper,
getConfigFromMappings,
numberConfigHelper,
} from '@aztec/foundation/config';

/** World State synchronizer configuration values. */
export interface WorldStateConfig {
Expand All @@ -16,6 +21,9 @@ export interface WorldStateConfig {

/** Optional directory for the world state DB, if unspecified will default to the general data directory */
worldStateDataDirectory?: string;

/** The number of historic blocks to maintain */
worldStateBlockHistory: number;
}

export const worldStateConfigMappings: ConfigMappingsType<WorldStateConfig> = {
Expand Down Expand Up @@ -44,6 +52,11 @@ export const worldStateConfigMappings: ConfigMappingsType<WorldStateConfig> = {
env: 'WS_DATA_DIRECTORY',
description: 'Optional directory for the world state database',
},
worldStateBlockHistory: {
env: 'WS_NUM_HISTORIC_BLOCKS',
description: 'The number of historic blocks to maintain. Values less than 1 mean all history is maintained',
...numberConfigHelper(64),
},
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe('ServerWorldStateSynchronizer', () => {
worldStateBlockCheckIntervalMS: 100,
worldStateProvenBlocksOnly: false,
worldStateDbMapSizeKb: 1024 * 1024,
worldStateBlockHistory: 0,
};

server = new TestWorldStateSynchronizer(merkleTreeDb, blockAndMessagesSource, config, l2BlockStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class ServerWorldStateSynchronizer
private readonly merkleTreeCommitted: MerkleTreeReadOperations;

private latestBlockNumberAtStart = 0;
private historyToKeep: number | undefined;
private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
private latestBlockHashQuery: { blockNumber: number; hash: string | undefined } | undefined = undefined;

Expand All @@ -57,6 +58,12 @@ export class ServerWorldStateSynchronizer
) {
this.instrumentation = new WorldStateInstrumentation(telemetry);
this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
this.historyToKeep = config.worldStateBlockHistory < 1 ? undefined : config.worldStateBlockHistory;
this.log.info(
`Created world state synchroniser with block history of ${
this.historyToKeep === undefined ? 'infinity' : this.historyToKeep
}`,
);
}

public getCommitted(): MerkleTreeReadOperations {
Expand Down Expand Up @@ -266,7 +273,16 @@ export class ServerWorldStateSynchronizer

private async handleChainFinalized(blockNumber: number) {
this.log.verbose(`Chain finalized at block ${blockNumber}`);
await this.merkleTreeDb.setFinalised(BigInt(blockNumber));
const summary = await this.merkleTreeDb.setFinalised(BigInt(blockNumber));
if (this.historyToKeep === undefined) {
return;
}
const newHistoricBlock = summary.finalisedBlockNumber - BigInt(this.historyToKeep) + 1n;
if (newHistoricBlock <= 1) {
return;
}
this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
}

private handleChainProven(blockNumber: number) {
Expand Down
1 change: 1 addition & 0 deletions yarn-project/world-state/src/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('world-state integration', () => {
worldStateProvenBlocksOnly: false,
worldStateBlockRequestBatchSize: 5,
worldStateDbMapSizeKb: 1024 * 1024,
worldStateBlockHistory: 0,
};

archiver = new MockPrefilledArchiver(blocks, messages);
Expand Down