Commit e765e8d5906eb0ab820c7eba46ead2f17766f54f
add rootExists method
wanderer committed on 3/22/2018, 11:39:27 PMParent: 6bc933a81719dede128577c8261ed8f5b64e808d
Files changed
index.js | changed |
tests/index.js | changed |
index.js | ||
---|---|---|
@@ -229,8 +229,23 @@ | ||
229 | 229 | key = RadixTree.formatKey(key) |
230 | 230 | return key |
231 | 231 | } |
232 | 232 | |
233 | + /** | |
234 | + * Checks if a given root exists or not | |
235 | + * @param {Buffer} root | |
236 | + * @return {Promise<boolean>} | |
237 | + */ | |
238 | + async rootExists (root) { | |
239 | + await this.flush() | |
240 | + try { | |
241 | + await this.dag.get(root) | |
242 | + } catch (e) { | |
243 | + return false | |
244 | + } | |
245 | + return true | |
246 | + } | |
247 | + | |
233 | 248 | static formatKey (key) { |
234 | 249 | if (typeof key === 'string') { |
235 | 250 | key = encoder.encode(key) |
236 | 251 | } |
tests/index.js | ||
---|---|---|
@@ -3,8 +3,21 @@ | ||
3 | 3 | const level = require('level-browserify') |
4 | 4 | const RadixTree = require('../') |
5 | 5 | const db = level('./testdb') |
6 | 6 | |
7 | +tape('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 | + | |
7 | 20 | tape('should generate the same stateRoot', async t => { |
8 | 21 | let tree = new RadixTree({ |
9 | 22 | db: db |
10 | 23 | }) |
Built with git-ssb-web