git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Commit 6bc933a81719dede128577c8261ed8f5b64e808d

fix encoding bugs

wanderer committed on 3/22/2018, 11:24:19 PM
Parent: fac7ea03e4bddd4c95ce4337479feba208d63c48

Files changed

datastore.jschanged
index.jschanged
package-lock.jsonchanged
package.jsonchanged
tests/index.jschanged
treeNode.jschanged
datastore.jsView
@@ -5,17 +5,37 @@
55
66 const HASH_LEN = 20
77 const LINK_TAG = 42
88
9+function insertTags (val) {
10+ if (Array.isArray(val)) {
11+ val = val.map(v => {
12+ if (v && v.hasOwnProperty('/')) {
13+ return new cbor.Tagged(LINK_TAG, v['/'])
14+ } else {
15+ return insertTags(v)
16+ }
17+ })
18+ }
19+ return val
20+}
21+
922 module.exports = class TreeDAG extends DAG {
10- async put (val) {
11- if (val[1]) {
12- val[1] = new cbor.Tagged(LINK_TAG, val[1]['/'])
23+ constructor (dag, decoder = new cbor.Decoder({
24+ tags: {
25+ 42: val => {
26+ return {
27+ '/': val
28+ }
29+ }
1330 }
14- if (val[2]) {
15- val[2] = new cbor.Tagged(LINK_TAG, val[2]['/'])
16- }
31+ })) {
32+ super(dag)
33+ this.decoder = decoder
34+ }
1735
36+ async put (val) {
37+ val = insertTags(val)
1838 const encoded = cbor.encode(val)
1939 const key = await TreeDAG.getMerkleLink(encoded)
2040
2141 return new Promise((resolve, reject) => {
@@ -31,15 +51,9 @@
3151 if (err) {
3252 reject(err)
3353 } else {
3454 val = Buffer.from(val, 'hex')
35- const decoded = cbor.decode(val)
36- if (decoded[1]) {
37- decoded[1]['/'] = decoded[1].value
38- }
39- if (decoded[2]) {
40- decoded[2]['/'] = decoded[2].value
41- }
55+ const decoded = this.decoder.decodeFirst(val)
4256 resolve(decoded)
4357 }
4458 })
4559 })
index.jsView
@@ -12,15 +12,16 @@
1212 * @param opts.root {object} a merkle root to a radix tree. If none, RadixTree will create an new root.
1313 * @param opts.db {object} a level db instance; alternatively, `opts.graph` can be used
1414 * @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.
16+ * @param opts.decoder {object} a cbor decoder
1617 */
1718 constructor (opts) {
1819 this.root = opts.root || {
1920 '/': RadixTree.emptyTreeState
2021 }
2122
22- this.dag = opts.dag || new DataStore(opts.db)
23+ this.dag = opts.dag || new DataStore(opts.db, opts.decoder)
2324 this.graph = opts.graph || new Graph(this.dag)
2425 this._setting = Promise.resolve()
2526 }
2627
@@ -44,9 +45,8 @@
4445 // load the root
4546 const exNode = await this.graph.get(root, treeNode.EXTENSION, true)
4647 if (exNode) {
4748 let subKey = key.subarray(index)
48-
4949 const {extensionIndex, extensionLen, extension} = findMatchBits(subKey, root)
5050 index += extensionIndex
5151 // check if we complete traversed the extension
5252 if (extensionIndex !== extensionLen) {
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 357982 bytes
New file size: 358067 bytes
package.jsonView
@@ -26,9 +26,9 @@
2626 "standard": "^11.0.1",
2727 "tape": "^4.6.3"
2828 },
2929 "dependencies": {
30- "borc": "^2.0.2",
30+ "borc": "git+ssh://git@github.com:dignifiedquire/borc.git#fix/nested-array",
3131 "ipld-graph-builder": "^1.3.8",
3232 "text-encoding": "^0.6.4",
3333 "uint1array": "^1.0.5"
3434 },
tests/index.jsView
@@ -13,8 +13,25 @@
1313 t.deepEquals(stateRoot2, stateRoot)
1414 t.end()
1515 })
1616
17+tape('should generate the same stateRoot', async t => {
18+ let tree1 = new RadixTree({
19+ db
20+ })
21+
22+ let tree2 = new RadixTree({
23+ db
24+ })
25+ await tree1.flush()
26+ tree1.set('test', Buffer.from('cat'))
27+ tree2.set('test', Buffer.from('cat'))
28+ const stateRoot = await tree1.flush()
29+ const stateRoot2 = await tree2.flush()
30+ t.deepEquals(stateRoot2, stateRoot)
31+ t.end()
32+})
33+
1734 tape('set and get', async t => {
1835 const r = await RadixTree.getMerkleLink(Buffer.from([0]))
1936
2037 t.equal(r.toString('hex'), '6e340b9cffb37a989ca544e6bb780a2c78901d3f', 'should hash')
treeNode.jsView
@@ -1,5 +1,6 @@
11 const Uint1Array = require('uint1array')
2+const EMPTY_STATE_ROOT = Buffer.from('4cf812be9be2c6008325050f43d06676a08612c7', 'hex')
23
34 function toTypedArray (array) {
45 return new Uint1Array(new Uint8Array(array).buffer)
56 }
@@ -49,7 +50,10 @@
4950 node['/'][VALUE] = val
5051 }
5152
5253 exports.isEmpty = function (node) {
53- const branch = exports.getBranch(node)
54- return !node['/'][EXTENSION] && !branch[0] && !branch[1] && node['/'][VALUE] === undefined
54+ if (Buffer.isBuffer(node['/'])) {
55+ return !Buffer.compare(node['/'], EMPTY_STATE_ROOT)
56+ } else {
57+ return node['/'].every(el => !el)
58+ }
5559 }

Built with git-ssb-web