2014-01-10 11:02:33 -08:00
|
|
|
'use strict';
|
2014-01-07 10:21:59 -08:00
|
|
|
|
2014-01-10 11:02:33 -08:00
|
|
|
require('classtool');
|
2014-01-08 09:50:37 -08:00
|
|
|
|
|
|
|
|
2014-01-10 11:02:33 -08:00
|
|
|
function spec() {
|
2014-01-10 16:42:39 -08:00
|
|
|
var mongoose = require('mongoose');
|
|
|
|
var config = require('../config/config');
|
|
|
|
var Block = require('../app/models/Block');
|
|
|
|
var Transaction = require('../app/models/Transaction');
|
2014-01-16 12:25:52 -08:00
|
|
|
var sockets = require('../app/controllers/socket.js');
|
2014-01-15 12:36:49 -08:00
|
|
|
|
2014-01-10 11:57:16 -08:00
|
|
|
|
2014-01-16 18:23:10 -08:00
|
|
|
function Sync() {
|
2014-01-15 12:36:49 -08:00
|
|
|
this.tx_count = 0;
|
2014-01-10 11:57:16 -08:00
|
|
|
}
|
|
|
|
|
2014-01-18 13:28:24 -08:00
|
|
|
Sync.prototype.init = function(opts, cb) {
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
that.opts = opts;
|
|
|
|
|
|
|
|
if (!(opts && opts.skip_db_connection)) {
|
|
|
|
|
|
|
|
|
|
|
|
if (!mongoose.connection.readyState == 1) {
|
|
|
|
mongoose.connect(config.db, function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.log('CRITICAL ERROR: connecting to mongoDB:',err);
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
that.db = mongoose.connection;
|
|
|
|
|
|
|
|
that.db.on('error', function(err) {
|
|
|
|
console.log('MongoDB ERROR:' + err);
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
that.db.on('disconnect', function(err) {
|
|
|
|
console.log('MongoDB disconnect:' + err);
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
return that.db.once('open', function(err) {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else return cb();
|
|
|
|
};
|
|
|
|
|
|
|
|
Sync.prototype.close = function() {
|
|
|
|
if ( this.db && this.db.readyState ) {
|
|
|
|
this.db.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-10 11:57:16 -08:00
|
|
|
Sync.prototype.storeBlock = function(block, cb) {
|
2014-01-14 14:42:38 -08:00
|
|
|
var that = this;
|
2014-01-15 12:36:49 -08:00
|
|
|
|
2014-01-16 08:01:32 -08:00
|
|
|
Block.customCreate(block, function(err, block, inserted_txs){
|
2014-01-17 11:36:34 -08:00
|
|
|
if (err) return cb(err);
|
2014-01-15 12:36:49 -08:00
|
|
|
|
2014-01-16 08:01:32 -08:00
|
|
|
if (block && that.opts.broadcast_blocks) {
|
|
|
|
sockets.broadcast_block(block);
|
2014-01-14 14:42:38 -08:00
|
|
|
}
|
2014-01-10 11:57:16 -08:00
|
|
|
|
2014-01-16 08:01:32 -08:00
|
|
|
if (inserted_txs && that.opts.broadcast_txs) {
|
|
|
|
inserted_txs.forEach(function(tx) {
|
|
|
|
sockets.broadcast_tx(tx);
|
2014-01-15 12:36:49 -08:00
|
|
|
});
|
|
|
|
}
|
2014-01-14 13:09:45 -08:00
|
|
|
|
2014-01-16 08:01:32 -08:00
|
|
|
if (inserted_txs)
|
|
|
|
that.tx_count += inserted_txs.length;
|
2014-01-14 13:09:45 -08:00
|
|
|
|
2014-01-15 12:36:49 -08:00
|
|
|
return cb();
|
2014-01-11 20:29:25 -08:00
|
|
|
});
|
2014-01-10 11:57:16 -08:00
|
|
|
};
|
|
|
|
|
2014-01-16 08:01:32 -08:00
|
|
|
|
|
|
|
Sync.prototype.storeTxs = function(txs, inTime, cb) {
|
2014-01-10 11:57:16 -08:00
|
|
|
var that = this;
|
|
|
|
|
2014-01-16 08:01:32 -08:00
|
|
|
var time = inTime ? inTime : Math.round(new Date().getTime() / 1000);
|
2014-01-10 11:57:16 -08:00
|
|
|
|
2014-01-16 08:01:32 -08:00
|
|
|
Transaction.createFromArray(txs, time, function(err, inserted_txs) {
|
|
|
|
if (!err && inserted_txs && that.opts.broadcast_txs) {
|
|
|
|
|
|
|
|
inserted_txs.forEach(function(tx) {
|
|
|
|
sockets.broadcast_tx(tx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
};
|
2014-01-10 11:57:16 -08:00
|
|
|
return Sync;
|
2014-01-10 11:02:33 -08:00
|
|
|
}
|
2014-01-07 10:21:59 -08:00
|
|
|
module.defineClass(spec);
|
2014-01-10 11:02:33 -08:00
|
|
|
|