From fe8bb95e28e1542e5bfdb35adaa45f49aa17090d Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Tue, 4 Feb 2014 17:22:51 -0300 Subject: [PATCH 1/4] block test: list of hashes by date --- README.md | 11 ++++++----- lib/BlockDb.js | 22 ++++++++++++++++++++++ test/integration/blocklist.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 test/integration/blocklist.js diff --git a/README.md b/README.md index 4ee92d8..9b8d691 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/lib/BlockDb.js b/lib/BlockDb.js index 13dd2e8..35f3adb 100644 --- a/lib/BlockDb.js +++ b/lib/BlockDb.js @@ -129,6 +129,28 @@ function spec() { }); }; + BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, cb) { + var self = this; + var list = []; + this.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){ diff --git a/test/integration/blocklist.js b/test/integration/blocklist.js new file mode 100644 index 0000000..6b50952 --- /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 = '00000000b7cc12abe8a9a604813aab1f2c4f3a242a021065be52393a147a1a86'; +var START_TS = '1391538611'; +var END_TS = '1391538638'; + +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(); + }); + }); +}); + From df3336627706ab48ca7da5eeec32e0c3f0ad7847 Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Wed, 5 Feb 2014 10:16:40 -0300 Subject: [PATCH 2/4] removed mongoose. method: block list by date --- app/controllers/blocks.js | 16 +++++++++------- app/controllers/transactions.js | 7 +++++-- app/models/Transaction.js | 2 +- config/config.js | 1 - insight.js | 20 +------------------- lib/BlockDb.js | 4 +--- lib/Sync.js | 30 +----------------------------- 7 files changed, 18 insertions(+), 62 deletions(-) diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js index 726a086..4acc120 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 f7dd671..831025c 100644 --- a/app/controllers/transactions.js +++ b/app/controllers/transactions.js @@ -4,11 +4,14 @@ * Module dependencies. */ var Transaction = require('../models/Transaction').class(); -var Block = require('../models/Block'); 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/app/models/Transaction.js b/app/models/Transaction.js index f3b20dc..b752cfd 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -7,7 +7,7 @@ function spec() { var util = require('bitcore/util/util'), TransactionRpc = require('../../lib/TransactionRpc').class(), - TransactionOut = require('./TransactionOut'), + TransactionOut = require('../../lib/TransactionDb'), async = require('async'); var CONCURRENCY = 20; diff --git a/config/config.js b/config/config.js index 7ab059b..92fbeb6 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 9698509..29b0c4a 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 35f3adb..ca37c70 100644 --- a/lib/BlockDb.js +++ b/lib/BlockDb.js @@ -160,9 +160,7 @@ function spec() { }); }; - - - return BlockDb; + return BlockDb; } module.defineClass(spec); diff --git a/lib/Sync.js b/lib/Sync.js index 17c524e..f2aa966 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() { From 8b1092e3ffec43cf3c5aa2800489891fda3004c3 Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Wed, 5 Feb 2014 10:30:41 -0300 Subject: [PATCH 3/4] fix controller transaction --- app/controllers/transactions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/transactions.js b/app/controllers/transactions.js index 831025c..9eeb5f1 100644 --- a/app/controllers/transactions.js +++ b/app/controllers/transactions.js @@ -3,7 +3,7 @@ /** * Module dependencies. */ -var Transaction = require('../models/Transaction').class(); +var Transaction = require('../../lib/TransactionDb').class(); var Address = require('../models/Address'); var async = require('async'); var common = require('./common'); From aebfdebfde21eb7a7440d1412d62312c5aaaf022 Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Wed, 5 Feb 2014 10:59:01 -0300 Subject: [PATCH 4/4] fix test --- lib/BlockDb.js | 2 +- test/integration/blocklist.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/BlockDb.js b/lib/BlockDb.js index 81c57eb..8b9539d 100644 --- a/lib/BlockDb.js +++ b/lib/BlockDb.js @@ -133,7 +133,7 @@ function spec() { BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, cb) { var self = this; var list = []; - this.db.createReadStream({ + self.db.createReadStream({ start: TIMESTAMP_ROOT + start_ts, end: TIMESTAMP_ROOT + end_ts }) diff --git a/test/integration/blocklist.js b/test/integration/blocklist.js index 6b50952..8225a17 100644 --- a/test/integration/blocklist.js +++ b/test/integration/blocklist.js @@ -3,9 +3,9 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development'; -var TESTING_BLOCK = '00000000b7cc12abe8a9a604813aab1f2c4f3a242a021065be52393a147a1a86'; -var START_TS = '1391538611'; -var END_TS = '1391538638'; +var TESTING_BLOCK = '000000001f56660def9b5898ea8411d7b028854e78502e521f9ebd53e673751c'; +var START_TS = '1391607675'; +var END_TS = '1391607709'; var assert = require('assert'),