Skip to content

Commit

Permalink
fix + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 12, 2023
1 parent f20461a commit c7eba4c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
4 changes: 2 additions & 2 deletions yarn-project/archiver/src/archiver/archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
Expand Down Expand Up @@ -318,7 +318,7 @@ export class Archiver implements L2BlockSource, L2LogsSource, ContractDataSource
public async getBlock(number: number): Promise<L2Block | undefined> {
// 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];
Expand Down
24 changes: 6 additions & 18 deletions yarn-project/archiver/src/archiver/archiver_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,6 @@ export interface ArchiverDataStore {
* @returns The number of the latest L2 block processed.
*/
getBlockNumber(): Promise<number>;

/**
* Gets the length of L2 blocks in store.
* @returns The length of L2 Blocks stored.
*/
getBlocksLength(): number;
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit c7eba4c

Please sign in to comment.