check for findOne null

This commit is contained in:
tenthirtyone 2017-08-22 01:17:44 -04:00
parent c2c51e709a
commit 479d55de4a
5 changed files with 9 additions and 32 deletions

View File

@ -15,7 +15,7 @@ module.exports = function BlockAPI(router) {
// Pass Mongo params, fields and limit to db api.
return db.blocks.getByHash(blockHash,
(err, block) => {
if (err) {
if (err || !block) {
logger.log('err', err);
return res.status(404).send();
}
@ -43,7 +43,6 @@ module.exports = function BlockAPI(router) {
});
router.get('/blocks', (req, res) => {
const limit = parseInt(req.query.limit, 10) || 100;
// Pass Mongo params, fields and limit to db api.
db.blocks.getTopBlocks(
(err, blocks) => {
@ -80,7 +79,7 @@ module.exports = function BlockAPI(router) {
// Pass Mongo params, fields and limit to db api.
return db.blocks.getRawBlock(blockHash,
(err, block) => {
if (err) {
if (err || !block) {
logger.log('error',
`/rawblock/:blockHash: ${err}`);
return res.status(404).send();
@ -94,7 +93,7 @@ module.exports = function BlockAPI(router) {
// Pass Mongo params, fields and limit to db api.
return db.blocks.byHeight(height,
(err, block) => {
if (err) {
if (err || !block) {
logger.log('error',
`/block-index/:height: ${err}`);
return res.status(404).send();

View File

@ -1,5 +1,4 @@
const Transactions = require('../../models/transaction.js');
const logger = require('../logger');
const config = require('../../config');
const Txs = new Transactions();

View File

@ -24,18 +24,10 @@ function start() {
db.blocks.bestHeight(entry.height);
});
node.chain.on('full', (block) => {
});
node.on('error', (err) => {
logger.log('error',
`${err}`);
});
node.mempool.on('tx', (tx) => {
socket.emitTx(tx);
});
}
module.exports = {

View File

@ -6,16 +6,7 @@ const util = require('../../lib/util');
const logger = require('../logger');
const db = require('../db');
// Bleh, Bcoin pulls in blocks 20 at a time
// Crappy delay for now otherwise async saves
// could miss a tx if an input refs a block within
// the last 20 that hasn't saved.
// Aggregate stuff will replace all of this.
let counter = 0;
function parse(entry, txs) {
counter++;
txs.forEach((tx) => {
const txJSON = tx.toJSON();
const txRAW = tx.toRaw();
@ -61,12 +52,8 @@ function parse(entry, txs) {
if (err) {
logger.log('error', err.message);
}
// As long as this modulo is divisible by 20 we should be OK for now.
// Closer to 20 = chattier at start and less ideal later on
if (counter % 20 === 0) {
findEmptyInputs();
counter = 0;
}
findEmptyInputs();
});
});
}
@ -84,10 +71,10 @@ function findEmptyInputs() {
const txHash = input.prevout.hash;
const outIdx = input.prevout.index;
return db.txs.getTxById(txHash, (err, tx) => {
if (err) {
return db.txs.getTxById(txHash, (error, tx) => {
if (error || !tx) {
return logger.log('error',
`No Tx found: ${txHash} ${err.err}`);
`No Tx found: ${txHash} ${error}`);
}
return db.txs.updateInput(inputTx._id, input._id, tx.outputs[outIdx].value, tx.outputs[outIdx].address);
});

View File

@ -83,7 +83,7 @@ TransactionSchema.methods.last = function lastTx(cb) {
.sort({ height: -1 });
};
TransactionSchema.methods.getEmptyInputs = function findEmptyInputs(cb) {
TransactionSchema.methods.getEmptyInputs = function getEmptyInputs(cb) {
return this.model('Transaction').find({
'inputs.prevout.hash': { $ne: '0000000000000000000000000000000000000000000000000000000000000000' },
'inputs.address': '',