more pool work.
This commit is contained in:
parent
65125f7b4f
commit
cef88c6e4e
@ -102,7 +102,7 @@ Chain.prototype._init = function _init() {
|
|||||||
|
|
||||||
// Hook into events for debugging
|
// Hook into events for debugging
|
||||||
this.on('block', function(block, entry) {
|
this.on('block', function(block, entry) {
|
||||||
if (self.height < self.network.block.slowHeight)
|
if (!self.synced && self.height < self.network.block.slowHeight)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
bcoin.debug('Block %s (%d) added to chain.',
|
bcoin.debug('Block %s (%d) added to chain.',
|
||||||
|
|||||||
@ -61,6 +61,8 @@ var constants = bcoin.protocol.constants;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function Peer(pool, options) {
|
function Peer(pool, options) {
|
||||||
|
var seed;
|
||||||
|
|
||||||
if (!(this instanceof Peer))
|
if (!(this instanceof Peer))
|
||||||
return new Peer(pool, options);
|
return new Peer(pool, options);
|
||||||
|
|
||||||
@ -80,8 +82,8 @@ function Peer(pool, options) {
|
|||||||
this.mempool = this.pool.mempool;
|
this.mempool = this.pool.mempool;
|
||||||
this.bloom = this.pool.bloom;
|
this.bloom = this.pool.bloom;
|
||||||
this.network = this.chain.network;
|
this.network = this.chain.network;
|
||||||
this.parser = new bcoin.protocol.parser(this.chain.options);
|
this.parser = new bcoin.protocol.parser({ network: this.network });
|
||||||
this.framer = new bcoin.protocol.framer(this.chain.options);
|
this.framer = new bcoin.protocol.framer({ network: this.network });
|
||||||
this.version = null;
|
this.version = null;
|
||||||
this.destroyed = false;
|
this.destroyed = false;
|
||||||
this.ack = false;
|
this.ack = false;
|
||||||
@ -104,14 +106,18 @@ function Peer(pool, options) {
|
|||||||
this.socket = options.socket;
|
this.socket = options.socket;
|
||||||
this.host = this.socket.remoteAddress;
|
this.host = this.socket.remoteAddress;
|
||||||
this.port = this.socket.remotePort;
|
this.port = this.socket.remotePort;
|
||||||
assert(this.host);
|
|
||||||
assert(this.port != null);
|
|
||||||
} else if (options.seed) {
|
} else if (options.seed) {
|
||||||
options.seed = utils.parseHost(options.seed);
|
seed = utils.parseHost(options.seed);
|
||||||
options.seed.port = options.seed.port || this.network.port;
|
this.host = seed.host;
|
||||||
this.socket = this.createSocket(options.seed.port, options.seed.host);
|
this.port = seed.port || this.network.port;
|
||||||
|
this.socket = this.createSocket(this.port, this.host);
|
||||||
|
} else {
|
||||||
|
assert(false, 'No seed or socket.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert(typeof this.host === 'string');
|
||||||
|
assert(typeof this.port === 'number');
|
||||||
|
|
||||||
if (!this.socket)
|
if (!this.socket)
|
||||||
throw new Error('No socket');
|
throw new Error('No socket');
|
||||||
|
|
||||||
@ -146,19 +152,9 @@ Peer.uid = 0;
|
|||||||
Peer.prototype._init = function init() {
|
Peer.prototype._init = function init() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
if (!this.host)
|
|
||||||
this.host = this.socket.remoteAddress || this.socket._host || null;
|
|
||||||
|
|
||||||
if (!this.port)
|
|
||||||
this.port = this.socket.remotePort || 0;
|
|
||||||
|
|
||||||
this.socket.once('connect', function() {
|
this.socket.once('connect', function() {
|
||||||
self.ts = utils.now();
|
self.ts = utils.now();
|
||||||
self.connected = true;
|
self.connected = true;
|
||||||
if (!self.host)
|
|
||||||
self.host = self.socket.remoteAddress;
|
|
||||||
if (!self.port)
|
|
||||||
self.port = self.socket.remotePort;
|
|
||||||
self.emit('connect');
|
self.emit('connect');
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -181,6 +177,7 @@ Peer.prototype._init = function init() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.parser.on('error', function(err) {
|
this.parser.on('error', function(err) {
|
||||||
|
bcoin.debug('Parse error:');
|
||||||
bcoin.debug(err.stack + '');
|
bcoin.debug(err.stack + '');
|
||||||
self.sendReject(null, 'malformed', 'error parsing message', 100);
|
self.sendReject(null, 'malformed', 'error parsing message', 100);
|
||||||
self._error(err);
|
self._error(err);
|
||||||
@ -249,12 +246,6 @@ Peer.prototype.createSocket = function createSocket(port, host) {
|
|||||||
var self = this;
|
var self = this;
|
||||||
var socket, net;
|
var socket, net;
|
||||||
|
|
||||||
assert(port != null);
|
|
||||||
assert(host);
|
|
||||||
|
|
||||||
this.host = host;
|
|
||||||
this.port = port;
|
|
||||||
|
|
||||||
if (this._createSocket) {
|
if (this._createSocket) {
|
||||||
socket = this._createSocket(port, host);
|
socket = this._createSocket(port, host);
|
||||||
} else if (bcoin.isBrowser) {
|
} else if (bcoin.isBrowser) {
|
||||||
@ -264,13 +255,16 @@ Peer.prototype.createSocket = function createSocket(port, host) {
|
|||||||
socket = net.connect(port, host);
|
socket = net.connect(port, host);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (utils.isIP(host) === 6)
|
||||||
|
host = '[' + host + ']';
|
||||||
|
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Connecting to %s:%d (priority=%s)',
|
'Connecting to %s:%d (priority=%s).',
|
||||||
host, port, this.priority);
|
host, port, this.priority);
|
||||||
|
|
||||||
socket.on('connect', function() {
|
socket.on('connect', function() {
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Connected to %s:%d (priority=%s)',
|
'Connected to %s:%d (priority=%s).',
|
||||||
host, port, self.priority);
|
host, port, self.priority);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -411,7 +405,7 @@ Peer.prototype._error = function error(err) {
|
|||||||
if (typeof err === 'string')
|
if (typeof err === 'string')
|
||||||
err = new Error(err);
|
err = new Error(err);
|
||||||
|
|
||||||
err.message += ' (' + this.host + ':' + this.port + ')';
|
err.message += ' (' + this.hostname + ')';
|
||||||
|
|
||||||
this.destroy();
|
this.destroy();
|
||||||
this.emit('error', err);
|
this.emit('error', err);
|
||||||
@ -583,7 +577,7 @@ Peer.prototype._onPacket = function onPacket(packet) {
|
|||||||
this.fire(cmd, payload);
|
this.fire(cmd, payload);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
bcoin.debug('Unknown packet: %s', cmd);
|
bcoin.debug('Unknown packet: %s.', cmd);
|
||||||
this.fire(cmd, payload);
|
this.fire(cmd, payload);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -642,7 +636,8 @@ Peer.prototype._handleUTXOs = function _handleUTXOs(payload) {
|
|||||||
payload.coins = payload.coins(function(coin) {
|
payload.coins = payload.coins(function(coin) {
|
||||||
return new bcoin.coin(coin);
|
return new bcoin.coin(coin);
|
||||||
});
|
});
|
||||||
bcoin.debug('Received %d utxos from %s.', payload.coins.length, this.host);
|
bcoin.debug('Received %d utxos (%s).',
|
||||||
|
payload.coins.length, this.hostname);
|
||||||
this.fire('utxos', payload);
|
this.fire('utxos', payload);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1006,7 +1001,7 @@ Peer.prototype._handleMempool = function _handleMempool() {
|
|||||||
for (i = 0; i < hashes.length; i++)
|
for (i = 0; i < hashes.length; i++)
|
||||||
items.push({ type: constants.inv.TX, hash: hashes[i] });
|
items.push({ type: constants.inv.TX, hash: hashes[i] });
|
||||||
|
|
||||||
bcoin.debug('Sending mempool snapshot to %s.', self.host);
|
bcoin.debug('Sending mempool snapshot (%s).', self.hostname);
|
||||||
|
|
||||||
self.sendInv(items);
|
self.sendInv(items);
|
||||||
});
|
});
|
||||||
@ -1033,17 +1028,17 @@ Peer.prototype._handleGetData = function handleGetData(items) {
|
|||||||
|
|
||||||
if ((item.type & ~constants.WITNESS_MASK) !== entry.type) {
|
if ((item.type & ~constants.WITNESS_MASK) !== entry.type) {
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Peer %s requested an existing item with the wrong type.',
|
'Peer requested an existing item with the wrong type (%s).',
|
||||||
this.host);
|
this.hostname);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Peer %s requested %s:%s as a %s packet.',
|
'Peer requested %s:%s as a %s packet (%s).',
|
||||||
this.host,
|
|
||||||
entry.packetType,
|
entry.packetType,
|
||||||
utils.revHex(entry.key),
|
utils.revHex(entry.hash),
|
||||||
witness ? 'witness' : 'normal');
|
witness ? 'witness' : 'normal',
|
||||||
|
this.hostname);
|
||||||
|
|
||||||
entry.sendTo(peer, witness);
|
entry.sendTo(peer, witness);
|
||||||
}
|
}
|
||||||
@ -1182,10 +1177,10 @@ Peer.prototype._handleGetData = function handleGetData(items) {
|
|||||||
self.emit('error', err);
|
self.emit('error', err);
|
||||||
|
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Served %d items to %s with getdata (notfound=%d).',
|
'Served %d items s with getdata (notfound=%d) (%s).',
|
||||||
items.length - notfound.length,
|
items.length - notfound.length,
|
||||||
self.host,
|
notfound.length,
|
||||||
notfound.length);
|
self.hostname);
|
||||||
|
|
||||||
if (notfound.length > 0)
|
if (notfound.length > 0)
|
||||||
self.write(self.framer.notFound(notfound));
|
self.write(self.framer.notFound(notfound));
|
||||||
@ -1217,10 +1212,11 @@ Peer.prototype._handleAddr = function handleAddr(addrs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Recieved %d peers (seeds=%d, peers=%d).',
|
'Received %d addrs (seeds=%d, peers=%d) (%s).',
|
||||||
addrs.length,
|
addrs.length,
|
||||||
this.pool.seeds.length,
|
this.pool.seeds.length,
|
||||||
this.pool.peers.all.length);
|
this.pool.peers.all.length,
|
||||||
|
this.hostname);
|
||||||
};
|
};
|
||||||
|
|
||||||
Peer.prototype._handlePing = function handlePing(data) {
|
Peer.prototype._handlePing = function handlePing(data) {
|
||||||
@ -1250,7 +1246,7 @@ Peer.prototype._handleGetAddr = function handleGetAddr() {
|
|||||||
|
|
||||||
for (i = 0; i < this.pool.seeds.length; i++) {
|
for (i = 0; i < this.pool.seeds.length; i++) {
|
||||||
seed = utils.parseHost(this.pool.seeds[i]);
|
seed = utils.parseHost(this.pool.seeds[i]);
|
||||||
seed = this.pool.getPeer(seed) || seed;
|
seed = this.pool.getPeer(seed.host) || seed;
|
||||||
version = utils.isIP(seed.host);
|
version = utils.isIP(seed.host);
|
||||||
|
|
||||||
if (!version)
|
if (!version)
|
||||||
@ -1274,6 +1270,11 @@ Peer.prototype._handleGetAddr = function handleGetAddr() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bcoin.debug(
|
||||||
|
'Sending %d addrs to peer (%s)',
|
||||||
|
addrs.length,
|
||||||
|
this.hostname);
|
||||||
|
|
||||||
return this.write(this.framer.addr(items));
|
return this.write(this.framer.addr(items));
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1329,7 +1330,7 @@ Peer.prototype._handleAlert = function handleAlert(details) {
|
|||||||
var signature = details.signature;
|
var signature = details.signature;
|
||||||
|
|
||||||
if (!bcoin.ec.verify(hash, signature, this.network.alertKey)) {
|
if (!bcoin.ec.verify(hash, signature, this.network.alertKey)) {
|
||||||
bcoin.debug('Peer %s sent a phony alert packet.', this.host);
|
bcoin.debug('Peer sent a phony alert packet (%s).', this.hostname);
|
||||||
// Let's look at it because why not?
|
// Let's look at it because why not?
|
||||||
bcoin.debug(details);
|
bcoin.debug(details);
|
||||||
this.setMisbehavior(100);
|
this.setMisbehavior(100);
|
||||||
@ -1348,8 +1349,8 @@ Peer.prototype._handleAlert = function handleAlert(details) {
|
|||||||
|
|
||||||
Peer.prototype.getHeaders = function getHeaders(locator, stop) {
|
Peer.prototype.getHeaders = function getHeaders(locator, stop) {
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Requesting headers packet from %s with getheaders',
|
'Requesting headers packet from peer with getheaders (%s).',
|
||||||
this.host);
|
this.hostname);
|
||||||
|
|
||||||
bcoin.debug('Height: %s, Hash: %s, Stop: %s',
|
bcoin.debug('Height: %s, Hash: %s, Stop: %s',
|
||||||
locator && locator.length ? this.chain._getCachedHeight(locator[0]) : -1,
|
locator && locator.length ? this.chain._getCachedHeight(locator[0]) : -1,
|
||||||
@ -1367,8 +1368,8 @@ Peer.prototype.getHeaders = function getHeaders(locator, stop) {
|
|||||||
|
|
||||||
Peer.prototype.getBlocks = function getBlocks(locator, stop) {
|
Peer.prototype.getBlocks = function getBlocks(locator, stop) {
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Requesting inv packet from %s with getblocks',
|
'Requesting inv packet from peer with getblocks (%s).',
|
||||||
this.host);
|
this.hostname);
|
||||||
|
|
||||||
bcoin.debug('Height: %s, Hash: %s, Stop: %s',
|
bcoin.debug('Height: %s, Hash: %s, Stop: %s',
|
||||||
locator && locator.length ? this.chain._getCachedHeight(locator[0]) : null,
|
locator && locator.length ? this.chain._getCachedHeight(locator[0]) : null,
|
||||||
@ -1384,8 +1385,8 @@ Peer.prototype.getBlocks = function getBlocks(locator, stop) {
|
|||||||
|
|
||||||
Peer.prototype.getMempool = function getMempool() {
|
Peer.prototype.getMempool = function getMempool() {
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Requesting inv packet from %s with mempool',
|
'Requesting inv packet from peer with mempool (%s).',
|
||||||
this.host);
|
this.hostname);
|
||||||
|
|
||||||
this.write(this.framer.mempool());
|
this.write(this.framer.mempool());
|
||||||
};
|
};
|
||||||
@ -1397,8 +1398,8 @@ Peer.prototype.getMempool = function getMempool() {
|
|||||||
|
|
||||||
Peer.prototype.reject = function reject(details) {
|
Peer.prototype.reject = function reject(details) {
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Sending reject packet to %s',
|
'Sending reject packet to peer (%s).',
|
||||||
this.host);
|
this.hostname);
|
||||||
|
|
||||||
this.write(this.framer.reject(details));
|
this.write(this.framer.reject(details));
|
||||||
};
|
};
|
||||||
@ -1434,6 +1435,28 @@ Peer.prototype.sendReject = function sendReject(obj, code, reason, score) {
|
|||||||
return this.pool.reject(this, obj, code, reason, score);
|
return this.pool.reject(this, obj, code, reason, score);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Peer.prototype.__defineGetter__('hostname', function() {
|
||||||
|
var host = this.host;
|
||||||
|
|
||||||
|
if (utils.isIP(host) === 6)
|
||||||
|
host = '[' + host + ']';
|
||||||
|
|
||||||
|
return host + ':' + this.port;
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inspect the peer.
|
||||||
|
* @returns {String}
|
||||||
|
*/
|
||||||
|
|
||||||
|
Peer.prototype.inspect = function inspect() {
|
||||||
|
return '<Peer:'
|
||||||
|
+ ' id=' + this.id
|
||||||
|
+ ' connected=' + this.connected
|
||||||
|
+ ' host=' + this.hostname
|
||||||
|
+ '>';
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helpers
|
* Helpers
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -253,7 +253,7 @@ Pool.prototype.connect = function connect() {
|
|||||||
if (this.originalSeeds.length > 0) {
|
if (this.originalSeeds.length > 0) {
|
||||||
this._addLoader();
|
this._addLoader();
|
||||||
|
|
||||||
for (i = 0; i < this.size; i++)
|
for (i = 0; i < this.size - 1; i++)
|
||||||
this._addPeer();
|
this._addPeer();
|
||||||
|
|
||||||
this.connected = true;
|
this.connected = true;
|
||||||
@ -413,7 +413,7 @@ Pool.prototype.listen = function listen(callback) {
|
|||||||
this.server.on('listening', function() {
|
this.server.on('listening', function() {
|
||||||
var data = self.server.address();
|
var data = self.server.address();
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Bitcoin server listening on %s (port=%d)',
|
'Bitcoin server listening on %s (port=%d).',
|
||||||
data.address, data.port);
|
data.address, data.port);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -484,9 +484,7 @@ Pool.prototype._startInterval = function _startInterval() {
|
|||||||
if (self.chain.isBusy())
|
if (self.chain.isBusy())
|
||||||
return self._startTimer();
|
return self._startTimer();
|
||||||
|
|
||||||
bcoin.debug('Stall recovery: loading again.');
|
bcoin.debug('Warning: Stalling.');
|
||||||
|
|
||||||
// self._load();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this._interval = setInterval(load, this.load.interval);
|
this._interval = setInterval(load, this.load.interval);
|
||||||
@ -507,7 +505,7 @@ Pool.prototype._addLoader = function _addLoader() {
|
|||||||
if (this.destroyed)
|
if (this.destroyed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (this.peers.load != null)
|
if (this.peers.load)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
peer = this._createPeer({
|
peer = this._createPeer({
|
||||||
@ -518,7 +516,7 @@ Pool.prototype._addLoader = function _addLoader() {
|
|||||||
witness: this.options.witness
|
witness: this.options.witness
|
||||||
});
|
});
|
||||||
|
|
||||||
bcoin.debug('Added loader peer: %s', peer.host);
|
bcoin.debug('Added loader peer (%s).', peer.hostname);
|
||||||
|
|
||||||
this.peers.load = peer;
|
this.peers.load = peer;
|
||||||
this.peers.all.push(peer);
|
this.peers.all.push(peer);
|
||||||
@ -528,8 +526,16 @@ Pool.prototype._addLoader = function _addLoader() {
|
|||||||
self._stopInterval();
|
self._stopInterval();
|
||||||
self._stopTimer();
|
self._stopTimer();
|
||||||
self._removePeer(peer);
|
self._removePeer(peer);
|
||||||
if (self.destroyed)
|
if (self.peers.regular.length === 0) {
|
||||||
|
bcoin.debug('%s %s %s',
|
||||||
|
'Could not connect to any peers.',
|
||||||
|
'Do you have a network connection?',
|
||||||
|
'Retrying in 5 seconds.');
|
||||||
|
setTimeout(function() {
|
||||||
|
self._addLoader();
|
||||||
|
}, 5000);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
self._addLoader();
|
self._addLoader();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -660,9 +666,9 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer, callback)
|
|||||||
return callback();
|
return callback();
|
||||||
|
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Recieved %s headers from %s',
|
'Received %s headers from peer (%s).',
|
||||||
headers.length,
|
headers.length,
|
||||||
peer.host);
|
peer.hostname);
|
||||||
|
|
||||||
if (headers.length > 2000) {
|
if (headers.length > 2000) {
|
||||||
peer.setMisbehavior(100);
|
peer.setMisbehavior(100);
|
||||||
@ -718,9 +724,9 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer, callback) {
|
|||||||
return callback();
|
return callback();
|
||||||
|
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Recieved %s block hashes from %s',
|
'Received %s block hashes from peer (%s).',
|
||||||
hashes.length,
|
hashes.length,
|
||||||
peer.host);
|
peer.hostname);
|
||||||
|
|
||||||
// Normally this is 500, but with older
|
// Normally this is 500, but with older
|
||||||
// versions locator.GetDistanceBack() is called.
|
// versions locator.GetDistanceBack() is called.
|
||||||
@ -812,8 +818,8 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) {
|
|||||||
// us requesting them.
|
// us requesting them.
|
||||||
if (!requested) {
|
if (!requested) {
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Recieved unrequested block: %s (%s)',
|
'Received unrequested block: %s (%s).',
|
||||||
block.rhash, peer.host);
|
block.rhash, peer.hostname);
|
||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -916,7 +922,7 @@ Pool.prototype._createPeer = function _createPeer(options) {
|
|||||||
|
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Reject (%s): msg=%s ccode=%s reason=%s data=%s',
|
'Reject (%s): msg=%s ccode=%s reason=%s data=%s',
|
||||||
peer.host,
|
peer.hostname,
|
||||||
payload.message,
|
payload.message,
|
||||||
payload.ccode,
|
payload.ccode,
|
||||||
payload.reason,
|
payload.reason,
|
||||||
@ -926,7 +932,7 @@ Pool.prototype._createPeer = function _createPeer(options) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
peer.on('alert', function(payload) {
|
peer.on('alert', function(payload) {
|
||||||
bcoin.debug('Received alert from: %s', peer.host);
|
bcoin.debug('Received alert from peer (%s).', peer.hostname);
|
||||||
bcoin.debug(payload);
|
bcoin.debug(payload);
|
||||||
self.emit('alert', payload, peer);
|
self.emit('alert', payload, peer);
|
||||||
});
|
});
|
||||||
@ -1001,8 +1007,8 @@ Pool.prototype._createPeer = function _createPeer(options) {
|
|||||||
self.block.versionHeight = version.height;
|
self.block.versionHeight = version.height;
|
||||||
|
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Received version from %s: version=%d height=%d agent=%s',
|
'Received version (%s): version=%d height=%d agent=%s',
|
||||||
peer.host, version.version, version.height, version.agent);
|
peer.hostname, version.version, version.height, version.agent);
|
||||||
|
|
||||||
bcoin.time.add(peer.host, version.ts);
|
bcoin.time.add(peer.host, version.ts);
|
||||||
|
|
||||||
@ -1065,7 +1071,7 @@ Pool.prototype._addLeech = function _addLeech(socket) {
|
|||||||
witness: false
|
witness: false
|
||||||
});
|
});
|
||||||
|
|
||||||
bcoin.debug('Added leech peer: %s', peer.host);
|
bcoin.debug('Added leech peer (%s).', peer.hostname);
|
||||||
|
|
||||||
this.peers.leeches.push(peer);
|
this.peers.leeches.push(peer);
|
||||||
this.peers.all.push(peer);
|
this.peers.all.push(peer);
|
||||||
@ -1117,7 +1123,7 @@ Pool.prototype._addPeer = function _addPeer() {
|
|||||||
if (this.destroyed)
|
if (this.destroyed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (this.peers.regular.length + this.peers.pending.length >= this.size)
|
if (this.peers.regular.length + this.peers.pending.length >= this.size - 1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
seed = this.getSeed(false);
|
seed = this.getSeed(false);
|
||||||
@ -1215,10 +1221,10 @@ Pool.prototype.bestPeer = function bestPeer() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Pool.prototype._removePeer = function _removePeer(peer) {
|
Pool.prototype._removePeer = function _removePeer(peer) {
|
||||||
utils.binaryRemove(this.peers.pending, peer);
|
utils.binaryRemove(this.peers.pending, peer, compare);
|
||||||
utils.binaryRemove(this.peers.regular, peer);
|
utils.binaryRemove(this.peers.regular, peer, compare);
|
||||||
utils.binaryRemove(this.peers.leeches, peer);
|
utils.binaryRemove(this.peers.leeches, peer, compare);
|
||||||
utils.binaryRemove(this.peers.all, peer);
|
utils.binaryRemove(this.peers.all, peer, compare);
|
||||||
delete this.peers.map[peer.host];
|
delete this.peers.map[peer.host];
|
||||||
|
|
||||||
if (this.peers.load === peer) {
|
if (this.peers.load === peer) {
|
||||||
@ -1227,7 +1233,7 @@ Pool.prototype._removePeer = function _removePeer(peer) {
|
|||||||
if (item.peer === peer)
|
if (item.peer === peer)
|
||||||
item.finish();
|
item.finish();
|
||||||
}, this);
|
}, this);
|
||||||
bcoin.debug('Removed loader peer (%s).', peer.host);
|
bcoin.debug('Removed loader peer (%s).', peer.hostname);
|
||||||
this.peers.load = null;
|
this.peers.load = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -1424,13 +1430,13 @@ Pool.prototype.searchWallet = function(wallet, callback) {
|
|||||||
|
|
||||||
self.chain.reset(height, function(err) {
|
self.chain.reset(height, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
bcoin.debug('Failed to reset height: %s', err.stack + '');
|
bcoin.debug('Failed to reset height: %s', err.message);
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
bcoin.debug('Wallet height: %s', height);
|
bcoin.debug('Wallet height: %s.', height);
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Reverted chain to height=%d (%s)',
|
'Reverted chain to height=%d (%s).',
|
||||||
self.chain.height,
|
self.chain.height,
|
||||||
utils.date(self.chain.tip.ts)
|
utils.date(self.chain.tip.ts)
|
||||||
);
|
);
|
||||||
@ -1446,13 +1452,13 @@ Pool.prototype.searchWallet = function(wallet, callback) {
|
|||||||
|
|
||||||
self.chain.resetTime(ts, function(err) {
|
self.chain.resetTime(ts, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
bcoin.debug('Failed to reset time: %s', err.stack + '');
|
bcoin.debug('Failed to reset time: %s', err.message);
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
bcoin.debug('Wallet time: %s', utils.date(ts));
|
bcoin.debug('Wallet time is %s.', utils.date(ts));
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Reverted chain to height=%d (%s)',
|
'Reverted chain to height=%d (%s).',
|
||||||
self.chain.height,
|
self.chain.height,
|
||||||
utils.date(self.chain.tip.ts)
|
utils.date(self.chain.tip.ts)
|
||||||
);
|
);
|
||||||
@ -1511,10 +1517,10 @@ Pool.prototype.getData = function getData(peer, type, hash, options, callback) {
|
|||||||
if (peer.queue.tx.length === 0) {
|
if (peer.queue.tx.length === 0) {
|
||||||
utils.nextTick(function() {
|
utils.nextTick(function() {
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Requesting %d/%d txs from %s with getdata',
|
'Requesting %d/%d txs from peer with getdata (%s).',
|
||||||
peer.queue.tx.length,
|
peer.queue.tx.length,
|
||||||
self.request.activeTX,
|
self.request.activeTX,
|
||||||
peer.host);
|
peer.hostname);
|
||||||
|
|
||||||
peer.getData(peer.queue.tx);
|
peer.getData(peer.queue.tx);
|
||||||
peer.queue.tx.length = 0;
|
peer.queue.tx.length = 0;
|
||||||
@ -1559,9 +1565,12 @@ Pool.prototype.scheduleRequests = function scheduleRequests(peer) {
|
|||||||
|
|
||||||
this._scheduled = true;
|
this._scheduled = true;
|
||||||
|
|
||||||
|
utils.print(this.request);
|
||||||
|
|
||||||
this.chain.onFlush(function() {
|
this.chain.onFlush(function() {
|
||||||
utils.nextTick(function() {
|
utils.nextTick(function() {
|
||||||
self._sendRequests(peer);
|
self._sendRequests(peer);
|
||||||
|
utils.print(self.request);
|
||||||
self._scheduled = false;
|
self._scheduled = false;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -1599,10 +1608,10 @@ Pool.prototype._sendRequests = function _sendRequests(peer) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Requesting %d/%d blocks from %s with getdata',
|
'Requesting %d/%d blocks from peer with getdata (%s).',
|
||||||
items.length,
|
items.length,
|
||||||
this.request.activeBlocks,
|
this.request.activeBlocks,
|
||||||
peer.host);
|
peer.hostname);
|
||||||
|
|
||||||
peer.getData(items);
|
peer.getData(items);
|
||||||
};
|
};
|
||||||
@ -1661,7 +1670,12 @@ Pool.prototype.broadcast = function broadcast(msg, callback) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Pool.prototype.announce = function announce(msg) {
|
Pool.prototype.announce = function announce(msg) {
|
||||||
for (var i = 0; i < this.peers.all.length; i++)
|
var i;
|
||||||
|
|
||||||
|
if (this.peers.load)
|
||||||
|
this.peers.load.sendInv(msg);
|
||||||
|
|
||||||
|
for (i = 0; i < this.peers.regular.length; i++)
|
||||||
this.peers.all[i].sendInv(msg);
|
this.peers.all[i].sendInv(msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1907,7 +1921,7 @@ Pool.prototype.setMisbehavior = function setMisbehavior(peer, score) {
|
|||||||
|
|
||||||
if (peer.banScore >= constants.BAN_SCORE) {
|
if (peer.banScore >= constants.BAN_SCORE) {
|
||||||
this.peers.misbehaving[peer.host] = utils.now();
|
this.peers.misbehaving[peer.host] = utils.now();
|
||||||
bcoin.debug('Ban threshold exceeded for %s', peer.host);
|
bcoin.debug('Ban threshold exceeded (%s).', peer.host);
|
||||||
peer.destroy();
|
peer.destroy();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -1959,8 +1973,8 @@ Pool.prototype.reject = function reject(peer, obj, code, reason, score) {
|
|||||||
if (obj) {
|
if (obj) {
|
||||||
type = (obj instanceof bcoin.tx) ? 'tx' : 'block';
|
type = (obj instanceof bcoin.tx) ? 'tx' : 'block';
|
||||||
|
|
||||||
bcoin.debug('Rejecting %s %s from %s: ccode=%s reason=%s',
|
bcoin.debug('Rejecting %s %s (%s): ccode=%s reason=%s.',
|
||||||
type, obj.rhash, peer.host, code, reason);
|
type, obj.rhash, peer.hostname, code, reason);
|
||||||
|
|
||||||
peer.reject({
|
peer.reject({
|
||||||
message: type,
|
message: type,
|
||||||
@ -1969,8 +1983,8 @@ Pool.prototype.reject = function reject(peer, obj, code, reason, score) {
|
|||||||
data: obj.hash()
|
data: obj.hash()
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
bcoin.debug('Rejecting packet from %s: ccode=%s reason=%s',
|
bcoin.debug('Rejecting packet from %s: ccode=%s reason=%s.',
|
||||||
peer.host, code, reason);
|
peer.hostname, code, reason);
|
||||||
|
|
||||||
peer.reject({
|
peer.reject({
|
||||||
ccode: code,
|
ccode: code,
|
||||||
@ -2067,6 +2081,20 @@ LoadRequest.prototype.finish = function finish() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inspect the load request.
|
||||||
|
* @returns {String}
|
||||||
|
*/
|
||||||
|
|
||||||
|
LoadRequest.prototype.inspect = function inspect() {
|
||||||
|
return '<LoadRequest:'
|
||||||
|
+ ' id=' + this.id
|
||||||
|
+ ' type=' + (this.type === this.pool.tx.type ? 'tx' : 'block')
|
||||||
|
+ ' active=' + this.active
|
||||||
|
+ ' hash=' + utils.revHex(this.hash)
|
||||||
|
+ '>';
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an item that is broadcasted via an inv/getdata cycle.
|
* Represents an item that is broadcasted via an inv/getdata cycle.
|
||||||
* @exports BroadcastItem
|
* @exports BroadcastItem
|
||||||
@ -2093,12 +2121,11 @@ function BroadcastItem(pool, item, callback) {
|
|||||||
this.callback = [];
|
this.callback = [];
|
||||||
|
|
||||||
this.id = this.pool.uid++;
|
this.id = this.pool.uid++;
|
||||||
this.key = item.hash('hex');
|
this.hash = item.hash('hex');
|
||||||
this.type = (item instanceof bcoin.tx)
|
this.type = (item instanceof bcoin.tx)
|
||||||
? constants.inv.TX
|
? constants.inv.TX
|
||||||
: constants.inv.BLOCK;
|
: constants.inv.BLOCK;
|
||||||
this.msg = item;
|
this.msg = item;
|
||||||
this.hash = item.hash();
|
|
||||||
this.normalValue = item.renderNormal();
|
this.normalValue = item.renderNormal();
|
||||||
this.witnessValue = item.render();
|
this.witnessValue = item.render();
|
||||||
|
|
||||||
@ -2130,9 +2157,9 @@ BroadcastItem.prototype.start = function start() {
|
|||||||
var i;
|
var i;
|
||||||
|
|
||||||
assert(!this.timeout, 'Already started.');
|
assert(!this.timeout, 'Already started.');
|
||||||
assert(!this.pool.inv.map[this.key], 'Already started.');
|
assert(!this.pool.inv.map[this.hash], 'Already started.');
|
||||||
|
|
||||||
this.pool.inv.map[this.key] = this;
|
this.pool.inv.map[this.hash] = this;
|
||||||
utils.binaryInsert(this.pool.inv.list, this, compare);
|
utils.binaryInsert(this.pool.inv.list, this, compare);
|
||||||
|
|
||||||
this.refresh();
|
this.refresh();
|
||||||
@ -2170,12 +2197,12 @@ BroadcastItem.prototype.finish = function finish(err) {
|
|||||||
var i;
|
var i;
|
||||||
|
|
||||||
assert(this.timeout, 'Already finished.');
|
assert(this.timeout, 'Already finished.');
|
||||||
assert(this.pool.inv.map[this.key], 'Already finished.');
|
assert(this.pool.inv.map[this.hash], 'Already finished.');
|
||||||
|
|
||||||
clearInterval(this.timeout);
|
clearInterval(this.timeout);
|
||||||
this.timeout = null;
|
this.timeout = null;
|
||||||
|
|
||||||
delete this.pool.inv.map[this.key];
|
delete this.pool.inv.map[this.hash];
|
||||||
utils.binaryRemove(this.pool.inv.list, this, compare);
|
utils.binaryRemove(this.pool.inv.list, this, compare);
|
||||||
|
|
||||||
for (i = 0; i < this.callback.length; i++)
|
for (i = 0; i < this.callback.length; i++)
|
||||||
@ -2218,7 +2245,7 @@ BroadcastItem.prototype.reject = function reject(peer) {
|
|||||||
|
|
||||||
this.emit('reject', peer);
|
this.emit('reject', peer);
|
||||||
|
|
||||||
err = new Error('Rejected by ' + peer.host);
|
err = new Error('Rejected by ' + peer.hostname);
|
||||||
|
|
||||||
for (i = 0; i < this.callback.length; i++)
|
for (i = 0; i < this.callback.length; i++)
|
||||||
this.callback[i](err);
|
this.callback[i](err);
|
||||||
@ -2226,6 +2253,18 @@ BroadcastItem.prototype.reject = function reject(peer) {
|
|||||||
this.callback.length = 0;
|
this.callback.length = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inspect the broadcast item.
|
||||||
|
*/
|
||||||
|
|
||||||
|
BroadcastItem.prototype.inspect = function inspect() {
|
||||||
|
return '<BroadcastItem:'
|
||||||
|
+ ' id=' + this.id
|
||||||
|
+ ' type=' + (this.type === constants.TX ? 'tx' : 'block')
|
||||||
|
+ ' hash=' + utils.revHex(this.hash)
|
||||||
|
+ '>';
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helpers
|
* Helpers
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -31,7 +31,7 @@ function Framer(options) {
|
|||||||
this.options = options;
|
this.options = options;
|
||||||
this.network = bcoin.network.get(options.network);
|
this.network = bcoin.network.get(options.network);
|
||||||
|
|
||||||
this.agent = new Buffer(options.USER_AGENT || constants.USER_AGENT, 'ascii');
|
this.agent = options.agent || constants.USER_AGENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -454,9 +454,6 @@ Framer.version = function version(options, writer) {
|
|||||||
var local = options.local || {};
|
var local = options.local || {};
|
||||||
var nonce = options.nonce;
|
var nonce = options.nonce;
|
||||||
|
|
||||||
if (typeof agent === 'string')
|
|
||||||
agent = new Buffer(agent, 'ascii');
|
|
||||||
|
|
||||||
if (local.network == null)
|
if (local.network == null)
|
||||||
local.network = options.network;
|
local.network = options.network;
|
||||||
|
|
||||||
@ -475,7 +472,7 @@ Framer.version = function version(options, writer) {
|
|||||||
Framer.address(remote, false, p);
|
Framer.address(remote, false, p);
|
||||||
Framer.address(local, false, p);
|
Framer.address(local, false, p);
|
||||||
p.writeU64(nonce);
|
p.writeU64(nonce);
|
||||||
p.writeVarString(agent);
|
p.writeVarString(agent, 'ascii');
|
||||||
p.write32(options.height || 0);
|
p.write32(options.height || 0);
|
||||||
p.writeU8(options.relay ? 1 : 0);
|
p.writeU8(options.relay ? 1 : 0);
|
||||||
|
|
||||||
|
|||||||
@ -805,7 +805,8 @@ segnet3.block = {
|
|||||||
bip34height: -1,
|
bip34height: -1,
|
||||||
bip34hash: null,
|
bip34hash: null,
|
||||||
pruneAfterHeight: 1000,
|
pruneAfterHeight: 1000,
|
||||||
maxTipAge: 0x7fffffff,
|
// maxTipAge: 0x7fffffff,
|
||||||
|
maxTipAge: 24 * 60 * 60,
|
||||||
slowHeight: 0x7fffffff
|
slowHeight: 0x7fffffff
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -925,7 +926,8 @@ segnet4.block = {
|
|||||||
bip34height: -1,
|
bip34height: -1,
|
||||||
bip34hash: null,
|
bip34hash: null,
|
||||||
pruneAfterHeight: 1000,
|
pruneAfterHeight: 1000,
|
||||||
maxTipAge: 0x7fffffff,
|
// maxTipAge: 0x7fffffff,
|
||||||
|
maxTipAge: 24 * 60 * 60,
|
||||||
slowHeight: 0x7fffffff
|
slowHeight: 0x7fffffff
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user