pool.listen.

This commit is contained in:
Christopher Jeffrey 2016-05-19 11:56:11 -07:00
parent b83a4a6a2c
commit ed1a8ef718
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 35 additions and 24 deletions

View File

@ -26,12 +26,17 @@ node.open(function(err) {
if (err) if (err)
throw err; throw err;
if (node.options.mine) { node.listen(function(err) {
if (bcoin.network.get().type !== 'regtest') if (err)
node.pool.connect(); throw err;
node.miner.start();
return;
}
node.startSync(); if (node.options.mine) {
if (bcoin.network.get().type !== 'regtest')
node.pool.connect();
node.miner.start();
return;
}
node.startSync();
});
}); });

View File

@ -21,7 +21,6 @@ var assert = utils.assert;
* @param {Boolean?} options.requireStandard * @param {Boolean?} options.requireStandard
* @param {Boolean?} options.rejectInsaneFees * @param {Boolean?} options.rejectInsaneFees
* @param {Boolean?} options.replaceByFee * @param {Boolean?} options.replaceByFee
* @param {Boolean?} options.listen
* @param {Boolean?} options.selfish * @param {Boolean?} options.selfish
* @param {Base58Address?} options.payoutAddress * @param {Base58Address?} options.payoutAddress
* @param {String?} options.coinbaseFlags * @param {String?} options.coinbaseFlags
@ -86,7 +85,6 @@ Fullnode.prototype._init = function _init() {
chain: this.chain, chain: this.chain,
mempool: this.mempool, mempool: this.mempool,
witness: this.network.witness, witness: this.network.witness,
listen: this.options.listen,
selfish: this.options.selfish, selfish: this.options.selfish,
broadcast: this.options.broadcast, broadcast: this.options.broadcast,
spv: false spv: false
@ -267,6 +265,15 @@ Fullnode.prototype.sendTX = function sendTX(item, wait, callback) {
}); });
}; };
/**
* Listen on a server socket on
* the p2p network (accepts leech peers).
*/
Fullnode.prototype.listen = function listen(callback) {
return this.pool.listen(callback);
};
/** /**
* Connect to the network. * Connect to the network.
*/ */

View File

@ -112,6 +112,7 @@ function Pool(options) {
this.destroyed = false; this.destroyed = false;
this.loaded = false; this.loaded = false;
this.size = options.size || 8; this.size = options.size || 8;
this.maxLeeches = options.maxLeeches || 8;
this.connected = false; this.connected = false;
this.uid = 0; this.uid = 0;
@ -264,8 +265,6 @@ Pool.prototype.connect = function connect() {
this.connected = true; this.connected = true;
} }
this.startServer();
}; };
Pool.prototype._init = function _init() { Pool.prototype._init = function _init() {
@ -395,7 +394,7 @@ Pool.prototype.getHeaders = function getHeaders(peer, top, stop, callback) {
* @param {Function} callback * @param {Function} callback
*/ */
Pool.prototype.startServer = function startServer(callback) { Pool.prototype.listen = function listen(callback) {
var self = this; var self = this;
var net; var net;
@ -406,14 +405,15 @@ Pool.prototype.startServer = function startServer(callback) {
net = require('n' + 'et'); net = require('n' + 'et');
if (!this.options.listen) assert(!this.server, 'Server already listening.');
return utils.nextTick(callback);
assert(!this.server);
this.server = new net.Server(); this.server = new net.Server();
this.server.on('connection', function(socket) { this.server.on('connection', function(socket) {
if (self.peers.leeches.length >= self.maxLeeches) {
socket.destroy();
return;
}
self._addLeech(socket); self._addLeech(socket);
}); });
@ -432,7 +432,7 @@ Pool.prototype.startServer = function startServer(callback) {
* @param {Function} callback * @param {Function} callback
*/ */
Pool.prototype.stopServer = function stopServer(callback) { Pool.prototype.unlisten = function unlisten(callback) {
callback = utils.ensure(callback); callback = utils.ensure(callback);
if (bcoin.isBrowser) if (bcoin.isBrowser)
@ -442,7 +442,7 @@ Pool.prototype.stopServer = function stopServer(callback) {
return utils.nextTick(callback); return utils.nextTick(callback);
this.server.close(callback); this.server.close(callback);
delete this.server; this.server = null;
}; };
Pool.prototype._startTimer = function _startTimer() { Pool.prototype._startTimer = function _startTimer() {
@ -525,8 +525,6 @@ Pool.prototype._addLoader = function _addLoader() {
witness: this.options.witness witness: this.options.witness
}); });
assert(peer);
bcoin.debug('Added loader peer: %s', peer.host); bcoin.debug('Added loader peer: %s', peer.host);
this.peers.load = peer; this.peers.load = peer;
@ -1074,7 +1072,7 @@ Pool.prototype._addLeech = function _addLeech(socket) {
witness: false witness: false
}); });
assert(peer); bcoin.debug('Added leech peer: %s', peer.host);
this.peers.leeches.push(peer); this.peers.leeches.push(peer);
this.peers.all.push(peer); this.peers.all.push(peer);
@ -1714,7 +1712,7 @@ Pool.prototype.destroy = function destroy(callback) {
peer.destroy(); peer.destroy();
}); });
this.stopServer(callback); this.unlisten(callback);
}; };
/** /**

View File

@ -1882,8 +1882,9 @@ Script.num = function num(value, flags, size) {
* account negative zero, minimaldata, etc. * account negative zero, minimaldata, etc.
* @example * @example
* assert.deepEqual(Script.array(0), new Buffer([])); * assert.deepEqual(Script.array(0), new Buffer([]));
* assert.deepEqual(Script.array(0xffee), new Buffer([0xee, 0xff])); * assert.deepEqual(Script.array(0xffee), new Buffer([0xee, 0xff, 0x00]));
* assert.deepEqual(Script.array(new bn(0xffee)), new Buffer([0xee, 0xff])); * assert.deepEqual(Script.array(new bn(0xffee)), new Buffer([0xee, 0xff, 0x00]));
* assert.deepEqual(Script.array(new bn(0x1e).ineg()), new Buffer([0x9e]));
* @param {Buffer|Number|BN} value * @param {Buffer|Number|BN} value
* @returns {Buffer} * @returns {Buffer}
*/ */