mirror of https://github.com/BTCPrivate/copay.git
Merge pull request #360 from maraoz/optimize/many-addresses
Optimize for many addresses
This commit is contained in:
commit
eedfdcfaa1
|
@ -12,41 +12,44 @@ function Insight(opts) {
|
|||
|
||||
function _asyncForEach(array, fn, callback) {
|
||||
array = array.slice(0);
|
||||
|
||||
function processOne() {
|
||||
var item = array.pop();
|
||||
fn(item, function(result) {
|
||||
if(array.length > 0) {
|
||||
if (array.length > 0) {
|
||||
setTimeout(processOne, 0); // schedule immediately
|
||||
} else {
|
||||
callback(); // Done!
|
||||
}
|
||||
});
|
||||
}
|
||||
if(array.length > 0) {
|
||||
if (array.length > 0) {
|
||||
setTimeout(processOne, 0); // schedule immediately
|
||||
} else {
|
||||
callback(); // Done!
|
||||
}
|
||||
};
|
||||
|
||||
function removeRepeatedElements(ar){
|
||||
var ya=false,v="",aux=[].concat(ar),r=Array();
|
||||
for (var i in aux){ //
|
||||
v=aux[i];
|
||||
ya=false;
|
||||
for (var a in aux){
|
||||
if (v==aux[a]){
|
||||
if (ya==false){
|
||||
ya=true;
|
||||
}
|
||||
else{
|
||||
aux[a]="";
|
||||
function removeRepeatedElements(ar) {
|
||||
var ya = false,
|
||||
v = "",
|
||||
aux = [].concat(ar),
|
||||
r = Array();
|
||||
for (var i in aux) { //
|
||||
v = aux[i];
|
||||
ya = false;
|
||||
for (var a in aux) {
|
||||
if (v == aux[a]) {
|
||||
if (ya == false) {
|
||||
ya = true;
|
||||
} else {
|
||||
aux[a] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (var a in aux){
|
||||
if (aux[a]!=""){
|
||||
for (var a in aux) {
|
||||
if (aux[a] != "") {
|
||||
r.push(aux[a]);
|
||||
}
|
||||
}
|
||||
|
@ -69,12 +72,14 @@ Insight.prototype.getTransactions = function(addresses, cb) {
|
|||
method: 'GET',
|
||||
path: '/api/addr/' + addr,
|
||||
|
||||
headers: { 'Access-Control-Request-Headers' : '' }
|
||||
headers: {
|
||||
'Access-Control-Request-Headers': ''
|
||||
}
|
||||
};
|
||||
|
||||
self._request(options, function(err, res) {
|
||||
var txids_tmp = res.transactions;
|
||||
for(var i=0; i<txids_tmp.length; i++) {
|
||||
for (var i = 0; i < txids_tmp.length; i++) {
|
||||
txids.push(txids_tmp[i]);
|
||||
}
|
||||
callback();
|
||||
|
@ -88,7 +93,9 @@ Insight.prototype.getTransactions = function(addresses, cb) {
|
|||
scheme: self.scheme,
|
||||
method: 'GET',
|
||||
path: '/api/tx/' + txid,
|
||||
headers: { 'Access-Control-Request-Headers' : '' }
|
||||
headers: {
|
||||
'Access-Control-Request-Headers': ''
|
||||
}
|
||||
};
|
||||
self._request(options, function(err, res) {
|
||||
txs.push(res);
|
||||
|
@ -107,24 +114,22 @@ Insight.prototype.getUnspent = function(addresses, cb) {
|
|||
|
||||
var all = [];
|
||||
|
||||
_asyncForEach(addresses, function(addr, callback) {
|
||||
var options = {
|
||||
host: self.host,
|
||||
port: self.port,
|
||||
scheme: self.scheme,
|
||||
method: 'GET',
|
||||
path: '/api/addr/' + addr + '/utxo',
|
||||
path: '/api/addrs/' + addresses.join(',') + '/utxo',
|
||||
|
||||
headers: { 'Access-Control-Request-Headers' : '' }
|
||||
headers: {
|
||||
'Access-Control-Request-Headers': ''
|
||||
}
|
||||
};
|
||||
|
||||
self._request(options, function(err, res) {
|
||||
if (res && res.length > 0) {
|
||||
all = all.concat(res);
|
||||
}
|
||||
callback();
|
||||
});
|
||||
}, function() {
|
||||
return cb(all);
|
||||
});
|
||||
};
|
||||
|
@ -137,14 +142,16 @@ Insight.prototype.sendRawTransaction = function(rawtx, cb) {
|
|||
port: this.port,
|
||||
method: 'POST',
|
||||
path: '/api/tx/send',
|
||||
data: 'rawtx='+rawtx,
|
||||
headers: { 'content-type' : 'application/x-www-form-urlencoded' }
|
||||
data: 'rawtx=' + rawtx,
|
||||
headers: {
|
||||
'content-type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
};
|
||||
this._request(options, function(err,res) {
|
||||
console.log('[Insight.js.73:err:]',err); //TODO
|
||||
this._request(options, function(err, res) {
|
||||
console.log('[Insight.js.73:err:]', err); //TODO
|
||||
if (err) return cb();
|
||||
|
||||
console.log('[Insight.js.74]', res); //TODO
|
||||
console.log('[Insight.js.74]', res); //TODO
|
||||
return cb(res.txid);
|
||||
});
|
||||
};
|
||||
|
@ -171,8 +178,7 @@ Insight.prototype._request = function(options, callback) {
|
|||
if (request.readyState === 4) {
|
||||
if (request.status === 200) {
|
||||
return callback(null, JSON.parse(request.responseText));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return callback({
|
||||
message: 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText
|
||||
});
|
||||
|
@ -195,17 +201,20 @@ Insight.prototype._request = function(options, callback) {
|
|||
try {
|
||||
ret = JSON.parse(chunk);
|
||||
} catch (e) {
|
||||
callback({message: "Wrong response from insight"});
|
||||
callback({
|
||||
message: "Wrong response from insight"
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
response.on('end', function () {
|
||||
response.on('end', function() {
|
||||
callback(undefined, ret);
|
||||
return;
|
||||
});
|
||||
}
|
||||
else {
|
||||
callback({message: 'Error ' + response.statusCode});
|
||||
} else {
|
||||
callback({
|
||||
message: 'Error ' + response.statusCode
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
@ -218,4 +227,3 @@ Insight.prototype._request = function(options, callback) {
|
|||
|
||||
|
||||
module.exports = require('soop')(Insight);
|
||||
|
||||
|
|
|
@ -101,7 +101,6 @@ angular.module('copay.controllerUtils')
|
|||
};
|
||||
|
||||
root.updateBalance = function(cb) {
|
||||
root.setSocketHandlers();
|
||||
$rootScope.balanceByAddr = {};
|
||||
var w = $rootScope.wallet;
|
||||
$rootScope.addrInfos = w.getAddressesInfo();
|
||||
|
@ -121,6 +120,7 @@ angular.module('copay.controllerUtils')
|
|||
$rootScope.$digest();
|
||||
if (cb) cb();
|
||||
});
|
||||
root.setSocketHandlers();
|
||||
};
|
||||
|
||||
root.updateTxs = function() {
|
||||
|
|
Loading…
Reference in New Issue