cleaned up tx db. Moved logic to models. API next
This commit is contained in:
parent
efe19444d5
commit
050f45015a
@ -2,10 +2,8 @@ const Transactions = require('../../models/transaction.js');
|
|||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
const config = require('../../config');
|
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.
|
// No optimization yet.
|
||||||
// Will be replaced with a more sophisticated api soon
|
// Will be replaced with a more sophisticated api soon
|
||||||
|
|
||||||
@ -49,147 +47,44 @@ function getTransactions(params, options, limit, skip, cb) {
|
|||||||
.limit(limit);
|
.limit(limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTransaction(params, options, limit, skip, cb) {
|
function getEmptyInputs(cb) {
|
||||||
getTransactions(params, options, limit, skip, (err, tx) => {
|
return Txs.getEmptyInputs(cb);
|
||||||
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]);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Req Change, refactor above
|
|
||||||
function getTopTransactions(cb) {
|
function getTopTransactions(cb) {
|
||||||
// Do not return mongo ids
|
return Txs.last(cb)
|
||||||
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 })
|
|
||||||
.limit(MAX_TXS);
|
.limit(MAX_TXS);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTxById(txid, cb) {
|
function getTxById(txid, cb) {
|
||||||
txs.byId(txid,
|
return Txs.byId(txid, cb);
|
||||||
(err, transaction) => {
|
|
||||||
if (err) {
|
|
||||||
logger.log('error',
|
|
||||||
`getTxById: ${txid} ${err.err}`);
|
|
||||||
return cb(err);
|
|
||||||
}
|
|
||||||
return cb(null, transaction);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTxByBlock(blockHash, page, limit, cb) {
|
function getTxByBlock(blockHash, page, limit, cb) {
|
||||||
getTransactions(
|
return Txs.byBlockHash(blockHash, cb)
|
||||||
{ block: blockHash },
|
.limit(MAX_TXS);
|
||||||
{},
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTxByAddress(address, page, limit, cb) {
|
function getTxByAddress(address, page, limit, cb) {
|
||||||
getTransactions(
|
return Txs.byAddress(address, cb)
|
||||||
{
|
.limit(MAX_TXS);
|
||||||
$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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTxCountByBlock(blockHash, cb) {
|
function getTxCountByBlock(blockHash, cb) {
|
||||||
Transactions.count(
|
return Txs.countByBlock(blockHash, cb);
|
||||||
{ block: blockHash },
|
|
||||||
(err, count) => {
|
|
||||||
if (err) {
|
|
||||||
logger.log('error',
|
|
||||||
`getTxCountByBlock ${err}`);
|
|
||||||
return cb(err);
|
|
||||||
}
|
|
||||||
return cb(null, count);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTxCountByAddress(address, cb) {
|
function getTxCountByAddress(address, cb) {
|
||||||
Transactions.count(
|
return Txs.countByAddress(address, cb);
|
||||||
{ $or: [
|
|
||||||
{ 'inputs.address': address },
|
|
||||||
{ 'outputs.address': address }],
|
|
||||||
},
|
|
||||||
(err, count) => {
|
|
||||||
if (err) {
|
|
||||||
logger.log('error',
|
|
||||||
`getTxCountByAddress ${err}`);
|
|
||||||
return cb(err);
|
|
||||||
}
|
|
||||||
return cb(null, count);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function updateInput(txid, inputid, value, address) {
|
function updateInput(txid, inputid, value, address) {
|
||||||
Transactions.findOneAndUpdate(
|
return Txs.updateInput(txid, inputid, value, address);
|
||||||
{ _id: txid, 'inputs._id': inputid },
|
|
||||||
{
|
|
||||||
$set: {
|
|
||||||
'inputs.$.value': value,
|
|
||||||
'inputs.$.address': address,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
(err, tx) => {
|
|
||||||
if (err) {
|
|
||||||
logger.log('error',
|
|
||||||
`updateInput: ${err}`);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getTransaction,
|
|
||||||
getTransactions,
|
getTransactions,
|
||||||
|
getEmptyInputs,
|
||||||
getTopTransactions,
|
getTopTransactions,
|
||||||
getTxById,
|
getTxById,
|
||||||
getTxByBlock,
|
getTxByBlock,
|
||||||
|
|||||||
@ -72,14 +72,7 @@ function parse(entry, txs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function findEmptyInputs() {
|
function findEmptyInputs() {
|
||||||
db.txs.getTransactions(
|
db.txs.getEmptyInputs(
|
||||||
{
|
|
||||||
'inputs.prevout.hash': { $ne: '0000000000000000000000000000000000000000000000000000000000000000' },
|
|
||||||
'inputs.address': '',
|
|
||||||
},
|
|
||||||
{},
|
|
||||||
100,
|
|
||||||
0,
|
|
||||||
(err, txs) => {
|
(err, txs) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return logger.log('error',
|
return logger.log('error',
|
||||||
|
|||||||
@ -31,12 +31,126 @@ TransactionSchema.methods.byId = function txById(txid, cb) {
|
|||||||
(err, tx) => {
|
(err, tx) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
`TransactionSchema.methods.byId: ${err}`);
|
`byId: ${err}`);
|
||||||
return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
return cb(null, tx);
|
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);
|
module.exports = mongoose.model('Transaction', TransactionSchema);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user