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
/
block-store.js
592 lines (561 loc) · 18.4 KB
/
block-store.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
const fs = require('fs')
const log = require('debug')('info:block-store')
const BN = require('web3').utils.BN
const makeBlockTxKey = require('../utils.js').makeBlockTxKey
const LevelDBSumTree = require('./leveldb-sum-tree.js')
const models = require('plasma-utils').serialization.models
const UnsignedTransaction = models.UnsignedTransaction
const SignedTransaction = models.SignedTransaction
const Transfer = models.Transfer
const TransactionProof = models.TransactionProof
const BLOCK_TX_PREFIX = require('../constants.js').BLOCK_TX_PREFIX
const BLOCK_DEPOSIT_PREFIX = require('../constants.js').BLOCK_DEPOSIT_PREFIX
const BLOCK_ROOT_HASH_PREFIX = require('../constants.js').BLOCK_ROOT_HASH_PREFIX
const BLOCK_NUM_TXS_PREFIX = require('../constants.js').BLOCK_NUM_TXS_PREFIX
const BLOCK_TIMESTAMP_PREFIX = require('../constants.js').BLOCK_TIMESTAMP_PREFIX
const HASH_TO_TX_PREFIX = require('../constants.js').HASH_TO_TX_PREFIX
const BLOCKNUMBER_BYTE_SIZE = require('../constants.js').BLOCKNUMBER_BYTE_SIZE
const TRANSFER_BYTE_SIZE = require('../constants.js').TRANSFER_BYTE_SIZE
const SIGNATURE_BYTE_SIZE = require('../constants.js').SIGNATURE_BYTE_SIZE
const itNext = require('../utils.js').itNext
const itEnd = require('../utils.js').itEnd
const defer = require('../utils.js').defer
const getCoinId = require('../utils.js').getCoinId
/* ******** HELPER FUNCTIONS ********** */
function getHexStringProof(proof) {
// TODO: Remove this and instead support buffers by default
let inclusionProof = []
for (const sibling of proof) {
inclusionProof.push(
sibling.hash.toString('hex') + sibling.sum.toString('hex', 32)
)
}
return inclusionProof
}
function makeDepositKey(type, start) {
return Buffer.concat([BLOCK_DEPOSIT_PREFIX, getCoinId(type, start)])
}
class BlockStore {
constructor(db, txLogDir) {
log('Creating new block store')
this.db = db
this.sumTree = new LevelDBSumTree(this.db)
this.txLogDir = txLogDir
this.partialChunk = null
this.batchPromises = []
this.blockNumberBN = new BN(0) // Set block number to be -1 so that the first block is block 0
this.newBlockQueue = []
this.lastBlockNumTxs = new BN(0)
}
async addBlock(txLogFile) {
log('Adding new block:', txLogFile)
const deferred = defer()
this.newBlockQueue.push({ txLogFile, resolve: deferred.resolve })
if (this.newBlockQueue.length === 1) {
this._processNewBlockQueue()
}
return deferred.promise
}
async addDeposit(depositTx) {
log(
'Adding new deposit for',
depositTx.tr.recipient,
'with start:',
depositTx.tr.start.toString(16),
'and end:',
depositTx.tr.end.toString(16)
)
// Store the deposit in our database in a similar way that we store normal transactions, by start position
await this.db.put(
makeDepositKey(depositTx.tr.token, depositTx.tr.start),
Buffer.from(depositTx.encoded, 'hex')
)
}
async getRootHash(blockNumberBN) {
const blockNumber = blockNumberBN.toArrayLike(
Buffer,
'big',
BLOCKNUMBER_BYTE_SIZE
)
const rootHash = await this.db.get(
Buffer.concat([BLOCK_ROOT_HASH_PREFIX, blockNumber])
)
return rootHash
}
async getBlockMetadata(startBlockBN, endBlockBN) {
let blockNumberBN = new BN(startBlockBN)
const metadata = []
for (
blockNumberBN;
blockNumberBN.lte(endBlockBN);
blockNumberBN = blockNumberBN.addn(1)
) {
const blockNumber = blockNumberBN.toArrayLike(
Buffer,
'big',
BLOCKNUMBER_BYTE_SIZE
)
try {
const rootHash = await this.db.get(
Buffer.concat([BLOCK_ROOT_HASH_PREFIX, blockNumber])
)
const timestamp = await this.db.get(
Buffer.concat([BLOCK_TIMESTAMP_PREFIX, blockNumber])
)
const numTxs = await this.db.get(
Buffer.concat([BLOCK_NUM_TXS_PREFIX, blockNumber])
)
metadata.push({
blockNumber,
rootHash,
timestamp,
numTxs,
})
} catch (err) {}
}
return metadata
}
async getTxFromHash(txHash) {
let txEncoding
try {
txEncoding = await this.db.get(Buffer.concat([HASH_TO_TX_PREFIX, txHash]))
} catch (err) {
return { error: 'Tx hash: ' + txHash.toString('hex') + ' not found!' }
}
return txEncoding
}
async _processNewBlockQueue() {
let numBlocksProcessed
for (
numBlocksProcessed = 0;
numBlocksProcessed < this.newBlockQueue.length;
numBlocksProcessed++
) {
this.newBlockQueue[
numBlocksProcessed
].newBlock = await this._processBlock(
this.newBlockQueue[numBlocksProcessed].txLogFile
)
}
const processedBlocks = this.newBlockQueue.splice(0, numBlocksProcessed)
for (const processedBlock of processedBlocks) {
processedBlock.resolve(processedBlock.newBlock)
}
}
async _processBlock(txLogFile) {
const blockNumberBN = new BN(txLogFile)
const blockNumber = blockNumberBN.toArrayLike(
Buffer,
'big',
BLOCKNUMBER_BYTE_SIZE
)
if (!this.blockNumberBN.add(new BN(1)).eq(blockNumberBN)) {
// TODO: After launch figure out why I was even storing block number in the first place....? Either store in DB or remove it.
log(
'Warning: Expected block number to be ' +
this.blockNumberBN.add(new BN(1)).toString() +
' not ' +
blockNumberBN.toString()
)
}
await this.ingestBlock(blockNumber, this.txLogDir + txLogFile)
const rootHash = await this.sumTree.generateTree(blockNumber)
// Set block metadata for the block explorer api
this.db.put(Buffer.concat([BLOCK_ROOT_HASH_PREFIX, blockNumber]), rootHash)
this.db.put(
Buffer.concat([BLOCK_TIMESTAMP_PREFIX, blockNumber]),
new BN(Date.now()).toArrayLike(Buffer, 'big')
)
this.db.put(
Buffer.concat([BLOCK_NUM_TXS_PREFIX, blockNumber]),
this.lastBlockNumTxs.toArrayLike(Buffer, 'big')
) // lastBlockNumTxs computed during ingestion
this.blockNumberBN = this.blockNumberBN.addn(1)
log(
'Adding block number:',
blockNumberBN.toString(),
'with root hash:',
Buffer.from(rootHash).toString('hex')
)
return {
blockNumber,
rootHash,
}
}
/*
* History proof logic
*/
async getLeavesAt(blockNumber, token, start, end, maxLeaves) {
let startKey
let endKey
if (token === 'none') {
startKey = start.toArrayLike(Buffer, 'big', 16)
endKey = end.toArrayLike(Buffer, 'big', 16)
} else {
startKey = makeBlockTxKey(blockNumber, token, start)
endKey = makeBlockTxKey(blockNumber, token, end)
}
if (maxLeaves === undefined) {
maxLeaves = 50000
}
const it = this.db.iterator({
lt: endKey,
reverse: true,
})
let result = await this._getNextBlockTx(it)
const ranges = [result]
// Make sure that we returned values that we expect
while (result.key > startKey) {
result = await this._getNextBlockTx(it)
ranges.push(result)
if (ranges.length > maxLeaves) {
await itEnd(it)
return ranges
}
}
await itEnd(it)
return ranges
}
async _getNextBlockTx(it) {
const result = await itNext(it)
if (result.key === undefined) {
await itEnd(it)
throw new Error('getLeavesAt iterator returned undefined!')
}
if (result.key[0] !== BLOCK_TX_PREFIX[0]) {
await itEnd(it)
throw new Error('Expected BLOCK_TX_PREFIX instead of ' + result.key[0])
}
return result
}
async getDepositsAt(token, start, end) {
const startKey = makeDepositKey(token, start)
const endKey = makeDepositKey(token, end)
const it = this.db.iterator({
lt: endKey,
reverse: true,
})
let [result, deposit] = await this._getNextDeposit(it)
const deposits = [deposit]
let earliestBlock = new BN(deposit.block)
// Make sure that we returned values that we expect
while (result.key > startKey) {
;[result, deposit] = await this._getNextDeposit(it)
deposits.push(deposit)
if (earliestBlock.gt(deposit.block)) {
earliestBlock = new BN(deposit.block)
}
}
await itEnd(it)
return [deposits, earliestBlock]
}
async _getNextDeposit(it) {
const result = await itNext(it)
if (result.key === undefined) {
await itEnd(it)
throw new Error('getDepositsAt iterator returned undefined!')
}
if (result.key[0] !== BLOCK_DEPOSIT_PREFIX[0]) {
await itEnd(it)
throw new Error(
'Expected BLOCK_DEPOSIT_PREFIX instead of ' + result.key[0]
)
}
const deposit = new UnsignedTransaction(result.value.toString('hex'))
return [result, deposit]
}
async getTransactions(
startBlockNumberBN,
endBlockNumberBN,
token,
start,
end,
maxTransactions
) {
let blockNumberBN = startBlockNumberBN
const relevantTransactions = []
while (blockNumberBN.lte(endBlockNumberBN)) {
const blockNumberKey = blockNumberBN.toArrayLike(
Buffer,
'big',
BLOCKNUMBER_BYTE_SIZE
)
const ranges = await this.getLeavesAt(
blockNumberKey,
token,
start,
end,
maxTransactions
)
for (const r of ranges) {
const tx = await this.sumTree.getTransactionFromLeaf(r.value)
relevantTransactions.push(tx)
}
if (
maxTransactions !== undefined &&
relevantTransactions.length > maxTransactions
) {
return relevantTransactions
}
blockNumberBN = blockNumberBN.add(new BN(1))
}
return relevantTransactions
}
async getTxsWithProofsFor(blockNumber, token, start, end) {
const numLevels = await this.sumTree.getNumLevels(blockNumber)
const leaves = await this.getLeavesAt(blockNumber, token, start, end)
const txProofs = []
for (const leaf of leaves) {
const transaction = this.sumTree.getTransactionFromLeaf(leaf.value)
const transactionProof = await this.getTransactionInclusionProof(
transaction,
blockNumber,
numLevels
)
txProofs.push({
transaction,
transactionProof,
})
}
return txProofs
}
async getTxsWithProofs(
startBlockNumberBN,
endBlockNumberBN,
token,
start,
end
) {
let blockNumberBN = startBlockNumberBN
const transactionProofs = {}
while (blockNumberBN.lte(endBlockNumberBN)) {
const blockNumberKey = blockNumberBN.toArrayLike(
Buffer,
'big',
BLOCKNUMBER_BYTE_SIZE
)
const proofs = await this.getTxsWithProofsFor(
blockNumberKey,
token,
start,
end
)
transactionProofs[blockNumberBN.toString()] = proofs
blockNumberBN = blockNumberBN.add(new BN(1))
}
return transactionProofs
}
async getTxHistory(startBlockNumberBN, endBlockNumberBN, transaction) {
let blockNumberBN = startBlockNumberBN.eqn(0)
? new BN(1)
: startBlockNumberBN
// First get all of the deposits for each transaction
let deposits = []
const earliestBlocks = []
for (const transfer of transaction.transfers) {
const [transferDeposits, earliestBlock] = await this.getDepositsAt(
transfer.token,
transfer.start,
transfer.end
)
// Get the earliest deposit & set the transfer `earliestDeposit` field to it
deposits = deposits.concat(transferDeposits)
earliestBlocks.push(earliestBlock)
}
const transactionHistory = {}
while (blockNumberBN.lte(endBlockNumberBN)) {
const rootHash = await this.getRootHash(blockNumberBN)
if (new BN(rootHash).eq(new BN(0))) {
blockNumberBN = blockNumberBN.addn(1)
continue
}
let proofs = []
for (const [i, transfer] of transaction.transfers.entries()) {
if (blockNumberBN.lte(earliestBlocks[i])) {
log('Reached deposit end for transfer', i)
// Stop retrieving proofs if we have reached the block which got the deposit for the transaction
continue
}
// For each one of the transfer records get the proofs
const blockNumberKey = blockNumberBN.toArrayLike(
Buffer,
'big',
BLOCKNUMBER_BYTE_SIZE
)
const rangeTxProof = await this.getTxsWithProofsFor(
blockNumberKey,
transfer.token,
transfer.start,
transfer.end
)
proofs = proofs.concat(rangeTxProof)
}
transactionHistory[blockNumberBN.toString()] = proofs
blockNumberBN = blockNumberBN.add(new BN(1))
}
return {
deposits,
transactionHistory,
}
}
async getTransactionInclusionProof(transaction, blockNumber, numLevels) {
const getTr = (tx, trIndex) => new Transfer(tx.transfers[trIndex])
const transferProofs = []
// For all transfers in our transaction, get transfer proof
for (let i = 0; i < transaction.transfers.length; i++) {
// First we need the index in the merkle sum tree of this leaf
const trEncoding = Buffer.from(getTr(transaction, i).encoded, 'hex')
const leafIndex = await this.sumTree.getIndex(blockNumber, trEncoding)
// Now get the transfer inclusion proof
const inclusionProof = await this.getTransferInclusionProof(
blockNumber,
numLevels,
new BN(leafIndex)
)
const trProof = {
parsedSum: new BN(inclusionProof.includedNode.sum),
transaction: transaction,
leafIndex,
signature: transaction.signatures[i],
inclusionProof: getHexStringProof(inclusionProof.proof),
}
// Add it to our transaction proof
transferProofs.push(trProof)
}
return new TransactionProof({ transferProofs })
}
async getTransferInclusionProof(blockNumber, numLevels, index) {
const proof = []
// Included node
const includedNodeValue = await this.sumTree.getNode(blockNumber, 0, index)
const includedNode = this.sumTree.parseNodeValue(includedNodeValue)
log(
'Included node hash:',
includedNode.hash.toString('hex'),
'--sum:',
includedNode.sum.toString(16)
)
let parentIndex
let nodeValue
let node
let siblingIndex = index.addn(index.mod(new BN(2)).eq(new BN(0)) ? 1 : -1)
for (let i = 0; i < numLevels; i++) {
try {
nodeValue = await this.sumTree.getNode(blockNumber, i, siblingIndex)
node = this.sumTree.parseNodeValue(nodeValue)
} catch (err) {
if (err.type === 'NotFoundError') {
log('Node not found in block tree! Treating it as an empty leaf...')
nodeValue = undefined
node = this.sumTree.emptyNode()
} else throw err
}
proof.push({
hash: node.hash,
sum: node.sum,
})
// Figure out the parent and then figure out the parent's sibling.
parentIndex = siblingIndex.eq(new BN(0))
? new BN(0)
: siblingIndex.divn(2)
siblingIndex = parentIndex.addn(
parentIndex.mod(new BN(2)).eq(new BN(0)) ? 1 : -1
)
}
return {
includedNode,
proof,
}
}
/*
* Block ingestion logic
*/
ingestBlock(blockNumber, txLogFilePath) {
const self = this
log('Generating new block for block:', blockNumber)
// First set the total number of txs for the latest block to zero. This just metadata for the block
this.lastBlockNumTxs = new BN(0)
const readStream = fs.createReadStream(txLogFilePath)
let chunkNumber = 0
readStream.on('data', function(chunk) {
log('Read chunk of size:', chunk.length)
self.parseTxBinary(blockNumber, chunk, chunkNumber)
chunkNumber++
})
// Return a promise which resolves once the entire file has been read
return new Promise((resolve, reject) => {
readStream.on('end', (res) => {
log('Finished reading all chunks')
Promise.all(this.batchPromises).then(() => {
log('Finished ingesting & sorting all chunks')
resolve()
})
})
})
}
readNextTransaction(cursor, chunk) {
const numElements = new BN(chunk.slice(cursor + 4, cursor + 5)).toNumber()
const transferSize = numElements * TRANSFER_BYTE_SIZE
const signatureSize = numElements * SIGNATURE_BYTE_SIZE
const txSize = BLOCKNUMBER_BYTE_SIZE + transferSize + signatureSize + 2 // We have two length identifiers, so plus 2
// Check if this transaction is the very last in our chunk
if (cursor + txSize > chunk.length) {
// Set partial tx
this.partialChunk = chunk.slice(cursor)
return [null]
}
const txStart = cursor
const txEnd = txStart + txSize
// Make the transaction object
const nextTransaction = new SignedTransaction(
chunk.slice(txStart, txEnd).toString('hex')
)
log('Read new transaction')
this.lastBlockNumTxs = this.lastBlockNumTxs.addn(1) // Increment the total number of txs which we've added
return [cursor + txSize, nextTransaction, chunk.slice(cursor, txEnd)]
}
parseTxBinary(blockNumber, chunk, chunkNumber) {
if (this.partialChunk != null) {
chunk = Buffer.concat([this.partialChunk, chunk])
}
const txBundle = []
let [cursor, nextTx, nextTxEncoding] = this.readNextTransaction(0, chunk)
while (cursor !== null) {
txBundle.push([nextTx, nextTxEncoding])
;[cursor, nextTx, nextTxEncoding] = this.readNextTransaction(
cursor,
chunk
)
}
// Leftmost transfer of the first transaction in a block MUST start at zero.
// We should probably get rid of this later on.
if (chunkNumber === 0 && txBundle.length > 0) {
const leftmost = txBundle[0][0].transfers
.sort((a, b) => {
return a.token.sub(b.token)
})
.reduce((prev, curr) => {
return prev.start.lt(curr.start) ? prev : curr
})
leftmost.token = new BN(0)
leftmost.start = new BN(0)
}
this.storeTransactions(blockNumber, txBundle)
}
storeTransactions(blockNumber, txBundle) {
// Ingest these transactions, into levelDB as `blocknum + typedStart +
const dbBatch = []
for (const tx of txBundle) {
for (const [i, tr] of tx[0].transfers.entries()) {
log('Storing tx at:', makeBlockTxKey(blockNumber, tr.token, tr.start))
dbBatch.push({
type: 'put',
key: makeBlockTxKey(blockNumber, tr.token, tr.start),
value: Buffer.concat([Buffer.from([i]), Buffer.from(tx[1])]), // Store as index of the TR & then transaction
})
}
}
this.batchPromises.push(this.db.batch(dbBatch))
}
}
module.exports = BlockStore