forked from ethereumjs/merkle-patricia-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrieNode.js
255 lines (227 loc) · 5.04 KB
/
trieNode.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
const rlp = require('rlp')
const ethUtil = require('ethereumjs-util')
module.exports = TrieNode
function TrieNode (type, key, value) {
if (Array.isArray(type)) {
// parse raw node
this.parseNode(type)
} else {
this.type = type
if (type === 'branch') {
var values = key
this.raw = Array.apply(null, Array(17))
if (values) {
values.forEach(function (keyVal) {
this.set.apply(this, keyVal)
})
}
} else {
this.raw = Array(2)
this.setValue(value)
this.setKey(key)
}
}
}
TrieNode.isRawNode = isRawNode
TrieNode.addHexPrefix = addHexPrefix
TrieNode.removeHexPrefix = removeHexPrefix
TrieNode.isTerminator = isTerminator
TrieNode.stringToNibbles = stringToNibbles
TrieNode.nibblesToBuffer = nibblesToBuffer
TrieNode.getNodeType = getNodeType
Object.defineProperty(TrieNode.prototype, 'value', {
get: function () {
return this.getValue()
},
set: function (v) {
this.setValue(v)
}
})
Object.defineProperty(TrieNode.prototype, 'key', {
get: function () {
return this.getKey()
},
set: function (k) {
this.setKey(k)
}
})
// parses a raw node
TrieNode.prototype.parseNode = function (rawNode) {
this.raw = rawNode
this.type = getNodeType(rawNode)
}
// sets the value of the node
TrieNode.prototype.setValue = function (key, value) {
if (this.type !== 'branch') {
this.raw[1] = key
} else {
if (arguments.length === 1) {
value = key
key = 16
}
this.raw[key] = value
}
}
TrieNode.prototype.getValue = function (key) {
if (this.type === 'branch') {
if (arguments.length === 0) {
key = 16
}
var val = this.raw[key]
if (val !== null && val !== undefined && val.length !== 0) {
return val
}
} else {
return this.raw[1]
}
}
TrieNode.prototype.setKey = function (key) {
if (this.type !== 'branch') {
if (Buffer.isBuffer(key)) {
key = stringToNibbles(key)
} else {
key = key.slice(0) // copy the key
}
key = addHexPrefix(key, this.type === 'leaf')
this.raw[0] = nibblesToBuffer(key)
}
}
// returns the key as a nibble
TrieNode.prototype.getKey = function () {
if (this.type !== 'branch') {
var key = this.raw[0]
key = removeHexPrefix(stringToNibbles(key))
return (key)
}
}
TrieNode.prototype.serialize = function () {
return rlp.encode(this.raw)
}
TrieNode.prototype.hash = function () {
return ethUtil.sha3(this.serialize())
}
TrieNode.prototype.toString = function () {
var out = this.type
out += ': ['
this.raw.forEach(function (el) {
if (Buffer.isBuffer(el)) {
out += el.toString('hex') + ', '
} else if (el) {
out += 'object, '
} else {
out += 'empty, '
}
})
out = out.slice(0, -2)
out += ']'
return out
}
TrieNode.prototype.getChildren = function () {
var children = []
switch (this.type) {
case 'leaf':
// no children
break
case 'extention':
// one child
children.push([this.key, this.getValue()])
break
case 'branch':
for (var index = 0, end = 16; index < end; index++) {
var value = this.getValue(index)
if (value) {
children.push([
[index], value
])
}
}
break
}
return children
}
/**
* @param {Array} dataArr
* @returns {Buffer} - returns buffer of encoded data
* hexPrefix
**/
function addHexPrefix (key, terminator) {
// odd
if (key.length % 2) {
key.unshift(1)
} else {
// even
key.unshift(0)
key.unshift(0)
}
if (terminator) {
key[0] += 2
}
return key
}
function removeHexPrefix (val) {
if (val[0] % 2) {
val = val.slice(1)
} else {
val = val.slice(2)
}
return val
}
/**
* Determines if a key has Arnold Schwarzenegger in it.
* @method isTerminator
* @param {Array} key - an hexprefixed array of nibbles
*/
function isTerminator (key) {
return key[0] > 1
}
/**
* Converts a string OR a buffer to a nibble array.
* @method stringToNibbles
* @param {Buffer| String} key
*/
function stringToNibbles (key) {
var bkey = new Buffer(key)
var nibbles = []
for (var i = 0; i < bkey.length; i++) {
var q = i * 2
nibbles[q] = bkey[i] >> 4
++q
nibbles[q] = bkey[i] % 16
}
return nibbles
}
/**
* Converts a nibble array into a buffer.
* @method nibblesToBuffer
* @param arr
*/
function nibblesToBuffer (arr) {
var buf = new Buffer(arr.length / 2)
for (var i = 0; i < buf.length; i++) {
var q = i * 2
buf[i] = (arr[q] << 4) + arr[++q]
}
return buf
}
/**
* Determines the node type.
* @returns {String} - the node type
* - leaf - if the node is a leaf
* - branch - if the node is a branch
* - extention - if the node is an extention
* - unknown - if something else got borked
*/
function getNodeType (node) {
if (node.length === 17) {
return 'branch'
} else if (node.length === 2) {
var key = stringToNibbles(node[0])
if (isTerminator(key)) {
return 'leaf'
}
return 'extention'
}
}
function isRawNode (node) {
return Array.isArray(node) && !Buffer.isBuffer(node)
}