rename methods.

This commit is contained in:
Christopher Jeffrey 2016-02-19 21:40:50 -08:00
parent ad7cd04e79
commit 28274cdba4
5 changed files with 68 additions and 67 deletions

View File

@ -971,7 +971,7 @@ Chain.prototype.resetTimeAsync = function resetTimeAsync(ts, callback, force) {
}, true); }, true);
}; };
Chain.prototype._onFlush = function _onFlush(callback) { Chain.prototype.onFlush = function onFlush(callback) {
if (this.pending.length === 0) if (this.pending.length === 0)
return callback(); return callback();
@ -1248,8 +1248,8 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
self.emit('add block', block); self.emit('add block', block);
// Fullfill request // Fulfill request
self.request.fullfill(hash, block); self.request.fulfill(hash, block);
handleOrphans(); handleOrphans();
}); });

View File

@ -83,8 +83,10 @@ function Peer(pool, options) {
interval: this.options.pingInterval || 30000 interval: this.options.pingInterval || 30000
}; };
this._blockQueue = []; this.queue = {
this._txQueue = []; block: [],
tx: []
};
Peer.uid.iaddn(1); Peer.uid.iaddn(1);
@ -635,7 +637,7 @@ Peer.prototype.loadHeaders = function loadHeaders(hashes, stop) {
this._write(this.framer.getHeaders(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( utils.debug(
'Requesting inv packet from %s with getblocks', 'Requesting inv packet from %s with getblocks',
this.host); this.host);
@ -646,13 +648,7 @@ Peer.prototype.loadBlocks = function loadBlocks(hashes, stop) {
this._write(this.framer.getBlocks(hashes, stop)); this._write(this.framer.getBlocks(hashes, stop));
}; };
Peer.prototype.loadItems = function loadItems(hashes, stop) { Peer.prototype.getMempool = function getMempool() {
if (this.pool.options.headers)
return this.loadHeaders(hashes, stop);
return this.loadBlocks(hashes, stop);
};
Peer.prototype.loadMempool = function loadMempool() {
utils.debug( utils.debug(
'Requesting inv packet from %s with mempool', 'Requesting inv packet from %s with mempool',
this.host); this.host);

View File

@ -247,13 +247,13 @@ Pool.prototype._init = function _init() {
this.startServer(); this.startServer();
}; };
Pool.prototype.loadBlocks = function loadBlocks(peer, top, stop) { Pool.prototype.getBlocks = function getBlocks(peer, top, stop) {
var self = this; var self = this;
this.chain._onFlush(function() { this.chain.onFlush(function() {
self.chain.getLocatorAsync(top, function(err, locator) { self.chain.getLocatorAsync(top, function(err, locator) {
if (err) if (err)
throw 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) { Pool.prototype.loadOrphan = function loadOrphan(peer, top, orphan) {
var self = this; var self = this;
assert(orphan); assert(orphan);
this.chain._onFlush(function() { this.chain.onFlush(function() {
self.chain.getLocatorAsync(top, function(err, locator) { self.chain.getLocatorAsync(top, function(err, locator) {
if (err) if (err)
throw err; throw err;
peer.loadBlocks( peer.getBlocks(
locator, locator,
self.chain.getOrphanRoot(orphan) 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; var self = this;
this.chain._onFlush(function() { this.chain.onFlush(function() {
self.chain.getLocatorAsync(top, function(err, locator) { self.chain.getLocatorAsync(top, function(err, locator) {
if (err) if (err)
throw err; throw err;
peer.loadHeaders(locator, stop); peer.getHeaders(locator, stop);
}); });
}); });
}; };
@ -542,7 +542,7 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
break; break;
if (!this.chain.has(block)) 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; last = block;
} }
@ -555,7 +555,7 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
// simply tries to find the latest block in // simply tries to find the latest block in
// the peer's chain. // the peer's chain.
if (last && headers.length === 2000) if (last && headers.length === 2000)
this.loadHeaders(peer, last, null); this.getHeaders(peer, last, null);
// Reset interval to avoid calling getheaders unnecessarily // Reset interval to avoid calling getheaders unnecessarily
this._startInterval(); this._startInterval();
@ -582,7 +582,7 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) {
this.emit('blocks', hashes); this.emit('blocks', hashes);
this.chain._onFlush(function() { this.chain.onFlush(function() {
for (i = 0; i < hashes.length; i++) { for (i = 0; i < hashes.length; i++) {
hash = hashes[i]; hash = hashes[i];
@ -595,7 +595,7 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) {
// Request a block if we don't have it. // Request a block if we don't have it.
if (!self.chain.has(hash)) { if (!self.chain.has(hash)) {
self._request(peer, self.block.type, hash); self.getData(peer, self.block.type, hash);
continue; continue;
} }
@ -607,10 +607,10 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) {
// from the last hash. // from the last hash.
if (i === hashes.length - 1) { if (i === hashes.length - 1) {
// Request more hashes: // Request more hashes:
self.loadBlocks(peer, hash, null); self.getBlocks(peer, hash, null);
// Re-download the block (traditional method): // Re-download the block (traditional method):
// self._request(peer, self.block.type, hash); // self.getData(peer, self.block.type, hash, { force: true });
continue; continue;
} }
@ -635,9 +635,9 @@ Pool.prototype._handleInv = function _handleInv(hashes, peer) {
hash = utils.toHex(hashes[i]); hash = utils.toHex(hashes[i]);
if (!this.chain.has(hash)) { if (!this.chain.has(hash)) {
if (this.options.headers) if (this.options.headers)
this.loadHeaders(this.peers.load, null, hash); this.getHeaders(this.peers.load, null, hash);
else 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); callback = utils.asyncify(callback);
// Fulfill our request. // Fulfill our request.
requested = self._response(block); requested = self.fulfill(block);
// Someone is sending us blocks without // Someone is sending us blocks without
// us requesting them. // us requesting them.
@ -676,7 +676,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) {
if (err) if (err)
return callback(err); return callback(err);
self._startRequests(peer); self.scheduleRequests(peer);
if (added === 0) if (added === 0)
return callback(null, false); return callback(null, false);
@ -693,7 +693,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) {
self.chain.total, self.chain.total,
self.chain.orphan.count, self.chain.orphan.count,
self.request.activeBlocks, self.request.activeBlocks,
peer._blockQueue.length, peer.queue.block.length,
self.chain.getCurrentTarget(), self.chain.getCurrentTarget(),
self.peers.all.length, self.peers.all.length,
self.chain.pending.length, self.chain.pending.length,
@ -723,17 +723,17 @@ Pool.prototype._load = function _load() {
} }
if (this.options.headers) if (this.options.headers)
this.loadHeaders(this.peers.load, null, null); this.getHeaders(this.peers.load, null, null);
else 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) if (this.peers.load)
this.peers.load.loadMempool(); this.peers.load.getMempool();
this.peers.regular.forEach(function(peer) { this.peers.regular.forEach(function(peer) {
peer.loadMempool(); peer.getMempool();
}); });
}; };
@ -799,8 +799,8 @@ Pool.prototype._createPeer = function _createPeer(options) {
txs.forEach(function(hash) { txs.forEach(function(hash) {
hash = utils.toHex(hash); hash = utils.toHex(hash);
if (self._addTX(hash, 0)) if (self.markTX(hash, 0))
self._request(peer, 'tx', hash); self.getData(peer, 'tx', hash);
}); });
}); });
@ -822,8 +822,8 @@ Pool.prototype._handleTX = function _handleTX(tx, peer, callback) {
callback = utils.asyncify(callback); callback = utils.asyncify(callback);
requested = self._response(tx); requested = this.fulfill(tx);
added = self._addTX(tx, 1); added = this.markTX(tx, 1);
function addMempool(tx, peer, callback) { function addMempool(tx, peer, callback) {
if (!self.mempool) 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)) if (utils.isBuffer(hash))
hash = utils.toHex(hash); hash = utils.toHex(hash);
else if (hash.hash) else if (hash.hash)
@ -1459,7 +1459,7 @@ Pool.prototype.search = function search(id, range, e) {
return 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 self = this;
var item; var item;
@ -1483,53 +1483,58 @@ Pool.prototype._request = function _request(peer, type, hash, options, cb) {
return; return;
} }
if (!options.force && type !== 'tx') {
if (this.chain.has(hash))
return;
}
if (options.noQueue) if (options.noQueue)
return; return;
item = new LoadRequest(this, peer, type, hash, cb); item = new LoadRequest(this, peer, type, hash, cb);
if (type === 'tx') { if (type === 'tx') {
if (peer._txQueue.length === 0) { if (peer.queue.tx.length === 0) {
utils.nextTick(function() { utils.nextTick(function() {
utils.debug( utils.debug(
'Requesting %d/%d txs from %s with getdata', 'Requesting %d/%d txs from %s with getdata',
peer._txQueue.length, peer.queue.tx.length,
self.request.activeTX, self.request.activeTX,
peer.host); peer.host);
peer.getData(peer._txQueue); peer.getData(peer.queue.tx);
peer._txQueue.length = 0; peer.queue.tx.length = 0;
}); });
} }
peer._txQueue.push(item.start()); peer.queue.tx.push(item.start());
return; return;
} }
if (peer._blockQueue.length === 0) { if (peer.queue.block.length === 0) {
this.chain._onFlush(function() { this.chain.onFlush(function() {
utils.nextTick(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; var size, items;
if (this.chain.pending.length > 0) if (this.chain.pending.length > 0)
return; return;
if (peer._blockQueue.length === 0) if (peer.queue.block.length === 0)
return; return;
if (this.options.spv) { if (this.options.spv) {
items = peer._blockQueue.slice(); items = peer.queue.block.slice();
peer._blockQueue.length = 0; peer.queue.block.length = 0;
} else { } else {
// Blocks start getting big after 150k. // Blocks start getting big after 150k.
if (this.chain.height <= 100000) if (this.chain.height <= 100000)
@ -1541,8 +1546,8 @@ Pool.prototype._startRequests = function _startRequests(peer) {
else else
size = this.blockdb ? 1 : 10; size = this.blockdb ? 1 : 10;
items = peer._blockQueue.slice(0, size); items = peer.queue.block.slice(0, size);
peer._blockQueue = peer._blockQueue.slice(size); peer.queue.block = peer.queue.block.slice(size);
} }
items = items.map(function(item) { items = items.map(function(item) {
@ -1558,7 +1563,7 @@ Pool.prototype._startRequests = function _startRequests(peer) {
peer.getData(items); peer.getData(items);
}; };
Pool.prototype._response = function _response(hash) { Pool.prototype.fulfill = function fulfill(hash) {
var hash; var hash;
if (utils.isBuffer(hash)) if (utils.isBuffer(hash))
@ -1583,7 +1588,7 @@ Pool.prototype.getBlock = function getBlock(hash, cb) {
if (!this.peers.load) if (!this.peers.load)
return setTimeout(this.getBlock.bind(this, hash, cb), 1000); 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); 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 // Add request without queueing it to get notification at the time of load
tx = null; tx = null;
finished = false; 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; finished = true;
tx = t; tx = t;
}); });
@ -1950,13 +1955,13 @@ LoadRequest.prototype.finish = function finish() {
} }
if (this.type === 'tx') { if (this.type === 'tx') {
index = this.peer._txQueue.indexOf(this); index = this.peer.queue.tx.indexOf(this);
if (index !== -1) if (index !== -1)
this.peer._txQueue.splice(index, 1); this.peer.queue.tx.splice(index, 1);
} else { } else {
index = this.peer._blockQueue.indexOf(this); index = this.peer.queue.block.indexOf(this);
if (index !== -1) if (index !== -1)
this.peer._blockQueue.splice(index, 1); this.peer.queue.block.splice(index, 1);
} }
this.peer.removeListener('close', this._finish); this.peer.removeListener('close', this._finish);

View File

@ -151,7 +151,7 @@ TXPool.prototype.add = function add(tx, noWrite, strict) {
this._orphans[key] = [orphan]; this._orphans[key] = [orphan];
} }
// Add unspent outputs or fullfill orphans // Add unspent outputs or resolve orphans
for (i = 0; i < tx.outputs.length; i++) { for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i]; output = tx.outputs[i];

View File

@ -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; var cbs;
id = utils.toHex(id); id = utils.toHex(id);