diff --git a/server/config/index.js b/server/config/index.js index 011f3d8..79c87f3 100644 --- a/server/config/index.js +++ b/server/config/index.js @@ -4,7 +4,7 @@ const config = { bcoin_http: 'localhost', bcoin: { network: 'main', - db: 'leveldb', + db: 'mem', prefix: '.', checkpoints: true, workers: false, diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index a882ced..1552e07 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -23,7 +23,7 @@ module.exports = function transactionAPI(router) { const txid = req.params.txid || ''; db.txs.getTxById(txid, (err, transaction) => { - if (err) { + if (err || !transaction) { logger.log('error', `/tx/:tid getTxById: ${err.err}`); return res.status(404).send(); diff --git a/server/lib/db/index.js b/server/lib/db/index.js index 974ca5b..65af60d 100644 --- a/server/lib/db/index.js +++ b/server/lib/db/index.js @@ -11,9 +11,13 @@ mongoose.connection.on('error', (err) => { process.on('SIGINT', gracefulExit).on('SIGTERM', gracefulExit); +// Catastrophic Fails can still result in data loss function gracefulExit() { + logger.log('debug', + 'Graceful Shutdown Starting...'); mongoose.connection.close(() => { - console.log('Mongoose connection with DB disconnected through app termination'); + logger.log('debug', + 'Mongoose connection with DB disconnected through app termination'); process.exit(0); }); } diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index 6f84645..35dde60 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -45,7 +45,7 @@ function auditInputs() { getEmptyInputs( (err, txs) => { if (err) { - return logger.log('error', + return logger.log('warn', `No Empty Inputs found: ${err.err}`); } // For each tx with unmarked inputs @@ -56,7 +56,8 @@ function auditInputs() { return getTxById(txHash, (error, tx) => { if (error || !tx) { - return logger.log('error', + // Mongo save is async. Bcoin is kinda sync... Does not mean the tx will not be found + return logger.log('warn', `No Tx found: ${txHash} ${error}`); } return updateInput(inputTx._id, input._id, tx.outputs[outIdx].value, tx.outputs[outIdx].address); diff --git a/server/lib/node/index.js b/server/lib/node/index.js index a3b4b00..458c872 100644 --- a/server/lib/node/index.js +++ b/server/lib/node/index.js @@ -6,9 +6,10 @@ const config = require('../../config'); const socket = require('../../lib/api/socket'); const db = require('../../lib/db'); -const node = new FullNode(config.bcoin); +const node = new FullNode(config.bcoin); let doneSyncing = false; + function start(bestBlockHeight) { node.open() .then(() => { @@ -20,13 +21,16 @@ function start(bestBlockHeight) { }); node.chain.on('connect', (entry, block) => { + // Saved block acts like a journal BlockParser.parse(entry, block); TxParser.parse(entry, block.txs); socket.processBlock(entry, block); db.blocks.bestHeight(entry.height); - if (entry.height % 20 === 0 || doneSyncing) { - db.txs.auditInputs(); - } + + node.chain.db.getBlockView(block) + .then((view) => { + console.log(view); + }); }); node.chain.on('full', () => {