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

Added activeHardfork() function #11

Merged
merged 1 commit into from
May 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added activeHardfork() function to get latest active HF for chain or …
…block
holgerd77 committed May 14, 2018
commit 9910fad7528a6f95656a8dae9b66ac0c10b14089
147 changes: 81 additions & 66 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -13,61 +13,62 @@
- [hardforkIsActiveOnBlock][9]
- [hardforkIsActiveOnChain][10]
- [activeHardforks][11]
- [hardforkBlock][12]
- [isHardforkBlock][13]
- [consensus][14]
- [finality][15]
- [genesis][16]
- [hardforks][17]
- [bootstrapNodes][18]
- [hardfork][19]
- [chainId][20]
- [chainName][21]
- [networkId][22]
- [activeHardfork][12]
- [hardforkBlock][13]
- [isHardforkBlock][14]
- [consensus][15]
- [finality][16]
- [genesis][17]
- [hardforks][18]
- [bootstrapNodes][19]
- [hardfork][20]
- [chainId][21]
- [chainName][22]
- [networkId][23]

## Common

Common class to access chain and hardfork parameters

**Parameters**

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

### setChain

Sets the chain

**Parameters**

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

### setHardfork

Sets the hardfork to get params for

**Parameters**

- `hardfork` **[String][23]** String identifier ('byzantium')
- `hardfork` **[String][24]** String identifier ('byzantium')

### \_chooseHardfork

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

**Parameters**

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

Returns **[String][23]** Hardfork chosen to be used
Returns **[String][24]** Hardfork chosen to be used

### \_getHardfork

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

**Parameters**

- `hardfork` **[String][23]** Hardfork name
- `hardfork` **[String][24]** Hardfork name

Returns **Dictionary**

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

**Parameters**

- `hardfork` **[String][23]** Hardfork name
- `hardfork` **[String][24]** Hardfork name

Returns **[Boolean][26]** True if hardfork is supported
Returns **[Boolean][27]** True if hardfork is supported

### param

Returns the parameter corresponding to a hardfork

**Parameters**

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

### paramByBlock

Returns a parameter for the hardfork active on block number

**Parameters**

- `topic` **[String][23]** Parameter topic
- `name` **[String][23]** Parameter name
- `blockNumber` **[Number][24]** Block number
- `topic` **[String][24]** Parameter topic
- `name` **[String][24]** Parameter name
- `blockNumber` **[Number][25]** Block number

### hardforkIsActiveOnBlock

Checks if a hardfork is active for a given block number

**Parameters**

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

Returns **[Boolean][26]**
Returns **[Boolean][27]**

### hardforkIsActiveOnChain

Checks if the hardfork provided is active on the chain

**Parameters**

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

Returns **[Boolean][26]**
Returns **[Boolean][27]**

### activeHardforks

Returns the active hardfork switches for the current chain

**Parameters**

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

Returns **[Array][25]** Array with hardfork arrays
Returns **[Array][26]** Array with hardfork arrays

### activeHardfork

Returns the latest active hardfork name for chain or block or throws if unavailable

**Parameters**

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

Returns **[String][24]** Hardfork name

### hardforkBlock

Returns the hardfork change block for the given hardfork

**Parameters**

- `hardfork` **[String][23]** Hardfork name
- `hardfork` **[String][24]** Hardfork name

Returns **[Number][24]** Block number
Returns **[Number][25]** Block number

### isHardforkBlock

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

**Parameters**

- `hardfork` **[String][23]** Hardfork name
- `blockNumber` **[Number][24]** Number of the block to check
- `hardfork` **[String][24]** Hardfork name
- `blockNumber` **[Number][25]** Number of the block to check

Returns **[Boolean][26]**
Returns **[Boolean][27]**

### consensus

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

**Parameters**

- `hardfork` **[String][23]** Hardfork name, optional if hardfork set
- `hardfork` **[String][24]** Hardfork name, optional if hardfork set

Returns **[String][23]** Consensus type (e.g. 'pow', 'poa')
Returns **[String][24]** Consensus type (e.g. 'pow', 'poa')

### finality

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

**Parameters**

- `hardfork` **[String][23]** Hardfork name, optional if hardfork set
- `hardfork` **[String][24]** Hardfork name, optional if hardfork set

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

### genesis

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

Returns the hardforks for current chain

Returns **[Array][25]** Array with arrays of hardforks
Returns **[Array][26]** Array with arrays of hardforks

### bootstrapNodes

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

Returns the hardfork set

Returns **[String][23]** Hardfork name
Returns **[String][24]** Hardfork name

### chainId

Returns the Id of current chain

Returns **[Number][24]** chain Id
Returns **[Number][25]** chain Id

### chainName

Returns the name of current chain

Returns **[String][23]** chain name (lower case)
Returns **[String][24]** chain name (lower case)

### networkId

Returns the Id of current network

Returns **[Number][24]** network Id
Returns **[Number][25]** network Id

[1]: #common

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

[11]: #activehardforks

[12]: #hardforkblock
[12]: #activehardfork

[13]: #hardforkblock

[13]: #ishardforkblock
[14]: #ishardforkblock

[14]: #consensus
[15]: #consensus

[15]: #finality
[16]: #finality

[16]: #genesis
[17]: #genesis

[17]: #hardforks
[18]: #hardforks

[18]: #bootstrapnodes
[19]: #bootstrapnodes

[19]: #hardfork
[20]: #hardfork

[20]: #chainid
[21]: #chainid

[21]: #chainname
[22]: #chainname

[22]: #networkid
[23]: #networkid

[23]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[24]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[24]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[25]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

[25]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[26]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

[26]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
[27]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -203,6 +203,22 @@ class Common {
return activeHardforks
}

/**
* Returns the latest active hardfork name for chain or block or throws if unavailable
* @param {Number} blockNumber up to block if provided, otherwise for the whole chain
* @param {Array} opts
* @param {Array.Boolean} opts.onlySupported optional, limit results to supported HFs (default: false)
* @return {String} Hardfork name
*/
activeHardfork (blockNumber, opts) {
let activeHardforks = this.activeHardforks(blockNumber, opts)
if (activeHardforks.length > 0) {
return activeHardforks[activeHardforks.length - 1]['name']
} else {
throw new Error(`No (supported) active hardfork found`)
}
}

/**
* Returns the hardfork change block for the given hardfork
* @param {String} hardfork Hardfork name
11 changes: 11 additions & 0 deletions tests/hardforks.js
Original file line number Diff line number Diff line change
@@ -51,6 +51,17 @@ tape('[Common]: Hardfork logic', function (t) {
st.end()
})

t.test('activeHardfork()', function (st) {
let c = new Common('ropsten')
st.equal(c.activeHardfork(), 'byzantium', 'should return byzantium as latest active HF for Ropsten')
st.equal(c.activeHardfork(10), 'spuriousDragon', 'should return spuriousDragon as latest active HF for Ropsten for block 10')

c = new Common('ropsten', null, ['tangerineWhistle', 'spuriousDragon'])
st.equal(c.activeHardfork(null, { onlySupported: true }), 'spuriousDragon', 'should return spuriousDragon as latest active HF for Ropsten with limited supported hardforks')

st.end()
})

t.test('hardforkIsActiveOnChain()', function (st) {
let c = new Common('ropsten')
st.equal(c.hardforkIsActiveOnChain('byzantium'), true, 'should return true for byzantium on Ropsten')