git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Commit e765e8d5906eb0ab820c7eba46ead2f17766f54f

add rootExists method

wanderer committed on 3/22/2018, 11:39:27 PM
Parent: 6bc933a81719dede128577c8261ed8f5b64e808d

Files changed

index.jschanged
tests/index.jschanged
index.jsView
@@ -229,8 +229,23 @@
229229 key = RadixTree.formatKey(key)
230230 return key
231231 }
232232
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+
233248 static formatKey (key) {
234249 if (typeof key === 'string') {
235250 key = encoder.encode(key)
236251 }
tests/index.jsView
@@ -3,8 +3,21 @@
33 const level = require('level-browserify')
44 const RadixTree = require('../')
55 const db = level('./testdb')
66
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+
720 tape('should generate the same stateRoot', async t => {
821 let tree = new RadixTree({
922 db: db
1023 })

Built with git-ssb-web