Skip to content

Commit 6ef872a

Browse files
committed
feat(Blockchain): allow custom BlockHeader constructor
This can be used for blockchains that have a different block format, for example the alpha sidechain.
1 parent 33aec0d commit 6ef872a

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

lib/blockStore.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ var BlockStore = module.exports = function (opts, cb) {
2626
}
2727
opts = opts || {}
2828

29+
this.BlockHeader = opts.BlockHeader || bitcore.BlockHeader
30+
2931
this.store = opts.store || u.createStore({
3032
path: opts.path,
3133
db: opts.db,
@@ -44,7 +46,7 @@ BlockStore.prototype.put = function (block, opts, cb) {
4446
}
4547
if (block.height == null) return cb(new Error('Must specify height'))
4648
if (block.header == null) return cb(new Error('Must specify header'))
47-
if (!(block.header instanceof bitcore.BlockHeader)) {
49+
if (!(block.header instanceof this.BlockHeader)) {
4850
return cb(new Error('Header must be instance of BlockHeader'))
4951
}
5052
if (opts.tip) opts.best = true
@@ -81,10 +83,11 @@ BlockStore.prototype.get = function (hash, cb) {
8183
return cb(err)
8284
}
8385

86+
var self = this
8487
this.store.get(key, function (err, block) {
8588
if (err) return cb(err)
8689
var header = new Buffer(block.header, 'base64')
87-
block.header = bitcore.BlockHeader.fromBuffer(header)
90+
block.header = self.BlockHeader.fromBuffer(header)
8891
cb(null, block)
8992
})
9093
}

lib/blockchain.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var Blockchain = module.exports = function (opts) {
3030
header: genesisHeader
3131
}
3232

33+
this.BlockHeader = genesisHeader.constructor
34+
3335
if (opts.checkpoint) {
3436
this.checkpoint = opts.checkpoint
3537
this.checkpoint.hash = u.toHash(this.checkpoint.header.hash)
@@ -39,7 +41,10 @@ var Blockchain = module.exports = function (opts) {
3941
this.initialized = false
4042
this.closed = false
4143

42-
this.store = opts.store || new BlockStore({ path: opts.path })
44+
this.store = opts.store || new BlockStore({
45+
path: opts.path,
46+
BlockHeader: this.BlockHeader
47+
})
4348
this._initStore(function (err) {
4449
if (err && err !== storeClosedError) return self._error(err)
4550
else if (err && err === storeClosedError) return
@@ -358,8 +363,8 @@ Blockchain.prototype._listenForUpdates = function () {
358363
var self = this
359364
this.peerGroup.on('inv', function (peer, message) {
360365
message.inventory.forEach(function (item) {
361-
if (item.type === Inventory.TYPE.BLOCK
362-
|| item.type === Inventory.TYPE.FILTERED_BLOCK) {
366+
if (item.type === Inventory.TYPE.BLOCK ||
367+
item.type === Inventory.TYPE.FILTERED_BLOCK) {
363368
if (self.lastInv && self.lastInv.compare(item.hash) === 0) return
364369
self.lastInv = item.hash
365370
self._getHeaders()
@@ -444,18 +449,18 @@ Blockchain.prototype.processHeader = function (prev, header, cb) {
444449
return cb(new Error('Block does not connect to previous'), block)
445450
}
446451
// TODO: testnet difficulty rules (for now we are just not verifying testnet block difficulty)
447-
if (this.network !== bitcore.Networks.testnet
448-
&& !self.shouldRetarget(height)
449-
&& header.bits !== prev.header.bits) {
452+
if (this.network !== bitcore.Networks.testnet &&
453+
!self.shouldRetarget(height) &&
454+
header.bits !== prev.header.bits) {
450455
return cb(new Error('Unexpected difficulty change'), block)
451456
}
452457
if (!header.validProofOfWork()) {
453458
return cb(new Error('Invalid proof of work'), block)
454459
}
455460
// TODO: other checks (timestamp, version)
456-
if (self.shouldRetarget(height)
461+
if (self.shouldRetarget(height) &&
457462
// don't verify retarget if it requires checking before our checkpoint
458-
&& !(this.checkpoint && height - this.checkpoint.height < this.interval)) {
463+
!(this.checkpoint && height - this.checkpoint.height < this.interval)) {
459464
return self.calculateTarget(block, function (err, target) {
460465
if (err) return cb(err, block)
461466

0 commit comments

Comments
 (0)