Skip to content

Commit

Permalink
further simplify the fn and add test to enhance coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Sep 27, 2022
1 parent f52a7ad commit bdff698
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/common/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,27 +308,32 @@ export class Common extends EventEmitter {
continue
}

if (hf.ttd !== undefined && hf.ttd !== null) {
// Only one hardfork merge is based on ttd, if mergeTTDSeen already seen than throw error
if (passedMergeHF !== undefined) {
throw new Error(
`Invalid hardfork config with repeat ttd hardfork=${hf.name} ttd=${hf.ttd}, previous seen at hardfork=${passedMergeHF.name} ttd=${passedMergeHF.ttd}`
)
}
if (hf.ttd !== undefined && hf.ttd !== null && preMergeHF === undefined) {
// Ok, this is the hardfork with ttd
preMergeHF = hardfork
}

// If merge hardfork has block null set, we will make determination of that hardfork if no future hardforks qualify
// If merge hardfork has block null set, we will make determination if this is merge hardfork or preMergeHF
// - if no future hardforks qualify by block number
// So we assume it to quality for now to move to check future post merge hardfork qualitfication
//
// For the hardforks with block specified we check for qualification
if (hf.block !== null) {
if (blockNumber < BigInt(hf.block)) {
break
}
}

// Move the candidate hardfork pointer to this hf else we would have broken out of loop
// hf qualifies else we would have broken out of the loop till now
if (hardfork?.ttd !== undefined && hardfork?.ttd !== null) {
passedMergeHF = hardfork!
}
// If passedMergeHF, the next hardforks can't have ttd specified
if (passedMergeHF !== undefined && hf.ttd !== undefined && hf.ttd !== null) {
throw new Error(
`Invalid hardfork config with repeat ttd hardfork=${hf.name} ttd=${hf.ttd}, previous seen at hardfork=${passedMergeHF.name} ttd=${passedMergeHF.ttd}`
)
}
hardfork = hf
}

Expand Down
14 changes: 14 additions & 0 deletions packages/common/tests/hardforks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ tape('[Common]: Hardfork logic', function (t: tape.Test) {
st.end()
})

t.test('should reject two hardforks with ttd specified', function (st: tape.Test) {
const c = new Common({ chain: Chain.Sepolia })
c.hardforks().filter((hf) => hf.name === 'mergeForkIdTransition')[0]!['ttd'] =
'17000000000000000'

try {
c.setHardforkByBlockNumber(1735371)
st.fail('should have thrown as two hardforks with ttd specified')
} catch (error) {
st.pass('throws error as two hardforks with ttd specified')
}
st.end()
})

t.test(
'should generate expected hash with london block zero and base fee per gas defined',
async (st) => {
Expand Down

0 comments on commit bdff698

Please sign in to comment.