Skip to content

Commit 3cd3c4f

Browse files
committed
Added batch query support, plus a bit more information to some queries
1 parent d987571 commit 3cd3c4f

File tree

5 files changed

+98
-24
lines changed

5 files changed

+98
-24
lines changed

app/controllers/blocks.js

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,42 @@
44
* Module dependencies.
55
*/
66
var common = require('./common');
7+
var multi = common.multi;
78
var async = require('async');
89
var bdb = require('../../lib/BlockDb').default();
910
var tdb = require('../../lib/TransactionDb').default();
1011

1112
/**
1213
* Find block by hash ...
1314
*/
14-
exports.block = function(req, res, next, hash) {
15+
exports.block = multi(function(hash, cb) {
1516
bdb.fromHashWithInfo(hash, function(err, block) {
1617
if (err || !block)
1718
return common.handleErrors(err, res, next);
1819
else {
1920
tdb.getPoolInfo(block.info.tx[0], function(info) {
2021
block.info.poolInfo = info;
21-
req.block = block.info;
22-
return next();
22+
cb(null, block.info);
2323
});
2424
}
2525
});
26-
};
26+
}, 'block');
27+
28+
29+
/**
30+
* Find block header by hash ...
31+
*/
32+
exports.blockHeader = multi(function(hash, cb) {
33+
bdb.fromHashWithInfo(hash, function(err, block) {
34+
if (err || !block)
35+
return common.handleErrors(err, res, next);
36+
else {
37+
delete block.info.tx;
38+
cb(null, block.info);
39+
}
40+
});
41+
}, 'block');
42+
2743

2844

2945
/**
@@ -35,19 +51,50 @@ exports.show = function(req, res) {
3551
}
3652
};
3753

54+
/**
55+
* Show block hash
56+
*/
57+
exports.showBlockHash = function(req, res) {
58+
if (req.blockHash) {
59+
res.jsonp(req.blockHash);
60+
}
61+
};
62+
3863
/**
3964
* Show block by Height
4065
*/
41-
exports.blockindex = function(req, res, next, height) {
66+
exports.blockIndex = multi(function(height, cb) {
4267
bdb.blockIndex(height, function(err, hashStr) {
4368
if (err) {
4469
console.log(err);
45-
res.status(400).send('Bad Request'); // TODO
70+
cb('Bad Request'); // TODO
4671
} else {
47-
res.jsonp(hashStr);
72+
cb(null, hashStr);
4873
}
4974
});
50-
};
75+
}, 'blockHash');
76+
77+
78+
/**
79+
* Show block header by Height
80+
*/
81+
exports.blockHeaderByIndex = multi(function(height, cb) {
82+
bdb.blockIndex(height, function(err, hashStr) {
83+
if (err) {
84+
console.log(err);
85+
cb('Bad Request');
86+
} else {
87+
bdb.fromHashWithInfo(hashStr.blockHash, function(err, block) {
88+
if (err || !block)
89+
cb(err);
90+
else {
91+
delete block.info.tx;
92+
cb(null, block.info);
93+
}
94+
});
95+
}
96+
});
97+
}, 'block');
5198

5299
var getBlock = function(blockhash, cb) {
53100
bdb.fromHashWithInfo(blockhash, function(err, block) {

app/controllers/common.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var async = require('async');
4+
35
exports.notReady = function (err, res, p) {
46
res.status(503).send('Server not yet ready. Sync Percentage:' + p);
57
};
@@ -17,3 +19,21 @@ exports.handleErrors = function (err, res) {
1719
res.status(404).send('Not found');
1820
}
1921
};
22+
23+
exports.multi = function(f, outkey) {
24+
return function(req, res, next, inputdata) {
25+
var inputs;
26+
if (inputdata.indexOf(',') >= 0) {
27+
inputs = inputdata.split(',');
28+
}
29+
else inputs = [inputdata];
30+
async.mapSeries(inputs, f, function(err, results) {
31+
if (err)
32+
return exports.handleErrors(err, res);
33+
req[outkey] = results;
34+
if (req[outkey].length == 1)
35+
req[outkey] = req[outkey][0]
36+
return next();
37+
});
38+
};
39+
}

app/controllers/transactions.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
var Address = require('../models/Address');
77
var async = require('async');
88
var common = require('./common');
9+
var multi = common.multi;
910
var util = require('util');
1011

1112
var Rpc = require('../../lib/Rpc');
@@ -54,22 +55,19 @@ exports.rawTransaction = function (req, res, next, txid) {
5455
/**
5556
* Find transaction by hash ...
5657
*/
57-
exports.transaction = function(req, res, next, txid) {
58-
59-
tDb.fromIdWithInfo(txid, function(err, tx) {
60-
if (err || ! tx)
61-
return common.handleErrors(err, res);
62-
63-
bdb.fillVinConfirmations(tx.info, function(err) {
64-
if (err)
65-
return common.handleErrors(err, res);
58+
exports.transaction = multi(function(txid, cb) {
59+
tDb.fromIdWithInfo(txid, function(err, tx) {
60+
if (err || ! tx)
61+
return cb(err);
62+
63+
bdb.fillVinConfirmations(tx.info, function(err) {
64+
if (err)
65+
return cb(err);
66+
return cb(null, tx.info);
67+
});
6668

67-
req.transaction = tx.info;
68-
return next();
6969
});
70-
71-
});
72-
};
70+
}, 'transaction');
7371

7472

7573
/**

config/routes.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ module.exports = function(app) {
1717
app.get(apiPrefix + '/block/:blockHash', blocks.show);
1818
app.param('blockHash', blocks.block);
1919

20-
app.get(apiPrefix + '/block-index/:height', blocks.blockindex);
21-
app.param('height', blocks.blockindex);
20+
app.get(apiPrefix + '/blockheader/:blockHeaderHash', blocks.show);
21+
app.param('blockHeaderHash', blocks.blockHeader);
22+
23+
app.get(apiPrefix + '/block-index/:height', blocks.showBlockHash);
24+
app.param('height', blocks.blockIndex);
25+
26+
app.get(apiPrefix + '/blockheader-by-index/:headerHeight', blocks.show);
27+
app.param('headerHeight', blocks.blockHeaderByIndex);
2228

2329
// Transaction routes
2430
var transactions = require('../app/controllers/transactions');
2531
app.get(apiPrefix + '/tx/:txid', transactions.show);
2632
app.param('txid', transactions.transaction);
2733
app.get(apiPrefix + '/txs', transactions.list);
2834
app.post(apiPrefix + '/tx/send', transactions.send);
35+
app.get(apiPrefix + '/multitx/:txids', transactions.show);
36+
app.param('txids', transactions.transaction);
2937

3038
// Raw Routes
3139
app.get(apiPrefix + '/rawtx/:txid', transactions.showRaw);

lib/BlockDb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ BlockDb.prototype._fillConfirmationsOneVin = function(o, chainHeight, cb) {
382382
o.confirmations = chainHeight - height + 1;
383383
}
384384
o.unconfirmedInput = ! o.isConfirmed;
385+
o.confirmedIn = height;
385386
return cb();
386387
});
387388
};

0 commit comments

Comments
 (0)