-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: from < genesis allowed in getBlocks #2816
Changes from all commits
abf4a2b
3c81585
9830c73
68e3fca
3d094d6
df1a603
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,15 +30,15 @@ export interface ArchiverDataStore { | |
* @param blocks - The L2 blocks to be added to the store. | ||
* @returns True if the operation is successful. | ||
*/ | ||
addL2Blocks(blocks: L2Block[]): Promise<boolean>; | ||
addBlocks(blocks: L2Block[]): Promise<boolean>; | ||
|
||
/** | ||
* Gets up to `limit` amount of L2 blocks starting from `from`. | ||
* @param from - Number of the first block to return (inclusive). | ||
* @param limit - The number of blocks to return. | ||
* @returns The requested L2 blocks. | ||
*/ | ||
getL2Blocks(from: number, limit: number): Promise<L2Block[]>; | ||
getBlocks(from: number, limit: number): Promise<L2Block[]>; | ||
|
||
/** | ||
* Gets an l2 tx. | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -215,7 +209,7 @@ export class MemoryArchiverStore implements ArchiverDataStore { | |
* @param blocks - The L2 blocks to be added to the store. | ||
* @returns True if the operation is successful (always in this implementation). | ||
*/ | ||
public addL2Blocks(blocks: L2Block[]): Promise<boolean> { | ||
public addBlocks(blocks: L2Block[]): Promise<boolean> { | ||
this.l2BlockContexts.push(...blocks.map(block => new L2BlockContext(block))); | ||
this.l2Txs.push(...blocks.flatMap(b => b.getTxs())); | ||
return Promise.resolve(true); | ||
|
@@ -299,18 +293,21 @@ export class MemoryArchiverStore implements ArchiverDataStore { | |
* @param from - Number of the first block to return (inclusive). | ||
* @param limit - The number of blocks to return. | ||
* @returns The requested L2 blocks. | ||
* @remarks When "from" is smaller than genesis block number, blocks from the beginning are returned. | ||
*/ | ||
public getL2Blocks(from: number, limit: number): Promise<L2Block[]> { | ||
public getBlocks(from: number, limit: number): Promise<L2Block[]> { | ||
// Return an empty array if we are outside of range | ||
if (limit < 1) { | ||
throw new Error(`Invalid block range from: ${from}, limit: ${limit}`); | ||
throw new Error(`Invalid limit: ${limit}`); | ||
} | ||
if (from < INITIAL_L2_BLOCK_NUM || from > this.l2BlockContexts.length) { | ||
|
||
const fromIndex = Math.max(from - INITIAL_L2_BLOCK_NUM, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was being used in an overloaded context in the archiver. ( incrementing to next block / getting the first block number ) |
||
if (fromIndex >= this.l2BlockContexts.length) { | ||
return Promise.resolve([]); | ||
} | ||
const startIndex = from - INITIAL_L2_BLOCK_NUM; | ||
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)); | ||
} | ||
|
||
/** | ||
|
@@ -354,7 +351,7 @@ export class MemoryArchiverStore implements ArchiverDataStore { | |
*/ | ||
getLogs(from: number, limit: number, logType: LogType): Promise<L2BlockL2Logs[]> { | ||
if (from < INITIAL_L2_BLOCK_NUM || limit < 1) { | ||
throw new Error(`Invalid block range from: ${from}, limit: ${limit}`); | ||
throw new Error(`Invalid limit: ${limit}`); | ||
} | ||
const logs = logType === LogType.ENCRYPTED ? this.encryptedLogsPerBlock : this.unencryptedLogsPerBlock; | ||
if (from > logs.length) { | ||
|
@@ -513,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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this break if we restart the archiver? Say L2 is at block 30. Say archiver restarts. So
nextExpectedL2BlockNum
would be 1. But I suppose we don't care about re-syncing until testnet?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would have been one in the first instance too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems correct to me because when we start archiver the next expected L2 block num is 1 because that's the first block.