0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-02 09:46:52 -05:00

[net/refactor] Remove fInbound flag from CNode

This commit is contained in:
Amiti Uttarwar 2020-07-28 13:39:38 -07:00
parent 7b322df629
commit 60156f5fc4
4 changed files with 29 additions and 27 deletions

View file

@ -346,7 +346,7 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : vNodes) {
if (!pnode->fSuccessfullyConnected && !pnode->fInbound && pnode->GetLocalNonce() == nonce) if (!pnode->fSuccessfullyConnected && !pnode->IsInboundConn() && pnode->GetLocalNonce() == nonce)
return false; return false;
} }
return true; return true;
@ -538,7 +538,7 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
LOCK(cs_SubVer); LOCK(cs_SubVer);
X(cleanSubVer); X(cleanSubVer);
} }
X(fInbound); stats.fInbound = IsInboundConn();
stats.m_manual_connection = IsManualConn(); stats.m_manual_connection = IsManualConn();
X(nStartingHeight); X(nStartingHeight);
{ {
@ -874,7 +874,7 @@ bool CConnman::AttemptToEvictConnection()
for (const CNode* node : vNodes) { for (const CNode* node : vNodes) {
if (node->HasPermission(PF_NOBAN)) if (node->HasPermission(PF_NOBAN))
continue; continue;
if (!node->fInbound) if (!node->IsInboundConn())
continue; continue;
if (node->fDisconnect) if (node->fDisconnect)
continue; continue;
@ -985,7 +985,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : vNodes) {
if (pnode->fInbound) nInbound++; if (pnode->IsInboundConn()) nInbound++;
} }
} }
@ -1648,7 +1648,7 @@ void CConnman::ThreadDNSAddressSeed()
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : vNodes) {
nRelevant += pnode->fSuccessfullyConnected && !pnode->IsFeelerConn() && !pnode->IsAddrFetchConn() && !pnode->IsManualConn() && !pnode->fInbound; nRelevant += pnode->fSuccessfullyConnected && !pnode->IsFeelerConn() && !pnode->IsAddrFetchConn() && !pnode->IsManualConn() && !pnode->IsInboundConn();
} }
} }
if (nRelevant >= 2) { if (nRelevant >= 2) {
@ -1758,7 +1758,7 @@ int CConnman::GetExtraOutboundCount()
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : vNodes) {
if (!pnode->fInbound && !pnode->IsManualConn() && !pnode->IsFeelerConn() && !pnode->fDisconnect && !pnode->IsAddrFetchConn() && pnode->fSuccessfullyConnected) { if (!pnode->IsInboundConn() && !pnode->IsManualConn() && !pnode->IsFeelerConn() && !pnode->fDisconnect && !pnode->IsAddrFetchConn() && pnode->fSuccessfullyConnected) {
++nOutbound; ++nOutbound;
} }
} }
@ -1832,7 +1832,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : vNodes) {
if (!pnode->fInbound && (pnode->m_conn_type != ConnectionType::MANUAL)) { if (!pnode->IsInboundConn() && (pnode->m_conn_type != ConnectionType::MANUAL)) {
// Netgroups for inbound and addnode peers are not excluded because our goal here // Netgroups for inbound and addnode peers are not excluded because our goal here
// is to not use multiple of our limited outbound slots on a single netgroup // is to not use multiple of our limited outbound slots on a single netgroup
// but inbound and addnode peers do not use our outbound slots. Inbound peers // but inbound and addnode peers do not use our outbound slots. Inbound peers
@ -1972,11 +1972,11 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : vNodes) {
if (pnode->addr.IsValid()) { if (pnode->addr.IsValid()) {
mapConnected[pnode->addr] = pnode->fInbound; mapConnected[pnode->addr] = pnode->IsInboundConn();
} }
std::string addrName = pnode->GetAddrName(); std::string addrName = pnode->GetAddrName();
if (!addrName.empty()) { if (!addrName.empty()) {
mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr)); mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->IsInboundConn(), static_cast<const CService&>(pnode->addr));
} }
} }
} }
@ -2551,7 +2551,7 @@ size_t CConnman::GetNodeCount(NumConnections flags)
int nNum = 0; int nNum = 0;
for (const auto& pnode : vNodes) { for (const auto& pnode : vNodes) {
if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) { if (flags & (pnode->IsInboundConn() ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
nNum++; nNum++;
} }
} }
@ -2739,7 +2739,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
: nTimeConnected(GetSystemTimeInSeconds()), : nTimeConnected(GetSystemTimeInSeconds()),
addr(addrIn), addr(addrIn),
addrBind(addrBindIn), addrBind(addrBindIn),
fInbound(conn_type_in == ConnectionType::INBOUND),
nKeyedNetGroup(nKeyedNetGroupIn), nKeyedNetGroup(nKeyedNetGroupIn),
// Don't relay addr messages to peers that we connect to as block-relay-only // Don't relay addr messages to peers that we connect to as block-relay-only
// peers (to prevent adversaries from inferring these links from addr // peers (to prevent adversaries from inferring these links from addr

View file

@ -777,7 +777,6 @@ public:
bool m_legacyWhitelisted{false}; bool m_legacyWhitelisted{false};
bool fClient{false}; // set by version message bool fClient{false}; // set by version message
bool m_limited_node{false}; //after BIP159, set by version message bool m_limited_node{false}; //after BIP159, set by version message
const bool fInbound;
std::atomic_bool fSuccessfullyConnected{false}; std::atomic_bool fSuccessfullyConnected{false};
// Setting fDisconnect to true will cause the node to be disconnected the // Setting fDisconnect to true will cause the node to be disconnected the
// next time DisconnectNodes() runs // next time DisconnectNodes() runs
@ -802,6 +801,10 @@ public:
return m_conn_type == ConnectionType::ADDR_FETCH; return m_conn_type == ConnectionType::ADDR_FETCH;
} }
bool IsInboundConn() const {
return m_conn_type == ConnectionType::INBOUND;
}
protected: protected:
mapMsgCmdSize mapSendBytesPerMsgCmd; mapMsgCmdSize mapSendBytesPerMsgCmd;
mapMsgCmdSize mapRecvBytesPerMsgCmd GUARDED_BY(cs_vRecv); mapMsgCmdSize mapRecvBytesPerMsgCmd GUARDED_BY(cs_vRecv);

View file

@ -473,7 +473,7 @@ static void UpdatePreferredDownload(const CNode& node, CNodeState* state) EXCLUS
nPreferredDownload -= state->fPreferredDownload; nPreferredDownload -= state->fPreferredDownload;
// Whether this node should be marked as a preferred download node. // Whether this node should be marked as a preferred download node.
state->fPreferredDownload = (!node.fInbound || node.HasPermission(PF_NOBAN)) && !node.IsAddrFetchConn() && !node.fClient; state->fPreferredDownload = (!node.IsInboundConn() || node.HasPermission(PF_NOBAN)) && !node.IsAddrFetchConn() && !node.fClient;
nPreferredDownload += state->fPreferredDownload; nPreferredDownload += state->fPreferredDownload;
} }
@ -829,7 +829,7 @@ void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
static bool IsOutboundDisconnectionCandidate(const CNode& node) static bool IsOutboundDisconnectionCandidate(const CNode& node)
{ {
return !(node.fInbound || node.IsManualConn() || node.IsFeelerConn() || node.IsAddrFetchConn()); return !(node.IsInboundConn() || node.IsManualConn() || node.IsFeelerConn() || node.IsAddrFetchConn());
} }
void PeerLogicValidation::InitializeNode(CNode *pnode) { void PeerLogicValidation::InitializeNode(CNode *pnode) {
@ -838,9 +838,9 @@ void PeerLogicValidation::InitializeNode(CNode *pnode) {
NodeId nodeid = pnode->GetId(); NodeId nodeid = pnode->GetId();
{ {
LOCK(cs_main); LOCK(cs_main);
mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName), pnode->fInbound, pnode->IsManualConn())); mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName), pnode->IsInboundConn(), pnode->IsManualConn()));
} }
if(!pnode->fInbound) if(!pnode->IsInboundConn())
PushNodeVersion(*pnode, *connman, GetTime()); PushNodeVersion(*pnode, *connman, GetTime());
} }
@ -2320,11 +2320,11 @@ void ProcessMessage(
vRecv >> nVersion >> nServiceInt >> nTime >> addrMe; vRecv >> nVersion >> nServiceInt >> nTime >> addrMe;
nSendVersion = std::min(nVersion, PROTOCOL_VERSION); nSendVersion = std::min(nVersion, PROTOCOL_VERSION);
nServices = ServiceFlags(nServiceInt); nServices = ServiceFlags(nServiceInt);
if (!pfrom.fInbound) if (!pfrom.IsInboundConn())
{ {
connman.SetServices(pfrom.addr, nServices); connman.SetServices(pfrom.addr, nServices);
} }
if (!pfrom.fInbound && !pfrom.IsFeelerConn() && !pfrom.IsManualConn() && !HasAllDesirableServiceFlags(nServices)) if (!pfrom.IsInboundConn() && !pfrom.IsFeelerConn() && !pfrom.IsManualConn() && !HasAllDesirableServiceFlags(nServices))
{ {
LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom.GetId(), nServices, GetDesirableServiceFlags(nServices)); LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom.GetId(), nServices, GetDesirableServiceFlags(nServices));
pfrom.fDisconnect = true; pfrom.fDisconnect = true;
@ -2351,20 +2351,20 @@ void ProcessMessage(
if (!vRecv.empty()) if (!vRecv.empty())
vRecv >> fRelay; vRecv >> fRelay;
// Disconnect if we connected to ourself // Disconnect if we connected to ourself
if (pfrom.fInbound && !connman.CheckIncomingNonce(nNonce)) if (pfrom.IsInboundConn() && !connman.CheckIncomingNonce(nNonce))
{ {
LogPrintf("connected to self at %s, disconnecting\n", pfrom.addr.ToString()); LogPrintf("connected to self at %s, disconnecting\n", pfrom.addr.ToString());
pfrom.fDisconnect = true; pfrom.fDisconnect = true;
return; return;
} }
if (pfrom.fInbound && addrMe.IsRoutable()) if (pfrom.IsInboundConn() && addrMe.IsRoutable())
{ {
SeenLocal(addrMe); SeenLocal(addrMe);
} }
// Be shy and don't send version until we hear // Be shy and don't send version until we hear
if (pfrom.fInbound) if (pfrom.IsInboundConn())
PushNodeVersion(pfrom, connman, GetAdjustedTime()); PushNodeVersion(pfrom, connman, GetAdjustedTime());
if (nVersion >= WTXID_RELAY_VERSION) { if (nVersion >= WTXID_RELAY_VERSION) {
@ -2408,7 +2408,7 @@ void ProcessMessage(
UpdatePreferredDownload(pfrom, State(pfrom.GetId())); UpdatePreferredDownload(pfrom, State(pfrom.GetId()));
} }
if (!pfrom.fInbound && pfrom.IsAddrRelayPeer()) if (!pfrom.IsInboundConn() && pfrom.IsAddrRelayPeer())
{ {
// Advertise our address // Advertise our address
if (fListen && !::ChainstateActive().IsInitialBlockDownload()) if (fListen && !::ChainstateActive().IsInitialBlockDownload())
@ -2472,7 +2472,7 @@ void ProcessMessage(
{ {
pfrom.SetRecvVersion(std::min(pfrom.nVersion.load(), PROTOCOL_VERSION)); pfrom.SetRecvVersion(std::min(pfrom.nVersion.load(), PROTOCOL_VERSION));
if (!pfrom.fInbound) { if (!pfrom.IsInboundConn()) {
// Mark this node as currently connected, so we update its timestamp later. // Mark this node as currently connected, so we update its timestamp later.
LOCK(cs_main); LOCK(cs_main);
State(pfrom.GetId())->fCurrentlyConnected = true; State(pfrom.GetId())->fCurrentlyConnected = true;
@ -3452,7 +3452,7 @@ void ProcessMessage(
// to users' AddrMan and later request them by sending getaddr messages. // to users' AddrMan and later request them by sending getaddr messages.
// Making nodes which are behind NAT and can only make outgoing connections ignore // Making nodes which are behind NAT and can only make outgoing connections ignore
// the getaddr message mitigates the attack. // the getaddr message mitigates the attack.
if (!pfrom.fInbound) { if (!pfrom.IsInboundConn()) {
LogPrint(BCLog::NET, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom.GetId()); LogPrint(BCLog::NET, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom.GetId());
return; return;
} }
@ -4279,7 +4279,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
bool fSendTrickle = pto->HasPermission(PF_NOBAN); bool fSendTrickle = pto->HasPermission(PF_NOBAN);
if (pto->m_tx_relay->nNextInvSend < current_time) { if (pto->m_tx_relay->nNextInvSend < current_time) {
fSendTrickle = true; fSendTrickle = true;
if (pto->fInbound) { if (pto->IsInboundConn()) {
pto->m_tx_relay->nNextInvSend = std::chrono::microseconds{connman->PoissonNextSendInbound(nNow, INVENTORY_BROADCAST_INTERVAL)}; pto->m_tx_relay->nNextInvSend = std::chrono::microseconds{connman->PoissonNextSendInbound(nNow, INVENTORY_BROADCAST_INTERVAL)};
} else { } else {
// Use half the delay for outbound peers, as there is less privacy concern for them. // Use half the delay for outbound peers, as there is less privacy concern for them.

View file

@ -182,10 +182,10 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test)
std::string pszDest; std::string pszDest;
std::unique_ptr<CNode> pnode1 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, ConnectionType::OUTBOUND); std::unique_ptr<CNode> pnode1 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, ConnectionType::OUTBOUND);
BOOST_CHECK(pnode1->fInbound == false); BOOST_CHECK(pnode1->IsInboundConn() == false);
std::unique_ptr<CNode> pnode2 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, ConnectionType::INBOUND); std::unique_ptr<CNode> pnode2 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, ConnectionType::INBOUND);
BOOST_CHECK(pnode2->fInbound == true); BOOST_CHECK(pnode2->IsInboundConn() == true);
} }
// prior to PR #14728, this test triggers an undefined behavior // prior to PR #14728, this test triggers an undefined behavior