sync API not emits "sync" thru socket.io
This commit is contained in:
parent
08a54a40e2
commit
10972fd8dd
|
@ -21,3 +21,7 @@ module.exports.broadcast_tx = function(tx) {
|
||||||
module.exports.broadcast_block = function(block) {
|
module.exports.broadcast_block = function(block) {
|
||||||
ios.sockets.emit('block', block);
|
ios.sockets.emit('block', block);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.broadcastSyncInfo = function(syncInfo) {
|
||||||
|
ios.sockets.emit('block', syncInfo);
|
||||||
|
};
|
||||||
|
|
|
@ -11,6 +11,7 @@ function spec() {
|
||||||
var config = require('../config/config');
|
var config = require('../config/config');
|
||||||
var Block = require('../app/models/Block');
|
var Block = require('../app/models/Block');
|
||||||
var Sync = require('./Sync').class();
|
var Sync = require('./Sync').class();
|
||||||
|
var sockets = require('../app/controllers/socket.js');
|
||||||
|
|
||||||
function HistoricSync(opts) {
|
function HistoricSync(opts) {
|
||||||
this.network = config.network === 'testnet' ? networks.testnet: networks.livenet;
|
this.network = config.network === 'testnet' ? networks.testnet: networks.livenet;
|
||||||
|
@ -20,6 +21,7 @@ function spec() {
|
||||||
this.genesis = genesisHashReversed.reverse().toString('hex');
|
this.genesis = genesisHashReversed.reverse().toString('hex');
|
||||||
this.sync = new Sync(opts);
|
this.sync = new Sync(opts);
|
||||||
|
|
||||||
|
|
||||||
//available status: new / syncing / finished / aborted
|
//available status: new / syncing / finished / aborted
|
||||||
this.status = 'new';
|
this.status = 'new';
|
||||||
this.syncInfo = {};
|
this.syncInfo = {};
|
||||||
|
@ -28,17 +30,14 @@ function spec() {
|
||||||
function p() {
|
function p() {
|
||||||
var args = [];
|
var args = [];
|
||||||
Array.prototype.push.apply( args, arguments );
|
Array.prototype.push.apply( args, arguments );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
args.unshift('[historic_sync]');
|
args.unshift('[historic_sync]');
|
||||||
/*jshint validthis:true */
|
/*jshint validthis:true */
|
||||||
console.log.apply(this, args);
|
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) {
|
HistoricSync.prototype.init = function(opts,cb) {
|
||||||
this.rpc = new RpcClient(config.bitcoind);
|
this.rpc = new RpcClient(config.bitcoind);
|
||||||
this.opts = opts;
|
this.opts = opts;
|
||||||
|
@ -49,6 +48,19 @@ function spec() {
|
||||||
this.sync.close();
|
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) {
|
HistoricSync.prototype.getPrevNextBlock = function(blockHash, blockEnd, opts, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
@ -77,7 +89,7 @@ function spec() {
|
||||||
if (step < 10) step = 10;
|
if (step < 10) step = 10;
|
||||||
|
|
||||||
if (self.syncInfo.syncedBlocks % step === 1) {
|
if (self.syncInfo.syncedBlocks % step === 1) {
|
||||||
printProgress(self.syncInfo);
|
self.showProgress();
|
||||||
}
|
}
|
||||||
return c();
|
return c();
|
||||||
},
|
},
|
||||||
|
@ -129,7 +141,7 @@ function spec() {
|
||||||
self.status = 'syncing';
|
self.status = 'syncing';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.uptoexisting && existed ) {
|
if (opts.upToExisting && existed ) {
|
||||||
if (self.syncInfo.blocksToSync <= self.syncInfo.syncedBlocks) {
|
if (self.syncInfo.blocksToSync <= self.syncInfo.syncedBlocks) {
|
||||||
self.status = 'finished';
|
self.status = 'finished';
|
||||||
p('DONE. Found existing block: ', blockHash);
|
p('DONE. Found existing block: ', blockHash);
|
||||||
|
@ -172,29 +184,11 @@ function spec() {
|
||||||
async.series([
|
async.series([
|
||||||
function(cb) {
|
function(cb) {
|
||||||
if (opts.destroy) {
|
if (opts.destroy) {
|
||||||
p('Deleting Blocks...');
|
p('Deleting DB...');
|
||||||
self.db.collections.blocks.drop(cb);
|
return self.sync.destroy(cb);
|
||||||
} else {
|
|
||||||
return 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
|
// We are not using getBestBlockHash, because is not available in all clients
|
||||||
function(cb) {
|
function(cb) {
|
||||||
if (!opts.reverse) return cb();
|
if (!opts.reverse) return cb();
|
||||||
|
@ -218,7 +212,7 @@ function spec() {
|
||||||
},
|
},
|
||||||
function(cb) {
|
function(cb) {
|
||||||
// This is only to inform progress.
|
// This is only to inform progress.
|
||||||
if (!opts.uptoexisting) {
|
if (!opts.upToExisting) {
|
||||||
self.rpc.getInfo(function(err, res) {
|
self.rpc.getInfo(function(err, res) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
self.syncInfo.blocksToSync = res.result.blocks;
|
self.syncInfo.blocksToSync = res.result.blocks;
|
||||||
|
@ -260,7 +254,7 @@ function spec() {
|
||||||
isEndGenesis: end === self.genesis,
|
isEndGenesis: end === self.genesis,
|
||||||
scanningForward: opts.next,
|
scanningForward: opts.next,
|
||||||
scanningBackward: opts.prev,
|
scanningBackward: opts.prev,
|
||||||
uptoexisting: opts.uptoexisting,
|
upToExisting: opts.upToExisting,
|
||||||
syncedBlocks: 0,
|
syncedBlocks: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -306,7 +300,7 @@ function spec() {
|
||||||
|
|
||||||
var opts = {
|
var opts = {
|
||||||
reverse: 1,
|
reverse: 1,
|
||||||
uptoexisting: b ? true: false,
|
upToExisting: b ? true: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
return self.import_history(opts, next);
|
return self.import_history(opts, next);
|
||||||
|
|
38
lib/Sync.js
38
lib/Sync.js
|
@ -9,6 +9,7 @@ function spec() {
|
||||||
var Block = require('../app/models/Block');
|
var Block = require('../app/models/Block');
|
||||||
var Transaction = require('../app/models/Transaction');
|
var Transaction = require('../app/models/Transaction');
|
||||||
var sockets = require('../app/controllers/socket.js');
|
var sockets = require('../app/controllers/socket.js');
|
||||||
|
var async = require('async');
|
||||||
|
|
||||||
|
|
||||||
function Sync() {
|
function Sync() {
|
||||||
|
@ -16,12 +17,11 @@ function spec() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Sync.prototype.init = function(opts, cb) {
|
Sync.prototype.init = function(opts, cb) {
|
||||||
var that = this;
|
var self = this;
|
||||||
|
|
||||||
that.opts = opts;
|
self.opts = opts;
|
||||||
|
|
||||||
if (!(opts && opts.skip_db_connection)) {
|
|
||||||
|
|
||||||
|
if (!(opts && opts.skipDbConnection)) {
|
||||||
|
|
||||||
if (mongoose.connection.readyState !== 1) {
|
if (mongoose.connection.readyState !== 1) {
|
||||||
mongoose.connect(config.db, function(err) {
|
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);
|
console.log('MongoDB ERROR:' + err);
|
||||||
return cb(err);
|
return cb(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
that.db.on('disconnect', function(err) {
|
self.db.on('disconnect', function(err) {
|
||||||
console.log('MongoDB disconnect:' + err);
|
console.log('MongoDB disconnect:' + err);
|
||||||
return cb(err);
|
return cb(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
return that.db.once('open', function(err) {
|
return self.db.once('open', function(err) {
|
||||||
return cb(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) {
|
Sync.prototype.storeBlock = function(block, cb) {
|
||||||
var that = this;
|
var self = this;
|
||||||
|
|
||||||
Block.customCreate(block, function(err, block, inserted_txs){
|
Block.customCreate(block, function(err, block, inserted_txs){
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
if (block && that.opts.broadcast_blocks) {
|
if (block && self.opts.broadcast_blocks) {
|
||||||
sockets.broadcast_block(block);
|
sockets.broadcast_block(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inserted_txs && that.opts.broadcast_txs) {
|
if (inserted_txs && self.opts.broadcast_txs) {
|
||||||
inserted_txs.forEach(function(tx) {
|
inserted_txs.forEach(function(tx) {
|
||||||
sockets.broadcast_tx(tx);
|
sockets.broadcast_tx(tx);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inserted_txs)
|
if (inserted_txs)
|
||||||
that.tx_count += inserted_txs.length;
|
self.tx_count += inserted_txs.length;
|
||||||
|
|
||||||
return cb();
|
return cb();
|
||||||
});
|
});
|
||||||
|
@ -82,12 +92,12 @@ function spec() {
|
||||||
|
|
||||||
|
|
||||||
Sync.prototype.storeTxs = function(txs, inTime, cb) {
|
Sync.prototype.storeTxs = function(txs, inTime, cb) {
|
||||||
var that = this;
|
var self = this;
|
||||||
|
|
||||||
var time = inTime ? inTime : Math.round(new Date().getTime() / 1000);
|
var time = inTime ? inTime : Math.round(new Date().getTime() / 1000);
|
||||||
|
|
||||||
Transaction.createFromArray(txs, time, function(err, inserted_txs) {
|
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) {
|
inserted_txs.forEach(function(tx) {
|
||||||
sockets.broadcast_tx(tx);
|
sockets.broadcast_tx(tx);
|
||||||
|
|
|
@ -48,7 +48,8 @@ var historicSync = {};
|
||||||
if (!config.disableHistoricSync) {
|
if (!config.disableHistoricSync) {
|
||||||
historicSync = new HistoricSync();
|
historicSync = new HistoricSync();
|
||||||
historicSync.init({
|
historicSync.init({
|
||||||
skip_db_connection: true,
|
skipDbConnection: true,
|
||||||
|
shouldBroadcast: true,
|
||||||
networkName: config.network
|
networkName: config.network
|
||||||
}, function() {
|
}, function() {
|
||||||
historicSync.smart_import(function(err){
|
historicSync.smart_import(function(err){
|
||||||
|
@ -65,7 +66,7 @@ if (!config.disableHistoricSync) {
|
||||||
if (!config.disableP2pSync) {
|
if (!config.disableP2pSync) {
|
||||||
var ps = new PeerSync();
|
var ps = new PeerSync();
|
||||||
ps.init({
|
ps.init({
|
||||||
skip_db_connection: true,
|
skipDbConnection: true,
|
||||||
broadcast_txs: true,
|
broadcast_txs: true,
|
||||||
broadcast_blocks: true
|
broadcast_blocks: true
|
||||||
}, function() {
|
}, function() {
|
||||||
|
|
|
@ -41,7 +41,7 @@ async.series([
|
||||||
historicSync.import_history({
|
historicSync.import_history({
|
||||||
destroy: program.destroy,
|
destroy: program.destroy,
|
||||||
reverse: program.reverse,
|
reverse: program.reverse,
|
||||||
uptoexisting: program.uptoexisting,
|
upToExisting: program.uptoexisting,
|
||||||
}, cb);
|
}, cb);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue