This commit is contained in:
Chris Kleeschulte 2017-07-18 10:04:22 -04:00
parent 5a6d8760b7
commit 8365f358e7
2 changed files with 41 additions and 3 deletions

View File

@ -399,9 +399,44 @@ AddressService.prototype._startSubscriptions = function() {
} }
this._bus.on('block/block', this._onBlock.bind(this)); this._bus.on('block/block', this._onBlock.bind(this));
this._bus.on('block/reorg', this._onReorg.bind(this));
this._bus.subscribe('block/reorg');
this._bus.subscribe('block/block'); this._bus.subscribe('block/block');
}; };
AddressService.prototype._onReorg = function(oldBlockList, newBlockList, commonAncestor) {
// if the common ancestor block height is greater than our own, then nothing to do for the reorg
if (this._tip.height <= commonAncestor.header.height) {
return;
}
// set the tip to the common ancestor in case something goes wrong with the reorg
var tipOps = utils.encodeTip({ hash: commonAncestor.hash, height: commonAncestor.header.height }, this.name);
var removalOps = [{
type: 'put',
key: tipOps.key,
value: tipOps.value
}];
// remove all the old blocks that we reorg from
oldBlockList.forEach(function(block) {
removalOps.push([
{
type: 'del',
key: this.encoding.encodeTransactionKey(),
},
]);
});
this._db.batch(removalOps);
//call onBlock for each of the new blocks
newBlockList.forEach(this._onBlock.bind(this));
};
AddressService.prototype._onBlock = function(block) { AddressService.prototype._onBlock = function(block) {
var self = this; var self = this;

View File

@ -176,7 +176,7 @@ TransactionService.prototype._onReorg = function(oldBlockList, newBlockList, com
} }
// set the tip to the common ancestor in case something goes wrong with the reorg // set the tip to the common ancestor in case something goes wrong with the reorg
var tipOps = utils.encodeTip({ hash: commonAncestor.hash, height: commonAncestor.header.height }); var tipOps = utils.encodeTip({ hash: commonAncestor.hash, height: commonAncestor.header.height }, this.name);
var removalOps = [{ var removalOps = [{
type: 'put', type: 'put',
@ -189,7 +189,7 @@ TransactionService.prototype._onReorg = function(oldBlockList, newBlockList, com
removalOps.concat([ removalOps.concat([
{ {
type: 'del', type: 'del',
key: this.encoding.encodeAddressIndexKey(), key: this.encoding.encodeAddressIndexKey(block.hash),
}, },
{ {
type: 'del', type: 'del',
@ -246,8 +246,11 @@ TransactionService.prototype._startSubscriptions = function() {
this._bus = this.node.openBus({remoteAddress: 'localhost'}); this._bus = this.node.openBus({remoteAddress: 'localhost'});
} }
this._bus.on('block/block', this._onTransaction.bind(this)); this._bus.on('block/block', this._onBlock.bind(this));
this._bus.on('block/reorg', this._onReorg.bind(this));
this._bus.subscribe('block/block'); this._bus.subscribe('block/block');
this._bus.subscribe('block/reorg');
}; };
module.exports = TransactionService; module.exports = TransactionService;