@@ -9,6 +9,9 @@ var Address = require('../models/Address');
9
9
var common = require ( './common' ) ;
10
10
var async = require ( 'async' ) ;
11
11
12
+ var MAX_BATCH_SIZE = 100 ;
13
+ var RPC_CONCURRENCY = 5 ;
14
+
12
15
var tDb = require ( '../../lib/TransactionDb' ) . default ( ) ;
13
16
14
17
var checkSync = function ( req , res ) {
@@ -50,7 +53,7 @@ var getAddrs = function(req, res, next) {
50
53
}
51
54
} catch ( e ) {
52
55
common . handleErrors ( {
53
- message : 'Invalid address :' + e . message ,
56
+ message : 'Invalid addrs param :' + e . message ,
54
57
code : 1
55
58
} , res , next ) ;
56
59
return null ;
@@ -101,7 +104,7 @@ exports.multiutxo = function(req, res, next) {
101
104
var as = getAddrs ( req , res , next ) ;
102
105
if ( as ) {
103
106
var utxos = [ ] ;
104
- async . each ( as , function ( a , callback ) {
107
+ async . eachLimit ( as , RPC_CONCURRENCY , function ( a , callback ) {
105
108
a . update ( function ( err ) {
106
109
if ( err ) callback ( err ) ;
107
110
utxos = utxos . concat ( a . unspent ) ;
@@ -123,25 +126,39 @@ exports.multitxs = function(req, res, next) {
123
126
function processTxs ( txs , from , to , cb ) {
124
127
txs = _ . uniq ( _ . flatten ( txs ) , 'txid' ) ;
125
128
var nbTxs = txs . length ;
126
- var paginated = ! _ . isUndefined ( from ) || ! _ . isUndefined ( to ) ;
127
129
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 ;
135
133
}
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 ) ;
136
150
137
151
var txIndex = { } ;
138
152
_ . each ( txs , function ( tx ) {
139
153
txIndex [ tx . txid ] = tx ;
140
154
} ) ;
141
155
142
- async . each ( txs , function ( tx , callback ) {
156
+ async . eachLimit ( txs , RPC_CONCURRENCY , function ( tx , callback ) {
143
157
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
+ }
145
162
if ( tx && tx . info ) {
146
163
txIndex [ tx . txid ] . info = tx . info ;
147
164
}
@@ -151,14 +168,12 @@ exports.multitxs = function(req, res, next) {
151
168
if ( err ) return cb ( err ) ;
152
169
153
170
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
+ } ;
162
177
return cb ( null , transactions ) ;
163
178
} ) ;
164
179
} ;
@@ -169,17 +184,19 @@ exports.multitxs = function(req, res, next) {
169
184
var as = getAddrs ( req , res , next ) ;
170
185
if ( as ) {
171
186
var txs = [ ] ;
172
- async . eachLimit ( as , 10 , function ( a , callback ) {
187
+ async . eachLimit ( as , RPC_CONCURRENCY , function ( a , callback ) {
173
188
a . update ( function ( err ) {
174
189
if ( err ) callback ( err ) ;
175
190
txs . push ( a . transactions ) ;
176
191
callback ( ) ;
177
192
} , {
178
193
ignoreCache : req . param ( 'noCache' ) ,
179
- includeTxInfo : true
194
+ includeTxInfo : true ,
195
+ dontFillSpent : true ,
180
196
} ) ;
181
197
} , function ( err ) { // finished callback
182
198
if ( err ) return common . handleErrors ( err , res ) ;
199
+
183
200
processTxs ( txs , from , to , function ( err , transactions ) {
184
201
if ( err ) return common . handleErrors ( err , res ) ;
185
202
res . jsonp ( transactions ) ;
0 commit comments