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 "es6": true
}, },
"rules": { "rules": {
"no-multi-spaces": { "no-multi-spaces": 0,
"exceptions": { "ImportDeclaration": true } "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 * sockets
# ToDo # ToDo
Mongo Models : Bcoin primitives. A Bcoin block does not present all of bitcore's data. * 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. * Reorg testing - Bcoin will handle this but we need to account for this in our mongo indexes.
JSDoc & Unit tests * 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.connect(config.mongodb.uri, config.mongodb.options);
db.connection.once('open', () => {
Bcoin.start();
});
Api.listen(config.api.port, () => { Api.listen(config.api.port, () => {
logger.log('debug', logger.log('debug',
'listening on port 3000'); '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) { module.exports = function BlockAPI(app) {
app.get('/block/:blockHash', (req, res) => { app.get('/block/:blockHash', (req, res) => {
Block.find({ hash: req.params.blockHash }, getBlock(res,
{ _id: 0 }, { hash: req.params.blockHash },
(err, block) => { { rawBlock: 0 });
if (err) {
res.status(501).send();
logger.log('err', err);
}
res.json(block[0]);
});
}); });
app.get('/blocks', (req, res) => { app.get('/blocks', (req, res) => {
@ -28,6 +22,23 @@ module.exports = function BlockAPI(app) {
}); });
app.get('/block-index/:height', (req, res) => { 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(); const app = express();
app.set('json spaces', config.json_spaces); app.set('json spaces', config.api.json_spaces);
// Pass express to register the routes // Pass express to register the routes
const AddressAPI = require('./address')(app); const AddressAPI = require('./address')(app);

View File

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

View File

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

View File

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

1863
package-lock.json generated

File diff suppressed because it is too large Load Diff