Skip to content

Commit 9158d6f

Browse files
author
MarcoFalke
committed
Merge #20786: net: [refactor] Prefer integral types in CNodeStats
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
2 parents 1e078f1 + faecb74 commit 9158d6f

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/net.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,9 @@ void CConnman::AddWhitelistPermissionFlags(NetPermissionFlags& flags, const CNet
498498
}
499499
}
500500

501-
std::string CNode::ConnectionTypeAsString() const
501+
std::string ConnectionTypeAsString(ConnectionType conn_type)
502502
{
503-
switch (m_conn_type) {
503+
switch (conn_type) {
504504
case ConnectionType::INBOUND:
505505
return "inbound";
506506
case ConnectionType::MANUAL:
@@ -618,7 +618,7 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
618618
CService addrLocalUnlocked = GetAddrLocal();
619619
stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
620620

621-
stats.m_conn_type_string = ConnectionTypeAsString();
621+
X(m_conn_type);
622622
}
623623
#undef X
624624

src/net.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ enum class ConnectionType {
174174
ADDR_FETCH,
175175
};
176176

177+
/** Convert ConnectionType enum to a string value */
178+
std::string ConnectionTypeAsString(ConnectionType conn_type);
177179
void Discover();
178180
uint16_t GetListenPort();
179181

@@ -264,11 +266,10 @@ class CNodeStats
264266
// Network the peer connected through
265267
Network m_network;
266268
uint32_t m_mapped_as;
267-
std::string m_conn_type_string;
269+
ConnectionType m_conn_type;
268270
};
269271

270272

271-
272273
/** Transport protocol agnostic message container.
273274
* Ideally it should only contain receive time, payload,
274275
* command and size.
@@ -756,7 +757,7 @@ class CNode
756757
//! Sets the addrName only if it was not previously set
757758
void MaybeSetAddrName(const std::string& addrNameIn);
758759

759-
std::string ConnectionTypeAsString() const;
760+
std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }
760761

761762
/** Whether this peer is an inbound onion, e.g. connected via our Tor onion service. */
762763
bool IsInboundOnion() const { return m_inbound_onion; }

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static RPCHelpMan getpeerinfo()
249249
recvPerMsgCmd.pushKV(i.first, i.second);
250250
}
251251
obj.pushKV("bytesrecv_per_msg", recvPerMsgCmd);
252-
obj.pushKV("connection_type", stats.m_conn_type_string);
252+
obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));
253253

254254
ret.push_back(obj);
255255
}

0 commit comments

Comments
 (0)