From a3941f75efa7dfcca64714db4f992f9f197155d8 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Wed, 23 Aug 2017 23:25:36 -0400 Subject: [PATCH] Update inputs --- server/lib/db/transactions.js | 30 ++++++++++++++++++++++++++++++ server/lib/node/index.js | 8 ++++++++ server/lib/parser/transaction.js | 28 +--------------------------- server/models/transaction.js | 7 ++++--- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index 9c7b724..98d7f0c 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -1,5 +1,6 @@ const Transactions = require('../../models/transaction.js'); const config = require('../../config'); +const logger = require('../logger'); const Txs = new Transactions(); const MAX_PAGE_TXS = config.api.max_page_txs; @@ -39,7 +40,36 @@ function updateInput(txid, inputid, value, address) { return Txs.updateInput(txid, inputid, value, address); } +function auditInputs() { + getEmptyInputs( + (err, txs) => { + if (err) { + return logger.log('error', + `No Empty Inputs found: ${err.err}`); + } + // For each tx with unmarked inputs + logger.log('debug', + `Found ${txs.length} txs with inputs to update`); + + return txs.forEach((inputTx) => { + inputTx.inputs.forEach((input) => { + const txHash = input.prevout.hash; + const outIdx = input.prevout.index; + + return getTxById(txHash, (error, tx) => { + if (error || !tx) { + return logger.log('error', + `No Tx found: ${txHash} ${error}`); + } + return updateInput(inputTx._id, input._id, tx.outputs[outIdx].value, tx.outputs[outIdx].address); + }); + }); + }); + }); +} + module.exports = { + auditInputs, getEmptyInputs, getTopTransactions, getTxById, diff --git a/server/lib/node/index.js b/server/lib/node/index.js index 2f73f29..8bb864e 100644 --- a/server/lib/node/index.js +++ b/server/lib/node/index.js @@ -7,6 +7,7 @@ const socket = require('../../lib/api/socket'); const db = require('../../lib/db'); const node = new FullNode(config.bcoin); +let doneSyncing = false; function start() { node.open() @@ -22,6 +23,13 @@ function start() { 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.on('full', () => { + doneSyncing = true; }); node.on('error', (err) => { diff --git a/server/lib/parser/transaction.js b/server/lib/parser/transaction.js index 73c559e..c6bf908 100644 --- a/server/lib/parser/transaction.js +++ b/server/lib/parser/transaction.js @@ -7,6 +7,7 @@ const logger = require('../logger'); const db = require('../db'); function parse(entry, txs) { + // findEmptyInputs(); txs.forEach((tx) => { const txJSON = tx.toJSON(); const txRAW = tx.toRaw(); @@ -52,37 +53,10 @@ function parse(entry, txs) { if (err) { logger.log('error', err.message); } - - findEmptyInputs(); }); }); } -function findEmptyInputs() { - db.txs.getEmptyInputs( - (err, txs) => { - if (err) { - return logger.log('error', - `No Empty Inputs found: ${err.err}`); - } - // For each tx with unmarked inputs - return txs.forEach((inputTx) => { - inputTx.inputs.forEach((input) => { - const txHash = input.prevout.hash; - const outIdx = input.prevout.index; - - return db.txs.getTxById(txHash, (error, tx) => { - if (error || !tx) { - return logger.log('error', - `No Tx found: ${txHash} ${error}`); - } - return db.txs.updateInput(inputTx._id, input._id, tx.outputs[outIdx].value, tx.outputs[outIdx].address); - }); - }); - }); - }); -} - module.exports = { parse, }; diff --git a/server/models/transaction.js b/server/models/transaction.js index 6a03caf..4c7140c 100644 --- a/server/models/transaction.js +++ b/server/models/transaction.js @@ -87,13 +87,14 @@ TransactionSchema.methods.last = function lastTx(cb) { TransactionSchema.methods.getEmptyInputs = function getEmptyInputs(cb) { return this.model('Transaction').find({ 'inputs.prevout.hash': { $ne: '0000000000000000000000000000000000000000000000000000000000000000' }, - 'inputs.address': '', + 'inputs.value': 0, }, - cb) - .limit(MAX_TXS); + cb); }; TransactionSchema.methods.updateInput = function updateInput(txid, inputid, value, address) { + logger.log('debug', + `${txid} ${address}value is ${value}`); return this.model('Transaction').findOneAndUpdate( { _id: txid, 'inputs._id': inputid }, {