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