Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cleanup] Remove fNetworkNode and pnodeLocalHost, and encapsulate CNode id. #2586

Merged
merged 4 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ bool fListen = true;
RecursiveMutex cs_mapLocalHost;
std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
static bool vfLimited[NET_MAX] = {};
static CNode* pnodeLocalHost = NULL;
std::string strSubVersion;

limitedmap<CInv, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
Expand Down Expand Up @@ -926,7 +925,7 @@ bool CConnman::AttemptToEvictConnection(bool fPreferNewConnection)
continue;
if (node->fDisconnect)
continue;
NodeEvictionCandidate candidate = {node->id, node->nTimeConnected, node->nMinPingUsecTime, node->addr, node->nKeyedNetGroup};
NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime, node->addr, node->nKeyedNetGroup};
vEvictionCandidates.push_back(candidate);
}
}
Expand Down Expand Up @@ -1085,8 +1084,7 @@ void CConnman::ThreadSocketHandler()
pnode->CloseSocketDisconnect();

// hold in disconnected pool until all refs are released
if (pnode->fNetworkNode || pnode->fInbound)
pnode->Release();
pnode->Release();
vNodesDisconnected.push_back(pnode);
}
}
Expand Down Expand Up @@ -1308,7 +1306,7 @@ void CConnman::ThreadSocketHandler()
int64_t nTime = GetSystemTimeInSeconds();
if (nTime - pnode->nTimeConnected > 60) {
if (pnode->nLastRecv == 0 || pnode->nLastSend == 0) {
LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->id);
LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId());
pnode->fDisconnect = true;
} else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL) {
LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
Expand Down Expand Up @@ -1701,7 +1699,6 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
return false;
if (grantOutbound)
grantOutbound->MoveTo(pnode->grantOutbound);
pnode->fNetworkNode = true;
if (fOneShot)
pnode->fOneShot = true;
if (fFeeler)
Expand Down Expand Up @@ -1973,17 +1970,6 @@ bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options c
semOutbound = std::make_unique<CSemaphore>(std::min((nMaxOutbound + nMaxFeeler), nMaxConnections));
}

if (pnodeLocalHost == nullptr) {
CNetAddr local;
LookupHost("127.0.0.1", local, false);

NodeId id = GetNewNodeId();
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();

pnodeLocalHost = new CNode(id, nLocalServices, GetBestHeight(), INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices), 0, nonce);
m_msgproc->InitializeNode(pnodeLocalHost);
}

//
// Start threads
//
Expand Down Expand Up @@ -2098,9 +2084,6 @@ void CConnman::Stop()
vNodesDisconnected.clear();
vhListenSocket.clear();
semOutbound.reset();
if(pnodeLocalHost)
DeleteNode(pnodeLocalHost);
pnodeLocalHost = NULL;
}

void CConnman::DeleteNode(CNode* pnode)
Expand Down Expand Up @@ -2209,7 +2192,7 @@ bool CConnman::DisconnectNode(NodeId id)
{
LOCK(cs_vNodes);
for(CNode* pnode : vNodes) {
if (id == pnode->id) {
if (id == pnode->GetId()) {
pnode->fDisconnect = true;
return true;
}
Expand Down Expand Up @@ -2273,10 +2256,10 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
nTimeConnected(GetSystemTimeInSeconds()),
addr(addrIn),
fInbound(fInboundIn),
id(idIn),
nKeyedNetGroup(nKeyedNetGroupIn),
addrKnown(5000, 0.001),
filterInventoryKnown(50000, 0.000001),
id(idIn),
nLocalHostNonce(nLocalHostNonceIn),
nLocalServices(nLocalServicesIn),
nMyStartingHeight(nMyStartingHeightIn),
Expand All @@ -2298,7 +2281,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
fOneShot = false;
fClient = false; // set by version message
fFeeler = false;
fNetworkNode = false;
fSuccessfullyConnected = false;
fDisconnect = false;
nRefCount = 0;
Expand Down Expand Up @@ -2379,7 +2361,7 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
{
size_t nMessageSize = msg.data.size();
size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE;
LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command.c_str()), nMessageSize, pnode->id);
LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command.c_str()), nMessageSize, pnode->GetId());

std::vector<unsigned char> serializedHeader;
serializedHeader.reserve(CMessageHeader::HEADER_SIZE);
Expand Down Expand Up @@ -2417,7 +2399,7 @@ bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
CNode* found = nullptr;
LOCK(cs_vNodes);
for (auto&& pnode : vNodes) {
if(pnode->id == id) {
if(pnode->GetId() == id) {
found = pnode;
break;
}
Expand Down
3 changes: 1 addition & 2 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ class CNode
bool fOneShot;
bool fClient;
const bool fInbound;
bool fNetworkNode;
/**
* Whether the peer has signaled support for receiving ADDRv2 (BIP155)
* messages, implying a preference to receive ADDRv2 instead of ADDR ones.
Expand All @@ -594,7 +593,6 @@ class CNode
RecursiveMutex cs_filter;
std::unique_ptr<CBloomFilter> pfilter;
std::atomic<int> nRefCount;
const NodeId id;

const uint64_t nKeyedNetGroup;
std::atomic_bool fPauseRecv;
Expand Down Expand Up @@ -657,6 +655,7 @@ class CNode
CNode& operator=(const CNode&) = delete;

private:
const NodeId id;
const uint64_t nLocalHostNonce;
// Services offered to this peer
const ServiceFlags nLocalServices;
Expand Down
Loading