From f714bbf2c0601c003288e7eaffd5e8bc8d474a55 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 18 May 2016 23:21:53 -0700 Subject: [PATCH] minor. remove sendBlock/sendTX. --- lib/bcoin/block.js | 7 ++--- lib/bcoin/fullnode.js | 17 ++--------- lib/bcoin/mtx.js | 1 - lib/bcoin/peer.js | 15 +++++++++- lib/bcoin/pool.js | 66 +++++++++---------------------------------- lib/bcoin/spvnode.js | 14 ++------- lib/bcoin/tx.js | 3 +- 7 files changed, 35 insertions(+), 88 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 2d0cffbe..75d61dde 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -46,7 +46,6 @@ function Block(data) { bcoin.abstractblock.call(this, data); - this.type = 'block'; this.txs = []; this._cbHeight = null; this._commitmentHash = null; @@ -563,9 +562,9 @@ Block.prototype.getPrevout = function getPrevout() { Block.prototype.inspect = function inspect() { return { - type: this.type, - height: this.height, + type: 'block', hash: utils.revHex(this.hash('hex')), + height: this.height, size: this.getSize(), virtualSize: this.getVirtualSize(), date: utils.date(this.ts), @@ -594,8 +593,8 @@ Block.prototype.inspect = function inspect() { Block.prototype.toJSON = function toJSON() { return { type: 'block', - height: this.height, hash: utils.revHex(this.hash('hex')), + height: this.height, version: this.version, prevBlock: utils.revHex(this.prevBlock), merkleRoot: utils.revHex(this.merkleRoot), diff --git a/lib/bcoin/fullnode.js b/lib/bcoin/fullnode.js index b203ffd7..d011e086 100644 --- a/lib/bcoin/fullnode.js +++ b/lib/bcoin/fullnode.js @@ -261,25 +261,12 @@ Fullnode.prototype.sendTX = function sendTX(item, wait, callback) { if (err) return callback(err); - if (!wait) { - self.pool.announce(item); - return callback(); - } + self.pool.announce(item); - self.pool.announce(item, callback); + return callback(); }); }; -/** - * Broadcast a block. - * @param {Block} item - * @param {Function} callback - */ - -Fullnode.prototype.sendBlock = function sendBlock(item, callback) { - return this.pool.sendBlock(item, callback); -}; - /** * Connect to the network. */ diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index dd1b644b..235db915 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -62,7 +62,6 @@ function MTX(options) { if (!options) options = {}; - this.type = 'tx'; this.version = options.version || 1; this.inputs = []; this.outputs = []; diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 3c129c1c..cb059b99 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -302,7 +302,20 @@ Peer.prototype.sendInv = function sendInv(items) { items = items.slice(0, 50000); if (this.filter) - items = items.map(this.isWatched, this); + items = items.filter(this.isWatched, this); + + items = items.map(function(msg) { + if (!(msg instanceof bcoin.tx) + && !(msg instanceof bcoin.block)) { + return msg; + } + return { + type: (msg instanceof bcoin.tx) + ? constants.inv.TX + : constants.inv.BLOCK, + hash: msg.hash() + }; + }); this.write(this.framer.inv(items)); }; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index e3927a49..b5dc2792 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -274,7 +274,7 @@ Pool.prototype._init = function _init() { this.chain.on('block', function(block, entry, peer) { // Emit merkle txs after the fact - if (block.type === 'merkleblock') { + if (self.options.spv) { utils.forEachSerial(block.txs, function(tx, next) { self._handleTX(tx, peer, next); }, function(err) { @@ -1639,32 +1639,6 @@ Pool.prototype.fulfill = function fulfill(hash) { return item; }; -/** - * Broadcast a block. - * @param {Block} block - * @param {Function} callback - Returns [Error]. Executes on request, reject, - * or timeout. - */ - -Pool.prototype.sendBlock = function sendBlock(block, callback) { - return this.broadcast(block, callback); -}; - -/** - * Broadcast a transaction. - * @param {TX} tx - * @param {Function} callback - Returns [Error]. Executes on request, reject, - * or timeout. - */ - -Pool.prototype.sendTX = function sendTX(tx, callback) { - // Failsafe to avoid getting banned by bitcoind nodes. - if (!tx.isSane()) - return utils.asyncify(callback)(new Error('CheckTransaction failed.')); - - return this.broadcast(tx, callback); -}; - /** * Broadcast a transaction or block. * @param {TX|Block} msg @@ -1685,8 +1659,7 @@ Pool.prototype.broadcast = function broadcast(msg, callback) { item = new BroadcastItem(this, msg, callback); - // return item.start(); - return item; + return item.start(); }; /** @@ -1697,16 +1670,7 @@ Pool.prototype.broadcast = function broadcast(msg, callback) { */ Pool.prototype.announce = function announce(msg) { - var i, msg; - - msg = { - type: (msg instanceof bcoin.tx) - ? constants.inv.TX - : constants.inv.BLOCK, - hash: msg.hash() - }; - - for (i = 0; i < this.peers.all.length; i++) + for (var i = 0; i < this.peers.all.length; i++) this.peers.all[i].sendInv(msg); }; @@ -1727,9 +1691,8 @@ Pool.prototype.destroy = function destroy(callback) { this.stopSync(); - this.inv.list.forEach(function(entry) { - clearTimeout(entry.timer); - entry.timer = null; + this.inv.list.slice().forEach(function(entry) { + entry.finish(); }); Object.keys(this.request.map).forEach(function(hash) { @@ -2154,7 +2117,7 @@ function BroadcastItem(pool, item, callback) { this.type = (item instanceof bcoin.tx) ? constants.inv.TX : constants.inv.BLOCK; - // this.msg = item; + this.msg = item; this.hash = item.hash(); this.normalValue = item.renderNormal(); this.witnessValue = item.render(); @@ -2164,8 +2127,6 @@ function BroadcastItem(pool, item, callback) { assert((this.type & constants.WITNESS_MASK) === 0); this.addCallback(callback); - - this.start(item); } utils.inherits(BroadcastItem, EventEmitter); @@ -2182,10 +2143,9 @@ BroadcastItem.prototype.addCallback = function addCallback(callback) { /** * Start the broadcast. - * @param {TX|Block} item */ -BroadcastItem.prototype.start = function start(item) { +BroadcastItem.prototype.start = function start() { var self = this; var i; @@ -2195,7 +2155,7 @@ BroadcastItem.prototype.start = function start(item) { this.pool.inv.map[this.key] = this; utils.binaryInsert(this.pool.inv.list, this, compare); - this.refresh(item); + this.refresh(); return this; }; @@ -2204,7 +2164,9 @@ BroadcastItem.prototype.start = function start(item) { * Refresh the timeout on the broadcast. */ -BroadcastItem.prototype.refresh = function refresh(item) { +BroadcastItem.prototype.refresh = function refresh() { + var self = this; + if (this.timeout) { clearTimeout(this.timeout); this.timeout = null; @@ -2215,10 +2177,8 @@ BroadcastItem.prototype.refresh = function refresh(item) { self.finish(new Error('Timed out.')); }, this.pool.inv.timeout); - for (i = 0; i < this.pool.peers.all.length; i++) { - if (this.pool.peers.all[i].isWatched(item)) - this.pool.peers.all[i].sendInv(this); - } + for (i = 0; i < this.pool.peers.all.length; i++) + this.pool.peers.all[i].sendInv(this); }; /** diff --git a/lib/bcoin/spvnode.js b/lib/bcoin/spvnode.js index 75ab8b04..03b09297 100644 --- a/lib/bcoin/spvnode.js +++ b/lib/bcoin/spvnode.js @@ -187,21 +187,11 @@ SPVNode.prototype.sendTX = function sendTX(item, wait, callback) { } if (!wait) { - this.pool.sendTX(item); + this.pool.broadcast(item); return utils.nextTick(callback); } - return this.pool.sendTX(item, callback); -}; - -/** - * Broadcast a block. - * @param {Block} item - * @param {Function} callback - */ - -SPVNode.prototype.sendBlock = function sendBlock(item, callback) { - return this.pool.sendBlock(item, callback); + return this.pool.broadcast(item, callback); }; /** diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 6990aa39..1b8fb2fa 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -60,7 +60,6 @@ function TX(data) { assert(Array.isArray(data.outputs)); assert(typeof data.locktime === 'number'); - this.type = 'tx'; this.version = data.version; this.flag = data.flag; this.inputs = []; @@ -1739,7 +1738,7 @@ TX.prototype.__defineGetter__('wtxid', function() { TX.prototype.inspect = function inspect() { return { - type: this.type, + type: 'tx', hash: utils.revHex(this.hash('hex')), witnessHash: utils.revHex(this.witnessHash('hex')), size: this.getSize(),