Skip to content
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: Rename private function in L2 block stream #9481

Merged
Merged
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,22 @@ export class L2BlockStream {
const localTips = await this.localData.getL2Tips();
this.log.debug(`Running L2 block stream`, {
sourceLatest: sourceTips.latest.number,
localLatest: localTips.latest,
localLatest: localTips.latest.number,
sourceFinalized: sourceTips.finalized.number,
localFinalized: localTips.finalized,
localFinalized: localTips.finalized.number,
sourceProven: sourceTips.proven.number,
localProven: localTips.proven,
localProven: localTips.proven.number,
sourceLatestHash: sourceTips.latest.hash,
localLatestHash: localTips.latest.hash,
sourceProvenHash: sourceTips.proven.hash,
localProvenHash: localTips.proven.hash,
sourceFinalizedHash: sourceTips.finalized.hash,
localFinalizedHash: localTips.finalized.hash,
});

// Check if there was a reorg and emit a chain-pruned event if so.
let latestBlockNumber = localTips.latest.number;
while (!(await this.areBlockHashesEqual(latestBlockNumber, sourceTips.latest))) {
while (!(await this.areBlockHashesEqualAt(latestBlockNumber, { sourceCache: [sourceTips.latest] }))) {
latestBlockNumber--;
}
if (latestBlockNumber < localTips.latest.number) {
Expand Down Expand Up @@ -97,15 +100,19 @@ export class L2BlockStream {
}
}

private async areBlockHashesEqual(blockNumber: number, sourceLatest: L2BlockId) {
/**
* Returns whether the source and local agree on the block hash at a given height.
* @param blockNumber - The block number to test.
* @param args - A cache of data already requested from source, to avoid re-requesting it.
*/
private async areBlockHashesEqualAt(blockNumber: number, args: { sourceCache: L2BlockId[] }) {
if (blockNumber === 0) {
return true;
}
const localBlockHash = await this.localData.getL2BlockHash(blockNumber);
const sourceBlockHash =
sourceLatest.number === blockNumber && sourceLatest.hash
? sourceLatest.hash
: await this.l2BlockSource.getBlockHeader(blockNumber).then(h => h?.hash().toString());
args.sourceCache.find(id => id.number === blockNumber && id.hash)?.hash ??
(await this.l2BlockSource.getBlockHeader(blockNumber).then(h => h?.hash().toString()));
this.log.debug(`Comparing block hashes for block ${blockNumber}`, { localBlockHash, sourceBlockHash });
return localBlockHash === sourceBlockHash;
}
Expand Down
Loading