Skip to content

Commit

Permalink
Avoid test-before-evict evictions of current peers
Browse files Browse the repository at this point in the history
Summary:
Outbound peer logic prevents connecting to addresses that we're already
connected to, so prevent inadvertent eviction of current peers via
test-before-evict by checking this condition and marking current peer's
addresses as `Good()`.

Co-authored-by: John Newbery <john@johnnewbery.com>

This is a backport of [[bitcoin/bitcoin#20187 | core#20187]] [4/4]
bitcoin/bitcoin@16d9bfc

Depends on D10696

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10697
  • Loading branch information
sdaftuar authored and PiRK committed Dec 21, 2021
1 parent 811f799 commit 82d88e3
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2230,11 +2230,31 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect) {
break;
}

CAddrInfo addr = addrman.SelectTriedCollision();
CAddrInfo addr;

// SelectTriedCollision returns an invalid address if it is empty.
if (!fFeeler || !addr.IsValid()) {
addr = addrman.Select(fFeeler);
if (fFeeler) {
// First, try to get a tried table collision address. This
// returns an empty (invalid) address if there are no collisions
// to try.
addr = addrman.SelectTriedCollision();

if (!addr.IsValid()) {
// No tried table collisions. Select a new table address
// for our feeler.
addr = addrman.Select(true);
} else if (AlreadyConnectedToAddress(addr)) {
// If test-before-evict logic would have us connect to a
// peer that we're already connected to, just mark that
// address as Good(). We won't be able to initiate the
// connection anyway, so this avoids inadvertently evicting
// a currently-connected peer.
addrman.Good(addr);
// Select a new table address for our feeler instead.
addr = addrman.Select(true);
}
} else {
// Not a feeler
addr = addrman.Select();
}

// Require outbound connections, other than feelers, to be to
Expand Down

0 comments on commit 82d88e3

Please sign in to comment.