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

鲍飞星
2023-12-01

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.blockDBopts.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 parameters err and the saved block 回调函数。给定两个参数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 parameters err and the returned block  回调函数。给定两个参数err和返回的区块

blockchain.getLatestHeader(cb)

Returns the latest header in the canonical chain.

返回最新的区块头

  • cb - the callback. It is given two parameters err and the returned header回调函数。给定两个参数err和区块头

blockchain.getLatestBlock(cb)

Returns the latest full block in the canonical chain.

返回规范链上最新的整个区块

  • cb - the callback. It is given two parameters err and the returned block回调函数。给定两个参数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 parameters err and the last of the saved blocks 回调函数。给定两个参数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 parameters err and the saved block  回调函数。给定两个参数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 parameters err and the found block (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 parameters err and the found blocks 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 parameters err and the last of the saved headers回调函数。给定两个参数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 parameters err and the saved header回调函数。给定两个参数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 parameters err and hashes found. 回调函数。给定两个参数err和找到的hash数组

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

转载于:https://www.cnblogs.com/wanghui-garcia/p/10089781.html

 类似资料: