|
1 | 1 | import * as tape from 'tape' |
2 | 2 |
|
3 | | -import { Common, Hardfork } from '../src' |
| 3 | +import { Chain, Common, Hardfork } from '../src' |
4 | 4 |
|
5 | 5 | import * as testnetMerge from './data/merge/testnetMerge.json' |
6 | 6 | import * as testnetPOS from './data/merge/testnetPOS.json' |
@@ -142,13 +142,15 @@ tape('[Common]: Merge/POS specific logic', function (t: tape.Test) { |
142 | 142 |
|
143 | 143 | try { |
144 | 144 | c.setHardforkByBlockNumber(16, 4999) |
| 145 | + st.fail(`should have thrown td < ttd validation error`) |
145 | 146 | } catch (e: any) { |
146 | 147 | msg = 'block number > last HF block number set, TD set and smaller (should throw)' |
147 | 148 | const eMsg = 'Maximum HF determined by total difficulty is lower than the block number HF' |
148 | 149 | st.ok(e.message.includes(eMsg), msg) |
149 | 150 | } |
150 | 151 | try { |
151 | 152 | c.setHardforkByBlockNumber(14, 5000) |
| 153 | + st.fail(`should have thrown td > ttd validation error`) |
152 | 154 | } catch (e: any) { |
153 | 155 | msg = 'block number < last HF block number set, TD set and higher (should throw)' |
154 | 156 | const eMsg = 'HF determined by block number is lower than the minimum total difficulty HF' |
@@ -187,4 +189,122 @@ tape('[Common]: Merge/POS specific logic', function (t: tape.Test) { |
187 | 189 | st.equal(c.getHardforkByBlockNumber(5, 0), 'shanghai', msg) |
188 | 190 | st.end() |
189 | 191 | }) |
| 192 | + |
| 193 | + t.test('should get the correct merge hardfork at genesis', async (st) => { |
| 194 | + const json = require(`../../client/test/testdata/geth-genesis/post-merge.json`) |
| 195 | + const c = Common.fromGethGenesis(json, { chain: 'post-merge' }) |
| 196 | + const msg = 'should get HF correctly' |
| 197 | + st.equal(c.getHardforkByBlockNumber(0), Hardfork.London, msg) |
| 198 | + st.equal(c.getHardforkByBlockNumber(0, BigInt(0)), Hardfork.Merge, msg) |
| 199 | + }) |
| 200 | + |
| 201 | + t.test('test post merge hardforks using Sepolia with block null', function (st: tape.Test) { |
| 202 | + const c = new Common({ chain: Chain.Sepolia }) |
| 203 | + let msg = 'should get HF correctly' |
| 204 | + |
| 205 | + st.equal(c.getHardforkByBlockNumber(0), Hardfork.London, msg) |
| 206 | + // Make it null manually as config could be updated later |
| 207 | + const mergeHf = c.hardforks().filter((hf) => hf.ttd !== undefined && hf.ttd !== null)[0] |
| 208 | + const prevMergeBlockVal = mergeHf.block |
| 209 | + mergeHf.block = null |
| 210 | + |
| 211 | + // should get Hardfork.London even though happened with 1450408 as terminal as config doesn't have that info |
| 212 | + st.equal(c.getHardforkByBlockNumber(1450409), Hardfork.London, msg) |
| 213 | + // however with correct td in input it should select merge |
| 214 | + st.equal(c.getHardforkByBlockNumber(1450409, BigInt('17000000000000000')), Hardfork.Merge, msg) |
| 215 | + // should select MergeForkIdTransition even without td specified as the block is set for this hardfork |
| 216 | + st.equal(c.getHardforkByBlockNumber(1735371), Hardfork.MergeForkIdTransition, msg) |
| 217 | + // also with td specified |
| 218 | + st.equal( |
| 219 | + c.getHardforkByBlockNumber(1735371, BigInt('17000000000000000')), |
| 220 | + Hardfork.MergeForkIdTransition, |
| 221 | + msg |
| 222 | + ) |
| 223 | + try { |
| 224 | + st.equal( |
| 225 | + c.getHardforkByBlockNumber(1735371, BigInt('15000000000000000')), |
| 226 | + Hardfork.MergeForkIdTransition, |
| 227 | + msg |
| 228 | + ) |
| 229 | + st.fail('should have thrown as specified td < merge ttd for a post merge hardfork') |
| 230 | + } catch (error) { |
| 231 | + st.pass('throws error as specified td < merge ttd for a post merge hardfork') |
| 232 | + } |
| 233 | + |
| 234 | + msg = 'should set HF correctly' |
| 235 | + |
| 236 | + st.equal(c.setHardforkByBlockNumber(0), Hardfork.London, msg) |
| 237 | + st.equal(c.setHardforkByBlockNumber(1450409), Hardfork.London, msg) |
| 238 | + st.equal(c.setHardforkByBlockNumber(1450409, BigInt('17000000000000000')), Hardfork.Merge, msg) |
| 239 | + st.equal(c.setHardforkByBlockNumber(1735371), Hardfork.MergeForkIdTransition, msg) |
| 240 | + st.equal( |
| 241 | + c.setHardforkByBlockNumber(1735371, BigInt('17000000000000000')), |
| 242 | + Hardfork.MergeForkIdTransition, |
| 243 | + msg |
| 244 | + ) |
| 245 | + try { |
| 246 | + st.equal( |
| 247 | + c.setHardforkByBlockNumber(1735371, BigInt('15000000000000000')), |
| 248 | + Hardfork.MergeForkIdTransition, |
| 249 | + msg |
| 250 | + ) |
| 251 | + st.fail('should have thrown as specified td < merge ttd for a post merge hardfork') |
| 252 | + } catch (error) { |
| 253 | + st.pass('throws error as specified td < merge ttd for a post merge hardfork') |
| 254 | + } |
| 255 | + |
| 256 | + // restore value |
| 257 | + mergeHf.block = prevMergeBlockVal |
| 258 | + |
| 259 | + st.end() |
| 260 | + }) |
| 261 | + |
| 262 | + t.test( |
| 263 | + 'should get correct merge and post merge hf with merge block specified ', |
| 264 | + function (st: tape.Test) { |
| 265 | + const c = new Common({ chain: Chain.Sepolia }) |
| 266 | + |
| 267 | + const mergeHf = c.hardforks().filter((hf) => hf.ttd !== undefined && hf.ttd !== null)[0] |
| 268 | + const prevMergeBlockVal = mergeHf.block |
| 269 | + // the terminal block on sepolia is 1450408 |
| 270 | + mergeHf.block = 1450409 |
| 271 | + const msg = 'should get HF correctly' |
| 272 | + |
| 273 | + // should get merge even without td supplied as the merge hf now has the block specified |
| 274 | + st.equal(c.setHardforkByBlockNumber(1450409), Hardfork.Merge, msg) |
| 275 | + st.equal( |
| 276 | + c.setHardforkByBlockNumber(1450409, BigInt('17000000000000000')), |
| 277 | + Hardfork.Merge, |
| 278 | + msg |
| 279 | + ) |
| 280 | + st.equal(c.setHardforkByBlockNumber(1735371), Hardfork.MergeForkIdTransition, msg) |
| 281 | + st.equal( |
| 282 | + c.setHardforkByBlockNumber(1735371, BigInt('17000000000000000')), |
| 283 | + Hardfork.MergeForkIdTransition, |
| 284 | + msg |
| 285 | + ) |
| 286 | + // restore value |
| 287 | + mergeHf.block = prevMergeBlockVal |
| 288 | + |
| 289 | + st.end() |
| 290 | + } |
| 291 | + ) |
| 292 | + |
| 293 | + t.test( |
| 294 | + 'should throw if encounters a double ttd hardfork specification', |
| 295 | + function (st: tape.Test) { |
| 296 | + const c = new Common({ chain: Chain.Sepolia }) |
| 297 | + // Add the ttd to mergeForkIdTransition which occurs post merge in sepolia |
| 298 | + c.hardforks().filter((hf) => hf.name === 'mergeForkIdTransition')[0]!['ttd'] = |
| 299 | + '17000000000000000' |
| 300 | + |
| 301 | + try { |
| 302 | + c.setHardforkByBlockNumber(1735371) |
| 303 | + st.fail('should have thrown as two hardforks with ttd specified') |
| 304 | + } catch (error) { |
| 305 | + st.pass('throws error as two hardforks with ttd specified') |
| 306 | + } |
| 307 | + st.end() |
| 308 | + } |
| 309 | + ) |
190 | 310 | }) |
0 commit comments