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) 相关数据
  • opts Array Options 选项
    • opts.chain (String | Number) The chain for the block [default: 'mainnet'] 区块连接的链(默认为主链mainnet)
    • opts.hardfork String Hardfork for the block [default: null, block number-based behaviour]  区块的分支(默认为null,区块基于数量的行为,当区块的数量大于协议中的数量时就会进行分叉)
    • opts.common Object Alternatively pass a Common instance (ethereumjs-common) instead of setting chain/hardfork directly  可选地传递Common实例 (ethereumjs-common)以替代直接设置上面的chain/hardfork

Properties属性

  • header Header the block's header  区块的头
  • uncleList Array<Header> an array of uncle headers  叔块头的数组
  • raw Array<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 参数

  • rlpEncode Boolean 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参数

调用:

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输入参数

  • stringError Boolean? whether to return a string with a description of why the validation failed or return a Bloolean (optional, default false) 是否返回带有描述验证失败原因的字符串或返回一个布尔值(可选,默认为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输入参数

  • blockChain BlockChain the blockchain that this block wants to be part of 区块想要加入的区块链
  • cb Function the callback which is given a String if 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

  • blockChaina Blockchain an instance of the Blockchain 区块链实例
  • cb Function the callback 回调函数
  • blockChain

toJSON

Converts the block to JSON

将区块转换成JSON格式

Parameters输入参数

  • labeled Bool 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输入参数

  • data Array raw data, deserialized 原始数据,非序列化
  • opts Array Options
    • opts.chain (String | Number) The chain for the block header [default: 'mainnet']  区块连接的链(默认为主链mainnet)
    • opts.hardfork String Hardfork for the block header [default: null, block number-based behaviour] 区块的分支(默认为null,区块基于数量的行为,当区块的数量大于协议中的数量时就会进行分叉)
    • opts.common Object Alternatively pass a Common instance instead of setting chain/hardfork directly 可选地传递Common实例 (ethereumjs-common)以替代直接设置上面的chain/hardfork

Properties属性

  • parentHash Buffer the blocks' parent's hash  区块的父区块的hash值
  • uncleHash Buffer sha3(rlp_encode(uncle_list))   叔块列表的rlp编码的hash值
  • coinbase Buffer the miner address  矿工的地址
  • stateRoot Buffer The root of a Merkle Patricia tree  Merkle Patricia树的根
  • transactionTrie Buffer the root of a Trie containing the transactions 包含交易的前缀树的根
  • receiptTrie Buffer the root of a Trie containing the transaction Reciept  包含交易收据的前缀树的根
  • bloom Buffer
  • difficulty Buffer 设置的挖矿的困难度
  • number Buffer the block's height 区块的高度
  • gasLimit Buffer 使用gas的限制
  • gasUsed Buffer  真正使用的gas
  • timestamp Buffer  区块的时间戳
  • extraData Buffer 额外的数据
  • raw Array<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输入参数

  • parentBlock Block the parent Block of the this header 这个区块头的父区块

Returns BN

从父区块得到的区块头规范困难度与当前区块头的困难度应该是相等的

validateDifficulty

checks that the block's difficuly matches the canonical difficulty

查看区块头是否符合规范困难度的区块困难度

Parameters

  • parentBlock Block this block's parent 这个区块的父区块

Returns Boolean

validateGasLimit

Validates the gasLimit

验证gas限制

Parameters

  • parentBlock Block this block's parent 区块的父区块

Returns Boolean

validate

Validates the entire block header

验证整个区块头

Parameters输入参数

  • blockChain Blockchain the blockchain that this block is validating against 验证区块所在的区块链
  • height Bignum? if this is an uncle header, this is the height of the block that is including it 如果是一个叔块头,这将是包含这个叔块区块的区块的高度
  • cb Function the callback function. The callback is given an error if 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的更多相关文章

  1. ethereumjs/ethereumjs-blockchain-1-简介和API

    https://github.com/ethereumjs/ethereumjs-blockchain SYNOPSIS概要 A module to store and interact with b ...

  2. ethereumjs/ethereumjs-account-1-简介和API

    https://github.com/ethereumjs/ethereumjs-account Encoding, decoding and validation of Ethereum's Acc ...

  3. ethereumjs/ethereumjs-vm-3-StateManager

    https://github.com/ethereumjs/ethereumjs-vm/blob/master/docs/stateManager.md StateManager 要与本博客的ethe ...

  4. 用block做事件回调来简化代码,提高开发效率

       我们在自定义view的时候,通常要考虑view的封装复用,所以如何把view的事件回调给Controller就是个需要好好考虑的问题, 一般来说,可选的方式主要有target-action和de ...

  5. Insight API开源项目介绍

           首先,在阅读本文以前假设您已经了解比特币Bitcoin基本原理. Insight API是一个开源基于比特币Bitcoin  blockchain的REST风格的API框架.Insigh ...

  6. openstack 云平台API

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAKrCAIAAACV8EEMAAAgAElEQVR4nOydeVgUaZ7n/W9299nd7n

  7. UIViewController所有API的学习。

    <欢迎大家加入iOS开发学习交流群:QQ529560119> /*      UIViewController is a generic controller base class tha ...

  8. 【译】Android NDK API 规范

    [译]Android NDK API 规范 译者按: 修改R代码遇到Lint tool的报错,搜到了这篇文档,aosp仓库地址:Android NDK API Guidelines. 975a589 ...

  9. chunkupload 文件上传断点续传组件(java) - 正式发布

    chunkupload简介 chunkupload是一款基于java语言的断点续传组件,针对文件上传,非文件下载,集成方便,使用简单. chunkupload实现如下功能: ·  实现断点续传 ·  ...

  10. GCD 开发

    一.简介 GCD 的全称是 Grand Centre Dispatch 是一个强大的任务编程管理工具.通过GCD你可以同步或者异步地执行block.function. 二.dispatch Queue ...

随机推荐

  1. 二、socket编写简单BIO的HTTP服务器

    一.目标 诸如tomcat等web服务器中间件简化了我们web的开发成本,但有时候我们或许并不需要这么一个完备的服务器,只是希望做一个简单地处理或者做特殊用途的服务器. 本文将提供一个HTTP的服务器 ...

  2. Qless 相关知识

    Qless是一个基于redis的分布式任务架构.相关代码在 https://github.com/seomoz/qless 它是完全有lua实现的,依靠 redis 对lua的支持,http://ww ...

  3. java设计模式-----3、抽象工厂模式

    抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态.抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式.抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体的情况下,创 ...

  4. MVC 中导出Execl 对 科学计数 的转化

    相信大家在使用MVC 的做execl 数据导出的时候,遇到过 身份证号码 银行卡号 交易号 等一大串数字的时候,在导出execl 的时候就会出现 科学计数的输入:下面来说一下解决方案: 第一种:在MV ...

  5. csharp:A Custom CheckedListBox with Datasource

    /// <summary> /// (eraghi) /// Custom CheckedListBox with binding facilities (Value property) ...

  6. Maven学习总结(三):修改从Maven中心仓库下载到本地的jar包的默认存储位置

    一:修改从Maven中心仓库下载到本地的jar包的默认存储位置 从Maven中心仓库下载到本地的jar包的默认存放在”${user.home}/.m2/repository”中,${user.home ...

  7. Angular进阶教程三

    7 总结 angular上手比较难,初学者(特别是习惯了使用JQuery的人)可能不太适应其语法以及思想.随着对ng探索的一步步深入,也确实感觉到了这一点,尤其是框架内部的某些执行机制. 7.1页面效 ...

  8. LeetCode赛题394----Decode String

    394. Decode String Given an encoded string, return it's decoded string. The encoding rule is: k[enco ...

  9. Spring IOC (DI-依赖注入)

    看到一篇文章,讲Spring的依赖注入讲的很好理解,也很容易理解,非常详细.原文地址: https://blog.csdn.net/javazejian/article/details/5456130 ...

  10. IntelliJ Idea编译报错:javacTask: 源发行版 1.8 需要目标发行版 1.8

    解决办法: 1.Project Settings-Modules,选择项目,选择language level 8 2.选中项目,右击选择Maven-->Reimport, 再次编译. 3.Fil ...