ethereumjs/ethereumjs-block-2-api
https://github.com/ethereumjs/ethereumjs-block/blob/master/docs/index.md
详细的调用代码可见本博客的ethereumjs/ethereumjs-block-3-tests
1.Block区块
Creates a new block object创建一个区块对象
Parameters输入参数
data(Array | Buffer | Object) 相关数据optsArray Options 选项opts.chain(String | Number) The chain for the block [default: 'mainnet'] 区块连接的链(默认为主链mainnet)opts.hardforkString Hardfork for the block [default: null, block number-based behaviour] 区块的分支(默认为null,区块基于数量的行为,当区块的数量大于协议中的数量时就会进行分叉)opts.commonObject Alternatively pass a Common instance (ethereumjs-common) instead of setting chain/hardfork directly 可选地传递Common实例 (ethereumjs-common)以替代直接设置上面的chain/hardfork
Properties属性
headerHeader the block's header 区块的头uncleListArray<Header> an array of uncle headers 叔块头的数组rawArray<Buffer> an array of buffers containing the raw blocks. 包含原始区块的buffers数组
调用:
const block1 = new Block(null, { 'chain': 'ropsten' }) //初始化,一种是提供chain/hardfork
const common = new Common('ropsten')
const block2 = new Block(null, { 'common': common })//另一种则是使用common,common和chain不能同时提供,会报错
const testData = require('./bcBlockGasLimitTest.json').tests
const bcBlockGasLimigTestData = testData.BlockGasLimit2p63m1
Object.keys(bcBlockGasLimigTestData).forEach(key => {
var parentBlock = new Block(rlp.decode(bcBlockGasLimigTestData[key].genesisRLP)) //使用genesisRLP初始化block得到的是父区块
var block = new Block(rlp.decode(bcBlockGasLimigTestData[key].blocks[].rlp)) //使用blocks[0].rlp初始化block得到的是本区块
block.header.validateGasLimit(parentBlock)//true
})
hash
Produces a hash the RLP of the block 生成区块的RLP的hash值
调用:
var common = new Common('ropsten')
var genesisBlock = new Block(null, { common: common })
genesisBlock.setGenesisParams()
console.log(genesisBlock.hash().toString('hex') === common.genesis().hash.slice()) //true
isGenisis
Determines if a given block is the genesis block 定义给定的区块是否时创世区块
Returns any Boolean 返回任何布尔值
调用:
var block = new Block()
block.isGenesis()//查看是否为初始区块,为false
block.header.number = Buffer.from([])//决定因素是block.header.number,设置为空数组buffer即可
block.isGenesis()// true
setGenesisParams
turns the block into the canonical genesis block 将区块变为规范创世区块
调用:
const block1 = new Block(null, { 'chain': 'ropsten' }) //初始化,一种是提供chain/hardfork
const common = new Common('ropsten')
const block2 = new Block(null, { 'common': common })//另一种则是使用common,common和chain不能同时提供,会报错
block1.setGenesisParams()//就是将该区块设置为规范初始区块
block2.setGenesisParams()
)//因为两个都设置为规范初始区块,所以相应的值是相同的
block1.hash().toString('hex')
block2.hash().toString('hex'
serialize
Produces a serialization of the block. 生成块的序列化
Parameters 参数
rlpEncodeBoolean whether to rlp encode the block or not 是否对区块进行rlp编码
调用:
const testDataGenesis = require('./genesishashestest.json').test//(初始区块信息)
var genesisBlock = new Block()
genesisBlock.setGenesisParams()//设置为初始区块
var rlp = genesisBlock.serialize() //序列化,就是将其生产rlp格式
console.log(rlp.toString('hex') === testDataGenesis.genesis_rlp_hex) //true
console.log(genesisBlock.hash().toString('hex') === testDataGenesis.genesis_hash) //true
genTxTrie
Generate transaction trie. The tx trie must be generated before the transaction trie can be validated with validateTransactionTrie
生成交易前缀树。交易前缀树一定要在交易前缀树能够被validateTransactionTrie验证之前生成
Parameters参数
cbFunction the callback 回调函数
调用:
const block = new Block(null, { 'chain': 'ropsten' })
block.genTxTrie(function () {//必须要先生成了前缀树后才能调用验证前缀树的操作
block.validateTransactionsTrie()// true
})
validateTransactionTrie
Validates the transaction trie 验证交易前缀树
Returns Boolean
调用看上面
validateTransactions
Validates the transactions 验证交易
Parameters输入参数
stringErrorBoolean? whether to return a string with a description of why the validation failed or return a Bloolean (optional, defaultfalse) 是否返回带有描述验证失败原因的字符串或返回一个布尔值(可选,默认为false)
Returns Boolean
调用:
const block = new Block(null, { 'chain': 'ropsten' })
block.validateTransactions()// true
validate
Validates the entire block. Returns a string to the callback if block is invalid 验证整个区块。如果区块无效则返回字符串给回调函数
Parameters输入参数
blockChainBlockChain the blockchain that this block wants to be part of 区块想要加入的区块链cbFunction the callback which is given aStringif the block is not valid 如果区块无效将会提供给字符串的回调函数
validateUncleHash
Validates the uncle's hash 验证叔块的hash
Returns Boolean
调用:
const testData2 = require('./testdata2.json')
var block = new Block(rlp.decode(testData2.blocks[].rlp)) //从区块信息文件生成相同区块
block.validateUnclesHash()// true,验证该区块的叔块hash
validateUncles
Validates the uncles that are in the block if any. Returns a string to the callback if uncles are invalid
如果区块中有叔块,则对其进行验证
Parameters
blockChainaBlockchain an instance of the Blockchain 区块链实例cbFunction the callback 回调函数blockChain
toJSON
Converts the block to JSON
将区块转换成JSON格式
Parameters输入参数
labeledBool whether to create an labeled object or an array 是否要创建一个标签对象或一个数组
Returns Object
调用:
var block = new Block(rlp.decode(testData2.blocks[].rlp))
console.log(typeof (block.toJSON()) === 'object') //true
2.BlockHeader区块头
An object that repersents the block header 代表区块头的对象
Parameters输入参数
dataArray raw data, deserialized 原始数据,非序列化optsArray Optionsopts.chain(String | Number) The chain for the block header [default: 'mainnet'] 区块连接的链(默认为主链mainnet)opts.hardforkString Hardfork for the block header [default: null, block number-based behaviour] 区块的分支(默认为null,区块基于数量的行为,当区块的数量大于协议中的数量时就会进行分叉)opts.commonObject Alternatively pass a Common instance instead of setting chain/hardfork directly 可选地传递Common实例 (ethereumjs-common)以替代直接设置上面的chain/hardfork
Properties属性
parentHashBuffer the blocks' parent's hash 区块的父区块的hash值uncleHashBuffer sha3(rlp_encode(uncle_list)) 叔块列表的rlp编码的hash值coinbaseBuffer the miner address 矿工的地址stateRootBuffer The root of a Merkle Patricia tree Merkle Patricia树的根transactionTrieBuffer the root of a Trie containing the transactions 包含交易的前缀树的根receiptTrieBuffer the root of a Trie containing the transaction Reciept 包含交易收据的前缀树的根bloomBufferdifficultyBuffer 设置的挖矿的困难度numberBuffer the block's height 区块的高度gasLimitBuffer 使用gas的限制gasUsedBuffer 真正使用的gastimestampBuffer 区块的时间戳extraDataBuffer 额外的数据rawArray<Buffer> an array of buffers containing the raw blocks. 包含原始区块的buffers数组
调用:
const header1 = new Header(null, { 'chain': 'ropsten' }) //初始化,一种是提供chain/hardfork
const common = new Common('ropsten')
const header2 = new Header(null, { 'common': common })//另一种则是使用common,common和chain不能同时提供,会报错
canonicalDifficulty
Returns the canoncical difficulty of the block
返回区块头的规范困难度
Parameters输入参数
parentBlockBlock the parentBlockof the this header 这个区块头的父区块
Returns BN
从父区块得到的区块头规范困难度与当前区块头的困难度应该是相等的
validateDifficulty
checks that the block's difficuly matches the canonical difficulty
查看区块头是否符合规范困难度的区块困难度
Parameters
parentBlockBlock this block's parent 这个区块的父区块
Returns Boolean
validateGasLimit
Validates the gasLimit
验证gas限制
Parameters
parentBlockBlock this block's parent 区块的父区块
Returns Boolean
validate
Validates the entire block header
验证整个区块头
Parameters输入参数
blockChainBlockchain the blockchain that this block is validating against 验证区块所在的区块链heightBignum? if this is an uncle header, this is the height of the block that is including it 如果是一个叔块头,这将是包含这个叔块区块的区块的高度cbFunction the callback function. The callback is given anerrorif the block is invalid 回调函数。如果区块无效,回调则返回错误blockchain
hash
Returns the sha3 hash of the blockheader
返回区块头的sha3 hash值
Returns Buffer
isGenesis
checks if the blockheader is a genesis header
查看区块头是否是初始区块头
Returns Boolean
调用:
var header = new Header() //查看该区块头是否为初始区块
header.isGenesis()//false)
header.number = Buffer.from([]) //通过设置header.number为空buffer数组就能得到是初始区块的结果
header.isGenesis()// true
setGenesisParams
turns the header into the canonical genesis block header
转换该区块成规范初始区块头
调用:
const header2 = new Header(null, { 'common': common })//使用common初始化,common和chain不能同时提供,会报错
header1.setGenesisParams()//就是将该区块头设置为规范初始区块头
ethereumjs/ethereumjs-block-2-api的更多相关文章
- ethereumjs/ethereumjs-blockchain-1-简介和API
https://github.com/ethereumjs/ethereumjs-blockchain SYNOPSIS概要 A module to store and interact with b ...
- ethereumjs/ethereumjs-account-1-简介和API
https://github.com/ethereumjs/ethereumjs-account Encoding, decoding and validation of Ethereum's Acc ...
- ethereumjs/ethereumjs-vm-3-StateManager
https://github.com/ethereumjs/ethereumjs-vm/blob/master/docs/stateManager.md StateManager 要与本博客的ethe ...
- 用block做事件回调来简化代码,提高开发效率
我们在自定义view的时候,通常要考虑view的封装复用,所以如何把view的事件回调给Controller就是个需要好好考虑的问题, 一般来说,可选的方式主要有target-action和de ...
- Insight API开源项目介绍
首先,在阅读本文以前假设您已经了解比特币Bitcoin基本原理. Insight API是一个开源基于比特币Bitcoin blockchain的REST风格的API框架.Insigh ...
- openstack 云平台API
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAKrCAIAAACV8EEMAAAgAElEQVR4nOydeVgUaZ7n/W9299nd7n
- UIViewController所有API的学习。
<欢迎大家加入iOS开发学习交流群:QQ529560119> /* UIViewController is a generic controller base class tha ...
- 【译】Android NDK API 规范
[译]Android NDK API 规范 译者按: 修改R代码遇到Lint tool的报错,搜到了这篇文档,aosp仓库地址:Android NDK API Guidelines. 975a589 ...
- chunkupload 文件上传断点续传组件(java) - 正式发布
chunkupload简介 chunkupload是一款基于java语言的断点续传组件,针对文件上传,非文件下载,集成方便,使用简单. chunkupload实现如下功能: · 实现断点续传 · ...
- GCD 开发
一.简介 GCD 的全称是 Grand Centre Dispatch 是一个强大的任务编程管理工具.通过GCD你可以同步或者异步地执行block.function. 二.dispatch Queue ...
随机推荐
- [PHP] PHP数组的实现哈希表(HashTable)结构
PHP中使用最为频繁的数据类型非字符串和数组莫属,使用哈希表实现的PHP数组.1.数据结构:保存哈希表容器,保存数据的容器2.哈希函数实现:需要尽可能的将不同的key映射到不同的槽(bucket)中, ...
- 七、spark核心数据集RDD
简介 spark RDD操作具体参考官网:http://spark.apache.org/docs/latest/rdd-programming-guide.html#overview RDD全称叫做 ...
- bash shell学习笔记(一)—— 常用命令
一.基本的bash shell命令 1.默认bash shell 提示符是美元符号($); 2.bash手册 使用man命令来访问存储在Linux系统上的手册页面,如: bogon:~ Mac$ ma ...
- cf1090 I.Minimal Product(贪心)
题意 题目链接 给出长度为\(n\)的序列\(a\),序列中的元素取值为\([-2e9, 2e9]\) 找到两个位置\((i, j) (i <j, a[i] < a[j])\),最小化\( ...
- Java基础之引用(String,char[],Integer)总结于牛客网的专项练习题
1.String的引用: 下列代码执行后的结果为: public class Test { public static void main(String[] args) { StringBuffer ...
- MySQL数据库(8)----表的创建、删除、索引和更改
MySQL允许使用 CREATE TABLE 语句和 DROP TABLE 语句来创建.删除表,使用 ALTER TABLE 语句更改其结构.CREATE INDEX 语句和 DROP INDEX 语 ...
- PID控制算法
PID控制算法 四轴如何起飞的原理 四轴飞行器的螺旋桨与空气发生相对运动,产生了向上的升力,当升力大于四轴的重力时四轴就可以起飞了. 四轴飞行器飞行过程中如何保持水平: 我们先假设一种理想状况:四个电 ...
- 整理一下最近Android面试的提问
java相关: 1. public protect private default关键字有什么区别? public:表示可以在任何一个类中被访问: protect:表示可以在自身.子类以及同一包下的类 ...
- Android 线程池的类型、区别以及为何要用线程池
每个 Android 应用进程在创建时,会同时创建一个线程,我们称之为主线程,负责更新 UI 界面以及和处理用户之间的交互,因此,在 Android 中,我们又称之为 UI 线程.一个进程中 UI 线 ...
- sql join实例图解
https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ 1. inner join: 所有满足join条件的两个table对应 ...