Merge branch 'feature/leveldb' of github.com:matiu/insight into feature/leveldb

This commit is contained in:
Matias Alejo Garcia 2014-02-05 11:24:20 -03:00
commit 58104707a2
8 changed files with 74 additions and 67 deletions

View File

@ -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.

View File

@ -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) {
});
}
});
*/
};

View File

@ -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');

View File

@ -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',

View File

@ -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
*/

View File

@ -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);

View File

@ -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) {

View File

@ -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();
});
});
});