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. 二、hbase shell工具

    hbase单节点安装请参考: https://www.cnblogs.com/lay2017/p/9944387.html 下文演示hbase shell工具常用的命令,首先启动hbase以及进入sh ...

  2. Q:关于栈的常见问题

     对于栈,一个常见的问题是:给定一个序列a0,a1,a2,a3...an依次顺序入栈,在元素顺序入栈的过程中,栈中任意一个元素可以选择是否出栈,则其共有几种出栈的可能,给定的出栈序列中,哪种是不可能的 ...

  3. K:单例模式中存在的问题

      对于单例模式的实现,无论其是否具有懒加载的功能,我们的目标是有且仅生成一个对象.但是,实际上,对于单例模式的一般实现,都会存在着以下的两个问题: 序列化攻击: 对于枚举方式实现的单例模式,并不存在 ...

  4. Web安全相关(三):开放重定向(Open Redirection)

    简介 那些通过请求(如查询字符串和表单数据)指定重定向URL的Web程序可能会被篡改,而把用户重定向到外部的恶意URL.这种篡改就被称为开发重定向攻击. 场景分析 假设有一个正规网站http://ne ...

  5. 洛谷P4781 【模板】拉格朗日插值(拉格朗日插值)

    题意 题目链接 Sol 记得NJU有个特别强的ACM队叫拉格朗,总感觉少了什么.. 不说了直接扔公式 \[f(x) = \sum_{i = 1}^n y_i \prod_{j \not = i} \f ...

  6. 【数据库】10.0 MySQL常用语句(一)

    显示数据库语句: SHOW DATABASES    只是显示数据库的名字 显示数据库创建语句: SHOW CREATE DATABASE db_name 数据库删除语句: DROP DATABASE ...

  7. js原生日历

    突然发现日期对象可以进行 加减 , 利用这个特性写了一个可以说是对只要会JavaScript  的就可以写的日历:没有各种算法,只有一些逻辑相信只要懂javascript就差不多看俩眼就会的日历. & ...

  8. 使用PuTTy在CentOS下安装web.py与简单的文件传输

    两周前,出于帮朋友忙的目的,尝试了一下微信公众号的菜单自定义与自动回复功能的实现,成了. 两周后,需要将代码转移至朋友新购的服务器上,发现基本操作全忘记了,麻瓜!所以记一笔,希望也能对大家也有帮助. ...

  9. Java学习笔记(4)----Public,Protected,Package,Private修饰符可见性

    Java修饰符类型(public,protected,private,friendly) public的类.类属变量及方法,包内及包外的任何类均可以访问:protected的类.类属变量及方法,包内的 ...

  10. bootstrap 的模态框的宽与高设置

    1,改变bootstrap 的宽与高, 将style=“height:900px”放在<div class = "modal-dialog">或者更外层上,整个模态框的 ...