Skip to content

Commit 4f45ea1

Browse files
author
MarcoFalke
committed
Merge #19725: [RPC] Add connection type to getpeerinfo, improve logs
a512925 [doc] Release notes (Amiti Uttarwar) 50f94b3 [rpc] Deprecate getpeerinfo addnode field (Amiti Uttarwar) df091b9 [refactor] Rename test file to allow any getpeerinfo deprecations. (Amiti Uttarwar) 395acfa [rpc] Add connection type to getpeerinfo RPC, update tests (Amiti Uttarwar) 49c10a9 [log] Add connection type to log statement (Amiti Uttarwar) Pull request description: After #19316, we can more directly expose information about the connection type on the `getpeerinfo` RPC. Doing so also makes the existing addnode field redundant, so this PR begins the process of deprecating this field. This PR also includes one commit that improves a log message, as both use a shared function to return the connection type as a string. Suggested by sdaftuar- bitcoin/bitcoin#19316 (comment) & bitcoin/bitcoin#19316 (comment) ACKs for top commit: jnewbery: Code review ACK a512925. sipa: utACK a512925 guggero: Tested and code review ACK a512925. MarcoFalke: cr ACK a512925 🌇 promag: Code review ACK a512925. Tree-SHA512: 601a7a38aee235ee59aca690784f886dc2ae4e418b2e6422c4b58cd597376c00f74910f66920b08a08a0bec28bf8022e71a1435785ff6ba8a188954261aba78e
2 parents 8aa3a4a + a512925 commit 4f45ea1

File tree

9 files changed

+99
-32
lines changed

9 files changed

+99
-32
lines changed

doc/release-notes.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ will trigger BIP 125 (replace-by-fee) opt-in. (#11413)
105105
- The `testmempoolaccept` RPC returns `vsize` and a `fee` object with the `base` fee
106106
if the transaction passes validation. (#19940)
107107

108+
- The `getpeerinfo` RPC now returns a `connection_type` field. This indicates
109+
the type of connection established with the peer. It will return one of six
110+
options. For more information, see the `getpeerinfo` help documentation.
111+
(#19725)
112+
113+
- The `getpeerinfo` RPC no longer returns the `addnode` field by default. This
114+
field will be fully removed in the next major release. It can be accessed
115+
with the configuration option `-deprecatedrpc=getpeerinfo_addnode`. However,
116+
it is recommended to instead use the `connection_type` field (it will return
117+
`manual` when addnode is true). (#19725)
118+
108119
- The `walletcreatefundedpsbt` RPC call will now fail with
109120
`Insufficient funds` when inputs are manually selected but are not enough to cover
110121
the outputs and fee. Additional inputs can automatically be added through the

src/net.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,26 @@ void CConnman::AddWhitelistPermissionFlags(NetPermissionFlags& flags, const CNet
488488
}
489489
}
490490

491+
std::string CNode::ConnectionTypeAsString() const
492+
{
493+
switch (m_conn_type) {
494+
case ConnectionType::INBOUND:
495+
return "inbound";
496+
case ConnectionType::MANUAL:
497+
return "manual";
498+
case ConnectionType::FEELER:
499+
return "feeler";
500+
case ConnectionType::OUTBOUND_FULL_RELAY:
501+
return "outbound-full-relay";
502+
case ConnectionType::BLOCK_RELAY:
503+
return "block-relay-only";
504+
case ConnectionType::ADDR_FETCH:
505+
return "addr-fetch";
506+
} // no default case, so the compiler can warn about missing cases
507+
508+
assert(false);
509+
}
510+
491511
std::string CNode::GetAddrName() const {
492512
LOCK(cs_addrName);
493513
return addrName;
@@ -582,6 +602,8 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
582602
// Leave string empty if addrLocal invalid (not filled in yet)
583603
CService addrLocalUnlocked = GetAddrLocal();
584604
stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
605+
606+
stats.m_conn_type_string = ConnectionTypeAsString();
585607
}
586608
#undef X
587609

src/net.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ struct CSerializedNetMsg
114114
std::string m_type;
115115
};
116116

117+
const std::vector<std::string> CONNECTION_TYPE_DOC{
118+
"outbound-full-relay (default automatic connections)",
119+
"block-relay-only (does not relay transactions or addresses)",
120+
"inbound (initiated by the peer)",
121+
"manual (added via addnode RPC or -addnode/-connect configuration options)",
122+
"addr-fetch (short-lived automatic connection for soliciting addresses)",
123+
"feeler (short-lived automatic connection for testing addresses)"};
124+
117125
/** Different types of connections to a peer. This enum encapsulates the
118126
* information we have available at the time of opening or accepting the
119127
* connection. Aside from INBOUND, all types are initiated by us. */
@@ -691,6 +699,7 @@ class CNodeStats
691699
// Bind address of our side of the connection
692700
CAddress addrBind;
693701
uint32_t m_mapped_as;
702+
std::string m_conn_type_string;
694703
};
695704

696705

@@ -1144,6 +1153,8 @@ class CNode
11441153
std::string GetAddrName() const;
11451154
//! Sets the addrName only if it was not previously set
11461155
void MaybeSetAddrName(const std::string& addrNameIn);
1156+
1157+
std::string ConnectionTypeAsString() const;
11471158
};
11481159

11491160
/** Return a timestamp in the future (in microseconds) for exponentially distributed events. */

src/net_processing.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,11 +3520,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
35203520
// Making nodes which are behind NAT and can only make outgoing connections ignore
35213521
// the getaddr message mitigates the attack.
35223522
if (!pfrom.IsInboundConn()) {
3523-
LogPrint(BCLog::NET, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom.GetId());
3524-
return;
3525-
}
3526-
if (!pfrom.RelayAddrsWithConn()) {
3527-
LogPrint(BCLog::NET, "Ignoring \"getaddr\" from block-relay-only connection. peer=%d\n", pfrom.GetId());
3523+
LogPrint(BCLog::NET, "Ignoring \"getaddr\" from %s connection. peer=%d\n", pfrom.ConnectionTypeAsString(), pfrom.GetId());
35283524
return;
35293525
}
35303526

src/rpc/net.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ static RPCHelpMan getpeerinfo()
116116
{RPCResult::Type::NUM, "version", "The peer version, such as 70001"},
117117
{RPCResult::Type::STR, "subver", "The string version"},
118118
{RPCResult::Type::BOOL, "inbound", "Inbound (true) or Outbound (false)"},
119-
{RPCResult::Type::BOOL, "addnode", "Whether connection was due to addnode/-connect or if it was an automatic/inbound connection"},
119+
{RPCResult::Type::BOOL, "addnode", "Whether connection was due to addnode/-connect or if it was an automatic/inbound connection\n"
120+
"(DEPRECATED, returned only if the config option -deprecatedrpc=getpeerinfo_addnode is passed)"},
121+
{RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + "."},
120122
{RPCResult::Type::NUM, "startingheight", "The starting height (block) of the peer"},
121123
{RPCResult::Type::NUM, "banscore", "The ban score (DEPRECATED, returned only if config option -deprecatedrpc=banscore is passed)"},
122124
{RPCResult::Type::NUM, "synced_headers", "The last header we have in common with this peer"},
@@ -196,7 +198,10 @@ static RPCHelpMan getpeerinfo()
196198
// their ver message.
197199
obj.pushKV("subver", stats.cleanSubVer);
198200
obj.pushKV("inbound", stats.fInbound);
199-
obj.pushKV("addnode", stats.m_manual_connection);
201+
if (IsDeprecatedRPCEnabled("getpeerinfo_addnode")) {
202+
// addnode is deprecated in v0.21 for removal in v0.22
203+
obj.pushKV("addnode", stats.m_manual_connection);
204+
}
200205
obj.pushKV("startingheight", stats.nStartingHeight);
201206
if (fStateStats) {
202207
if (IsDeprecatedRPCEnabled("banscore")) {
@@ -232,6 +237,7 @@ static RPCHelpMan getpeerinfo()
232237
recvPerMsgCmd.pushKV(i.first, i.second);
233238
}
234239
obj.pushKV("bytesrecv_per_msg", recvPerMsgCmd);
240+
obj.pushKV("connection_type", stats.m_conn_type_string);
235241

236242
ret.push_back(obj);
237243
}

test/functional/rpc_getpeerinfo_banscore_deprecation.py

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 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+
"""Test deprecation of getpeerinfo RPC fields."""
6+
7+
from test_framework.test_framework import BitcoinTestFramework
8+
from test_framework.util import connect_nodes
9+
10+
11+
class GetpeerinfoDeprecationTest(BitcoinTestFramework):
12+
def set_test_params(self):
13+
self.num_nodes = 2
14+
self.extra_args = [[], ["-deprecatedrpc=banscore"]]
15+
16+
def run_test(self):
17+
self.test_banscore_deprecation()
18+
self.test_addnode_deprecation()
19+
20+
def test_banscore_deprecation(self):
21+
self.log.info("Test getpeerinfo by default no longer returns a banscore field")
22+
assert "banscore" not in self.nodes[0].getpeerinfo()[0].keys()
23+
24+
self.log.info("Test getpeerinfo returns banscore with -deprecatedrpc=banscore")
25+
assert "banscore" in self.nodes[1].getpeerinfo()[0].keys()
26+
27+
def test_addnode_deprecation(self):
28+
self.restart_node(1, ["-deprecatedrpc=getpeerinfo_addnode"])
29+
connect_nodes(self.nodes[0], 1)
30+
31+
self.log.info("Test getpeerinfo by default no longer returns an addnode field")
32+
assert "addnode" not in self.nodes[0].getpeerinfo()[0].keys()
33+
34+
self.log.info("Test getpeerinfo returns addnode with -deprecatedrpc=addnode")
35+
assert "addnode" in self.nodes[1].getpeerinfo()[0].keys()
36+
37+
38+
if __name__ == "__main__":
39+
GetpeerinfoDeprecationTest().main()

test/functional/rpc_net.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ def test_getpeerinfo(self):
175175
for info in peer_info:
176176
assert_net_servicesnames(int(info[0]["services"], 0x10), info[0]["servicesnames"])
177177

178+
assert_equal(peer_info[0][0]['connection_type'], 'inbound')
179+
assert_equal(peer_info[0][1]['connection_type'], 'manual')
180+
181+
assert_equal(peer_info[1][0]['connection_type'], 'manual')
182+
assert_equal(peer_info[1][1]['connection_type'], 'inbound')
183+
178184
def test_service_flags(self):
179185
self.log.info("Test service flags")
180186
self.nodes[0].add_p2p_connection(P2PInterface(), services=(1 << 4) | (1 << 63))

test/functional/test_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@
251251
'feature_config_args.py',
252252
'feature_settings.py',
253253
'rpc_getdescriptorinfo.py',
254-
'rpc_getpeerinfo_banscore_deprecation.py',
254+
'rpc_getpeerinfo_deprecation.py',
255255
'rpc_help.py',
256256
'feature_help.py',
257257
'feature_shutdown.py',

0 commit comments

Comments
 (0)