Merge branch 'master' of github.com:bitpay/mystery into feature/stream-new-txs
Conflicts: lib/Sync.js
This commit is contained in:
commit
2e0bd4c660
|
@ -8,7 +8,7 @@ lib-cov
|
|||
*.pid
|
||||
*.gz
|
||||
*.swp
|
||||
|
||||
tags
|
||||
pids
|
||||
logs
|
||||
results
|
||||
|
@ -23,7 +23,6 @@ node_modules
|
|||
peerdb.json
|
||||
|
||||
npm-debug.log
|
||||
node_modules
|
||||
.nodemonignore
|
||||
|
||||
.DS_Store
|
||||
|
|
|
@ -93,14 +93,16 @@ TransactionSchema.statics.createFromArray = function(txs, next) {
|
|||
if (!txs) return next();
|
||||
var mongo_txs = [];
|
||||
async.forEach( txs,
|
||||
function(tx, callback) {
|
||||
function(tx, cb) {
|
||||
that.create({ txid: tx }, function(err, new_tx) {
|
||||
if (err && ! err.toString().match(/E11000/)) {
|
||||
return callback(err);
|
||||
if (err) {
|
||||
if (err.toString().match(/E11000/)) {
|
||||
return cb();
|
||||
}
|
||||
return cb(err);
|
||||
}
|
||||
console.log(new_tx);
|
||||
mongo_txs.push(new_tx);
|
||||
return callback();
|
||||
return cb();
|
||||
});
|
||||
},
|
||||
function(err) {
|
||||
|
|
|
@ -10,11 +10,7 @@ module.exports.init = function(app, io_ext) {
|
|||
io = io_ext;
|
||||
io.set('log level', 1); // reduce logging
|
||||
io.sockets.on('connection', function(socket) {
|
||||
Transaction.findOne(function(err, tx) {
|
||||
setTimeout(function() {
|
||||
socket.emit('tx', tx);
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
|
|
47
lib/Sync.js
47
lib/Sync.js
|
@ -64,11 +64,12 @@ function spec() {
|
|||
var that=this;
|
||||
Transaction.createFromArray(txids, function(err, inserted_txs) {
|
||||
if (err) return cb(err);
|
||||
|
||||
async.each(inserted_txs, function(new_tx, next) {
|
||||
var txid = new_tx.txid;
|
||||
|
||||
sockets.broadcast_tx(new_tx);
|
||||
if (that.opts.broadcast_txs) {
|
||||
sockets.broadcast_tx(new_tx);
|
||||
}
|
||||
// This will trigger an RPC call
|
||||
Transaction.explodeTransactionItems( txid, function(err) {
|
||||
that.tx_count++;
|
||||
|
@ -202,20 +203,35 @@ function spec() {
|
|||
};
|
||||
|
||||
Sync.prototype.init = function(opts) {
|
||||
this.rpc = new RpcClient(config.bitcoind);
|
||||
|
||||
|
||||
if (!(opts && opts.skip_db_connection)) {
|
||||
mongoose.connect(config.db);
|
||||
mongoose.connect(config.db, {server: {auto_reconnect: true}} );
|
||||
}
|
||||
this.opts = opts;
|
||||
this.db = mongoose.connection;
|
||||
this.rpc = new RpcClient(config.bitcoind);
|
||||
|
||||
this.db.on('error', console.error.bind(console, 'connection error:'));
|
||||
this.db.on('error', function(err) {
|
||||
console.log('connection error:' + err);
|
||||
moogose.disconnect();
|
||||
});
|
||||
|
||||
this.db.on('disconnect', function(err) {
|
||||
console.log('disconnect:' + err);
|
||||
mongoose.connect(config.db, {server: {auto_reconnect: true}} );
|
||||
});
|
||||
|
||||
|
||||
};
|
||||
|
||||
Sync.prototype.import_history = function(opts, next) {
|
||||
|
||||
var that = this;
|
||||
|
||||
var retry_attemps = 100;
|
||||
var retry_secs = 2;
|
||||
|
||||
this.db.once('open', function() {
|
||||
async.series([
|
||||
function(cb) {
|
||||
|
@ -244,10 +260,23 @@ function spec() {
|
|||
},
|
||||
|
||||
function(cb) {
|
||||
function sync() {
|
||||
that.syncBlocks( function(err) {
|
||||
|
||||
|
||||
if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){
|
||||
setTimeout(function() {
|
||||
console.log("Retrying in %d secs ", retry_secs);
|
||||
sync();
|
||||
}, retry_secs * 1000);
|
||||
}
|
||||
else
|
||||
return next(err);
|
||||
});
|
||||
}
|
||||
|
||||
if (!opts.skip_blocks) {
|
||||
that.syncBlocks( cb);
|
||||
} else {
|
||||
cb();
|
||||
sync();
|
||||
}
|
||||
},
|
||||
/* Exploding happens on block insertion
|
||||
|
@ -271,7 +300,7 @@ function spec() {
|
|||
}
|
||||
*/
|
||||
], function(err) {
|
||||
return next(err);
|
||||
return next(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue