Skip to content

Commit 5da7322

Browse files
emersonmacroholgerd77
authored andcommitted
Common, Block: remove and rename ambiguous active methods (#1753)
* common: rename hardforkIsActiveOnChain to isIncludedHardfork * common: remove activeHardfork and activeHardforks * common: adapt _getHardfork, remove isIncludedHardfork * common: type fixes in forkHash
1 parent 9f5ded6 commit 5da7322

File tree

7 files changed

+41
-134
lines changed

7 files changed

+41
-134
lines changed

packages/block/src/header.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ export class BlockHeader {
447447
)
448448
throw new Error(msg)
449449
}
450-
const hardfork = this._getHardfork()
450+
const hardfork = this._common.hardfork()
451451
const blockTs = this.timestamp
452452
const { timestamp: parentTs, difficulty: parentDif } = parentBlockHeader
453453
const minimumDifficulty = new BN(
@@ -570,7 +570,7 @@ export class BlockHeader {
570570
parentGasLimit = parentGasLimit.mul(elasticity)
571571
}
572572
const gasLimit = this.gasLimit
573-
const hardfork = this._getHardfork()
573+
const hardfork = this._common.hardfork()
574574

575575
const a = parentGasLimit.div(
576576
new BN(this._common.paramByHardfork('gasConfig', 'gasLimitBoundDivisor', hardfork))
@@ -607,7 +607,7 @@ export class BlockHeader {
607607
if (this.isGenesis()) {
608608
return
609609
}
610-
const hardfork = this._getHardfork()
610+
const hardfork = this._common.hardfork()
611611
// Consensus type dependent checks
612612
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Ethash) {
613613
// PoW/Ethash
@@ -980,10 +980,6 @@ export class BlockHeader {
980980
return jsonDict
981981
}
982982

983-
private _getHardfork(): string {
984-
return this._common.hardfork() || this._common.activeHardfork(this.number.toNumber())
985-
}
986-
987983
private async _getHeaderByHash(
988984
blockchain: Blockchain,
989985
hash: Buffer
@@ -1005,7 +1001,7 @@ export class BlockHeader {
10051001
* activation block (see: https://blog.slock.it/hard-fork-specification-24b889e70703)
10061002
*/
10071003
private _validateDAOExtraData() {
1008-
if (!this._common.hardforkIsActiveOnChain(Hardfork.Dao)) {
1004+
if (!this._common.hardforkIsActiveOnBlock(Hardfork.Dao, this.number)) {
10091005
return
10101006
}
10111007
const DAOActivationBlock = this._common.hardforkBlock(Hardfork.Dao)

packages/block/test/block.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import util from 'util'
1717

1818
tape('[Block]: block functions', function (t) {
1919
t.test('should test block initialization', function (st) {
20-
const common = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.Chainstart })
20+
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
2121
const genesis = Block.genesis({}, { common })
2222
st.ok(genesis.hash().toString('hex'), 'block should initialize')
2323

packages/common/src/index.ts

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,14 @@ export default class Common extends EventEmitter {
505505
/**
506506
* Internal helper function, returns the params for the given hardfork for the chain set
507507
* @param hardfork Hardfork name
508-
* @returns Dictionary with hardfork params
508+
* @returns Dictionary with hardfork params or null if hardfork not on chain
509509
*/
510-
_getHardfork(hardfork: string | Hardfork): any {
510+
_getHardfork(hardfork: string | Hardfork): HardforkParams | null {
511511
const hfs = this.hardforks()
512512
for (const hf of hfs) {
513513
if (hf['name'] === hardfork) return hf
514514
}
515-
throw new Error(`Hardfork ${hardfork} not defined for chain ${this.chainName()}`)
515+
return null
516516
}
517517

518518
/**
@@ -620,14 +620,15 @@ export default class Common extends EventEmitter {
620620
}
621621

622622
/**
623-
* Returns a parameter for the hardfork active on block number
623+
* Returns a parameter for the hardfork active on block number or
624+
* optional provided total difficulty (Merge HF)
624625
* @param topic Parameter topic
625626
* @param name Parameter name
626627
* @param blockNumber Block number
628+
* @param td Total difficulty
627629
*/
628-
paramByBlock(topic: string, name: string, blockNumber: BNLike): any {
629-
const activeHfs = this.activeHardforks(blockNumber)
630-
const hardfork = activeHfs[activeHfs.length - 1]['name']
630+
paramByBlock(topic: string, name: string, blockNumber: BNLike, td?: BNLike): any {
631+
const hardfork = this.getHardforkByBlockNumber(blockNumber, td)
631632
return this.paramByHardfork(topic, name, hardfork)
632633
}
633634

@@ -711,58 +712,14 @@ export default class Common extends EventEmitter {
711712
return this.hardforkGteHardfork(null, hardfork)
712713
}
713714

714-
/**
715-
* Checks if given or set hardfork is active on the chain
716-
* @param hardfork Hardfork name, optional if HF set
717-
* @returns True if hardfork is active on the chain
718-
*/
719-
hardforkIsActiveOnChain(hardfork?: string | Hardfork | null): boolean {
720-
hardfork = hardfork ?? this._hardfork
721-
for (const hf of this.hardforks()) {
722-
if (hf['name'] === hardfork && hf['block'] !== null) return true
723-
}
724-
return false
725-
}
726-
727-
/**
728-
* Returns the active hardfork switches for the current chain
729-
* @param blockNumber up to block if provided, otherwise for the whole chain
730-
* @return Array with hardfork arrays
731-
*/
732-
activeHardforks(blockNumber?: BNLike | null): HardforkParams[] {
733-
const activeHardforks: HardforkParams[] = []
734-
const hfs = this.hardforks()
735-
for (const hf of hfs) {
736-
if (hf['block'] === null) continue
737-
if (blockNumber !== undefined && blockNumber !== null && blockNumber < hf['block']) break
738-
739-
activeHardforks.push(hf)
740-
}
741-
return activeHardforks
742-
}
743-
744-
/**
745-
* Returns the latest active hardfork name for chain or block or throws if unavailable
746-
* @param blockNumber up to block if provided, otherwise for the whole chain
747-
* @return Hardfork name
748-
*/
749-
activeHardfork(blockNumber?: BNLike | null): string {
750-
const activeHardforks = this.activeHardforks(blockNumber)
751-
if (activeHardforks.length > 0) {
752-
return activeHardforks[activeHardforks.length - 1]['name']
753-
} else {
754-
throw new Error(`No (supported) active hardfork found`)
755-
}
756-
}
757-
758715
/**
759716
* Returns the hardfork change block for hardfork provided or set
760717
* @param hardfork Hardfork name, optional if HF set
761718
* @returns Block number or null if unscheduled
762719
*/
763720
hardforkBlock(hardfork?: string | Hardfork): BN | null {
764721
hardfork = hardfork ?? this._hardfork
765-
const block = this._getHardfork(hardfork)['block']
722+
const block = this._getHardfork(hardfork)?.['block']
766723
if (block === undefined || block === null) {
767724
return null
768725
}
@@ -776,7 +733,7 @@ export default class Common extends EventEmitter {
776733
*/
777734
hardforkTD(hardfork?: string | Hardfork): BN | null {
778735
hardfork = hardfork ?? this._hardfork
779-
const td = this._getHardfork(hardfork)['td']
736+
const td = this._getHardfork(hardfork)?.['td']
780737
if (td === undefined || td === null) {
781738
return null
782739
}
@@ -869,14 +826,14 @@ export default class Common extends EventEmitter {
869826
* Returns an eth/64 compliant fork hash (EIP-2124)
870827
* @param hardfork Hardfork name, optional if HF set
871828
*/
872-
forkHash(hardfork?: string | Hardfork) {
829+
forkHash(hardfork?: string | Hardfork): string {
873830
hardfork = hardfork ?? this._hardfork
874831
const data = this._getHardfork(hardfork)
875-
if (data['block'] === null && data['td'] === undefined) {
832+
if (data === null || (data?.['block'] === null && data?.['td'] === undefined)) {
876833
const msg = 'No fork hash calculation possible for future hardfork'
877834
throw new Error(msg)
878835
}
879-
if (data['forkHash'] !== undefined) {
836+
if (data?.['forkHash'] !== null && data?.['forkHash'] !== undefined) {
880837
return data['forkHash']
881838
}
882839
return this._calcForkHash(hardfork)

packages/common/tests/hardforks.spec.ts

Lines changed: 10 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ tape('[Common]: Hardfork logic', function (t: tape.Test) {
7575
let msg = 'should return the correct HF change block for byzantium (provided)'
7676
st.ok(c.hardforkBlock(Hardfork.Byzantium)!.eqn(1700000), msg)
7777

78+
msg = 'should return null if HF does not exist on chain'
79+
st.equal(c.hardforkBlock('thisHardforkDoesNotExist'), null, msg)
80+
7881
c = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.Byzantium })
7982
msg = 'should return the correct HF change block for byzantium (set)'
8083
st.ok(c.hardforkBlock()!.eqn(1700000), msg)
@@ -150,50 +153,6 @@ tape('[Common]: Hardfork logic', function (t: tape.Test) {
150153
st.end()
151154
})
152155

153-
t.test('activeHardforks()', function (st: tape.Test) {
154-
let c = new Common({ chain: Chain.Ropsten })
155-
let msg = 'should return correct number of active hardforks for Ropsten'
156-
st.equal(c.activeHardforks().length, 11, msg)
157-
158-
msg = 'should return the correct HF data for Ropsten'
159-
st.equal(c.activeHardforks()[3]['name'], Hardfork.SpuriousDragon, msg)
160-
161-
msg = 'should return 3 active hardforks for Ropsten up to block 9'
162-
st.equal(c.activeHardforks(9).length, 3, msg)
163-
164-
msg = 'should return 4 active hardforks for Ropsten up to block 10'
165-
st.equal(c.activeHardforks(10).length, 4, msg)
166-
167-
c = new Common({ chain: Chain.Mainnet })
168-
msg = 'should return correct number of active HFs for mainnet'
169-
st.equal(c.activeHardforks().length, 13, msg)
170-
171-
c = new Common({ chain: Chain.Rinkeby })
172-
msg = 'should return correct number of active HFs for rinkeby'
173-
st.equal(c.activeHardforks().length, 10, msg)
174-
175-
c = new Common({ chain: Chain.Goerli })
176-
msg = 'should return correct number of active HFs for goerli'
177-
st.equal(c.activeHardforks().length, 10, msg)
178-
179-
st.end()
180-
})
181-
182-
t.test('activeHardfork()', function (st: tape.Test) {
183-
let c = new Common({ chain: Chain.Ropsten })
184-
let msg = 'should return correct latest active HF for Ropsten'
185-
st.equal(c.activeHardfork(), Hardfork.London, msg)
186-
187-
msg = 'should return spuriousDragon as latest active HF for Ropsten for block 10'
188-
st.equal(c.activeHardfork(10), Hardfork.SpuriousDragon, msg)
189-
190-
c = new Common({ chain: Chain.Rinkeby })
191-
msg = 'should return correct latest active HF for Rinkeby'
192-
st.equal(c.activeHardfork(), Hardfork.London, msg)
193-
194-
st.end()
195-
})
196-
197156
t.test('hardforkIsActiveOnBlock() / activeOnBlock()', function (st: tape.Test) {
198157
let c = new Common({ chain: Chain.Ropsten })
199158
let msg = 'Ropsten, byzantium (provided), 1700000 -> true'
@@ -276,27 +235,6 @@ tape('[Common]: Hardfork logic', function (t: tape.Test) {
276235
st.end()
277236
})
278237

279-
t.test('hardforkIsActiveOnChain()', function (st: tape.Test) {
280-
let c = new Common({ chain: Chain.Ropsten })
281-
let msg = 'should return true for byzantium (provided) on Ropsten'
282-
st.equal(c.hardforkIsActiveOnChain(Hardfork.Byzantium), true, msg)
283-
284-
msg = 'should return false for dao (provided) on Ropsten'
285-
st.equal(c.hardforkIsActiveOnChain(Hardfork.Dao), false, msg)
286-
287-
msg = 'should return true for petersburg (provided) on Ropsten'
288-
st.equal(c.hardforkIsActiveOnChain(Hardfork.Petersburg), true, msg)
289-
290-
msg = 'should return false for a non-existing HF (provided) on Ropsten'
291-
st.equal(c.hardforkIsActiveOnChain('notexistinghardfork'), false, msg)
292-
293-
c = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.Byzantium })
294-
msg = 'should return true for byzantium (set) on Ropsten'
295-
st.equal(c.hardforkIsActiveOnChain(), true, msg)
296-
297-
st.end()
298-
})
299-
300238
t.test('_calcForkHash()', function (st: tape.Test) {
301239
let c = new Common({ chain: Chain.Mainnet })
302240
let msg = 'should calc correctly for chainstart (only genesis)'
@@ -333,12 +271,18 @@ tape('[Common]: Hardfork logic', function (t: tape.Test) {
333271
st.equal(c.forkHash(Hardfork.SpuriousDragon), '0x3edd5b10', msg)
334272

335273
c = new Common({ chain: Chain.Kovan })
336-
const f = () => {
274+
let f = () => {
337275
c.forkHash(Hardfork.Merge)
338276
}
339277
msg = 'should throw when called on non-applied or future HF'
340278
st.throws(f, /No fork hash calculation possible/, msg)
341279

280+
f = () => {
281+
c.forkHash('thisHardforkDoesNotExist')
282+
}
283+
msg = 'should throw when called with a HF that does not exist on chain'
284+
st.throws(f, /No fork hash calculation possible/, msg)
285+
342286
st.end()
343287
})
344288

packages/common/tests/mergePOS.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ tape('[Common]: Merge/POS specific logic', function (t: tape.Test) {
88
const customChains = [testnetMerge]
99
const c = new Common({ chain: 'testnetMerge', hardfork: Hardfork.Istanbul, customChains })
1010
st.ok(c.hardforkTD(Hardfork.Merge)?.eqn(5000), 'should get the HF total difficulty')
11+
st.equal(
12+
c.hardforkTD('thisHardforkDoesNotExist'),
13+
null,
14+
'should return null if HF does not exist on chain'
15+
)
1116

1217
st.end()
1318
})

packages/common/tests/params.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import tape from 'tape'
22
import Common, { Chain, Hardfork } from '../src/'
3+
import { BN } from 'ethereumjs-util'
34

45
tape('[Common]: Parameter access for param(), paramByHardfork()', function (t: tape.Test) {
56
t.test('Basic usage', function (st: tape.Test) {
@@ -72,6 +73,10 @@ tape('[Common]: Parameter access for param(), paramByHardfork()', function (t: t
7273
msg = 'Should correctly translate block numbers into HF states (original value)'
7374
st.equal(c.paramByBlock('pow', 'minerReward', 4369999), '5000000000000000000', msg)
7475

76+
msg = 'Should correctly translate total difficulty into HF states'
77+
const td = new BN('1196768507891266117779')
78+
st.equal(c.paramByBlock('pow', 'minerReward', 4370000, td), '3000000000000000000', msg)
79+
7580
st.comment('-----------------------------------------------------------------')
7681
st.end()
7782
})

packages/vm/src/runBlock.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { debug as createDebugLogger } from 'debug'
22
import { BaseTrie as Trie } from 'merkle-patricia-tree'
33
import { Account, Address, bigIntToBN, BN, bnToBigInt, intToBuffer, rlp } from 'ethereumjs-util'
44
import { Block } from '@ethereumjs/block'
5-
import { ConsensusType } from '@ethereumjs/common'
5+
import { ConsensusType, Hardfork } from '@ethereumjs/common'
66
import VM from './index'
77
import Bloom from './bloom'
88
import { StateManager } from './state'
@@ -143,8 +143,8 @@ export default async function runBlock(this: VM, opts: RunBlockOpts): Promise<Ru
143143

144144
// check for DAO support and if we should apply the DAO fork
145145
if (
146-
this._common.hardforkIsActiveOnChain('dao') &&
147-
block.header.number.eq(this._common.hardforkBlock('dao')!)
146+
this._common.hardforkIsActiveOnBlock(Hardfork.Dao, block.header.number) &&
147+
block.header.number.eq(this._common.hardforkBlock(Hardfork.Dao)!)
148148
) {
149149
if (this.DEBUG) {
150150
debug(`Apply DAO hardfork`)

0 commit comments

Comments
 (0)