ethereumjs/ethereumjs-common-3-test
查看test能够让你更好滴了解其API文档的使用
ethereumjs-common/tests/chains.js
const tape = require('tape')
const Common = require('../index.js') tape('[Common]: Initialization / Chain params', function (t) {
t.test('Should initialize with chain provided', function (st) {//只使用chain来初始化一个Common对象
let c = new Common('mainnet')//使用的是mainnet链
st.equal(c.chainName(), 'mainnet', 'should initialize with chain name')//使用chainName API得到当前链名
st.equal(c.chainId(), , 'should return correct chain Id') //得到chainId
st.equal(c.networkId(), , 'should return correct network Id') //得到networkId
st.equal(c.hardfork(), null, 'should set hardfork to null') //使用的硬分叉,因为没有设置,所以是null
st.equal(c._isSupportedHardfork('constantinople'), true, 'should not restrict supported HFs') //是否支持constantinople硬分叉,这是默认支持的硬分叉类型中的一种,所以返回true c = new Common() //也可以使用chain ID数字来表示mainnet链
st.equal(c.chainName(), 'mainnet', 'should initialize with chain Id') st.end()
}) t.test('Should initialize with chain and hardfork provided', function (st) { //使用chain和hardfork两个参数来初始化对象
let c = new Common('mainnet', 'byzantium') //chain = mainnet ,hardfork = byzantium
st.equal(c.hardfork(), 'byzantium', 'should return correct hardfork name') st.end()
}) t.test('Should initialize with supportedHardforks provided', function (st) { //使用chain、hardfork和supportedHardforks三个参数来初始化对象
let c = new Common('mainnet', 'byzantium', ['byzantium', 'constantinople']) //supportedHardforks = ['byzantium', 'constantinople'],设置只支持这两个硬分叉类型
st.equal(c._isSupportedHardfork('byzantium'), true, 'should return true for supported HF')
st.equal(c._isSupportedHardfork('spuriousDragon'), false, 'should return false for unsupported HF')//因为supportedHardforks中没有它,所以不支持 st.end()
}) t.test('Should handle initialization errors', function (st) {
st.throws(function () { new Common('chainnotexisting') }, /not supported$/, 'should throw an exception on non-existing chain') // eslint-disable-line no-new ,不是支持的chain类型
st.throws(function () { new Common('mainnet', 'hardforknotexisting') }, /not supported$/, 'should throw an exception on non-existing hardfork') // eslint-disable-line no-new ,不是支持的hardfork类型
st.throws(function () { new Common('mainnet', 'spuriousDragon', ['byzantium', 'constantinople']) }, /supportedHardforks$/, 'should throw an exception on conflicting active/supported HF params') // eslint-disable-line no-new ,不是supportedHardforks中包含的hardfork类型 st.end()
}) t.test('Should provide correct access to chain parameters', function (st) {
let c = new Common('mainnet')
st.equal(c.genesis().hash, '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3', 'should return correct genesis hash')//返回当前链的初始状态中的hash值
st.equal(c.hardforks()[]['block'], , 'should return correct hardfork data')//返回当前链的硬分叉数组中第四个分叉的'block'值
st.equal(c.bootstrapNodes()[].port, , 'should return a bootstrap node array')//返回当前链的所有bootstrap节点字典中第一个节点的端口port值 st.end()
}) t.test('Should be able to access data for all chains provided', function (st) {
let c = new Common('mainnet')
st.equal(c.genesis().hash, '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3', 'mainnet')
c.setChain('ropsten') //重新将链设置为ropsten
st.equal(c.genesis().hash, '0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d', 'ropsten')
c.setChain('rinkeby')
st.equal(c.genesis().hash, '0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177', 'rinkeby')
c.setChain('kovan')
st.equal(c.genesis().hash, '0xa3c565fc15c7478862d50ccd6561e3c06b24cc509bf388941c25ea985ce32cb9', 'kovan')
c.setChain('goerli')
st.equal(c.genesis().hash, '0xfa57319d09fd8a32faaf18d338c8a925a5a7975285bf29ecd024e083cba8abb1', 'goerli') st.end()
}) t.test('Should provide correct access to private network chain parameters', function (st) {//如果你连接的chain不是上面那些定义好的chain,而是你私有的或定制的,初始化的方式是下面这样的
let chainParams = require('./testnet.json') //testnet.json中是具体的链描述信息
let c = new Common(chainParams, 'byzantium')
st.equal(c.chainName(), 'testnet', 'should initialize with chain name')
st.equal(c.chainId(), , 'should return correct chain Id')
st.equal(c.networkId(), , 'should return correct network Id')
st.equal(c.genesis().hash, '0xaa00000000000000000000000000000000000000000000000000000000000000', 'should return correct genesis hash')
st.equal(c.hardforks()[]['block'], , 'should return correct hardfork data')
st.equal(c.bootstrapNodes()[].ip, '10.0.0.2', 'should return a bootstrap node array') st.end()
}) t.test('Should handle custom chain parameters with missing field', function (st) {
let chainParams = require('./testnet.json')
delete chainParams['hardforks'] //如果有任何内容的缺失,初始化时将报错
st.throws(function () { new Common(chainParams) }, /Missing required/, 'should throw an exception on missing parameter') // eslint-disable-line no-new st.end()
})
})
ethereumjs-common/tests/hardforks.js
const tape = require('tape')
const Common = require('../index.js') tape('[Common]: Hardfork logic', function (t) {
t.test('Hardfork access', function (st) {
let supportedHardforks = [ //设置支持的硬分支类型
'chainstart',
'homestead',
'dao',
'tangerineWhistle',
'spuriousDragon',
'byzantium',
'constantinople'
]
let c for (let hardfork of supportedHardforks) {
c = new Common('mainnet', hardfork)
st.equal(c.hardfork(), hardfork, hardfork)
} st.end()
}) t.test('hardforkBlock()', function (st) {
let c = new Common('ropsten')
st.equal(c.hardforkBlock('byzantium'), , 'should return the correct HF change block for byzantium (provided)') //得到byzantium分叉开始的区块数 c = new Common('ropsten', 'byzantium')
st.equal(c.hardforkBlock(), , 'should return the correct HF change block for byzantium (set)') st.end()
}) t.test('isHardforkBlock()', function (st) {
let c = new Common('ropsten')
st.equal(c.isHardforkBlock(, 'byzantium'), true, 'should return true for HF change block for byzantium (provided)')
st.equal(c.isHardforkBlock(, 'byzantium'), false, 'should return false for another block for byzantium (provided)') c = new Common('ropsten', 'byzantium')
st.equal(c.isHardforkBlock(), true, 'should return true for HF change block for byzantium (set)')
st.equal(c.isHardforkBlock(), false, 'should return false for another block for byzantium (set)') st.end()
}) t.test('activeHardforks()', function (st) {
let c = new Common('ropsten')
st.equal(c.activeHardforks().length, , 'should return 5 active hardforks for Ropsten') //说明ropsten链中有5个活跃分叉类型
st.equal(c.activeHardforks()[]['name'], 'spuriousDragon', 'should return the correct HF data for Ropsten')
st.equal(c.activeHardforks().length, , 'should return 3 active hardforks for Ropsten up to block 9')//即直到区块9有的活跃分叉个数为3
st.equal(c.activeHardforks().length, , 'should return 4 active hardforks for Ropsten up to block 10') c = new Common('ropsten', null, ['spuriousDragon', 'byzantium', 'constantinople'])
//onlySupported: true说明只支持supportedHardforks里面的分叉,所以返回的结果就从5变成了2,只包含了2个活跃分叉类型
st.equal(c.activeHardforks(null, { onlySupported: true }).length, , 'should return 2 active HFs when restricted to supported HFs') 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') //说明整条链最新的分叉为byzantium
st.equal(c.activeHardfork(), 'spuriousDragon', 'should return spuriousDragon as latest active HF for Ropsten for block 10') //即到区块10的最新分叉类型为spuriousDragon c = new Common('ropsten', null, ['tangerineWhistle', 'spuriousDragon'])
//返回'spuriousDragon',因为supportedHardforks里最新的类型为它
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('hardforkIsActiveOnBlock() / activeOnBlock()', function (st) {
let c = new Common('ropsten')
st.equal(c.hardforkIsActiveOnBlock('byzantium', ), true, 'Ropsten, byzantium (provided), 1700000 -> true')
st.equal(c.hardforkIsActiveOnBlock('byzantium', ), true, 'Ropsten, byzantium (provided), 1700005 -> true')
st.equal(c.hardforkIsActiveOnBlock('byzantium', ), false, 'Ropsten, byzantium (provided), 1699999 -> false') c = new Common('ropsten', 'byzantium')
st.equal(c.hardforkIsActiveOnBlock(null, ), true, 'Ropsten, byzantium (set), 1700000 -> true')
st.equal(c.activeOnBlock(), true, 'Ropsten, byzantium (set), 1700000 -> true (alias function)')
st.equal(c.hardforkIsActiveOnBlock(null, ), true, 'Ropsten, byzantium (set), 1700005 -> true')
st.equal(c.hardforkIsActiveOnBlock(null, ), false, 'Ropsten, byzantium (set), 1699999 -> false') st.end()
}) t.test('hardforkGteHardfork()', function (st) {
let c = new Common('ropsten')
st.equal(c.hardforkGteHardfork('constantinople', 'byzantium'), true, 'Ropsten, constantinople >= byzantium (provided) -> true')
st.equal(c.hardforkGteHardfork('constantinople', 'byzantium', { onlyActive: true }), false, 'Ropsten, constantinople >= byzantium (provided), onlyActive -> fale')
st.equal(c.hardforkGteHardfork('byzantium', 'byzantium'), true, 'Ropsten, byzantium >= byzantium (provided) -> true')
st.equal(c.hardforkGteHardfork('spuriousDragon', 'byzantium'), false, 'Ropsten, spuriousDragon >= byzantium (provided) -> false') c = new Common('ropsten', 'byzantium')
st.equal(c.hardforkGteHardfork(null, 'spuriousDragon'), true, 'Ropsten, byzantium (set) >= spuriousDragon -> true')
st.equal(c.gteHardfork('spuriousDragon'), true, 'Ropsten, byzantium (set) >= spuriousDragon -> true (alias function)')
st.equal(c.hardforkGteHardfork(null, 'spuriousDragon', { onlyActive: true }), true, 'Ropsten, byzantium (set) >= spuriousDragon, onlyActive -> true')
st.equal(c.hardforkGteHardfork(null, 'byzantium'), true, 'Ropsten, byzantium (set) >= byzantium -> true')
st.equal(c.hardforkGteHardfork(null, 'constantinople'), false, 'Ropsten, byzantium (set) >= constantinople -> false') st.end()
}) t.test('hardforkIsActiveOnChain()', function (st) {
let c = new Common('ropsten')
st.equal(c.hardforkIsActiveOnChain('byzantium'), true, 'should return true for byzantium (provided) on Ropsten')
st.equal(c.hardforkIsActiveOnChain('dao'), false, 'should return false for dao (provided) on Ropsten')
st.equal(c.hardforkIsActiveOnChain('constantinople'), false, 'should return false for constantinople (provided) on Ropsten')
st.equal(c.hardforkIsActiveOnChain('notexistinghardfork'), false, 'should return false for a non-existing HF (provided) on Ropsten')
//因为这里并没有设置,但是使用了onlySupported: true,所以会报出"spuriousDragon"为不支持的分叉的错误
st.doesNotThrow(function () { c.hardforkIsActiveOnChain('spuriousDragon', { onlySupported: true }) }, /unsupported hardfork$/, 'should not throw with unsupported Hf (provided) and onlySupported set to false') // eslint-disable-line no-new c = new Common('ropsten', 'byzantium')
st.equal(c.hardforkIsActiveOnChain(), true, 'should return true for byzantium (set) on Ropsten') c = new Common('ropsten', null, ['byzantium', 'constantinople'])
st.throws(function () { c.hardforkIsActiveOnChain('spuriousDragon', { onlySupported: true }) }, /not set as supported in supportedHardforks$/, 'should throw with unsupported Hf and onlySupported set to true') // eslint-disable-line no-new st.end()
}) t.test('consensus()/finality()', function (st) {
let c = new Common('mainnet')
st.equal(c.consensus('byzantium'), 'pow', 'should return pow for byzantium consensus')//返回byzantium分叉共识为'pow'
st.equal(c.consensus('constantinople'), 'pow', 'should return pow for constantinople consensus')
st.equal(c.finality('byzantium'), null, 'should return null for byzantium finality') st.end()
})
})
ethereumjs-common/tests/params.js
const tape = require('tape')
const Common = require('../index.js') tape('[Common]: Parameter access', function (t) {//这个测试就是获取参数值
t.test('Basic usage', function (st) {
let c = new Common('mainnet')
st.equal(c.param('gasPrices', 'ecAdd', 'byzantium'), , 'Should return correct value when HF directly provided') c.setHardfork('byzantium')
st.equal(c.param('gasPrices', 'ecAdd'), , 'Should return correct value for HF set in class') st.end()
}) t.test('Error cases', function (st) {
let c = new Common('mainnet')
st.throws(function () { c.param('gasPrices', 'ecAdd') }, /neither a hardfork set nor provided by param$/, 'Should throw when no hardfork set or provided')
st.throws(function () { c.param('gasPrizes', 'ecAdd', 'byzantium') }, /Topic gasPrizes not defined$/, 'Should throw when called with non-existing topic')
st.throws(function () { c.param('gasPrices', 'notexistingvalue', 'byzantium') }, /value for notexistingvalue not found$/, 'Should throw when called with non-existing value') c.setHardfork('byzantium')
st.equal(c.param('gasPrices', 'ecAdd'), , 'Should return correct value for HF set in class') c = new Common('mainnet', 'byzantium', ['byzantium', 'constantinople'])
st.throws(function () { c.param('gasPrices', 'expByte', 'spuriousDragon') }, /supportedHardforks$/, 'Should throw when calling param() with an unsupported hardfork')
st.throws(function () { c.paramByBlock('gasPrices', 'expByte', ) }, /supportedHardforks$/, 'Should throw when calling paramByBlock() with an unsupported hardfork') st.end()
}) t.test('Parameter updates', function (st) {
let c = new Common('mainnet')
st.throws(function () { c.param('gasPrices', 'ecAdd', 'spuriousDragon') }, /value for ecAdd not found$/, 'Should throw for a value set on a later HF') st.equal(c.param('pow', 'minerReward', 'chainstart'), '', 'Should return correct value for chain start')
st.equal(c.param('pow', 'minerReward', 'byzantium'), '', 'Should reflect HF update changes')
st.equal(c.param('gasPrices', 'netSstoreNoopGas', 'constantinople'), , 'Should return updated sstore gas prices for constantinople') st.end()
}) t.test('Access by block number, paramByBlock()', function (st) {
let c = new Common('mainnet', 'byzantium') st.equal(c.paramByBlock('pow', 'minerReward', ), '', 'Should correctly translate block numbers into HF states (updated value)')
st.equal(c.paramByBlock('pow', 'minerReward', ), '', 'Should correctly translate block numbers into HF states (original value)') st.end()
})
})
ethereumjs-common/tests/testnet.json
{
"name": "testnet",
"chainId": ,
"networkId": ,
"comment": "Private test network",
"genesis": {
"hash": "0xaa00000000000000000000000000000000000000000000000000000000000000",
"timestamp": null,
"gasLimit": ,
"difficulty": ,
"nonce": "0xbb00000000000000",
"extraData": "0xcc000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"stateRoot": "0xdd00000000000000000000000000000000000000000000000000000000000000"
},
"hardforks": [
{
"name": "chainstart",
"block": ,
"consensus": "poa",
"finality": null
},
{
"name": "homestead",
"block": ,
"consensus": "poa",
"finality": null
},
{
"name": "tangerineWhistle",
"block": ,
"consensus": "poa",
"finality": null
},
{
"name": "spuriousDragon",
"block": ,
"consensus": "poa",
"finality": null
},
{
"name": "byzantium",
"block": ,
"consensus": "poa",
"finality": null
}
],
"bootstrapNodes": [
{
"ip": "10.0.0.1",
"port": ,
"id": "",
"location": "",
"comment": ""
},
{
"ip": "10.0.0.2",
"port": ,
"id": "",
"location": "",
"comment": ""
}
]
}
ethereumjs/ethereumjs-common-3-test的更多相关文章
- ethereumjs/ethereumjs-common-1-简介
为了了解ethereumjs/ethereumjs-block-3-代码的使用需要了解的一个模块 https://github.com/ethereumjs/ethereumjs-common Com ...
- ethereumjs/ethereumjs-common-2-API文档
https://github.com/ethereumjs/ethereumjs-common/blob/master/docs/index.md 该API的调用的详细例子可见ethereumjs/e ...
- ethereumjs/ethereumjs-vm-3-StateManager
https://github.com/ethereumjs/ethereumjs-vm/blob/master/docs/stateManager.md StateManager 要与本博客的ethe ...
- ethereumjs/ethereumjs-block-3-tests
之前可以先了解一下另一个模块,看本博客的ethereumjs/ethereumjs-common部分内容 通过tests测试文件能够帮助更好了解API的使用 ethereumjs-block/test ...
- ethereumjs/ethereumjs-block-2-api
https://github.com/ethereumjs/ethereumjs-block/blob/master/docs/index.md 详细的调用代码可见本博客的ethereumjs/eth ...
- ethereumjs/ethereumjs-blockchain-2-test
https://github.com/ethereumjs/ethereumjs-blockchain/tree/master/test 'use strict' const test = requi ...
- ethereumjs/ethereumjs-util
ethereumjs/ethereumjs-util Most of the string manipulation methods are provided by ethjs-util 更多的字符串 ...
- ethereumjs/ethereumjs-wallet
Utilities for handling Ethereum keys ethereumjs-wallet A lightweight wallet implementation. At the m ...
- ethereumjs/ethereumjs-tx
https://github.com/ethereumjs/ethereumjs-tx A simple module for creating, manipulating and signing e ...
- ethereumjs/ethereumjs-icap
https://github.com/ethereumjs/ethereumjs-icap ethereumjs-icap 安装: npm install ethereumjs-icap --save ...
随机推荐
- MySQL 中文未正常显示
关于MySQL中文乱码问题 最近发现,在MySQL的dos客户端输出窗口中查询表中的数据时,表中的中文数据都显示成乱码: 之所以会显示乱码,就是因为MySQL客户端输出窗口显示中文时使用的字符编码不对 ...
- 中小型研发团队架构实践九:任务调度Job
一.Job 简介 Job 类似于数据库中的作业,多用于实现定时执行任务.适用场景主要包括定时轮询数据库同步.定时处理数据.定时邮件通知等. 我们的 Job 分为操作系统级别定时任务 WinJob 和 ...
- Form表单中Post与Get方法的区别
Form提供了两种数据传输的方式:get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的影响. Form中的get和post方法,在数据传输过程中分别 ...
- MVC 导出Execl 的总结几种方式 (四)
这种方式我个人还是比较喜欢的,使用部分视图的方式,导出Execl 这样在编辑样式上也是很方便的 第一步: 编辑导出视图页 @using H5UpdateImage.Models; @{ Layout ...
- POJ3414(KB1-H BFS)
Pots Description You are given two pots, having the volume of A and B liters respectively. The follo ...
- Linux 线程调度与优先级
[转] http://blog.chinaunix.net/uid-20788636-id-1841334.html http://blog.chinaunix.net/uid-20788636-id ...
- 润乾配置连接kingbase(金仓)数据库
问题背景 客户根据项目的不同,使用润乾连接的数据库类型各种各样,此文针对前几日使用润乾设计器连接kingbase金仓数据库做一个说明. kingbase金仓数据库是一款国产数据库,操作方式和配置 ...
- linux rpm之已安装包校验、rpm包中文件提取
已安装包校验 rpm -V 已安装的包名-V 校验指定rpm包中的文件 rpm -V pth没有任何提示,说明自安装后没有做过任何修改 rpm包中文件提取 比如对一个系统配置文件误操作,可以根据这个文 ...
- d3js layout 深入理解
D3 layouts help you create more advanced visualisations such as treemaps: D3 layouts帮助您创造更加高级复杂的可视化图 ...
- [控件] LineAnimationView
LineAnimationView 效果 说明 水平循环无间隔播放动画效果,用于loading的界面 源码 https://github.com/YouXianMing/UI-Component-Co ...