Skip to content

Commit 395acfa

Browse files
committed
[rpc] Add connection type to getpeerinfo RPC, update tests
1 parent 49c10a9 commit 395acfa

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

src/net.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,8 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
602602
// Leave string empty if addrLocal invalid (not filled in yet)
603603
CService addrLocalUnlocked = GetAddrLocal();
604604
stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
605+
606+
stats.m_conn_type_string = ConnectionTypeAsString();
605607
}
606608
#undef X
607609

src/net.h

Lines changed: 9 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. */
@@ -692,6 +700,7 @@ class CNodeStats
692700
// Bind address of our side of the connection
693701
CAddress addrBind;
694702
uint32_t m_mapped_as;
703+
std::string m_conn_type_string;
695704
};
696705

697706

src/rpc/net.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static UniValue getpeerinfo(const JSONRPCRequest& request)
113113
{RPCResult::Type::STR, "subver", "The string version"},
114114
{RPCResult::Type::BOOL, "inbound", "Inbound (true) or Outbound (false)"},
115115
{RPCResult::Type::BOOL, "addnode", "Whether connection was due to addnode/-connect or if it was an automatic/inbound connection"},
116+
{RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + "."},
116117
{RPCResult::Type::NUM, "startingheight", "The starting height (block) of the peer"},
117118
{RPCResult::Type::NUM, "banscore", "The ban score (DEPRECATED, returned only if config option -deprecatedrpc=banscore is passed)"},
118119
{RPCResult::Type::NUM, "synced_headers", "The last header we have in common with this peer"},
@@ -228,6 +229,7 @@ static UniValue getpeerinfo(const JSONRPCRequest& request)
228229
recvPerMsgCmd.pushKV(i.first, i.second);
229230
}
230231
obj.pushKV("bytesrecv_per_msg", recvPerMsgCmd);
232+
obj.pushKV("connection_type", stats.m_conn_type_string);
231233

232234
ret.push_back(obj);
233235
}

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))

0 commit comments

Comments
 (0)