Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.

Commit efe5bf2

Browse files
authored
Merge pull request #11 from ethereumjs/add-active-hardfork
Added activeHardfork() function
2 parents 35713f0 + 9910fad commit efe5bf2

File tree

3 files changed

+108
-66
lines changed

3 files changed

+108
-66
lines changed

docs/index.md

Lines changed: 81 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,62 @@
1313
- [hardforkIsActiveOnBlock][9]
1414
- [hardforkIsActiveOnChain][10]
1515
- [activeHardforks][11]
16-
- [hardforkBlock][12]
17-
- [isHardforkBlock][13]
18-
- [consensus][14]
19-
- [finality][15]
20-
- [genesis][16]
21-
- [hardforks][17]
22-
- [bootstrapNodes][18]
23-
- [hardfork][19]
24-
- [chainId][20]
25-
- [chainName][21]
26-
- [networkId][22]
16+
- [activeHardfork][12]
17+
- [hardforkBlock][13]
18+
- [isHardforkBlock][14]
19+
- [consensus][15]
20+
- [finality][16]
21+
- [genesis][17]
22+
- [hardforks][18]
23+
- [bootstrapNodes][19]
24+
- [hardfork][20]
25+
- [chainId][21]
26+
- [chainName][22]
27+
- [networkId][23]
2728

2829
## Common
2930

3031
Common class to access chain and hardfork parameters
3132

3233
**Parameters**
3334

34-
- `chain` **([String][23] \| [Number][24])** String ('mainnet') or Number (1) chain representation
35-
- `hardfork` **[String][23]** String identifier ('byzantium') for hardfork (optional)
36-
- `supportedHardforks` **[Array][25]** Limit parameter returns to the given hardforks (optional)
35+
- `chain` **([String][24] \| [Number][25])** String ('mainnet') or Number (1) chain representation
36+
- `hardfork` **[String][24]** String identifier ('byzantium') for hardfork (optional)
37+
- `supportedHardforks` **[Array][26]** Limit parameter returns to the given hardforks (optional)
3738

3839
### setChain
3940

4041
Sets the chain
4142

4243
**Parameters**
4344

44-
- `chain` **([String][23] \| [Number][24])** String ('mainnet') or Number (1) chain representation
45+
- `chain` **([String][24] \| [Number][25])** String ('mainnet') or Number (1) chain representation
4546

4647
### setHardfork
4748

4849
Sets the hardfork to get params for
4950

5051
**Parameters**
5152

52-
- `hardfork` **[String][23]** String identifier ('byzantium')
53+
- `hardfork` **[String][24]** String identifier ('byzantium')
5354

5455
### \_chooseHardfork
5556

5657
Internal helper function to choose between hardfork set and hardfork provided as param
5758

5859
**Parameters**
5960

60-
- `hardfork` **[String][23]** Hardfork given to function as a parameter
61+
- `hardfork` **[String][24]** Hardfork given to function as a parameter
6162

62-
Returns **[String][23]** Hardfork chosen to be used
63+
Returns **[String][24]** Hardfork chosen to be used
6364

6465
### \_getHardfork
6566

6667
Internal helper function, returns the params for the given hardfork for the chain set
6768

6869
**Parameters**
6970

70-
- `hardfork` **[String][23]** Hardfork name
71+
- `hardfork` **[String][24]** Hardfork name
7172

7273
Returns **Dictionary**
7374

@@ -77,107 +78,119 @@ Internal helper function to check if a hardfork is set to be supported by the li
7778

7879
**Parameters**
7980

80-
- `hardfork` **[String][23]** Hardfork name
81+
- `hardfork` **[String][24]** Hardfork name
8182

82-
Returns **[Boolean][26]** True if hardfork is supported
83+
Returns **[Boolean][27]** True if hardfork is supported
8384

8485
### param
8586

8687
Returns the parameter corresponding to a hardfork
8788

8889
**Parameters**
8990

90-
- `topic` **[String][23]** Parameter topic ('gasConfig', 'gasPrices', 'vm', 'pow', 'casper', 'sharding')
91-
- `name` **[String][23]** Parameter name (e.g. 'minGasLimit' for 'gasConfig' topic)
92-
- `hardfork` **[String][23]** Hardfork name, optional if hardfork set
91+
- `topic` **[String][24]** Parameter topic ('gasConfig', 'gasPrices', 'vm', 'pow', 'casper', 'sharding')
92+
- `name` **[String][24]** Parameter name (e.g. 'minGasLimit' for 'gasConfig' topic)
93+
- `hardfork` **[String][24]** Hardfork name, optional if hardfork set
9394

9495
### paramByBlock
9596

9697
Returns a parameter for the hardfork active on block number
9798

9899
**Parameters**
99100

100-
- `topic` **[String][23]** Parameter topic
101-
- `name` **[String][23]** Parameter name
102-
- `blockNumber` **[Number][24]** Block number
101+
- `topic` **[String][24]** Parameter topic
102+
- `name` **[String][24]** Parameter name
103+
- `blockNumber` **[Number][25]** Block number
103104

104105
### hardforkIsActiveOnBlock
105106

106107
Checks if a hardfork is active for a given block number
107108

108109
**Parameters**
109110

110-
- `hardfork` **[String][23]** Hardfork name
111-
- `blockNumber` **[Number][24]**
112-
- `opts` **[Array][25]**
111+
- `hardfork` **[String][24]** Hardfork name
112+
- `blockNumber` **[Number][25]**
113+
- `opts` **[Array][26]**
113114
- `opts.onlySupported` **Array.Boolean** optional, only allow supported HFs (default: false)
114115

115-
Returns **[Boolean][26]**
116+
Returns **[Boolean][27]**
116117

117118
### hardforkIsActiveOnChain
118119

119120
Checks if the hardfork provided is active on the chain
120121

121122
**Parameters**
122123

123-
- `hardfork` **[String][23]**
124-
- `opts` **[Array][25]**
124+
- `hardfork` **[String][24]**
125+
- `opts` **[Array][26]**
125126
- `opts.onlySupported` **Array.Boolean** optional, only allow supported HFs (default: false)
126127

127-
Returns **[Boolean][26]**
128+
Returns **[Boolean][27]**
128129

129130
### activeHardforks
130131

131132
Returns the active hardfork switches for the current chain
132133

133134
**Parameters**
134135

135-
- `blockNumber` **[Number][24]** up to block if provided, otherwise for the whole chain
136-
- `opts` **[Array][25]**
136+
- `blockNumber` **[Number][25]** up to block if provided, otherwise for the whole chain
137+
- `opts` **[Array][26]**
137138
- `opts.onlySupported` **Array.Boolean** optional, limit results to supported HFs (default: false)
138139

139-
Returns **[Array][25]** Array with hardfork arrays
140+
Returns **[Array][26]** Array with hardfork arrays
141+
142+
### activeHardfork
143+
144+
Returns the latest active hardfork name for chain or block or throws if unavailable
145+
146+
**Parameters**
147+
148+
- `blockNumber` **[Number][25]** up to block if provided, otherwise for the whole chain
149+
- `opts` **[Array][26]**
150+
- `opts.onlySupported` **Array.Boolean** optional, limit results to supported HFs (default: false)
151+
152+
Returns **[String][24]** Hardfork name
140153

141154
### hardforkBlock
142155

143156
Returns the hardfork change block for the given hardfork
144157

145158
**Parameters**
146159

147-
- `hardfork` **[String][23]** Hardfork name
160+
- `hardfork` **[String][24]** Hardfork name
148161

149-
Returns **[Number][24]** Block number
162+
Returns **[Number][25]** Block number
150163

151164
### isHardforkBlock
152165

153166
True if block number provided is the hardfork change block of the current chain
154167

155168
**Parameters**
156169

157-
- `hardfork` **[String][23]** Hardfork name
158-
- `blockNumber` **[Number][24]** Number of the block to check
170+
- `hardfork` **[String][24]** Hardfork name
171+
- `blockNumber` **[Number][25]** Number of the block to check
159172

160-
Returns **[Boolean][26]**
173+
Returns **[Boolean][27]**
161174

162175
### consensus
163176

164177
Provide the consensus type for the hardfork set or provided as param
165178

166179
**Parameters**
167180

168-
- `hardfork` **[String][23]** Hardfork name, optional if hardfork set
181+
- `hardfork` **[String][24]** Hardfork name, optional if hardfork set
169182

170-
Returns **[String][23]** Consensus type (e.g. 'pow', 'poa')
183+
Returns **[String][24]** Consensus type (e.g. 'pow', 'poa')
171184

172185
### finality
173186

174187
Provide the finality type for the hardfork set or provided as param
175188

176189
**Parameters**
177190

178-
- `hardfork` **[String][23]** Hardfork name, optional if hardfork set
191+
- `hardfork` **[String][24]** Hardfork name, optional if hardfork set
179192

180-
Returns **[String][23]** Finality type (e.g. 'pos', null of no finality)
193+
Returns **[String][24]** Finality type (e.g. 'pos', null of no finality)
181194

182195
### genesis
183196

@@ -189,7 +202,7 @@ Returns **Dictionary** Genesis dict
189202

190203
Returns the hardforks for current chain
191204

192-
Returns **[Array][25]** Array with arrays of hardforks
205+
Returns **[Array][26]** Array with arrays of hardforks
193206

194207
### bootstrapNodes
195208

@@ -201,25 +214,25 @@ Returns **Dictionary** Dict with bootstrap nodes
201214

202215
Returns the hardfork set
203216

204-
Returns **[String][23]** Hardfork name
217+
Returns **[String][24]** Hardfork name
205218

206219
### chainId
207220

208221
Returns the Id of current chain
209222

210-
Returns **[Number][24]** chain Id
223+
Returns **[Number][25]** chain Id
211224

212225
### chainName
213226

214227
Returns the name of current chain
215228

216-
Returns **[String][23]** chain name (lower case)
229+
Returns **[String][24]** chain name (lower case)
217230

218231
### networkId
219232

220233
Returns the Id of current network
221234

222-
Returns **[Number][24]** network Id
235+
Returns **[Number][25]** network Id
223236

224237
[1]: #common
225238

@@ -243,32 +256,34 @@ Returns **[Number][24]** network Id
243256

244257
[11]: #activehardforks
245258

246-
[12]: #hardforkblock
259+
[12]: #activehardfork
260+
261+
[13]: #hardforkblock
247262

248-
[13]: #ishardforkblock
263+
[14]: #ishardforkblock
249264

250-
[14]: #consensus
265+
[15]: #consensus
251266

252-
[15]: #finality
267+
[16]: #finality
253268

254-
[16]: #genesis
269+
[17]: #genesis
255270

256-
[17]: #hardforks
271+
[18]: #hardforks
257272

258-
[18]: #bootstrapnodes
273+
[19]: #bootstrapnodes
259274

260-
[19]: #hardfork
275+
[20]: #hardfork
261276

262-
[20]: #chainid
277+
[21]: #chainid
263278

264-
[21]: #chainname
279+
[22]: #chainname
265280

266-
[22]: #networkid
281+
[23]: #networkid
267282

268-
[23]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
283+
[24]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
269284

270-
[24]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
285+
[25]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
271286

272-
[25]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
287+
[26]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
273288

274-
[26]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
289+
[27]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,22 @@ class Common {
203203
return activeHardforks
204204
}
205205

206+
/**
207+
* Returns the latest active hardfork name for chain or block or throws if unavailable
208+
* @param {Number} blockNumber up to block if provided, otherwise for the whole chain
209+
* @param {Array} opts
210+
* @param {Array.Boolean} opts.onlySupported optional, limit results to supported HFs (default: false)
211+
* @return {String} Hardfork name
212+
*/
213+
activeHardfork (blockNumber, opts) {
214+
let activeHardforks = this.activeHardforks(blockNumber, opts)
215+
if (activeHardforks.length > 0) {
216+
return activeHardforks[activeHardforks.length - 1]['name']
217+
} else {
218+
throw new Error(`No (supported) active hardfork found`)
219+
}
220+
}
221+
206222
/**
207223
* Returns the hardfork change block for the given hardfork
208224
* @param {String} hardfork Hardfork name

tests/hardforks.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ tape('[Common]: Hardfork logic', function (t) {
5151
st.end()
5252
})
5353

54+
t.test('activeHardfork()', function (st) {
55+
let c = new Common('ropsten')
56+
st.equal(c.activeHardfork(), 'byzantium', 'should return byzantium as latest active HF for Ropsten')
57+
st.equal(c.activeHardfork(10), 'spuriousDragon', 'should return spuriousDragon as latest active HF for Ropsten for block 10')
58+
59+
c = new Common('ropsten', null, ['tangerineWhistle', 'spuriousDragon'])
60+
st.equal(c.activeHardfork(null, { onlySupported: true }), 'spuriousDragon', 'should return spuriousDragon as latest active HF for Ropsten with limited supported hardforks')
61+
62+
st.end()
63+
})
64+
5465
t.test('hardforkIsActiveOnChain()', function (st) {
5566
let c = new Common('ropsten')
5667
st.equal(c.hardforkIsActiveOnChain('byzantium'), true, 'should return true for byzantium on Ropsten')

0 commit comments

Comments
 (0)