git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: ccf8f0f9324db6b24936206bd97e1f8554e7dacb

Files: ccf8f0f9324db6b24936206bd97e1f8554e7dacb / tests / index.js

5017 bytesRaw
1const tape = require('tape')
2const crypto = require('crypto')
3const level = require('level-browserify')
4const RadixTree = require('../')
5const db = level('./testdb')
6
7tape('root existance', async t => {
8 let tree = new RadixTree({
9 db: db
10 })
11 let exists = await tree.rootExists(Buffer.from([0]))
12 t.equals(exists, false)
13
14 tree.set('test', Buffer.from('cat'))
15 exists = await tree.rootExists(Buffer.from('01cff22f1e93e25d8691d6238031d98885ab468f', 'hex'))
16 t.equals(exists, true)
17 t.end()
18})
19
20tape('should generate the same stateRoot', async t => {
21 let tree = new RadixTree({
22 db: db
23 })
24 tree.root = [null, null, null]
25 const stateRoot = await tree.flush()
26 const stateRoot2 = await tree.flush()
27 t.deepEquals(stateRoot2, stateRoot)
28 t.end()
29})
30
31tape('should generate the same stateRoot', async t => {
32 let tree1 = new RadixTree({
33 db
34 })
35
36 let tree2 = new RadixTree({
37 db
38 })
39 await tree1.flush()
40 tree1.set('test', Buffer.from('cat'))
41 tree2.set('test', Buffer.from('cat'))
42 const stateRoot = await tree1.flush()
43 const stateRoot2 = await tree2.flush()
44 t.deepEquals(stateRoot2, stateRoot)
45 t.end()
46})
47
48tape('set and get', async t => {
49 const r = await RadixTree.getMerkleLink(Buffer.from([0]))
50
51 t.equal(r.toString('hex'), '6e340b9cffb37a989ca544e6bb780a2c78901d3f', 'should hash')
52
53 let tree = new RadixTree({
54 db: db
55 })
56
57 tree.set('test', Buffer.from('cat'))
58 let val = await tree.get('test')
59 t.equals(val.value.toString(), 'cat')
60 tree.set('te', Buffer.from('blop'))
61 val = await tree.get('test')
62 t.equals(val.value.toString(), 'cat')
63
64 val = await tree.get('te')
65 t.equals(val.value.toString(), 'blop')
66
67 tree.set('rad', Buffer.from('cat2'))
68
69 val = await tree.get('rad')
70 t.equals(val.value.toString(), 'cat2')
71
72 tree.set('test', Buffer.from('cat111'))
73 val = await tree.get('test')
74 t.equals(val.value.toString(), 'cat111')
75
76 const stateRoot = await tree.flush()
77
78 // try reteriving node from ipfs
79 tree = new RadixTree({
80 db: db,
81 root: stateRoot
82 })
83
84 val = await tree.get('te')
85 t.equals(val.value.toString(), 'blop')
86
87 val = await tree.get('rad')
88 t.equals(val.value.toString(), 'cat2')
89
90 val = await tree.get('test')
91 t.equals(val.value.toString(), 'cat111')
92 // console.log(JSON.stringify(tree.root, null, 2))
93 t.end()
94})
95
96tape('branch nodes', async t => {
97 const tree = new RadixTree({
98 db: db
99 })
100
101 let key0 = new RadixTree.ArrayConstructor([1, 1])
102 let key1 = new RadixTree.ArrayConstructor([0, 1])
103 let key2 = new RadixTree.ArrayConstructor([1, 0])
104 let key3 = new RadixTree.ArrayConstructor([0, 0])
105
106 tree.set(key0, Buffer.from('cat'))
107 tree.set(key1, Buffer.from('cat2'))
108 tree.set(key2, Buffer.from('cat'))
109 tree.set(key3, Buffer.from('cat3'))
110
111 let val = await tree.get(key0)
112 t.equals(val.value.toString(), 'cat')
113 val = await tree.get(key1)
114 t.equals(val.value.toString(), 'cat2')
115 val = await tree.get(key2)
116 t.equals(val.value.toString(), 'cat')
117 val = await tree.get(key3)
118 t.equals(val.value.toString(), 'cat3')
119
120 t.end()
121})
122
123tape('delete', async t => {
124 const tree = new RadixTree({
125 db: db
126 })
127 tree.set('test', Buffer.from('cat'))
128 tree.set('ter', Buffer.from('cat3'))
129 tree.delete('te')
130 tree.delete('test')
131 await tree.delete('ter')
132 t.deepEquals(tree.root, RadixTree.emptyTreeState)
133
134 // tests delete midle branchs
135 tree.set('test', Buffer.from('cat'))
136 tree.set('te', Buffer.from('cat2'))
137 tree.set('ter', Buffer.from('cat3'))
138 await tree.delete('te')
139 let val = await tree.get('test')
140 t.equals(val.value.toString(), 'cat')
141
142 // tests delete end branchs
143 tree.set('te', 'cat2')
144 tree.delete('ter')
145 tree.delete('te')
146 await tree.delete('test')
147 t.deepEquals(tree._root['/'], RadixTree.emptyTreeState)
148 t.end()
149})
150
151tape('large values', async t => {
152 const tree = new RadixTree({
153 db: db
154 })
155 const saved = Buffer.alloc(33).fill(1)
156 tree.set('test', saved)
157 const value = await tree.get('test')
158 t.equals(value.value.toString(), saved.toString())
159 t.end()
160})
161
162tape('errors', async t => {
163 const tree = new RadixTree({
164 db: db,
165 root: {
166 '/': Buffer.alloc(20)
167 }
168 })
169
170 try {
171 await tree.get('test')
172 } catch (e) {
173 t.end()
174 }
175})
176
177tape('random', async t => {
178 const tree = new RadixTree({
179 db: db
180 })
181 const entries = 100
182 for (let i = 0; i < entries; i++) {
183 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
184 tree.set(key, Buffer.from([i]))
185 }
186 // console.log(JSON.stringify(tree.root, null, 2))
187
188 await tree.flush()
189
190 for (let i = 0; i < entries; i++) {
191 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
192 const value = await tree.get(key)
193 t.equals(value.value[0], i)
194 }
195
196 await tree.flush()
197 for (let i = 0; i < entries; i++) {
198 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
199 await tree.delete(key)
200 }
201
202 t.deepEquals(tree._root['/'], RadixTree.emptyTreeState)
203
204 t.end()
205})
206

Built with git-ssb-web