Skip to content

Commit

Permalink
Move CNode::addrName accesses behind locked accessors
Browse files Browse the repository at this point in the history
zcash: cherry picked from commit 036073b
zcash: bitcoin/bitcoin#9708
  • Loading branch information
TheBlueMatt authored and str4d committed Apr 1, 2021
1 parent e6eac19 commit 116deee
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ int64_t GetBlockTimeout(int64_t nTime, int nValidatedQueuedBefore, const Consens
void InitializeNode(NodeId nodeid, const CNode *pnode) {
LOCK(cs_main);
CNodeState &state = mapNodeState.insert(std::make_pair(nodeid, CNodeState())).first->second;
state.name = pnode->addrName;
state.name = pnode->GetAddrName();
state.address = pnode->addr;
}

Expand Down
21 changes: 18 additions & 3 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,11 @@ CNode* FindNode(const CSubNet& subNet)
CNode* FindNode(const std::string& addrName)
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes)
if (pnode->addrName == addrName)
for (CNode* pnode : vNodes) {
if (pnode->GetAddrName() == addrName) {
return (pnode);
}
}
return NULL;
}

Expand Down Expand Up @@ -637,6 +639,19 @@ void CNode::AddWhitelistedRange(const CSubNet &subnet) {
vWhitelistedRange.push_back(subnet);
}


std::string CNode::GetAddrName() const {
LOCK(cs_addrName);
return addrName;
}

void CNode::MaybeSetAddrName(const std::string& addrNameIn) {
LOCK(cs_addrName);
if (addrName.empty()) {
addrName = addrNameIn;
}
}

void CNode::copyStats(CNodeStats &stats)
{
stats.nodeid = this->GetId();
Expand All @@ -649,7 +664,7 @@ void CNode::copyStats(CNodeStats &stats)
stats.nLastRecv = nLastRecv;
stats.nTimeConnected = nTimeConnected;
stats.nTimeOffset = nTimeOffset;
stats.addrName = addrName;
stats.addrName = GetAddrName();
stats.nVersion = nVersion;
{
LOCK(cs_SubVer);
Expand Down
7 changes: 6 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ class CNode
const int64_t nTimeConnected;
std::atomic<int64_t> nTimeOffset;
const CAddress addr;
std::string addrName;
CService addrLocal;
int nVersion;
// strSubVer is whatever byte array we read from the wire. However, this field is intended
Expand Down Expand Up @@ -379,6 +378,9 @@ class CNode

static uint64_t CalculateKeyedNetGroup(const CAddress& ad);


mutable CCriticalSection cs_addrName;
std::string addrName;
public:

// Regenerate the span for this CNode. This re-queries the log filter to see
Expand Down Expand Up @@ -703,6 +705,9 @@ class CNode
//!response the time in seconds left in the current max outbound cycle
// in case of no limit, it will always respond with 0
static uint64_t GetMaxOutboundTimeLeftInCycle();
std::string GetAddrName() const;
//! Sets the addrName only if it was not previously set
void MaybeSetAddrName(const std::string& addrNameIn);
};


Expand Down

0 comments on commit 116deee

Please sign in to comment.