Skip to content

Commit f67b51c

Browse files
committed
refactor: Rename private function in L2 block stream
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
1 parent 078c318 commit f67b51c

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

yarn-project/circuit-types/src/l2_block_downloader/l2_block_stream.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,22 @@ export class L2BlockStream {
4747
const localTips = await this.localData.getL2Tips();
4848
this.log.debug(`Running L2 block stream`, {
4949
sourceLatest: sourceTips.latest.number,
50-
localLatest: localTips.latest,
50+
localLatest: localTips.latest.number,
5151
sourceFinalized: sourceTips.finalized.number,
52-
localFinalized: localTips.finalized,
52+
localFinalized: localTips.finalized.number,
5353
sourceProven: sourceTips.proven.number,
54-
localProven: localTips.proven,
54+
localProven: localTips.proven.number,
5555
sourceLatestHash: sourceTips.latest.hash,
56+
localLatestHash: localTips.latest.hash,
5657
sourceProvenHash: sourceTips.proven.hash,
58+
localProvenHash: localTips.proven.hash,
5759
sourceFinalizedHash: sourceTips.finalized.hash,
60+
localFinalizedHash: localTips.finalized.hash,
5861
});
5962

6063
// Check if there was a reorg and emit a chain-pruned event if so.
6164
let latestBlockNumber = localTips.latest.number;
62-
while (!(await this.areBlockHashesEqual(latestBlockNumber, sourceTips.latest))) {
65+
while (!(await this.areBlockHashesEqualAt(latestBlockNumber, { sourceCache: [sourceTips.latest] }))) {
6366
latestBlockNumber--;
6467
}
6568
if (latestBlockNumber < localTips.latest.number) {
@@ -97,15 +100,19 @@ export class L2BlockStream {
97100
}
98101
}
99102

100-
private async areBlockHashesEqual(blockNumber: number, sourceLatest: L2BlockId) {
103+
/**
104+
* Returns whether the source and local agree on the block hash at a given height.
105+
* @param blockNumber - The block number to test.
106+
* @param args - A cache of data already requested from source, to avoid re-requesting it.
107+
*/
108+
private async areBlockHashesEqualAt(blockNumber: number, args: { sourceCache: L2BlockId[] }) {
101109
if (blockNumber === 0) {
102110
return true;
103111
}
104112
const localBlockHash = await this.localData.getL2BlockHash(blockNumber);
105113
const sourceBlockHash =
106-
sourceLatest.number === blockNumber && sourceLatest.hash
107-
? sourceLatest.hash
108-
: await this.l2BlockSource.getBlockHeader(blockNumber).then(h => h?.hash().toString());
114+
args.sourceCache.find(id => id.number === blockNumber && id.hash)?.hash ??
115+
(await this.l2BlockSource.getBlockHeader(blockNumber).then(h => h?.hash().toString()));
109116
this.log.debug(`Comparing block hashes for block ${blockNumber}`, { localBlockHash, sourceBlockHash });
110117
return localBlockHash === sourceBlockHash;
111118
}

0 commit comments

Comments
 (0)