Commit 5b0dc0645be6db668260d4699035f42d5b880a01
Merge pull request #10 from dfinity/crypto
replace node-webcrypto-shimwanderer authored on 3/18/2018, 2:38:03 PM
GitHub committed on 3/18/2018, 2:38:03 PM
Parent: 8370f62963962f6038a5541f8b3fbc040babd4e7
Parent: dc4dcb9b9fa76eee440632309af8c4a27fd14f84
Files changed
README.md | changed |
datastore.js | changed |
docs/index.md | changed |
docs/spec.md | changed |
index.js | changed |
package-lock.json | changed |
package.json | changed |
.gitignore | added |
README.md | ||
---|---|---|
@@ -1,17 +1,17 @@ | ||
1 | 1 | [![NPM Package](https://img.shields.io/npm/v/dfinity-radix-tree.svg?style=flat-square)](https://www.npmjs.org/package/dfinity-radix-tree) |
2 | 2 | [![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) |
3 | -[![Coverage Status](https://img.shields.io/coveralls/dfinity/js-dfinity-radix-tree.svg?style=flat-square)](https://coveralls.io/dfinity/js-dfinity-radix-tree) | |
3 | +[![Coverage Status](https://img.shields.io/coveralls/dfinity/js-dfinity-radix-tree.svg?style=flat-square)](https://coveralls.io/dfinity/js-dfinity-radix-tree) | |
4 | 4 | |
5 | -[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | |
5 | +[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | |
6 | 6 | |
7 | 7 | # Synopsis |
8 | 8 | |
9 | 9 | :evergreen_tree: This implements a binary merkle radix tree. The point of using a binary radix |
10 | 10 | tree is that it generates smaller proof size then trees with larger radixes. |
11 | -This tree is well suited for storing large dictonaries of fairly random keys. | |
12 | -And is optimized for storing keys of the same length. If the keys are not | |
13 | -random better performance can be achived by hashing them first. It builds on | |
11 | +This tree is well suited for storing large dictionaries of fairly random keys. | |
12 | +And is optimized for storing keys of the same length. If the keys are not | |
13 | +random better performance can be archived by hashing them first. It builds on | |
14 | 14 | top of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder) |
15 | 15 | and the resulting state and proofs are generated using it. |
16 | 16 | |
17 | 17 | ## Install |
@@ -51,20 +51,20 @@ | ||
51 | 51 | ``` |
52 | 52 | ## API |
53 | 53 | ['./docs/'](./docs/index.md) |
54 | 54 | |
55 | -## Spefication | |
55 | +## Specification | |
56 | 56 | ['./docs/spec.md'](./docs/spec.md) |
57 | 57 | |
58 | 58 | ## Benchmarks |
59 | 59 | The result of the benchmarks show that the binary radix tree produces proofs on |
60 | -average %67 small then the Ethereum Trie with 100000 keys stored. | |
60 | +average 67% small then the Ethereum Trie with 100000 keys stored. | |
61 | 61 | |
62 | 62 | ['./benchmarks/benchmarks.md'](./benchmark/results.md) |
63 | 63 | |
64 | 64 | ## License |
65 | 65 | |
66 | -[**(C) 2017 DFINITY STIFTUNG**](http://dfinity.network) | |
66 | +[**(C) 2018 DFINITY STIFTUNG**](http://dfinity.network) | |
67 | 67 | |
68 | 68 | All code and designs are open sourced under GPL V3. |
69 | 69 | |
70 | 70 | ![image](https://user-images.githubusercontent.com/6457089/32753794-10f4cbc2-c883-11e7-8dcf-ff8088b38f9f.png) |
datastore.js | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | 1 | const Buffer = require('safe-buffer').Buffer |
2 | -const crypto = require('node-webcrypto-shim') | |
2 | +const crypto = require('crypto') | |
3 | 3 | const DAG = require('ipld-graph-builder/datastore.js') |
4 | 4 | const HASH_LEN = 20 |
5 | 5 | const cbor = require('borc') |
6 | 6 | |
@@ -34,9 +34,9 @@ | ||
34 | 34 | return Buffer.isBuffer(link) && link.length === HASH_LEN |
35 | 35 | } |
36 | 36 | |
37 | 37 | static getMerkleLink (buf) { |
38 | - return crypto.subtle.digest({ | |
39 | - name: 'SHA-256' | |
40 | - }, buf).then(link => Buffer.from(link.slice(0, HASH_LEN))) | |
38 | + const hash = crypto.createHash('sha256') | |
39 | + hash.update(buf) | |
40 | + return hash.digest().slice(0, HASH_LEN) | |
41 | 41 | } |
42 | 42 | } |
docs/index.md | ||
---|---|---|
@@ -1,150 +1,104 @@ | ||
1 | 1 | <!-- Generated by documentation.js. Update this documentation by updating the source code. --> |
2 | 2 | |
3 | 3 | ### Table of Contents |
4 | 4 | |
5 | -- [constructor][1] | |
6 | -- [get][2] | |
7 | -- [set][3] | |
8 | -- [delete][4] | |
9 | -- [done][5] | |
10 | -- [flush][6] | |
11 | -- [emptyTreeState][7] | |
12 | -- [ArrayConstructor][8] | |
13 | -- [getMerkleLink][9] | |
5 | +- [constructor](#constructor) | |
6 | +- [get](#get) | |
7 | +- [set](#set) | |
8 | +- [delete](#delete) | |
9 | +- [done](#done) | |
10 | +- [flush](#flush) | |
11 | +- [emptyTreeState](#emptytreestate) | |
12 | +- [ArrayConstructor](#arrayconstructor) | |
13 | +- [getMerkleLink](#getmerklelink) | |
14 | 14 | |
15 | 15 | ## constructor |
16 | 16 | |
17 | -[index.js:17-25][10] | |
17 | +[index.js:17-25](https://github.com/dfinity/js-dfinity-radix-tree/blob/806b9f80cab0da81d7e98a62754b80167fe58296/index.js#L17-L25 "Source code on GitHub") | |
18 | 18 | |
19 | 19 | **Parameters** |
20 | 20 | |
21 | 21 | - `opts` |
22 | 22 | - `opts.root` {object} a merkle root to a radix tree. If none, RadixTree will create an new root. |
23 | - - `opts.db` {object} a level db instance alternitly `opts.graph` can be used | |
24 | - - `opts.graph` {object} an instance of [ipld-graph-builder][11] alternitvly `opts.dag` can be used | |
25 | - - `opts.dag` {object} an instance if [ipfs.dag][12]. If there is no `opts.graph` this will be used to create a new graph instance. | |
23 | + - `opts.db` {object} a level db instance; alternatively, `opts.graph` can be used | |
24 | + - `opts.graph` {object} an instance of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder); alternatively, `opts.dag` can be used | |
25 | + - `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. | |
26 | 26 | |
27 | 27 | ## get |
28 | 28 | |
29 | -[index.js:32-36][13] | |
29 | +[index.js:32-36](https://github.com/dfinity/js-dfinity-radix-tree/blob/806b9f80cab0da81d7e98a62754b80167fe58296/index.js#L32-L36 "Source code on GitHub") | |
30 | 30 | |
31 | 31 | gets a value given a key |
32 | 32 | |
33 | 33 | **Parameters** |
34 | 34 | |
35 | 35 | - `key` **any** |
36 | 36 | |
37 | -Returns **[Promise][14]** | |
37 | +Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)** | |
38 | 38 | |
39 | 39 | ## set |
40 | 40 | |
41 | -[index.js:87-90][15] | |
41 | +[index.js:87-90](https://github.com/dfinity/js-dfinity-radix-tree/blob/806b9f80cab0da81d7e98a62754b80167fe58296/index.js#L87-L90 "Source code on GitHub") | |
42 | 42 | |
43 | 43 | stores a value at a given key returning the tree node that the value was saved in |
44 | 44 | |
45 | 45 | **Parameters** |
46 | 46 | |
47 | 47 | - `key` **any** |
48 | 48 | - `value` |
49 | 49 | |
50 | -Returns **[Promise][14]** | |
50 | +Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)** | |
51 | 51 | |
52 | 52 | ## delete |
53 | 53 | |
54 | -[index.js:139-142][16] | |
54 | +[index.js:139-142](https://github.com/dfinity/js-dfinity-radix-tree/blob/806b9f80cab0da81d7e98a62754b80167fe58296/index.js#L139-L142 "Source code on GitHub") | |
55 | 55 | |
56 | 56 | smContainer.js deletes a value at a given key |
57 | 57 | |
58 | 58 | **Parameters** |
59 | 59 | |
60 | 60 | - `key` **any** |
61 | 61 | |
62 | -Returns **[Promise][14]** | |
62 | +Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)** | |
63 | 63 | |
64 | 64 | ## done |
65 | 65 | |
66 | -[index.js:201-207][17] | |
66 | +[index.js:201-207](https://github.com/dfinity/js-dfinity-radix-tree/blob/806b9f80cab0da81d7e98a62754b80167fe58296/index.js#L201-L207 "Source code on GitHub") | |
67 | 67 | |
68 | 68 | returns a promise that resolve when the tree is done with all of its writes |
69 | 69 | |
70 | -Returns **[Promise][14]** | |
70 | +Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)** | |
71 | 71 | |
72 | 72 | ## flush |
73 | 73 | |
74 | -[index.js:223-226][18] | |
74 | +[index.js:223-226](https://github.com/dfinity/js-dfinity-radix-tree/blob/806b9f80cab0da81d7e98a62754b80167fe58296/index.js#L223-L226 "Source code on GitHub") | |
75 | 75 | |
76 | -creates a merkle root for the current tree and stores the data perstantly | |
76 | +creates a merkle root for the current tree and stores the data persistently | |
77 | 77 | |
78 | -Returns **[Promise][14]** | |
78 | +Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)** | |
79 | 79 | |
80 | 80 | ## emptyTreeState |
81 | 81 | |
82 | -[index.js:252-254][19] | |
82 | +[index.js:252-254](https://github.com/dfinity/js-dfinity-radix-tree/blob/806b9f80cab0da81d7e98a62754b80167fe58296/index.js#L252-L254 "Source code on GitHub") | |
83 | 83 | |
84 | 84 | returns the state of an empty tree |
85 | 85 | |
86 | 86 | ## ArrayConstructor |
87 | 87 | |
88 | -[index.js:260-262][20] | |
88 | +[index.js:260-262](https://github.com/dfinity/js-dfinity-radix-tree/blob/806b9f80cab0da81d7e98a62754b80167fe58296/index.js#L260-L262 "Source code on GitHub") | |
89 | 89 | |
90 | -returns an Uint1Array constructir which is used to repersent keys | |
90 | +returns an Uint1Array constructor which is used to represent keys | |
91 | 91 | |
92 | -Returns **[object][21]** | |
92 | +Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** | |
93 | 93 | |
94 | 94 | ## getMerkleLink |
95 | 95 | |
96 | -[index.js:269-271][22] | |
96 | +[index.js:269-271](https://github.com/dfinity/js-dfinity-radix-tree/blob/806b9f80cab0da81d7e98a62754b80167fe58296/index.js#L269-L271 "Source code on GitHub") | |
97 | 97 | |
98 | 98 | returns a merkle link for some given data |
99 | 99 | |
100 | 100 | **Parameters** |
101 | 101 | |
102 | -- `data` **[Buffer][23]** the data which you would like to hash | |
102 | +- `data` **[Buffer](https://nodejs.org/api/buffer.html)** the data which you would like to hash | |
103 | 103 | |
104 | -Returns **[Buffer][23]** | |
105 | - | |
106 | -[1]: #constructor | |
107 | - | |
108 | -[2]: #get | |
109 | - | |
110 | -[3]: #set | |
111 | - | |
112 | -[4]: #delete | |
113 | - | |
114 | -[5]: #done | |
115 | - | |
116 | -[6]: #flush | |
117 | - | |
118 | -[7]: #emptytreestate | |
119 | - | |
120 | -[8]: #arrayconstructor | |
121 | - | |
122 | -[9]: #getmerklelink | |
123 | - | |
124 | -[10]: https://github.com/dfinity/js-dfinity-radix-tree/blob/0ab6c08c96392d6f3a76fbaf0a826b48445e56b8/index.js#L17-L25 "Source code on GitHub" | |
125 | - | |
126 | -[11]: https://github.com/ipld/js-ipld-graph-builder | |
127 | - | |
128 | -[12]: https://github.com/ipfs/js-ipfs#dag | |
129 | - | |
130 | -[13]: https://github.com/dfinity/js-dfinity-radix-tree/blob/0ab6c08c96392d6f3a76fbaf0a826b48445e56b8/index.js#L32-L36 "Source code on GitHub" | |
131 | - | |
132 | -[14]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise | |
133 | - | |
134 | -[15]: https://github.com/dfinity/js-dfinity-radix-tree/blob/0ab6c08c96392d6f3a76fbaf0a826b48445e56b8/index.js#L87-L90 "Source code on GitHub" | |
135 | - | |
136 | -[16]: https://github.com/dfinity/js-dfinity-radix-tree/blob/0ab6c08c96392d6f3a76fbaf0a826b48445e56b8/index.js#L139-L142 "Source code on GitHub" | |
137 | - | |
138 | -[17]: https://github.com/dfinity/js-dfinity-radix-tree/blob/0ab6c08c96392d6f3a76fbaf0a826b48445e56b8/index.js#L201-L207 "Source code on GitHub" | |
139 | - | |
140 | -[18]: https://github.com/dfinity/js-dfinity-radix-tree/blob/0ab6c08c96392d6f3a76fbaf0a826b48445e56b8/index.js#L223-L226 "Source code on GitHub" | |
141 | - | |
142 | -[19]: https://github.com/dfinity/js-dfinity-radix-tree/blob/0ab6c08c96392d6f3a76fbaf0a826b48445e56b8/index.js#L252-L254 "Source code on GitHub" | |
143 | - | |
144 | -[20]: https://github.com/dfinity/js-dfinity-radix-tree/blob/0ab6c08c96392d6f3a76fbaf0a826b48445e56b8/index.js#L260-L262 "Source code on GitHub" | |
145 | - | |
146 | -[21]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object | |
147 | - | |
148 | -[22]: https://github.com/dfinity/js-dfinity-radix-tree/blob/0ab6c08c96392d6f3a76fbaf0a826b48445e56b8/index.js#L269-L271 "Source code on GitHub" | |
149 | - | |
150 | -[23]: https://nodejs.org/api/buffer.html | |
104 | +Returns **[Buffer](https://nodejs.org/api/buffer.html)** |
docs/spec.md | ||
---|---|---|
@@ -1,25 +1,25 @@ | ||
1 | -This documnet provides the structure of the [Dfinity's Radix Tree.](https://ipfs.io/ipns/QmdJiuMWp2FxyaerfLrtdLF6Nr1EWpL7dPAxA9oKSPYYgV/wiki/Radix_tree.html) | |
2 | -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) | |
1 | +This document provides the structure of the [Dfinity Radix Tree.](https://ipfs.io/ipns/QmdJiuMWp2FxyaerfLrtdLF6Nr1EWpL7dPAxA9oKSPYYgV/wiki/Radix_tree.html) | |
2 | +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) | |
3 | 3 | |
4 | 4 | ## Node |
5 | -Each node has a type and contains at most four elements: | |
5 | +Each node has a type and contains at most four elements: | |
6 | 6 | "extension", "left branch", "right branch" and "value". |
7 | 7 | |
8 | 8 | ``` |
9 | 9 | node : = TYPE | EXTENSION | LBRANCH | RBRANCH | VALUE |
10 | 10 | ``` |
11 | 11 | |
12 | 12 | ### Type |
13 | -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 | |
13 | +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 | |
14 | 14 | |
15 | 15 | ``` |
16 | 16 | Type := 0 | 0 | 0 | 0 | HasEXTENSION | HasLBRANCH | HasRBRANCH | HasVALUE |
17 | 17 | ``` |
18 | 18 | |
19 | 19 | For example a node that contained a left branch and a value would have a prefix byte of 00000101 or 0x07 |
20 | 20 | |
21 | -The full encoded node would then look something like. `0x07<20_bytes_for_lbranch><remaing_bytes_for_value>` | |
21 | +The full encoded node would then look something like. `0x07<20_bytes_for_lbranch><remaing_bytes_for_value>` | |
22 | 22 | |
23 | 23 | |
24 | 24 | ### Branches |
25 | 25 | |
@@ -42,10 +42,10 @@ | ||
42 | 42 | ``` |
43 | 43 | extension := Length | ExtensionValue |
44 | 44 | |
45 | 45 | ``` |
46 | -Where the length is the number of bits that extension repesents. This varuint32 | |
47 | -encoded with leb128. And the extension is bit array padded with 0's to the nearst byte. | |
46 | +Where the length is the number of bits that extension represents. This varuint32 | |
47 | +encoded with leb128. And the extension is bit array padded with 0's to the nearest byte. | |
48 | 48 | |
49 | 49 | For example if the binary keys [0, 0, 1, 1] and |
50 | 50 | [0, 0, 1, 0] have a shared path of [0, 0, 1]. The extension node would therefor be |
51 | 51 |
index.js | ||
---|---|---|
@@ -9,10 +9,10 @@ | ||
9 | 9 | module.exports = class RadixTree { |
10 | 10 | /** |
11 | 11 | * @param opts |
12 | 12 | * @param opts.root {object} a merkle root to a radix tree. If none, RadixTree will create an new root. |
13 | - * @param opts.db {object} a level db instance alternitly `opts.graph` can be used | |
14 | - * @param opts.graph {object} an instance of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder) alternitvly `opts.dag` can be used | |
13 | + * @param opts.db {object} a level db instance; alternatively, `opts.graph` can be used | |
14 | + * @param opts.graph {object} an instance of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder); alternatively, `opts.dag` can be used | |
15 | 15 | * @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. |
16 | 16 | */ |
17 | 17 | constructor (opts) { |
18 | 18 | this.root = opts.root || { |
@@ -47,9 +47,9 @@ | ||
47 | 47 | let subKey = key.subarray(index) |
48 | 48 | |
49 | 49 | const {extensionIndex, extensionLen, extension} = findMatchBits(subKey, root) |
50 | 50 | index += extensionIndex |
51 | - // check if we compelete travered the extension | |
51 | + // check if we complete traversed the extension | |
52 | 52 | if (extensionIndex !== extensionLen) { |
53 | 53 | return {index, root, extension, extensionIndex} |
54 | 54 | } |
55 | 55 | } |
@@ -57,9 +57,9 @@ | ||
57 | 57 | let keySegment = key[index] |
58 | 58 | if (keySegment !== undefined) { |
59 | 59 | const branch = treeNode.getBranch(root) |
60 | 60 | await this.graph.get(branch, keySegment, true) |
61 | - // preseves the '/' | |
61 | + // preserves the '/' | |
62 | 62 | const nextRoot = branch[keySegment] |
63 | 63 | if (!nextRoot) { |
64 | 64 | return {root, index} |
65 | 65 | } else { |
@@ -216,9 +216,9 @@ | ||
216 | 216 | return this._setting |
217 | 217 | } |
218 | 218 | |
219 | 219 | /** |
220 | - * creates a merkle root for the current tree and stores the data perstantly | |
220 | + * creates a merkle root for the current tree and stores the data persistently | |
221 | 221 | * @returns {Promise} |
222 | 222 | */ |
223 | 223 | async flush () { |
224 | 224 | await this.done() |
@@ -253,9 +253,9 @@ | ||
253 | 253 | return [null, null, null] |
254 | 254 | } |
255 | 255 | |
256 | 256 | /** |
257 | - * returns an Uint1Array constructir which is used to repersent keys | |
257 | + * returns an Uint1Array constructor which is used to represent keys | |
258 | 258 | * @returns {object} |
259 | 259 | */ |
260 | 260 | static get ArrayConstructor () { |
261 | 261 | return Uint1Array |
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 328236 bytes New file size: 326966 bytes |
package.json | ||
---|---|---|
@@ -1,7 +1,7 @@ | ||
1 | 1 | { |
2 | 2 | "name": "dfinity-radix-tree", |
3 | - "version": "0.1.1", | |
3 | + "version": "0.1.2", | |
4 | 4 | "description": "This implements a binary merkle radix tree", |
5 | 5 | "main": "index.js", |
6 | 6 | "scripts": { |
7 | 7 | "coveralls": "npm run coverage && nyc report --reporter=text-lcov | coveralls", |
@@ -28,9 +28,8 @@ | ||
28 | 28 | }, |
29 | 29 | "dependencies": { |
30 | 30 | "borc": "^2.0.2", |
31 | 31 | "ipld-graph-builder": "^1.3.7", |
32 | - "node-webcrypto-shim": "0.0.0", | |
33 | 32 | "text-encoding": "^0.6.4", |
34 | 33 | "uint1array": "^1.0.5" |
35 | 34 | }, |
36 | 35 | "standard": { |
Built with git-ssb-web