diff --git a/README.md b/README.md index 35ff00c2..eb30b464 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,126 @@ -mystery -======= +# Mystery + +Project description. + +## Prerequisites +* Node.js - Download and Install [Node.js](http://www.nodejs.org/download/). You can also follow [this gist](https://gist.github.com/isaacs/579814) for a quick and easy way to install Node.js and npm +* MongoDB - Download and Install [MongoDB](http://www.mongodb.org/downloads) - Make sure it's running on the default port (27017). + +### Tools Prerequisites +* NPM - Node.js package manager, should be installed when you install node.js. +* Grunt - Download and Install [Grunt](http://gruntjs.com). +* Bower - Web package manager, installing [Bower](http://bower.io/) is simple when you have npm: + +``` +$ npm install -g bower +``` + +## Additional Packages +* Express - Defined as npm module in the [package.json](package.json) file. +* Mongoose - Defined as npm module in the [package.json](package.json) file. +* AngularJS - Defined as bower module in the [bower.json](bower.json) file. +* Twitter Bootstrap - Defined as bower module in the [bower.json](bower.json) file. +* UI Bootstrap - Defined as bower module in the [bower.json](bower.json) file. + +## Quick Install + The quickest way to get started with MEAN is to clone the project and utilize it like this: + + Grunt Command Line Interface: + + $ sudo npm -g install grunt-cli + + Install dependencies: + + $ npm install + + We use [Grunt](https://github.com/gruntjs/grunt-cli) to start the server: + + $ grunt + + When not using grunt you can use (for example in production): + + $ node server + + Then open a browser and go to: + + http://localhost:3000 -## Install - -- sudo npm -g i grunt-cli -- npm i -- grunt -## bitcoind configuration +## API - There is a bitcoind configuration sample at: +## Prerequisites + Get bitcore from github repository: + $ git clone https://github.com/bitpay/bitcore.git + $ cd bitcore + $ npm install + + Run sync from mystery repository: + $ utils/sync.js + +### Blocks +``` + /block/[:hash] + /block/00000000a967199a2fad0877433c93df785a8d8ce062e5f9b451cd1397bdbf62 +``` + + + + +## Troubleshooting +If you did not get all library during grunt command, please use the follow command: + + $ bower install + +## Configuration +All configuration is specified in the [config](config/) folder, particularly the [config.js](config/config.js) file and the [env](config/env/) files. Here you will need to specify your application name and database name. + +### bitcoind + +There is a bitcoind configuration sample at: ``` etc/mystery/bitcoin.conf ``` + +If you want to use a external bitcoind server set BITCOIND_HOST / BITCOIND_PORT enviroment variables. Make sure that bitcoind is configured to accept incomming connections using 'rpcallowip' decribed in https://en.bitcoin.it/wiki/Running_Bitcoin. + + +### Environmental Settings + +There are three environments provided by default, __development__, __test__, and __production__. Each of these environments has the following configuration options: +* __db__ - This is the name of the MongoDB database to use, and is set by default to __mystery-dev__ for the development environment. +* __app.name__ - This is the name of your app or website, and can be different for each environment. You can tell which environment you are running by looking at the TITLE attribute that your app generates. + +To run with a different environment, just specify NODE_ENV as you call grunt: + + $ NODE_ENV=test grunt + +If you are using node instead of grunt, it is very similar: + + $ NODE_ENV=test node server + +## Github +[Mystery](https://github.com/bitpay/mystery) + +## License +(The MIT License) + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js new file mode 100644 index 00000000..fdef6655 --- /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 new file mode 100644 index 00000000..53272b52 --- /dev/null +++ b/app/models/Block.js @@ -0,0 +1,68 @@ +'use strict'; + +/** + * Module dependencies. + */ +var mongoose = require('mongoose'), + Schema = mongoose.Schema; + + +/** + * Block Schema + */ +var BlockSchema = new Schema({ + hash: { + type: String, + index: true, + unique: true, + }, + size: Number, + confirmations: Number, + version: Number, + merkleroot: String, + tx: [ String ], + time: Date, + nonce: Number, + bits: String, + difficulty: Number, + chainwork: String, + previousblockhash: { + type: String, + index: true, + unique: true, + }, + nextblockhash: { + type: String, + index: true, + unique: true, + }, +}); + +/** + * Validations + */ + +/* +BlockSchema.path('title').validate(function(title) { + return title.length; +},'Title cannot be blank'); +*/ + +/** + * Statics + */ + +BlockSchema.statics.load = function(id, cb) { + this.findOne({ + _id: id + }).exec(cb); +}; + + +BlockSchema.statics.fromHash = function(hash, cb) { + this.findOne({ + hash: hash, + }).exec(cb); +}; + +module.exports = mongoose.model('Block', BlockSchema); diff --git a/config/env/development.js b/config/env/development.js index 0ef298cc..6836a43c 100755 --- a/config/env/development.js +++ b/config/env/development.js @@ -4,5 +4,12 @@ module.exports = { db: "mongodb://localhost/mystery-dev", app: { name: "Mystery - Development" + }, + bitcoind: { + user: 'mystery', + pass: 'real_mystery', + protocol: 'http', + host: process.env.BITCOIND_HOST || '127.0.0.1', + port: process.env.BITCOIND_PORT || '8332', } -} \ No newline at end of file +} diff --git a/config/routes.js b/config/routes.js index 182a2162..7150af08 100644 --- a/config/routes.js +++ b/config/routes.js @@ -6,7 +6,12 @@ module.exports = function(app) { var index = require('../app/controllers/index'); app.get('/', index.render); - //TX routes - // + //Block routes + var blocks = require('../app/controllers/blocks'); + app.get('/block/:blockHash', blocks.show); + + + app.param('blockHash', blocks.block); + }; diff --git a/package.json b/package.json index 110c3e0f..0fc2f268 100644 --- a/package.json +++ b/package.json @@ -35,12 +35,14 @@ "postinstall": "node node_modules/bower/bin/bower install" }, "dependencies": { + "classtool": "*", "express": "~3.4.7", "jade": "~1.0.2", "mongoose": "~3.8.3", "lodash": "~2.4.1", "bower": "~1.2.8", "bitcore": "*", + "buffertools": "*", "grunt": "~0.4.2", "grunt-cli": "~0.1.11", "grunt-env": "~0.4.1", diff --git a/util/get_block.js b/util/get_block.js index 27880d8f..783d7cf7 100755 --- a/util/get_block.js +++ b/util/get_block.js @@ -1,17 +1,15 @@ #!/usr/bin/env node - - +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; var RpcClient = require('../node_modules/bitcore/RpcClient').class(); +var config = require('../config/config'); + var block_hash = process.argv[2] || '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; -var rpc = new RpcClient({ - user: 'mystery', - pass: 'real_mystery', - protocol: 'http', -}); + +var rpc = new RpcClient(config.bitcoind); var block = rpc.getBlock(block_hash, function(err, block) { diff --git a/util/sync.js b/util/sync.js new file mode 100755 index 00000000..9aff4453 --- /dev/null +++ b/util/sync.js @@ -0,0 +1,89 @@ +#!/usr/bin/env node + +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; +require('buffertools').extend(); + +var util = require('util'); +var RpcClient = require('../node_modules/bitcore/RpcClient').class(); +var networks = require('../node_modules/bitcore/networks'); + +var Block = require('../app/models/Block'); +var config = require('../config/config'); +var mongoose = require('mongoose'); + +var networkName = process.argv[2] || 'testnet'; +var network = networkName == 'testnet' ? networks.testnet : networks.livenet; + + +function getNextBlock(blockHash,cb) { + + if ( !blockHash ) { + console.log("done"); + return cb(); + } + + rpc.getBlock(blockHash, function(err, blockInfo) { + if (err) { + return cb(err); + } + + if ( ! ( blockInfo.result.height % 1000) ) { + var h = blockInfo.result.height, + d = blockInfo.result.confirmations; + console.log( util.format("Height: %d/%d [%d%%]", h, d, 100*h/(h+d))); + } + + Block.create( blockInfo.result, function(err, inBlock) { + + // E11000 => already exists + if (err && ! err.toString().match(/E11000/)) { + return cb(err); + } + + return getNextBlock(blockInfo.result.nextblockhash, cb); + + }); + }); + +} + +function syncBlocks(network, cb) { + + Block.findOne({}, {}, { sort: { 'confirmations' : 1 } }, function(err, block) { + if (err) { + return cb(err); + } + + + + var nextHash = + block && block.hash + ? block.hash + : network.genesisBlock.hash.reverse().toString('hex') + ; + + + console.log('Starting at hash: ' + nextHash); + getNextBlock(nextHash, cb); + }); +} + + +mongoose.connect(config.db); + +var db = mongoose.connection; +var rpc = new RpcClient(config.bitcoind); + + +db.on('error', console.error.bind(console, 'connection error:')); +db.once('open', function callback () { + syncBlocks(network, function(err) { + if (err) { + console.log(err); + } + mongoose.connection.close(); + }); +}); + + +