-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
743 lines (647 loc) · 17.5 KB
/
index.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
735
736
737
738
739
740
741
742
743
'use strict'
/**
* NOTES
* block details are child block hash, parent block hash, isUncle, isProcessed, and total difficulty
* block details are stored to key 'detail:'+<blockhash>
* meta.rawHead is the the head of the chain with the most POW
* meta.head is the head of the chain that has had its state root verifed
*/
const async = require('async')
const Stoplight = require('flow-stoplight')
const semaphore = require('semaphore')
const levelup = require('levelup')
const memdown = require('memdown')
const Block = require('vaporyjs-block')
const vapUtil = require('vaporyjs-util')
const Vapash = require('vapashjs')
const Buffer = require('safe-buffer').Buffer
const BN = vapUtil.BN
const rlp = vapUtil.rlp
module.exports = Blockchain
function Blockchain (opts) {
opts = opts || {}
const self = this
// TODO: SET LOCK
// defaults
self.blockDb = opts.blockDb ? opts.blockDb : levelup('', { db: memdown })
self.detailsDb = opts.detailsDb ? opts.detailsDb : levelup('', { db: memdown })
self.validate = (opts.validate === undefined ? true : opts.validate)
self.vapash = self.validate ? new Vapash(self.detailsDb) : null
self.meta = null
self._initDone = false
self._putSemaphore = semaphore(1)
self._initLock = new Stoplight()
self._init(function (err) {
if (err) throw err
self._initLock.go()
})
}
/**
* Fetches the meta info about the blockchain from the db. Meta info contains
* the hash of the head block and the hash of the genisis block
* @method _init
*/
Blockchain.prototype._init = function (cb) {
const self = this
self.detailsDb.get('meta', {
valueEncoding: 'json'
}, onHeadFound)
function onHeadFound (err, meta) {
// look up failure
if (err || !meta) {
// generate default meta + genesis
self.meta = {
heads: {},
td: new BN()
}
self._setCanonicalGenesisBlock(cb)
return
}
// look up success
self.meta = meta
self.meta.td = new BN(meta.td)
cb()
}
}
/**
* Sets the default genesis block
* @method _setCanonicalGenesisBlock
*/
Blockchain.prototype._setCanonicalGenesisBlock = function (cb) {
const self = this
var genesisBlock = new Block()
genesisBlock.setGenesisParams()
self._putBlock(genesisBlock, cb, true)
}
/**
* Puts the genesis block in the database
* @method putGenesis
*/
Blockchain.prototype.putGenesis = function (genesis, cb) {
const self = this
self.putBlock(genesis, cb, true)
}
/**
* Returns that head block
* @method getHead
* @param cb Function the callback
*/
Blockchain.prototype.getHead = function (name, cb) {
const self = this
// handle optional args
if (typeof name === 'function') {
cb = name
name = 'vm'
}
// ensure init completed
self._initLock.await(function runGetHead () {
// if the head is not found return the rawHead
var hash = self.meta.heads[name] || self.meta.rawHead
if (!hash) {
return cb(new Error('No head found.'))
}
self.getBlock(Buffer.from(hash, 'hex'), cb)
})
}
/**
* Adds many blocks to the blockchain
* @method putBlocks
* @param {array} blocks - the blocks to be added to the blockchain
* @param {function} cb - a callback function
*/
Blockchain.prototype.putBlocks = function (blocks, cb) {
const self = this
async.eachSeries(blocks, function (block, done) {
self.putBlock(block, done)
}, cb)
}
/**
* Adds a block to the blockchain
* @method putBlock
* @param {object} block -the block to be added to the block chain
* @param {function} cb - a callback function
* @param {function} isGenesis - a flag for indicating if the block is the genesis block
*/
Blockchain.prototype.putBlock = function (block, cb, isGenesis) {
const self = this
// make sure init has completed
self._initLock.await(() => {
// perform put with mutex dance
lockUnlock(function (done) {
self._putBlock(block, done, isGenesis)
}, cb)
})
// lock, call fn, unlock
function lockUnlock (fn, cb) {
// take lock
self._putSemaphore.take(function () {
// call fn
fn(function () {
// leave lock
self._putSemaphore.leave()
// exit
cb.apply(null, arguments)
})
})
}
}
Blockchain.prototype._putBlock = function (block, cb, isGenesis) {
const self = this
var blockHash = block.hash()
var blockHashHexString = blockHash.toString('hex')
var parentDetails
var dbOps = []
if (block.constructor !== Block) {
block = new Block(block)
}
async.series([
verify,
verifyPOW,
lookupParentBlock,
rebuildInfo,
(cb) => self._batchDbOps(dbOps, cb)
], cb)
function verify (next) {
if (!self.validate) return next()
if (!isGenesis && block.isGenesis()) {
return next(new Error('already have genesis set'))
}
block.validate(self, next)
}
function verifyPOW (next) {
if (!self.validate) return next()
self.vapash.verifyPOW(block, function (valid) {
next(valid ? null : new Error('invalid POW'))
})
}
// look up the parent meta info
function lookupParentBlock (next) {
// if genesis block
if (isGenesis) return next()
self.getDetails(block.header.parentHash, function (err, _parentDetails) {
parentDetails = _parentDetails
if (!err && parentDetails) {
next()
} else {
let parentHash = vapUtil.bufferToHex(block.header.parentHash.toString('hex'))
next(new Error(`parent hash not found: ${parentHash}`))
}
})
}
function rebuildInfo (next) {
// calculate the total difficulty for this block
var totalDifficulty = new BN(vapUtil.bufferToInt(block.header.difficulty))
// add this block as a child to the parent's block details
if (!isGenesis) {
totalDifficulty.iadd(new BN(parentDetails.td))
parentDetails.staleChildren.push(blockHashHexString)
}
// store the block details
var blockDetails = {
parent: block.header.parentHash.toString('hex'),
td: totalDifficulty.toString(),
number: vapUtil.bufferToInt(block.header.number),
child: null,
staleChildren: [],
genesis: block.isGenesis()
}
dbOps.push({
db: 'details',
type: 'put',
key: 'detail:' + blockHashHexString,
valueEncoding: 'json',
value: blockDetails
})
// store the block
if (!Buffer.isBuffer(blockHash)) console.trace()
dbOps.push({
db: 'block',
type: 'put',
key: blockHash,
keyEncoding: 'binary',
valueEncoding: 'binary',
value: block.serialize()
})
// need to update totalDifficulty
if (block.isGenesis() || totalDifficulty.cmp(self.meta.td) === 1) {
blockDetails.inChain = true
self.meta.rawHead = blockHashHexString
self.meta.height = vapUtil.bufferToInt(block.header.number)
self.meta.td = totalDifficulty
// blockNumber as decimal string
const blockNumber = parseInt(block.header.number.toString('hex') || '00', 16).toString()
// index by number
dbOps.push({
db: 'details',
type: 'put',
key: blockNumber,
valueEncoding: 'binary',
value: blockHash
})
// save meta
dbOps.push({
db: 'details',
type: 'put',
key: 'meta',
valueEncoding: 'json',
value: self.meta
})
if (block.isGenesis()) {
self.meta.genesis = blockHashHexString
next()
} else {
self._rebuildBlockchain(blockHashHexString, block.header.parentHash, parentDetails, dbOps, next)
}
} else {
dbOps.push({
db: 'details',
type: 'put',
key: 'detail:' + block.header.parentHash.toString('hex'),
valueEncoding: 'json',
value: parentDetails
})
next()
}
}
}
/**
*Gets a block by its hash
* @method getBlock
* @param {String|Buffer|Number} hash - the sha256 hash of the rlp encoding of the block
* @param {Function} cb - the callback function
*/
Blockchain.prototype.getBlock = function (blockTag, cb) {
const self = this
// determine BlockTag type
if (Buffer.isBuffer(blockTag)) {
lookupByHash(blockTag, cb)
} else if (Number.isInteger(blockTag)) {
async.waterfall([
(cb) => lookupNumberToHash(blockTag, cb),
(blockHash, cb) => lookupByHash(blockHash, cb)
], cb)
} else {
cb(new Error('Unknown blockTag type'))
}
function lookupByHash (hash, cb) {
self.blockDb.get(hash, {
keyEncoding: 'binary',
valueEncoding: 'binary'
}, (err, encodedBlock) => {
if (err) return cb(err)
let block = new Block(rlp.decode(encodedBlock))
cb(null, block)
})
}
function lookupNumberToHash (hexString, cb) {
self.detailsDb.get(hexString, {
valueEncoding: 'binary'
}, cb)
}
}
/**
* Looks up many blocks relative to blockId
* @method getBlocks
* @param {Buffer|Number} blockId - the block's hash or number
* @param {Number} skip - number of blocks to skip
* @param {Bool} reverse - fetch blocks in reverse
* @param {Function} cb - the callback function
*/
Blockchain.prototype.getBlocks = function (blockId, maxBlocks, skip, reverse, cb) {
const self = this
var blocks = []
var i = -1
function nextBlock (blockId) {
self.getBlock(blockId, function (err, block) {
i++
// TODO: only abort happily if the error is a "block not found" error
if (err) {
return cb(null, blocks)
}
var nextBlockNumber = vapUtil.bufferToInt(block.header.number) + (reverse ? -1 : 1)
if (i !== 0 && skip && i % (skip + 1) !== 0) {
return nextBlock(nextBlockNumber)
}
blocks.push(block)
if (blocks.length === maxBlocks) {
return cb(null, blocks)
}
nextBlock(nextBlockNumber)
})
}
nextBlock(blockId)
}
/**
* Gets a block by its hash
* @method getBlockInfo
* @param {String} hash - the sha256 hash of the rlp encoding of the block
* @param {Function} cb - the callback function
*/
Blockchain.prototype.getDetails = function (hash, cb) {
const self = this
self.detailsDb.get('detail:' + hash.toString('hex'), {
valueEncoding: 'json'
}, cb)
}
/**
* Gets a block by its hash
* @method getBlockInfo
* @param {String} hash - the sha256 hash of the rlp encoding of the block
* @param {Function} cb - the callback function
*/
Blockchain.prototype.putDetails = function (hash, val, cb) {
const self = this
self.detailsDb.put('detail:' + hash.toString('hex'), val, {
valueEncoding: 'json'
}, cb)
}
/**
* Given an ordered array, returns to the callback an array of hashes that are
* not in the blockchain yet
* @method selectNeededHashes
* @param {Array} hashes
* @param {function} cb the callback
*/
Blockchain.prototype.selectNeededHashes = function (hashes, cb) {
const self = this
var max, mid, min
max = hashes.length - 1
mid = min = 0
async.whilst(function test () {
return max >= min
},
function iterate (cb2) {
self.getBlockInfo(hashes[mid], function (err, hash) {
if (!err && hash) {
min = mid + 1
} else {
max = mid - 1
}
mid = Math.floor((min + max) / 2)
cb2()
})
},
function onDone (err) {
if (err) return cb(err)
cb(null, hashes.slice(min))
})
}
Blockchain.prototype._saveMeta = function (cb) {
const self = this
self.detailsDb.put('meta', self.meta, {
keyEncoding: 'json'
}, cb)
}
// builds the chain double link list from the head to the tail.
Blockchain.prototype._rebuildBlockchain = function (hash, parentHash, parentDetails, ops, cb) {
const self = this
var ppDetails, staleHash
parentHash = parentHash.toString('hex')
var i = parentDetails.staleChildren.indexOf(hash)
if (i !== -1) {
parentDetails.staleChildren.splice(i, 1)
}
if (parentDetails.child && parentDetails.child !== hash) {
parentDetails.staleChildren.push(parentDetails.child)
}
parentDetails.child = hash
ops.push({
db: 'details',
type: 'put',
key: 'detail:' + parentHash.toString('hex'),
valueEncoding: 'json',
value: parentDetails
})
// exit early if parent is in chain
if (parentDetails.inChain) {
cb()
return
}
parentDetails.inChain = true
async.series([
loadNumberIndex,
loadStaleDetails,
getNextDetails
], function (err) {
if (err) return cb(err)
self._rebuildBlockchain(parentHash, parentDetails.parent, ppDetails, ops, cb)
})
function loadNumberIndex (done) {
self.detailsDb.get(parentDetails.number, {
valueEncoding: 'binary'
}, function (err, _staleHash) {
if (err) return done(err)
staleHash = _staleHash
done()
})
}
function loadStaleDetails (done) {
if (!staleHash) {
done()
return
}
self.getDetails(staleHash, function (err, staleDetails) {
if (err) return done(err)
staleDetails.inChain = false
// reindex the block number
ops.push({
db: 'details',
type: 'put',
valueEncoding: 'binary',
key: staleDetails.number,
value: Buffer.from(parentHash, 'hex')
})
ops.push({
db: 'details',
type: 'put',
key: 'detail:' + staleHash.toString('hex'),
value: staleDetails,
valueEncoding: 'json'
})
done()
})
}
function getNextDetails (done) {
self.getDetails(parentDetails.parent, function (err, d) {
if (err) return done(err)
ppDetails = d
done()
})
}
}
// todo add SEMIPHORE; the semiphore
// also this doesn't reset the heads
Blockchain.prototype.delBlock = function (blockhash, cb) {
const self = this
var dbOps = []
var resetHeads = []
if (!Buffer.isBuffer(blockhash)) {
blockhash = blockhash.hash()
}
async.series([
buildDBops,
getLastDeletesDetils,
(cb) => self._batchDbOps(dbOps, cb)
], cb)
function buildDBops (cb2) {
self._delBlock(blockhash, dbOps, resetHeads, cb2)
}
function getLastDeletesDetils (cb2) {
self.getDetails(blockhash.toString('hex'), function (err, details) {
if (details.inChain) {
self.meta.rawHead = details.parent
}
resetHeads.forEach(function (head) {
self.meta.heads[head] = details.parent
})
cb2(err)
})
}
}
Blockchain.prototype._delBlock = function (blockhash, dbOps, resetHeads, cb) {
const self = this
var details
dbOps.push({
db: 'details',
type: 'del',
key: 'detail:' + blockhash.toString('hex')
})
// delete the block
dbOps.push({
db: 'block',
type: 'del',
key: blockhash.toString('hex')
})
async.series([
getDetails,
removeChild,
removeStaleChildren
], cb)
function getDetails (cb2) {
self.getDetails(blockhash, function (err, d) {
for (var head in self.meta.heads) {
if (blockhash.toString('hex') === self.meta.heads[head]) {
resetHeads.push(head)
}
}
details = d
cb2(err)
})
}
function removeChild (cb2) {
if (details.child) {
self._delBlock(details.child, dbOps, resetHeads, cb2)
} else {
cb2()
}
}
function removeStaleChildren (cb2) {
if (details.staleChildren) {
async.each(details.staleChildern, function (child, cb3) {
self._delBlock(child, dbOps, resetHeads, cb3)
}, function (err) {
cb2(err, details)
})
} else {
cb2(null, details)
}
}
}
Blockchain.prototype.iterator = function (name, onBlock, cb) {
const self = this
// ensure init completed
self._initLock.await(function () {
self._iterator(name, onBlock, cb)
})
}
Blockchain.prototype._iterator = function (name, func, cb) {
const self = this
var blockhash = self.meta.heads[name] || self.meta.genesis
var lastBlock
if (!blockhash) {
return cb()
}
self.getDetails(blockhash, function (err, d) {
if (err) cb(err)
blockhash = d.child
async.whilst(function () {
return blockhash
}, run, function () {
self._saveMeta(cb)
})
})
function run (cb2) {
var details, block
async.series([
getDetails,
getBlock,
runFunc,
saveDetails
], function (err) {
if (!err) {
blockhash = details.child
} else {
blockhash = false
}
cb2(err)
})
function getDetails (cb3) {
self.getDetails(blockhash, function (err, d) {
details = d
if (d) {
self.meta.heads[name] = blockhash
}
cb3(err)
})
}
function getBlock (cb3) {
self.getBlock(Buffer.from(blockhash, 'hex'), function (err, b) {
block = b
cb3(err)
})
}
function runFunc (cb3) {
var reorg = lastBlock ? lastBlock.hash().toString('hex') !== block.header.parentHash.toString('hex') : false
lastBlock = block
func(block, reorg, cb3)
}
function saveDetails (cb3) {
details[name] = true
var dbOps = [{
db: 'details',
type: 'put',
key: 'detail:' + blockhash.toString('hex'),
value: details,
valueEncoding: 'json'
}, {
db: 'details',
type: 'put',
key: 'meta',
valueEncoding: 'json',
value: self.meta
}]
self._batchDbOps(dbOps, cb3)
}
}
}
Blockchain.prototype._batchDbOps = function (dbOps, cb) {
const self = this
let blockDbOps = []
let detailsDbOps = []
dbOps.forEach((op) => {
switch (op.db) {
case 'block':
blockDbOps.push(op)
break
case 'details':
detailsDbOps.push(op)
break
default:
return cb(new Error('DB op did not specify known db:', op))
}
})
async.parallel([
(cb) => self.blockDb.batch(blockDbOps, cb),
(cb) => self.detailsDb.batch(detailsDbOps, cb)
], cb)
}