All Projects → ethereumjs → Merkle Patricia Tree

ethereumjs / Merkle Patricia Tree

Project is in active development and has been moved to the EthereumJS VM monorepo.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Merkle Patricia Tree

Qp Trie Rs
An idiomatic and fast QP-trie implementation in pure Rust.
Stars: ✭ 47 (-83.03%)
Mutual labels:  data-structures, trie
Trienet
.NET Implementations of Trie Data Structures for Substring Search, Auto-completion and Intelli-sense. Includes: patricia trie, suffix trie and a trie implementation using Ukkonen's algorithm.
Stars: ✭ 122 (-55.96%)
Mutual labels:  data-structures, trie
Adaptive Radix Tree
A fast and space efficient Radix tree in Java
Stars: ✭ 76 (-72.56%)
Mutual labels:  data-structures, trie
Hat Trie
C++ implementation of a fast and memory efficient HAT-trie
Stars: ✭ 565 (+103.97%)
Mutual labels:  data-structures, trie
Data Structures
A collection of powerful data structures
Stars: ✭ 2,534 (+814.8%)
Mutual labels:  data-structures, trie
Trie
A Mixed Trie and Levenshtein distance implementation in Java for extremely fast prefix string searching and string similarity.
Stars: ✭ 25 (-90.97%)
Mutual labels:  data-structures, trie
Data Structures
Data-Structures using C++.
Stars: ✭ 121 (-56.32%)
Mutual labels:  data-structures, trie
Merkle.rs
🎄 Merkle tree in Rust
Stars: ✭ 98 (-64.62%)
Mutual labels:  data-structures, merkle-tree
Opends
Template Library of Data Structures in C++17
Stars: ✭ 151 (-45.49%)
Mutual labels:  data-structures, trie
Algorithms
A collection of common algorithms and data structures implemented in java, c++, and python.
Stars: ✭ 142 (-48.74%)
Mutual labels:  data-structures, trie
Data Structures
Go datastructures.
Stars: ✭ 336 (+21.3%)
Mutual labels:  data-structures, trie
Merkletreejs
🌱 Construct Merkle Trees and verify proofs in JavaScript.
Stars: ✭ 238 (-14.08%)
Mutual labels:  ethereum, merkle-tree
Hashapi Lib Node
Tierion Hash API client library for Node.js
Stars: ✭ 20 (-92.78%)
Mutual labels:  merkle-tree, proof
Libgenerics
libgenerics is a minimalistic and generic library for C basic data structures.
Stars: ✭ 42 (-84.84%)
Mutual labels:  data-structures, trie
Merkle Tree
Merkle Trees and Merkle Inclusion Proofs
Stars: ✭ 130 (-53.07%)
Mutual labels:  data-structures, merkle-tree
Merkle Tree Solidity
JS - Solidity sha3 merkle tree bridge. Generate proofs in JS; verify in Solidity.
Stars: ✭ 94 (-66.06%)
Mutual labels:  ethereum, merkle-tree
proofable-image
Build trust into your image by creating a blockchain certificate for it
Stars: ✭ 17 (-93.86%)
Mutual labels:  proof, merkle-tree
Nanominer
Nanominer is a versatile tool for mining cryptocurrencies on GPUs and CPUs.
Stars: ✭ 263 (-5.05%)
Mutual labels:  ethereum
Php
All Algorithms implemented in Php
Stars: ✭ 272 (-1.81%)
Mutual labels:  data-structures
Notes
算法刷题指南、Java多线程与高并发、Java集合源码、Spring boot、Spring Cloud等笔记,源码级学习笔记后续也会更新。
Stars: ✭ 256 (-7.58%)
Mutual labels:  data-structures

SYNOPSIS

NPM Status Actions Status Coverage Status Discord

This is an implementation of the modified merkle patricia tree as specified in the Ethereum 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.

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

INSTALL

npm install merkle-patricia-tree

USAGE

There are 3 variants of the tree implemented in this library, namely: BaseTrie, CheckpointTrie and SecureTrie. CheckpointTrie adds checkpointing functionality to the BaseTrie with the methods checkpoint, commit and revert. SecureTrie extends CheckpointTrie and is the most suitable variant for Ethereum applications. It stores values under the keccak256 hash of their keys.

Initialization and Basic Usage

import level from 'level'
import { BaseTrie as Trie } from 'merkle-patricia-tree'

const db = level('./testdb')
const trie = new Trie(db)

async function test() {
  await trie.put(Buffer.from('test'), Buffer.from('one'))
  const value = await trie.get(Buffer.from('test'))
  console.log(value.toString()) // 'one'
}

test()

Merkle Proofs

const trie = new Trie()

async function test() {
  await trie.put(Buffer.from('test'), Buffer.from('one'))
  const proof = await Trie.createProof(trie, Buffer.from('test'))
  const value = await Trie.verifyProof(trie.root, Buffer.from('test'), proof)
  console.log(value.toString()) // 'one'
}

test()

Read stream on Geth DB

import level from 'level'
import { SecureTrie as Trie } from 'merkle-patricia-tree'

const db = level('YOUR_PATH_TO_THE_GETH_CHAIN_DB')
// Set stateRoot to block #222
const stateRoot = '0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544'
// Initialize trie
const trie = new Trie(db, stateRoot)

trie
  .createReadStream()
  .on('data', console.log)
  .on('end', () => {
    console.log('End.')
  })

Read Account State including Storage from Geth DB

import level from 'level'
import rlp from 'rlp'
import { BN, bufferToHex } from 'ethereumjs-util'
import Account from 'ethereumjs-account'
import { SecureTrie as Trie } from 'merkle-patricia-tree'

const stateRoot = 'STATE_ROOT_OF_A_BLOCK'

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

const address = 'AN_ETHEREUM_ACCOUNT_ADDRESS'

async function test() {
  const data = await trie.get(address)
  const 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: ${bufferToHex(acc.stateRoot)}`)
  console.log(`codeHash: ${bufferToHex(acc.codeHash)}`)

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

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

test()

Additional examples with detailed explanations are available here.

API

Documentation

TESTING

npm test

BENCHMARKS

There are two simple benchmarks in the benchmarks folder:

  • random.ts runs random PUT operations on the tree.
  • checkpointing.ts runs checkpoints and commits between PUT operations.

A third benchmark using mainnet data to simulate real load is also under consideration.

Benchmarks can be run with:

npm run benchmarks

To run a profiler on the random.ts benchmark and generate a flamegraph with 0x you can use:

npm run profiling

0x processes the stacks and generates a profile folder (<pid>.0x) containing flamegraph.html.

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].