diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js new file mode 100644 index 0000000..fdef665 --- /dev/null +++ b/app/controllers/blocks.js @@ -0,0 +1,33 @@ +'use strict'; + + +var Block = require('../models/Block'); +//, _ = require('lodash'); + + + +/** + * Module dependencies. + */ + + +/** + * Find block by hash ... + */ +exports.block = function(req, res, next, hash) { + Block.fromHash(hash, function(err, block) { + if (err) return next(err); + if (!block) return next(new Error('Failed to load block ' + hash)); + req.block = block; + next(); + }); +}; + + +/** + * Show block + */ +exports.show = function(req, res) { + res.jsonp(req.block); +}; + diff --git a/app/models/Block.js b/app/models/Block.js index 9440426..53272b5 100644 --- a/app/models/Block.js +++ b/app/models/Block.js @@ -11,8 +11,8 @@ var mongoose = require('mongoose'), * Block Schema */ var BlockSchema = new Schema({ - hash: { - type: String, + hash: { + type: String, index: true, unique: true, }, @@ -27,12 +27,12 @@ var BlockSchema = new Schema({ difficulty: Number, chainwork: String, previousblockhash: { - type: String, + type: String, index: true, unique: true, }, nextblockhash: { - type: String, + type: String, index: true, unique: true, }, @@ -53,16 +53,16 @@ BlockSchema.path('title').validate(function(title) { */ BlockSchema.statics.load = function(id, cb) { - this.findOne({ - _id: id - }).exec(cb); + this.findOne({ + _id: id + }).exec(cb); }; BlockSchema.statics.fromHash = function(hash, cb) { - this.findOne({ - hash: hash, - }).exec(cb); + this.findOne({ + hash: hash, + }).exec(cb); }; module.exports = mongoose.model('Block', BlockSchema); diff --git a/config/routes.js b/config/routes.js index 1f75a0e..7150af0 100644 --- a/config/routes.js +++ b/config/routes.js @@ -8,7 +8,10 @@ module.exports = function(app) { //Block routes - var blocks = require('model/app/controllers/blocks'); - app.get('/block/:block_hash', blocks.show); + var blocks = require('../app/controllers/blocks'); + app.get('/block/:blockHash', blocks.show); + + + app.param('blockHash', blocks.block); }; diff --git a/util/sync.js b/util/sync.js index 78991de..9aff445 100755 --- a/util/sync.js +++ b/util/sync.js @@ -40,7 +40,7 @@ function getNextBlock(blockHash,cb) { return cb(err); } - return getNextBlock(blockInfo.result.nextblockhash); + return getNextBlock(blockInfo.result.nextblockhash, cb); }); }); @@ -49,7 +49,7 @@ function getNextBlock(blockHash,cb) { function syncBlocks(network, cb) { - Block.findOne({}, {}, { sort: { 'height' : -1 } }, function(err, block) { + Block.findOne({}, {}, { sort: { 'confirmations' : 1 } }, function(err, block) { if (err) { return cb(err); } @@ -63,7 +63,7 @@ function syncBlocks(network, cb) { ; - console.log('Starting at hash' + nextHash); + console.log('Starting at hash: ' + nextHash); getNextBlock(nextHash, cb); }); }