Fixed Bcoin error returned as data, fixed dead /txs endpoint missing error, added ttl to bcoin requests but large addrs still take too long

This commit is contained in:
tenthirtyone 2017-08-16 16:30:33 -04:00
parent c47f58f8e8
commit 2ac70bc9a2
5 changed files with 141 additions and 155 deletions

View File

@ -29,6 +29,7 @@ const config = {
ticker_prop: 'bitstamp', ticker_prop: 'bitstamp',
max_blocks: 72, max_blocks: 72,
max_txs: 10, max_txs: 10,
request_ttl: 100000,
}, },
}; };

View File

@ -3,12 +3,14 @@ const request = require('request');
const config = require('../../config'); const config = require('../../config');
const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`; const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`;
const TTL = config.api.request_ttl;
module.exports = function AddressAPI(router) { module.exports = function AddressAPI(router) {
router.get('/addr/:addr', (req, res) => { router.get('/addr/:addr', (req, res) => {
const addr = req.params.addr || ''; const addr = req.params.addr || '';
// Get Bcoin data // Get Bcoin data
return request(`${API_URL}/tx/address/${addr}`, return request(`${API_URL}/tx/address/${addr}`,
{ timeout: TTL },
(error, bcoinRes, bcoinTxs) => { (error, bcoinRes, bcoinTxs) => {
if (error) { if (error) {
logger.log('error', logger.log('error',

View File

@ -5,6 +5,7 @@ const db = require('../db');
const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`; const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`;
const MAX_TXS = config.api.max_txs; const MAX_TXS = config.api.max_txs;
const TTL = config.api.request_ttl;
module.exports = function transactionAPI(router) { module.exports = function transactionAPI(router) {
// Txs by txid // Txs by txid
@ -18,7 +19,9 @@ module.exports = function transactionAPI(router) {
} }
const height = blockHeight; const height = blockHeight;
// Bcoin transaction data // Bcoin transaction data
return request(`${API_URL}/tx/${req.params.txid}`, (error, localRes, tx) => { return request(`${API_URL}/tx/${req.params.txid}`,
{ timeout: TTL },
(error, localRes, tx) => {
if (error) { if (error) {
logger.log('error', logger.log('error',
`${error}`); `${error}`);
@ -83,10 +86,13 @@ module.exports = function transactionAPI(router) {
} }
const height = blockHeight; const height = blockHeight;
// Get Bcoin data // Get Bcoin data
return request(`${API_URL}/block/${req.query.block}`, (error, localRes, block) => { return request(`${API_URL}/block/${req.query.block}`,
{ timeout: TTL },
(error, localRes, block) => {
if (error) { if (error) {
logger.log('error', logger.log('error',
`${error}`); `${error}`);
return res.status(404).send();
} }
// Catch JSON errors // Catch JSON errors
try { try {
@ -96,10 +102,11 @@ module.exports = function transactionAPI(router) {
`${e}`); `${e}`);
return res.status(404).send(); return res.status(404).send();
} }
if (!block.txs.length) {
if (block.error) {
logger.log('error', logger.log('error',
`${'No tx results'}`); `${'No tx results'}`);
res.status(404).send(); return res.status(404).send();
} }
// Setup UI JSON // Setup UI JSON
const totalPages = Math.ceil(block.txs.length / MAX_TXS); const totalPages = Math.ceil(block.txs.length / MAX_TXS);
@ -139,7 +146,12 @@ module.exports = function transactionAPI(router) {
const height = blockHeight; const height = blockHeight;
const addr = req.query.address || ''; const addr = req.query.address || '';
return request(`${API_URL}/tx/address/${addr}`, (error, localRes, txs) => { logger.log('debug',
'Warning: Requesting data from Bcoin, may take some time');
return request(`${API_URL}/tx/address/${addr}`,
{ timeout: TTL },
(error, localRes, txs) => {
if (error) { if (error) {
logger.log('error', logger.log('error',
`${error}`); `${error}`);
@ -153,6 +165,12 @@ module.exports = function transactionAPI(router) {
`${e}`); `${e}`);
return res.status(404).send(); return res.status(404).send();
} }
// Bcoin returns error as part of data object
if (txs.error) {
logger.log('error',
`${'No tx results'}`);
return res.status(404).send();
}
// Setup UI JSON // Setup UI JSON
return res.send({ return res.send({
pagesTotal: 1, pagesTotal: 1,
@ -178,44 +196,7 @@ module.exports = function transactionAPI(router) {
}); });
} else { } else {
// Get last n txs // Get last n txs
db.txs.getTransactions( return res.status(404).send({ error: 'Block hash or address expected' });
{},
{},
MAX_TXS,
(err, txs) => {
if (err) {
logger.log('err',
`getTransactions: ${err}`);
res.status(404).send();
}
return res.json({
pagesTotal: 1,
txs: txs.map(tx => ({
txid: tx.hash,
version: tx.version,
locktime: tx.locktime,
vin: tx.inputs.map(input => ({
coinbase: input.script,
sequence: input.sequence,
n: 0,
})),
vout: tx.outputs.map(output => ({
value: output.value,
n: 0,
scriptPubKey: {
hex: '',
asm: '',
addresses: [output.address],
type: output.type,
},
spentTxid: '',
spentIndex: 0,
spentHeight: 0,
})),
})),
});
},
);
} }
}); });

View File

@ -21,6 +21,7 @@ function getBlocks(params, options, limit, cb) {
if (limit < 1) { if (limit < 1) {
limit = 1; limit = 1;
} }
// Query mongo // Query mongo
Block.find( Block.find(
params, params,
@ -59,7 +60,7 @@ function getBestHeight(cb) {
if (err) { if (err) {
logger.log('error', logger.log('error',
`getBlock: ${err.err}`); `getBlock: ${err.err}`);
return cb(err); return cb(err, null);
} }
return cb(null, block.height); return cb(null, block.height);
}); });

View File

@ -26,6 +26,7 @@ function getTransactions(params, options, limit, cb) {
params, params,
defaultOptions, defaultOptions,
(err, txs) => { (err, txs) => {
console.log(txs)
if (err) { if (err) {
logger.log('error', logger.log('error',
`getTransactions: ${err}`); `getTransactions: ${err}`);