Skip to content

Commit 16d9bfc

Browse files
sdaftuarjnewbery
andcommitted
Avoid test-before-evict evictions of current peers
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 <[email protected]>
1 parent e8b215a commit 16d9bfc

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/net.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,11 +1999,30 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
19991999
if (nTries > 100)
20002000
break;
20012001

2002-
CAddrInfo addr = addrman.SelectTriedCollision();
2002+
CAddrInfo addr;
20032003

2004-
// SelectTriedCollision returns an invalid address if it is empty.
2005-
if (!fFeeler || !addr.IsValid()) {
2006-
addr = addrman.Select(fFeeler);
2004+
if (fFeeler) {
2005+
// First, try to get a tried table collision address. This returns
2006+
// an empty (invalid) address if there are no collisions to try.
2007+
addr = addrman.SelectTriedCollision();
2008+
2009+
if (!addr.IsValid()) {
2010+
// No tried table collisions. Select a new table address
2011+
// for our feeler.
2012+
addr = addrman.Select(true);
2013+
} else if (AlreadyConnectedToAddress(addr)) {
2014+
// If test-before-evict logic would have us connect to a
2015+
// peer that we're already connected to, just mark that
2016+
// address as Good(). We won't be able to initiate the
2017+
// connection anyway, so this avoids inadvertently evicting
2018+
// a currently-connected peer.
2019+
addrman.Good(addr);
2020+
// Select a new table address for our feeler instead.
2021+
addr = addrman.Select(true);
2022+
}
2023+
} else {
2024+
// Not a feeler
2025+
addr = addrman.Select();
20072026
}
20082027

20092028
// Require outbound connections, other than feelers, to be to distinct network groups

0 commit comments

Comments
 (0)