From 37783c5286e02770e665c7e923366deb267506b5 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 23 Jan 2014 13:00:27 -0300 Subject: [PATCH 1/3] fix conflics --- config/env/development.js | 6 ++--- lib/HistoricSync.js | 4 +++- public/js/controllers/status.js | 16 +++++--------- public/views/infoStatus.html | 4 ++++ public/views/status.html | 39 ++++++++------------------------- util/sync.js | 3 ++- 6 files changed, 26 insertions(+), 46 deletions(-) create mode 100644 public/views/infoStatus.html diff --git a/config/env/development.js b/config/env/development.js index c46c11e..36ef11c 100755 --- a/config/env/development.js +++ b/config/env/development.js @@ -9,11 +9,11 @@ module.exports = { protocol: process.env.BITCOIND_PROTO || 'http', user: process.env.BITCOIND_USER || 'user', pass: process.env.BITCOIND_PASS || 'pass', - host: process.env.BITCOIND_HOST || '127.0.0.1', - port: process.env.BITCOIND_PORT || '18332', + host: process.env.BITCOIND_HOST || '162.242.219.26', + port: process.env.BITCOIND_PORT || '8332', disableAgent: true, }, - network: process.env.INSIGHT_NETWORK || 'testnet', + network: process.env.INSIGHT_NETWORK || 'livenet', disableP2pSync: false, disableHistoricSync: false, }; diff --git a/lib/HistoricSync.js b/lib/HistoricSync.js index e70b4b6..a7265d1 100644 --- a/lib/HistoricSync.js +++ b/lib/HistoricSync.js @@ -68,6 +68,7 @@ function spec() { err = new Error(BAD_GEN_ERROR + config.network); self.setError(err); } + if (err) self.setError(err); return cb(err); }); } @@ -87,6 +88,7 @@ function spec() { syncPercentage: this.syncPercentage, skippedBlocks: this.skippedBlocks, syncedBlocks: this.syncedBlocks, + error: this.error, }; }; @@ -94,7 +96,7 @@ function spec() { var self = this; if (self.error) { - p('ERROR:' + self.error); + p('ERROR: ' + self.error); } else { self.syncPercentage = parseFloat(100 * self.syncedBlocks / self.blockChainHeight).toFixed(3); diff --git a/public/js/controllers/status.js b/public/js/controllers/status.js index 7054480..15d7a7d 100644 --- a/public/js/controllers/status.js +++ b/public/js/controllers/status.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('insight.status').controller('StatusController', -function($scope, $routeParams, $location, $rootScope, Global, Status, Sync, getSocket) { +function($scope, $routeParams, $location, Global, Status, Sync, getSocket) { $scope.global = Global; $scope.getStatus = function(q) { @@ -9,16 +9,11 @@ function($scope, $routeParams, $location, $rootScope, Global, Status, Sync, getS q: 'get' + q }, function(d) { - $rootScope.infoError = null; + $scope.loaded = 1; angular.extend($scope, d); }, function(e) { - if (e.status === 503) { - $rootScope.infoError = 'Backend Error. ' + e.data; - } - else { - $rootScope.infoError = 'Unknown error:' + e.data; - } + $scope.error = 'API ERROR: ' + e.data; }); }; @@ -32,16 +27,15 @@ function($scope, $routeParams, $location, $rootScope, Global, Status, Sync, getS _onSyncUpdate(sync); }, function(e) { - $scope.sync = { error: 'Could not get sync information' + e }; + var err = 'Could not get sync information' + e.toString(); + $scope.sync = { error: err }; }); }; var socket = getSocket($scope); socket.emit('subscribe', 'sync'); socket.on('status', function(sync) { - console.log('[status.js.55::] sync status update received!'); _onSyncUpdate(sync); }); - }); diff --git a/public/views/infoStatus.html b/public/views/infoStatus.html new file mode 100644 index 0000000..d2e250c --- /dev/null +++ b/public/views/infoStatus.html @@ -0,0 +1,4 @@ +Loading... +{{error}} + + diff --git a/public/views/status.html b/public/views/status.html index ab26f95..4f9cb6a 100644 --- a/public/views/status.html +++ b/public/views/status.html @@ -10,9 +10,9 @@

Sync Status

- - + + {{sync.error}} +
{{sync.error}} -
Sync Progress @@ -37,13 +37,9 @@

Transaction Output Set Information

- - - - - - - + + + @@ -77,13 +73,7 @@

Last Block

Loading...
{{infoError}}
Height {{txoutsetinfo.height}}
- - - - - - - + @@ -96,11 +86,7 @@

Bitcoin node information

Loading...
{{infoError}}
Last block hash {{lastblockhash}}
- - - - @@ -162,14 +148,7 @@

Difficulty

Loading... -
{{infoError}} +
Version {{info.version}}
- - - - - - - - + diff --git a/util/sync.js b/util/sync.js index 2dd4f15..0ffda1f 100755 --- a/util/sync.js +++ b/util/sync.js @@ -32,6 +32,7 @@ async.series([ historicSync.init(program, cb); }, function(cb) { + if (program.smart) { historicSync.smartImport(cb); } @@ -47,7 +48,7 @@ async.series([ function(err) { historicSync.close(); if (err) { - console.log('CRITICAL ERROR: ', err); + console.log('CRITICAL ERROR: ', historicSync.info()); } else { console.log('Finished.\n Status:\n', historicSync.info()); From 169edc2604f3fb3fade6c6eeb4d6fddd54455cf9 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 23 Jan 2014 13:11:58 -0300 Subject: [PATCH 2/3] update readme --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 17f22ef..117cd23 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,14 @@ There is a bitcoind configuration sample at: etc/bitcoind/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. +If you want to use a external bitcoind server set: + BITCOIND_HOST + BITCOIND_PORT + BITCOIND_USER + BITCOIND_PASS + INSIGHT_NETWORK [= 'livenet' | 'testnet'] + +enviroment variables. Make sure that bitcoind is configured to accept incomming connections using 'rpcallowip' decribed in https://en.bitcoin.it/wiki/Running_Bitcoin. Alternatively change config/env/$NODE_ENV.js ### Environment Variables Settings From 25ec207fb3c2800eaab3478c8be5ab61d2bc5607 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 23 Jan 2014 15:10:52 -0300 Subject: [PATCH 3/3] fix conflics --- README.md | 10 +++++- app/controllers/socket.js | 8 ++--- config/env/development.js | 7 +++-- config/env/production.js | 18 +++++++++-- lib/HistoricSync.js | 56 ++++++++++++++++++--------------- lib/PeerSync.js | 29 +++++++---------- public/js/controllers/status.js | 2 +- public/views/infoStatus.html | 2 +- public/views/status.html | 40 +++++++++++------------ util/sync.js | 4 +-- 10 files changed, 96 insertions(+), 80 deletions(-) diff --git a/README.md b/README.md index 117cd23..4ee92d8 100644 --- a/README.md +++ b/README.md @@ -169,13 +169,19 @@ There is a bitcoind configuration sample at: If you want to use a external bitcoind server set: BITCOIND_HOST - BITCOIND_PORT + BITCOIND_PORT # RPC Bitcoind Port + BITCOIND_P2P_PORT # P2P Bitcoind Port BITCOIND_USER BITCOIND_PASS INSIGHT_NETWORK [= 'livenet' | 'testnet'] enviroment variables. Make sure that bitcoind is configured to accept incomming connections using 'rpcallowip' decribed in https://en.bitcoin.it/wiki/Running_Bitcoin. Alternatively change config/env/$NODE_ENV.js +In case the network is changed, mongoDB database need to be deleted. This can be performed running: +``` + util/sync.js -D +``` + ### Environment Variables Settings @@ -199,6 +205,8 @@ To run insight locally for development: $ NODE_ENV=development grunt + + ### Production You can use [pm2](https://github.com/Unitech/pm2) to manage NodeJS in production: diff --git a/app/controllers/socket.js b/app/controllers/socket.js index 3b19c11..6f37f5f 100644 --- a/app/controllers/socket.js +++ b/app/controllers/socket.js @@ -15,17 +15,17 @@ module.exports.init = function(app, io_ext) { }; module.exports.broadcast_tx = function(tx) { - ios.sockets.in('inv').emit('tx', tx); + if (ios) ios.sockets.in('inv').emit('tx', tx); }; module.exports.broadcast_block = function(block) { - ios.sockets.in('inv').emit('block', block); + if (ios) ios.sockets.in('inv').emit('block', block); }; module.exports.broadcast_address_tx = function(address, tx) { - ios.sockets.in(address).emit(address, tx); + if (ios) ios.sockets.in(address).emit(address, tx); }; module.exports.broadcastSyncInfo = function(historicSync) { - ios.sockets.in('sync').emit('status', historicSync); + if (ios) ios.sockets.in('sync').emit('status', historicSync); }; diff --git a/config/env/development.js b/config/env/development.js index 36ef11c..05ae2af 100755 --- a/config/env/development.js +++ b/config/env/development.js @@ -9,11 +9,12 @@ module.exports = { protocol: process.env.BITCOIND_PROTO || 'http', user: process.env.BITCOIND_USER || 'user', pass: process.env.BITCOIND_PASS || 'pass', - host: process.env.BITCOIND_HOST || '162.242.219.26', - port: process.env.BITCOIND_PORT || '8332', + host: process.env.BITCOIND_HOST || '127.0.0.1', + port: process.env.BITCOIND_PORT || '18332', + p2p_port: process.env.BITCOIND_P2P_PORT || '18333', disableAgent: true, }, - network: process.env.INSIGHT_NETWORK || 'livenet', + network: process.env.INSIGHT_NETWORK || 'testnet', disableP2pSync: false, disableHistoricSync: false, }; diff --git a/config/env/production.js b/config/env/production.js index f001799..b85d1ce 100755 --- a/config/env/production.js +++ b/config/env/production.js @@ -1,8 +1,20 @@ 'use strict'; module.exports = { - db: 'mongodb://localhost/insight', + db: 'mongodb://localhost/insight-test', app: { - name: 'Insight - Production' - } + name: 'Insight - Prod' + }, + port: '3301', + bitcoind: { + protocol: process.env.BITCOIND_PROTO || 'http', + user: process.env.BITCOIND_USER || 'user', + pass: process.env.BITCOIND_PASS || 'pass', + host: process.env.BITCOIND_HOST || '127.0.0.1', + port: process.env.BITCOIND_PORT || '18332', + p2p_port: process.env.BITCOIND_P2P_PORT || '18333', + disableAgent: true, + + }, + network: 'testnet', }; diff --git a/lib/HistoricSync.js b/lib/HistoricSync.js index a7265d1..47101cc 100644 --- a/lib/HistoricSync.js +++ b/lib/HistoricSync.js @@ -17,16 +17,16 @@ function spec() { var BAD_GEN_ERROR = 'Bad genesis block. Network mismatch between Insight and bitcoind? Insight is configured for:'; - function HistoricSync(opts) { + function HistoricSync() { this.network = config.network === 'testnet' ? networks.testnet: networks.livenet; var genesisHashReversed = new Buffer(32); this.network.genesisBlock.hash.copy(genesisHashReversed); this.genesis = genesisHashReversed.reverse().toString('hex'); - this.sync = new Sync(opts); - //available status: new / syncing / finished / aborted - this.status = 'new'; + + //available status: starting / syncing / finished / aborted + this.status = 'starting'; this.error = null; this.syncPercentage = 0; @@ -54,8 +54,11 @@ function spec() { HistoricSync.prototype.init = function(opts, cb) { var self = this; - self.rpc = new RpcClient(config.bitcoind); self.opts = opts; + + self.rpc = new RpcClient(config.bitcoind); + self.sync = new Sync(opts); + self.sync.init(opts, function(err) { if (err) { self.setError(err); @@ -109,7 +112,7 @@ function spec() { } }; - HistoricSync.prototype.getPrevNextBlock = function(blockHash, blockEnd, opts, cb) { + HistoricSync.prototype.getPrevNextBlock = function(blockHash, blockEnd, scanOpts, cb) { var self = this; // recursion end. @@ -173,7 +176,7 @@ function spec() { if (blockInfo.result.prevblockhash != current.blockHash) { p("reorg?"); - opts.prev = 1; + scanOpts.prev = 1; } return c(); } @@ -191,7 +194,7 @@ function spec() { self.status = 'syncing'; } - if ( (opts.upToExisting && existed && self.syncedBlocks >= self.blockChainHeight) || + if ( (scanOpts.upToExisting && existed && self.syncedBlocks >= self.blockChainHeight) || (blockEnd && blockEnd === blockHash)) { self.status = 'finished'; p('DONE. Found existing block: ', blockHash); @@ -208,17 +211,17 @@ function spec() { self.syncedBlocks++; // recursion - if (opts.prev && blockInfo.result.previousblockhash) - return self.getPrevNextBlock(blockInfo.result.previousblockhash, blockEnd, opts, cb); + if (scanOpts.prev && blockInfo.result.previousblockhash) + return self.getPrevNextBlock(blockInfo.result.previousblockhash, blockEnd, scanOpts, cb); - if (opts.next && blockInfo.result.nextblockhash) - return self.getPrevNextBlock(blockInfo.result.nextblockhash, blockEnd, opts, cb); + if (scanOpts.next && blockInfo.result.nextblockhash) + return self.getPrevNextBlock(blockInfo.result.nextblockhash, blockEnd, scanOpts, cb); } return cb(err); }); }; - HistoricSync.prototype.importHistory = function(opts, next) { + HistoricSync.prototype.importHistory = function(scanOpts, next) { var self = this; var retry_secs = 2; @@ -227,7 +230,7 @@ function spec() { async.series([ function(cb) { - if (opts.destroy) { + if (scanOpts.destroy) { p('Deleting DB...'); return self.sync.destroy(cb); } @@ -235,7 +238,7 @@ function spec() { }, // We are not using getBestBlockHash, because is not available in all clients function(cb) { - if (!opts.reverse) return cb(); + if (!scanOpts.reverse) return cb(); self.rpc.getBlockCount(function(err, res) { if (err) return cb(err); @@ -244,19 +247,17 @@ function spec() { }); }, function(cb) { - if (!opts.reverse) return cb(); + if (!scanOpts.reverse) return cb(); self.rpc.getBlockHash(self.blockChainHeight, function(err, res) { if (err) return cb(err); - lastBlock = res.result; return cb(); }); }, function(cb) { - if (opts.upToExisting) { - + if (scanOpts.upToExisting) { // should be isOrphan = true or null to be more accurate. Block.count({ isOrphan: null @@ -268,25 +269,28 @@ function spec() { return cb(); }); } + else { + return cb(); + } }, ], function(err) { var start, end; function sync() { - if (opts.reverse) { + if (scanOpts.reverse) { start = lastBlock; end = self.genesis; - opts.prev = true; + scanOpts.prev = true; } else { start = self.genesis; end = null; - opts.next = true; + scanOpts.next = true; } p('Starting from: ', start); p(' to : ', end); - p(' opts: ', JSON.stringify(opts)); + p(' scanOpts: ', JSON.stringify(scanOpts)); - self.getPrevNextBlock(start, end, opts, function(err) { + self.getPrevNextBlock(start, end, scanOpts, function(err) { if (err && err.message.match(/ECONNREFUSED/)) { setTimeout(function() { p('Retrying in %d secs', retry_secs); @@ -338,12 +342,12 @@ function spec() { p('Genesis block found. Syncing upto known blocks.'); } - var opts = { + var scanOpts = { reverse: true, upToExisting: b ? true: false, }; - return self.importHistory(opts, next); + return self.importHistory(scanOpts, next); }); }; diff --git a/lib/PeerSync.js b/lib/PeerSync.js index 7ef05cd..d4c861c 100644 --- a/lib/PeerSync.js +++ b/lib/PeerSync.js @@ -7,47 +7,42 @@ function spec() { var coinUtil = require('bitcore/util/util'); var Sync = require('./Sync').class(); var Peer = require('bitcore/Peer').class(); + var config = require('../config/config'); var peerdb_fn = 'peerdb.json'; function PeerSync() {} - PeerSync.prototype.init = function(config, cb) { - if (!config) config = {}; - var network = config && (config.network || 'testnet'); + PeerSync.prototype.init = function(opts, cb) { + if (!opts) opts = {}; + var network = opts && (opts.network || 'testnet'); - this.verbose = config.verbose; + this.verbose = opts.verbose; this.peerdb = undefined; this.sync = new Sync({ networkName: network }); this.PeerManager = require('bitcore/PeerManager').createClass({ - config: { + opts: { network: network } }); this.peerman = new this.PeerManager(); this.load_peers(); - this.sync.init(config, function() { + this.sync.init(opts, function() { return cb(); }); }; PeerSync.prototype.load_peers = function() { - try { - this.peerdb = JSON.parse(fs.readFileSync(peerdb_fn)); - } catch(d) { - console.warn('Unable to read peer db', peerdb_fn, 'creating new one.'); - this.peerdb = [{ - ipv4: '127.0.0.1', - port: 18333 - }, - ]; + this.peerdb = [{ + ipv4: config.bitcoind.host, + port: config.bitcoind.p2p_port + }]; - fs.writeFileSync(peerdb_fn, JSON.stringify(this.peerdb)); - } + fs.writeFileSync(peerdb_fn, JSON.stringify(this.peerdb)); }; PeerSync.prototype.handle_inv = function(info) { diff --git a/public/js/controllers/status.js b/public/js/controllers/status.js index 15d7a7d..8a67644 100644 --- a/public/js/controllers/status.js +++ b/public/js/controllers/status.js @@ -13,7 +13,7 @@ function($scope, $routeParams, $location, Global, Status, Sync, getSocket) { angular.extend($scope, d); }, function(e) { - $scope.error = 'API ERROR: ' + e.data; + $scope.error = 'API ERROR: ' + e.data; }); }; diff --git a/public/views/infoStatus.html b/public/views/infoStatus.html index d2e250c..011a809 100644 --- a/public/views/infoStatus.html +++ b/public/views/infoStatus.html @@ -1,4 +1,4 @@ -Loading... +Loading... {{error}} diff --git a/public/views/status.html b/public/views/status.html index 4f9cb6a..d5f99bb 100644 --- a/public/views/status.html +++ b/public/views/status.html @@ -1,4 +1,4 @@ -
+
Loading...
{{infoError}}
Mining Difficulty {{difficulty}}
+
{{sync.error}} @@ -22,6 +22,11 @@ + + + + + @@ -36,10 +41,9 @@
Current Sync Status{{sync.status}}
Initial Block Chain Height {{sync.blockChainHeight}}

Transaction Output Set Information

- - - - +
+ + @@ -72,10 +76,10 @@
Height {{txoutsetinfo.height}}

Last Block

- - +
+ - + @@ -85,8 +89,9 @@

Bitcoin node information

-
Last block hashLast Block Hash {{lastblockhash}}
- +
+ + @@ -95,14 +100,6 @@ - - - - - - - - @@ -147,8 +144,9 @@
Version {{info.version}}Protocol version {{info.protocolversion}}
Wallet version{{info.walletversion}}
Balance (BTC){{info.balance}}
Blocks {{info.blocks}}

Difficulty

- - +
+ + diff --git a/util/sync.js b/util/sync.js index 0ffda1f..48a4307 100755 --- a/util/sync.js +++ b/util/sync.js @@ -18,9 +18,7 @@ program .option('-U --uptoexisting', 'Sync only until an existing block is found', 0) .parse(process.argv); -var historicSync = new HistoricSync({ - networkName: program.network -}); +var historicSync = new HistoricSync(); /* TODO: Sure? if (program.remove) {
Mining Difficulty {{difficulty}}