From bdff698344f52a80eed2db6d7d438715607d98a2 Mon Sep 17 00:00:00 2001 From: harkamal Date: Tue, 27 Sep 2022 21:33:28 +0530 Subject: [PATCH] further simplify the fn and add test to enhance coverage --- packages/common/src/common.ts | 23 ++++++++++++++--------- packages/common/tests/hardforks.spec.ts | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/common/src/common.ts b/packages/common/src/common.ts index 4abf0dfe276..3cf7ee43417 100644 --- a/packages/common/src/common.ts +++ b/packages/common/src/common.ts @@ -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 } diff --git a/packages/common/tests/hardforks.spec.ts b/packages/common/tests/hardforks.spec.ts index 39d3d1b14f5..18f1f6feeee 100644 --- a/packages/common/tests/hardforks.spec.ts +++ b/packages/common/tests/hardforks.spec.ts @@ -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) => {