Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds batch request support for block and transaction methods, plus a bit more data to transactions #344

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
return cb(err); // 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);
return cb(err);
} else {
bdb.fromHashWithInfo(hashStr.blockHash, function(err, block) {
if (err || !block)
return 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
21 changes: 21 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,22 @@ 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();
});
};
}
35 changes: 16 additions & 19 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 @@ -40,36 +41,32 @@ exports.send = function(req, res) {
});
};

exports.rawTransaction = function (req, res, next, txid) {
exports.rawTransaction = multi(function(txid, cb) {
bitcoreRpc.getRawTransaction(txid, function (err, transaction) {
if (err || !transaction)
return common.handleErrors(err, res);
return cb(err);
else {
req.rawTransaction = { 'rawtx': transaction.result };
return next();
return cb(null, transaction.result);
}
});
};
}, 'rawTransaction');

/**
* 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
14 changes: 12 additions & 2 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,29 @@ 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);
app.param('txid', transactions.rawTransaction);
app.get(apiPrefix + '/rawmultitx/:txids', transactions.showRaw);
app.param('txids', transactions.rawTransaction);

// Address routes
var addresses = require('../app/controllers/addresses');
Expand Down
2 changes: 2 additions & 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 Expand Up @@ -426,6 +427,7 @@ BlockDb.prototype.fillVinConfirmations = function(tx, cb) {
if (!vin) return cb();

async.eachLimit(vin, CONCURRENCY, function(v, e_c) {
tx.confirmedIn = height - tx.confirmations + 1;
self._fillConfirmationsOneVin(v, height, e_c);
}, cb);
});
Expand Down
24 changes: 16 additions & 8 deletions lib/TransactionDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,18 @@ TransactionDb.prototype._fillSpent = function(info, cb) {
});
};

var valToSat = function(x) { return parseInt(x.replace('.', '')) }

TransactionDb.prototype._fillOutpoints = function(txInfo, cb) {
var self = this;

if (!txInfo || txInfo.isCoinBase) return cb();
if (!txInfo) return cb();

for (var i = 0; i < txInfo.vout.length; i++) {
txInfo.vout[i].valueSat = valToSat(txInfo.vout[i].value);
}

if (txInfo.isCoinBase) return cb();

var valueIn = 0;
var incompleteInputs = 0;
Expand Down Expand Up @@ -227,16 +234,17 @@ TransactionDb.prototype._fillOutpoints = function(txInfo, cb) {
});
},
function() {
if (!incompleteInputs) {
txInfo.valueIn = valueIn / util.COIN;
txInfo.fees = (valueIn - (txInfo.valueOut * util.COIN)).toFixed(0) / util.COIN;
} else {
txInfo.incompleteInputs = 1;
}
return cb();
if (!incompleteInputs) {
txInfo.valueIn = valueIn / util.COIN;
txInfo.fees = (valueIn - (txInfo.valueOut * util.COIN)).toFixed(0) / util.COIN;
} else {
txInfo.incompleteInputs = 1;
}
return cb();
});
};


TransactionDb.prototype._getInfo = function(txid, next) {
var self = this;

Expand Down