merge bitcoin#25962: Add CNodeOptions and increase constness

This commit is contained in:
Kittywhiskers Van Gogh 2022-08-31 17:04:13 +10:00
parent 79e67fd96a
commit d9e56f3e78
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
9 changed files with 47 additions and 38 deletions

View File

@ -617,7 +617,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
pszDest ? pszDest : "", pszDest ? pszDest : "",
conn_type, conn_type,
/*inbound_onion=*/false, /*inbound_onion=*/false,
std::move(i2p_transient_session)); CNodeOptions{ .i2p_sam_session = std::move(i2p_transient_session) });
pnode->AddRef(); pnode->AddRef();
::g_stats_client->inc("peers.connect", 1.0f); ::g_stats_client->inc("peers.connect", 1.0f);
@ -741,7 +741,7 @@ void CNode::CopyStats(CNodeStats& stats)
X(nRecvBytes); X(nRecvBytes);
} }
X(m_legacyWhitelisted); X(m_legacyWhitelisted);
X(m_permissionFlags); X(m_permission_flags);
X(m_last_ping_time); X(m_last_ping_time);
X(m_min_ping_time); X(m_min_ping_time);
@ -1157,14 +1157,14 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket, CMasternodeSy
const CAddress addr_bind{MaybeFlipIPv6toCJDNS(GetBindAddress(*sock)), NODE_NONE}; const CAddress addr_bind{MaybeFlipIPv6toCJDNS(GetBindAddress(*sock)), NODE_NONE};
NetPermissionFlags permissionFlags = NetPermissionFlags::None; NetPermissionFlags permission_flags = NetPermissionFlags::None;
hListenSocket.AddSocketPermissionFlags(permissionFlags); hListenSocket.AddSocketPermissionFlags(permission_flags);
CreateNodeFromAcceptedSocket(std::move(sock), permissionFlags, addr_bind, addr, mn_sync); CreateNodeFromAcceptedSocket(std::move(sock), permission_flags, addr_bind, addr, mn_sync);
} }
void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock, void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
NetPermissionFlags permissionFlags, NetPermissionFlags permission_flags,
const CAddress& addr_bind, const CAddress& addr_bind,
const CAddress& addr, const CAddress& addr,
CMasternodeSync& mn_sync) CMasternodeSync& mn_sync)
@ -1173,14 +1173,14 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
int nVerifiedInboundMasternodes = 0; int nVerifiedInboundMasternodes = 0;
int nMaxInbound = nMaxConnections - m_max_outbound; int nMaxInbound = nMaxConnections - m_max_outbound;
AddWhitelistPermissionFlags(permissionFlags, addr); AddWhitelistPermissionFlags(permission_flags, addr);
bool legacyWhitelisted = false; bool legacyWhitelisted = false;
if (NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::Implicit)) { if (NetPermissions::HasFlag(permission_flags, NetPermissionFlags::Implicit)) {
NetPermissions::ClearFlag(permissionFlags, NetPermissionFlags::Implicit); NetPermissions::ClearFlag(permission_flags, NetPermissionFlags::Implicit);
if (gArgs.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) NetPermissions::AddFlag(permissionFlags, NetPermissionFlags::ForceRelay); if (gArgs.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) NetPermissions::AddFlag(permission_flags, NetPermissionFlags::ForceRelay);
if (gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)) NetPermissions::AddFlag(permissionFlags, NetPermissionFlags::Relay); if (gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)) NetPermissions::AddFlag(permission_flags, NetPermissionFlags::Relay);
NetPermissions::AddFlag(permissionFlags, NetPermissionFlags::Mempool); NetPermissions::AddFlag(permission_flags, NetPermissionFlags::Mempool);
NetPermissions::AddFlag(permissionFlags, NetPermissionFlags::NoBan); NetPermissions::AddFlag(permission_flags, NetPermissionFlags::NoBan);
legacyWhitelisted = true; legacyWhitelisted = true;
} }
@ -1225,7 +1225,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
// Don't accept connections from banned peers. // Don't accept connections from banned peers.
bool banned = m_banman && m_banman->IsBanned(addr); bool banned = m_banman && m_banman->IsBanned(addr);
if (!NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::NoBan) && banned) if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && banned)
{ {
LogPrint(BCLog::NET, "%s (banned)\n", strDropped); LogPrint(BCLog::NET, "%s (banned)\n", strDropped);
return; return;
@ -1233,7 +1233,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
// Only accept connections from discouraged peers if our inbound slots aren't (almost) full. // Only accept connections from discouraged peers if our inbound slots aren't (almost) full.
bool discouraged = m_banman && m_banman->IsDiscouraged(addr); bool discouraged = m_banman && m_banman->IsDiscouraged(addr);
if (!NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::NoBan) && nInbound + 1 >= nMaxInbound && discouraged) if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && nInbound + 1 >= nMaxInbound && discouraged)
{ {
LogPrint(BCLog::NET, "connection from %s dropped (discouraged)\n", addr.ToStringAddrPort()); LogPrint(BCLog::NET, "connection from %s dropped (discouraged)\n", addr.ToStringAddrPort());
return; return;
@ -1264,7 +1264,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize(); uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
ServiceFlags nodeServices = nLocalServices; ServiceFlags nodeServices = nLocalServices;
if (NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::BloomFilter)) { if (NetPermissions::HasFlag(permission_flags, NetPermissionFlags::BloomFilter)) {
nodeServices = static_cast<ServiceFlags>(nodeServices | NODE_BLOOM); nodeServices = static_cast<ServiceFlags>(nodeServices | NODE_BLOOM);
} }
@ -1277,12 +1277,14 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
addr_bind, addr_bind,
/*addrNameIn=*/"", /*addrNameIn=*/"",
ConnectionType::INBOUND, ConnectionType::INBOUND,
inbound_onion); inbound_onion,
CNodeOptions{
.permission_flags = permission_flags,
.prefer_evict = discouraged,
});
pnode->AddRef(); pnode->AddRef();
pnode->m_permissionFlags = permissionFlags;
// If this flag is present, the user probably expect that RPC and QT report it as whitelisted (backward compatibility) // If this flag is present, the user probably expect that RPC and QT report it as whitelisted (backward compatibility)
pnode->m_legacyWhitelisted = legacyWhitelisted; pnode->m_legacyWhitelisted = legacyWhitelisted;
pnode->m_prefer_evict = discouraged;
m_msgproc->InitializeNode(*pnode, nodeServices); m_msgproc->InitializeNode(*pnode, nodeServices);
{ {
@ -4043,19 +4045,21 @@ CNode::CNode(NodeId idIn,
const std::string& addrNameIn, const std::string& addrNameIn,
ConnectionType conn_type_in, ConnectionType conn_type_in,
bool inbound_onion, bool inbound_onion,
std::unique_ptr<i2p::sam::Session>&& i2p_sam_session) CNodeOptions&& node_opts)
: m_transport{std::make_unique<V1Transport>(idIn, SER_NETWORK, INIT_PROTO_VERSION)}, : m_transport{std::make_unique<V1Transport>(idIn, SER_NETWORK, INIT_PROTO_VERSION)},
m_permission_flags{node_opts.permission_flags},
m_sock{sock}, m_sock{sock},
m_connected{GetTime<std::chrono::seconds>()}, m_connected{GetTime<std::chrono::seconds>()},
addr{addrIn}, addr{addrIn},
addrBind{addrBindIn}, addrBind{addrBindIn},
m_addr_name{addrNameIn.empty() ? addr.ToStringAddrPort() : addrNameIn}, m_addr_name{addrNameIn.empty() ? addr.ToStringAddrPort() : addrNameIn},
m_inbound_onion{inbound_onion}, m_inbound_onion{inbound_onion},
m_prefer_evict{node_opts.prefer_evict},
nKeyedNetGroup{nKeyedNetGroupIn}, nKeyedNetGroup{nKeyedNetGroupIn},
id{idIn}, id{idIn},
nLocalHostNonce{nLocalHostNonceIn}, nLocalHostNonce{nLocalHostNonceIn},
m_conn_type{conn_type_in}, m_conn_type{conn_type_in},
m_i2p_sam_session{std::move(i2p_sam_session)} m_i2p_sam_session{std::move(node_opts.i2p_sam_session)}
{ {
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND); if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);

View File

@ -242,7 +242,7 @@ public:
mapMsgTypeSize mapSendBytesPerMsgType; mapMsgTypeSize mapSendBytesPerMsgType;
uint64_t nRecvBytes; uint64_t nRecvBytes;
mapMsgTypeSize mapRecvBytesPerMsgType; mapMsgTypeSize mapRecvBytesPerMsgType;
NetPermissionFlags m_permissionFlags; NetPermissionFlags m_permission_flags;
bool m_legacyWhitelisted; bool m_legacyWhitelisted;
std::chrono::microseconds m_last_ping_time; std::chrono::microseconds m_last_ping_time;
std::chrono::microseconds m_min_ping_time; std::chrono::microseconds m_min_ping_time;
@ -449,6 +449,13 @@ public:
size_t GetSendMemoryUsage() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex); size_t GetSendMemoryUsage() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex);
}; };
struct CNodeOptions
{
NetPermissionFlags permission_flags = NetPermissionFlags::None;
std::unique_ptr<i2p::sam::Session> i2p_sam_session = nullptr;
bool prefer_evict = false;
};
/** Information about a peer */ /** Information about a peer */
class CNode class CNode
{ {
@ -460,7 +467,7 @@ public:
* the sending side functions are only called under cs_vSend. */ * the sending side functions are only called under cs_vSend. */
const std::unique_ptr<Transport> m_transport; const std::unique_ptr<Transport> m_transport;
NetPermissionFlags m_permissionFlags{NetPermissionFlags::None}; // treated as const outside of fuzz tester const NetPermissionFlags m_permission_flags;
/** /**
* Socket used for communication with the node. * Socket used for communication with the node.
@ -512,9 +519,9 @@ public:
* from the wire. This cleaned string can safely be logged or displayed. * from the wire. This cleaned string can safely be logged or displayed.
*/ */
std::string cleanSubVer GUARDED_BY(m_subver_mutex){}; std::string cleanSubVer GUARDED_BY(m_subver_mutex){};
bool m_prefer_evict{false}; // This peer is preferred for eviction. (treated as const) const bool m_prefer_evict{false}; // This peer is preferred for eviction.
bool HasPermission(NetPermissionFlags permission) const { bool HasPermission(NetPermissionFlags permission) const {
return NetPermissions::HasFlag(m_permissionFlags, permission); return NetPermissions::HasFlag(m_permission_flags, permission);
} }
// This boolean is unusued in actual processing, only present for backward compatibility at RPC/QT level // This boolean is unusued in actual processing, only present for backward compatibility at RPC/QT level
bool m_legacyWhitelisted{false}; bool m_legacyWhitelisted{false};
@ -669,7 +676,7 @@ public:
const std::string &addrNameIn, const std::string &addrNameIn,
ConnectionType conn_type_in, ConnectionType conn_type_in,
bool inbound_onion, bool inbound_onion,
std::unique_ptr<i2p::sam::Session>&& i2p_sam_session = nullptr); CNodeOptions&& node_opts = {});
CNode(const CNode&) = delete; CNode(const CNode&) = delete;
CNode& operator=(const CNode&) = delete; CNode& operator=(const CNode&) = delete;
@ -1275,12 +1282,12 @@ private:
* Create a `CNode` object from a socket that has just been accepted and add the node to * Create a `CNode` object from a socket that has just been accepted and add the node to
* the `m_nodes` member. * the `m_nodes` member.
* @param[in] sock Connected socket to communicate with the peer. * @param[in] sock Connected socket to communicate with the peer.
* @param[in] permissionFlags The peer's permissions. * @param[in] permission_flags The peer's permissions.
* @param[in] addr_bind The address and port at our side of the connection. * @param[in] addr_bind The address and port at our side of the connection.
* @param[in] addr The address and port at the peer's side of the connection. * @param[in] addr The address and port at the peer's side of the connection.
*/ */
void CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock, void CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
NetPermissionFlags permissionFlags, NetPermissionFlags permission_flags,
const CAddress& addr_bind, const CAddress& addr_bind,
const CAddress& addr, const CAddress& addr,
CMasternodeSync& mn_sync) EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc); CMasternodeSync& mn_sync) EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);

View File

@ -1234,11 +1234,11 @@ void RPCConsole::updateDetailWidget()
ui->peerSubversion->setText(QString::fromStdString(stats->nodeStats.cleanSubVer)); ui->peerSubversion->setText(QString::fromStdString(stats->nodeStats.cleanSubVer));
ui->peerConnectionType->setText(GUIUtil::ConnectionTypeToQString(stats->nodeStats.m_conn_type, /* prepend_direction */ true)); ui->peerConnectionType->setText(GUIUtil::ConnectionTypeToQString(stats->nodeStats.m_conn_type, /* prepend_direction */ true));
ui->peerNetwork->setText(GUIUtil::NetworkToQString(stats->nodeStats.m_network)); ui->peerNetwork->setText(GUIUtil::NetworkToQString(stats->nodeStats.m_network));
if (stats->nodeStats.m_permissionFlags == NetPermissionFlags::None) { if (stats->nodeStats.m_permission_flags == NetPermissionFlags::None) {
ui->peerPermissions->setText(ts.na); ui->peerPermissions->setText(ts.na);
} else { } else {
QStringList permissions; QStringList permissions;
for (const auto& permission : NetPermissions::ToStrings(stats->nodeStats.m_permissionFlags)) { for (const auto& permission : NetPermissions::ToStrings(stats->nodeStats.m_permission_flags)) {
permissions.append(QString::fromStdString(permission)); permissions.append(QString::fromStdString(permission));
} }
ui->peerPermissions->setText(permissions.join(" & ")); ui->peerPermissions->setText(permissions.join(" & "));

View File

@ -260,7 +260,7 @@ static RPCHelpMan getpeerinfo()
obj.pushKV("whitelisted", stats.m_legacyWhitelisted); obj.pushKV("whitelisted", stats.m_legacyWhitelisted);
} }
UniValue permissions(UniValue::VARR); UniValue permissions(UniValue::VARR);
for (const auto& permission : NetPermissions::ToStrings(stats.m_permissionFlags)) { for (const auto& permission : NetPermissions::ToStrings(stats.m_permission_flags)) {
permissions.push_back(permission); permissions.push_back(permission);
} }
obj.pushKV("permissions", permissions); obj.pushKV("permissions", permissions);

View File

@ -71,7 +71,6 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
/*successfully_connected=*/true, /*successfully_connected=*/true,
/*remote_services=*/ServiceFlags(NODE_NETWORK), /*remote_services=*/ServiceFlags(NODE_NETWORK),
/*local_services=*/ServiceFlags(NODE_NETWORK), /*local_services=*/ServiceFlags(NODE_NETWORK),
/*permission_flags=*/NetPermissionFlags::None,
/*version=*/PROTOCOL_VERSION, /*version=*/PROTOCOL_VERSION,
/*relay_txs=*/true); /*relay_txs=*/true);
TestOnlyResetTimeData(); TestOnlyResetTimeData();

View File

@ -288,7 +288,6 @@ void FillNode(FuzzedDataProvider& fuzzed_data_provider, ConnmanTestMsg& connman,
/*successfully_connected=*/fuzzed_data_provider.ConsumeBool(), /*successfully_connected=*/fuzzed_data_provider.ConsumeBool(),
/*remote_services=*/ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS), /*remote_services=*/ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS),
/*local_services=*/ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS), /*local_services=*/ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS),
/*permission_flags=*/ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS),
/*version=*/fuzzed_data_provider.ConsumeIntegralInRange<int32_t>(MIN_PEER_PROTO_VERSION, std::numeric_limits<int32_t>::max()), /*version=*/fuzzed_data_provider.ConsumeIntegralInRange<int32_t>(MIN_PEER_PROTO_VERSION, std::numeric_limits<int32_t>::max()),
/*relay_txs=*/fuzzed_data_provider.ConsumeBool()); /*relay_txs=*/fuzzed_data_provider.ConsumeBool());
} }

View File

@ -366,6 +366,7 @@ auto ConsumeNode(FuzzedDataProvider& fuzzed_data_provider, const std::optional<N
const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray(ALL_CONNECTION_TYPES); const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray(ALL_CONNECTION_TYPES);
const bool inbound_onion{conn_type == ConnectionType::INBOUND ? fuzzed_data_provider.ConsumeBool() : false}; const bool inbound_onion{conn_type == ConnectionType::INBOUND ? fuzzed_data_provider.ConsumeBool() : false};
NetPermissionFlags permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
if constexpr (ReturnUniquePtr) { if constexpr (ReturnUniquePtr) {
return std::make_unique<CNode>(node_id, return std::make_unique<CNode>(node_id,
sock, sock,
@ -375,7 +376,8 @@ auto ConsumeNode(FuzzedDataProvider& fuzzed_data_provider, const std::optional<N
addr_bind, addr_bind,
addr_name, addr_name,
conn_type, conn_type,
inbound_onion); inbound_onion,
CNodeOptions{ .permission_flags = permission_flags });
} else { } else {
return CNode{node_id, return CNode{node_id,
sock, sock,
@ -385,7 +387,8 @@ auto ConsumeNode(FuzzedDataProvider& fuzzed_data_provider, const std::optional<N
addr_bind, addr_bind,
addr_name, addr_name,
conn_type, conn_type,
inbound_onion}; inbound_onion,
CNodeOptions{ .permission_flags = permission_flags }};
} }
} }
inline std::unique_ptr<CNode> ConsumeNodeAsUniquePtr(FuzzedDataProvider& fdp, const std::optional<NodeId>& node_id_in = std::nullopt) { return ConsumeNode<true>(fdp, node_id_in); } inline std::unique_ptr<CNode> ConsumeNodeAsUniquePtr(FuzzedDataProvider& fdp, const std::optional<NodeId>& node_id_in = std::nullopt) { return ConsumeNode<true>(fdp, node_id_in); }

View File

@ -17,7 +17,6 @@ void ConnmanTestMsg::Handshake(CNode& node,
bool successfully_connected, bool successfully_connected,
ServiceFlags remote_services, ServiceFlags remote_services,
ServiceFlags local_services, ServiceFlags local_services,
NetPermissionFlags permission_flags,
int32_t version, int32_t version,
bool relay_txs) bool relay_txs)
{ {
@ -55,7 +54,6 @@ void ConnmanTestMsg::Handshake(CNode& node,
assert(peerman.GetNodeStateStats(node.GetId(), statestats)); assert(peerman.GetNodeStateStats(node.GetId(), statestats));
assert(statestats.m_relay_txs == (relay_txs && !node.IsBlockOnlyConn())); assert(statestats.m_relay_txs == (relay_txs && !node.IsBlockOnlyConn()));
assert(statestats.their_services == remote_services); assert(statestats.their_services == remote_services);
node.m_permissionFlags = permission_flags;
if (successfully_connected) { if (successfully_connected) {
CSerializedNetMsg msg_verack{mm.Make(NetMsgType::VERACK)}; CSerializedNetMsg msg_verack{mm.Make(NetMsgType::VERACK)};
(void)connman.ReceiveMsgFrom(node, std::move(msg_verack)); (void)connman.ReceiveMsgFrom(node, std::move(msg_verack));

View File

@ -43,7 +43,6 @@ struct ConnmanTestMsg : public CConnman {
bool successfully_connected, bool successfully_connected,
ServiceFlags remote_services, ServiceFlags remote_services,
ServiceFlags local_services, ServiceFlags local_services,
NetPermissionFlags permission_flags,
int32_t version, int32_t version,
bool relay_txs) bool relay_txs)
EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex); EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex);