https://github.com/ethereumjs/merkle-patricia-tree

SYNOPSIS概要

This is an implementation of the modified merkle patricia tree as specified in the Ethereum's yellow paper.

这是以太坊黄皮书上指定的改良后的merkle patricia树的实现。

The modified Merkle Patricia tree (trie) provides a persistent data structure to map between arbitrary-length binary data (byte arrays). It is defined in terms of a mutable data structure to map between 256-bit binary fragments and arbitrary-length binary data. The core of the trie, and its sole requirement in terms of the protocol specification is to provide a single 32-byte value that identifies a given set of key-value pairs.
- Ethereum's yellow paper

修改后的Merkle Patricia树(前缀树)提供了一个持久的数据结构来映射任意长度的二进制数据(字节数组)。它是根据可变数据结构定义的,以映射256位二进制片段和任意长度的二进制数据。前缀树的核心和协议规范方面的惟一要求是提供一个32字节的值,该值标识给定的一组键-值对。

The only backing store supported is LevelDB through the levelup module.

支持的唯一备份存储是通过levelup模块使用的LevelDB

INSTALL安装

npm install merkle-patricia-tree

USAGE使用

Initialization and Basic Usage初始化和基本使用

var Trie = require('merkle-patricia-tree'),
level = require('level'),
db = level('./testdb'),
trie = new Trie(db); trie.put('test', 'one', function () {
trie.get('test', function (err, value) {
if(value) console.log(value.toString())
});
});

Merkle Proofs Merkle证明

Trie.prove(trie, 'test', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'test', prove, function (err, value) {
if (err) return cb(err)
console.log(value.toString())
cb()
})
})

Read stream on Geth DB 读取Geth数据库中的流

var level = require('level')
var Trie = require('./secure') var stateRoot = "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544" // Block #222 var db = level('YOUR_PATH_TO_THE_GETH_CHAIN_DB')
var trie = new Trie(db, stateRoot) trie.createReadStream()
.on('data', function (data) {
console.log(data)
})
.on('end', function() {
console.log('End.')
})

API

看本博客的ethereumjs/merkle-patricia-tree-2-API

TESTING

npm test

在区块链上使用Merkle-Patricia树存储了四类数据:一个是某个账户中的账户状态的stateRoot值,其key = account.serialize(),value = account state;以及三个前缀树root存储在区块头上的数据:区块链中的state(即整个区块链上所有账户的信息,key = address ,value = account.serialize());transaction(即整个区块链上所有交易的信息);transactionReceipt(即整个区块链上所有交易收据的信息)的信息

 

 
 

最新文章

  1. JS的Object漫想:从现象到“本质”
  2. BLE教程 - 官方tutorial翻译
  3. GIT文件的三种状态
  4. IP 协议首部格式与其配套使用的四个协议(ARP,RARP,ICMP,IGMP)
  5. POJ2289-Jamie's Contact Groups-二分图多重匹配-ISAP
  6. kappa 一致性系数计算实例
  7. OC语言-02面向对象的三大特性
  8. 模拟表格 inline-block等高布局
  9. NOI2014 Day2
  10. linux expect, spawn用法小记
  11. 【Unity3D】Unity3D开发《我的世界》之四、创建一个Block
  12. C语言——第二次作业
  13. Python之celery的简介与使用
  14. 锋利的jquery 事件 动画
  15. HTML与CSS的一些知识(二)
  16. centos7的Kubernetes部署记录
  17. 使用thumbnailator不按照比例,改变图片的大小
  18. form表单提交,Servlet接收并读取Excel文件
  19. easyui-combobox url绑定后台json数据问题
  20. HTTP协议教程

热门文章

  1. ASP.NET之Jquery入门级别
  2. SqlServer之一些小问题
  3. MyEclipse中设置开发项目时使用的默认JDK
  4. HTML5之新增的元素和废除的元素 (声明:内容节选自《HTML 5从入门到精通》)
  5. 理解webpack4.splitChunks之maxAsyncRequests
  6. ajax异步上传图片三种方案
  7. GDAL2.0编译——32位和64位
  8. Codeforces Round #419 A+B
  9. 从零开始安装、编译、部署 Docker
  10. android热加载随记