Skip to content

Commit e4487fd

Browse files
committed
Merge bitcoin#22096: p2p: AddrFetch - don't disconnect on self-announcements
5730a43 test: Add functional test for AddrFetch connections (Martin Zumsande) c34ad33 net, rpc: Enable AddrFetch connections for functional testing (Martin Zumsande) 533500d p2p: Add timeout for AddrFetch peers (Martin Zumsande) b6c5d1e p2p: AddrFetch - don't disconnect on self-announcements (Martin Zumsande) Pull request description: AddrFetch connections (old name: oneshots) are intended to be short-lived connections on which we ask a peer for addresses via `getaddr` and disconnect after receiving them. This is done by disconnecting after receiving the first `addr`. However, it is no longer working as intended, because nowadays, the first `addr` a typical bitcoin core node sends is its self-announcement. So we'll disconnect before the peer gets a chance to answer our `getaddr`. I checked that this affects both `-seednode` peers specified manually, and DNS seeds when AddrFetch is used as a fallback if DNS doesn't work for us. The current behavior of getting peers via AddrFetch when starting with an empty addrman would be to connect to the peer, receive its self-announcement and add it to addrman, disconnect, reconnect to the same peer again as a full outbound (no other addresses in addrman) and then receive more `addr`. This is silly and not in line with AddrFetch peer being intended to be short-lived peers.  Fix this by only disconnecting after receiving an `addr` message of size > 1. [Edit] As per review discussion, this PR now also adds a timeout after which we disconnect if we haven't received any suitable `addr`, and a functional test. ACKs for top commit: amitiuttarwar: reACK 5730a43 naumenkogs: ACK 5730a43 jnewbery: ACK 5730a43 Tree-SHA512: 8a81234f37e827705138eb254223f7f3b3bf44a06cb02126fc7990b0d231b9bd8f07d38d185cc30d55bf35548a6fdc286b69602498d875b937e7c58332158bf9
2 parents 5c8820b + 5730a43 commit e4487fd

File tree

7 files changed

+95
-9
lines changed

7 files changed

+95
-9
lines changed

src/net.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,16 +1212,29 @@ void CConnman::CreateNodeFromAcceptedSocket(SOCKET hSocket,
12121212

12131213
bool CConnman::AddConnection(const std::string& address, ConnectionType conn_type)
12141214
{
1215-
if (conn_type != ConnectionType::OUTBOUND_FULL_RELAY && conn_type != ConnectionType::BLOCK_RELAY) return false;
1216-
1217-
const int max_connections = conn_type == ConnectionType::OUTBOUND_FULL_RELAY ? m_max_outbound_full_relay : m_max_outbound_block_relay;
1215+
std::optional<int> max_connections;
1216+
switch (conn_type) {
1217+
case ConnectionType::INBOUND:
1218+
case ConnectionType::MANUAL:
1219+
case ConnectionType::FEELER:
1220+
return false;
1221+
case ConnectionType::OUTBOUND_FULL_RELAY:
1222+
max_connections = m_max_outbound_full_relay;
1223+
break;
1224+
case ConnectionType::BLOCK_RELAY:
1225+
max_connections = m_max_outbound_block_relay;
1226+
break;
1227+
// no limit for ADDR_FETCH because -seednode has no limit either
1228+
case ConnectionType::ADDR_FETCH:
1229+
break;
1230+
} // no default case, so the compiler can warn about missing cases
12181231

12191232
// Count existing connections
12201233
int existing_connections = WITH_LOCK(cs_vNodes,
12211234
return std::count_if(vNodes.begin(), vNodes.end(), [conn_type](CNode* node) { return node->m_conn_type == conn_type; }););
12221235

12231236
// Max connections of specified type already exist
1224-
if (existing_connections >= max_connections) return false;
1237+
if (max_connections != std::nullopt && existing_connections >= max_connections) return false;
12251238

12261239
// Max total outbound connections already exist
12271240
CSemaphoreGrant grant(*semOutbound, true);

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ class CConnman
893893
*
894894
* @param[in] address Address of node to try connecting to
895895
* @param[in] conn_type ConnectionType::OUTBOUND or ConnectionType::BLOCK_RELAY
896+
* or ConnectionType::ADDR_FETCH
896897
* @return bool Returns false if there are no available
897898
* slots for this connection:
898899
* - conn_type not a supported ConnectionType

src/net_processing.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2860,7 +2860,9 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
28602860

28612861
m_addrman.Add(vAddrOk, pfrom.addr, 2 * 60 * 60);
28622862
if (vAddr.size() < 1000) peer->m_getaddr_sent = false;
2863-
if (pfrom.IsAddrFetchConn()) {
2863+
2864+
// AddrFetch: Require multiple addresses to avoid disconnecting on self-announcements
2865+
if (pfrom.IsAddrFetchConn() && vAddr.size() > 1) {
28642866
LogPrint(BCLog::NET, "addrfetch connection completed peer=%d; disconnecting\n", pfrom.GetId());
28652867
pfrom.fDisconnect = true;
28662868
}
@@ -4444,6 +4446,12 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
44444446

44454447
const auto current_time = GetTime<std::chrono::microseconds>();
44464448

4449+
if (pto->IsAddrFetchConn() && current_time - std::chrono::seconds(pto->nTimeConnected) > 10 * AVG_ADDRESS_BROADCAST_INTERVAL) {
4450+
LogPrint(BCLog::NET, "addrfetch connection timeout; disconnecting peer=%d\n", pto->GetId());
4451+
pto->fDisconnect = true;
4452+
return true;
4453+
}
4454+
44474455
MaybeSendPing(*pto, *peer, current_time);
44484456

44494457
// MaybeSendPing may have marked peer for disconnection

src/rpc/net.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ static RPCHelpMan addconnection()
339339
"\nOpen an outbound connection to a specified node. This RPC is for testing only.\n",
340340
{
341341
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address and port to attempt connecting to."},
342-
{"connection_type", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of connection to open, either \"outbound-full-relay\" or \"block-relay-only\"."},
342+
{"connection_type", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of connection to open (\"outbound-full-relay\", \"block-relay-only\" or \"addr-fetch\")."},
343343
},
344344
RPCResult{
345345
RPCResult::Type::OBJ, "", "",
@@ -365,6 +365,8 @@ static RPCHelpMan addconnection()
365365
conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
366366
} else if (conn_type_in == "block-relay-only") {
367367
conn_type = ConnectionType::BLOCK_RELAY;
368+
} else if (conn_type_in == "addr-fetch") {
369+
conn_type = ConnectionType::ADDR_FETCH;
368370
} else {
369371
throw JSONRPCError(RPC_INVALID_PARAMETER, self.ToString());
370372
}

test/functional/p2p_addrfetch.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2021 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""
6+
Test p2p addr-fetch connections
7+
"""
8+
9+
import time
10+
11+
from test_framework.messages import msg_addr, CAddress, NODE_NETWORK, NODE_WITNESS
12+
from test_framework.p2p import P2PInterface, p2p_lock
13+
from test_framework.test_framework import BitcoinTestFramework
14+
from test_framework.util import assert_equal
15+
16+
ADDR = CAddress()
17+
ADDR.time = int(time.time())
18+
ADDR.nServices = NODE_NETWORK | NODE_WITNESS
19+
ADDR.ip = "192.0.0.8"
20+
ADDR.port = 18444
21+
22+
23+
class P2PAddrFetch(BitcoinTestFramework):
24+
25+
def set_test_params(self):
26+
self.setup_clean_chain = True
27+
self.num_nodes = 1
28+
29+
def run_test(self):
30+
node = self.nodes[0]
31+
self.log.info("Connect to an addr-fetch peer")
32+
peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, connection_type="addr-fetch")
33+
info = node.getpeerinfo()
34+
assert_equal(len(info), 1)
35+
assert_equal(info[0]['connection_type'], 'addr-fetch')
36+
37+
self.log.info("Check that we send getaddr but don't try to sync headers with the addr-fetch peer")
38+
peer.sync_send_with_ping()
39+
with p2p_lock:
40+
assert peer.message_count['getaddr'] == 1
41+
assert peer.message_count['getheaders'] == 0
42+
43+
self.log.info("Check that answering the getaddr with a single address does not lead to disconnect")
44+
# This prevents disconnecting on self-announcements
45+
msg = msg_addr()
46+
msg.addrs = [ADDR]
47+
peer.send_and_ping(msg)
48+
assert_equal(len(node.getpeerinfo()), 1)
49+
50+
self.log.info("Check that answering with larger addr messages leads to disconnect")
51+
msg.addrs = [ADDR] * 2
52+
peer.send_message(msg)
53+
peer.wait_for_disconnect(timeout=5)
54+
55+
self.log.info("Check timeout for addr-fetch peer that does not send addrs")
56+
peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, connection_type="addr-fetch")
57+
node.setmocktime(int(time.time()) + 301) # Timeout: 5 minutes
58+
peer.wait_for_disconnect(timeout=5)
59+
60+
61+
if __name__ == '__main__':
62+
P2PAddrFetch().main()

test/functional/test_framework/test_node.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,8 @@ def add_p2p_connection(self, p2p_conn, *, wait_for_verack=True, **kwargs):
557557
return p2p_conn
558558

559559
def add_outbound_p2p_connection(self, p2p_conn, *, p2p_idx, connection_type="outbound-full-relay", **kwargs):
560-
"""Add an outbound p2p connection from node. Either
561-
full-relay("outbound-full-relay") or
562-
block-relay-only("block-relay-only") connection.
560+
"""Add an outbound p2p connection from node. Must be an
561+
"outbound-full-relay", "block-relay-only" or "addr-fetch" connection.
563562
564563
This method adds the p2p connection to the self.p2ps list and returns
565564
the connection to the caller.

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
'p2p_addr_relay.py',
187187
'p2p_getaddr_caching.py',
188188
'p2p_getdata.py',
189+
'p2p_addrfetch.py',
189190
'rpc_net.py',
190191
'wallet_keypool.py --legacy-wallet',
191192
'wallet_keypool.py --descriptors',

0 commit comments

Comments
 (0)