diff --git a/app/controllers/socket.js b/app/controllers/socket.js index 6ef1f8f4..a4977af5 100644 --- a/app/controllers/socket.js +++ b/app/controllers/socket.js @@ -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) { diff --git a/lib/BlockDb.js b/lib/BlockDb.js index 9f2d5d8f..633cf753 100644 --- a/lib/BlockDb.js +++ b/lib/BlockDb.js @@ -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); }); }; diff --git a/lib/HistoricSync.js b/lib/HistoricSync.js index 29a79f02..41251b6f 100644 --- a/lib/HistoricSync.js +++ b/lib/HistoricSync.js @@ -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); }); diff --git a/lib/PeerSync.js b/lib/PeerSync.js index 348a4b36..568ccd6c 100644 --- a/lib/PeerSync.js +++ b/lib/PeerSync.js @@ -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 } } }); diff --git a/lib/Sync.js b/lib/Sync.js index 7809f4f6..29ebed8a 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -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); }; diff --git a/lib/TransactionDb.js b/lib/TransactionDb.js index e7a203e5..117f78a8 100644 --- a/lib/TransactionDb.js +++ b/lib/TransactionDb.js @@ -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]);