From c9bf605a85113785542d852d6f8f5c889f8cd653 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 5 Mar 2016 09:15:59 -0800 Subject: [PATCH] fixes. improvements. --- lib/bcoin/blockdb.js | 124 +++++++++++++++++++++++++++++++------------ lib/bcoin/chain.js | 2 +- lib/bcoin/http.js | 2 +- lib/bcoin/node.js | 15 ++---- lib/bcoin/pool.js | 2 +- 5 files changed, 96 insertions(+), 49 deletions(-) diff --git a/lib/bcoin/blockdb.js b/lib/bcoin/blockdb.js index 130334aa..ebde8fcd 100644 --- a/lib/bcoin/blockdb.js +++ b/lib/bcoin/blockdb.js @@ -464,6 +464,7 @@ BlockDB.prototype.fillTX = function fillTX(tx, callback) { BlockDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, options, callback) { var self = this; + var ids = []; var coins = []; if (!callback) { @@ -476,7 +477,7 @@ BlockDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, opti addresses = utils.uniqs(addresses); - utils.forEachSerial(addresses, function(address, done) { + utils.forEach(addresses, function(address, done) { var iter = self.db.db.iterator({ gte: 'u/a/' + address, lte: 'u/a/' + address + '~', @@ -509,31 +510,43 @@ BlockDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, opti hash = parts[3]; index = +parts[4]; - self.getCoin(hash, index, function(err, coin) { - if (err) - return next(err); + ids.push([hash, index]); - if (!coin) - return next(); - - if (self.options.cache) - self.cache.unspent.set(id, coin); - - coins.push(coin); - next(); - }); + next(); }); })(); }, function(err) { if (err) return callback(err); - return callback(null, coins); + + utils.forEach(ids, function(item, next) { + var hash = item[0]; + var index = item[1]; + self.getCoin(hash, index, function(err, coin) { + if (err) + return next(err); + + if (!coin) + return next(); + + if (self.options.cache) + self.cache.unspent.set(id, coin); + + coins.push(coin); + next(); + }); + }, function(err) { + if (err) + return callback(err); + return callback(null, coins); + }); }); }; BlockDB.prototype.getCoin = function getCoin(hash, index, callback) { var self = this; var id = 'u/t/' + hash + '/' + index; + var coin; this.db.get(id, function(err, data) { if (err) { @@ -542,12 +555,19 @@ BlockDB.prototype.getCoin = function getCoin(hash, index, callback) { return callback(err); } - return callback(null, bcoin.coin.fromRaw(data)); + try { + coin = bcoin.coin.fromRaw(data); + } catch (e) { + return callback(e); + } + + return callback(null, coin); }); }; BlockDB.prototype.getTXByAddress = function getTXByAddress(addresses, options, callback) { var self = this; + var hashes = []; var txs = []; var have = {}; @@ -561,7 +581,7 @@ BlockDB.prototype.getTXByAddress = function getTXByAddress(addresses, options, c addresses = utils.uniqs(addresses); - utils.forEachSerial(addresses, function(address, done) { + utils.forEach(addresses, function(address, done) { var iter = self.db.db.iterator({ gte: 't/a/' + address, lte: 't/a/' + address + '~', @@ -601,23 +621,29 @@ BlockDB.prototype.getTXByAddress = function getTXByAddress(addresses, options, c if (self.options.cache && self.cache.tx.has(hash)) { txs.push(self.cache.tx.get(hash)); - return done(); + return next(); } - self.getTX(hash, function(err, tx) { - if (err) - return next(err); - if (!tx) - return next(); - txs.push(tx); - next(); - }); + hashes.push(hash); }); })(); }, function(err) { - if (err) - return callback(err); - return callback(null, txs); + utils.forEach(hashes, function(hash, next) { + self.getTX(hash, function(err, tx) { + if (err) + return next(err); + + if (!tx) + return next(); + + txs.push(tx); + next(); + }); + }, function(err) { + if (err) + return callback(err); + return callback(null, txs); + }); }); }; @@ -633,7 +659,11 @@ BlockDB.prototype.getTX = function getTX(hash, callback) { return callback(err); } - tx = bcoin.tx.fromExtended(data); + try { + tx = bcoin.tx.fromExtended(data); + } catch (e) { + return callback(e); + } if (self.options.paranoid && tx.hash('hex') !== hash) return callback(new Error('BlockDB is corrupt. All is lost.')); @@ -822,18 +852,22 @@ BlockDB.prototype.hasCoin = function hasCoin(hash, index, callback) { }); }; -// For BIP30 -// https://bitcointalk.org/index.php?topic=67738.0 -BlockDB.prototype.hasUnspentTX = function hasUnspentTX(hash, callback) { +BlockDB.prototype._getTX = function _getTX(hash, callback) { + if (hash instanceof bcoin.tx) + return callback(null, hash); + return this.getTX(hash); +}; + +BlockDB.prototype._spentTX = function _spentTX(hash, callback) { var self = this; - this.getTX(hash, function(err, tx) { + this._getTX(hash, function(err, tx) { var hash, spent; if (err) return callback(err); if (!tx) - return callback(null, false); + return callback(null, 0, -1); hash = tx.hash('hex'); spent = 0; @@ -852,7 +886,27 @@ BlockDB.prototype.hasUnspentTX = function hasUnspentTX(hash, callback) { }, function(err) { if (err) return callback(err); - return callback(null, spent < tx.outputs.length); + return callback(null, spent, tx.outputs.length); + }); +}; + +// For BIP30 +// https://bitcointalk.org/index.php?topic=67738.0 +BlockDB.prototype.isUnspentTX = function isUnspentTX(hash, callback) { + return this._spentTX(hash, function(err, spent, outputs) { + if (err) + return callback(err); + + return callback(null, spent < outputs); + }); +}; + +BlockDB.prototype.isSpentTX = function isSpentTX(hash, callback) { + return this._spentTX(hash, function(err, spent, outputs) { + if (err) + return callback(err); + + return callback(null, spent === outputs); }); }; diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 19c2129d..05e9b262 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -1673,7 +1673,7 @@ Chain.prototype.getLocator = function getLocator(start, callback, force) { function getTop(callback) { if (typeof start === 'string') { - self.db.getHeight(start, function(err, top) { + return self.db.getHeight(start, function(err, top) { if (err) return callback(err); diff --git a/lib/bcoin/http.js b/lib/bcoin/http.js index e77d1e04..8eee8108 100644 --- a/lib/bcoin/http.js +++ b/lib/bcoin/http.js @@ -155,7 +155,7 @@ HTTPServer.prototype._init = function _init() { else hash = utils.revHex(hash); - self.node.getBlock(hash, function(err, block) { + self.node.getFullBlock(hash, function(err, block) { if (err) return next(err); diff --git a/lib/bcoin/node.js b/lib/bcoin/node.js index 355dd245..fc7ddfe1 100644 --- a/lib/bcoin/node.js +++ b/lib/bcoin/node.js @@ -200,18 +200,11 @@ Fullnode.prototype.scanWallet = function scanWallet(wallet, callback) { }; Fullnode.prototype.getBlock = function getBlock(hash, callback) { - var self = this; - var coin; + this.blockdb.getBlock(hash, callback); +}; - this.blockdb.getBlock(hash, function(err, block) { - if (err) - return callback(err); - - if (!block) - return callback(); - - return callback(null, block); - }); +Fullnode.prototype.getFullBlock = function getFullBlock(hash, callback) { + this.blockdb.getFullBlock(hash, callback); }; Fullnode.prototype.getCoin = function getCoin(hash, index, callback) { diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index ec7c01ef..3bdc942f 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -574,7 +574,7 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) { // simply tries to find the latest block in // the peer's chain. if (last && headers.length === 2000) - self.getHeaders(peer, last.hash, null); + self.getHeaders(peer, last, null); }); // Reset interval to avoid calling getheaders unnecessarily