This repository has been archived by the owner on Jun 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
leveldb-sum-tree.js
372 lines (348 loc) · 12.1 KB
/
leveldb-sum-tree.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
const log = require('debug')('info:leveldb-sum-tree')
const web3 = require('web3')
const BN = web3.utils.BN
const COIN_ID_BYTE_SIZE = require('../constants.js').COIN_ID_BYTE_SIZE
const BLOCK_TX_PREFIX = require('../constants.js').BLOCK_TX_PREFIX
const BLOCK_INDEX_PREFIX = require('../constants.js').BLOCK_INDEX_PREFIX
const NUM_LEVELS_PREFIX = require('../constants.js').BLOCK_TX_PREFIX
const NODE_DB_PREFIX = require('../constants.js').NODE_DB_PREFIX
const HASH_TO_TX_PREFIX = require('../constants.js').HASH_TO_TX_PREFIX
const models = require('plasma-utils').serialization.models
const SignedTransaction = models.SignedTransaction
const UnsignedTransaction = models.UnsignedTransaction
const Transfer = models.Transfer
const itNext = require('../utils.js').itNext
const itEnd = require('../utils.js').itEnd
const sha3 = require('../utils.js').sha3
const Decimal = require('decimal.js-light')
const INDEX_BYTES_SIZE = 4
function coinIdToBuffer(coinId) {
return coinId.toArrayLike(Buffer, 'big', COIN_ID_BYTE_SIZE)
}
class LevelDBSumTree {
constructor(db) {
this.db = db
}
async generateTree(blockNumber) {
const numLeaves = await this.parseLeaves(blockNumber)
let heightOfTree
if (numLeaves === undefined) {
heightOfTree = 0
} else {
// TODO: Replace this and instead detect heightOfTree in generateLevel
heightOfTree = Math.ceil(
new Decimal(numLeaves.toString(10)).log(2).toNumber()
)
}
const rootHash = await this.generateLevel(blockNumber, 0, heightOfTree)
log(
'Generating tree for block:',
blockNumber.toString('hex'),
'with root:',
Buffer.from(rootHash).toString('hex')
)
return rootHash
}
getTransactionFromLeaf(value) {
const index = value[0]
const encoding = value.slice(1)
const transaction = new SignedTransaction(encoding.toString('hex'))
transaction.trIndex = index
transaction.encoding = encoding
return transaction
}
getUnsignedTransaction(tx) {
const unsignedTx = new UnsignedTransaction({
block: tx.block,
transfers: tx.transfers,
})
return unsignedTx
}
/**
* Parses the leaves to generate the zero'th level of our sum tree
*/
async parseLeaves(blockNumber) {
const self = this
return new Promise(async (resolve, reject) => {
// Helper functions for getting properties of our transactions
const getTr = (tx) => new Transfer(tx.transfers[tx.trIndex])
const typedStart = (tr) =>
new BN(tr.token.toString(16, 8) + tr.start.toString(16, 24), 16)
// Store the min and max values which can exist for any range. This will be used as the bounds of our stream
const minStart = Buffer.from('0'.repeat(COIN_ID_BYTE_SIZE * 2), 'hex')
const maxEnd = Buffer.from('f'.repeat(COIN_ID_BYTE_SIZE * 2), 'hex')
// Store the prefix which all our transactions should have
const blockTxPrefix = Buffer.concat([BLOCK_TX_PREFIX, blockNumber])
// We need special logic to handle the first leaf / transaction. Because of this, look it up independently.
const firstLeaf = await this.getNearest(
Buffer.concat([blockTxPrefix, minStart])
)
// Check if this block is empty -- if the nearest
if (
firstLeaf.key === undefined ||
!firstLeaf.key.slice(0, blockTxPrefix.length).equals(blockTxPrefix)
) {
// This block appears to be empty! Return early
resolve()
return
}
const firstTransaction = this.getTransactionFromLeaf(firstLeaf.value)
// Now set the prev tx's sum start to *zero* instead of what it normally is--the previous transaction's start
let previousTransaction = firstTransaction
previousTransaction.sumStart = new BN(0)
let previousTxIndex = new BN(0)
// Read all remaining leaves, computing hash and setting sum value
const firstTxStart = coinIdToBuffer(typedStart(getTr(firstTransaction))) // Store the first start as we will use it for our next seek
this.db
.createReadStream({
gt: Buffer.concat([BLOCK_TX_PREFIX, blockNumber, firstTxStart]),
lt: Buffer.concat([BLOCK_TX_PREFIX, blockNumber, maxEnd]),
})
.on('data', function(data) {
const transaction = self.getTransactionFromLeaf(data.value)
transaction.sumStart = typedStart(getTr(transaction))
const range = coinIdToBuffer(
transaction.sumStart.sub(previousTransaction.sumStart)
)
const prevTxEncodedBuf = Buffer.from(
self.getUnsignedTransaction(previousTransaction).encoded,
'hex'
)
const prevTxHash = sha3(prevTxEncodedBuf)
self.writeNode(blockNumber, 0, previousTxIndex, prevTxHash, range)
self.writeTrToIndex(
blockNumber,
Buffer.from(getTr(previousTransaction).encoded, 'hex'),
previousTxIndex
)
self.writeHashToTransaction(prevTxHash, prevTxEncodedBuf)
previousTxIndex = previousTxIndex.add(new BN(1))
previousTransaction = transaction
})
.on('end', function(data) {
const range = coinIdToBuffer(
new BN(maxEnd).sub(previousTransaction.sumStart)
)
const prevTxEncodedBuf = Buffer.from(
self.getUnsignedTransaction(previousTransaction).encoded,
'hex'
)
const prevTxHash = sha3(prevTxEncodedBuf)
self.writeNode(blockNumber, 0, previousTxIndex, prevTxHash, range)
self.writeTrToIndex(
blockNumber,
Buffer.from(getTr(previousTransaction).encoded, 'hex'),
previousTxIndex
)
self.writeHashToTransaction(prevTxHash, prevTxEncodedBuf)
// Return the total number of leaves
resolve(previousTxIndex.addn(1))
})
.on('error', function(err) {
reject(err)
})
})
}
async getNearest(key) {
const it = this.db.iterator({
gte: key,
limit: 1,
})
const result = await itNext(it)
await itEnd(it)
return result
}
async getHeight(blockNumber) {
const height = await this.get(
Buffer.concat([blockNumber, Buffer.from('height')])
)
return height
}
async getNode(blockNumber, level, index) {
const node = await this.db.get(this.makeNodeKey(blockNumber, level, index))
return node
}
async getIndex(blockNumber, trEncoding) {
const index = await this.db.get(
this.makeTrToIndexKey(blockNumber, trEncoding)
)
return index
}
parseNodeValue(value) {
return {
hash: value.slice(0, 32),
sum: new BN(value.slice(32)),
}
}
makeNodeKey(blockNumber, level, index) {
return Buffer.concat([
Buffer.from(NODE_DB_PREFIX),
blockNumber,
this.makeIndexId(level, index),
])
}
makeTrToIndexKey(blockNumber, trEncoding) {
return Buffer.concat([BLOCK_INDEX_PREFIX, blockNumber, trEncoding])
}
async writeNode(blockNumber, level, index, hash, sum) {
const newNodeKey = this.makeNodeKey(blockNumber, level, index)
log(
'Writing new node\nKey:',
newNodeKey.toString('hex'),
'\nValue:',
Buffer.concat([Buffer.from(hash), sum]).toString('hex')
)
await this.db.put(newNodeKey, Buffer.concat([Buffer.from(hash), sum]))
}
async writeHashToTransaction(txHash, prevTxEncodedBuf) {
const newHashToTransactionKey = Buffer.concat([
Buffer.from(HASH_TO_TX_PREFIX),
txHash,
])
log(
'Writing hash -> encoding\nKey:',
newHashToTransactionKey.toString('hex'),
'\nValue:',
prevTxEncodedBuf.toString('hex')
)
await this.db.put(newHashToTransactionKey, prevTxEncodedBuf)
}
async writeTrToIndex(blockNumber, trEncoding, index) {
const newTrKey = this.makeTrToIndexKey(blockNumber, trEncoding)
const indexBuff = index.toArrayLike(Buffer, 'big', INDEX_BYTES_SIZE)
log(
'Writing new tr -> index\nKey:',
newTrKey.toString('hex'),
'\nValue:',
indexBuff.toString('hex')
)
await this.db.put(newTrKey, indexBuff)
}
async writeNumLevels(blockNumber, numLevels) {
log(
'Writing num levels for block:',
Buffer.concat([NUM_LEVELS_PREFIX, blockNumber]),
'\nWith value:',
Buffer.from([numLevels])
)
await this.db.put(
Buffer.concat([NUM_LEVELS_PREFIX, blockNumber]),
Buffer.from([numLevels])
)
}
async getNumLevels(blockNumber) {
const numLevels = await this.db.get(
Buffer.concat([NUM_LEVELS_PREFIX, blockNumber])
)
return new BN(numLevels)
}
makeIndexId(level, index) {
return Buffer.concat([
Buffer.from([level]),
index.toArrayLike(Buffer, 'big', INDEX_BYTES_SIZE),
])
}
emptyNode() {
const emptyHash = Buffer.from('0'.repeat(64), 'hex')
const emptySum = new BN(0)
return {
hash: emptyHash,
sum: emptySum,
}
}
getParent(left, right) {
const leftSum = left.sum.toArrayLike(Buffer, 'big', 16)
const rightSum = right.sum.toArrayLike(Buffer, 'big', 16)
const parentHash = sha3(
Buffer.concat([left.hash, leftSum, right.hash, rightSum])
)
return {
hash: parentHash,
sum: left.sum
.add(right.sum)
.toArrayLike(Buffer, 'big', COIN_ID_BYTE_SIZE),
}
}
async generateLevel(blockNumber, level, height) {
log(
'Starting to generate level:',
level,
'for block:',
blockNumber.toString('hex')
)
const self = this
const parentLevel = level + 1
// Check that there is at least one node at this level--if not it might be an empty block
try {
await this.getNode(blockNumber, level, new BN(0))
} catch (err) {
// No node found! Is this an empty block?
return this.emptyNode().hash
}
return new Promise((resolve, reject) => {
// Create readstream for all nodes at the previous level
const maxEnd = new BN('f'.repeat(INDEX_BYTES_SIZE * 2), 16)
const readStream = this.db.createReadStream({
gte: this.makeNodeKey(blockNumber, level, new BN(0)),
lte: this.makeNodeKey(blockNumber, level, maxEnd),
})
// Go through every node at this level and build the next level's nodes
let leftChild = null
let numChildren = new BN(0)
let parentIndex = new BN(0)
let parentNode
readStream
.on('data', (data) => {
log('Processing child:', data.key.toString('hex'))
numChildren = numChildren.add(new BN(1))
// If this is the left child store it and move on
if (leftChild === null) {
leftChild = this.parseNodeValue(data.value)
return
}
// Now we have the left and right children. Let's hash and compute the next sum
const rightChild = this.parseNodeValue(data.value)
parentNode = this.getParent(leftChild, rightChild)
self.writeNode(
blockNumber,
parentLevel,
parentIndex,
parentNode.hash,
parentNode.sum
)
parentIndex = parentIndex.add(new BN(1))
leftChild = null
})
.on('end', async () => {
// If level equals height, we have reached the root node.
if (level === height) {
log(
'Returning root hash:',
Buffer.from(leftChild.hash).toString('hex')
)
await self.writeNumLevels(blockNumber, level)
resolve(leftChild.hash)
return
}
// Check if we ended on an element that wasn't a right node. If so fill it in with a blank node
if (leftChild !== null) {
log('Filling in an odd length level with a zero node')
const rightChild = this.emptyNode()
const parentNode = this.getParent(leftChild, rightChild)
self.writeNode(
blockNumber,
parentLevel,
parentIndex,
parentNode.hash,
parentNode.sum
)
}
resolve(await self.generateLevel(blockNumber, parentLevel, height))
})
.on('error', (err) => {
reject(err)
})
})
}
}
module.exports = LevelDBSumTree