Skip to content

Commit

Permalink
refactor: Rename private function in L2 block stream
Browse files Browse the repository at this point in the history
Renames a private function in the L2 block stream for clarity. The
function checks whether local and source data agree on the hash for a
block at a given height, and accepts a "cache" of data already requested
from the source.

Fixes #9314
  • Loading branch information
spalladino committed Oct 28, 2024
1 parent 8658abd commit 98f8064
Showing 1 changed file with 15 additions and 8 deletions.
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

0 comments on commit 98f8064

Please sign in to comment.