git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Commit f47aea5be452991650859d2af99234ea38e6efd4

expost merkleLink function

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 10/9/2017, 7:02:55 PM
Parent: 41d856e209658485fcf9b1adaa49cada3e75c9de

Files changed

datastore.jschanged
index.jschanged
package.jsonchanged
tests/index.jschanged
datastore.jsView
@@ -6,14 +6,10 @@
66
77 module.exports = class TreeDAG extends DAG {
88 async put (val) {
99 const encoded = treeNode.encode(val)
10- let key = await crypto.subtle.digest({
11- name: 'SHA-256'
12- }, encoded)
10 + let key = await TreeDAG.getMerkleLink(encoded)
1311
14- key = Buffer.from(key.slice(0, HASH_LEN))
15-
1612 return new Promise((resolve, reject) => {
1713 this._dag.put(key, encoded.toString('hex'), () => {
1814 resolve(key)
1915 })
@@ -33,8 +29,14 @@
3329 })
3430 })
3531 }
3632
37- isValidLink (link) {
33 + static isValidLink (link) {
3834 return Buffer.isBuffer(link) && link.length === HASH_LEN
3935 }
36 +
37 + static getMerkleLink (buf) {
38 + return crypto.subtle.digest({
39 + name: 'SHA-256'
40 + }, buf).then(link => Buffer.from(link.slice(0, HASH_LEN)))
41 + }
4042 }
index.jsView
@@ -37,8 +37,17 @@
3737 static get ArrayConstructor () {
3838 return Uint1Array
3939 }
4040
41 + /**
42 + * returns a merkle link for some given data
43 + * @param {Buffer} data - the data which you would like to hash
44 + * @returns {Buffer}
45 + */
46 + static getMerkleLink (data) {
47 + return DataStore.getMerkleLink(data)
48 + }
49 +
4150 async _get (key) {
4251 let index = 0
4352 let root = this.root
4453 let parent
package.jsonView
@@ -26,9 +26,9 @@
2626 "standard": "^10.0.0",
2727 "tape": "^4.6.3"
2828 },
2929 "dependencies": {
30- "ipld-graph-builder": "^1.3.3",
30 + "ipld-graph-builder": "^1.3.5",
3131 "leb128": "0.0.3",
3232 "node-webcrypto-shim": "0.0.0",
3333 "safe-buffer": "^5.1.1",
3434 "text-encoding": "^0.6.4",
tests/index.jsView
@@ -4,8 +4,12 @@
44 const RadixTree = require('../')
55 const db = level('./testdb')
66
77 tape('set and get', async t => {
8 + const r = await RadixTree.getMerkleLink(Buffer.from([0]))
9 +
10 + t.equal(r.toString('hex'), '6e340b9cffb37a989ca544e6bb780a2c78901d3f', 'should hash')
11 +
812 let tree = new RadixTree({
913 db: db
1014 })
1115
@@ -78,33 +82,29 @@
7882 tape('delete', async t => {
7983 const tree = new RadixTree({
8084 db: db
8185 })
82- try {
83- await tree.set('test', Buffer.from('cat'))
84- await tree.set('ter', Buffer.from('cat3'))
85- await tree.delete('te')
86- await tree.delete('test')
87- await tree.delete('ter')
88- t.deepEquals(tree.root['/'], RadixTree.emptyTreeState)
86 + await tree.set('test', Buffer.from('cat'))
87 + await tree.set('ter', Buffer.from('cat3'))
88 + await tree.delete('te')
89 + await tree.delete('test')
90 + await tree.delete('ter')
91 + t.deepEquals(tree.root['/'], RadixTree.emptyTreeState)
8992
90- // tests delete midle branchs
91- await tree.set('test', Buffer.from('cat'))
92- await tree.set('te', Buffer.from('cat2'))
93- await tree.set('ter', Buffer.from('cat3'))
94- await tree.delete('te')
95- let val = await tree.get('test')
96- t.equals(val.toString(), 'cat')
93 + // tests delete midle branchs
94 + await tree.set('test', Buffer.from('cat'))
95 + await tree.set('te', Buffer.from('cat2'))
96 + await tree.set('ter', Buffer.from('cat3'))
97 + await tree.delete('te')
98 + let val = await tree.get('test')
99 + t.equals(val.toString(), 'cat')
97100
98- // tests delete end branchs
99- await tree.set('te', 'cat2')
100- await tree.delete('ter')
101- await tree.delete('te')
102- await tree.delete('test')
103- t.deepEquals(tree.root['/'], RadixTree.emptyTreeState)
104- } catch (e) {
105- console.log(e)
106- }
101 + // tests delete end branchs
102 + await tree.set('te', 'cat2')
103 + await tree.delete('ter')
104 + await tree.delete('te')
105 + await tree.delete('test')
106 + t.deepEquals(tree.root['/'], RadixTree.emptyTreeState)
107107 t.end()
108108 })
109109
110110 tape('large values', async t => {

Built with git-ssb-web