Block API dried up and mostly complete. Blocks still needs date params support

This commit is contained in:
tenthirtyone 2017-08-04 15:39:04 -04:00
parent 7afbdb41b1
commit 6e70484362
3 changed files with 80 additions and 36 deletions

View File

@ -60,4 +60,5 @@ The API is configured to run on port 3000 by default. Use the standard Nginx rev
* JSDoc & Unit tests * JSDoc & Unit tests
* Rate Limiting * Rate Limiting
* Helmet * Helmet
* Rate Limiting * Rate Limiting
* Sanitize user input - mongo and api params. Just make a quick middleware

View File

@ -1,44 +1,87 @@
const Block = require('../../models/block.js'); const Block = require('../../models/block.js');
const logger = require('../logger'); const logger = require('../logger');
module.exports = function BlockAPI(app) { const MAX_BLOCKS = 200;
app.get('/block/:blockHash', (req, res) => {
getBlock(res,
{ hash: req.params.blockHash },
{ rawBlock: 0 });
});
app.get('/blocks', (req, res) => { function getBlock(params, options, cb) {
res.send({
blocks: [],
length: 0,
pagination: {
},
});
});
app.get('/rawblock/:blockHash', (req, res) => {
res.send(req.params.blockHash);
});
app.get('/block-index/:height', (req, res) => {
getBlock(res, { height: req.params.height });
});
};
function getBlock(res, params, options) {
const defaultOptions = { _id: 0 }; const defaultOptions = { _id: 0 };
Object.assign(defaultOptions, options); Object.assign(defaultOptions, options);
Block.find(params, Block.find(
params,
defaultOptions, defaultOptions,
(err, block) => { cb)
if (err) { .sort({ height: 1 })
res.status(501).send(); .limit(MAX_BLOCKS);
logger.log('err', err);
}
res.json(block[0]);
});
} }
module.exports = function BlockAPI(app) {
app.get('/block/:blockHash', (req, res) => {
getBlock(
{ hash: req.params.blockHash },
{ rawBlock: 0 },
(err, block) => {
if (err) {
res.status(501).send();
logger.log('err', err);
}
res.json(block[0]);
});
});
app.get('/blocks', (req, res) => {
getBlock(
{},
{ height: 1,
size: 1,
hash: 1,
time: 1,
transactionCount: 1,
poolInfo: 1 },
(err, blocks) => {
if (err) {
res.status(501).send();
logger.log('err', err);
}
res.json({
blocks: blocks.map((block) => {
return {
hash: block.hash,
height: block.height,
time: block.time,
txlength: block.transactionCount
};
}),
lenght: blocks.length,
pagination: {},
});
});
});
app.get('/rawblock/:blockHash', (req, res) => {
getBlock(
{ hash: req.params.blockHash },
{ rawBlock: 1 },
(err, block) => {
if (err) {
res.status(501).send();
logger.log('err', err);
}
res.json(block);
});
});
app.get('/block-index/:height', (req, res) => {
getBlock(
{ height: req.params.height },
{},
(err, block) => {
if (err) {
res.status(501).send();
logger.log('err', err);
}
res.json(block);
});
});
};

View File

@ -26,7 +26,7 @@ function parse(entry, block) {
reward: 0, reward: 0,
timeNormalized: block.ts, timeNormalized: block.ts,
isMainChain: true, isMainChain: true,
poolInfo: Object, poolInfo: {},
transactionCount: block.txs.length, transactionCount: block.txs.length,
rawBlock: rawBlock, rawBlock: rawBlock,
}); });