Skip to content

Commit

Permalink
handle no hardfork found case
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Sep 29, 2022
1 parent 30a8394 commit 55e0380
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/chains/goerli.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge, terminal block: https://goerli.etherscan.io/block/7382818",
"name": "merge",
"ttd": "10790000",
"block": 7382819,
"block": null,
"forkHash": "0xb8c6299d"
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/chains/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge, terminal block: https://etherscan.io/block/15537393",
"name": "merge",
"ttd": "58750000000000000000000",
"block": 15537394,
"block": null,
"forkHash": "0xf0afd0e3"
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/chains/sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge, terminal block: https://sepolia.etherscan.io/block/1450408",
"name": "merge",
"ttd": "17000000000000000",
"block": 1450409,
"block": null,
"forkHash": "0xfe3366e7"
},
{
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export class Common extends EventEmitter {
hfIndex = hfs.length - 1
} else if (hfIndex === 0) {
// Cant move back, ideally we should throw??
throw Error('No hardfork qualitifies')
} else {
// The previous hardfork is the candidate here
hfIndex = hfIndex - 1
Expand Down
30 changes: 30 additions & 0 deletions packages/common/tests/hardforks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ tape('[Common]: Hardfork logic', function (t: tape.Test) {
st.end()
})

t.test('should throw if no hardfork qualifies', function (st) {
const hardforks = [
{
name: 'homestead',
block: 3,
},
{
name: 'tangerineWhistle',
block: 3,
},
{
name: 'spuriousDragon',
block: 3,
},
]
const c = Common.custom({ hardforks }, { baseChain: Chain.Sepolia })

try {
c.getHardforkByBlockNumber(0)
t.fail('should have thrown since no hardfork should quality')
} catch (e) {
t.pass('throw since no hardfork qualifies')
}

const msg = 'should return correct value'
st.equal(c.setHardforkByBlockNumber(3), Hardfork.SpuriousDragon, msg)

t.end()
})

t.test('setHardfork(): hardforkChanged event', function (st) {
const c = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
c.on('hardforkChanged', (hardfork: string) => {
Expand Down

0 comments on commit 55e0380

Please sign in to comment.