Update inputs

This commit is contained in:
tenthirtyone 2017-08-23 23:25:36 -04:00
parent c560ae3cff
commit a3941f75ef
4 changed files with 43 additions and 30 deletions

View File

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

View File

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

View File

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

View File

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