ethereumjs/ethereumjs-common-2-API文檔

https://github.com/ethereumjs/ethereumjs-common/blob/master/docs/index.mdhtml

 該API的調用的詳細例子可見ethereumjs/ethereumjs-common-3-testnode

Common

Common class to access chain and hardfork parametersgit

訪問鏈和硬分叉參數的Common類github

Parameters輸入參數json

  • chain (String | Number | Dictionary) String ('mainnet') or Number (1) chain  能夠使用字符串(如'mainnet')或數字(如1)表示鏈
  • hardfork String String identifier ('byzantium') for hardfork (optional) (可選項)使用字符串標識('byzantium')表示
  • supportedHardforks Array Limit parameter returns to the given hardforks (optional) (可選項)返回給硬分叉的限制參數

 調用:

let c = new Common('mainnet')//使用的是mainnet鏈
let c = new Common('mainnet', 'byzantium')
let c = new Common('mainnet', 'byzantium', ['byzantium', 'constantinople'])

 

⚠️若是你鏈接的是本身私有的chain,初始化的方式是:bootstrap

let chainParams = require('./testnet.json') //testnet.json中是具體的鏈描述信息 let c = new Common(chainParams, 'byzantium')

testnet.json爲:數組

{
  "name": "testnet", "chainId": 12345, "networkId": 12345, "comment": "Private test network", "genesis": { "hash": "0xaa00000000000000000000000000000000000000000000000000000000000000", "timestamp": null, "gasLimit": 1000000, "difficulty": 1, "nonce": "0xbb00000000000000", "extraData": "0xcc000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "stateRoot": "0xdd00000000000000000000000000000000000000000000000000000000000000" }, "hardforks": [ { "name": "chainstart", "block": 0, "consensus": "poa", "finality": null }, { "name": "homestead", "block": 1, "consensus": "poa", "finality": null }, { "name": "tangerineWhistle", "block": 2, "consensus": "poa", "finality": null }, { "name": "spuriousDragon", "block": 3, "consensus": "poa", "finality": null }, { "name": "byzantium", "block": 4, "consensus": "poa", "finality": null } ], "bootstrapNodes": [ { "ip": "10.0.0.1", "port": 30303, "id": "11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "location": "", "comment": "" }, { "ip": "10.0.0.2", "port": 30303, "id": "22000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "location": "", "comment": "" } ] }

 

注意:網絡

let chainParams = require('./testnet.json') delete chainParams['hardforks'] //若是有任何內容的缺失,初始化時將報錯 let c = new Common(chainParams, 'byzantium') //報錯

 

 

 

setChain

Sets the chain 設置鏈ide

Parameters函數

  • chain (String | Number | Dictionary) String ('mainnet') or Number (1) chain representation. Or, a Dictionary of chain parameters for a private network. 能夠使用字符串(如'mainnet')或數字(如1)表示鏈或者對於私有網絡則使用鏈參數字典

調用:

let c = new Common('mainnet')
c.setChain('ropsten') //從新將鏈設置爲ropsten

 

 

 

setHardfork

Sets the hardfork to get params for

設置硬叉以獲取參數

Parameters

  • hardfork String String identifier ('byzantium')使用字符串標識('byzantium')表示

 調用:

let c = new Common('mainnet') c.param('gasPrices', 'ecAdd', 'byzantium') //500  c.setHardfork('byzantium') st.equal(c.param('gasPrices', 'ecAdd') //500

 

 

_chooseHardfork

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

內部幫助函數,用於在硬分叉集中選擇並提供其爲參數

Parameters

  • hardfork String Hardfork given to function as a parameter 做爲函數參數的硬分叉
  • onlySupported

Returns String Hardfork chosen to be used 返回選擇使用的硬分叉

 

_getHardfork

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

內部幫助函數,返回鏈集的給定硬分叉的參數

Parameters

  • hardfork String Hardfork name 硬分叉名字

Returns Dictionary

 

_isSupportedHardfork

Internal helper function to check if a hardfork is set to be supported by the library

內部幫助函數,查看設置的硬分叉是否被庫支持

Parameters

  • hardfork String Hardfork name 硬分叉的名字

Returns Boolean True if hardfork is supported 若是硬分叉被支持則返回true

 調用:

let c = new Common('mainnet', 'byzantium', ['byzantium', 'constantinople'])
c._isSupportedHardfork('byzantium') //返回true
c._isSupportedHardfork('spuriousDragon') //返回false

 

 

param

Returns the parameter corresponding to a hardfork

返回與硬分叉相關聯的參數

Parameters

  • topic String Parameter topic ('gasConfig', 'gasPrices', 'vm', 'pow', 'casper', 'sharding') 參數主題(有'gasConfig', 'gasPrices', 'vm', 'pow', 'casper', 'sharding')
  • name String Parameter name (e.g. 'minGasLimit' for 'gasConfig' topic) 參數名字 (好比'gasConfig' 主題中的 'minGasLimit'
  • hardfork String Hardfork name, optional if hardfork set 影分叉的名字,若是硬分叉集可選

 調用:

let c = new Common('mainnet')
c.param('gasPrices', 'ecAdd', 'byzantium') //500

c.setHardfork('byzantium')
st.equal(c.param('gasPrices', 'ecAdd') //500

 

 

paramByBlock

Returns a parameter for the hardfork active on block number

返回在區塊數中活躍硬分叉的參數

Parameters

  • topic String Parameter topic 參數主體
  • name String Parameter name 參數名
  • blockNumber Number Block number 區塊數

調用:

let c = new Common('mainnet', 'byzantium')
c.paramByBlock('pow', 'minerReward', 4370000) //'3000000000000000000'

 

 

hardforkIsActiveOnBlock

Checks if set or provided hardfork is active on block number

查看是否設置了或提供的硬分叉在區塊數中是活躍的

Parameters

  • hardfork String Hardfork name or null (for HF set) 硬分叉名字或null
  • blockNumber Number 區塊數
  • opts Array
    • opts.onlySupported Array.Boolean optional, only allow supported HFs (default: false) (可選的)表示只支持HFs ,即初始化時本身設置的supportedHardforks(默認爲false)

Returns Boolean

調用:

let c = new Common('ropsten')
c.hardforkIsActiveOnBlock('byzantium', 1700000) //返回true,等價於下面的
c.activeOnBlock(1700000) //返回true

c.hardforkIsActiveOnBlock('byzantium', 1700005)//返回true
c.hardforkIsActiveOnBlock('byzantium', 1699999) //返回false

 

c = new Common('ropsten', 'byzantium')
c.hardforkIsActiveOnBlock(null, 1700000) //true,由於上面已經設置鏈分叉爲 'byzantium',因此這裏使用的是null
c.hardforkIsActiveOnBlock(null, 1700005) //true
c.hardforkIsActiveOnBlock(null, 1699999) //false

 

 

activeOnBlock

Alias to hardforkIsActiveOnBlock when hardfork is set

當硬分叉被設置時,其爲hardforkIsActiveOnBlock的別名

Parameters

  • blockNumber Number區塊數
  • opts Array
    • opts.onlySupported Array.Boolean optional, only allow supported HFs (default: false)(可選的)表示只支持HFs ,即初始化時本身設置的supportedHardforks(默認爲false)

Returns Boolean

 調用:

c = new Common('ropsten', 'byzantium')//當分叉已經設置時
c.hardforkIsActiveOnBlock(null, 1700000) //true,或使用
c.activeOnBlock(1700000)

 

 

 

hardforkGteHardfork

Sequence based check if given or set HF1 is greater than or equal HF2

基於序列的檢查是否給定的或設置的硬分叉1強過或等同於硬分叉2時

Parameters

  • hardfork1 String Hardfork name or null (if set) 硬分叉名字或null
  • hardfork2 String Hardfork name 硬分叉名字
  • opts Array
    • opts.onlyActive Array.Boolean optional, only active HFs (default: false)(可選的)表示活躍HFs(默認爲false)
    • opts.onlySupported Array.Boolean optional, only allow supported HFs (default: false)(可選的)表示只支持HFs ,即初始化時本身設置的supportedHardforks(默認爲false)

Returns Boolean

 調用:

let c = new Common('ropsten')
c.hardforkGteHardfork('constantinople', 'byzantium') //返回true,由於constantinople >= byzantium (provided)
c.hardforkGteHardfork('spuriousDragon', 'byzantium') //false

 

gteHardfork

Alias to hardforkGteHardfork when hardfork is set

當硬分叉被設置時其爲hardforkGteHardfork別名

Parameters

  • hardfork String Hardfork name硬分叉名字
  • opts Array
    • opts.onlyActive Array.Boolean optional, only active HFs (default: false)(可選的)表示活躍HFs(默認爲false)
    • opts.onlySupported Array.Boolean optional, only allow supported HFs (default: false)(可選的)表示只支持HFs ,即初始化時本身設置的supportedHardforks(默認爲false)

Returns Boolean

 調用:

c = new Common('ropsten', 'byzantium') //當分叉已經被設置時
c.hardforkGteHardfork(null, 'spuriousDragon') //返回true,或使用下面
c.gteHardfork('spuriousDragon') //true

 

hardforkIsActiveOnChain

Checks if given or set hardfork is active on the chain

檢查是否給定的或設置的硬分叉在鏈上是活躍的

Parameters

  • hardfork String Hardfork name, optional if HF set 硬分叉名字
  • opts Array
    • opts.onlySupported Array.Boolean optional, only allow supported HFs (default: false) 可選的)表示只支持HFs ,即初始化時本身設置的supportedHardforks(默認爲false)

Returns Boolean

 調用:

let c = new Common('ropsten')
c.hardforkIsActiveOnChain('byzantium') //true
c.hardforkIsActiveOnChain('dao') //false
c.hardforkIsActiveOnChain('spuriousDragon', { onlySupported: true })//由於這裏並無設置,可是使用了onlySupported: true,因此會報出"spuriousDragon"爲不支持的分叉的錯誤

c = new Common('ropsten', 'byzantium')
c.hardforkIsActiveOnChain() //true,即設置的'byzantium'在ropsten鏈中活躍

 

 

activeHardforks

Returns the active hardfork switches for the current chain

返回當前鏈的活躍硬分叉開關

Parameters

  • blockNumber Number up to block if provided, otherwise for the whole chain 若是提供則查詢至該指定區塊,不然爲整條鏈
  • opts Array
    • opts.onlySupported Array.Boolean optional, limit results to supported HFs (default: false)(可選的)表示只支持HFs 即初始化時本身設置的supportedHardforks(默認爲false)

Returns Array Array with hardfork arrays 硬分叉數組

 調用:

let c = new Common('ropsten')
c.activeHardforks().length //返回5,說明ropsten鏈中有5個活躍分叉類型
c.activeHardforks()[3]['name'] //'spuriousDragon'
c.activeHardforks(9).length //返回3,即直到區塊9有的活躍分叉個數爲3

 

c = new Common('ropsten', null, ['spuriousDragon', 'byzantium', 'constantinople'])
c.activeHardforks(null, { onlySupported: true }).length //返回2 ,onlySupported: true說明只支持supportedHardforks裏面的分叉,因此返回的結果就從5變成了2,只包含了2個活躍分叉類型

 

 

activeHardfork

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

返回鏈或區塊最新的活躍硬分叉的名字,若是不可行則拋出錯誤

Parameters

  • blockNumber Number up to block if provided, otherwise for the whole chain若是提供則查詢至該指定區塊,不然爲整條鏈
  • opts Array
    • opts.onlySupported Array.Boolean optional, limit results to supported HFs (default: false)(可選的)表示只支持HFs即初始化時本身設置的supportedHardforks(默認爲false)

Returns String Hardfork name 硬分叉名

 調用:

let c = new Common('ropsten')
c.activeHardfork() //返回byzantium ,說明整條鏈最新的分叉爲byzantium
c.activeHardfork(10) //返回spuriousDragon,即到區塊10的最新分叉類型爲spuriousDragon

 

c = new Common('ropsten', null, ['tangerineWhistle', 'spuriousDragon'])
c.activeHardfork(null, { onlySupported: true }) //返回'spuriousDragon',由於裏最新的類型爲它supportedHardforks

 

hardforkBlock

Returns the hardfork change block for hardfork provided or set

爲提供或設置的硬分叉返回硬分叉改變塊

Parameters

  • hardfork String Hardfork name, optional if HF set 硬分叉名,若是硬分叉集設置則可選

Returns Number Block number 返回區塊數

 例子:

let c = new Common('ropsten')
c.hardforkBlock('byzantium') //返回1700000,獲得byzantium分叉開始的區塊數

 

 

isHardforkBlock

True if block number provided is the hardfork (given or set) change block of the current chain

若是提供的區塊數是目前鏈的硬分叉(給定或設置的)改變區塊,則返回true

Parameters

  • blockNumber Number Number of the block to check 查看的區塊數
  • hardfork String Hardfork name, optional if HF set 硬分叉名,若是硬分叉集設置則可選

Returns Boolean

 調用:

let c = new Common('ropsten')
c.isHardforkBlock(1700000) //返回true
c.isHardforkBlock(1700001) //返回false

 

 

consensus共識

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

爲設置或提供的硬分叉集提供共識類型做爲參數

Parameters

  • hardfork String Hardfork name, optional if hardfork set 硬分叉名,若是硬分叉集設置則可選

Returns String Consensus type (e.g. 'pow', 'poa') 返回共識類型(如'pow', 'poa'

 

let c = new Common('mainnet')
c.consensus('byzantium') //返回byzantium分叉共識爲'pow'

 

 

finality

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

爲設置或提供的硬分叉集提供結束類型做爲參數

Parameters

  • hardfork String Hardfork name, optional if hardfork set 硬分叉名,若是硬分叉集設置則可選

Returns String Finality type (e.g. 'pos', null of no finality) 結束類型(如'pos',沒有則爲null)

 調用:

let c = new Common('mainnet')
c.finality('byzantium') //null

 

 

genesis

Returns the Genesis parameters of current chain

返回當前鏈的初始狀態參數

Returns Dictionary Genesis dict 初始字典

 如:

 "genesis": {
    "hash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
    "timestamp": null,
    "gasLimit": 5000,
    "difficulty": 17179869184,
    "nonce": "0x0000000000000042",
    "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
    "stateRoot": "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544"
  }

調用:

let c = new Common('mainnet')
c.genesis().hash //返回"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"

 

 

hardforks

Returns the hardforks for current chain返回當前鏈的硬分叉

Returns Array Array with arrays of hardforks 硬分叉的數組

 如:

 "hardforks": [
    {
      "name": "chainstart",
      "block": 0,
      "consensus": "pow",
      "finality": null
    },
    {
      "name": "homestead",
      "block": 1150000,
      "consensus": "pow",
      "finality": null
    }
]

調用:

let c = new Common('mainnet')
c.hardforks()[1]['block']//返回1150000

 

 

hardfork

Returns the hardfork set 返回硬分叉集

Returns String Hardfork name 硬分叉名字

調用:

let c = new Common('mainnet', 'byzantium') 
c.hardfork() //返回'byzantium'

 

 

bootstrapNodes

Returns bootstrap nodes for the current chain 返回當前鏈的bootstrap節點

Returns Dictionary Dict with bootstrap nodes  bootstrap節點字典

如:

  "bootstrapNodes": [
    {
      "ip": "13.93.211.84",
      "port": 30303,
      "id": "3f1d12044546b76342d59d4a05532c14b85aa669704bfe1f864fe079415aa2c02d743e03218e57a33fb94523adb54032871a6c51b2cc5514cb7c7e35b3ed0a99",
      "location": "US-WEST",
      "comment": "Go Bootnode"
    },
    {
      "ip": "191.235.84.50",
      "port": 30303,
      "id": "78de8a0916848093c73790ead81d1928bec737d565119932b98c6b100d944b7a95e94f847f689fc723399d2e31129d182f7ef3863f2b4c820abbf3ab2722344d",
      "location": "BR",
      "comment": "Go Bootnode"
    }
]

調用:

let c = new Common('mainnet')
c.bootstrapNodes()[0].port //返回:30303

 

 

chainId

Returns the Id of current chain 返回當前鏈的ID

Returns Number chain Id

調用:

let c = new Common('mainnet')
c.chainId() //返回1

 

 

chainName

Returns the name of current chain 返回當前鏈的名字

Returns String chain name (lower case) (小寫)

調用:

let c = new Common('mainnet')
c.chainName() //返回'mainnet'

 

 

networkId

Returns the Id of current network 返回當前網絡的ID

Returns Number network Id

 調用:

let c = new Common('mainnet')
c.networkId() //返回1
相關文章
相關標籤/搜索