ethereumjs/ethereumjs-block-2-api

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

詳細的調用代碼可見本博客的ethereumjs/ethereumjs-block-3-testsnode

1.Block區塊

Creates a new block object建立一個區塊對象git

Parameters輸入參數github

  • 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屬性json

  • 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數組

 調用:api

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[0].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(2)) //true

 

 

isGenisis

Determines if a given block is the genesis block 定義給定的區塊是否時創世區塊post

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參數

  • cb Function 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輸入參數

  • 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[2].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[2].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()//就是將該區塊頭設置爲規範初始區塊頭
本站公眾號
   歡迎關注本站公眾號,獲取更多信息