git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: 523fd9bda6df579abb596ed6e04a5c19c32ef4c2

Files: 523fd9bda6df579abb596ed6e04a5c19c32ef4c2 / tests / index.js

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

Built with git-ssb-web