Skip to content

Commit

Permalink
net-pool: Check outbound peers for same group before connecting
Browse files Browse the repository at this point in the history
This commit checks the outbound peers to see which group their network
addresses lives in before connecting. By doing this, we prevent a node
from connecting to all outbound addresses in a very close network group
bunch. This helps prevent someone spinning up multiple nodes on a
similar network e.g. AWS, GCP and using those to fill other nodes'
outbounds.

This commit just adds 2 operations in both addOutbound and addLoader. It
first checks if the new address pull from hostlist belongs in the same
group as an already connected peer. If it does not, it precedes as
usual, and if the connection succeeds then it adds that new outbound
group to the list of groups currently connected to.

Co-authored-by: Nodari Chkuaselidze <nodar.chkuaselidze@gmail.com>
  • Loading branch information
kilpatty and nodech committed Feb 24, 2022
1 parent 4ee4a42 commit 71ffba6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Pool extends EventEmitter {
this.pendingFilter = null;
this.refillTimer = null;
this.discoverTimer = null;
this.connectedGroups = new Set();

this.checkpoints = false;
this.headerChain = new List();
Expand Down Expand Up @@ -663,6 +664,7 @@ class Pool extends EventEmitter {
this.logger.info('Adding loader peer (%s).', peer.hostname());

this.peers.add(peer);
this.connectedGroups.add(addr.getGroup());

this.setLoader(peer);
}
Expand Down Expand Up @@ -3367,6 +3369,7 @@ class Pool extends EventEmitter {

if (!entry)
break;

const addr = entry.addr;

if (this.peers.has(addr.hostname))
Expand All @@ -3390,6 +3393,10 @@ class Pool extends EventEmitter {
if (this.options.brontideOnly && !addr.hasKey())
continue;

// Don't connect to outbound peers in the same group.
if (this.connectedGroups.has(addr.getGroup()))
continue;

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

Expand Down Expand Up @@ -3428,6 +3435,7 @@ class Pool extends EventEmitter {
const peer = this.createOutbound(addr);

this.peers.add(peer);
this.connectedGroups.add(addr.getGroup());

this.emit('peer', peer);
}
Expand Down Expand Up @@ -3482,6 +3490,9 @@ class Pool extends EventEmitter {
removePeer(peer) {
this.peers.remove(peer);

if (peer.outbound)
this.connectedGroups.delete(peer.address.getGroup());

for (const hash of peer.blockMap.keys())
this.resolveBlock(peer, hash);

Expand Down
9 changes: 9 additions & 0 deletions test/net-netaddress-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
'use strict';

/* Parts of this software are based on bitcoin/bitcoin:
* Copyright (c) 2009-2019, The Bitcoin Core Developers (MIT License).
* Copyright (c) 2009-2019, The Bitcoin Developers (MIT License).
* https://github.com/bitcoin/bitcoin
*
* Resources:
* https://github.com/bitcoin/bitcoin/blob/46fc4d1a24c88e797d6080336e3828e45e39c3fd/src/test/netbase_tests.cpp
*/

const assert = require('bsert');
const NetAddress = require('../lib/net/netaddress');
const Network = require('../lib/protocol/network');
Expand Down

0 comments on commit 71ffba6

Please sign in to comment.