This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
baseTrie.js
734 lines (657 loc) · 20.5 KB
/
baseTrie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
const assert = require('assert')
const levelup = require('levelup')
const memdown = require('memdown')
const async = require('async')
const rlp = require('rlp')
const ethUtil = require('ethereumjs-util')
const semaphore = require('semaphore')
const TrieNode = require('./trieNode')
const ReadStream = require('./readStream')
const matchingNibbleLength = require('./util').matchingNibbleLength
const doKeysMatch = require('./util').doKeysMatch
const callTogether = require('./util').callTogether
const asyncFirstSeries = require('./util').asyncFirstSeries
module.exports = Trie
/**
* Use `require('merkel-patricia-tree')` for the base interface. In Ethereum applications stick with the Secure Trie Overlay `require('merkel-patricia-tree/secure')`. The API for the raw and the secure interface are about the same
* @class Trie
* @param {Object} [db] An instance of [levelup](https://github.com/rvagg/node-levelup/) or a compatible API. If the db is `null` or left undefined, then the trie will be stored in memory via [memdown](https://github.com/rvagg/memdown)
* @param {Buffer|String} [root]` A hex `String` or `Buffer` for the root of a previously stored trie
* @prop {Buffer} root The current root of the `trie`
* @prop {Boolean} isCheckpoint determines if you are saving to a checkpoint or directly to the db
* @prop {Buffer} EMPTY_TRIE_ROOT the Root for an empty trie
*/
function Trie (db, root) {
var self = this
this.EMPTY_TRIE_ROOT = ethUtil.SHA3_RLP
this.sem = semaphore(1)
// setup dbs
this.db = db ||
levelup('', {
db: memdown
})
this._getDBs = [this.db]
this._putDBs = [this.db]
Object.defineProperty(this, 'root', {
set: function (value) {
if (value) {
value = ethUtil.toBuffer(value)
assert(value.length === 32, 'Invalid root length. Roots are 32 bytes')
} else {
value = self.EMPTY_TRIE_ROOT
}
this._root = value
},
get: function () {
return this._root
}
})
this.root = root
}
/**
* Gets a value given a `key`
* @method get
* @param {Buffer|String} key - the key to search for
* @param {Function} cb A callback `Function` which is given the arguments `err` - for errors that may have occured and `value` - the found value in a `Buffer` or if no value was found `null`
*/
Trie.prototype.get = function (key, cb) {
var self = this
key = ethUtil.toBuffer(key)
self._findPath(key, function (err, node, remainder, stack) {
var value = null
if (node && remainder.length === 0) {
value = node.value
}
cb(err, value)
})
}
/**
* Stores a given `value` at the given `key`
* @method put
* @param {Buffer|String} key
* @param {Buffer|String} Value
* @param {Function} cb A callback `Function` which is given the argument `err` - for errors that may have occured
*/
Trie.prototype.put = function (key, value, cb) {
var self = this
key = ethUtil.toBuffer(key)
value = ethUtil.toBuffer(value)
if (!value || value.toString() === '') {
self.del(key, cb)
} else {
cb = callTogether(cb, self.sem.leave)
self.sem.take(function () {
if (self.root.toString('hex') !== ethUtil.SHA3_RLP.toString('hex')) {
// first try to find the give key or its nearst node
self._findPath(key, function (err, foundValue, keyRemainder, stack) {
if (err) {
return cb(err)
}
// then update
self._updateNode(key, value, keyRemainder, stack, cb)
})
} else {
self._createInitialNode(key, value, cb) // if no root initialize this trie
}
})
}
}
/**
* deletes a value given a `key`
* @method del
* @param {Buffer|String} key
* @param {Function} callback the callback `Function`
*/
Trie.prototype.del = function (key, cb) {
var self = this
key = ethUtil.toBuffer(key)
cb = callTogether(cb, self.sem.leave)
self.sem.take(function () {
self._findPath(key, function (err, foundValue, keyRemainder, stack) {
if (err) {
return cb(err)
}
if (foundValue) {
self._deleteNode(key, stack, cb)
} else {
cb()
}
})
})
}
/**
* Retrieves a raw value in the underlying db
* @method getRaw
* @param {Buffer} key
* @param {Function} callback A callback `Function`, which is given the arguments `err` - for errors that may have occured and `value` - the found value in a `Buffer` or if no value was found `null`.
*/
Trie.prototype.getRaw = function (key, cb) {
key = ethUtil.toBuffer(key)
function dbGet (db, cb2) {
db.get(key, {
keyEncoding: 'binary',
valueEncoding: 'binary'
}, function (err, foundNode) {
if (err || !foundNode) {
cb2(null, null)
} else {
cb2(null, foundNode)
}
})
}
asyncFirstSeries(this._getDBs, dbGet, cb)
}
// retrieves a node from dbs by hash
Trie.prototype._lookupNode = function (node, cb) {
if (TrieNode.isRawNode(node)) {
cb(new TrieNode(node))
} else {
this.getRaw(node, function (err, value) {
if (err) {
throw err
}
if (value) {
value = new TrieNode(rlp.decode(value))
}
cb(value)
})
}
}
// TODO: remove the proxy method when changing the caching
Trie.prototype._putRaw = function (key, val, cb) {
function dbPut (db, cb2) {
db.put(key, val, {
keyEncoding: 'binary',
valueEncoding: 'binary'
}, cb2)
}
async.each(this._putDBs, dbPut, cb)
}
/**
* Writes a value directly to the underlining db
* @method putRaw
* @param {Buffer|String} key The key as a `Buffer` or `String`
* @param {Buffer} value The value to be stored
* @param {Function} callback A callback `Function`, which is given the argument `err` - for errors that may have occured
*/
Trie.prototype.putRaw = Trie.prototype._putRaw
/**
* Removes a raw value in the underlying db
* @method delRaw
* @param {Buffer|String} key
* @param {Function} callback A callback `Function`, which is given the argument `err` - for errors that may have occured
*/
Trie.prototype.delRaw = function (key, cb) {
function del (db, cb2) {
db.del(key, {
keyEncoding: 'binary'
}, cb2)
}
async.each(this._putDBs, del, cb)
}
// writes a single node to dbs
Trie.prototype._putNode = function (node, cb) {
var hash = node.hash()
var serialized = node.serialize()
this._putRaw(hash, serialized, cb)
}
// writes many nodes to db
Trie.prototype._batchNodes = function (opStack, cb) {
function dbBatch (db, cb) {
db.batch(opStack, {
keyEncoding: 'binary',
valueEncoding: 'binary'
}, cb)
}
async.each(this._putDBs, dbBatch, cb)
}
/**
* Trys to find a path to the node for the given key
* It returns a `stack` of nodes to the closet node
* @method _findPath
* @param {String|Buffer} - key - the search key
* @param {Function} - cb - the callback function. Its is given the following
* arguments
* - err - any errors encontered
* - node - the last node found
* - keyRemainder - the remaining key nibbles not accounted for
* - stack - an array of nodes that forms the path to node we are searching for
*/
Trie.prototype._findPath = function (targetKey, cb) {
var self = this
var root = self.root
var stack = []
targetKey = TrieNode.stringToNibbles(targetKey)
this._walkTrie(root, processNode, cb)
function processNode (root, node, keyProgress, walkController) {
var nodeKey = node.key || []
var keyRemainder = targetKey.slice(matchingNibbleLength(keyProgress, targetKey))
var matchingLen = matchingNibbleLength(keyRemainder, nodeKey)
stack.push(node)
if (node.type === 'branch') {
if (keyRemainder.length === 0) {
walkController.return(null, node, [], stack)
// we exhausted the key without finding a node
} else {
var branchIndex = keyRemainder[0]
var branchNode = node.getValue(branchIndex)
if (!branchNode) {
// there are no more nodes to find and we didn't find the key
walkController.return(null, null, keyRemainder, stack)
} else {
// node found, continuing search
walkController.only(branchIndex)
}
}
} else if (node.type === 'leaf') {
if (doKeysMatch(keyRemainder, nodeKey)) {
// keys match, return node with empty key
walkController.return(null, node, [], stack)
} else {
// reached leaf but keys dont match
walkController.return(null, null, keyRemainder, stack)
}
} else if (node.type === 'extention') {
if (matchingLen !== nodeKey.length) {
// keys dont match, fail
walkController.return(null, null, keyRemainder, stack)
} else {
// keys match, continue search
walkController.next()
}
}
}
}
/*
* Finds all nodes that store k,v values
*/
Trie.prototype._findNode = function (key, root, stack, cb) {
this._findPath(key, function () {
cb.apply(null, arguments)
})
}
/*
* Finds all nodes that store k,v values
*/
Trie.prototype._findValueNodes = function (onFound, cb) {
this._walkTrie(this.root, function (root, node, key, walkController) {
var fullKey = key
if (node.key) {
fullKey = key.concat(node.key)
}
if (node.type === 'leaf') {
// found leaf node!
onFound(root, node, fullKey, walkController.next)
} else if (node.type === 'branch' && node.value) {
// found branch with value
onFound(root, node, fullKey, walkController.next)
} else {
// keep looking for value nodes
walkController.next()
}
}, cb)
}
/*
* Finds all nodes that are stored directly in the db
* (some nodes are stored raw inside other nodes)
*/
Trie.prototype._findDbNodes = function (onFound, cb) {
this._walkTrie(this.root, function (root, node, key, walkController) {
if (TrieNode.isRawNode(root)) {
walkController.next()
} else {
onFound(root, node, key, walkController.next)
}
}, cb)
}
/**
* Updates a node
* @method _updateNode
* @param {Buffer} key
* @param {Buffer| String} value
* @param {Array} keyRemainder
* @param {Array} stack -
* @param {Function} cb - the callback
*/
Trie.prototype._updateNode = function (key, value, keyRemainder, stack, cb) {
var toSave = []
var lastNode = stack.pop()
// add the new nodes
key = TrieNode.stringToNibbles(key)
if (lastNode.type === 'branch') {
stack.push(lastNode)
if (keyRemainder !== 0) {
// add an extention to a branch node
keyRemainder.shift()
// create a new leaf
var newLeaf = new TrieNode('leaf', keyRemainder, value)
stack.push(newLeaf)
} else {
lastNode.value = value
}
} else if (lastNode.type === 'leaf' && keyRemainder.length === 0) {
// just updating a found value
lastNode.value = value
stack.push(lastNode)
} else {
// create a branch node
var lastKey = lastNode.key
var matchingLength = matchingNibbleLength(lastKey, keyRemainder)
var newBranchNode = new TrieNode('branch')
// create a new extention node
if (matchingLength !== 0) {
var newKey = lastNode.key.slice(0, matchingLength)
var newExtNode = new TrieNode('extention', newKey, value)
stack.push(newExtNode)
lastKey.splice(0, matchingLength)
keyRemainder.splice(0, matchingLength)
}
stack.push(newBranchNode)
if (lastKey.length !== 0) {
var branchKey = lastKey.shift()
if (lastKey.length !== 0 || lastNode.type === 'leaf') {
// shriking extention or leaf
lastNode.key = lastKey
var formatedNode = this._formatNode(lastNode, false, toSave)
newBranchNode.setValue(branchKey, formatedNode)
} else {
// remove extention or attaching
this._formatNode(lastNode, false, true, toSave)
newBranchNode.setValue(branchKey, lastNode.value)
}
} else {
newBranchNode.value = lastNode.value
}
if (keyRemainder.length !== 0) {
keyRemainder.shift()
// add a leaf node to the new branch node
var newLeafNode = new TrieNode('leaf', keyRemainder, value)
stack.push(newLeafNode)
} else {
newBranchNode.value = value
}
}
this._saveStack(key, stack, toSave, cb)
}
// walk tree
Trie.prototype._walkTrie = function (root, onNode, onDone) {
var self = this
root = root || self.root
onDone = onDone || function () {}
var aborted = false
var returnValues = []
if (root.toString('hex') === ethUtil.SHA3_RLP.toString('hex')) {
return onDone()
}
self._lookupNode(root, function (node) {
processNode(root, node, null, function (err) {
if (err) {
return onDone(err)
}
onDone.apply(null, returnValues)
})
})
function processNode (root, node, key, cb) {
if (!node) return cb()
if (aborted) return cb()
var stopped = false
key = key || []
var walkController = {
stop: function () {
stopped = true
cb()
},
// end all traversal and return values to the onDone cb
return: function () {
aborted = true
returnValues = arguments
cb()
},
next: function () {
if (aborted) {
return cb()
}
if (stopped) {
return cb()
}
var children = node.getChildren()
async.forEachOf(children, function (data, index, cb) {
var keyExtension = data[0]
var childRoot = data[1]
var childKey = key.concat(keyExtension)
self._lookupNode(childRoot, function (node) {
processNode(childRoot, node, childKey, cb)
})
}, cb)
},
only: function (childIndex) {
var childRoot = node.getValue(childIndex)
self._lookupNode(childRoot, function (node) {
var childKey = key.slice()
childKey.push(childIndex)
processNode(childRoot, node, childKey, cb)
})
}
}
onNode(root, node, key, walkController)
}
}
/**
* saves a stack
* @method _saveStack
* @param {Array} key - the key. Should follow the stack
* @param {Array} stack - a stack of nodes to the value given by the key
* @param {Array} opStack - a stack of levelup operations to commit at the end of this funciton
* @param {Function} cb
*/
Trie.prototype._saveStack = function (key, stack, opStack, cb) {
var lastRoot
// update nodes
while (stack.length) {
var node = stack.pop()
if (node.type === 'leaf') {
key.splice(key.length - node.key.length)
} else if (node.type === 'extention') {
key.splice(key.length - node.key.length)
if (lastRoot) {
node.value = lastRoot
}
} else if (node.type === 'branch') {
if (lastRoot) {
var branchKey = key.pop()
node.setValue(branchKey, lastRoot)
}
}
lastRoot = this._formatNode(node, stack.length === 0, opStack)
}
if (lastRoot) {
this.root = lastRoot
}
this._batchNodes(opStack, cb)
}
Trie.prototype._deleteNode = function (key, stack, cb) {
function processBranchNode (key, branchKey, branchNode, parentNode, stack) {
// branchNode is the node ON the branch node not THE branch node
var branchNodeKey = branchNode.key
if (!parentNode || parentNode.type === 'branch') {
// branch->?
if (parentNode) {
stack.push(parentNode)
}
if (branchNode.type === 'branch') {
// create an extention node
// branch->extention->branch
var extentionNode = new TrieNode('extention', [branchKey], null)
stack.push(extentionNode)
key.push(branchKey)
} else {
// branch key is an extention or a leaf
// branch->(leaf or extention)
branchNodeKey.unshift(branchKey)
branchNode.key = branchNodeKey
// hackery. This is equvilant to array.concat except we need keep the
// rerfance to the `key` that was passed in.
branchNodeKey.unshift(0)
branchNodeKey.unshift(key.length)
key.splice.apply(key, branchNodeKey)
}
stack.push(branchNode)
} else {
// parent is a extention
var parentKey = parentNode.key
if (branchNode.type === 'branch') {
// ext->branch
parentKey.push(branchKey)
key.push(branchKey)
parentNode.key = parentKey
stack.push(parentNode)
} else {
// branch node is an leaf or extention and parent node is an exstention
// add two keys together
// dont push the parent node
branchNodeKey.unshift(branchKey)
key = key.concat(branchNodeKey)
parentKey = parentKey.concat(branchNodeKey)
branchNode.key = parentKey
}
stack.push(branchNode)
}
return key
}
var lastNode = stack.pop()
var parentNode = stack.pop()
var opStack = []
var self = this
if (!Array.isArray(key)) {
// convert key to nibbles
key = TrieNode.stringToNibbles(key)
}
if (!parentNode) {
// the root here has to be a leaf.
this.root = this.EMPTY_TRIE_ROOT
cb()
} else {
if (lastNode.type === 'branch') {
lastNode.value = null
} else {
// the lastNode has to be a leaf if its not a branch. And a leaf's parent
// if it has one must be a branch.
var lastNodeKey = lastNode.key
key.splice(key.length - lastNodeKey.length)
// delete the value
this._formatNode(lastNode, false, true, opStack)
parentNode.setValue(key.pop(), null)
lastNode = parentNode
parentNode = stack.pop()
}
// nodes on the branch
var branchNodes = []
// count the number of nodes on the branch
lastNode.raw.forEach(function (node, i) {
var val = lastNode.getValue(i)
if (val) branchNodes.push([i, val])
})
// if there is only one branch node left, collapse the branch node
if (branchNodes.length === 1) {
// add the one remaing branch node to node above it
var branchNode = branchNodes[0][1]
var branchNodeKey = branchNodes[0][0]
// look up node
this._lookupNode(branchNode, function (foundNode) {
key = processBranchNode(key, branchNodeKey, foundNode, parentNode, stack, opStack)
self._saveStack(key, stack, opStack, cb)
})
} else {
// simple removing a leaf and recaluclation the stack
if (parentNode) {
stack.push(parentNode)
}
stack.push(lastNode)
self._saveStack(key, stack, opStack, cb)
}
}
}
// Creates the initial node from an empty tree
Trie.prototype._createInitialNode = function (key, value, cb) {
var newNode = new TrieNode('leaf', key, value)
this.root = newNode.hash()
this._putNode(newNode, cb)
}
// formats node to be saved by levelup.batch.
// returns either the hash that will be used key or the rawNode
Trie.prototype._formatNode = function (node, topLevel, remove, opStack) {
if (arguments.length === 3) {
opStack = remove
remove = false
}
var rlpNode = node.serialize()
if (rlpNode.length >= 32 || topLevel) {
var hashRoot = node.hash()
if (remove && this.isCheckpoint) {
opStack.push({
type: 'del',
key: hashRoot
})
} else {
opStack.push({
type: 'put',
key: hashRoot,
value: rlpNode
})
}
return hashRoot
}
return node.raw
}
/**
* The `data` event is given an `Object` hat has two properties; the `key` and the `value`. Both should be Buffers.
* @method createReadStream
* @return {stream.Readable} Returns a [stream](https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_class_stream_readable) of the contents of the `trie`
*/
Trie.prototype.createReadStream = function () {
return new ReadStream(this)
}
// creates a new trie backed by the same db
// and starting at the same root
Trie.prototype.copy = function () {
return new Trie(this.db, this.root)
}
/**
* The given hash of operations (key additions or deletions) are executed on the DB
* @method batch
* @example
* var ops = [
* { type: 'del', key: 'father' }
* , { type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' }
* , { type: 'put', key: 'dob', value: '16 February 1941' }
* , { type: 'put', key: 'spouse', value: 'Kim Young-sook' }
* , { type: 'put', key: 'occupation', value: 'Clown' }
* ]
* trie.batch(ops)
* @param {Array} ops
* @param {Function} cb
*/
Trie.prototype.batch = function (ops, cb) {
var self = this
async.eachSeries(ops, function (op, cb2) {
if (op.type === 'put') {
self.put(op.key, op.value, cb2)
} else if (op.type === 'del') {
self.del(op.key, cb2)
} else {
cb2()
}
}, cb)
}
/**
* Checks if a given root exists
* @method checkRoot
* @param {Buffer} root
* @param {Function} cb
*/
Trie.prototype.checkRoot = function (root, cb) {
root = ethUtil.toBuffer(root)
this._lookupNode(root, function (value) {
cb(null, !!value)
})
}