Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into promisify/proof
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Apr 1, 2020
2 parents 899cdd6 + 3b37de5 commit 7655bc8
Show file tree
Hide file tree
Showing 51 changed files with 3,495 additions and 1,504 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Build
on:
push:
branches:
- master
tags:
- '*'
pull_request:
types: [opened, reopened, synchronize]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v1
- uses: actions/checkout@v1
- run: npm install
- run: npm run lint

coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v1
- uses: actions/checkout@v1
- run: npm install
- run: npm run coverage
- name: Upload coverage to Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 13.x]
steps:
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- uses: actions/checkout@v1
- run: npm install
- run: npm run test
10 changes: 0 additions & 10 deletions .nycrc

This file was deleted.

36 changes: 0 additions & 36 deletions .travis.yml

This file was deleted.

33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# SYNOPSIS

[![NPM Package](https://img.shields.io/npm/v/merkle-patricia-tree.svg?style=flat-square)](https://www.npmjs.org/package/merkle-patricia-tree)
[![Build Status](https://img.shields.io/travis/ethereumjs/merkle-patricia-tree.svg?branch=master&style=flat-square)](https://travis-ci.org/ethereumjs/merkle-patricia-tree)
[![Coverage Status](https://img.shields.io/coveralls/ethereumjs/merkle-patricia-tree.svg?style=flat-square)](https://coveralls.io/r/ethereumjs/merkle-patricia-tree)
[![Gitter](https://img.shields.io/gitter/room/ethereum/ethereumjs-lib.svg?style=flat-square)](https://gitter.im/ethereum/ethereumjs-lib) or #ethereumjs on freenode
[![NPM Package](https://img.shields.io/npm/v/merkle-patricia-tree)](https://www.npmjs.org/package/merkle-patricia-tree)
[![Actions Status](https://github.com/ethereumjs/ethereumjs-util/workflows/Build/badge.svg)](https://github.com/ethereumjs/merkle-patricia-tree/actions)
[![Coverage Status](https://img.shields.io/coveralls/ethereumjs/merkle-patricia-tree.svg)](https://coveralls.io/r/ethereumjs/merkle-patricia-tree)
[![Gitter](https://img.shields.io/gitter/room/ethereum/ethereumjs.svg)](https://gitter.im/ethereum/ethereumjs)

[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

This is an implementation of the modified merkle patricia tree as specified in the [Ethereum's yellow paper](http://gavwood.com/Paper.pdf).
This is an implementation of the modified merkle patricia tree as specified in the [Ethereum's Yellow Paper](http://gavwood.com/Paper.pdf):

> 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 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.

Expand All @@ -30,8 +29,8 @@ const Trie = require('merkle-patricia-tree').BaseTrie,
db = level('./testdb'),
trie = new Trie(db)

trie.put('test', 'one', function() {
trie.get('test', function(err, value) {
trie.put(Buffer.from('test'), Buffer.from('one'), function () {
trie.get(Buffer.from('test'), function (err, value) {
if (value) console.log(value.toString())
})
})
Expand All @@ -40,9 +39,9 @@ trie.put('test', 'one', function() {
## Merkle Proofs

```javascript
Trie.prove(trie, 'test', function(err, prove) {
Trie.prove(trie, Buffer.from('test'), function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'test', prove, function(err, value) {
Trie.verifyProof(trie.root, Buffer.from('test'), prove, function (err, value) {
if (err) return cb(err)
console.log(value.toString())
cb()
Expand All @@ -63,10 +62,10 @@ var trie = new Trie(db, stateRoot)

trie
.createReadStream()
.on('data', function(data) {
.on('data', function (data) {
console.log(data)
})
.on('end', function() {
.on('end', function () {
console.log('End.')
})
```
Expand All @@ -89,7 +88,7 @@ var trie = new Trie(db, stateRoot)

var address = 'AN_ETHEREUM_ACCOUNT_ADDRESS'

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

var acc = new Account(data)
Expand All @@ -105,19 +104,19 @@ trie.get(address, function(err, data) {
console.log('------Storage------')
var stream = storageTrie.createReadStream()
stream
.on('data', function(data) {
.on('data', function (data) {
console.log(`key: ${ethutil.bufferToHex(data.key)}`)
console.log(`Value: ${ethutil.bufferToHex(rlp.decode(data.value))}`)
})
.on('end', function() {
.on('end', function () {
console.log('Finished reading storage.')
})
})
```

# API

[./docs/](./docs/index.md)
[Documentation](./docs/README.md)

# TESTING

Expand Down
52 changes: 0 additions & 52 deletions benchmarks/checkpointing.js

This file was deleted.

60 changes: 60 additions & 0 deletions benchmarks/checkpointing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as async from 'async'
import * as crypto from 'crypto'
const Trie = require('../dist/index.js').CheckpointTrie

let iterations = 500
let samples = 20
let i = 0

function iterTest(numOfIter: number, cb: Function) {
let vals = [] as any
let keys = [] as any

for (i = 0; i <= numOfIter; i++) {
vals.push(crypto.pseudoRandomBytes(32))
keys.push(crypto.pseudoRandomBytes(32))
}

let hrstart = process.hrtime()
let numOfOps = 0
let trie = new Trie()

for (i = 0; i < numOfIter; i++) {
trie.put(vals[i], keys[i], function () {
trie.checkpoint()
trie.get(Buffer.from('test'), function () {
numOfOps++
if (numOfOps === numOfIter) {
const hrend = process.hrtime(hrstart)
cb(hrend)
}
})
})
}
}

i = 0
let avg = [0, 0]

async.whilst(
function () {
i++
return i <= samples
},
function (done) {
iterTest(iterations, function (hrend: Array<number>) {
avg[0] += hrend[0]
avg[1] += hrend[1]

console.info('Execution time (hr): %ds %dms', hrend[0], hrend[1] / 1000000)
done()
})
},
function () {
console.info(
'Average Execution time (hr): %ds %dms',
avg[0] / samples,
avg[1] / 1000000 / samples,
)
},
)
2 changes: 2 additions & 0 deletions benchmarks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./checkpointing')
require('./random')
15 changes: 8 additions & 7 deletions benchmarks/random.js → benchmarks/random.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// https://github.com/ethereum/wiki/wiki/Benchmarks
'use strict'
const Trie = require('../')
const ethUtil = require('ethereumjs-util')
const async = require('async')
import * as async from 'async'
import * as ethUtil from 'ethereumjs-util'
const Trie = require('../dist/index.js').BaseTrie

const ROUNDS = 1000
const SYMMETRIC = true
const ERA_SIZE = 1000

let trie = new Trie()
let seed = new Buffer(32).fill(0)
let seed = Buffer.alloc(32).fill(0)

let testName = 'rounds ' + ROUNDS + ' ' + ERA_SIZE + ' ' + SYMMETRIC ? 'sys' : 'rand'
console.time(testName)
run(() => {
console.timeEnd(testName)
})

function run (cb) {
function run(cb: any) {
let i = 0
async.whilst(
() => {
Expand All @@ -33,12 +33,13 @@ function run (cb) {
trie.put(seed, val, genRoot)
}

function genRoot () {
function genRoot() {
if (i % ERA_SIZE === 0) {
seed = trie.root
}
done()
}
}, cb
},
cb,
)
}
11 changes: 11 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[merkle-patricia-tree](README.md)

# merkle-patricia-tree

## Index

### Modules

* ["baseTrie"](modules/_basetrie_.md)
* ["checkpointTrie"](modules/_checkpointtrie_.md)
* ["secure"](modules/_secure_.md)
Loading

0 comments on commit 7655bc8

Please sign in to comment.