fix addr broadcast and sync info;

This commit is contained in:
Matias Alejo Garcia 2014-05-28 00:17:42 -03:00
parent 3da0779d70
commit 6a751e2a8f
6 changed files with 34 additions and 28 deletions

View File

@ -46,9 +46,10 @@ module.exports.broadcastBlock = function(block) {
ios.sockets.in('inv').emit('block', block);
};
module.exports.broadcastAddressTx = function(address, tx) {
if (ios)
ios.sockets.in(address).emit(address, tx);
module.exports.broadcastAddressTx = function(txid, address) {
if (ios) {
ios.sockets.in(address).emit(address, txid);
}
};
module.exports.broadcastSyncInfo = function(historicSync) {

View File

@ -148,7 +148,7 @@ BlockDb.prototype.add = function(b, height, cb) {
dbScript = dbScript.concat(this._addTxsScript(txs, b.hash, height));
this.txDb.addMany(b.tx, function(err) {
if (err) return cb(err);
db.batch(dbScript,cb);
db.batch(dbScript, cb);
});
};

View File

@ -397,7 +397,7 @@ HistoricSync.prototype.start = function(opts, next) {
if (blockInfo && blockInfo.hash && (!opts.stopAt || opts.stopAt !== blockInfo.hash)) {
self.sync.storeTipBlock(blockInfo, self.allowReorgs, function(err, height) {
if (err) return w_cb(self.setError(err));
self.height=height;
if (height>=0) self.height=height;
setImmediate(function(){
return w_cb(err);
});

View File

@ -47,25 +47,28 @@ PeerSync.prototype.handleInv = function(info) {
info.conn.sendGetData(invs);
};
PeerSync.prototype._broadcastAddr = function(txid, addrs) {
if (addrs) {
for(var ii in addrs){
sockets.broadcastAddressTx(txid, ii);
}
}
};
PeerSync.prototype.handleTx = function(info) {
var self =this;
var tx = this.sync.txDb.getStandardizedTx(info.message.tx);
console.log('[p2p_sync] Handle tx: ' + tx.txid);
tx.time = tx.time || Math.round(new Date().getTime() / 1000);
this.sync.storeTxs([tx], function(err) {
this.sync.storeTx(tx, function(err, relatedAddrs) {
if (err) {
console.log('[p2p_sync] Error in handle TX: ' + JSON.stringify(err));
}
else {
if (self.shouldBroadcast) {
sockets.broadcastTx(tx);
if (tx.relatedAddrs) {
for(var ii in tx.relatedAddrs){
sockets.broadcastAddressTx(tx.relatedAddrs[ii], tx.txid);
}
}
}
else if (self.shouldBroadcast) {
sockets.broadcastTx(tx);
self._broadcastAddr(tx.txid, relatedAddrs);
}
});
};
@ -85,7 +88,7 @@ PeerSync.prototype.handleBlock = function(info) {
'hash': blockHash,
'tx': tx_hashes,
'previousblockhash': bitcoreUtil.formatHashFull(block.prev_hash),
}, self.allowReorgs, function(err) {
}, self.allowReorgs, function(err, height) {
if (err && err.message.match(/NEED_SYNC/) && self.historicSync) {
console.log('[p2p_sync] Orphan block received. Triggering sync');
self.historicSync.start({}, function(){
@ -98,6 +101,8 @@ PeerSync.prototype.handleBlock = function(info) {
else {
if (self.shouldBroadcast) {
sockets.broadcastBlock(blockHash);
// broadcasting address here is a bad idea. listening to new block
// should be enoght
}
}
});

View File

@ -278,8 +278,8 @@ Sync.prototype.setBranchConnectedBackwards = function(fromHash, cb) {
//Store unconfirmed TXs
Sync.prototype.storeTxs = function(txs, cb) {
this.txDb.addMany(txs, cb);
Sync.prototype.storeTx = function(tx, cb) {
this.txDb.add(tx, cb);
};

View File

@ -558,8 +558,8 @@ TransactionDb.prototype.removeFromTxId = function(txid, cb) {
};
TransactionDb.prototype._addScript = function(tx) {
var relatedAddrs = [];
// relatedAddress is an optional hash, to collect related addresses in the transaction
TransactionDb.prototype._addScript = function(tx, relatedAddrs) {
var dbScript = [];
var ts = tx.time;
var txid = tx.txid || tx.hash;
@ -586,7 +586,7 @@ TransactionDb.prototype._addScript = function(tx) {
var addr = o.scriptPubKey.addresses[0];
var sat = o.valueSat || ((o.value||0) * util.COIN).toFixed(0);
relatedAddrs[addr]=1;
if (relatedAddrs) relatedAddrs[addr]=1;
var k = OUTS_PREFIX + txid + '-' + o.n;
dbScript.push({
type: 'put',
@ -599,14 +599,14 @@ TransactionDb.prototype._addScript = function(tx) {
});
}
}
tx.relatedAddrs=relatedAddrs;
return dbScript;
};
TransactionDb.prototype.add = function(tx, blockhash, cb) {
var dbScript = this._addScript(tx, blockhash);
db.batch(dbScript, cb);
// adds an unconfimed TX
TransactionDb.prototype.add = function(tx, cb) {
var relatedAddrs = {};
var dbScript = this._addScript(tx, relatedAddrs);
db.batch(dbScript, function(err) { return cb(err,relatedAddrs);});
};
TransactionDb.prototype._addManyFromObjs = function(txs, next) {
@ -633,7 +633,7 @@ TransactionDb.prototype._addManyFromHashes = function(txs, next) {
},
function(err) {
if (err) return next(err);
db.batch(dbScript,next);
db.batch(dbScript, next);
});
};
@ -641,7 +641,7 @@ TransactionDb.prototype._addManyFromHashes = function(txs, next) {
TransactionDb.prototype.addMany = function(txs, next) {
if (!txs) return next();
var fn = (typeof txs[0] ==='string') ?
var fn = (typeof txs[0] ==='string') ?
this._addManyFromHashes : this._addManyFromObjs;
return fn.apply(this,[txs, next]);