bestBlockHeight set by node now. Less cb Hell.

This commit is contained in:
tenthirtyone 2017-08-16 21:56:56 -04:00
parent 1cb0a6b385
commit 3e7f263e18
4 changed files with 162 additions and 167 deletions

View File

@ -11,13 +11,7 @@ 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) => {
if (err) {
logger.log('err', err);
return res.status(404).send();
}
const height = blockHeight;
// Bcoin transaction data // Bcoin transaction data
return request(`${API_URL}/tx/${req.params.txid}`, return request(`${API_URL}/tx/${req.params.txid}`,
{ timeout: TTL }, { timeout: TTL },
@ -66,7 +60,6 @@ module.exports = function transactionAPI(router) {
}); });
}); });
}); });
});
// /txs is overloaded. Next ver separate concerns // /txs is overloaded. Next ver separate concerns
// query by block // query by block
@ -78,13 +71,8 @@ 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) {
logger.log('err', err);
return res.status(404).send();
}
const height = blockHeight;
// Get Bcoin data // Get Bcoin data
return request(`${API_URL}/block/${req.query.block}`, return request(`${API_URL}/block/${req.query.block}`,
{ timeout: TTL }, { timeout: TTL },
@ -133,17 +121,9 @@ module.exports = function transactionAPI(router) {
})), })),
}); });
}); });
});
} 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) => {
if (err) {
logger.log('err', err);
return res.status(404).send();
}
const height = blockHeight;
const addr = req.query.address || ''; const addr = req.query.address || '';
logger.log('debug', logger.log('debug',
@ -193,11 +173,9 @@ module.exports = function transactionAPI(router) {
})), })),
}); });
}); });
}); }
} else {
// Get last n txs // Get last n txs
return res.status(404).send({ error: 'Block hash or address expected' }); return res.status(404).send({ error: 'Block hash or address expected' });
}
}); });
router.get('/rawtx/:txid', (req, res) => { router.get('/rawtx/:txid', (req, res) => {

View File

@ -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,
}; };

View File

@ -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}`);

View File

@ -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) => {