Skip to content

Commit

Permalink
net: more improvements for p2p health.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Mar 4, 2020
1 parent 42515fd commit 2cfda9b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 87 deletions.
1 change: 0 additions & 1 deletion etc/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ persistent-mempool: false
# Pool
#

selfish: false
compact: true
bip37: false
listen: true
Expand Down
2 changes: 1 addition & 1 deletion lib/net/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const pkg = require('../pkg');
* @default
*/

exports.PROTOCOL_VERSION = 2;
exports.PROTOCOL_VERSION = 3;

/**
* Minimum protocol version we're willing to talk to.
Expand Down
47 changes: 37 additions & 10 deletions lib/net/hostlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,18 @@ class HostList {
for (const node of nodes) {
const addr = NetAddress.fromHostname(node, this.network);

if (this.map.has(addr.hostname))
continue;

if (!addr.isRoutable())
continue;

if (!this.options.onion && addr.isOnion())
continue;

if (this.options.brontideOnly && !addr.hasKey())
continue;

if (addr.port === 0)
continue;

Expand Down Expand Up @@ -934,13 +940,24 @@ class HostList {
let bestScore = -1;
let bestDest = null;

if (!src)
src = this.address;
if (!src) {
for (const dest of this.local.values()) {
if (dest.score > bestScore) {
bestScore = dest.score;
bestDest = dest.addr;
}
}

if (this.local.size === 0)
return null;
return bestDest;
}

for (const dest of this.local.values()) {
if (this.network.type === 'main') {
// Disable everything else for now.
if (dest.type < HostList.scores.UPNP)
continue;
}

const reach = src.getReachability(dest.addr);

if (reach < bestReach)
Expand All @@ -953,7 +970,8 @@ class HostList {
}
}

bestDest.time = this.network.now();
if (bestDest)
bestDest.time = this.network.now();

return bestDest;
}
Expand Down Expand Up @@ -1127,7 +1145,7 @@ class HostList {

assert(json.magic === this.network.magic, 'Magic mismatch.');

if (json.version === 1) {
if (json.version < 3) {
// Migrate to v2.
for (const item of json.addrs) {
const entry = HostEntry.fromJSON(item, this.network);
Expand All @@ -1142,6 +1160,8 @@ class HostList {
this.markAck(addr.hostname, 0);
}

this.injectSeeds();

return this;
}

Expand Down Expand Up @@ -1294,7 +1314,7 @@ HostList.MAX_REFS = 8;
* @default
*/

HostList.VERSION = 2;
HostList.VERSION = 3;

/**
* Local address scores.
Expand All @@ -1306,10 +1326,10 @@ HostList.scores = {
NONE: 0,
IF: 1,
BIND: 2,
UPNP: 3,
DNS: 3,
MANUAL: 4,
MAX: 5
UPNP: 4,
MANUAL: 5,
MAX: 6
};

/**
Expand Down Expand Up @@ -1509,6 +1529,7 @@ class LocalAddress {

constructor(addr, score) {
this.addr = addr;
this.type = score || 0;
this.score = score || 0;
}
}
Expand All @@ -1533,6 +1554,7 @@ class HostListOptions {
this.port = this.network.port;
this.services = common.LOCAL_SERVICES;
this.onion = false;
this.brontideOnly = false;
this.banTime = common.BAN_TIME;

this.address = new NetAddress();
Expand Down Expand Up @@ -1639,6 +1661,11 @@ class HostListOptions {
this.onion = options.onion;
}

if (options.brontideOnly != null) {
assert(typeof options.brontideOnly === 'boolean');
this.brontideOnly = options.brontideOnly;
}

if (options.maxBuckets != null) {
assert(typeof options.maxBuckets === 'number');
this.maxBuckets = options.maxBuckets;
Expand Down
3 changes: 3 additions & 0 deletions lib/net/netaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ class NetAddress extends bio.Struct {
*/

getKey(enc) {
if (!this.hasKey())
return null;

if (enc === 'base32')
return base32.encode(this.key);

Expand Down
82 changes: 28 additions & 54 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class Pool extends EventEmitter {
});
}

if (!this.options.selfish && !this.options.spv) {
if (!this.options.spv) {
if (this.mempool) {
this.mempool.on('tx', (tx) => {
try {
Expand Down Expand Up @@ -540,9 +540,6 @@ class Pool extends EventEmitter {
return false;
}

if (this.hosts.addLocal(host, src, scores.UPNP))
this.logger.info('External IP found (upnp): %s.', host);

this.logger.debug(
'Adding port mapping %d->%d.',
src, dest);
Expand All @@ -555,6 +552,9 @@ class Pool extends EventEmitter {
return false;
}

if (this.hosts.addLocal(host, src, scores.UPNP))
this.logger.info('External IP found (upnp): %s.', host);

return true;
}

Expand Down Expand Up @@ -1106,9 +1106,6 @@ class Pool extends EventEmitter {
if (entry)
return entry;

if (this.options.selfish)
return null;

if (item.isTX()) {
if (!this.mempool)
return null;
Expand Down Expand Up @@ -1154,8 +1151,7 @@ class Pool extends EventEmitter {
return true;
}

if (this.options.selfish
|| this.chain.options.spv
if (this.chain.options.spv
|| this.chain.options.prune) {
return false;
}
Expand Down Expand Up @@ -1415,16 +1411,15 @@ class Pool extends EventEmitter {

async handleOpen(peer) {
// Advertise our address.
if (peer.outbound) {
if (!this.options.selfish && this.options.listen) {
const addr = this.hosts.getLocal(peer.address);
if (addr)
peer.send(new packets.AddrPacket([addr]));
}
if (peer.outbound && this.options.listen) {
const addr = this.hosts.getLocal(peer.address);

if (addr)
peer.send(new packets.AddrPacket([addr]));
}

// Find some more peers.
if (peer.version >= 2 && !this.hosts.isFull())
if (peer.version >= 3 && !this.hosts.isFull())
peer.sendGetAddr();

// We want compact blocks!
Expand Down Expand Up @@ -1586,9 +1581,6 @@ class Pool extends EventEmitter {
*/

async handleGetAddr(peer, packet) {
if (this.options.selfish)
return;

if (peer.sentAddr) {
this.logger.debug(
'Ignoring repeated getaddr (%s).',
Expand Down Expand Up @@ -2134,9 +2126,6 @@ class Pool extends EventEmitter {
if (!this.chain.synced)
return;

if (this.options.selfish)
return;

if (this.chain.options.spv)
return;

Expand Down Expand Up @@ -2179,9 +2168,6 @@ class Pool extends EventEmitter {
if (!this.chain.synced)
return;

if (this.options.selfish)
return;

if (this.chain.options.spv)
return;

Expand Down Expand Up @@ -2854,9 +2840,6 @@ class Pool extends EventEmitter {
if (!this.chain.synced)
return;

if (this.options.selfish)
return;

if (!this.options.bip37) {
this.logger.debug(
'Peer requested mempool without bip37 enabled (%s).',
Expand Down Expand Up @@ -3156,9 +3139,6 @@ class Pool extends EventEmitter {
if (this.chain.options.prune)
return;

if (this.options.selfish)
return;

const item = new InvItem(invTypes.BLOCK, req.hash);

const block = await this.getItem(peer, item);
Expand Down Expand Up @@ -3382,6 +3362,9 @@ class Pool extends EventEmitter {
if (!this.options.onion && addr.isOnion())
continue;

if (this.options.brontideOnly && !addr.hasKey())
continue;

if (i < 30 && now - entry.lastAttempt < 600)
continue;

Expand Down Expand Up @@ -4317,8 +4300,8 @@ class PoolOptions {
this.resolve = this._resolve.bind(this);
this.proxy = null;
this.onion = false;
this.brontideOnly = false;
this.upnp = false;
this.selfish = false;
this.version = common.PROTOCOL_VERSION;
this.agent = common.USER_AGENT;
this.identityKey = secp256k1.privateKeyGenerate();
Expand Down Expand Up @@ -4410,13 +4393,8 @@ class PoolOptions {
this.noRelay = options.noRelay;
}

if (options.host != null) {
assert(typeof options.host === 'string');
const raw = IP.toBuffer(options.host);
this.host = IP.toString(raw);
if (IP.isRoutable(raw))
this.publicHost = this.host;
}
if (options.host != null)
this.host = IP.normalize(options.host);

if (options.port != null) {
assert((options.port & 0xffff) === options.port);
Expand All @@ -4430,8 +4408,12 @@ class PoolOptions {
}

if (options.publicHost != null) {
assert(typeof options.publicHost === 'string');
this.publicHost = IP.normalize(options.publicHost);
const raw = IP.toBuffer(options.publicHost);

if (!IP.isRoutable(raw))
throw new Error('Invalid public host.');

this.publicHost = IP.toString(raw);
}

if (options.publicPort != null) {
Expand Down Expand Up @@ -4475,16 +4457,16 @@ class PoolOptions {
this.onion = options.onion;
}

if (options.brontideOnly != null) {
assert(typeof options.brontideOnly === 'boolean');
this.brontideOnly = options.brontideOnly;
}

if (options.upnp != null) {
assert(typeof options.upnp === 'boolean');
this.upnp = options.upnp;
}

if (options.selfish) {
assert(typeof options.selfish === 'boolean');
this.selfish = options.selfish;
}

if (options.version) {
assert(typeof options.version === 'number');
this.version = options.version;
Expand Down Expand Up @@ -4567,11 +4549,6 @@ class PoolOptions {
this.listen = false;
}

if (this.selfish) {
this.services &= ~common.services.NETWORK;
this.bip37 = false;
}

if (this.bip37)
this.services |= common.services.BLOOM;

Expand All @@ -4588,9 +4565,6 @@ class PoolOptions {
this.requiredServices = options.requiredServices;
}

if (this.listen && this.publicHost === '0.0.0.0')
throw new Error('Must pass --public-host=[ip] with --listen!');

return this;
}

Expand Down
9 changes: 5 additions & 4 deletions lib/net/seeds/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ module.exports = [
'ajdzrpoxsusaw4ixq4ttibxxsuh5fkkduc5qszyboidif2z25i362@173.255.209.126:44806',
'akimcha5bck7s344dmge6k3agtxd2txi6x4qzg3mo26spvf5bjol2@74.207.247.120:44806',
'ap5vuwabzwyz6akhesanada4skhetd2jsvpkwuqxzuaoovn5ez4xg@45.79.134.225:44806',
'ampst5rxtg6razxizrqasvopkqrtvqs4qyllohvzhkrd2y2mnokju@138.68.61.31:44806',
'apmv724pysb6ifiqsqzrixg23dybnwehinvmzdv6v6n4h7w2da5rq@45.55.111.156:44806',
'anhaoy6kcptjt25qeas3zgmsrt5twwjlvqqa4pwnt6elsceazsez2@174.138.111.111:44806',
'ajd6iidvt7p22eomfkzxtslgsrp2wllef3j4xqemvbknjfquppdmk@44.229.138.206:44806',
'aonetsezqp4m52w4jpfq2gv3dggy2wqfwqtkfjyttgdidbvhgp5as@165.22.151.242:44806',
'172.104.214.189:12038',
'173.255.209.126:12038',
'74.207.247.120:12038',
'45.79.134.225:12038',
'165.22.151.242:12038',
'3.17.33.134:12038',
'3.18.31.3:12038',
'165.22.151.242:12038',
Expand Down
Loading

0 comments on commit 2cfda9b

Please sign in to comment.