bestBlockHeight set by node now. Less cb Hell.
This commit is contained in:
parent
1cb0a6b385
commit
3e7f263e18
@ -11,60 +11,53 @@ module.exports = function transactionAPI(router) {
|
|||||||
// Txs by txid
|
// Txs by txid
|
||||||
router.get('/tx/:txid', (req, res) => {
|
router.get('/tx/:txid', (req, res) => {
|
||||||
// Get max block height for calculating confirmations
|
// Get max block height for calculating confirmations
|
||||||
db.blocks.getBestHeight(
|
const height = db.blocks.bestHeight();
|
||||||
(err, blockHeight) => {
|
// Bcoin transaction data
|
||||||
if (err) {
|
return request(`${API_URL}/tx/${req.params.txid}`,
|
||||||
logger.log('err', err);
|
{ timeout: TTL },
|
||||||
|
(error, localRes, tx) => {
|
||||||
|
if (error) {
|
||||||
|
logger.log('error',
|
||||||
|
`${error}`);
|
||||||
|
return res.status(404).send();
|
||||||
|
}
|
||||||
|
// Catch JSON errors
|
||||||
|
try {
|
||||||
|
tx = JSON.parse(tx);
|
||||||
|
} catch (e) {
|
||||||
|
logger.log('error',
|
||||||
|
`${e}`);
|
||||||
|
return res.status(404).send();
|
||||||
|
}
|
||||||
|
if (!tx || !tx.hash) {
|
||||||
|
logger.log('error',
|
||||||
|
'No results found');
|
||||||
return res.status(404).send();
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
const height = blockHeight;
|
|
||||||
// Bcoin transaction data
|
|
||||||
return request(`${API_URL}/tx/${req.params.txid}`,
|
|
||||||
{ timeout: TTL },
|
|
||||||
(error, localRes, tx) => {
|
|
||||||
if (error) {
|
|
||||||
logger.log('error',
|
|
||||||
`${error}`);
|
|
||||||
return res.status(404).send();
|
|
||||||
}
|
|
||||||
// Catch JSON errors
|
|
||||||
try {
|
|
||||||
tx = JSON.parse(tx);
|
|
||||||
} catch (e) {
|
|
||||||
logger.log('error',
|
|
||||||
`${e}`);
|
|
||||||
return res.status(404).send();
|
|
||||||
}
|
|
||||||
if (!tx || !tx.hash) {
|
|
||||||
logger.log('error',
|
|
||||||
'No results found');
|
|
||||||
return res.status(404).send();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return UI JSON
|
// Return UI JSON
|
||||||
return res.send({
|
return res.send({
|
||||||
txid: tx.hash,
|
txid: tx.hash,
|
||||||
version: tx.version,
|
version: tx.version,
|
||||||
time: tx.ps,
|
time: tx.ps,
|
||||||
blocktime: tx.ps,
|
blocktime: tx.ps,
|
||||||
locktime: tx.locktime,
|
locktime: tx.locktime,
|
||||||
blockhash: tx.block,
|
blockhash: tx.block,
|
||||||
fees: tx.fee / 1e8,
|
fees: tx.fee / 1e8,
|
||||||
confirmations: (height - tx.height) + 1,
|
confirmations: (height - tx.height) + 1,
|
||||||
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
||||||
vin: tx.inputs.map(input => ({
|
vin: tx.inputs.map(input => ({
|
||||||
addr: input.coin ? input.coin.address : '',
|
addr: input.coin ? input.coin.address : '',
|
||||||
value: input.coin ? input.coin.value / 1e8 : 0,
|
value: input.coin ? input.coin.value / 1e8 : 0,
|
||||||
})),
|
})),
|
||||||
vout: tx.outputs.map(output => ({
|
vout: tx.outputs.map(output => ({
|
||||||
scriptPubKey: {
|
scriptPubKey: {
|
||||||
addresses: [output.address],
|
addresses: [output.address],
|
||||||
},
|
},
|
||||||
value: output.value / 1e8,
|
value: output.value / 1e8,
|
||||||
})),
|
})),
|
||||||
isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
|
isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -78,126 +71,111 @@ module.exports = function transactionAPI(router) {
|
|||||||
const rangeEnd = rangeStart + MAX_TXS;
|
const rangeEnd = rangeStart + MAX_TXS;
|
||||||
// get txs for blockhash, start with best height to calc confirmations
|
// get txs for blockhash, start with best height to calc confirmations
|
||||||
if (req.query.block) {
|
if (req.query.block) {
|
||||||
db.blocks.getBestHeight(
|
|
||||||
(err, blockHeight) => {
|
const height = db.blocks.bestHeight();
|
||||||
if (err) {
|
// Get Bcoin data
|
||||||
logger.log('err', err);
|
return request(`${API_URL}/block/${req.query.block}`,
|
||||||
|
{ timeout: TTL },
|
||||||
|
(error, localRes, block) => {
|
||||||
|
if (error) {
|
||||||
|
logger.log('error',
|
||||||
|
`${error}`);
|
||||||
|
return res.status(404).send();
|
||||||
|
}
|
||||||
|
// Catch JSON errors
|
||||||
|
try {
|
||||||
|
block = JSON.parse(block);
|
||||||
|
} catch (e) {
|
||||||
|
logger.log('error',
|
||||||
|
`${e}`);
|
||||||
return res.status(404).send();
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
const height = blockHeight;
|
|
||||||
// Get Bcoin data
|
|
||||||
return request(`${API_URL}/block/${req.query.block}`,
|
|
||||||
{ timeout: TTL },
|
|
||||||
(error, localRes, block) => {
|
|
||||||
if (error) {
|
|
||||||
logger.log('error',
|
|
||||||
`${error}`);
|
|
||||||
return res.status(404).send();
|
|
||||||
}
|
|
||||||
// Catch JSON errors
|
|
||||||
try {
|
|
||||||
block = JSON.parse(block);
|
|
||||||
} catch (e) {
|
|
||||||
logger.log('error',
|
|
||||||
`${e}`);
|
|
||||||
return res.status(404).send();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (block.error) {
|
if (block.error) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
`${'No tx results'}`);
|
`${'No tx results'}`);
|
||||||
return 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);
|
||||||
block.txs = block.txs.slice(rangeStart, rangeEnd);
|
block.txs = block.txs.slice(rangeStart, rangeEnd);
|
||||||
|
|
||||||
return res.send({
|
return res.send({
|
||||||
pagesTotal: totalPages,
|
pagesTotal: totalPages,
|
||||||
txs: block.txs.map(tx => ({
|
txs: block.txs.map(tx => ({
|
||||||
txid: tx.hash,
|
txid: tx.hash,
|
||||||
fees: tx.fee / 1e8,
|
fees: tx.fee / 1e8,
|
||||||
confirmations: (height - block.height) + 1,
|
confirmations: (height - block.height) + 1,
|
||||||
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
||||||
vin: tx.inputs.map(input => ({
|
vin: tx.inputs.map(input => ({
|
||||||
addr: input.coin ? input.coin.address : '',
|
addr: input.coin ? input.coin.address : '',
|
||||||
value: input.coin ? input.coin.value / 1e8 : 0,
|
value: input.coin ? input.coin.value / 1e8 : 0,
|
||||||
})),
|
})),
|
||||||
vout: tx.outputs.map(output => ({
|
vout: tx.outputs.map(output => ({
|
||||||
scriptPubKey: {
|
scriptPubKey: {
|
||||||
addresses: [output.address],
|
addresses: [output.address],
|
||||||
},
|
},
|
||||||
value: output.value / 1e8,
|
value: output.value / 1e8,
|
||||||
})),
|
})),
|
||||||
isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
|
isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
|
||||||
})),
|
})),
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
} else if (req.query.address) {
|
} else if (req.query.address) {
|
||||||
// Get txs by address, start with best height to calc confirmations
|
// Get txs by address, start with best height to calc confirmations
|
||||||
db.blocks.getBestHeight(
|
const height = db.blocks.bestHeight();
|
||||||
(err, blockHeight) => {
|
const addr = req.query.address || '';
|
||||||
if (err) {
|
|
||||||
logger.log('err', err);
|
logger.log('debug',
|
||||||
|
'Warning: Requesting data from Bcoin by address, may take some time');
|
||||||
|
|
||||||
|
return request(`${API_URL}/tx/address/${addr}`,
|
||||||
|
{ timeout: TTL },
|
||||||
|
(error, localRes, txs) => {
|
||||||
|
if (error) {
|
||||||
|
logger.log('error',
|
||||||
|
`${error}`);
|
||||||
return res.status(404).send();
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
|
// Catch JSON errors
|
||||||
const height = blockHeight;
|
try {
|
||||||
const addr = req.query.address || '';
|
txs = JSON.parse(txs);
|
||||||
|
} catch (e) {
|
||||||
logger.log('debug',
|
logger.log('error',
|
||||||
'Warning: Requesting data from Bcoin by address, may take some time');
|
`${e}`);
|
||||||
|
return res.status(404).send();
|
||||||
return request(`${API_URL}/tx/address/${addr}`,
|
}
|
||||||
{ timeout: TTL },
|
// Bcoin returns error as part of data object
|
||||||
(error, localRes, txs) => {
|
if (txs.error) {
|
||||||
if (error) {
|
logger.log('error',
|
||||||
logger.log('error',
|
`${'No tx results'}`);
|
||||||
`${error}`);
|
return res.status(404).send();
|
||||||
return res.status(404).send();
|
}
|
||||||
}
|
// Setup UI JSON
|
||||||
// Catch JSON errors
|
return res.send({
|
||||||
try {
|
pagesTotal: 1,
|
||||||
txs = JSON.parse(txs);
|
txs: txs.map(tx => ({
|
||||||
} catch (e) {
|
txid: tx.hash,
|
||||||
logger.log('error',
|
fees: tx.fee / 1e8,
|
||||||
`${e}`);
|
confirmations: (height - tx.height) + 1,
|
||||||
return res.status(404).send();
|
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
||||||
}
|
vin: tx.inputs.map(input => ({
|
||||||
// Bcoin returns error as part of data object
|
addr: input.coin ? input.coin.address : '',
|
||||||
if (txs.error) {
|
value: input.coin ? input.coin.value / 1e8 : 0,
|
||||||
logger.log('error',
|
})),
|
||||||
`${'No tx results'}`);
|
vout: tx.outputs.map(output => ({
|
||||||
return res.status(404).send();
|
scriptPubKey: {
|
||||||
}
|
addresses: [output.address],
|
||||||
// Setup UI JSON
|
},
|
||||||
return res.send({
|
value: output.value / 1e8,
|
||||||
pagesTotal: 1,
|
})),
|
||||||
txs: txs.map(tx => ({
|
isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
|
||||||
txid: tx.hash,
|
})),
|
||||||
fees: tx.fee / 1e8,
|
});
|
||||||
confirmations: (height - tx.height) + 1,
|
|
||||||
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
|
||||||
vin: tx.inputs.map(input => ({
|
|
||||||
addr: input.coin ? input.coin.address : '',
|
|
||||||
value: input.coin ? input.coin.value / 1e8 : 0,
|
|
||||||
})),
|
|
||||||
vout: tx.outputs.map(output => ({
|
|
||||||
scriptPubKey: {
|
|
||||||
addresses: [output.address],
|
|
||||||
},
|
|
||||||
value: output.value / 1e8,
|
|
||||||
})),
|
|
||||||
isCoinBase: tx.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
|
|
||||||
})),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
// Get last n txs
|
|
||||||
return res.status(404).send({ error: 'Block hash or address expected' });
|
|
||||||
}
|
}
|
||||||
|
// Get last n txs
|
||||||
|
return res.status(404).send({ error: 'Block hash or address expected' });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/rawtx/:txid', (req, res) => {
|
router.get('/rawtx/:txid', (req, res) => {
|
||||||
|
|||||||
@ -4,6 +4,8 @@ const config = require('../../config');
|
|||||||
|
|
||||||
const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours
|
const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours
|
||||||
|
|
||||||
|
let bestBlockHeight = 0;
|
||||||
|
|
||||||
function getBlocks(params, options, limit, cb) {
|
function getBlocks(params, options, limit, cb) {
|
||||||
// Do not return mongo ids
|
// Do not return mongo ids
|
||||||
const defaultOptions = { _id: 0 };
|
const defaultOptions = { _id: 0 };
|
||||||
@ -54,20 +56,31 @@ function getBlock(params, options, limit, cb) {
|
|||||||
return cb(null, blocks[0]);
|
return cb(null, blocks[0]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Highest known height
|
// Highest known height in mongo
|
||||||
function getBestHeight(cb) {
|
function getBestHeight() {
|
||||||
getBlock({}, {}, 1, (err, block) => {
|
getBlock({}, {}, 1, (err, block) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
`getBlock: ${err.err}`);
|
`getBestHeight: ${err.err}`);
|
||||||
return cb(err, null);
|
return;
|
||||||
}
|
}
|
||||||
return cb(null, block.height);
|
bestBlockHeight = block.height;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
// 1e9 limit = ~2M years from now
|
||||||
|
// Mostly for sync to set height
|
||||||
|
function bestHeight(height) {
|
||||||
|
if (Number.isInteger(height) &&
|
||||||
|
height > 0 &&
|
||||||
|
height < 1 * 1e9) {
|
||||||
|
bestBlockHeight = height;
|
||||||
|
return bestBlockHeight;
|
||||||
|
}
|
||||||
|
return bestBlockHeight;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getBlock,
|
getBlock,
|
||||||
getBlocks,
|
getBlocks,
|
||||||
getBestHeight,
|
bestHeight,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -2,6 +2,9 @@ const Transactions = require('../../models/transaction.js');
|
|||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
|
|
||||||
|
// For now, blocks handles these calls.
|
||||||
|
// These will be replaced with more advanced mongo
|
||||||
|
|
||||||
const MAX_TXS = config.api.max_txs;
|
const MAX_TXS = config.api.max_txs;
|
||||||
|
|
||||||
function getTransactions(params, options, limit, cb) {
|
function getTransactions(params, options, limit, cb) {
|
||||||
@ -26,7 +29,6 @@ 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}`);
|
||||||
|
|||||||
@ -3,6 +3,7 @@ const logger = require('../../lib/logger');
|
|||||||
const BlockParser = require('../parser').Block;
|
const BlockParser = require('../parser').Block;
|
||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
const socket = require('../../lib/api/socket');
|
const socket = require('../../lib/api/socket');
|
||||||
|
const db = require('../../lib/db');
|
||||||
|
|
||||||
const node = new FullNode(config.bcoin);
|
const node = new FullNode(config.bcoin);
|
||||||
|
|
||||||
@ -18,6 +19,7 @@ function start() {
|
|||||||
node.chain.on('connect', (entry, block) => {
|
node.chain.on('connect', (entry, block) => {
|
||||||
BlockParser.parse(entry, block);
|
BlockParser.parse(entry, block);
|
||||||
socket.processBlock(entry, block);
|
socket.processBlock(entry, block);
|
||||||
|
db.blocks.bestHeight(entry.height);
|
||||||
});
|
});
|
||||||
|
|
||||||
node.on('error', (err) => {
|
node.on('error', (err) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user