Skip to content

Commit

Permalink
Merge bitcoin#20786: net: [refactor] Prefer integral types in CNodeStats
Browse files Browse the repository at this point in the history
faecb74 Expose integral m_conn_type in CNodeStats, remove m_conn_type_string (Jon Atack)

Pull request description:

  Currently, strings are stored for what are actually integral (strong) enum types. This is fine, because the strings are only used as-is for the debug log and RPC. However, it complicates using them in the GUI. User facing strings in the GUI should be translated and only string literals can be picked up for translation, not runtime `std::string`s.

  Fix that by removing the `std::string` members and replace them by strong enum integral types.

ACKs for top commit:
  jonatack:
    Code review ACK faecb74
  theStack:
    Code review ACK faecb74 🌲

Tree-SHA512: 24df2bd0645432060e393eb44b8abaf20fe296457d07a867b0e735c3e2e75af7b03fc6bfeca734ec33ab816a7c8e1f8591a5ec342f3afe3098a4e41f5c2cfebb
  • Loading branch information
MarcoFalke authored and PastaPastaPasta committed Apr 23, 2024
1 parent 751c9e6 commit 18169f4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,9 @@ bool CNode::IsBlockRelayOnly() const {
return (ignores_incoming_txs && !HasPermission(NetPermissionFlags::Relay)) || IsBlockOnlyConn();
}

std::string CNode::ConnectionTypeAsString() const
std::string ConnectionTypeAsString(ConnectionType conn_type)
{
switch (m_conn_type) {
switch (conn_type) {
case ConnectionType::INBOUND:
return "inbound";
case ConnectionType::MANUAL:
Expand Down Expand Up @@ -700,7 +700,7 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
X(verifiedPubKeyHash);
}
X(m_masternode_connection);
stats.m_conn_type_string = ConnectionTypeAsString();
X(m_conn_type);
}
#undef X

Expand Down
6 changes: 4 additions & 2 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ enum class ConnectionType {
ADDR_FETCH,
};

/** Convert ConnectionType enum to a string value */
std::string ConnectionTypeAsString(ConnectionType conn_type);
void Discover();
uint16_t GetListenPort();

Expand Down Expand Up @@ -307,7 +309,7 @@ class CNodeStats
// In case this is a verified MN, this value is the hashed operator pubkey of the MN
uint256 verifiedPubKeyHash;
bool m_masternode_connection;
std::string m_conn_type_string;
ConnectionType m_conn_type;
};


Expand Down Expand Up @@ -733,7 +735,7 @@ class CNode
void MaybeSetAddrName(const std::string& addrNameIn);


std::string ConnectionTypeAsString() const;
std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }

/** A ping-pong round trip has completed successfully. Update latest and minimum ping times. */
void PongReceived(std::chrono::microseconds ping_time) {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static RPCHelpMan getpeerinfo()
recvPerMsgCmd.pushKV(i.first, i.second);
}
obj.pushKV("bytesrecv_per_msg", recvPerMsgCmd);
obj.pushKV("connection_type", stats.m_conn_type_string);
obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));

ret.push_back(obj);
}
Expand Down

0 comments on commit 18169f4

Please sign in to comment.