Skip to content

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

Notifications You must be signed in to change notification settings

didliedo/merkle-patricia-tree

 
 

Repository files navigation

SYNOPSIS

NPM Package Build Status Coverage Status Gitter or #ethereumjs on freenode

js-standard-style

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

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

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

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

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

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.')
  })

Read Account State including Storage from Geth DB

var level = require('level')
var rlp = require('rlp')
var ethutil = require('ethereumjs-util')

var Trie = require('merkle-patricia-tree/secure')
var Account = require('ethereumjs-account').default
var BN = ethutil.BN

var stateRoot = 'STATE_ROOT_OF_A_BLOCK'

var db = level('YOUR_PATH_TO_THE_GETH_CHAINDATA_FOLDER')
var trie = new Trie(db, stateRoot)

var address = 'AN_ETHEREUM_ACCOUNT_ADDRESS'

trie.get(address, function (err, data) {
  if (err) return cb(err)

  var acc = new Account(data)
  console.log('-------State-------')
  console.log(`nonce: ${new BN(acc.nonce)}`)
  console.log(`balance in wei: ${new BN(acc.balance)}`)
  console.log(`storageRoot: ${ethutil.bufferToHex(acc.stateRoot)}`)
  console.log(`codeHash: ${ethutil.bufferToHex(acc.codeHash)}`)

  var storageTrie = trie.copy()
  storageTrie.root = acc.stateRoot

  console.log('------Storage------')
  var stream = storageTrie.createReadStream()
  stream.on('data', function(data) {
    console.log(`key: ${ethutil.bufferToHex(data.key)}`)
    console.log(`Value: ${ethutil.bufferToHex(rlp.decode(data.value))}`)
  })
  .on('end', function() {
    console.log('Finished reading storage.')
  })
})

API

./docs/

TESTING

npm test

REFERENCES

EthereumJS

See our organizational documentation for an introduction to EthereumJS as well as information on current standards and best practices.

If you want to join for work or do improvements on the libraries have a look at our contribution guidelines.

LICENSE

MPL-2.0

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%