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(即整个区块链上所有交易收据的信息)的信息

 

 
 

ethereumjs/merkle-patricia-tree-1-简介的更多相关文章

  1. Merkle Patricia Tree (MPT) 树详解

    1.    介绍 Merkle Patricia Tree(简称MPT树,实际上是一种trie前缀树)是以太坊中的一种加密认证的数据结构,可以用来存储所有的(key,value)对.以太坊区块的头部包 ...

  2. 014-数据结构-树形结构-基数树、Patricia树、默克尔树、梅克尔帕特里夏树( Merkle Patricia Tree, MPT)

    一.基数树 Radix树,即基数树,也称压缩前缀树,是一种提供key-value存储查找的数据结构.与Trie不同的是,它对Trie树进行了空间优化,只有一个子节点的中间节点将被压缩.同样的,Radi ...

  3. Merkle Patricia Tree (MPT) 以太坊中的默克尔树

    本篇博文是自己学习mpt的过程,边学边记录,很多原理性内容非自己原创,好的博文将会以链接形式进行共享. 一.什么是mpt MPT是以太坊中的merkle改进树,基于基数树,即前缀树改进而来,大大提高了 ...

  4. 左偏树(Leftist Heap/Tree)简介及代码

    左偏树是一种常用的优先队列(堆)结构.与二叉堆相比,左偏树可以高效的实现两个堆的合并操作. 左偏树实现方便,编程复杂度低,而且有着不俗的效率表现. 它的一个常见应用就是与并查集结合使用.利用并查集确定 ...

  5. Merkle Tree学习

    /*最近在看Ethereum,其中一个重要的概念是Merkle Tree,以前从来没有听说过,所以查了些资料,学习了Merkle Tree的知识,因为接触时间不长,对Merkle Tree的理解也不是 ...

  6. Merkle Tree 概念

    Merkle Tree 概念 来源 https://www.cnblogs.com/fengzhiwu/p/5524324.html /*最近在看Ethereum,其中一个重要的概念是Merkle T ...

  7. Merkle Tree(默克尔树)算法解析

    Merkle Tree概念 Merkle Tree,通常也被称作Hash Tree,顾名思义,就是存储hash值的一棵树.Merkle树的叶子是数据块(例如,文件或者文件的集合)的hash值.非叶节点 ...

  8. 转 Merkle Tree(默克尔树)算法解析

    Merkle Tree概念  Merkle Tree,通常也被称作Hash Tree,顾名思义,就是存储hash值的一棵树.Merkle树的叶子是数据块(例如,文件或者文件的集合)的hash值.非叶节 ...

  9. 区块链~Merkle Tree(默克尔树)算法解析~转载

    转载~Merkle Tree(默克尔树)算法解析 /*最近在看Ethereum,其中一个重要的概念是Merkle Tree,以前从来没有听说过,所以查了些资料,学习了Merkle Tree的知识,因为 ...

  10. ethereumjs/ethereumjs-block-2-api

    https://github.com/ethereumjs/ethereumjs-block/blob/master/docs/index.md 详细的调用代码可见本博客的ethereumjs/eth ...

随机推荐

  1. ROS:消息发布器和订阅器(c++)

    学习资料主要源自http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29 $ roscd beginner_t ...

  2. Spring 创建 IOC 容器时加载配置文件的几种方式

    一.ClassPathXmlApplicationContext 类路径加载 1. 使用 classpath 路径,classpath 前缀加不加都可以. ApplicationContext act ...

  3. shell文本操作

    一.find查找命令的使用 1.find . -name "*.txt" 在当前目录下,查找以txt结尾的文件 2.find . -name "[a-z]" 在 ...

  4. java语言的各种输入情况(ACM常用)

    1.只输入一组数据: Scanner s=new Scanner(System.in); int a=s.nextInt(); int b=s.nextInt(); 2.输入有多组数据,没有说明输入几 ...

  5. Javascript获取For循环所用时间

    第一种: let tOne = new Date().getTime(); let n = new Date(); let hour = n.getHours() < 10 ? "0& ...

  6. HTML表单相关

    表单:<input type="text" name="" value="" size="显示字符数" maxle ...

  7. LeetCode赛题393----UTF-8 Validation

    393. UTF-8 Validation A character in UTF8 can be from 1 to 4 bytes long, subjected to the following ...

  8. 五、python小功能记录——打包程序

    使用pyinstaller打包Python程序 安装工具 :pip3 install pyinstaller 在Python程序文件夹上(不点进去)按住shift并且右键,在弹出的选项中点击" ...

  9. Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.

    System.Data.OracleClient 已经过时了.微软不再支持它. 因此,我建议你为. NET 使用Oracle数据提供程序:ODP.Net. 你可以从以下位置下载: 版本:Release ...

  10. CopyrightHelper—开源VS插件辅助插入版权注释

    前言 有很多时候,我们在写代码的时候需要在代码文件头加上描述和版权信息等,如果使用代码项目模板又得为每种文件定模板,而已不方便,如果从某个地方复制过来,又嫌麻烦... 为了能解决这种懒人的需求,我开始 ...