diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 236539cf..ecb64348 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -971,7 +971,7 @@ Chain.prototype.resetTimeAsync = function resetTimeAsync(ts, callback, force) { }, true); }; -Chain.prototype._onFlush = function _onFlush(callback) { +Chain.prototype.onFlush = function onFlush(callback) { if (this.pending.length === 0) return callback(); @@ -1248,8 +1248,8 @@ Chain.prototype.add = function add(initial, peer, callback, force) { self.emit('add block', block); - // Fullfill request - self.request.fullfill(hash, block); + // Fulfill request + self.request.fulfill(hash, block); handleOrphans(); }); diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index eda816ee..c7f8ce69 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -83,8 +83,10 @@ function Peer(pool, options) { interval: this.options.pingInterval || 30000 }; - this._blockQueue = []; - this._txQueue = []; + this.queue = { + block: [], + tx: [] + }; Peer.uid.iaddn(1); @@ -635,7 +637,7 @@ Peer.prototype.loadHeaders = function loadHeaders(hashes, stop) { this._write(this.framer.getHeaders(hashes, stop)); }; -Peer.prototype.loadBlocks = function loadBlocks(hashes, stop) { +Peer.prototype.getBlocks = function getBlocks(hashes, stop) { utils.debug( 'Requesting inv packet from %s with getblocks', this.host); @@ -646,13 +648,7 @@ Peer.prototype.loadBlocks = function loadBlocks(hashes, stop) { this._write(this.framer.getBlocks(hashes, stop)); }; -Peer.prototype.loadItems = function loadItems(hashes, stop) { - if (this.pool.options.headers) - return this.loadHeaders(hashes, stop); - return this.loadBlocks(hashes, stop); -}; - -Peer.prototype.loadMempool = function loadMempool() { +Peer.prototype.getMempool = function getMempool() { utils.debug( 'Requesting inv packet from %s with mempool', this.host); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 25d6b8e7..4a6a5b75 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -247,13 +247,13 @@ Pool.prototype._init = function _init() { this.startServer(); }; -Pool.prototype.loadBlocks = function loadBlocks(peer, top, stop) { +Pool.prototype.getBlocks = function getBlocks(peer, top, stop) { var self = this; - this.chain._onFlush(function() { + this.chain.onFlush(function() { self.chain.getLocatorAsync(top, function(err, locator) { if (err) throw err; - peer.loadBlocks(locator, stop); + peer.getBlocks(locator, stop); }); }); }; @@ -261,11 +261,11 @@ Pool.prototype.loadBlocks = function loadBlocks(peer, top, stop) { Pool.prototype.loadOrphan = function loadOrphan(peer, top, orphan) { var self = this; assert(orphan); - this.chain._onFlush(function() { + this.chain.onFlush(function() { self.chain.getLocatorAsync(top, function(err, locator) { if (err) throw err; - peer.loadBlocks( + peer.getBlocks( locator, self.chain.getOrphanRoot(orphan) ); @@ -273,13 +273,13 @@ Pool.prototype.loadOrphan = function loadOrphan(peer, top, orphan) { }); }; -Pool.prototype.loadHeaders = function loadHeaders(peer, top, stop) { +Pool.prototype.getHeaders = function getHeaders(peer, top, stop) { var self = this; - this.chain._onFlush(function() { + this.chain.onFlush(function() { self.chain.getLocatorAsync(top, function(err, locator) { if (err) throw err; - peer.loadHeaders(locator, stop); + peer.getHeaders(locator, stop); }); }); }; @@ -542,7 +542,7 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) { break; if (!this.chain.has(block)) - this._request(blockPeer, this.block.type, block.hash('hex')); + this.getData(blockPeer, this.block.type, block.hash('hex')); last = block; } @@ -555,7 +555,7 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) { // simply tries to find the latest block in // the peer's chain. if (last && headers.length === 2000) - this.loadHeaders(peer, last, null); + this.getHeaders(peer, last, null); // Reset interval to avoid calling getheaders unnecessarily this._startInterval(); @@ -582,7 +582,7 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) { this.emit('blocks', hashes); - this.chain._onFlush(function() { + this.chain.onFlush(function() { for (i = 0; i < hashes.length; i++) { hash = hashes[i]; @@ -595,7 +595,7 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) { // Request a block if we don't have it. if (!self.chain.has(hash)) { - self._request(peer, self.block.type, hash); + self.getData(peer, self.block.type, hash); continue; } @@ -607,10 +607,10 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) { // from the last hash. if (i === hashes.length - 1) { // Request more hashes: - self.loadBlocks(peer, hash, null); + self.getBlocks(peer, hash, null); // Re-download the block (traditional method): - // self._request(peer, self.block.type, hash); + // self.getData(peer, self.block.type, hash, { force: true }); continue; } @@ -635,9 +635,9 @@ Pool.prototype._handleInv = function _handleInv(hashes, peer) { hash = utils.toHex(hashes[i]); if (!this.chain.has(hash)) { if (this.options.headers) - this.loadHeaders(this.peers.load, null, hash); + this.getHeaders(this.peers.load, null, hash); else - this._request(peer, this.block.type, hash); + this.getData(peer, this.block.type, hash); } } }; @@ -657,7 +657,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) { callback = utils.asyncify(callback); // Fulfill our request. - requested = self._response(block); + requested = self.fulfill(block); // Someone is sending us blocks without // us requesting them. @@ -676,7 +676,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) { if (err) return callback(err); - self._startRequests(peer); + self.scheduleRequests(peer); if (added === 0) return callback(null, false); @@ -693,7 +693,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) { self.chain.total, self.chain.orphan.count, self.request.activeBlocks, - peer._blockQueue.length, + peer.queue.block.length, self.chain.getCurrentTarget(), self.peers.all.length, self.chain.pending.length, @@ -723,17 +723,17 @@ Pool.prototype._load = function _load() { } if (this.options.headers) - this.loadHeaders(this.peers.load, null, null); + this.getHeaders(this.peers.load, null, null); else - this.loadBlocks(this.peers.load, null, null); + this.getBlocks(this.peers.load, null, null); }; -Pool.prototype.loadMempool = function loadMempool() { +Pool.prototype.getMempool = function getMempool() { if (this.peers.load) - this.peers.load.loadMempool(); + this.peers.load.getMempool(); this.peers.regular.forEach(function(peer) { - peer.loadMempool(); + peer.getMempool(); }); }; @@ -799,8 +799,8 @@ Pool.prototype._createPeer = function _createPeer(options) { txs.forEach(function(hash) { hash = utils.toHex(hash); - if (self._addTX(hash, 0)) - self._request(peer, 'tx', hash); + if (self.markTX(hash, 0)) + self.getData(peer, 'tx', hash); }); }); @@ -822,8 +822,8 @@ Pool.prototype._handleTX = function _handleTX(tx, peer, callback) { callback = utils.asyncify(callback); - requested = self._response(tx); - added = self._addTX(tx, 1); + requested = this.fulfill(tx); + added = this.markTX(tx, 1); function addMempool(tx, peer, callback) { if (!self.mempool) @@ -992,7 +992,7 @@ Pool.prototype._addPeer = function _addPeer() { }); }; -Pool.prototype._addTX = function(hash, state) { +Pool.prototype.markTX = function(hash, state) { if (utils.isBuffer(hash)) hash = utils.toHex(hash); else if (hash.hash) @@ -1459,7 +1459,7 @@ Pool.prototype.search = function search(id, range, e) { return e; }; -Pool.prototype._request = function _request(peer, type, hash, options, cb) { +Pool.prototype.getData = function getData(peer, type, hash, options, cb) { var self = this; var item; @@ -1483,53 +1483,58 @@ Pool.prototype._request = function _request(peer, type, hash, options, cb) { return; } + if (!options.force && type !== 'tx') { + if (this.chain.has(hash)) + return; + } + if (options.noQueue) return; item = new LoadRequest(this, peer, type, hash, cb); if (type === 'tx') { - if (peer._txQueue.length === 0) { + if (peer.queue.tx.length === 0) { utils.nextTick(function() { utils.debug( 'Requesting %d/%d txs from %s with getdata', - peer._txQueue.length, + peer.queue.tx.length, self.request.activeTX, peer.host); - peer.getData(peer._txQueue); - peer._txQueue.length = 0; + peer.getData(peer.queue.tx); + peer.queue.tx.length = 0; }); } - peer._txQueue.push(item.start()); + peer.queue.tx.push(item.start()); return; } - if (peer._blockQueue.length === 0) { - this.chain._onFlush(function() { + if (peer.queue.block.length === 0) { + this.chain.onFlush(function() { utils.nextTick(function() { - self._startRequests(peer); + self.scheduleRequests(peer); }); }); } - peer._blockQueue.push(item); + peer.queue.block.push(item); }; -Pool.prototype._startRequests = function _startRequests(peer) { +Pool.prototype.scheduleRequests = function scheduleRequests(peer) { var size, items; if (this.chain.pending.length > 0) return; - if (peer._blockQueue.length === 0) + if (peer.queue.block.length === 0) return; if (this.options.spv) { - items = peer._blockQueue.slice(); - peer._blockQueue.length = 0; + items = peer.queue.block.slice(); + peer.queue.block.length = 0; } else { // Blocks start getting big after 150k. if (this.chain.height <= 100000) @@ -1541,8 +1546,8 @@ Pool.prototype._startRequests = function _startRequests(peer) { else size = this.blockdb ? 1 : 10; - items = peer._blockQueue.slice(0, size); - peer._blockQueue = peer._blockQueue.slice(size); + items = peer.queue.block.slice(0, size); + peer.queue.block = peer.queue.block.slice(size); } items = items.map(function(item) { @@ -1558,7 +1563,7 @@ Pool.prototype._startRequests = function _startRequests(peer) { peer.getData(items); }; -Pool.prototype._response = function _response(hash) { +Pool.prototype.fulfill = function fulfill(hash) { var hash; if (utils.isBuffer(hash)) @@ -1583,7 +1588,7 @@ Pool.prototype.getBlock = function getBlock(hash, cb) { if (!this.peers.load) return setTimeout(this.getBlock.bind(this, hash, cb), 1000); - this._request(this.peers.load, 'block', hash, { force: true }, function(block) { + this.getData(this.peers.load, 'block', hash, { force: true }, function(block) { cb(null, block); }); }; @@ -1619,7 +1624,7 @@ Pool.prototype.getTX = function getTX(hash, range, cb) { // Add request without queueing it to get notification at the time of load tx = null; finished = false; - req = this._request(this.peers.load, 'tx', hash, { noQueue: true }, function(t) { + req = this.getData(this.peers.load, 'tx', hash, { noQueue: true }, function(t) { finished = true; tx = t; }); @@ -1950,13 +1955,13 @@ LoadRequest.prototype.finish = function finish() { } if (this.type === 'tx') { - index = this.peer._txQueue.indexOf(this); + index = this.peer.queue.tx.indexOf(this); if (index !== -1) - this.peer._txQueue.splice(index, 1); + this.peer.queue.tx.splice(index, 1); } else { - index = this.peer._blockQueue.indexOf(this); + index = this.peer.queue.block.indexOf(this); if (index !== -1) - this.peer._blockQueue.splice(index, 1); + this.peer.queue.block.splice(index, 1); } this.peer.removeListener('close', this._finish); diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index 9470cf08..7a8ff4c2 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -151,7 +151,7 @@ TXPool.prototype.add = function add(tx, noWrite, strict) { this._orphans[key] = [orphan]; } - // Add unspent outputs or fullfill orphans + // Add unspent outputs or resolve orphans for (i = 0; i < tx.outputs.length; i++) { output = tx.outputs[i]; diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 4b2f7a16..2598ec44 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -398,7 +398,7 @@ RequestCache.prototype.add = function add(id, cb) { } }; -RequestCache.prototype.fullfill = function fullfill(id, err, data) { +RequestCache.prototype.fulfill = function fulfill(id, err, data) { var cbs; id = utils.toHex(id);