diff --git a/app/controllers/socket.js b/app/controllers/socket.js index 7bcad79..fa78f55 100644 --- a/app/controllers/socket.js +++ b/app/controllers/socket.js @@ -21,3 +21,7 @@ module.exports.broadcast_tx = function(tx) { module.exports.broadcast_block = function(block) { ios.sockets.emit('block', block); }; + +module.exports.broadcastSyncInfo = function(syncInfo) { + ios.sockets.emit('block', syncInfo); +}; diff --git a/lib/HistoricSync.js b/lib/HistoricSync.js index fd64c67..2735f44 100644 --- a/lib/HistoricSync.js +++ b/lib/HistoricSync.js @@ -11,6 +11,7 @@ function spec() { var config = require('../config/config'); var Block = require('../app/models/Block'); var Sync = require('./Sync').class(); + var sockets = require('../app/controllers/socket.js'); function HistoricSync(opts) { this.network = config.network === 'testnet' ? networks.testnet: networks.livenet; @@ -20,6 +21,7 @@ function spec() { this.genesis = genesisHashReversed.reverse().toString('hex'); this.sync = new Sync(opts); + //available status: new / syncing / finished / aborted this.status = 'new'; this.syncInfo = {}; @@ -28,17 +30,14 @@ function spec() { function p() { var args = []; Array.prototype.push.apply( args, arguments ); + + args.unshift('[historic_sync]'); /*jshint validthis:true */ console.log.apply(this, args); } - var printProgress = function(i) { - var per = parseInt(100 * i.syncedBlocks / i.blocksToSync); - p(util.format('status: %d/%d [%d%%]', i.syncedBlocks, i.blocksToSync, per)); - }; - HistoricSync.prototype.init = function(opts,cb) { this.rpc = new RpcClient(config.bitcoind); this.opts = opts; @@ -49,6 +48,19 @@ function spec() { this.sync.close(); }; + + HistoricSync.prototype.showProgress = function() { + var self = this; + + var i = self.syncInfo; + var per = parseInt(100 * i.syncedBlocks / i.blocksToSync); + p(util.format('status: %d/%d [%d%%]', i.syncedBlocks, i.blocksToSync, per)); + if (self.opts.broadcast) { + sockets.broadcastSyncInfo(self.syncInfo); + } + }; + + HistoricSync.prototype.getPrevNextBlock = function(blockHash, blockEnd, opts, cb) { var self = this; @@ -77,7 +89,7 @@ function spec() { if (step < 10) step = 10; if (self.syncInfo.syncedBlocks % step === 1) { - printProgress(self.syncInfo); + self.showProgress(); } return c(); }, @@ -129,7 +141,7 @@ function spec() { self.status = 'syncing'; } - if (opts.uptoexisting && existed ) { + if (opts.upToExisting && existed ) { if (self.syncInfo.blocksToSync <= self.syncInfo.syncedBlocks) { self.status = 'finished'; p('DONE. Found existing block: ', blockHash); @@ -172,29 +184,11 @@ function spec() { async.series([ function(cb) { if (opts.destroy) { - p('Deleting Blocks...'); - self.db.collections.blocks.drop(cb); - } else { - return cb(); + p('Deleting DB...'); + return self.sync.destroy(cb); } + return cb(); }, - function(cb) { - if (opts.destroy) { - p('Deleting TXs...'); - self.db.collections.transactions.drop(cb); - } else { - return cb(); - } - }, - function(cb) { - if (opts.destroy) { - p('Deleting TXItems...'); - self.db.collections.transactionitems.drop(cb); - } else { - return cb(); - } - }, - // We are not using getBestBlockHash, because is not available in all clients function(cb) { if (!opts.reverse) return cb(); @@ -218,7 +212,7 @@ function spec() { }, function(cb) { // This is only to inform progress. - if (!opts.uptoexisting) { + if (!opts.upToExisting) { self.rpc.getInfo(function(err, res) { if (err) return cb(err); self.syncInfo.blocksToSync = res.result.blocks; @@ -260,7 +254,7 @@ function spec() { isEndGenesis: end === self.genesis, scanningForward: opts.next, scanningBackward: opts.prev, - uptoexisting: opts.uptoexisting, + upToExisting: opts.upToExisting, syncedBlocks: 0, }); @@ -306,7 +300,7 @@ function spec() { var opts = { reverse: 1, - uptoexisting: b ? true: false, + upToExisting: b ? true: false, }; return self.import_history(opts, next); diff --git a/lib/Sync.js b/lib/Sync.js index 4a4ecdc..3ff5554 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -9,6 +9,7 @@ function spec() { var Block = require('../app/models/Block'); var Transaction = require('../app/models/Transaction'); var sockets = require('../app/controllers/socket.js'); + var async = require('async'); function Sync() { @@ -16,12 +17,11 @@ function spec() { } Sync.prototype.init = function(opts, cb) { - var that = this; + var self = this; - that.opts = opts; - - if (!(opts && opts.skip_db_connection)) { + self.opts = opts; + if (!(opts && opts.skipDbConnection)) { if (mongoose.connection.readyState !== 1) { mongoose.connect(config.db, function(err) { @@ -32,19 +32,19 @@ function spec() { }); } - that.db = mongoose.connection; + self.db = mongoose.connection; - that.db.on('error', function(err) { + self.db.on('error', function(err) { console.log('MongoDB ERROR:' + err); return cb(err); }); - that.db.on('disconnect', function(err) { + self.db.on('disconnect', function(err) { console.log('MongoDB disconnect:' + err); return cb(err); }); - return that.db.once('open', function(err) { + return self.db.once('open', function(err) { return cb(err); }); } @@ -57,24 +57,34 @@ function spec() { } }; + + Sync.prototype.destroy = function(next) { + var self = this; + async.series([ + function(b) { return self.db.collections.blocks.drop(b);}, + function(b) { return self.db.collections.transactions.drop(b);}, + function(b) { return self.db.collections.transactionitems.drop(b);}, + ], next); + }; + Sync.prototype.storeBlock = function(block, cb) { - var that = this; + var self = this; Block.customCreate(block, function(err, block, inserted_txs){ if (err) return cb(err); - if (block && that.opts.broadcast_blocks) { + if (block && self.opts.broadcast_blocks) { sockets.broadcast_block(block); } - if (inserted_txs && that.opts.broadcast_txs) { + if (inserted_txs && self.opts.broadcast_txs) { inserted_txs.forEach(function(tx) { sockets.broadcast_tx(tx); }); } if (inserted_txs) - that.tx_count += inserted_txs.length; + self.tx_count += inserted_txs.length; return cb(); }); @@ -82,12 +92,12 @@ function spec() { Sync.prototype.storeTxs = function(txs, inTime, cb) { - var that = this; + var self = this; var time = inTime ? inTime : Math.round(new Date().getTime() / 1000); Transaction.createFromArray(txs, time, function(err, inserted_txs) { - if (!err && inserted_txs && that.opts.broadcast_txs) { + if (!err && inserted_txs && self.opts.broadcast_txs) { inserted_txs.forEach(function(tx) { sockets.broadcast_tx(tx); diff --git a/server.js b/server.js index 59303fd..1f49503 100644 --- a/server.js +++ b/server.js @@ -48,7 +48,8 @@ var historicSync = {}; if (!config.disableHistoricSync) { historicSync = new HistoricSync(); historicSync.init({ - skip_db_connection: true, + skipDbConnection: true, + shouldBroadcast: true, networkName: config.network }, function() { historicSync.smart_import(function(err){ @@ -65,7 +66,7 @@ if (!config.disableHistoricSync) { if (!config.disableP2pSync) { var ps = new PeerSync(); ps.init({ - skip_db_connection: true, + skipDbConnection: true, broadcast_txs: true, broadcast_blocks: true }, function() { diff --git a/util/sync.js b/util/sync.js index 01205d9..d399a1e 100755 --- a/util/sync.js +++ b/util/sync.js @@ -41,7 +41,7 @@ async.series([ historicSync.import_history({ destroy: program.destroy, reverse: program.reverse, - uptoexisting: program.uptoexisting, + upToExisting: program.uptoexisting, }, cb); } },