From 395acfa83a5436790c1a722a5609ac9d48df235f Mon Sep 17 00:00:00 2001 From: Amiti Uttarwar Date: Wed, 12 Aug 2020 13:57:13 -0700 Subject: [PATCH] [rpc] Add connection type to getpeerinfo RPC, update tests --- src/net.cpp | 2 ++ src/net.h | 9 +++++++++ src/rpc/net.cpp | 2 ++ test/functional/rpc_net.py | 6 ++++++ 4 files changed, 19 insertions(+) diff --git a/src/net.cpp b/src/net.cpp index 15675a68adc..5b533d7d17e 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -602,6 +602,8 @@ void CNode::copyStats(CNodeStats &stats, const std::vector &m_asmap) // Leave string empty if addrLocal invalid (not filled in yet) CService addrLocalUnlocked = GetAddrLocal(); stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : ""; + + stats.m_conn_type_string = ConnectionTypeAsString(); } #undef X diff --git a/src/net.h b/src/net.h index 61a208802cf..5e134ca7c80 100644 --- a/src/net.h +++ b/src/net.h @@ -114,6 +114,14 @@ struct CSerializedNetMsg std::string m_type; }; +const std::vector CONNECTION_TYPE_DOC{ + "outbound-full-relay (default automatic connections)", + "block-relay-only (does not relay transactions or addresses)", + "inbound (initiated by the peer)", + "manual (added via addnode RPC or -addnode/-connect configuration options)", + "addr-fetch (short-lived automatic connection for soliciting addresses)", + "feeler (short-lived automatic connection for testing addresses)"}; + /** Different types of connections to a peer. This enum encapsulates the * information we have available at the time of opening or accepting the * connection. Aside from INBOUND, all types are initiated by us. */ @@ -692,6 +700,7 @@ public: // Bind address of our side of the connection CAddress addrBind; uint32_t m_mapped_as; + std::string m_conn_type_string; }; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 5af43898576..e480bd2a40d 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -113,6 +113,7 @@ static UniValue getpeerinfo(const JSONRPCRequest& request) {RPCResult::Type::STR, "subver", "The string version"}, {RPCResult::Type::BOOL, "inbound", "Inbound (true) or Outbound (false)"}, {RPCResult::Type::BOOL, "addnode", "Whether connection was due to addnode/-connect or if it was an automatic/inbound connection"}, + {RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + "."}, {RPCResult::Type::NUM, "startingheight", "The starting height (block) of the peer"}, {RPCResult::Type::NUM, "banscore", "The ban score (DEPRECATED, returned only if config option -deprecatedrpc=banscore is passed)"}, {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) recvPerMsgCmd.pushKV(i.first, i.second); } obj.pushKV("bytesrecv_per_msg", recvPerMsgCmd); + obj.pushKV("connection_type", stats.m_conn_type_string); ret.push_back(obj); } diff --git a/test/functional/rpc_net.py b/test/functional/rpc_net.py index bc0e5b458e6..b8a04f494d4 100755 --- a/test/functional/rpc_net.py +++ b/test/functional/rpc_net.py @@ -175,6 +175,12 @@ class NetTest(BitcoinTestFramework): for info in peer_info: assert_net_servicesnames(int(info[0]["services"], 0x10), info[0]["servicesnames"]) + assert_equal(peer_info[0][0]['connection_type'], 'inbound') + assert_equal(peer_info[0][1]['connection_type'], 'manual') + + assert_equal(peer_info[1][0]['connection_type'], 'manual') + assert_equal(peer_info[1][1]['connection_type'], 'inbound') + def test_service_flags(self): self.log.info("Test service flags") self.nodes[0].add_p2p_connection(P2PInterface(), services=(1 << 4) | (1 << 63))