Block API made more dry, rawblocks added to model

This commit is contained in:
tenthirtyone 2017-08-04 14:29:10 -04:00
parent e5ab809026
commit 7afbdb41b1
9 changed files with 39 additions and 1885 deletions

View File

@ -5,8 +5,7 @@
"es6": true
},
"rules": {
"no-multi-spaces": {
"exceptions": { "ImportDeclaration": true }
}
"no-multi-spaces": 0,
"no-use-before-define": 1
}
}

View File

@ -55,6 +55,9 @@ The API is configured to run on port 3000 by default. Use the standard Nginx rev
* sockets
# ToDo
Mongo Models : Bcoin primitives. A Bcoin block does not present all of bitcore's data.
Reorg testing - Bcoin will handle this but we need to account for this in our mongo indexes.
JSDoc & Unit tests
* Mongo Models : Bcoin primitives. A Bcoin block does not present all of bitcore's data.
* Reorg testing - Bcoin will handle this but we need to account for this in our mongo indexes.
* JSDoc & Unit tests
* Rate Limiting
* Helmet
* Rate Limiting

View File

@ -9,11 +9,11 @@ logger.log('debug',
db.connect(config.mongodb.uri, config.mongodb.options);
db.connection.once('open', () => {
Bcoin.start();
});
Api.listen(config.api.port, () => {
logger.log('debug',
'listening on port 3000');
});
db.connection.once('open', function() {
Bcoin.start();
});

View File

@ -3,15 +3,9 @@ const logger = require('../logger');
module.exports = function BlockAPI(app) {
app.get('/block/:blockHash', (req, res) => {
Block.find({ hash: req.params.blockHash },
{ _id: 0 },
(err, block) => {
if (err) {
res.status(501).send();
logger.log('err', err);
}
res.json(block[0]);
});
getBlock(res,
{ hash: req.params.blockHash },
{ rawBlock: 0 });
});
app.get('/blocks', (req, res) => {
@ -28,6 +22,23 @@ module.exports = function BlockAPI(app) {
});
app.get('/block-index/:height', (req, res) => {
res.send(req.params.height);
getBlock(res, { height: req.params.height });
});
};
function getBlock(res, params, options) {
const defaultOptions = { _id: 0 };
Object.assign(defaultOptions, options);
Block.find(params,
defaultOptions,
(err, block) => {
if (err) {
res.status(501).send();
logger.log('err', err);
}
res.json(block[0]);
});
}

View File

@ -3,7 +3,7 @@ const config = require('../../config');
const app = express();
app.set('json spaces', config.json_spaces);
app.set('json spaces', config.api.json_spaces);
// Pass express to register the routes
const AddressAPI = require('./address')(app);

View File

@ -7,6 +7,7 @@ module.exports = function transactionAPI(app) {
app.get('/txs', (req, res) => {
res.send('list of txs');
});
app.get('/rawtx/:txid', (req, res) => {

View File

@ -6,11 +6,12 @@ const logger = require('../logger');
function parse(entry, block) {
const blockHash = util.revHex(block.hash().toString('hex'));
const merkle = util.revHex(block.merkleRoot);
const rawBlock = block.toRaw().toString('hex');
const newBlock = new BlockModel({
hash: blockHash,
size: block.size,
height: block.height,
height: entry.height,
version: block.version,
merkleRoot: merkle,
tx: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))),
@ -27,6 +28,7 @@ function parse(entry, block) {
isMainChain: true,
poolInfo: Object,
transactionCount: block.txs.length,
rawBlock: rawBlock,
});
newBlock.save((err) => {

View File

@ -21,6 +21,7 @@ const BlockSchema = new Schema({
isMainChain: Boolean,
poolInfo: Object,
transactionCount: Number,
rawBlock: String,
});
BlockSchema.index({ hash: 1 }, { unique: true });

1863
package-lock.json generated

File diff suppressed because it is too large Load Diff