git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Commit 5b0dc0645be6db668260d4699035f42d5b880a01

Merge pull request #10 from dfinity/crypto

replace node-webcrypto-shim
wanderer 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.mdchanged
datastore.jschanged
docs/index.mdchanged
docs/spec.mdchanged
index.jschanged
package-lock.jsonchanged
package.jsonchanged
.gitignoreadded
README.mdView
@@ -1,17 +1,17 @@
11 [![NPM Package](https://img.shields.io/npm/v/dfinity-radix-tree.svg?style=flat-square)](https://www.npmjs.org/package/dfinity-radix-tree)
22 [![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)
44
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)
66
77 # Synopsis
88
99 :evergreen_tree: This implements a binary merkle radix tree. The point of using a binary radix
1010 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
1414 top of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder)
1515 and the resulting state and proofs are generated using it.
1616
1717 ## Install
@@ -51,20 +51,20 @@
5151 ```
5252 ## API
5353 ['./docs/'](./docs/index.md)
5454
55-## Spefication
55+## Specification
5656 ['./docs/spec.md'](./docs/spec.md)
5757
5858 ## Benchmarks
5959 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.
6161
6262 ['./benchmarks/benchmarks.md'](./benchmark/results.md)
6363
6464 ## License
6565
66-[**(C) 2017 DFINITY STIFTUNG**](http://dfinity.network)
66+[**(C) 2018 DFINITY STIFTUNG**](http://dfinity.network)
6767
6868 All code and designs are open sourced under GPL V3.
6969
7070 ![image](https://user-images.githubusercontent.com/6457089/32753794-10f4cbc2-c883-11e7-8dcf-ff8088b38f9f.png)
datastore.jsView
@@ -1,6 +1,6 @@
11 const Buffer = require('safe-buffer').Buffer
2-const crypto = require('node-webcrypto-shim')
2+const crypto = require('crypto')
33 const DAG = require('ipld-graph-builder/datastore.js')
44 const HASH_LEN = 20
55 const cbor = require('borc')
66
@@ -34,9 +34,9 @@
3434 return Buffer.isBuffer(link) && link.length === HASH_LEN
3535 }
3636
3737 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)
4141 }
4242 }
docs/index.mdView
@@ -1,150 +1,104 @@
11 <!-- Generated by documentation.js. Update this documentation by updating the source code. -->
22
33 ### Table of Contents
44
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)
1414
1515 ## constructor
1616
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")
1818
1919 **Parameters**
2020
2121 - `opts`
2222 - `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.
2626
2727 ## get
2828
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")
3030
3131 gets a value given a key
3232
3333 **Parameters**
3434
3535 - `key` **any**
3636
37-Returns **[Promise][14]**
37+Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
3838
3939 ## set
4040
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")
4242
4343 stores a value at a given key returning the tree node that the value was saved in
4444
4545 **Parameters**
4646
4747 - `key` **any**
4848 - `value`
4949
50-Returns **[Promise][14]**
50+Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
5151
5252 ## delete
5353
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")
5555
5656 smContainer.js deletes a value at a given key
5757
5858 **Parameters**
5959
6060 - `key` **any**
6161
62-Returns **[Promise][14]**
62+Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
6363
6464 ## done
6565
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")
6767
6868 returns a promise that resolve when the tree is done with all of its writes
6969
70-Returns **[Promise][14]**
70+Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
7171
7272 ## flush
7373
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")
7575
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
7777
78-Returns **[Promise][14]**
78+Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
7979
8080 ## emptyTreeState
8181
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")
8383
8484 returns the state of an empty tree
8585
8686 ## ArrayConstructor
8787
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")
8989
90-returns an Uint1Array constructir which is used to repersent keys
90+returns an Uint1Array constructor which is used to represent keys
9191
92-Returns **[object][21]**
92+Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
9393
9494 ## getMerkleLink
9595
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")
9797
9898 returns a merkle link for some given data
9999
100100 **Parameters**
101101
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
103103
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.mdView
@@ -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)
33
44 ## 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:
66 "extension", "left branch", "right branch" and "value".
77
88 ```
99 node : = TYPE | EXTENSION | LBRANCH | RBRANCH | VALUE
1010 ```
1111
1212 ### 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
1414
1515 ```
1616 Type := 0 | 0 | 0 | 0 | HasEXTENSION | HasLBRANCH | HasRBRANCH | HasVALUE
1717 ```
1818
1919 For example a node that contained a left branch and a value would have a prefix byte of 00000101 or 0x07
2020
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>`
2222
2323
2424 ### Branches
2525
@@ -42,10 +42,10 @@
4242 ```
4343 extension := Length | ExtensionValue
4444
4545 ```
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.
4848
4949 For example if the binary keys [0, 0, 1, 1] and
5050 [0, 0, 1, 0] have a shared path of [0, 0, 1]. The extension node would therefor be
5151
index.jsView
@@ -9,10 +9,10 @@
99 module.exports = class RadixTree {
1010 /**
1111 * @param opts
1212 * @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
1515 * @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.
1616 */
1717 constructor (opts) {
1818 this.root = opts.root || {
@@ -47,9 +47,9 @@
4747 let subKey = key.subarray(index)
4848
4949 const {extensionIndex, extensionLen, extension} = findMatchBits(subKey, root)
5050 index += extensionIndex
51- // check if we compelete travered the extension
51+ // check if we complete traversed the extension
5252 if (extensionIndex !== extensionLen) {
5353 return {index, root, extension, extensionIndex}
5454 }
5555 }
@@ -57,9 +57,9 @@
5757 let keySegment = key[index]
5858 if (keySegment !== undefined) {
5959 const branch = treeNode.getBranch(root)
6060 await this.graph.get(branch, keySegment, true)
61- // preseves the '/'
61+ // preserves the '/'
6262 const nextRoot = branch[keySegment]
6363 if (!nextRoot) {
6464 return {root, index}
6565 } else {
@@ -216,9 +216,9 @@
216216 return this._setting
217217 }
218218
219219 /**
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
221221 * @returns {Promise}
222222 */
223223 async flush () {
224224 await this.done()
@@ -253,9 +253,9 @@
253253 return [null, null, null]
254254 }
255255
256256 /**
257- * returns an Uint1Array constructir which is used to repersent keys
257+ * returns an Uint1Array constructor which is used to represent keys
258258 * @returns {object}
259259 */
260260 static get ArrayConstructor () {
261261 return Uint1Array
package-lock.jsonView
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.jsonView
@@ -1,7 +1,7 @@
11 {
22 "name": "dfinity-radix-tree",
3- "version": "0.1.1",
3+ "version": "0.1.2",
44 "description": "This implements a binary merkle radix tree",
55 "main": "index.js",
66 "scripts": {
77 "coveralls": "npm run coverage && nyc report --reporter=text-lcov | coveralls",
@@ -28,9 +28,8 @@
2828 },
2929 "dependencies": {
3030 "borc": "^2.0.2",
3131 "ipld-graph-builder": "^1.3.7",
32- "node-webcrypto-shim": "0.0.0",
3332 "text-encoding": "^0.6.4",
3433 "uint1array": "^1.0.5"
3534 },
3635 "standard": {
.gitignoreView
@@ -1,0 +1,2 @@
1+node_modules
2+testdb

Built with git-ssb-web