fixes jshint warnings. add config for disabling sync
This commit is contained in:
parent
a126bdd2f8
commit
59a34beda0
|
@ -1,25 +1,23 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var Transaction = require('../models/Transaction');
|
|
||||||
|
|
||||||
// server-side socket behaviour
|
// server-side socket behaviour
|
||||||
|
|
||||||
var io = null;
|
// io is a variable already taken in express
|
||||||
|
var ios = null;
|
||||||
|
|
||||||
module.exports.init = function(app, io_ext) {
|
module.exports.init = function(app, io_ext) {
|
||||||
io = io_ext;
|
ios = io_ext;
|
||||||
io.set('log level', 1); // reduce logging
|
ios.set('log level', 1); // reduce logging
|
||||||
io.sockets.on('connection', function(socket) {
|
ios.sockets.on('connection', function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports.broadcast_tx = function(tx) {
|
module.exports.broadcast_tx = function(tx) {
|
||||||
io.sockets.emit('tx', tx);
|
ios.sockets.emit('tx', tx);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports.broadcast_block = function(block) {
|
module.exports.broadcast_block = function(block) {
|
||||||
io.sockets.emit('block', block);
|
ios.sockets.emit('block', block);
|
||||||
};
|
};
|
||||||
|
|
|
@ -14,4 +14,6 @@ module.exports = {
|
||||||
disableAgent: true,
|
disableAgent: true,
|
||||||
},
|
},
|
||||||
network: 'testnet',
|
network: 'testnet',
|
||||||
|
disableP2pSync: false,
|
||||||
|
disableHistoricSync: false,
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,19 +4,13 @@ require('classtool');
|
||||||
|
|
||||||
|
|
||||||
function spec() {
|
function spec() {
|
||||||
var mongoose = require('mongoose');
|
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var RpcClient = require('bitcore/RpcClient').class();
|
var RpcClient = require('bitcore/RpcClient').class();
|
||||||
var networks = require('bitcore/networks');
|
var networks = require('bitcore/networks');
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
var config = require('../config/config');
|
var config = require('../config/config');
|
||||||
var Block = require('../app/models/Block');
|
var Block = require('../app/models/Block');
|
||||||
var Transaction = require('../app/models/Transaction');
|
|
||||||
var TransactionItem = require('../app/models/TransactionItem');
|
|
||||||
var Sync = require('./Sync').class();
|
var Sync = require('./Sync').class();
|
||||||
var sockets = require('../app/controllers/socket.js');
|
|
||||||
var CONCURRENCY = 5;
|
|
||||||
|
|
||||||
|
|
||||||
function HistoricSync(opts) {
|
function HistoricSync(opts) {
|
||||||
this.block_count= 0;
|
this.block_count= 0;
|
||||||
|
@ -26,10 +20,12 @@ function spec() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function p() {
|
function p() {
|
||||||
var params = Array.prototype.slice.call(arguments);
|
var args = [];
|
||||||
|
Array.prototype.push.apply( args, arguments );
|
||||||
|
|
||||||
params.unshift('[historic_sync]');
|
args.unshift('[historic_sync]');
|
||||||
console.log.apply(this,params);
|
/*jshint validthis:true */
|
||||||
|
console.log.apply(this, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
var progress_bar = function(string, current, total) {
|
var progress_bar = function(string, current, total) {
|
||||||
|
@ -63,7 +59,7 @@ function spec() {
|
||||||
// Already got it?
|
// Already got it?
|
||||||
function(c) {
|
function(c) {
|
||||||
Block.findOne({hash:blockHash}, function(err,block){
|
Block.findOne({hash:blockHash}, function(err,block){
|
||||||
if (err) { p(err); return c(err); };
|
if (err) { p(err); return c(err); }
|
||||||
if (block) {
|
if (block) {
|
||||||
existed = 1;
|
existed = 1;
|
||||||
blockObj = block;
|
blockObj = block;
|
||||||
|
@ -96,7 +92,7 @@ function spec() {
|
||||||
function(c) {
|
function(c) {
|
||||||
if (existed) return c();
|
if (existed) return c();
|
||||||
|
|
||||||
that.sync.storeBlock(blockInfo.result, function(err, block) {
|
that.sync.storeBlock(blockInfo.result, function(err) {
|
||||||
existed = err && err.toString().match(/E11000/);
|
existed = err && err.toString().match(/E11000/);
|
||||||
if (err && ! existed) return c(err);
|
if (err && ! existed) return c(err);
|
||||||
return c();
|
return c();
|
||||||
|
@ -133,8 +129,9 @@ function spec() {
|
||||||
HistoricSync.prototype.syncBlocks = function(start, end, isForward, cb) {
|
HistoricSync.prototype.syncBlocks = function(start, end, isForward, cb) {
|
||||||
var that = this;
|
var that = this;
|
||||||
|
|
||||||
p('Syncing Blocks, starting from: %s end: %s isForward:',
|
p('Starting from: ', start);
|
||||||
start, end, isForward);
|
p(' to : ', end);
|
||||||
|
p(' isForward: ', isForward);
|
||||||
|
|
||||||
|
|
||||||
return that.getPrevNextBlock( start, end,
|
return that.getPrevNextBlock( start, end,
|
||||||
|
@ -224,7 +221,7 @@ function spec() {
|
||||||
|
|
||||||
if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){
|
if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
p("Retrying in %d secs ", retry_secs);
|
p('Retrying in %d secs', retry_secs);
|
||||||
sync();
|
sync();
|
||||||
}, retry_secs * 1000);
|
}, retry_secs * 1000);
|
||||||
}
|
}
|
||||||
|
@ -232,9 +229,12 @@ function spec() {
|
||||||
return next(err, that.block_count);
|
return next(err, that.block_count);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
sync();
|
if (!err)
|
||||||
|
sync();
|
||||||
|
else
|
||||||
|
return next(err, 0);
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
HistoricSync.prototype.import_history = function(opts, next) {
|
HistoricSync.prototype.import_history = function(opts, next) {
|
||||||
var that = this;
|
var that = this;
|
||||||
|
|
|
@ -69,7 +69,6 @@ function spec() {
|
||||||
};
|
};
|
||||||
|
|
||||||
PeerSync.prototype.handle_block = function(info) {
|
PeerSync.prototype.handle_block = function(info) {
|
||||||
var self = this;
|
|
||||||
var block = info.message.block;
|
var block = info.message.block;
|
||||||
var blockHash = coinUtil.formatHashFull(block.calcHash());
|
var blockHash = coinUtil.formatHashFull(block.calcHash());
|
||||||
console.log('[p2p_sync] Handle block: ' + blockHash);
|
console.log('[p2p_sync] Handle block: ' + blockHash);
|
||||||
|
|
13
lib/Sync.js
13
lib/Sync.js
|
@ -5,18 +5,19 @@ require('classtool');
|
||||||
|
|
||||||
function spec() {
|
function spec() {
|
||||||
var mongoose = require('mongoose');
|
var mongoose = require('mongoose');
|
||||||
var util = require('util');
|
|
||||||
var RpcClient = require('bitcore/RpcClient').class();
|
|
||||||
var async = require('async');
|
|
||||||
var config = require('../config/config');
|
var config = require('../config/config');
|
||||||
var Block = require('../app/models/Block');
|
var Block = require('../app/models/Block');
|
||||||
var Transaction = require('../app/models/Transaction');
|
var Transaction = require('../app/models/Transaction');
|
||||||
|
<<<<<<< Updated upstream
|
||||||
var TransactionItem = require('../app/models/TransactionItem');
|
var TransactionItem = require('../app/models/TransactionItem');
|
||||||
var sockets = require('../app/controllers/socket.js');
|
var sockets = require('../app/controllers/socket.js');
|
||||||
var CONCURRENCY = 5;
|
var CONCURRENCY = 5;
|
||||||
|
=======
|
||||||
|
var sockets = require('../app/views/sockets/main.js');
|
||||||
|
>>>>>>> Stashed changes
|
||||||
|
|
||||||
|
|
||||||
function Sync(config) {
|
function Sync() {
|
||||||
this.tx_count = 0;
|
this.tx_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +85,7 @@ function spec() {
|
||||||
|
|
||||||
this.db.on('error', function(err) {
|
this.db.on('error', function(err) {
|
||||||
console.log('connection error:' + err);
|
console.log('connection error:' + err);
|
||||||
moogose.disconnect();
|
mongoose.disconnect();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.db.on('disconnect', function(err) {
|
this.db.on('disconnect', function(err) {
|
||||||
|
@ -99,7 +100,7 @@ function spec() {
|
||||||
|
|
||||||
Sync.prototype.close = function() {
|
Sync.prototype.close = function() {
|
||||||
if (!(this.opts && this.opts.skip_db_connection)) {
|
if (!(this.opts && this.opts.skip_db_connection)) {
|
||||||
console.log("closing connection");
|
console.log('closing connection');
|
||||||
this.db.close();
|
this.db.close();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
47
server.js
47
server.js
|
@ -4,6 +4,7 @@
|
||||||
//Set the node enviornment variable if not set before
|
//Set the node enviornment variable if not set before
|
||||||
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
@ -13,6 +14,7 @@ var express = require('express'),
|
||||||
HistoricSync = require('./lib/HistoricSync').class(),
|
HistoricSync = require('./lib/HistoricSync').class(),
|
||||||
mongoose = require('mongoose');
|
mongoose = require('mongoose');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main application entry file.
|
* Main application entry file.
|
||||||
*/
|
*/
|
||||||
|
@ -42,32 +44,39 @@ var walk = function(path) {
|
||||||
walk(models_path);
|
walk(models_path);
|
||||||
|
|
||||||
// historic_sync process
|
// historic_sync process
|
||||||
var hs = new HistoricSync();
|
if (!config.disableHistoricSync) {
|
||||||
hs.init({
|
var hs = new HistoricSync();
|
||||||
skip_db_connection: true,
|
hs.init({
|
||||||
networkName: config.network
|
skip_db_connection: true,
|
||||||
}, function() {
|
networkName: config.network
|
||||||
hs.import_history({
|
}, function() {
|
||||||
reverse: 1,
|
hs.import_history({
|
||||||
}, function(){
|
reverse: 1,
|
||||||
console.log('historic_sync finished!');
|
}, function(){
|
||||||
|
console.log('historic_sync finished!');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
|
||||||
// p2p_sync process
|
// p2p_sync process
|
||||||
var ps = new PeerSync();
|
if (!config.disableP2pSync) {
|
||||||
ps.init({
|
var ps = new PeerSync();
|
||||||
skip_db_connection: true,
|
ps.init({
|
||||||
broadcast_txs: true,
|
skip_db_connection: true,
|
||||||
broadcast_blocks: true
|
broadcast_txs: true,
|
||||||
}, function() {
|
broadcast_blocks: true
|
||||||
ps.run();
|
}, function() {
|
||||||
});
|
ps.run();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// express app
|
// express app
|
||||||
|
|
||||||
|
/*jshint validthis:true */
|
||||||
var app = express();
|
var app = express();
|
||||||
|
|
||||||
|
|
||||||
//express settings
|
//express settings
|
||||||
require('./config/express')(app, db);
|
require('./config/express')(app, db);
|
||||||
|
|
||||||
|
@ -76,7 +85,7 @@ require('./config/routes')(app);
|
||||||
|
|
||||||
// socket.io
|
// socket.io
|
||||||
var server = require('http').createServer(app);
|
var server = require('http').createServer(app);
|
||||||
var io = require('socket.io').listen(server);
|
var ios = require('socket.io').listen(server);
|
||||||
require('./app/controllers/socket.js').init(app,io);
|
require('./app/controllers/socket.js').init(app,io);
|
||||||
|
|
||||||
//Start the app by listening on <port>
|
//Start the app by listening on <port>
|
||||||
|
|
Loading…
Reference in New Issue