-
Notifications
You must be signed in to change notification settings - Fork 60
/
blockchain.js
547 lines (469 loc) · 14.8 KB
/
blockchain.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
var EventEmitter = require('events').EventEmitter
var util = require('util')
var async = require('async')
var bitcore = require('bitcore-lib')
var Inventory = require('bitcore-p2p').Inventory
var BN = bitcore.crypto.BN
var constants = require('./constants.js')
var BlockStore = require('./blockStore.js')
var u = require('./utils.js')
if (process.browser) {
require('setimmediate')
}
function noop () {}
var TIME_MARGIN = 2 * 60 * 60
var storeClosedError = new Error('Store is closed')
var Blockchain = module.exports = function (opts) {
var self = this
opts = opts || {}
this.peerGroup = opts.peerGroup
this.network = bitcore.Networks.get(opts.network) || bitcore.Networks.defaultNetwork
var genesisHeader = opts.genesis || constants.genesisHeaders[this.network.name]
this.genesis = this.tip = {
height: 0,
hash: u.toHash(genesisHeader.hash),
header: genesisHeader
}
this.BlockHeader = genesisHeader.constructor
if (opts.checkpoint && !opts.to) {
this.checkpoint = opts.checkpoint
this.checkpoint.hash = u.toHash(this.checkpoint.header.hash)
this.tip = this.checkpoint
}
this.to = opts.to
this.initialized = false
this.closed = false
this.store = opts.store || new BlockStore({
path: opts.path,
BlockHeader: this.BlockHeader
})
this._initStore(function (err) {
if (err && err !== storeClosedError) return self._error(err)
else if (err && err === storeClosedError) return
self.initialized = true
self.emit('ready')
})
this.downloadPeer = null
this.syncing = false
this.initialized = false
this.syncHeight = 0
this.lastInv = null
this.interval = opts.interval || 2016
this.targetSpacing = opts.targetSpacing || 10 * 60
this.targetTimespan = this.interval * this.targetSpacing
this.maxTarget = opts.maxTarget || constants.maxTarget
this._onHeaders = this._onHeaders.bind(this)
this._onDownloadPeerDisconnect = this._onDownloadPeerDisconnect.bind(this)
}
util.inherits(Blockchain, EventEmitter)
Blockchain.prototype._initStore = function (cb) {
var self = this
function putIfNotFound (block) {
return function (cb) {
self.store.get(block.hash, function (err) {
if (err && !err.notFound) return cb(err)
if (self.closed || self.store.isClosed()) return cb(storeClosedError)
self.store.put(block, cb)
})
}
}
var tasks = [ putIfNotFound(this.genesis) ]
if (this.checkpoint) tasks.push(putIfNotFound(this.checkpoint))
async.parallel(tasks, cb)
}
Blockchain.prototype.waitForReady = function (cb) {
if (this.initialized) return cb()
this.on('ready', cb)
}
Blockchain.prototype.close = function (cb) {
var self = this
this.waitForReady(function () {
self.closed = true
self.syncing = false
self.store.close(cb)
})
}
Blockchain.prototype.sync = function (opts, cb) {
var self = this
cb = cb || noop
if (this.syncing) return cb(null)
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = opts || {}
if (cb) this.once('synced', cb)
this.syncing = true
if (opts.to || this.to) this.syncHeight = opts.to || this.to
if (opts.peer) this._setDownloadPeer(opts.peer)
var syncDone = function () {
var done = false
if (self.syncHeight) {
done = self.tip.height >= self.syncHeight
} else {
done = self.downloadPeer && self.tip.height >= self.downloadPeer.bestHeight
}
if (done) {
self.removeListener('sync', syncDone)
self.removeListener('syncing', syncDone)
self.syncing = false
self._setDownloadPeer(null)
self.emit('synced', self.tip)
self._listenForUpdates()
}
}
this.on('sync', syncDone)
this.on('syncing', syncDone)
var startDownload = function (err) {
if (err) {
err = new Error('Error initializing blockchain: ' + err)
if (cb) return cb(err)
else self.emit('error', err)
}
self._getHeaders()
}
if (!this.initialized) this._initialize(startDownload)
else startDownload(null)
}
Blockchain.prototype.getTip = function () {
return this.tip
}
Blockchain.prototype.getPath = function (from, to, cb) {
var self = this
var output = {
add: [],
remove: [],
fork: null
}
var top, bottom, down
if (from.height > to.height) {
top = from
bottom = to
down = true
} else {
top = to
bottom = from
down = false
}
function addTraversedBlock (block) {
if (down && block.header.hash !== to.header.hash) output.remove.push(block)
else if (!down && block.header.hash !== from.header.hash) output.add.unshift(block)
}
// traverse down from the higher block to the lower block
function traverseDown (err, block) {
if (err) return cb(err)
if (block.height === bottom.height) {
// we traversed down to the lower height
if (block.header.hash === bottom.header.hash) {
// the blocks are the same, there was no fork
addTraversedBlock(block)
return cb(null, output)
}
// the blocks are not the same, so we need to traverse down to find a fork
return traverseToFork(block, bottom)
}
addTraversedBlock(block)
self._get(block.header.prevHash, traverseDown)
}
traverseDown(null, top)
// traverse down from both blocks until we find one block that is the same
function traverseToFork (left, right) {
if (left.height === 0 || right.height === 0) {
// we got all the way to two different genesis blocks,
// the blocks don't have a path between thems
return cb(new Error('Blocks are not in the same chain'))
}
output.remove.push(down ? left : right)
output.add.unshift(down ? right : left)
self._get(left.header.prevHash, function (err, left) {
if (err) return cb(err)
self._get(right.header.prevHash, function (err, right) {
if (err) return cb(err)
if (left.header.hash === right.header.hash) {
output.fork = left
return cb(null, output)
}
traverseToFork(left, right)
})
})
}
}
Blockchain.prototype.getPathToTip = function (from, cb) {
this.getPath(from, this.tip, cb)
}
Blockchain.prototype.getBlockAtTime = function (time, opts, cb) {
var self = this
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = opts || {}
opts.margin = opts.margin != null ? opts.margin : TIME_MARGIN
var output = this.tip
function traverse (err, block) {
if (err) return cb(err)
if (block.header.time <= time - opts.margin) return cb(null, output)
if (block.header.time >= time) output = block
if (block.height === 0) return cb(null, output)
self._get(block.header.prevHash, traverse)
}
traverse(null, this.tip)
}
Blockchain.prototype.getBlockAtHeight = function (height, cb) {
var self = this
if (height > this.tip.height) return cb(new Error('height is higher than tip'))
if (height < 0) return cb(new Error('height must be >= 0'))
var down = height > this.tip.height / 2
function traverse (err, block) {
if (err) return cb(err)
if (block.height === height) return cb(null, block)
self._get(down ? block.header.prevHash : block.next, traverse)
}
this._get(down ? this.tip.hash : this.genesis.hash, traverse)
}
Blockchain.prototype.getBlock = function (at, cb) {
// hash
if (Buffer.isBuffer(at)) return this._get(at, cb)
if (typeof at === 'number') {
// height
if (at < constants.timestampThreshold) {
return this.getBlockAtHeight(at, cb)
}
// timestamp
return this.getBlockAtTime(at, cb)
}
return cb(new Error('"at" must be a block hash, height, or timestamp'))
}
Blockchain.prototype.getLocator = function (from, cb) {
this.getBlock(from, function (err, block) {
if (err) return cb(err)
// TODO: include some previous blocks in case we are on a fork
return cb(null, [ u.toHash(block.header.hash) ])
})
}
Blockchain.prototype._error = function (err) {
this.emit('error', err)
}
Blockchain.prototype._initialize = function (cb) {
cb = cb || noop
if (this.initialized) return cb(null)
var self = this
this.store.getTip(function (err, tip) {
self.initialized = true
if (err && err.name === 'NotFoundError') return cb(null)
if (err) return cb(err)
self.tip = tip
cb(null, tip)
})
}
Blockchain.prototype._findDownloadPeer = function (cb) {
var self = this
cb = cb || noop
if (this.peerGroup.peers.length === 0) {
this.peerGroup.once('peer', function () {
self._findDownloadPeer(cb)
})
return
}
var peer = this.peerGroup.randomPeer()
this._setDownloadPeer(peer)
return cb(null, peer)
}
Blockchain.prototype._setDownloadPeer = function (peer) {
if (this.downloadPeer) {
this.downloadPeer.removeListener('headers', this._onHeaders)
this.downloadPeer.removeListener('disconnect', this._onDownloadPeerDisconnect)
}
this.downloadPeer = peer
this.syncing = !!peer
if (peer) {
peer.on('headers', this._onHeaders)
peer.on('disconnect', this._onDownloadPeerDisconnect)
this.emit('syncing', peer)
}
}
Blockchain.prototype._getHeaders = function (opts) {
var self = this
if (!this.syncing) return
if (!this.downloadPeer) {
this._findDownloadPeer(function (err, peer) {
if (err) return self._error(err)
self._getHeaders(opts)
})
return
}
opts = opts || {}
this.getLocator(opts.from || this.tip.hash, function (err, locator) {
if (err) return self._error(err)
if (!self.downloadPeer) return
var message = self.downloadPeer.messages.GetHeaders({
starts: locator
})
self.downloadPeer.sendMessage(message)
})
}
Blockchain.prototype._onHeaders = function (message) {
var self = this
if (message.headers.length === 0) return
this.processHeaders(message.headers, function (err, last) {
if (err) {
return self._error(new Error('Peer sent invalid headers: ' + err.message))
}
if (self.syncing) {
self.emit('sync', last)
self._getHeaders()
}
})
}
Blockchain.prototype._onDownloadPeerDisconnect = function () {
this.downloadPeer = null
if (this.syncing) this._getHeaders()
}
Blockchain.prototype._listenForUpdates = function () {
var self = this
this.peerGroup.on('inv', function (message, peer) {
message.inventory.forEach(function (item) {
if (item.type === Inventory.TYPE.BLOCK ||
item.type === Inventory.TYPE.FILTERED_BLOCK) {
if (self.lastInv && self.lastInv.compare(item.hash) === 0) return
self.lastInv = item.hash
self.sync({ peer: peer })
}
})
})
}
Blockchain.prototype._get = function (hash, cb) {
var self = this
if (!this.initialized) {
this.once('ready', function () { self._get(hash, cb) })
return
}
this.store.get(hash, cb)
}
Blockchain.prototype._put = function (hash, opts, cb) {
var self = this
if (!this.initialized) {
this.once('ready', function () { self._put(hash, opts, cb) })
return
}
this.store.put(hash, opts, cb)
}
Blockchain.prototype.processHeaders = function (headers, cb) {
var self = this
var previousTip = this.tip
this._get(headers[0].prevHash, function (err, start) {
if (err && err.name === 'NotFoundError') return cb(new Error('Block does not connect to chain'))
if (err) return cb(err)
start.hash = u.toHash(start.header.hash)
if (self.syncHeight && start.height + headers.length > self.syncHeight) {
headers = headers.slice(0, start.height + headers.length - self.syncHeight)
}
async.reduce(headers, start, self.processHeader.bind(self), function (err, last) {
if (err) return cb(err, last)
if (last.height > previousTip.height) {
self.getPath(previousTip, last, function (err, path) {
if (err) return cb(err, last)
if (path.remove.length > 0) {
var first = { height: start.height + 1, header: headers[0] }
self.store.put(first, { best: true, prev: start }, function (err) {
if (err) return cb(err)
self.emit('reorg', { remove: path.remove, tip: last })
cb(null, last)
})
return
}
cb(null, last)
})
return
}
cb(null, last)
})
})
}
Blockchain.prototype.processHeader = function (prev, header, cb) {
if (!cb) cb = typeof header === 'function' ? header : cb
if (prev instanceof bitcore.BlockHeader) {
header = prev
prev = this.tip
}
var self = this
var height = prev.height + 1
var block = {
height: height,
hash: u.toHash(header.hash),
header: header
}
if (header.prevHash.compare(prev.hash) !== 0) {
return cb(new Error('Block does not connect to previous'), block)
}
// TODO: testnet difficulty rules (for now we are just not verifying testnet block difficulty)
if (this.network !== bitcore.Networks.testnet &&
!self.shouldRetarget(height) &&
header.bits !== prev.header.bits) {
return cb(new Error('Unexpected difficulty change'), block)
}
if (!header.validProofOfWork()) {
return cb(new Error('Invalid proof of work'), block)
}
// TODO: other checks (timestamp, version)
if (self.shouldRetarget(height) &&
// don't verify retarget if it requires checking before our checkpoint
!(this.checkpoint && height - this.checkpoint.height < this.interval)) {
return self.calculateTarget(block, function (err, target) {
if (err) return cb(err, block)
var expected = u.toCompactTarget(target)
if (expected !== header.bits) {
return cb(new Error('Bits in block (' + header.bits.toString(16) + ')' +
' is different than expected (' + expected.toString(16) + ')'))
}
put()
})
}
put()
function put () {
var tip = height > self.tip.height
self._put({ header: header, height: height }, { tip: tip, prev: prev }, function (err) {
if (err) return cb(err)
if (tip) {
self.tip = block
block.syncing = self.syncing
self.emit('block', block)
}
cb(null, block)
})
}
}
Blockchain.prototype.shouldRetarget = function (height) {
return !(height % this.interval)
}
Blockchain.prototype.calculateTarget = function (block, cb) {
var self = this
var endBlock = null
var startBlock = null
function calculate () {
var timespan = endBlock.header.time - startBlock.header.time
timespan = Math.max(timespan, self.targetTimespan / 4)
timespan = Math.min(timespan, self.targetTimespan * 4)
var target = endBlock.header.getTargetDifficulty()
target.imul(new BN(timespan))
target.idivn(new BN(self.targetTimespan))
if (target.cmp(self.maxTarget) === 1) {
target = self.maxTarget
}
return cb(null, target)
}
var i = 0
function traverse (block) {
self.store.get(block.header.prevHash, function (err, prev) {
if (err) return cb(err)
if (i === 0) endBlock = prev
i++
if (i === self.interval) {
startBlock = prev
return calculate()
}
setImmediate(function () { traverse(prev) })
})
}
traverse(block)
}