Skip to content

Commit 4003b0c

Browse files
committed
Merge pull request #336 from matiu/opt/post-txs
Opt/post txs
2 parents 2709031 + 671ef9a commit 4003b0c

File tree

4 files changed

+50
-25
lines changed

4 files changed

+50
-25
lines changed

app/controllers/addresses.js

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ var Address = require('../models/Address');
99
var common = require('./common');
1010
var async = require('async');
1111

12+
var MAX_BATCH_SIZE = 100;
13+
var RPC_CONCURRENCY = 5;
14+
1215
var tDb = require('../../lib/TransactionDb').default();
1316

1417
var checkSync = function(req, res) {
@@ -50,7 +53,7 @@ var getAddrs = function(req, res, next) {
5053
}
5154
} catch (e) {
5255
common.handleErrors({
53-
message: 'Invalid address:' + e.message,
56+
message: 'Invalid addrs param:' + e.message,
5457
code: 1
5558
}, res, next);
5659
return null;
@@ -101,7 +104,7 @@ exports.multiutxo = function(req, res, next) {
101104
var as = getAddrs(req, res, next);
102105
if (as) {
103106
var utxos = [];
104-
async.each(as, function(a, callback) {
107+
async.eachLimit(as, RPC_CONCURRENCY, function(a, callback) {
105108
a.update(function(err) {
106109
if (err) callback(err);
107110
utxos = utxos.concat(a.unspent);
@@ -123,25 +126,39 @@ exports.multitxs = function(req, res, next) {
123126
function processTxs(txs, from, to, cb) {
124127
txs = _.uniq(_.flatten(txs), 'txid');
125128
var nbTxs = txs.length;
126-
var paginated = !_.isUndefined(from) || !_.isUndefined(to);
127129

128-
if (paginated) {
129-
txs.sort(function(a, b) {
130-
return (b.ts || b.ts) - (a.ts || a.ts);
131-
});
132-
var start = Math.max(from || 0, 0);
133-
var end = Math.min(to || txs.length, txs.length);
134-
txs = txs.slice(start, end);
130+
if (_.isUndefined(from) && _.isUndefined(to)) {
131+
from = 0;
132+
to = MAX_BATCH_SIZE;
135133
}
134+
if (!_.isUndefined(from) && _.isUndefined(to))
135+
to = from + MAX_BATCH_SIZE;
136+
137+
if (!_.isUndefined(from) && !_.isUndefined(to) && to - from > MAX_BATCH_SIZE)
138+
to = from + MAX_BATCH_SIZE;
139+
140+
if (from < 0) from = 0;
141+
if (to < 0) to = 0;
142+
if (from > nbTxs) from = nbTxs;
143+
if (to > nbTxs) to = nbTxs;
144+
145+
txs.sort(function(a, b) {
146+
return (b.ts || b.ts) - (a.ts || a.ts);
147+
});
148+
149+
txs = txs.slice(from, to);
136150

137151
var txIndex = {};
138152
_.each(txs, function(tx) {
139153
txIndex[tx.txid] = tx;
140154
});
141155

142-
async.each(txs, function(tx, callback) {
156+
async.eachLimit(txs, RPC_CONCURRENCY, function(tx, callback) {
143157
tDb.fromIdWithInfo(tx.txid, function(err, tx) {
144-
if (err) console.log(err);
158+
if (err) {
159+
console.log(err);
160+
return common.handleErrors(err, res);
161+
}
145162
if (tx && tx.info) {
146163
txIndex[tx.txid].info = tx.info;
147164
}
@@ -151,14 +168,12 @@ exports.multitxs = function(req, res, next) {
151168
if (err) return cb(err);
152169

153170
var transactions = _.pluck(txs, 'info');
154-
if (paginated) {
155-
transactions = {
156-
totalItems: nbTxs,
157-
from: +from,
158-
to: +to,
159-
items: transactions,
160-
};
161-
}
171+
transactions = {
172+
totalItems: nbTxs,
173+
from: +from,
174+
to: +to,
175+
items: transactions,
176+
};
162177
return cb(null, transactions);
163178
});
164179
};
@@ -169,17 +184,19 @@ exports.multitxs = function(req, res, next) {
169184
var as = getAddrs(req, res, next);
170185
if (as) {
171186
var txs = [];
172-
async.eachLimit(as, 10, function(a, callback) {
187+
async.eachLimit(as, RPC_CONCURRENCY, function(a, callback) {
173188
a.update(function(err) {
174189
if (err) callback(err);
175190
txs.push(a.transactions);
176191
callback();
177192
}, {
178193
ignoreCache: req.param('noCache'),
179-
includeTxInfo: true
194+
includeTxInfo: true,
195+
dontFillSpent: true,
180196
});
181197
}, function(err) { // finished callback
182198
if (err) return common.handleErrors(err, res);
199+
183200
processTxs(txs, from, to, function(err, transactions) {
184201
if (err) return common.handleErrors(err, res);
185202
res.jsonp(transactions);

config/express.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ module.exports = function(app, historicSync, peerSync) {
2828
app.enable('jsonp callback');
2929
app.use(config.apiPrefix, setHistoric);
3030
app.use(config.apiPrefix, setPeer);
31-
app.use(express.logger('dev'));
31+
app.use(require('morgan')(':remote-addr :date[iso] ":method :url" :status :res[content-length] :response-time ":user-agent" '));
32+
3233
app.use(express.json());
3334
app.use(express.urlencoded());
3435
app.use(express.methodOverride());

lib/TransactionDb.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var ADDR_PREFIX = 'txa2-'; //txa-<addr>-<tsr>-<txid>-<n>
1717

1818
// TODO: use bitcore networks module
1919
var genesisTXID = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b';
20-
var CONCURRENCY = 10;
20+
var CONCURRENCY = 5;
2121
var DEFAULT_SAFE_CONFIRMATIONS = 6;
2222

2323
var MAX_OPEN_FILES = 500;
@@ -448,6 +448,9 @@ TransactionDb.prototype._parseAddrData = function(k, data, ignoreCache) {
448448
return item;
449449
};
450450

451+
452+
// opts.dontFillSpent
453+
451454
TransactionDb.prototype.fromAddr = function(addr, opts, cb) {
452455
opts = opts || {};
453456
var self = this;
@@ -470,6 +473,10 @@ TransactionDb.prototype.fromAddr = function(addr, opts, cb) {
470473
})
471474
.on('error', cb)
472475
.on('end', function() {
476+
if (opts.dontFillSpent) {
477+
return cb(null, ret)
478+
}
479+
473480
async.eachLimit(ret.filter(function(x) {
474481
return !x.spentIsConfirmed;
475482
}), CONCURRENCY, function(o, e_c) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"async": "*",
5252
"base58-native": "0.1.2",
5353
"bignum": "*",
54-
"bitauth": "^0.1.1",
54+
"morgan": "*",
5555
"bitcore": "git://github.com/bitpay/bitcore.git#aa41c70cff2583d810664c073a324376c39c8b36",
5656
"bufferput": "git://github.com/bitpay/node-bufferput.git",
5757
"buffertools": "*",

0 commit comments

Comments
 (0)