From 7d2b759f2be92620f7e559e9ebbdfeb5b89a6a72 Mon Sep 17 00:00:00 2001 From: eordano Date: Tue, 7 Apr 2015 15:28:12 -0300 Subject: [PATCH] Prune --- lib/blockchain.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/blockchain.js b/lib/blockchain.js index 50b46275..6498491b 100644 --- a/lib/blockchain.js +++ b/lib/blockchain.js @@ -63,7 +63,7 @@ BlockChain.prototype.proposeNewBlock = function(block) { commonAncestor = pointer; pointer = this.tip; - while (pointer != commonAncestor) { + while (pointer !== commonAncestor) { toUnconfirm.push(pointer); pointer = this.prev[pointer]; } @@ -105,9 +105,9 @@ BlockChain.prototype.unconfirm = function(hash) { this.tip = prevHash; var height = this.height[hash]; - this.next[prevHash] = undefined; - this.hashByHeight[height] = undefined; - this.height[hash] = undefined; + delete this.next[prevHash]; + delete this.hashByHeight[height]; + delete this.height[hash]; }; BlockChain.prototype.getBlockLocator = function() { @@ -132,6 +132,16 @@ BlockChain.prototype.hasData = function(hash) { return !!this.prev[hash]; }; +BlockChain.prototype.prune = function() { + var self = this; + _.each(this.prev, function(key, value) { + if (!self.height[key]) { + delete this.prev[key]; + delete this.work[key]; + } + }); +}; + BlockChain.prototype.toObject = function() { return { tip: this.tip,