ethereumjs/ethereumjs-blockchain-1-簡介和API
https://github.com/ethereumjs/ethereumjs-blockchain
SYNOPSIS概要
A module to store and interact with blocks 儲存區塊和與區塊進行互動的模組
INSTALL安裝
npm install ethereumjs-blockchain
EXAMPLE
The following is an example to iterate through an existing Geth DB (needs level
to be installed separately):
const level = require('level') const Blockchain = require('ethereumjs-blockchain') const utils = require('ethereumjs-util') const gethDbPath = './chaindata' // Add your own path here const db = level(gethDbPath) new Blockchain({db: db}).iterator('i', (block, reorg, cb) => { const blockNumber = utils.bufferToInt(block.header.number)const blockHash = block.hash().toString('hex') console.log(`BLOCK ${blockNumber}: ${blockHash}`) cb() }, (err) => console.log(err || 'Done.'))
WARNING: Since ethereumjs-blockchain
is also doing write operations on the DB for safety reasons only run this on a copy of your database, otherwise this might lead to a compromised DB state.
警告:當 ethereumjs-blockchain也在資料庫進行寫操作時,為了安全性,只在資料庫的副本上執行,否則將會導致資料狀態破壞
API
Blockchain
Implements functions for retrieving, manipulating and storing Ethereum's blockchain
實現檢索函式,操作並存儲以太坊區塊鏈
new Blockchain(opts)
Creates new Blockchain object
建立新的區塊鏈物件
opts.db
- Database to store blocks and metadata. Should be a levelup instance. 儲存區塊和元資料的資料庫 ,應該是levelup例項opts.validate
- this the flag to validate blocks (e.g. Proof-of-Work), latest HF rules supported:Constantinople
. 驗證區塊標記(比如工作量證明),最新硬分叉規則支援的是Constantinople
[DEPRECATION NOTE] The old separated DB constructor parameters opts.blockDB
and opts.detailsDB
from before the Geth DB-compatible v3.0.0
release are deprecated and continued usage is discouraged. When provided opts.blockDB
will be used as opts.db
and opts.detailsDB
is ignored. On the storage level the DB formats are not compatible and it is not possible to load an old-format DB state into a post-v3.0.0
Blockchain
object.
棄用宣告:來自Geth的相容資料庫的v3.0.0
釋出版本之前的舊的單獨資料庫建構函式引數opts.blockDB
和
opts.detailsDB已經被棄用,不鼓勵繼續使用它。
當提供
opts.blockDB
時將將其作為opts.db
的值,提供的opts.detailsDB
將被忽略。資料庫格式在儲存級別上沒有被相容,所以下載一箇舊格式資料狀態到釋出的v3.0.0
版本的區塊鏈物件中是不可能做到的。
BlockChain
methods
blockchain.putGenesis(genesis, cb)
Puts the genesis block in the database.
將初始區塊放到資料庫中
genesis
- the genesis block to be added 新增的初始區塊cb
- the callback. It is given two parameterserr
and the savedblock 回撥函式。給定兩個引數err和儲存的區塊
blockchain.getHead(name, cb)
Returns the specified iterator head.
返回指定迭代區塊頭
name
- Optional name of the state root head (default: 'vm') 狀態根頭的可選名字cb
- the callback. It is given two parameterserr
and the returnedblock
回撥函式。給定兩個引數err和
返回的區塊
blockchain.getLatestHeader(cb)
Returns the latest header in the canonical chain.
返回最新的區塊頭
cb
- the callback. It is given two parameterserr
and the returnedheader
區塊頭回撥函式。給定兩個引數err和
blockchain.getLatestBlock(cb)
Returns the latest full block in the canonical chain.
返回規範鏈上最新的整個區塊
cb
- the callback. It is given two parameterserr
and the returnedblock
返回區塊回撥函式。給定兩個引數err和
blockchain.putBlocks(blocks, cb)
Adds many blocks to the blockchain.
新增多個區塊到區塊鏈上
blocks
- the blocks to be added to the blockchain 被新增到區塊鏈上的區塊cb
- the callback. It is given two parameterserr
and the last of the savedblocks
回撥函式。給定兩個引數err和最後被新增的區塊
blockchain.putBlock(block, cb)
Adds a block to the blockchain.
新增一個區塊到區塊鏈中
block
- the block to be added to the blockchain 被新增到區塊鏈上的區塊cb
- the callback. It is given two parameterserr
and the savedblock
儲存的區塊回撥函式。給定兩個引數err和
blockchain.getBlock(blockTag, cb)
Gets a block by its blockTag.
通過區塊標籤得到區塊
blockTag
- the block's hash or number區塊的hash值或號數cb
- the callback. It is given two parameterserr
and the foundblock
(an instance of https://github.com/ethereumjs/ethereumjs-block) if any.回撥函式。給定兩個引數err和如果有則返回的區塊
blockchain.getBlocks(blockId, maxBlocks, skip, reverse, cb)
Looks up many blocks relative to blockId.
找到許多相關區塊Id的區塊
blockId
- the block's hash or number 區塊的hash值或號數maxBlocks
- max number of blocks to return 返回的區塊的最大號數skip
- number of blocks to skip 跳過區塊的號數(即不想要的)reverse
- fetch blocks in reverse 反向取資料塊cb
- the callback. It is given two parameterserr
and the foundblocks
if any.
如果有才返回的區塊回撥函式。給定兩個引數err和
blockchain.putHeaders(headers, cb)
Adds many headers to the blockchain.
新增多個區塊頭到區塊鏈上
headers
- the headers to be added to the blockchain 要新增到區塊鏈上的區塊頭cb
- the callback. It is given two parameterserr
and the last of the savedheaders
最後儲存的區塊頭回撥函式。給定兩個引數err和
blockchain.putHeader(header, cb)
Adds a header to the blockchain.
新增一個區塊頭到區塊鏈中
header
- the header to be added to the blockchain 要新增到區塊鏈上的區塊頭cb
- the callback. It is given two parameterserr
and the savedheader
回撥函式。給定兩個引數err和儲存的區塊頭
blockchain.getDetails(hash, cb)
[DEPRECATED] Returns an empty object 棄用的函式,返回空物件
blockchain.selectNeededHashes(hashes, cb)
Given an ordered array, returns to the callback an array of hashes that are not in the blockchain yet.
給定排序陣列,返回給回撥函式一個還沒有在區塊鏈中的hash陣列
hashes
- Ordered array of hashes 排序hash陣列cb
- the callback. It is given two parameterserr
and hashes found.
找到的hash陣列回撥函式。給定兩個引數err和
blockchain.delBlock(blockHash, cb)
Deletes a block from the blockchain. All child blocks in the chain are deleted and any encountered heads are set to the parent block從區塊鏈中刪除區塊。所有在鏈上的該指定區及其子區塊都被刪除,並將任何遇見的區塊的頭設定為父區塊
blockHash
- the hash of the block to be deleted 要被刪除的區塊cb
- A callback. 回撥函式
blockchain.iterator(name, onBlock, cb)
Iterates through blocks starting at the specified verified state root head and calls the onBlock function on each block
通過區塊從指定的核查狀態根頭開始迭代,然後在每個區塊上呼叫onBlock函式
name
- name of the state root head 狀態根頭的名字onBlock
- function called on each block with params (block, reorg, cb) 在每個區塊上呼叫的帶有引數(block, reorg, cb)的onBlock韓素好cb
- A callback function 回撥函式
TESTS測試
Tests can be found in the test
directory and run with npm run test
.
在test目錄中能夠找到測試程式碼,使用
npm run test命令執行它
These can also be valuable as examples/inspiration on how to run the library and invoke different parts of the API.
對於如何執行庫和呼叫API的不同部分,這些示例/啟發也很有價值。
有關測試的內容看本部落格ethereumjs/ethereumjs-blockchain-2-test