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