Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
testdb
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[![NPM Package](https://img.shields.io/npm/v/dfinity-radix-tree.svg?style=flat-square)](https://www.npmjs.org/package/dfinity-radix-tree)
[![Build Status](https://img.shields.io/travis/dfinity/js-dfinity-radix-tree.svg?branch=master&style=flat-square)](https://travis-ci.org/dfinity/js-dfinity-radix-tree)
[![Coverage Status](https://img.shields.io/coveralls/dfinity/js-dfinity-radix-tree.svg?style=flat-square)](https://coveralls.io/dfinity/js-dfinity-radix-tree)
[![Coverage Status](https://img.shields.io/coveralls/dfinity/js-dfinity-radix-tree.svg?style=flat-square)](https://coveralls.io/dfinity/js-dfinity-radix-tree)

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

# Synopsis

:evergreen_tree: This implements a binary merkle radix tree. The point of using a binary radix
tree is that it generates smaller proof size then trees with larger radixes.
This tree is well suited for storing large dictonaries of fairly random keys.
And is optimized for storing keys of the same length. If the keys are not
random better performance can be achived by hashing them first. It builds on
This tree is well suited for storing large dictionaries of fairly random keys.
And is optimized for storing keys of the same length. If the keys are not
random better performance can be archived by hashing them first. It builds on
top of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder)
and the resulting state and proofs are generated using it.

Expand Down Expand Up @@ -52,18 +52,18 @@ main()
## API
['./docs/'](./docs/index.md)

## Spefication
## Specification
['./docs/spec.md'](./docs/spec.md)

## Benchmarks
The result of the benchmarks show that the binary radix tree produces proofs on
average %67 small then the Ethereum Trie with 100000 keys stored.
average 67% small then the Ethereum Trie with 100000 keys stored.

['./benchmarks/benchmarks.md'](./benchmark/results.md)

## License

[**(C) 2017 DFINITY STIFTUNG**](http://dfinity.network)
[**(C) 2018 DFINITY STIFTUNG**](http://dfinity.network)

All code and designs are open sourced under GPL V3.

Expand Down
26 changes: 13 additions & 13 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@

## constructor

[index.js:17-25](https://github.com/dfinity/js-dfinity-radix-tree/blob/d8e138d6c098bb9fe80d40c19b9987c94516c5d0/index.js#L17-L25 "Source code on GitHub")
[index.js:17-25](https://github.com/dfinity/js-dfinity-radix-tree/blob/3228beb1ebc4a9921b555fd514ede84f91ce5bf9/index.js#L17-L25 "Source code on GitHub")

**Parameters**

- `opts`
- `opts.root` {object} a merkle root to a radix tree. If none, RadixTree will create an new root.
- `opts.db` {object} a level db instance alternitly `opts.graph` can be used
- `opts.graph` {object} an instance of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder) alternitvly `opts.dag` can be used
- `opts.db` {object} a level db instance; alternatively, `opts.graph` can be used
- `opts.graph` {object} an instance of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder); alternatively, `opts.dag` can be used
- `opts.dag` {object} an instance if [ipfs.dag](https://github.com/ipfs/js-ipfs#dag). If there is no `opts.graph` this will be used to create a new graph instance.

## get

[index.js:32-36](https://github.com/dfinity/js-dfinity-radix-tree/blob/d8e138d6c098bb9fe80d40c19b9987c94516c5d0/index.js#L32-L36 "Source code on GitHub")
[index.js:32-36](https://github.com/dfinity/js-dfinity-radix-tree/blob/3228beb1ebc4a9921b555fd514ede84f91ce5bf9/index.js#L32-L36 "Source code on GitHub")

gets a value given a key

Expand All @@ -38,7 +38,7 @@ Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/

## set

[index.js:86-89](https://github.com/dfinity/js-dfinity-radix-tree/blob/d8e138d6c098bb9fe80d40c19b9987c94516c5d0/index.js#L86-L89 "Source code on GitHub")
[index.js:87-90](https://github.com/dfinity/js-dfinity-radix-tree/blob/3228beb1ebc4a9921b555fd514ede84f91ce5bf9/index.js#L87-L90 "Source code on GitHub")

stores a value at a given key

Expand All @@ -51,7 +51,7 @@ Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/

## delete

[index.js:134-137](https://github.com/dfinity/js-dfinity-radix-tree/blob/d8e138d6c098bb9fe80d40c19b9987c94516c5d0/index.js#L134-L137 "Source code on GitHub")
[index.js:135-138](https://github.com/dfinity/js-dfinity-radix-tree/blob/3228beb1ebc4a9921b555fd514ede84f91ce5bf9/index.js#L135-L138 "Source code on GitHub")

deletes a value at a given key

Expand All @@ -63,37 +63,37 @@ Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/

## done

[index.js:196-202](https://github.com/dfinity/js-dfinity-radix-tree/blob/d8e138d6c098bb9fe80d40c19b9987c94516c5d0/index.js#L196-L202 "Source code on GitHub")
[index.js:197-203](https://github.com/dfinity/js-dfinity-radix-tree/blob/3228beb1ebc4a9921b555fd514ede84f91ce5bf9/index.js#L197-L203 "Source code on GitHub")

returns a promise that resolve when the tree is done with all of its writes

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**

## flush

[index.js:218-221](https://github.com/dfinity/js-dfinity-radix-tree/blob/d8e138d6c098bb9fe80d40c19b9987c94516c5d0/index.js#L218-L221 "Source code on GitHub")
[index.js:219-222](https://github.com/dfinity/js-dfinity-radix-tree/blob/3228beb1ebc4a9921b555fd514ede84f91ce5bf9/index.js#L219-L222 "Source code on GitHub")

creates a merkle root for the current tree and stores the data perstantly
creates a merkle root for the current tree and stores the data persistently

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**

## emptyTreeState

[index.js:247-249](https://github.com/dfinity/js-dfinity-radix-tree/blob/d8e138d6c098bb9fe80d40c19b9987c94516c5d0/index.js#L247-L249 "Source code on GitHub")
[index.js:248-250](https://github.com/dfinity/js-dfinity-radix-tree/blob/3228beb1ebc4a9921b555fd514ede84f91ce5bf9/index.js#L248-L250 "Source code on GitHub")

returns the state of an empty tree

## ArrayConstructor

[index.js:255-257](https://github.com/dfinity/js-dfinity-radix-tree/blob/d8e138d6c098bb9fe80d40c19b9987c94516c5d0/index.js#L255-L257 "Source code on GitHub")
[index.js:256-258](https://github.com/dfinity/js-dfinity-radix-tree/blob/3228beb1ebc4a9921b555fd514ede84f91ce5bf9/index.js#L256-L258 "Source code on GitHub")

returns an Uint1Array constructir which is used to repersent keys
returns an Uint1Array constructor which is used to represent keys

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**

## getMerkleLink

[index.js:264-266](https://github.com/dfinity/js-dfinity-radix-tree/blob/d8e138d6c098bb9fe80d40c19b9987c94516c5d0/index.js#L264-L266 "Source code on GitHub")
[index.js:265-267](https://github.com/dfinity/js-dfinity-radix-tree/blob/3228beb1ebc4a9921b555fd514ede84f91ce5bf9/index.js#L265-L267 "Source code on GitHub")

returns a merkle link for some given data

Expand Down
14 changes: 7 additions & 7 deletions docs/spec.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
This documnet provides the structure of the [Dfinity's Radix Tree.](https://ipfs.io/ipns/QmdJiuMWp2FxyaerfLrtdLF6Nr1EWpL7dPAxA9oKSPYYgV/wiki/Radix_tree.html)
The radix tree data structure stores the key-values; the tree is an instances of nodes that contains the value and the key is the path to the node in the tree. All values are encoded with [CBOR](http://cbor.io)
This document provides the structure of the [Dfinity Radix Tree.](https://ipfs.io/ipns/QmdJiuMWp2FxyaerfLrtdLF6Nr1EWpL7dPAxA9oKSPYYgV/wiki/Radix_tree.html)
The radix tree data structure stores the key-values; the tree is an instance of nodes that contains the value and the key is the path to the node in the tree. All values are encoded with [CBOR](http://cbor.io)

## Node
Each node has a type and contains at most four elements:
Each node has a type and contains at most four elements:
"extension", "left branch", "right branch" and "value".

```
node : = TYPE | EXTENSION | LBRANCH | RBRANCH | VALUE
```

### Type
The type field contains a byte. The first 4 bits are paded to zero while the Node is stored in the tree. These bits are reserved as insicators of type when sending the nodes to other clients which we will describe later. The last 4 bits are used to signify which elements a node contains. The bit field is defined a the following
The type field contains a byte. The first 4 bits are padded to zero while the Node is stored in the tree. These bits are reserved as indicators of type when sending the nodes to other clients which we will describe later. The last 4 bits are used to signify which elements a node contains. The bit field is defined a the following

```
Type := 0 | 0 | 0 | 0 | HasEXTENSION | HasLBRANCH | HasRBRANCH | HasVALUE
```

For example a node that contained a left branch and a value would have a prefix byte of 00000101 or 0x07

The full encoded node would then look something like. `0x07<20_bytes_for_lbranch><remaing_bytes_for_value>`
The full encoded node would then look something like. `0x07<20_bytes_for_lbranch><remaing_bytes_for_value>`


### Branches
Expand All @@ -43,8 +43,8 @@ For optimization, we use the Extension element that encodes shared paths in the
extension := Length | ExtensionValue

```
Where the length is the number of bits that extension repesents. This varuint32
encoded with leb128. And the extension is bit array padded with 0's to the nearst byte.
Where the length is the number of bits that extension represents. This varuint32
encoded with leb128. And the extension is bit array padded with 0's to the nearest byte.

For example if the binary keys [0, 0, 1, 1] and
[0, 0, 1, 0] have a shared path of [0, 0, 1]. The extension node would therefor be
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = class RadixTree {
/**
* @param opts
* @param opts.root {object} a merkle root to a radix tree. If none, RadixTree will create an new root.
* @param opts.db {object} a level db instance alternitly `opts.graph` can be used
* @param opts.graph {object} an instance of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder) alternitvly `opts.dag` can be used
* @param opts.db {object} a level db instance; alternatively, `opts.graph` can be used
* @param opts.graph {object} an instance of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder); alternatively, `opts.dag` can be used
* @param opts.dag {object} an instance if [ipfs.dag](https://github.com/ipfs/js-ipfs#dag). If there is no `opts.graph` this will be used to create a new graph instance.
*/
constructor (opts) {
Expand Down Expand Up @@ -48,7 +48,7 @@ module.exports = class RadixTree {

const {extensionIndex, extensionLen, extension} = findMatchBits(subKey, root)
index += extensionIndex
// check if we compelete travered the extension
// check if we complete traversed the extension
if (extensionIndex !== extensionLen) {
return {index, root, extension, extensionIndex}
}
Expand All @@ -58,7 +58,7 @@ module.exports = class RadixTree {
if (keySegment !== undefined) {
const branch = treeNode.getBranch(root)
await this.graph.get(branch, keySegment, true)
// preseves the '/'
// preserves the '/'
const nextRoot = branch[keySegment]
if (!nextRoot) {
return {root, index}
Expand Down Expand Up @@ -213,7 +213,7 @@ module.exports = class RadixTree {
}

/**
* creates a merkle root for the current tree and stores the data perstantly
* creates a merkle root for the current tree and stores the data persistently
* @returns {Promise}
*/
async flush () {
Expand Down Expand Up @@ -250,7 +250,7 @@ module.exports = class RadixTree {
}

/**
* returns an Uint1Array constructir which is used to repersent keys
* returns an Uint1Array constructor which is used to represent keys
* @returns {object}
*/
static get ArrayConstructor () {
Expand Down