sync & async saving. Bleh

This commit is contained in:
tenthirtyone 2017-08-25 10:47:03 -04:00
parent 12cb8a0c29
commit afc063fb97
5 changed files with 18 additions and 9 deletions

View File

@ -4,7 +4,7 @@ const config = {
bcoin_http: 'localhost',
bcoin: {
network: 'main',
db: 'leveldb',
db: 'mem',
prefix: '.',
checkpoints: true,
workers: false,

View File

@ -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();

View File

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

View File

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

View File

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