diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index af02bb5..b00a21b 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -2,10 +2,8 @@ const Transactions = require('../../models/transaction.js'); const logger = require('../logger'); const config = require('../../config'); -const txs = new Transactions(); +const Txs = new Transactions(); -// For now, blocks handles these calls. -// These will be replaced with more advanced mongo // No optimization yet. // Will be replaced with a more sophisticated api soon @@ -49,147 +47,44 @@ function getTransactions(params, options, limit, skip, cb) { .limit(limit); } -function getTransaction(params, options, limit, skip, cb) { - getTransactions(params, options, limit, skip, (err, tx) => { - if (err) { - logger.log('error', - `getTransaction: ${err.err}`); - return cb(err); - } - if (!tx.length > 0) { - return cb({ err: 'Tx not found' }); - } - return cb(null, tx[0]); - }); +function getEmptyInputs(cb) { + return Txs.getEmptyInputs(cb); } -// Req Change, refactor above function getTopTransactions(cb) { - // Do not return mongo ids - const defaultOptions = { _id: 0 }; - // Query mongo - Transactions.find( - {}, - (err, txs) => { - if (err) { - logger.log('error', - `getTransactions: ${err}`); - return cb(err); - } - if (!txs.length > 0) { - return cb({ err: 'Tx not found' }); - } - return cb(null, txs); - }) - .sort({ height: -1 }) + return Txs.last(cb) .limit(MAX_TXS); } function getTxById(txid, cb) { - txs.byId(txid, - (err, transaction) => { - if (err) { - logger.log('error', - `getTxById: ${txid} ${err.err}`); - return cb(err); - } - return cb(null, transaction); - }); + return Txs.byId(txid, cb); } function getTxByBlock(blockHash, page, limit, cb) { - getTransactions( - { block: blockHash }, - {}, - limit, - page * limit, - (err, tx) => { - if (err) { - logger.log('error', - `getTxByBlock: ${err.err}`); - return cb(err); - } - if (!tx.length > 0) { - return cb({ err: 'Tx not found' }); - } - return cb(null, tx); - }); + return Txs.byBlockHash(blockHash, cb) + .limit(MAX_TXS); } function getTxByAddress(address, page, limit, cb) { - getTransactions( - { - $or: [ - { 'inputs.address': address }, - { 'outputs.address': address }], - }, - {}, - limit, - page * limit, - (err, tx) => { - if (err) { - logger.log('error', - `getTxByAddress: ${err.err}`); - return cb(err); - } - if (!tx.length > 0) { - return cb({ err: 'Tx not found' }); - } - return cb(null, tx); - }); + return Txs.byAddress(address, cb) + .limit(MAX_TXS); } function getTxCountByBlock(blockHash, cb) { - Transactions.count( - { block: blockHash }, - (err, count) => { - if (err) { - logger.log('error', - `getTxCountByBlock ${err}`); - return cb(err); - } - return cb(null, count); - }); + return Txs.countByBlock(blockHash, cb); } function getTxCountByAddress(address, cb) { - Transactions.count( - { $or: [ - { 'inputs.address': address }, - { 'outputs.address': address }], - }, - (err, count) => { - if (err) { - logger.log('error', - `getTxCountByAddress ${err}`); - return cb(err); - } - return cb(null, count); - }); + return Txs.countByAddress(address, cb); } - function updateInput(txid, inputid, value, address) { - Transactions.findOneAndUpdate( - { _id: txid, 'inputs._id': inputid }, - { - $set: { - 'inputs.$.value': value, - 'inputs.$.address': address, - }, - }, - (err, tx) => { - if (err) { - logger.log('error', - `updateInput: ${err}`); - } - }, - ); + return Txs.updateInput(txid, inputid, value, address); } module.exports = { - getTransaction, getTransactions, + getEmptyInputs, getTopTransactions, getTxById, getTxByBlock, diff --git a/server/lib/parser/transaction.js b/server/lib/parser/transaction.js index 51f853e..febef6f 100644 --- a/server/lib/parser/transaction.js +++ b/server/lib/parser/transaction.js @@ -72,14 +72,7 @@ function parse(entry, txs) { } function findEmptyInputs() { - db.txs.getTransactions( - { - 'inputs.prevout.hash': { $ne: '0000000000000000000000000000000000000000000000000000000000000000' }, - 'inputs.address': '', - }, - {}, - 100, - 0, + db.txs.getEmptyInputs( (err, txs) => { if (err) { return logger.log('error', diff --git a/server/models/transaction.js b/server/models/transaction.js index 1d5c842..d39855a 100644 --- a/server/models/transaction.js +++ b/server/models/transaction.js @@ -31,12 +31,126 @@ TransactionSchema.methods.byId = function txById(txid, cb) { (err, tx) => { if (err) { logger.log('error', - `TransactionSchema.methods.byId: ${err}`); + `byId: ${err}`); return cb(err); } return cb(null, tx); }); }; +TransactionSchema.methods.byHash = function txByHash(hash, cb) { + return this.byId(hash, cb); +}; + +TransactionSchema.methods.byBlockHash = function txByBlockHash(hash, cb) { + return this.model('Transaction').find( + { block: hash }, + (err, txs) => { + if (err) { + logger.log('error', + `byBlockHash: ${err}`); + return cb(err); + } + return cb(null, txs); + }, + ); +}; + +TransactionSchema.methods.byAddress = function txByAddress(address, cb) { + return this.model('Transaction').find( + { + $or: [ + { 'inputs.address': address }, + { 'outputs.address': address }], + }, + (err, tx) => { + if (err) { + logger.log('error', + `byAddress: ${err.err}`); + return cb(err); + } + if (!tx.length > 0) { + return cb({ err: 'Tx not found' }); + } + return cb(null, tx); + }, + ); +}; + +TransactionSchema.methods.countByBlock = function txByAddress(hash, cb) { + return this.model('Transaction').count( + { block: hash }, + (err, count) => { + if (err) { + logger.log('error', + `countByBlock ${err}`); + return cb(err); + } + return cb(null, count); + }, + ); +}; + +TransactionSchema.methods.countByAddress = function txByAddress(address, cb) { + return this.model('Transaction').count( + { + $or: [ + { 'inputs.address': address }, + { 'outputs.address': address }], + }, + (err, count) => { + if (err) { + logger.log('error', + `countByAddress ${err}`); + return cb(err); + } + return cb(null, count); + }, + ); +}; + +TransactionSchema.methods.updateInput = function updateInput(txid, inputid, value, address) { + return this.model('Transaction').findOneAndUpdate( + { _id: txid, 'inputs._id': inputid }, + { + $set: { + 'inputs.$.value': value, + 'inputs.$.address': address, + }, + }, + (err, tx) => { + if (err) { + logger.log('error', + `updateInput: ${err}`); + } + }, + ); +}; + +TransactionSchema.methods.last = function lastTx(cb) { + return this.model('Transaction').find( + {}, + (err, txs) => { + if (err) { + logger.log('error', + `TransactionSchema last: ${err}`); + return cb(err); + } + if (!txs.length > 0) { + return cb({ err: 'Tx not found' }); + } + return cb(null, txs); + }, + ); +}; + +TransactionSchema.methods.getEmptyInputs = function findEmptyInputs(cb) { + return this.model('Transaction').find({ + 'inputs.prevout.hash': { $ne: '0000000000000000000000000000000000000000000000000000000000000000' }, + 'inputs.address': '', + }, + cb); +}; + module.exports = mongoose.model('Transaction', TransactionSchema);