From 3a63da5735bdc6b31981f3f03ba5751b12e6d5ee Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 19 Feb 2016 08:07:50 -0800 Subject: [PATCH] async work. --- lib/bcoin/chain.js | 18 +++++------------- lib/bcoin/pool.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index cc9f589b..daf1db78 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -695,15 +695,9 @@ Chain.prototype._addEntry = function _addEntry(entry, block, callback) { }; Chain.prototype.resetHeight = function resetHeight(height) { - var self = this; - var i, existing; - - assert(height <= count - 1); - assert(this.tip); - this._clearPending(); - if (height === count - 1) + if (height === this.db.getSize() - 1) return; this.db.resetHeightSync(height); @@ -721,10 +715,6 @@ Chain.prototype.resetHeight = function resetHeight(height) { Chain.prototype.resetHeightAsync = function resetHeightAsync(height, callback) { var self = this; var lock = this.lock; - var i; - - assert(height <= count - 1); - assert(this.tip); this.lock = true; @@ -734,7 +724,7 @@ Chain.prototype.resetHeightAsync = function resetHeightAsync(height, callback) { callback(err, result); } - if (height === count - 1) + if (height === this.db.getSize() - 1) return utils.nextTick(done); this.db.resetHeightAsync(height, function(err) { @@ -1708,7 +1698,7 @@ Chain.prototype.getLocatorAsync = function getLocatorAsync(start, callback) { i = i - step; if (i <= 0) { if (i + step !== 0) - hashes.push(0); + hashes.push(network.genesis.hash); break; } if (hashes.length >= 10) @@ -1739,6 +1729,8 @@ Chain.prototype.getLocatorAsync = function getLocatorAsync(start, callback) { }); }; +// Chain.prototype.getLocatorAsync = wrap(Chain.prototype.getLocatorAsync); + Chain.prototype.getOrphanRoot = function getOrphanRoot(hash) { var self = this; var root; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index eb30e87a..f5f1e70f 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -272,6 +272,43 @@ Pool.prototype.loadHeaders = function loadHeaders(peer, top, stop) { }); }; +Pool.prototype.loadBlocks = function loadBlocks(peer, top, stop) { + var self = this; + this.chain._onFlush(function() { + self.chain.getLocatorAsync(top, function(err, locator) { + if (err) + throw err; + peer.loadBlocks(locator, stop); + }); + }); +}; + +Pool.prototype.loadOrphan = function loadOrphan(peer, top, orphan) { + var self = this; + assert(orphan); + this.chain._onFlush(function() { + self.chain.getLocatorAsync(top, function(err, locator) { + if (err) + throw err; + peer.loadBlocks( + locator, + self.chain.getOrphanRoot(orphan) + ); + }); + }); +}; + +Pool.prototype.loadHeaders = function loadHeaders(peer, top, stop) { + var self = this; + this.chain._onFlush(function() { + self.chain.getLocatorAsync(top, function(err, locator) { + if (err) + throw err; + peer.loadHeaders(locator, stop); + }); + }); +}; + Pool.prototype.startServer = function startServer() { var self = this;