From 3cd3c4f6572bacbb5f19f0115d17f356f51dde44 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Wed, 22 Jul 2015 16:11:31 +0200 Subject: [PATCH] Added batch query support, plus a bit more information to some queries --- app/controllers/blocks.js | 63 ++++++++++++++++++++++++++++----- app/controllers/common.js | 20 +++++++++++ app/controllers/transactions.js | 26 +++++++------- config/routes.js | 12 +++++-- lib/BlockDb.js | 1 + 5 files changed, 98 insertions(+), 24 deletions(-) diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js index 24788120a..0b94ba233 100644 --- a/app/controllers/blocks.js +++ b/app/controllers/blocks.js @@ -4,6 +4,7 @@ * Module dependencies. */ var common = require('./common'); +var multi = common.multi; var async = require('async'); var bdb = require('../../lib/BlockDb').default(); var tdb = require('../../lib/TransactionDb').default(); @@ -11,19 +12,34 @@ var tdb = require('../../lib/TransactionDb').default(); /** * Find block by hash ... */ -exports.block = function(req, res, next, hash) { +exports.block = multi(function(hash, cb) { bdb.fromHashWithInfo(hash, function(err, block) { if (err || !block) return common.handleErrors(err, res, next); else { tdb.getPoolInfo(block.info.tx[0], function(info) { block.info.poolInfo = info; - req.block = block.info; - return next(); + cb(null, block.info); }); } }); -}; +}, 'block'); + + +/** + * Find block header by hash ... + */ +exports.blockHeader = multi(function(hash, cb) { + bdb.fromHashWithInfo(hash, function(err, block) { + if (err || !block) + return common.handleErrors(err, res, next); + else { + delete block.info.tx; + cb(null, block.info); + } + }); +}, 'block'); + /** @@ -35,19 +51,50 @@ exports.show = function(req, res) { } }; +/** + * Show block hash + */ +exports.showBlockHash = function(req, res) { + if (req.blockHash) { + res.jsonp(req.blockHash); + } +}; + /** * Show block by Height */ -exports.blockindex = function(req, res, next, height) { +exports.blockIndex = multi(function(height, cb) { bdb.blockIndex(height, function(err, hashStr) { if (err) { console.log(err); - res.status(400).send('Bad Request'); // TODO + cb('Bad Request'); // TODO } else { - res.jsonp(hashStr); + cb(null, hashStr); } }); -}; +}, 'blockHash'); + + +/** + * Show block header by Height + */ +exports.blockHeaderByIndex = multi(function(height, cb) { + bdb.blockIndex(height, function(err, hashStr) { + if (err) { + console.log(err); + cb('Bad Request'); + } else { + bdb.fromHashWithInfo(hashStr.blockHash, function(err, block) { + if (err || !block) + cb(err); + else { + delete block.info.tx; + cb(null, block.info); + } + }); + } + }); +}, 'block'); var getBlock = function(blockhash, cb) { bdb.fromHashWithInfo(blockhash, function(err, block) { diff --git a/app/controllers/common.js b/app/controllers/common.js index d7fb9af67..c74c56804 100644 --- a/app/controllers/common.js +++ b/app/controllers/common.js @@ -1,5 +1,7 @@ 'use strict'; +var async = require('async'); + exports.notReady = function (err, res, p) { res.status(503).send('Server not yet ready. Sync Percentage:' + p); }; @@ -17,3 +19,21 @@ exports.handleErrors = function (err, res) { res.status(404).send('Not found'); } }; + +exports.multi = function(f, outkey) { + return function(req, res, next, inputdata) { + var inputs; + if (inputdata.indexOf(',') >= 0) { + inputs = inputdata.split(','); + } + else inputs = [inputdata]; + async.mapSeries(inputs, f, function(err, results) { + if (err) + return exports.handleErrors(err, res); + req[outkey] = results; + if (req[outkey].length == 1) + req[outkey] = req[outkey][0] + return next(); + }); + }; +} diff --git a/app/controllers/transactions.js b/app/controllers/transactions.js index bb1621918..e5b1b326f 100644 --- a/app/controllers/transactions.js +++ b/app/controllers/transactions.js @@ -6,6 +6,7 @@ var Address = require('../models/Address'); var async = require('async'); var common = require('./common'); +var multi = common.multi; var util = require('util'); var Rpc = require('../../lib/Rpc'); @@ -54,22 +55,19 @@ exports.rawTransaction = function (req, res, next, txid) { /** * Find transaction by hash ... */ -exports.transaction = function(req, res, next, txid) { - - tDb.fromIdWithInfo(txid, function(err, tx) { - if (err || ! tx) - return common.handleErrors(err, res); - - bdb.fillVinConfirmations(tx.info, function(err) { - if (err) - return common.handleErrors(err, res); +exports.transaction = multi(function(txid, cb) { + tDb.fromIdWithInfo(txid, function(err, tx) { + if (err || ! tx) + return cb(err); + + bdb.fillVinConfirmations(tx.info, function(err) { + if (err) + return cb(err); + return cb(null, tx.info); + }); - req.transaction = tx.info; - return next(); }); - - }); -}; +}, 'transaction'); /** diff --git a/config/routes.js b/config/routes.js index d4ba9521f..2caceca35 100644 --- a/config/routes.js +++ b/config/routes.js @@ -17,8 +17,14 @@ module.exports = function(app) { app.get(apiPrefix + '/block/:blockHash', blocks.show); app.param('blockHash', blocks.block); - app.get(apiPrefix + '/block-index/:height', blocks.blockindex); - app.param('height', blocks.blockindex); + app.get(apiPrefix + '/blockheader/:blockHeaderHash', blocks.show); + app.param('blockHeaderHash', blocks.blockHeader); + + app.get(apiPrefix + '/block-index/:height', blocks.showBlockHash); + app.param('height', blocks.blockIndex); + + app.get(apiPrefix + '/blockheader-by-index/:headerHeight', blocks.show); + app.param('headerHeight', blocks.blockHeaderByIndex); // Transaction routes var transactions = require('../app/controllers/transactions'); @@ -26,6 +32,8 @@ module.exports = function(app) { app.param('txid', transactions.transaction); app.get(apiPrefix + '/txs', transactions.list); app.post(apiPrefix + '/tx/send', transactions.send); + app.get(apiPrefix + '/multitx/:txids', transactions.show); + app.param('txids', transactions.transaction); // Raw Routes app.get(apiPrefix + '/rawtx/:txid', transactions.showRaw); diff --git a/lib/BlockDb.js b/lib/BlockDb.js index 41c5b86ea..1c50eb461 100644 --- a/lib/BlockDb.js +++ b/lib/BlockDb.js @@ -382,6 +382,7 @@ BlockDb.prototype._fillConfirmationsOneVin = function(o, chainHeight, cb) { o.confirmations = chainHeight - height + 1; } o.unconfirmedInput = ! o.isConfirmed; + o.confirmedIn = height; return cb(); }); };