From c7eba4c8f5944f8ad42d9f658171680a02094329 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 12 Oct 2023 09:20:16 +0000 Subject: [PATCH] fix + cleanup --- .../archiver/src/archiver/archiver.ts | 4 ++-- .../archiver/src/archiver/archiver_store.ts | 24 +++++-------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/yarn-project/archiver/src/archiver/archiver.ts b/yarn-project/archiver/src/archiver/archiver.ts index 40ac4e6b58ef..2ed434a3eeae 100644 --- a/yarn-project/archiver/src/archiver/archiver.ts +++ b/yarn-project/archiver/src/archiver/archiver.ts @@ -209,7 +209,7 @@ export class Archiver implements L2BlockSource, L2LogsSource, ContractDataSource // ********** Events that are processed per block ********** // Read all data from chain and then write to our stores at the end - const nextExpectedL2BlockNum = BigInt(this.store.getBlocksLength() + INITIAL_L2_BLOCK_NUM); + const nextExpectedL2BlockNum = BigInt((await this.store.getBlockNumber()) + 1); this.log( `Retrieving chain state from L1 block: ${this.nextL2BlockFromBlock}, next expected l2 block number: ${nextExpectedL2BlockNum}`, ); @@ -318,7 +318,7 @@ export class Archiver implements L2BlockSource, L2LogsSource, ContractDataSource public async getBlock(number: number): Promise { // If the number provided is -ve, then return the latest block. if (number < 0) { - number = this.store.getBlocksLength(); + number = await this.store.getBlockNumber(); } const blocks = await this.store.getBlocks(number, 1); return blocks.length === 0 ? undefined : blocks[0]; diff --git a/yarn-project/archiver/src/archiver/archiver_store.ts b/yarn-project/archiver/src/archiver/archiver_store.ts index 65b514311678..21f1c655ce9e 100644 --- a/yarn-project/archiver/src/archiver/archiver_store.ts +++ b/yarn-project/archiver/src/archiver/archiver_store.ts @@ -150,12 +150,6 @@ export interface ArchiverDataStore { * @returns The number of the latest L2 block processed. */ getBlockNumber(): Promise; - - /** - * Gets the length of L2 blocks in store. - * @returns The length of L2 Blocks stored. - */ - getBlocksLength(): number; } /** @@ -306,12 +300,14 @@ export class MemoryArchiverStore implements ArchiverDataStore { if (limit < 1) { throw new Error(`Invalid limit: ${limit}`); } - if (from >= this.l2BlockContexts.length) { + + const fromIndex = Math.max(from - INITIAL_L2_BLOCK_NUM, 0); + if (fromIndex >= this.l2BlockContexts.length) { return Promise.resolve([]); } - const startIndex = Math.max(from - INITIAL_L2_BLOCK_NUM, 0); - const endIndex = startIndex + limit; - return Promise.resolve(this.l2BlockContexts.slice(startIndex, endIndex).map(blockContext => blockContext.block)); + + const toIndex = fromIndex + limit; + return Promise.resolve(this.l2BlockContexts.slice(fromIndex, toIndex).map(blockContext => blockContext.block)); } /** @@ -514,12 +510,4 @@ export class MemoryArchiverStore implements ArchiverDataStore { if (this.l2BlockContexts.length === 0) return Promise.resolve(INITIAL_L2_BLOCK_NUM - 1); return Promise.resolve(this.l2BlockContexts[this.l2BlockContexts.length - 1].block.number); } - - /** - * Gets the length of L2 blocks in store. - * @returns The length of L2 Blocks array. - */ - public getBlocksLength(): number { - return this.l2BlockContexts.length; - } }