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

ethereumjs/ethereumjs-blockchain-1-简介和API的更多相关文章

  1. IO流 简介 总结 API 案例 MD

    目录 IO 流 简介 关闭流的正确方式 关闭流的封装方法 InputStream 转 String 的方式 转换流 InputStreamReader OutputStreamWriter 测试代码 ...

  2. Servlet基础(一) Servlet简介 关键API介绍及结合源码讲解

    Servlet基础(一) Servlet基础和关键的API介绍 Servlet简介 Java Servlet是和平台无关的服务器端组件,它运行在Servlet容器中. Servlet容器负责Servl ...

  3. Sentinel 简介与API订阅发布

    Sentinel 简介 Redis 的 Sentinel 系统用于管理多个 redis 服务器(instance), 该系统执行以下三个任务: 监控(Monitoring): Sentinel 会不断 ...

  4. API Monitor简介(API监控工具)

    API Monitor是一个免费软件,可以让你监视和控制应用程序和服务,取得了API调用. 它是一个强大的工具,看到的应用程序和服务是如何工作的,或跟踪,你在自己的应用程序的问题. 64位支持 API ...

  5. JavaMail入门第一篇 邮件简介及API概述

    现如今,电子邮件在我们的生活当中扮演着越来越重要的角色,我们每个人几乎都会与其打交道(至少时不时我们都会接收到莫名其妙的垃圾邮件),在工作中,使用邮件进行交流沟通,可以使我们的工作有迹可循,也显的较为 ...

  6. Linux PWM framework简介和API描述【转】

    本文转载自:https://blog.csdn.net/mike8825/article/details/51656400 1. 前言 PWM是Pulse Width Modulation(脉冲宽度调 ...

  7. SQLite3 C/C++ 开发接口简介(API函数)

    from : http://www.sqlite.com.cn/MySqlite/5/251.Html 1.0 总览 SQLite3是SQLite一个全新的版本,它虽然是在SQLite 2.8.13的 ...

  8. ZooKeeper系列4:ZooKeeper API简介及编程

    问题导读: 1.ZooKeeper API 共包含几个包? 2.如何使用ZooKeeper API 创建zookeeper应用程序? 1)ZooKeeper API 简介   ZooKeeper AP ...

  9. ZABBIX API简介及使用

    API简介 Zabbix API开始扮演着越来越重要的角色,尤其是在集成第三方软件和自动化日常任务时.很难想象管理数千台服务器而没有自动化是多么的困难.Zabbix API为批量操作.第三方软件集成以 ...

  10. [译]Quartz 框架 教程(中文版)2.2.x 之第二课 Quartz API,Jobs和Triggers简介

    第二课:QuartzAPI,Jobs和Triggers简介 Quartz API Quartz API 关键的几个接口: Scheduler:跟任务调度相关的最主要的API接口. Job:你期望任务调 ...

随机推荐

  1. Cheatsheet: 2018 05.01 ~ 07.31

    JAVA Java Tips: Creating a Monitoring-Friendly ExecutorService Other Modeling the Card Game War in C ...

  2. linux-ubuntu安装配置uwsgi

    参考原文 对于 Python2.x 版本:(测试通过) 第一步:sudo apt-get install python-dev 第二步:sudo apt-get install python-pip  ...

  3. C#解析PDF

    C#解析PDF的方式有很多,比较好用的有ITestSharp和PdfBox. PDF内容页如果是图片类型,例如扫描件,则需要进行OCR(光学字符识别). 文本内容的PDF文档,解析的过程中,我目前仅发 ...

  4. SQL导出到Excel 存储过程

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_exporttb]') and OBJECTPROPER ...

  5. python学习之老男孩python全栈第九期_day022作业

    1. 写一个求正方形周长和面积的类 class Square: def __init__(self, length): self.length = length def area(self): ret ...

  6. 浅谈arguments与arguments的妙用

    1.每个函数都有一个arguments属性,表示函数的实参集合,这里的实参是重点,就是执行函数时实际传入的参数的集合. 2.arguments不是数组而是一个对象,但它和数组很相似,所以通常称为类数组 ...

  7. BZOJ3600:没有人的算术

    传送门 如果能给每个 \(pair\) 按照权值编号就好了 假设之前已经有了所有的权值的编号,现在考虑编号新的 \(pair\) 如果看过了陈立杰的论文的话,不难得到一个重量平衡树的做法 给树上每个子 ...

  8. BZOJ P2157 旅游

    题目大意: 维护一棵树,每条边有边权,支持下列操作:1.修改某条边的边权2.将某条路经上的边权取反3.询问某条路经上的和4.询问某条路经上的最大值5.询问某条路经上的最小值 --by BZOJ; ht ...

  9. 1739 GPA排序 个人博客:doubleq.win

    个人博客:doubleq.win 1739 GPA排序  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解       题目描述 Description ...

  10. bootstrap学习笔记细化(标题)

    bootstrap中的排版: 标题(h1~h6/.h1~.h6) h1:36px;h2:30px;h3:24px;h4:18px;h5:14px;h6:12px; 副标题(small) 小练习(标题大 ...