improve getutxos.
This commit is contained in:
parent
167e187fe0
commit
4d823742c3
@ -877,21 +877,21 @@ Peer.prototype._handleFeeFilter = function _handleFeeFilter(payload) {
|
||||
|
||||
/**
|
||||
* Request UTXOs from peer.
|
||||
* @param {Array[]} - Array in the form `[[hash, index], ...]`.
|
||||
* @param {Outpoint[]} outpoints
|
||||
* @param {Function} callback - Returns [Error, {@link Coin}[]].
|
||||
*/
|
||||
|
||||
Peer.prototype.getUTXOs = function getUTXOs(utxos, callback) {
|
||||
Peer.prototype.getUTXOs = function getUTXOs(outpoints, callback) {
|
||||
var self = this;
|
||||
var reqs = [];
|
||||
var coins = [];
|
||||
var i;
|
||||
|
||||
for (i = 0; i < utxos.length; i += 15)
|
||||
reqs.push(utxos.slice(i, i + 15));
|
||||
for (i = 0; i < outpoints.length; i += 15)
|
||||
reqs.push(outpoints.slice(i, i + 15));
|
||||
|
||||
utils.forEachSerial(reqs, function(utxos, next) {
|
||||
self._getUTXOs(utxos, function(err, coin) {
|
||||
utils.forEachSerial(reqs, function(outpoints, next) {
|
||||
self._getUTXOs(outpoints, function(err, coin) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
@ -910,11 +910,11 @@ Peer.prototype.getUTXOs = function getUTXOs(utxos, callback) {
|
||||
/**
|
||||
* Send non-chunked getuxos to peer.
|
||||
* @private
|
||||
* @param {Array[]} utxos
|
||||
* @param {Outpoint[]} outpoints
|
||||
* @param {Function} callback
|
||||
*/
|
||||
|
||||
Peer.prototype._getUTXOs = function _getUTXOs(utxos, callback) {
|
||||
Peer.prototype._getUTXOs = function _getUTXOs(outpoints, callback) {
|
||||
var index = 0;
|
||||
var i, prevout, coin;
|
||||
|
||||
@ -924,7 +924,7 @@ Peer.prototype._getUTXOs = function _getUTXOs(utxos, callback) {
|
||||
|
||||
for (i = 0; i < payload.hits.length; i++) {
|
||||
if (payload.hits[i]) {
|
||||
prevout = utxos[i];
|
||||
prevout = outpoints[i];
|
||||
coin = payload.coins[index++];
|
||||
|
||||
if (!prevout || !coin)
|
||||
@ -940,9 +940,7 @@ Peer.prototype._getUTXOs = function _getUTXOs(utxos, callback) {
|
||||
|
||||
this.write(this.framer.getUTXOs({
|
||||
mempool: true,
|
||||
prevout: utxos.map(function(item) {
|
||||
return { hash: item[0], index: item[1] };
|
||||
})
|
||||
prevout: outpoints
|
||||
}));
|
||||
};
|
||||
|
||||
|
||||
@ -1704,32 +1704,27 @@ Pool.prototype.getPeer = function getPeer(host) {
|
||||
|
||||
/**
|
||||
* Request UTXOs from peer.
|
||||
* @param {Array[]} - Array in the form `[[hash, index], ...]`.
|
||||
* @param {Outpoint[]} outpoints
|
||||
* @param {Function} callback - Returns [Error, {@link Coin}[]].
|
||||
*/
|
||||
|
||||
Pool.prototype.getUTXOs = function getUTXOs(utxos, callback) {
|
||||
Pool.prototype.getUTXOs = function getUTXOs(outpoints, callback) {
|
||||
var i, peer;
|
||||
|
||||
if (this.peers.load && this.peers.load.version) {
|
||||
if (this.peers.load.version.services & constants.services.GETUXO)
|
||||
peer = this.peers.load;
|
||||
for (i = 0; i < this.peers.all.length; i++) {
|
||||
peer = this.peers.all[i];
|
||||
|
||||
if (!peer.version)
|
||||
continue;
|
||||
|
||||
if (peer.version.services & constants.services.GETUXO)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!peer) {
|
||||
for (i = 0; i < this.peers.regular.length; i++) {
|
||||
peer = this.peers.regular[i];
|
||||
if (peer.version.services & constants.services.GETUXO)
|
||||
break;
|
||||
}
|
||||
if (i === this.peers.regular.length)
|
||||
peer = null;
|
||||
}
|
||||
|
||||
if (!peer)
|
||||
if (i === this.peers.regular.length)
|
||||
return utils.asyncify(callback)(new Error('No peer available.'));
|
||||
|
||||
peer.getUTXOs(utxos, callback);
|
||||
peer.getUTXOs(outpoints, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -1739,19 +1734,19 @@ Pool.prototype.getUTXOs = function getUTXOs(utxos, callback) {
|
||||
*/
|
||||
|
||||
Pool.prototype.fillCoins = function fillCoins(tx, callback) {
|
||||
var utxos = [];
|
||||
var outpoints = [];
|
||||
var i, input;
|
||||
|
||||
for (i = 0; i < tx.inputs.length; i++) {
|
||||
input = tx.inputs[i];
|
||||
if (!input.coin)
|
||||
utxos.push([input.prevout.hash, input.prevout.index]);
|
||||
outpoints.push(input.prevout);
|
||||
}
|
||||
|
||||
if (utxos.length === 0)
|
||||
if (outpoints.length === 0)
|
||||
return utils.asyncify(callback)(null, tx);
|
||||
|
||||
this.getUTXOs(utxos, function(err, coins) {
|
||||
this.getUTXOs(outpoints, function(err, coins) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
|
||||
@ -884,11 +884,8 @@ Framer.getUTXOs = function getUTXOs(data, writer) {
|
||||
p.writeU8(data.mempool ? 1 : 0);
|
||||
p.writeVarint(data.prevout.length);
|
||||
|
||||
for (i = 0; i < data.prevout.length; i++) {
|
||||
prevout = data.prevout[i];
|
||||
p.writeHash(prevout.hash);
|
||||
p.writeU32(prevout.index);
|
||||
}
|
||||
for (i = 0; i < data.prevout.length; i++)
|
||||
data.prevout[i].toRaw(p);
|
||||
|
||||
if (!writer)
|
||||
p = p.render();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user