From 2eac46fdb593aff0372832586d029a246cc73f37 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Wed, 16 Aug 2017 00:17:45 -0400 Subject: [PATCH] transactions added to db api --- server/lib/api/transaction.js | 32 ++++++------------- server/lib/db/blocks.js | 7 ++-- server/lib/db/index.js | 2 ++ server/lib/db/transactions.js | 60 +++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 26 deletions(-) create mode 100644 server/lib/db/transactions.js diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index e074773..fef4efc 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -1,24 +1,10 @@ -const Transaction = require('../../models/transaction'); const logger = require('../logger'); const request = require('request'); const config = require('../../config'); const db = require('../db'); -const MAX_TXS = 10; const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`; - -function getTransactions(params, options, cb) { - const defaultOptions = { _id: 0 }; - - Object.assign(defaultOptions, options); - - Transaction.find( - params, - defaultOptions, - cb) - .sort({ height: 1 }) - .limit(MAX_TXS); -} +const MAX_TXS = 50; module.exports = function transactionAPI(router) { router.get('/tx/:txid', (req, res) => { @@ -38,22 +24,21 @@ module.exports = function transactionAPI(router) { if (error) { logger.log('error', `${error}`); + return res.status(404).send(); } try { body = JSON.parse(body); } catch (e) { logger.log('error', `${e}`); - res.status(404).send(); - return; + return res.status(404).send(); } if (!body || !body.hash) { logger.log('error', 'No results found'); - res.status(404).send(); - return; + return res.status(404).send(); } - res.send({ + return res.send({ txid: body.hash, version: body.version, time: body.ps, @@ -188,14 +173,17 @@ module.exports = function transactionAPI(router) { }); }); } else { - getTransactions( + db.txs.getTransactions( {}, {}, + 50, (err, txs) => { if (err) { + logger.log('err', + `getTransactions: ${err}`); res.status(404).send(); } - res.json({ + return res.json({ pagesTotal: 1, txs: txs.map(tx => ({ txid: tx.hash, diff --git a/server/lib/db/blocks.js b/server/lib/db/blocks.js index fcaa63b..a417194 100644 --- a/server/lib/db/blocks.js +++ b/server/lib/db/blocks.js @@ -3,8 +3,7 @@ const logger = require('../logger'); const config = require('../../config'); // move to config -const MAX_BLOCKS = 50; -const blockTemplate = new Block(); +const MAX_BLOCKS = 72; // ~ 12 hours function getBlocks(params, options, limit, cb) { const defaultOptions = { _id: 0 }; @@ -33,7 +32,7 @@ function getBlocks(params, options, limit, cb) { return cb(err); } if (!blocks.length > 0) { - return cb({err: 'Block not found'}); + return cb({ err: 'Block not found' }); } return cb(null, blocks); }) @@ -49,7 +48,7 @@ function getBlock(params, options, limit, cb) { return cb(err); } if (!blocks.length > 0) { - return cb(null, blockTemplate); + return cb({ err: 'Block not found' }); } return cb(null, blocks[0]); }); diff --git a/server/lib/db/index.js b/server/lib/db/index.js index 72d75ba..32e5be1 100644 --- a/server/lib/db/index.js +++ b/server/lib/db/index.js @@ -1,6 +1,7 @@ const mongoose = require('mongoose'); const logger = require('../logger'); const Blocks = require('./blocks'); +const Txs = require('./transactions'); mongoose.connection.on('error', (err) => { logger.log('error', @@ -12,4 +13,5 @@ module.exports = { connect: mongoose.connect, connection: mongoose.connection, blocks: Blocks, + txs: Txs, }; diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js new file mode 100644 index 0000000..2d160ab --- /dev/null +++ b/server/lib/db/transactions.js @@ -0,0 +1,60 @@ +const Transactions = require('../../models/transaction.js'); +const logger = require('../logger'); +const config = require('../../config'); + +// move to config +const MAX_TXS = 50; + +function getTransactions(params, options, limit, cb) { + const defaultOptions = { _id: 0 }; + + Object.assign(defaultOptions, options); + + if (!Number.isInteger(limit)) { + limit = 1; + } + + if (limit > MAX_TXS) { + limit = MAX_TXS; + } + + if (limit < 1) { + limit = 1; + } + + Transactions.find( + params, + defaultOptions, + (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(limit); +} + +function getTransaction(params, options, limit, cb) { + getTransactions(params, options, limit, (err, tx) => { + if (err) { + logger.log('error', + `getBlock: ${err.err}`); + return cb(err); + } + if (!tx.length > 0) { + return cb({ err: 'Tx not found' }); + } + return cb(null, tx[0]); + }); +} + +module.exports = { + getTransaction, + getTransactions, +};