Skip to content

Commit 539aaed

Browse files
ScottyPoiacolytec3
andauthored
common: improve ugly method (#4080)
* common: clean up nextHardforkBlockOrTimestamp method * remove TODO comment --------- Co-authored-by: acolytec3 <[email protected]>
1 parent 5eaf56a commit 539aaed

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

packages/client/src/sync/fullsync.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ export class FullSynchronizer extends Synchronizer {
250250
if (nextHFBlockNum !== null) {
251251
const remaining = nextHFBlockNum - last
252252
if (remaining <= BigInt(10000)) {
253-
// TODO: Do something about super-ugly nextHardforkBlockOrTimestamp() method
254253
const nextHF = this.config.chainCommon.getHardforkBy({ blockNumber: nextHFBlockNum })
255254
attentionHF = `${nextHF} HF in ${remaining} blocks`
256255
}

packages/common/src/common.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -567,52 +567,60 @@ export class Common {
567567
}
568568

569569
/**
570-
* Returns the change block for the next hardfork after the hardfork provided or set
570+
* Returns the block number or timestamp at which the next hardfork will occur.
571+
* For pre-merge hardforks, returns the block number.
572+
* For post-merge hardforks, returns the timestamp.
573+
* Returns null if there is no next hardfork.
571574
* @param hardfork Hardfork name, optional if HF set
572-
* @returns Block timestamp, number or null if not available
575+
* @returns Block number or timestamp, or null if not available
573576
*/
574577
nextHardforkBlockOrTimestamp(hardfork?: string | Hardfork): bigint | null {
575-
hardfork = hardfork ?? this._hardfork
578+
const targetHardfork = hardfork ?? this._hardfork
576579
const hfs = this.hardforks()
577-
let hfIndex = hfs.findIndex((hf) => hf.name === hardfork)
578-
// If the current hardfork is merge, go one behind as merge hf is not part of these
579-
// calcs even if the merge hf block is set
580-
if (hardfork === Hardfork.Paris) {
581-
hfIndex -= 1
582-
}
583-
// Hardfork not found
584-
if (hfIndex < 0) {
580+
581+
// Find the index of the target hardfork
582+
let targetHfIndex = hfs.findIndex((hf) => hf.name === targetHardfork)
583+
584+
// Special handling for The Merge (Paris) hardfork
585+
if (targetHardfork === Hardfork.Paris) {
586+
// The Merge is determined by total difficulty, not block number
587+
// So we look at the previous hardfork's parameters instead
588+
targetHfIndex -= 1
589+
}
590+
591+
// If we couldn't find a valid hardfork index, return null
592+
if (targetHfIndex < 0) {
585593
return null
586594
}
587595

588-
let currHfTimeOrBlock = hfs[hfIndex].timestamp ?? hfs[hfIndex].block
589-
currHfTimeOrBlock =
590-
currHfTimeOrBlock !== null && currHfTimeOrBlock !== undefined
591-
? Number(currHfTimeOrBlock)
592-
: null
596+
// Get the current hardfork's block/timestamp
597+
const currentHf = hfs[targetHfIndex]
598+
const currentBlockOrTimestamp = currentHf.timestamp ?? currentHf.block
599+
if (currentBlockOrTimestamp === null || currentBlockOrTimestamp === undefined) {
600+
return null
601+
}
593602

594-
const nextHf = hfs.slice(hfIndex + 1).find((hf) => {
595-
let hfTimeOrBlock = hf.timestamp ?? hf.block
596-
hfTimeOrBlock =
597-
hfTimeOrBlock !== null && hfTimeOrBlock !== undefined ? Number(hfTimeOrBlock) : null
603+
// Find the next hardfork that has a different block/timestamp
604+
const nextHf = hfs.slice(targetHfIndex + 1).find((hf) => {
605+
const nextBlockOrTimestamp = hf.timestamp ?? hf.block
598606
return (
599-
hf.name !== Hardfork.Paris &&
600-
hfTimeOrBlock !== null &&
601-
hfTimeOrBlock !== undefined &&
602-
hfTimeOrBlock !== currHfTimeOrBlock
607+
nextBlockOrTimestamp !== null &&
608+
nextBlockOrTimestamp !== undefined &&
609+
nextBlockOrTimestamp !== currentBlockOrTimestamp
603610
)
604611
})
605612
// If no next hf found with valid block or timestamp return null
606613
if (nextHf === undefined) {
607614
return null
608615
}
609616

610-
const nextHfBlock = nextHf.timestamp ?? nextHf.block
611-
if (nextHfBlock === null || nextHfBlock === undefined) {
617+
// Get the block/timestamp for the next hardfork
618+
const nextBlockOrTimestamp = nextHf.timestamp ?? nextHf.block
619+
if (nextBlockOrTimestamp === null || nextBlockOrTimestamp === undefined) {
612620
return null
613621
}
614622

615-
return BigInt(nextHfBlock)
623+
return BigInt(nextBlockOrTimestamp)
616624
}
617625

618626
/**

0 commit comments

Comments
 (0)