Skip to content

Commit

Permalink
Added batch query support, plus a bit more information to some queries
Browse files Browse the repository at this point in the history
  • Loading branch information
LefterisJP committed Jul 22, 2015
1 parent d987571 commit 3cd3c4f
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 24 deletions.
63 changes: 55 additions & 8 deletions app/controllers/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,42 @@
* 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();

/**
* 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');



/**
Expand All @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/common.js
Original file line number Diff line number Diff line change
@@ -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);
};
Expand All @@ -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();
});
};
}
26 changes: 12 additions & 14 deletions app/controllers/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');


/**
Expand Down
12 changes: 10 additions & 2 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ 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');
app.get(apiPrefix + '/tx/:txid', transactions.show);
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);
Expand Down
1 change: 1 addition & 0 deletions lib/BlockDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
};
Expand Down

0 comments on commit 3cd3c4f

Please sign in to comment.