diff --git a/README.md b/README.md index 4ee92d8f..9b8d6916 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,6 @@ $ 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. @@ -55,13 +54,15 @@ $ npm install -g bower http://localhost:3000 - If you get an error, please check the next section "Post-install" - ## Syncing old blockchain data - Run sync from insight repository (to save old blocks and transactions in MongoDB): + Run sync from insight repository (to save old blocks and transactions in + LevelDB): + + Create folders: - $ utils/sync.js + $ mkdir -p db/blocks + $ utils/sync.js -S Check utils/sync.js --help for options. diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js index 726a0864..4acc1207 100644 --- a/app/controllers/blocks.js +++ b/app/controllers/blocks.js @@ -3,17 +3,17 @@ /** * Module dependencies. */ -var mongoose = require('mongoose'), - Block = mongoose.model('Block'), - common = require('./common'), - async = require('async'); +var common = require('./common'), + async = require('async'), + BlockDb = require('../../lib/BlockDb').class(); +var bdb = new BlockDb(); /** * Find block by hash ... */ exports.block = function(req, res, next, hash) { - Block.fromHashWithInfo(hash, function(err, block) { + bdb.fromHashWithInfo(hash, function(err, block) { if (err || ! block) return common.handleErrors(err, res, next); else { @@ -37,7 +37,7 @@ exports.show = function(req, res) { * Show block by Height */ exports.blockindex = function(req, res, next, height) { - Block.blockIndex(height, function(err, hashStr) { + bdb.blockIndex(height, function(err, hashStr) { if (err) { console.log(err); res.status(400).send('Bad Request'); // TODO @@ -49,7 +49,7 @@ exports.blockindex = function(req, res, next, height) { }; var getBlock = function(blockhash, cb) { - Block.fromHashWithInfo(blockhash, function(err, block) { + bdb.fromHashWithInfo(blockhash, function(err, block) { if (err) { console.log(err); return cb(err); @@ -103,6 +103,7 @@ exports.list = function(req, res) { var prev = formatTimestamp(new Date((gte - 86400) * 1000)); var next = formatTimestamp(new Date(lte * 1000)); + /* Block .find({ time: { @@ -134,4 +135,5 @@ exports.list = function(req, res) { }); } }); + */ }; diff --git a/app/controllers/transactions.js b/app/controllers/transactions.js index f7dd671d..9eeb5f14 100644 --- a/app/controllers/transactions.js +++ b/app/controllers/transactions.js @@ -3,12 +3,15 @@ /** * Module dependencies. */ -var Transaction = require('../models/Transaction').class(); -var Block = require('../models/Block'); +var Transaction = require('../../lib/TransactionDb').class(); var Address = require('../models/Address'); var async = require('async'); var common = require('./common'); +var BlockDb = require('../../lib/BlockDb').class(); + +var bdb = new BlockDb(); + /** * Find transaction by hash ... @@ -68,7 +71,7 @@ exports.list = function(req, res, next) { var txs; if (bId) { - Block.fromHashWithInfo(bId, function(err, block) { + bdb.fromHashWithInfo(bId, function(err, block) { if (err) { console.log(err); return res.status(500).send('Internal Server Error'); diff --git a/config/config.js b/config/config.js index 7ab059b1..92fbeb66 100644 --- a/config/config.js +++ b/config/config.js @@ -23,7 +23,6 @@ module.exports = { root: rootPath, appName: 'Insight ' + env, port: process.env.PORT || 3000, - db: 'mongodb://localhost/insight-' + env, leveldb: './db', bitcoind: { protocol: process.env.BITCOIND_PROTO || 'http', diff --git a/insight.js b/insight.js index 96985095..29b0c4a5 100644 --- a/insight.js +++ b/insight.js @@ -9,9 +9,7 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development'; var express = require('express'), fs = require('fs'), PeerSync = require('./lib/PeerSync').class(), - HistoricSync = require('./lib/HistoricSync').class(), - mongoose = require('mongoose'); - + HistoricSync = require('./lib/HistoricSync').class(); //Initializing system variables var config = require('./config/config'); @@ -21,22 +19,6 @@ var config = require('./config/config'); */ var expressApp = express(); -/** - * Bootstrap db connection - */ -// If mongod is running -mongoose.connection.on('open', function() { - console.log('Connected to mongo server.'); -}); - -// If mongod is not running -mongoose.connection.on('error', function(err) { - console.log('Could not connect to mongo server!'); - console.log(err); -}); - -mongoose.connect(config.db); - /** * Bootstrap models */ diff --git a/lib/BlockDb.js b/lib/BlockDb.js index 7d5491aa..67eada86 100644 --- a/lib/BlockDb.js +++ b/lib/BlockDb.js @@ -134,6 +134,28 @@ function spec() { }); }; + BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, cb) { + var self = this; + var list = []; + self.db.createReadStream({ + start: TIMESTAMP_ROOT + start_ts, + end: TIMESTAMP_ROOT + end_ts + }) + .on('data', function (data) { + list.push({ + ts: data.key.replace(TIMESTAMP_ROOT, ''), + hash: data.value, + info: {} + }); + }) + .on('error', function (err) { + return cb(err); + }) + .on('end', function () { + return cb(null, list); + }); + }; + BlockDb.blockIndex = function(height, cb) { var rpc = new RpcClient(config.bitcoind); rpc.getBlockHash(height, function(err, bh){ @@ -143,9 +165,7 @@ function spec() { }); }; - - - return BlockDb; + return BlockDb; } module.defineClass(spec); diff --git a/lib/Sync.js b/lib/Sync.js index a4f4c1c7..b68bd437 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -4,7 +4,6 @@ require('classtool'); function spec() { - var mongoose = require('mongoose'); var config = require('../config/config'); var sockets = require('../app/controllers/socket.js'); var BlockDb = require('./BlockDb').class(); @@ -22,34 +21,7 @@ function spec() { self.opts = opts; - if (!(opts && opts.skipDbConnection)) { - - if (mongoose.connection.readyState !== 1) { - mongoose.connect(config.db, function(err) { - if (err) { - console.log('CRITICAL ERROR: connecting to mongoDB:',err); - return (err); - } - }); - } - - self.db = mongoose.connection; - - self.db.on('error', function(err) { - console.log('MongoDB ERROR:' + err); - return cb(err); - }); - - self.db.on('disconnect', function(err) { - console.log('MongoDB disconnect:' + err); - return cb(err); - }); - - return self.db.once('open', function(err) { - return cb(err); - }); - } - else return cb(); + return cb(); }; Sync.prototype.close = function(cb) { diff --git a/test/integration/blocklist.js b/test/integration/blocklist.js new file mode 100644 index 00000000..8225a173 --- /dev/null +++ b/test/integration/blocklist.js @@ -0,0 +1,28 @@ +#!/usr/bin/env node +'use strict'; + +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; + +var TESTING_BLOCK = '000000001f56660def9b5898ea8411d7b028854e78502e521f9ebd53e673751c'; +var START_TS = '1391607675'; +var END_TS = '1391607709'; + +var + assert = require('assert'), + config = require('../../config/config'), + BlockDb = require('../../lib/BlockDb').class(); + +describe('BlockDb getHashes', function(){ + + var bdb = new BlockDb(); + it('Get Hash by Date', function(done) { + + bdb.getBlocksByDate(START_TS, END_TS, function(err, list) { + if (err) done(err); + assert.equal(list[0].ts, START_TS); + assert.equal(list[0].hash, TESTING_BLOCK); + done(); + }); + }); +}); +