From 8365f358e7c004b74961420be936d37534601089 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Tue, 18 Jul 2017 10:04:22 -0400 Subject: [PATCH] wip --- lib/services/address/index.js | 35 +++++++++++++++++++++++++++++++ lib/services/transaction/index.js | 9 +++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/services/address/index.js b/lib/services/address/index.js index fe29aa5e..eac977d3 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -399,9 +399,44 @@ AddressService.prototype._startSubscriptions = function() { } 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'); }; +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) { var self = this; diff --git a/lib/services/transaction/index.js b/lib/services/transaction/index.js index 37fd44aa..9995a4bb 100644 --- a/lib/services/transaction/index.js +++ b/lib/services/transaction/index.js @@ -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 - 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 = [{ type: 'put', @@ -189,7 +189,7 @@ TransactionService.prototype._onReorg = function(oldBlockList, newBlockList, com removalOps.concat([ { type: 'del', - key: this.encoding.encodeAddressIndexKey(), + key: this.encoding.encodeAddressIndexKey(block.hash), }, { type: 'del', @@ -246,8 +246,11 @@ TransactionService.prototype._startSubscriptions = function() { 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/reorg'); }; module.exports = TransactionService;