fixes. improvements.

This commit is contained in:
Christopher Jeffrey 2016-03-05 09:15:59 -08:00
parent 67e739ef39
commit c9bf605a85
5 changed files with 96 additions and 49 deletions

View File

@ -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);
});
};

View File

@ -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);

View File

@ -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);

View File

@ -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) {

View File

@ -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