Skip to content

Commit

Permalink
rpc: Expose transport type via getpeerinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv committed Feb 20, 2023
1 parent 3906e96 commit 4f40c65
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ void CNode::CopyStats(CNodeStats& stats)
stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToStringAddrPort() : "";

X(m_conn_type);
X(m_transport_type);
}
#undef X

Expand Down Expand Up @@ -780,7 +781,7 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
// to talk v1 or v2. if the first message is not VERSION, we reinterpret the
// bytes as v2 ellswift
if (gArgs.GetBoolArg("-v2transport", DEFAULT_V2_TRANSPORT) &&
!m_prefer_p2p_v2 && IsInboundConn() &&
m_transport_type == TransportProtocolType::V1 && IsInboundConn() &&
mapRecvBytesPerMsgType.at(NetMsgType::VERSION) == 0 &&
msg.m_type != NetMsgType::VERSION) {
return false;
Expand Down Expand Up @@ -1352,6 +1353,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
.permission_flags = permission_flags,
.prefer_evict = discouraged,
.prefer_p2p_v2 = false});
pnode->m_transport_type = TransportProtocolType::V1;
pnode->AddRef();
m_msgproc->InitializeNode(*pnode, nodeServices);

Expand Down Expand Up @@ -1664,6 +1666,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
PushV2EllSwiftPubkey(pnode);
// We now know the peer prefers a BIP324 v2 connection
pnode->m_prefer_p2p_v2 = true;
pnode->m_transport_type = TransportProtocolType::V2;
}

// Keys are not derived because we don't have the peer ellswift yet, keep buffering.
Expand Down Expand Up @@ -1750,6 +1753,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
pnode->CloseSocketDisconnect();
} else {
pnode->m_bip324_shared_state->key_exchange_complete = true;
pnode->m_transport_type = TransportProtocolType::V2;
if (!pnode->IsInboundConn()) {
// Outbound peer has completed key exchange and can start the P2P protocol
m_msgproc->InitP2P(*pnode, nLocalServices);
Expand Down Expand Up @@ -2480,6 +2484,9 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
// Only the outbound peer knows that both sides support BIP324 transport
if (pnode->PreferV2Conn()) {
PushV2EllSwiftPubkey(pnode);
pnode->m_transport_type = TransportProtocolType::V2;
} else {
pnode->m_transport_type = TransportProtocolType::V1;
}
m_msgproc->InitializeNode(*pnode, nLocalServices);
{
Expand Down Expand Up @@ -3242,6 +3249,7 @@ CNode::CNode(NodeId idIn,
m_inbound_onion{inbound_onion},
m_prefer_evict{node_opts.prefer_evict},
nKeyedNetGroup{nKeyedNetGroupIn},
m_transport_type{node_opts.transport_type},
id{idIn},
nLocalHostNonce{nLocalHostNonceIn},
m_conn_type{conn_type_in},
Expand Down
3 changes: 3 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class CNodeStats
Network m_network;
uint32_t m_mapped_as;
ConnectionType m_conn_type;
TransportProtocolType m_transport_type;
};


Expand Down Expand Up @@ -439,6 +440,7 @@ struct CNodeOptions
std::unique_ptr<i2p::sam::Session> i2p_sam_session = nullptr;
bool prefer_evict = false;
bool prefer_p2p_v2 = false;
TransportProtocolType transport_type = TransportProtocolType::V1;
};

struct BIP324Session {
Expand Down Expand Up @@ -544,6 +546,7 @@ class CNode
std::atomic_bool fPauseRecv{false};
std::atomic_bool fPauseSend{false};
std::unique_ptr<BIP324SharedState> m_bip324_shared_state;
TransportProtocolType m_transport_type;

bool IsOutboundOrBlockRelayConn() const {
switch (m_conn_type) {
Expand Down
12 changes: 12 additions & 0 deletions src/node/connection_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ std::string ConnectionTypeAsString(ConnectionType conn_type)

assert(false);
}

std::string TransportTypeAsString(TransportProtocolType transport_type)
{
switch (transport_type) {
case TransportProtocolType::V1:
return "v1";
case TransportProtocolType::V2:
return "v2";
} // no default case, so the compiler can warn about missing cases

assert(false);
}
9 changes: 9 additions & 0 deletions src/node/connection_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,13 @@ enum class ConnectionType {
/** Convert ConnectionType enum to a string value */
std::string ConnectionTypeAsString(ConnectionType conn_type);

/** Transport layer version */
enum class TransportProtocolType {
V1, // unencrypted, plaintext protocol
V2, // BIP324 protocol
};

/** Convert TransportProtocolType enum to a string value */
std::string TransportTypeAsString(TransportProtocolType transport_type);

#endif // BITCOIN_NODE_CONNECTION_TYPES_H
6 changes: 6 additions & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const std::vector<std::string> CONNECTION_TYPE_DOC{
"feeler (short-lived automatic connection for testing addresses)"
};

const std::vector<std::string> TRANSPORT_TYPE_DOC{
"v1 (plaintext transport protocol)",
"v2 (BIP324 encrypted transport protocol)"};

static RPCHelpMan getconnectioncount()
{
return RPCHelpMan{"getconnectioncount",
Expand Down Expand Up @@ -163,6 +167,7 @@ static RPCHelpMan getpeerinfo()
{RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + ".\n"
"Please note this output is unlikely to be stable in upcoming releases as we iterate to\n"
"best capture connection behaviors."},
{RPCResult::Type::STR, "transport_protocol_type", "Type of transport protocol: \n" + Join(TRANSPORT_TYPE_DOC, ",\n") + ".\n"},
}},
}},
},
Expand Down Expand Up @@ -267,6 +272,7 @@ static RPCHelpMan getpeerinfo()
}
obj.pushKV("bytesrecv_per_msg", recvPerMsgType);
obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));
obj.pushKV("transport_protocol_type", TransportTypeAsString(stats.m_transport_type));

ret.push_back(obj);
}
Expand Down
5 changes: 5 additions & 0 deletions test/functional/p2p_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def run_test(self):
# sync_all() verifies that the block tips match
self.sync_all(self.nodes[0:2])
assert_equal(self.nodes[1].getblockcount(), 5)
assert_equal(self.nodes[1].getpeerinfo()[0]["transport_protocol_type"], "v2")

# V1 nodes can sync with each other
assert_equal(self.nodes[2].getblockcount(), 0)
Expand All @@ -48,6 +49,7 @@ def run_test(self):
self.sync_all(self.nodes[2:4])
assert_equal(self.nodes[3].getblockcount(), 8)
assert self.nodes[0].getbestblockhash() != self.nodes[2].getbestblockhash()
assert_equal(self.nodes[2].getpeerinfo()[0]["transport_protocol_type"], "v1")

# V1 nodes can sync with V2 nodes
self.disconnect_nodes(0, 1)
Expand All @@ -58,6 +60,7 @@ def run_test(self):
self.sync_all(self.nodes[1:3])
assert_equal(self.nodes[1].getblockcount(), 8)
assert self.nodes[0].getbestblockhash() != self.nodes[1].getbestblockhash()
assert_equal(self.nodes[2].getpeerinfo()[0]["transport_protocol_type"], "v1")

# V2 nodes can sync with V1 nodes
self.disconnect_nodes(1, 2)
Expand All @@ -66,6 +69,7 @@ def run_test(self):
self.connect_nodes(0, 3, False)
self.sync_all([self.nodes[0], self.nodes[3]])
assert_equal(self.nodes[0].getblockcount(), 8)
assert_equal(self.nodes[0].getpeerinfo()[0]["transport_protocol_type"], "v1")

# V2 node mines another block and everyone gets it
self.connect_nodes(0, 1, True)
Expand All @@ -85,6 +89,7 @@ def run_test(self):
self.connect_nodes(1, 4, True)
self.sync_all()
assert_equal(self.nodes[4].getblockcount(), 11)
assert_equal(self.nodes[4].getpeerinfo()[0]["transport_protocol_type"], "v1")


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions test/functional/rpc_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def test_getpeerinfo(self):
"synced_blocks": -1,
"synced_headers": -1,
"timeoffset": 0,
"transport_protocol_type": "v1",
"version": 0,
},
)
Expand Down

0 comments on commit 4f40c65

Please sign in to comment.