fix sockets related issues

This commit is contained in:
Matias Alejo Garcia 2014-03-20 14:06:37 -03:00
parent bc15a98aed
commit c3339dc4d6
5 changed files with 38 additions and 47 deletions

View File

@ -37,21 +37,21 @@ module.exports.broadcastTx = function(tx) {
t.valueOut = parseInt(valueOut) / util.COIN;
}
ios.sockets. in ('inv').emit('tx', t);
ios.sockets.in('inv').emit('tx', t);
}
};
module.exports.broadcastBlock = function(block) {
if (ios) ios.sockets. in ('inv').emit('block', block);
if (ios)
ios.sockets.in('inv').emit('block', block);
};
module.exports.broadcastAddressTx = function(address, tx) {
if (ios) ios.sockets. in (address).emit(address, tx);
if (ios)
ios.sockets.in(address).emit(address, tx);
};
module.exports.broadcastSyncInfo = function(historicSync) {
if (ios) {
ios.sockets. in ('sync').emit('status', historicSync);
}
if (ios)
ios.sockets.in('sync').emit('status', historicSync);
};

View File

@ -22,6 +22,7 @@ var BAD_GEN_ERROR = 'Bad genesis block. Network mismatch between Insight and bit
var BAD_GEN_ERROR_DB = 'Bad genesis block. Network mismatch between Insight and levelDB? Insight is configured for:';
function HistoricSync(opts) {
opts = opts || {};
this.shouldBroadcast = opts.shouldBroadcastSync;
this.network = config.network === 'testnet' ? networks.testnet: networks.livenet;
@ -31,7 +32,6 @@ function HistoricSync(opts) {
this.genesis = genesisHashReversed.toString('hex');
this.rpc = new RpcClient(config.bitcoind);
this.shouldBroadcast = opts.shouldBroadcastSync;
this.sync = new Sync(opts);
}

View File

@ -5,10 +5,13 @@ var Sync = require('./Sync');
var Peer = require('bitcore/Peer');
var config = require('../config/config');
var networks = require('bitcore/networks');
var sockets = require('../app/controllers/socket.js');
var peerdb_fn = 'peerdb.json';
function PeerSync(opts) {
opts = opts|| {};
this.shouldBroadcast = opts.shouldBroadcast;
this.connected = false;
this.peerdb = undefined;
this.allowReorgs = false;
@ -43,6 +46,7 @@ PeerSync.prototype.handleInv = function(info) {
};
PeerSync.prototype.handleTx = function(info) {
var self =this;
var tx = info.message.tx.getStandardizedObject();
tx.outs = info.message.tx.outs;
tx.ins = info.message.tx.ins;
@ -53,9 +57,20 @@ PeerSync.prototype.handleTx = function(info) {
if (err) {
console.log('[p2p_sync] Error in handle TX: ' + JSON.stringify(err));
}
else {
if (self.shouldBroadcast) {
sockets.broadcastTx(tx);
if (tx.addrsToEmit) {
tx.addrsToEmit.forEach(function(a) {
sockets.broadcastAddressTx(a, tx.txid);
});
}
}
}
});
};
PeerSync.prototype.handleBlock = function(info) {
var self = this;
var block = info.message.block;
@ -67,7 +82,7 @@ PeerSync.prototype.handleBlock = function(info) {
return bitcoreUtil.formatHashFull(tx.hash);
});
this.sync.storeTipBlock({
self.sync.storeTipBlock({
'hash': blockHash,
'tx': tx_hashes,
'previousblockhash': bitcoreUtil.formatHashFull(block.prev_hash),
@ -81,10 +96,15 @@ PeerSync.prototype.handleBlock = function(info) {
else if (err) {
console.log('[p2p_sync] Error in handle Block: ' + err);
}
else {
if (self.shouldBroadcast) {
sockets.broadcastBlock(blockHash);
}
}
});
};
PeerSync.prototype.handle_connected = function(data) {
PeerSync.prototype.handleConnected = function(data) {
var peerman = data.pm;
var peers_n = peerman.peers.length;
console.log('[p2p_sync] Connected to ' + peers_n + ' peer' + (peers_n !== 1 ? 's' : ''));
@ -104,7 +124,7 @@ PeerSync.prototype.run = function() {
conn.on('block', self.handleBlock.bind(self));
conn.on('tx', self.handleTx.bind(self));
});
this.peerman.on('connect', self.handle_connected.bind(self));
this.peerman.on('connect', self.handleConnected.bind(self));
this.peerman.on('netDisconnected', function() {
self.connected = false;

View File

@ -1,20 +1,18 @@
'use strict';
var imports = require('soop').imports();
var sockets = require('../app/controllers/socket.js');
var config = imports.config || require('../config/config');
var networks = require('bitcore/networks');
var async = require('async');
var syncId = 0;
function Sync(opts) {
this.id = syncId++;
this.opts = opts || {};
this.bDb = require('./BlockDb').default();
this.txDb = require('./TransactionDb').default();
this.txDb.on('tx_for_address', this.handleTxForAddress.bind(this));
this.txDb.on('new_tx', this.handleNewTx.bind(this));
this.bDb.on('new_block', this.handleNewBlock.bind(this));
this.network = config.network === 'testnet' ? networks.testnet : networks.livenet;
}
@ -252,24 +250,6 @@ Sync.prototype.setBranchConnectedBackwards = function(fromHash, cb) {
};
Sync.prototype.handleTxForAddress = function(data) {
if (this.opts.shouldBroadcast) {
sockets.broadcastAddressTx(data.address, data.txid);
}
};
Sync.prototype.handleNewTx = function(data) {
if (this.opts.shouldBroadcast) {
sockets.broadcastTx(data.tx);
}
};
Sync.prototype.handleNewBlock = function(data) {
if (this.opts.shouldBroadcast) {
sockets.broadcastBlock(data.blockid);
}
};
Sync.prototype.storeTxs = function(txs, cb) {
var self = this;
self.txDb.createFromArray(txs, null, function(err) {

View File

@ -2,7 +2,6 @@
var imports = require('soop').imports();
var ThisParent = imports.parent || require('events').EventEmitter;
// blockHash -> txid mapping
var IN_BLK_PREFIX = 'txb-'; //txb-<txid>-<block> => 1/0 (connected or not)
@ -51,7 +50,6 @@ var TransactionDb = function() {
TransactionDb.super(this, arguments);
this.network = config.network === 'testnet' ? networks.testnet : networks.livenet;
};
TransactionDb.parent = ThisParent;
TransactionDb.prototype.close = function(cb) {
db.close(cb);
@ -479,8 +477,10 @@ TransactionDb.prototype.getAddrStr = function(s) {
case Script.TX_MULTISIG:
var chunks = s.capture();
chunks.forEach(function(chunk) {
var a = new Address(self.network.addressPubkey, bitutil.sha256ripe160(chunk));
addrStrs.push(a.toString());
if (chunk && Buffer.isBuffer(chunk)) {
var a = new Address(self.network.addressPubkey, bitutil.sha256ripe160(chunk));
addrStrs.push(a.toString());
}
});
break;
case Script.TX_UNKNOWN:
@ -615,17 +615,8 @@ TransactionDb.prototype.add = function(tx, blockhash, cb) {
], function(err) {
if (addrs.length > 0 && !blockhash) {
// only emit if we are processing a single tx (not from a block)
addrs.forEach(function(addr) {
self.emit('tx_for_address', {
address: addr,
txid: tx.txid
});
});
tx.addrsToEmit=addrs;
}
self.emit('new_tx', {
tx: tx
});
return cb(err);
});
};