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) { BlockDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, options, callback) {
var self = this; var self = this;
var ids = [];
var coins = []; var coins = [];
if (!callback) { if (!callback) {
@ -476,7 +477,7 @@ BlockDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, opti
addresses = utils.uniqs(addresses); addresses = utils.uniqs(addresses);
utils.forEachSerial(addresses, function(address, done) { utils.forEach(addresses, function(address, done) {
var iter = self.db.db.iterator({ var iter = self.db.db.iterator({
gte: 'u/a/' + address, gte: 'u/a/' + address,
lte: 'u/a/' + address + '~', lte: 'u/a/' + address + '~',
@ -509,31 +510,43 @@ BlockDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, opti
hash = parts[3]; hash = parts[3];
index = +parts[4]; index = +parts[4];
self.getCoin(hash, index, function(err, coin) { ids.push([hash, index]);
if (err)
return next(err);
if (!coin) next();
return next();
if (self.options.cache)
self.cache.unspent.set(id, coin);
coins.push(coin);
next();
});
}); });
})(); })();
}, function(err) { }, function(err) {
if (err) if (err)
return callback(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) { BlockDB.prototype.getCoin = function getCoin(hash, index, callback) {
var self = this; var self = this;
var id = 'u/t/' + hash + '/' + index; var id = 'u/t/' + hash + '/' + index;
var coin;
this.db.get(id, function(err, data) { this.db.get(id, function(err, data) {
if (err) { if (err) {
@ -542,12 +555,19 @@ BlockDB.prototype.getCoin = function getCoin(hash, index, callback) {
return callback(err); 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) { BlockDB.prototype.getTXByAddress = function getTXByAddress(addresses, options, callback) {
var self = this; var self = this;
var hashes = [];
var txs = []; var txs = [];
var have = {}; var have = {};
@ -561,7 +581,7 @@ BlockDB.prototype.getTXByAddress = function getTXByAddress(addresses, options, c
addresses = utils.uniqs(addresses); addresses = utils.uniqs(addresses);
utils.forEachSerial(addresses, function(address, done) { utils.forEach(addresses, function(address, done) {
var iter = self.db.db.iterator({ var iter = self.db.db.iterator({
gte: 't/a/' + address, gte: 't/a/' + address,
lte: '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)) { if (self.options.cache && self.cache.tx.has(hash)) {
txs.push(self.cache.tx.get(hash)); txs.push(self.cache.tx.get(hash));
return done(); return next();
} }
self.getTX(hash, function(err, tx) { hashes.push(hash);
if (err)
return next(err);
if (!tx)
return next();
txs.push(tx);
next();
});
}); });
})(); })();
}, function(err) { }, function(err) {
if (err) utils.forEach(hashes, function(hash, next) {
return callback(err); self.getTX(hash, function(err, tx) {
return callback(null, txs); 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); 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) if (self.options.paranoid && tx.hash('hex') !== hash)
return callback(new Error('BlockDB is corrupt. All is lost.')); return callback(new Error('BlockDB is corrupt. All is lost.'));
@ -822,18 +852,22 @@ BlockDB.prototype.hasCoin = function hasCoin(hash, index, callback) {
}); });
}; };
// For BIP30 BlockDB.prototype._getTX = function _getTX(hash, callback) {
// https://bitcointalk.org/index.php?topic=67738.0 if (hash instanceof bcoin.tx)
BlockDB.prototype.hasUnspentTX = function hasUnspentTX(hash, callback) { return callback(null, hash);
return this.getTX(hash);
};
BlockDB.prototype._spentTX = function _spentTX(hash, callback) {
var self = this; var self = this;
this.getTX(hash, function(err, tx) { this._getTX(hash, function(err, tx) {
var hash, spent; var hash, spent;
if (err) if (err)
return callback(err); return callback(err);
if (!tx) if (!tx)
return callback(null, false); return callback(null, 0, -1);
hash = tx.hash('hex'); hash = tx.hash('hex');
spent = 0; spent = 0;
@ -852,7 +886,27 @@ BlockDB.prototype.hasUnspentTX = function hasUnspentTX(hash, callback) {
}, function(err) { }, function(err) {
if (err) if (err)
return callback(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) { function getTop(callback) {
if (typeof start === 'string') { if (typeof start === 'string') {
self.db.getHeight(start, function(err, top) { return self.db.getHeight(start, function(err, top) {
if (err) if (err)
return callback(err); return callback(err);

View File

@ -155,7 +155,7 @@ HTTPServer.prototype._init = function _init() {
else else
hash = utils.revHex(hash); hash = utils.revHex(hash);
self.node.getBlock(hash, function(err, block) { self.node.getFullBlock(hash, function(err, block) {
if (err) if (err)
return next(err); return next(err);

View File

@ -200,18 +200,11 @@ Fullnode.prototype.scanWallet = function scanWallet(wallet, callback) {
}; };
Fullnode.prototype.getBlock = function getBlock(hash, callback) { Fullnode.prototype.getBlock = function getBlock(hash, callback) {
var self = this; this.blockdb.getBlock(hash, callback);
var coin; };
this.blockdb.getBlock(hash, function(err, block) { Fullnode.prototype.getFullBlock = function getFullBlock(hash, callback) {
if (err) this.blockdb.getFullBlock(hash, callback);
return callback(err);
if (!block)
return callback();
return callback(null, block);
});
}; };
Fullnode.prototype.getCoin = function getCoin(hash, index, 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 // simply tries to find the latest block in
// the peer's chain. // the peer's chain.
if (last && headers.length === 2000) if (last && headers.length === 2000)
self.getHeaders(peer, last.hash, null); self.getHeaders(peer, last, null);
}); });
// Reset interval to avoid calling getheaders unnecessarily // Reset interval to avoid calling getheaders unnecessarily