diff --git a/yarn-project/archiver/src/archiver/archiver.test.ts b/yarn-project/archiver/src/archiver/archiver.test.ts index 80773cb89d00..b57d2fcdfceb 100644 --- a/yarn-project/archiver/src/archiver/archiver.test.ts +++ b/yarn-project/archiver/src/archiver/archiver.test.ts @@ -29,6 +29,7 @@ import { MemoryArchiverStore } from './memory_archiver_store/memory_archiver_sto interface MockRollupContractRead { archiveAt: (args: readonly [bigint]) => Promise<`0x${string}`>; + getProvenBlockNumber: () => Promise; } describe('Archiver', () => { @@ -40,9 +41,9 @@ describe('Archiver', () => { let publicClient: MockProxy>; let instrumentation: MockProxy; let archiverStore: ArchiverDataStore; - let proverId: Fr; let now: number; + let rollupRead: MockProxy; let archiver: Archiver; let blocks: L2Block[]; @@ -56,7 +57,6 @@ describe('Archiver', () => { instrumentation = mock({ isEnabled: () => true }); archiverStore = new MemoryArchiverStore(1000); - proverId = Fr.random(); archiver = new Archiver( publicClient, @@ -70,9 +70,11 @@ describe('Archiver', () => { blocks = blockNumbers.map(x => L2Block.random(x, 4, x, x + 1, 2, 2)); - ((archiver as any).rollup as any).read = mock({ + rollupRead = mock({ archiveAt: (args: readonly [bigint]) => Promise.resolve(blocks[Number(args[0] - 1n)].archive.root.toString()), }); + + ((archiver as any).rollup as any).read = rollupRead; }); afterEach(async () => { @@ -91,9 +93,10 @@ describe('Archiver', () => { mockGetLogs({ messageSent: [makeMessageSentEvent(98n, 1n, 0n), makeMessageSentEvent(99n, 1n, 1n)], L2BlockProposed: [makeL2BlockProposedEvent(101n, 1n, blocks[0].archive.root.toString())], - proofVerified: [makeProofVerifiedEvent(102n, 1n, proverId)], }); + rollupRead.getProvenBlockNumber.mockResolvedValueOnce(1n); + mockGetLogs({ messageSent: [ makeMessageSentEvent(2504n, 2n, 0n), @@ -175,11 +178,6 @@ describe('Archiver', () => { // Check getting only proven blocks expect((await archiver.getBlocks(1, 100)).map(b => b.number)).toEqual([1, 2, 3]); expect((await archiver.getBlocks(1, 100, true)).map(b => b.number)).toEqual([1]); - - // Check instrumentation of proven blocks - expect(instrumentation.processProofsVerified).toHaveBeenCalledWith([ - { delay: 1000n, l1BlockNumber: 102n, l2BlockNumber: 1n, proverId: proverId.toString() }, - ]); }, 10_000); it('does not sync past current block number', async () => { @@ -259,12 +257,10 @@ describe('Archiver', () => { const mockGetLogs = (logs: { messageSent?: ReturnType[]; L2BlockProposed?: ReturnType[]; - proofVerified?: ReturnType[]; }) => { publicClient.getLogs .mockResolvedValueOnce(logs.messageSent ?? []) - .mockResolvedValueOnce(logs.L2BlockProposed ?? []) - .mockResolvedValueOnce(logs.proofVerified ?? []); + .mockResolvedValueOnce(logs.L2BlockProposed ?? []); }; }); @@ -300,16 +296,6 @@ function makeMessageSentEvent(l1BlockNum: bigint, l2BlockNumber: bigint, index: } as Log; } -function makeProofVerifiedEvent(l1BlockNum: bigint, l2BlockNumber: bigint, proverId: Fr) { - return { - blockNumber: l1BlockNum, - args: { - blockNumber: l2BlockNumber, - proverId: proverId.toString(), - }, - } as Log; -} - /** * Makes a fake rollup tx for testing purposes. * @param block - The L2Block. diff --git a/yarn-project/archiver/src/archiver/archiver.ts b/yarn-project/archiver/src/archiver/archiver.ts index d5a2962c448b..62797c70a136 100644 --- a/yarn-project/archiver/src/archiver/archiver.ts +++ b/yarn-project/archiver/src/archiver/archiver.ts @@ -24,7 +24,6 @@ import { import { createEthereumChain } from '@aztec/ethereum'; import { type ContractArtifact } from '@aztec/foundation/abi'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; -import { compactArray, unique } from '@aztec/foundation/collection'; import { type EthAddress } from '@aztec/foundation/eth-address'; import { Fr } from '@aztec/foundation/fields'; import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log'; @@ -55,12 +54,7 @@ import { import { type ArchiverDataStore } from './archiver_store.js'; import { type ArchiverConfig } from './config.js'; -import { - getL1BlockTime, - retrieveBlockFromRollup, - retrieveL1ToL2Messages, - retrieveL2ProofVerifiedEvents, -} from './data_retrieval.js'; +import { retrieveBlockFromRollup, retrieveL1ToL2Messages } from './data_retrieval.js'; import { ArchiverInstrumentation } from './instrumentation.js'; import { type SingletonDataRetrieval } from './structs/data_retrieval.js'; @@ -206,31 +200,12 @@ export class Archiver implements ArchiveSource { * This code does not handle reorgs. */ const { - blockBodiesSynchedTo = this.l1StartBlock, blocksSynchedTo = this.l1StartBlock, messagesSynchedTo = this.l1StartBlock, provenLogsSynchedTo = this.l1StartBlock, } = await this.store.getSynchPoint(); const currentL1BlockNumber = await this.publicClient.getBlockNumber(); - if ( - currentL1BlockNumber <= blocksSynchedTo && - currentL1BlockNumber <= messagesSynchedTo && - currentL1BlockNumber <= blockBodiesSynchedTo && - currentL1BlockNumber <= provenLogsSynchedTo - ) { - // chain hasn't moved forward - // or it's been rolled back - this.log.debug(`Nothing to sync`, { - currentL1BlockNumber, - blocksSynchedTo, - messagesSynchedTo, - provenLogsSynchedTo, - blockBodiesSynchedTo, - }); - return; - } - // ********** Ensuring Consistency of data pulled from L1 ********** /** @@ -250,9 +225,24 @@ export class Archiver implements ArchiveSource { * in future but for the time being it should give us the guarantees that we need */ + await this.updateLastProvenL2Block(provenLogsSynchedTo, currentL1BlockNumber); + // ********** Events that are processed per L1 block ********** + await this.handleL1ToL2Messages(blockUntilSynced, messagesSynchedTo, currentL1BlockNumber); + // ********** Events that are processed per L2 block ********** + await this.handleL2blocks(blockUntilSynced, blocksSynchedTo, currentL1BlockNumber); + } + + private async handleL1ToL2Messages( + blockUntilSynced: boolean, + messagesSynchedTo: bigint, + currentL1BlockNumber: bigint, + ) { + if (currentL1BlockNumber <= messagesSynchedTo) { + return; + } const retrievedL1ToL2Messages = await retrieveL1ToL2Messages( this.inbox, @@ -262,14 +252,34 @@ export class Archiver implements ArchiveSource { ); if (retrievedL1ToL2Messages.retrievedData.length !== 0) { + await this.store.addL1ToL2Messages(retrievedL1ToL2Messages); + this.log.verbose( `Retrieved ${retrievedL1ToL2Messages.retrievedData.length} new L1 -> L2 messages between L1 blocks ${ messagesSynchedTo + 1n } and ${currentL1BlockNumber}.`, ); } + } - await this.store.addL1ToL2Messages(retrievedL1ToL2Messages); + private async updateLastProvenL2Block(provenSynchedTo: bigint, currentL1BlockNumber: bigint) { + if (currentL1BlockNumber <= provenSynchedTo) { + return; + } + + const provenBlockNumber = await this.rollup.read.getProvenBlockNumber(); + if (provenBlockNumber) { + await this.store.setProvenL2BlockNumber({ + retrievedData: Number(provenBlockNumber), + lastProcessedL1BlockNumber: currentL1BlockNumber, + }); + } + } + + private async handleL2blocks(blockUntilSynced: boolean, blocksSynchedTo: bigint, currentL1BlockNumber: bigint) { + if (currentL1BlockNumber <= blocksSynchedTo) { + return; + } this.log.debug(`Retrieving blocks from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`); const retrievedBlocks = await retrieveBlockFromRollup( @@ -281,8 +291,6 @@ export class Archiver implements ArchiveSource { this.log, ); - // Add the body - (retrievedBlocks.length ? this.log.verbose : this.log.debug)( `Retrieved ${retrievedBlocks.length || 'no'} new L2 blocks between L1 blocks ${ blocksSynchedTo + 1n @@ -298,13 +306,16 @@ export class Archiver implements ArchiveSource { .join(',')} with last processed L1 block ${lastProcessedL1BlockNumber}`, ); + // If we actually received something, we will use it. if (retrievedBlocks.length > 0) { await Promise.all( retrievedBlocks.map(block => { - const noteEncryptedLogs = block.data.body.noteEncryptedLogs; - const encryptedLogs = block.data.body.encryptedLogs; - const unencryptedLogs = block.data.body.unencryptedLogs; - return this.store.addLogs(noteEncryptedLogs, encryptedLogs, unencryptedLogs, block.data.number); + return this.store.addLogs( + block.data.body.noteEncryptedLogs, + block.data.body.encryptedLogs, + block.data.body.unencryptedLogs, + block.data.number, + ); }), ); @@ -321,10 +332,6 @@ export class Archiver implements ArchiveSource { ); const timer = new Timer(); - await this.store.addBlockBodies({ - lastProcessedL1BlockNumber: lastProcessedL1BlockNumber, - retrievedData: retrievedBlocks.map(b => b.data.body), - }); await this.store.addBlocks(retrievedBlocks); this.instrumentation.processNewBlocks( timer.ms() / retrievedBlocks.length, @@ -334,93 +341,11 @@ export class Archiver implements ArchiveSource { this.log.verbose(`Processed ${retrievedBlocks.length} new L2 blocks up to ${lastL2BlockNumber}`); } - // Fetch the logs for proven blocks in the block range and update the last proven block number. - if (currentL1BlockNumber > provenLogsSynchedTo) { - await this.updateLastProvenL2Block(provenLogsSynchedTo + 1n, currentL1BlockNumber); - } - if (retrievedBlocks.length > 0 || blockUntilSynced) { (blockUntilSynced ? this.log.info : this.log.verbose)(`Synced to L1 block ${currentL1BlockNumber}`); } } - private async updateLastProvenL2Block(fromBlock: bigint, toBlock: bigint) { - const logs = await retrieveL2ProofVerifiedEvents(this.publicClient, this.rollupAddress, fromBlock, toBlock); - const lastLog = logs[logs.length - 1]; - if (!lastLog) { - return; - } - - const provenBlockNumber = lastLog.l2BlockNumber; - if (!provenBlockNumber) { - throw new Error(`Missing argument blockNumber from L2ProofVerified event`); - } - - await this.emitProofVerifiedMetrics(logs); - - const currentProvenBlockNumber = await this.store.getProvenL2BlockNumber(); - if (provenBlockNumber > currentProvenBlockNumber) { - // Update the last proven block number - this.log.verbose(`Updated last proven block number from ${currentProvenBlockNumber} to ${provenBlockNumber}`); - await this.store.setProvenL2BlockNumber({ - retrievedData: Number(provenBlockNumber), - lastProcessedL1BlockNumber: lastLog.l1BlockNumber, - }); - this.instrumentation.updateLastProvenBlock(Number(provenBlockNumber)); - } else { - // We set the last processed L1 block number to the last L1 block number in the range to avoid duplicate processing - await this.store.setProvenL2BlockNumber({ - retrievedData: Number(currentProvenBlockNumber), - lastProcessedL1BlockNumber: lastLog.l1BlockNumber, - }); - } - } - - /** - * Emits as metrics the block number proven, who proved it, and how much time passed since it was submitted. - * @param logs - The ProofVerified logs to emit metrics for, as collected from `retrieveL2ProofVerifiedEvents`. - **/ - private async emitProofVerifiedMetrics(logs: { l1BlockNumber: bigint; l2BlockNumber: bigint; proverId: Fr }[]) { - if (!logs.length || !this.instrumentation.isEnabled()) { - return; - } - - const l1BlockTimes = new Map( - await Promise.all( - unique(logs.map(log => log.l1BlockNumber)).map( - async blockNumber => [blockNumber, await getL1BlockTime(this.publicClient, blockNumber)] as const, - ), - ), - ); - - // Collect L2 block times for all the blocks verified, this is the time in which the block proven was - // originally submitted to L1, using the L1 timestamp of the transaction. - const getL2BlockTime = async (blockNumber: bigint) => - (await this.store.getBlocks(Number(blockNumber), 1))[0]?.l1.timestamp; - - const l2BlockTimes = new Map( - await Promise.all( - unique(logs.map(log => log.l2BlockNumber)).map( - async blockNumber => [blockNumber, await getL2BlockTime(blockNumber)] as const, - ), - ), - ); - - // Emit the prover id and the time difference between block submission and proof. - this.instrumentation.processProofsVerified( - compactArray( - logs.map(log => { - const l1BlockTime = l1BlockTimes.get(log.l1BlockNumber)!; - const l2BlockTime = l2BlockTimes.get(log.l2BlockNumber); - if (!l2BlockTime) { - return undefined; - } - return { ...log, delay: l1BlockTime - l2BlockTime, proverId: log.proverId.toString() }; - }), - ), - ); - } - /** * Extracts and stores contract classes out of ContractClassRegistered events emitted by the class registerer contract. * @param allLogs - All logs emitted in a bunch of blocks. diff --git a/yarn-project/archiver/src/archiver/archiver_store.ts b/yarn-project/archiver/src/archiver/archiver_store.ts index 2d1f5e524255..b6da328fc64a 100644 --- a/yarn-project/archiver/src/archiver/archiver_store.ts +++ b/yarn-project/archiver/src/archiver/archiver_store.ts @@ -1,5 +1,4 @@ import { - type Body, type EncryptedL2BlockL2Logs, type EncryptedNoteL2BlockL2Logs, type FromLogType, @@ -33,8 +32,6 @@ import { type L1Published } from './structs/published.js'; export type ArchiverL1SynchPoint = { /** Number of the last L1 block that added a new L2 block metadata. */ blocksSynchedTo?: bigint; - /** Number of the last L1 block that added a new L2 block body. */ - blockBodiesSynchedTo?: bigint; /** Number of the last L1 block that added L1 -> L2 messages from the Inbox. */ messagesSynchedTo?: bigint; /** Number of the last L1 block that added a new proven block. */ @@ -53,21 +50,6 @@ export interface ArchiverDataStore { */ addBlocks(blocks: L1Published[]): Promise; - /** - * Append new block bodies to the store's list. - * @param blockBodies - The L2 block bodies to be added to the store. - * @returns True if the operation is successful. - */ - addBlockBodies(blockBodies: DataRetrieval): Promise; - - /** - * Gets block bodies that have the same txsEffectsHashes as we supply. - * - * @param txsEffectsHashes - A list of txsEffectsHashes. - * @returns The requested L2 block bodies - */ - getBlockBodies(txsEffectsHashes: Buffer[]): Promise<(Body | undefined)[]>; - /** * Gets up to `limit` amount of L2 blocks starting from `from`. * @param from - Number of the first block to return (inclusive). diff --git a/yarn-project/archiver/src/archiver/archiver_store_test_suite.ts b/yarn-project/archiver/src/archiver/archiver_store_test_suite.ts index 48b118a67b8b..4acaaeb9b39b 100644 --- a/yarn-project/archiver/src/archiver/archiver_store_test_suite.ts +++ b/yarn-project/archiver/src/archiver/archiver_store_test_suite.ts @@ -1,4 +1,4 @@ -import { type Body, InboxLeaf, L2Block, LogId, LogType, TxHash } from '@aztec/circuit-types'; +import { InboxLeaf, L2Block, LogId, LogType, TxHash } from '@aztec/circuit-types'; import '@aztec/circuit-types/jest'; import { AztecAddress, Fr, INITIAL_L2_BLOCK_NUM, L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/circuits.js'; import { @@ -15,7 +15,6 @@ import { } from '@aztec/types/contracts'; import { type ArchiverDataStore, type ArchiverL1SynchPoint } from './archiver_store.js'; -import { type DataRetrieval } from './structs/data_retrieval.js'; import { type L1Published } from './structs/published.js'; /** @@ -26,7 +25,6 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch describe(testName, () => { let store: ArchiverDataStore; let blocks: L1Published[]; - let blockBodies: DataRetrieval; const blockTests: [number, number, () => L1Published[]][] = [ [1, 1, () => blocks.slice(0, 1)], [10, 1, () => blocks.slice(9, 10)], @@ -41,17 +39,9 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch data: L2Block.random(i + 1), l1: { blockNumber: BigInt(i + 10), blockHash: `0x${i}`, timestamp: BigInt(i * 1000) }, })); - blockBodies = { - retrievedData: blocks.map(block => block.data.body), - lastProcessedL1BlockNumber: 4n, - }; }); describe('addBlocks', () => { - it('returns success when adding block bodies', async () => { - await expect(store.addBlockBodies(blockBodies)).resolves.toBe(true); - }); - it('returns success when adding blocks', async () => { await expect(store.addBlocks(blocks)).resolves.toBe(true); }); @@ -65,7 +55,6 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch describe('getBlocks', () => { beforeEach(async () => { await store.addBlocks(blocks); - await store.addBlockBodies(blockBodies); }); it.each(blockTests)('retrieves previously stored blocks', async (start, limit, getExpectedBlocks) => { @@ -101,7 +90,6 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch await expect(store.getSynchPoint()).resolves.toEqual({ blocksSynchedTo: undefined, messagesSynchedTo: undefined, - blockBodiesSynchedTo: undefined, provenLogsSynchedTo: undefined, } satisfies ArchiverL1SynchPoint); }); @@ -111,17 +99,6 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch await expect(store.getSynchPoint()).resolves.toEqual({ blocksSynchedTo: 19n, messagesSynchedTo: undefined, - blockBodiesSynchedTo: undefined, - provenLogsSynchedTo: undefined, - } satisfies ArchiverL1SynchPoint); - }); - - it('returns the L1 block number in which the most recent L2 block body was published', async () => { - await store.addBlockBodies(blockBodies); - await expect(store.getSynchPoint()).resolves.toEqual({ - blocksSynchedTo: undefined, - messagesSynchedTo: undefined, - blockBodiesSynchedTo: blockBodies.lastProcessedL1BlockNumber, provenLogsSynchedTo: undefined, } satisfies ArchiverL1SynchPoint); }); @@ -134,7 +111,6 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch await expect(store.getSynchPoint()).resolves.toEqual({ blocksSynchedTo: undefined, messagesSynchedTo: 1n, - blockBodiesSynchedTo: undefined, provenLogsSynchedTo: undefined, } satisfies ArchiverL1SynchPoint); }); @@ -144,7 +120,6 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch await expect(store.getSynchPoint()).resolves.toEqual({ blocksSynchedTo: undefined, messagesSynchedTo: undefined, - blockBodiesSynchedTo: undefined, provenLogsSynchedTo: 3n, } satisfies ArchiverL1SynchPoint); }); @@ -212,7 +187,6 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch ), ); await store.addBlocks(blocks); - await store.addBlockBodies(blockBodies); }); it.each([ @@ -364,7 +338,6 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch })); await store.addBlocks(blocks); - await store.addBlockBodies(blockBodies); await Promise.all( blocks.map(block => diff --git a/yarn-project/archiver/src/archiver/kv_archiver_store/block_body_store.test.ts b/yarn-project/archiver/src/archiver/kv_archiver_store/block_body_store.test.ts deleted file mode 100644 index f8cd220e109b..000000000000 --- a/yarn-project/archiver/src/archiver/kv_archiver_store/block_body_store.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Body } from '@aztec/circuit-types'; -import { openTmpStore } from '@aztec/kv-store/utils'; - -import { KVArchiverDataStore } from './kv_archiver_store.js'; - -describe('Block Body Store', () => { - let archiverStore: KVArchiverDataStore; - - beforeEach(() => { - archiverStore = new KVArchiverDataStore(openTmpStore()); - }); - - it('Should add and return block bodies', async () => { - const body = Body.random(1); - - await archiverStore.addBlockBodies({ retrievedData: [body], lastProcessedL1BlockNumber: 5n }); - - const txsEffectsHash = body.getTxsEffectsHash(); - - const [returnedBody] = await archiverStore.getBlockBodies([txsEffectsHash]); - expect(body).toStrictEqual(returnedBody); - - const { blockBodiesSynchedTo } = await archiverStore.getSynchPoint(); - expect(blockBodiesSynchedTo).toEqual(5n); - }); -}); diff --git a/yarn-project/archiver/src/archiver/kv_archiver_store/block_body_store.ts b/yarn-project/archiver/src/archiver/kv_archiver_store/block_body_store.ts deleted file mode 100644 index 566e637e11b9..000000000000 --- a/yarn-project/archiver/src/archiver/kv_archiver_store/block_body_store.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { Body } from '@aztec/circuit-types'; -import { createDebugLogger } from '@aztec/foundation/log'; -import { type AztecKVStore, type AztecMap, type AztecSingleton } from '@aztec/kv-store'; - -import { type DataRetrieval } from '../structs/data_retrieval.js'; - -export class BlockBodyStore { - /** Map block body hash to block body */ - #blockBodies: AztecMap; - - /** Stores L1 block number in which the last processed L2 block body was included */ - #lastSynchedL1Block: AztecSingleton; - - constructor(private db: AztecKVStore, private log = createDebugLogger('aztec:archiver:block_body_store')) { - this.#blockBodies = db.openMap('archiver_block_bodies'); - this.#lastSynchedL1Block = db.openSingleton('archiver_block_bodies_last_synched_l1_block'); - } - - /** - * Append new block bodies to the store's map. - * @param blockBodies - The L2 block bodies to be added to the store. - * @returns True if the operation is successful. - */ - addBlockBodies(blockBodies: DataRetrieval): Promise { - return this.db.transaction(() => { - for (const body of blockBodies.retrievedData) { - void this.#blockBodies.set(body.getTxsEffectsHash().toString('hex'), body.toBuffer()); - } - void this.#lastSynchedL1Block.set(blockBodies.lastProcessedL1BlockNumber); - return true; - }); - } - - /** - * Gets a list of L2 block bodies with its associated txsEffectsHashes - * @param txsEffectsHashes - The txsEffectsHashes list that corresponds to the blockBodies we want to retrieve - * @returns The requested L2 block bodies - */ - async getBlockBodies(txsEffectsHashes: Buffer[]): Promise<(Body | undefined)[]> { - const blockBodiesBuffer = await this.db.transaction(() => - txsEffectsHashes.map(txsEffectsHash => this.#blockBodies.get(txsEffectsHash.toString('hex'))), - ); - - const blockBodies: (Body | undefined)[] = []; - for (let i = 0; i < blockBodiesBuffer.length; i++) { - const blockBodyBuffer = blockBodiesBuffer[i]; - if (blockBodyBuffer === undefined) { - this.log.warn(`Block body buffer is undefined for txsEffectsHash: ${txsEffectsHashes[i].toString('hex')}`); - } - blockBodies.push(blockBodyBuffer ? Body.fromBuffer(blockBodyBuffer) : undefined); - } - - return blockBodies; - } - - /** - * Gets an L2 block body. - * @param txsEffectsHash - The txHash of the block body to return - * @returns The requested L2 block body - */ - getBlockBody(txsEffectsHash: Buffer): Body | undefined { - const blockBody = this.#blockBodies.get(txsEffectsHash.toString('hex')); - - return blockBody && Body.fromBuffer(blockBody); - } - - /** - * Gets the last L1 block number in which a L2 block body was included - * @returns The L1 block number - */ - getSynchedL1BlockNumber(): bigint | undefined { - return this.#lastSynchedL1Block.get(); - } -} diff --git a/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts b/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts index 7ae29bd41b4f..84d8455b422e 100644 --- a/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts +++ b/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts @@ -1,10 +1,9 @@ -import { L2Block, type TxEffect, type TxHash, TxReceipt } from '@aztec/circuit-types'; +import { Body, L2Block, type TxEffect, type TxHash, TxReceipt } from '@aztec/circuit-types'; import { AppendOnlyTreeSnapshot, type AztecAddress, Header, INITIAL_L2_BLOCK_NUM } from '@aztec/circuits.js'; import { createDebugLogger } from '@aztec/foundation/log'; import { type AztecKVStore, type AztecMap, type AztecSingleton, type Range } from '@aztec/kv-store'; import { type L1Published, type L1PublishedData } from '../structs/published.js'; -import { type BlockBodyStore } from './block_body_store.js'; type BlockIndexValue = [blockNumber: number, index: number]; @@ -20,6 +19,10 @@ type BlockStorage = { export class BlockStore { /** Map block number to block data */ #blocks: AztecMap; + + /** Map block body hash to block body */ + #blockBodies: AztecMap; + /** Stores L1 block number in which the last processed L2 block was included */ #lastSynchedL1Block: AztecSingleton; @@ -31,12 +34,9 @@ export class BlockStore { #log = createDebugLogger('aztec:archiver:block_store'); - #blockBodyStore: BlockBodyStore; - - constructor(private db: AztecKVStore, blockBodyStore: BlockBodyStore) { - this.#blockBodyStore = blockBodyStore; - + constructor(private db: AztecKVStore) { this.#blocks = db.openMap('archiver_blocks'); + this.#blockBodies = db.openMap('archiver_block_bodies'); this.#txIndex = db.openMap('archiver_tx_index'); this.#contractIndex = db.openMap('archiver_contract_index'); this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block'); @@ -63,6 +63,8 @@ export class BlockStore { block.data.body.txEffects.forEach((tx, i) => { void this.#txIndex.set(tx.txHash.toString(), [block.data.number, i]); }); + + void this.#blockBodies.set(block.data.body.getTxsEffectsHash().toString('hex'), block.data.body.toBuffer()); } void this.#lastSynchedL1Block.set(blocks[blocks.length - 1].l1.blockNumber); @@ -100,11 +102,12 @@ export class BlockStore { private getBlockFromBlockStorage(blockStorage: BlockStorage) { const header = Header.fromBuffer(blockStorage.header); const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive); - const body = this.#blockBodyStore.getBlockBody(header.contentCommitment.txsEffectsHash); - if (body === undefined) { - throw new Error('Body is not able to be retrieved from BodyStore'); + const blockBodyBuffer = this.#blockBodies.get(header.contentCommitment.txsEffectsHash.toString('hex')); + if (blockBodyBuffer === undefined) { + throw new Error('Body could not be retrieved'); } + const body = Body.fromBuffer(blockBodyBuffer); const l2Block = L2Block.fromFields({ header, archive, body }); return { data: l2Block, l1: blockStorage.l1 }; diff --git a/yarn-project/archiver/src/archiver/kv_archiver_store/kv_archiver_store.ts b/yarn-project/archiver/src/archiver/kv_archiver_store/kv_archiver_store.ts index 7a71f7861ea7..2677c55e3f0a 100644 --- a/yarn-project/archiver/src/archiver/kv_archiver_store/kv_archiver_store.ts +++ b/yarn-project/archiver/src/archiver/kv_archiver_store/kv_archiver_store.ts @@ -1,5 +1,4 @@ import { - type Body, type EncryptedL2BlockL2Logs, type EncryptedNoteL2BlockL2Logs, type FromLogType, @@ -29,7 +28,6 @@ import { import { type ArchiverDataStore, type ArchiverL1SynchPoint } from '../archiver_store.js'; import { type DataRetrieval, type SingletonDataRetrieval } from '../structs/data_retrieval.js'; import { type L1Published } from '../structs/published.js'; -import { BlockBodyStore } from './block_body_store.js'; import { BlockStore } from './block_store.js'; import { ContractArtifactsStore } from './contract_artifacts_store.js'; import { ContractClassStore } from './contract_class_store.js'; @@ -42,7 +40,6 @@ import { ProvenStore } from './proven_store.js'; * LMDB implementation of the ArchiverDataStore interface. */ export class KVArchiverDataStore implements ArchiverDataStore { - #blockBodyStore: BlockBodyStore; #blockStore: BlockStore; #provenStore: ProvenStore; #logStore: LogStore; @@ -54,8 +51,7 @@ export class KVArchiverDataStore implements ArchiverDataStore { #log = createDebugLogger('aztec:archiver:data-store'); constructor(db: AztecKVStore, logsMaxPageSize: number = 1000) { - this.#blockBodyStore = new BlockBodyStore(db); - this.#blockStore = new BlockStore(db, this.#blockBodyStore); + this.#blockStore = new BlockStore(db); this.#provenStore = new ProvenStore(db); this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize); this.#messageStore = new MessageStore(db); @@ -100,25 +96,6 @@ export class KVArchiverDataStore implements ArchiverDataStore { return (await Promise.all(data.map(c => this.#contractInstanceStore.addContractInstance(c)))).every(Boolean); } - /** - * Append new block bodies to the store's list. - * @param blockBodies - The L2 block bodies to be added to the store. - * @returns True if the operation is successful. - */ - addBlockBodies(blockBodies: DataRetrieval): Promise { - return this.#blockBodyStore.addBlockBodies(blockBodies); - } - - /** - * Gets block bodies that have the same txHashes as we supply. - * - * @param txsEffectsHashes - A list of txsEffectsHashes (body hashes). - * @returns The requested L2 block bodies - */ - getBlockBodies(txsEffectsHashes: Buffer[]): Promise<(Body | undefined)[]> { - return this.#blockBodyStore.getBlockBodies(txsEffectsHashes); - } - /** * Append new blocks to the store's list. * @param blocks - The L2 blocks to be added to the store and the last processed L1 block. @@ -264,7 +241,6 @@ export class KVArchiverDataStore implements ArchiverDataStore { getSynchPoint(): Promise { return Promise.resolve({ blocksSynchedTo: this.#blockStore.getSynchedL1BlockNumber(), - blockBodiesSynchedTo: this.#blockBodyStore.getSynchedL1BlockNumber(), messagesSynchedTo: this.#messageStore.getSynchedL1BlockNumber(), provenLogsSynchedTo: this.#provenStore.getSynchedL1BlockNumber(), }); diff --git a/yarn-project/archiver/src/archiver/memory_archiver_store/memory_archiver_store.ts b/yarn-project/archiver/src/archiver/memory_archiver_store/memory_archiver_store.ts index 5094382b15ce..9ea02e71f214 100644 --- a/yarn-project/archiver/src/archiver/memory_archiver_store/memory_archiver_store.ts +++ b/yarn-project/archiver/src/archiver/memory_archiver_store/memory_archiver_store.ts @@ -1,5 +1,4 @@ import { - type Body, type EncryptedL2BlockL2Logs, type EncryptedNoteL2BlockL2Logs, ExtendedUnencryptedL2Log, @@ -40,11 +39,6 @@ export class MemoryArchiverStore implements ArchiverDataStore { */ private l2Blocks: L1Published[] = []; - /** - * A mapping of body hash to body - */ - private l2BlockBodies: Map = new Map(); - /** * An array containing all the tx effects in the L2 blocks that have been fetched so far. */ @@ -84,7 +78,6 @@ export class MemoryArchiverStore implements ArchiverDataStore { private contractInstances: Map = new Map(); private lastL1BlockNewBlocks: bigint | undefined = undefined; - private lastL1BlockNewBlockBodies: bigint | undefined = undefined; private lastL1BlockNewMessages: bigint | undefined = undefined; private lastL1BlockNewProvenLogs: bigint | undefined = undefined; @@ -163,34 +156,10 @@ export class MemoryArchiverStore implements ArchiverDataStore { this.lastL1BlockNewBlocks = blocks[blocks.length - 1].l1.blockNumber; this.l2Blocks.push(...blocks); this.txEffects.push(...blocks.flatMap(b => b.data.body.txEffects)); - return Promise.resolve(true); - } - /** - * Append new block bodies to the store's list. - * @param blockBodies - The L2 block bodies to be added to the store. - * @returns True if the operation is successful. - */ - addBlockBodies(blockBodies: DataRetrieval): Promise { - for (const body of blockBodies.retrievedData) { - void this.l2BlockBodies.set(body.getTxsEffectsHash().toString('hex'), body); - } - this.lastL1BlockNewBlockBodies = blockBodies.lastProcessedL1BlockNumber; return Promise.resolve(true); } - /** - * Gets block bodies that have the same txHashes as we supply. - * - * @param txsEffectsHashes - A list of txsEffectsHashes (body hashes). - * @returns The requested L2 block bodies - */ - getBlockBodies(txsEffectsHashes: Buffer[]): Promise<(Body | undefined)[]> { - return Promise.resolve( - txsEffectsHashes.map(txsEffectsHash => this.l2BlockBodies.get(txsEffectsHash.toString('hex'))), - ); - } - /** * Append new logs to the store's list. * @param encryptedLogs - The encrypted logs to be added to the store. @@ -455,7 +424,6 @@ export class MemoryArchiverStore implements ArchiverDataStore { return Promise.resolve({ blocksSynchedTo: this.lastL1BlockNewBlocks, messagesSynchedTo: this.lastL1BlockNewMessages, - blockBodiesSynchedTo: this.lastL1BlockNewBlockBodies, provenLogsSynchedTo: this.lastL1BlockNewProvenLogs, }); }