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

Fix AddRef() usage #924

Merged
merged 2 commits into from
Jul 30, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 deletions src/darksend-relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ void CDarkSendRelay::RelayThroughNode(int nRank)

if(pmn != NULL){
//printf("RelayThroughNode %s\n", pmn->addr.ToString().c_str());
CNode* pnode = ConnectNode((CAddress)pmn->addr, NULL, false);
if(pnode){
CNode* pnode = ConnectNode((CAddress)pmn->addr, NULL);
if(pnode) {
//printf("Connected\n");
pnode->PushMessage("dsr", (*this));
pnode->Release();
return;
}
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,10 @@ void CMasternodeMan::ProcessMasternodeConnections()

LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes) {
if(pnode->fDarkSendMaster){
if(pnode->fDarkSendMaster) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldnt it be "privatesend" while you are at it? :-)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 👍 Actually shouldn't mention any specifics at all and usage was not described correctly imo (was "where" to use rather then "when"/"why") - fixed var names and comments.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it ❤️

if(darkSendPool.pSubmittedToMasternode != NULL && pnode->addr == darkSendPool.pSubmittedToMasternode->addr) continue;
LogPrintf("Closing Masternode connection %s \n", pnode->addr.ToString());
pnode->fDarkSendMaster = false;
pnode->Release();
pnode->fDisconnect = true;
}
}
}
Expand Down
32 changes: 20 additions & 12 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,21 +390,24 @@ CNode* FindNode(const CService& addr)
return NULL;
}

CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool darkSendMaster)
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fDarkSendMaster)
{
if (pszDest == NULL) {
// we clean masternode connections in CMasternodeMan::ProcessMasternodeConnections()
// so should be safe to skip this and connect to local Hot MN on CActiveMasternode::ManageStatus()
if (IsLocal(addrConnect) && !darkSendMaster)
if (IsLocal(addrConnect) && !fDarkSendMaster)
return NULL;

// Look for an existing connection
CNode* pnode = FindNode((CService)addrConnect);
if (pnode)
{
pnode->fDarkSendMaster = darkSendMaster;

pnode->AddRef();
// previous connection was not for mixing,
// change flag and add reference so that we can correctly clear it later
if(fDarkSendMaster && !pnode->fDarkSendMaster) {
pnode->fDarkSendMaster = true;
pnode->AddRef();
}
return pnode;
}
}
Expand All @@ -429,16 +432,18 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool darkSendMaste
addrman.Attempt(addrConnect);

// Add node
CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false);
pnode->AddRef();
CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false, true);

{
LOCK(cs_vNodes);
vNodes.push_back(pnode);
}

pnode->nTimeConnected = GetTime();
if(darkSendMaster) pnode->fDarkSendMaster = true;
if(fDarkSendMaster) {
pnode->fDarkSendMaster = true;
pnode->AddRef();
}

return pnode;
} else if (!proxyConnectionFailed) {
Expand Down Expand Up @@ -1025,7 +1030,6 @@ static void AcceptConnection(const ListenSocket& hListenSocket) {
}

CNode* pnode = new CNode(hSocket, addr, "", true);
pnode->AddRef();
pnode->fWhitelisted = whitelisted;

LogPrint("net", "connection from %s accepted\n", addr.ToString());
Expand Down Expand Up @@ -1065,6 +1069,8 @@ void ThreadSocketHandler()
// hold in disconnected pool until all refs are released
if (pnode->fNetworkNode || pnode->fInbound)
pnode->Release();
if (pnode->fDarkSendMaster)
pnode->Release();
vNodesDisconnected.push_back(pnode);
}
}
Expand Down Expand Up @@ -1715,7 +1721,6 @@ bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOu
return false;
if (grantOutbound)
grantOutbound->MoveTo(pnode->grantOutbound);
pnode->fNetworkNode = true;
if (fOneShot)
pnode->fOneShot = true;

Expand Down Expand Up @@ -2377,7 +2382,7 @@ bool CAddrDB::Read(CAddrMan& addr)
unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER); }
unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER); }

CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNameIn, bool fInboundIn) :
CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNameIn, bool fInboundIn, bool fNetworkNodeIn) :
ssSend(SER_NETWORK, INIT_PROTO_VERSION),
addrKnown(5000, 0.001),
filterInventoryKnown(50000, 0.000001)
Expand All @@ -2399,7 +2404,7 @@ CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNa
fOneShot = false;
fClient = false; // set by version message
fInbound = fInboundIn;
fNetworkNode = false;
fNetworkNode = fNetworkNodeIn;
fSuccessfullyConnected = false;
fDisconnect = false;
nRefCount = 0;
Expand All @@ -2426,6 +2431,9 @@ CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNa
id = nLastNodeId++;
}

if(fNetworkNode || fInbound)
AddRef();

if (fLogIPs)
LogPrint("net", "Added connection to %s peer=%d\n", addrName, id);
else
Expand Down
4 changes: 2 additions & 2 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ CNode* FindNode(const CNetAddr& ip);
CNode* FindNode(const CSubNet& subNet);
CNode* FindNode(const std::string& addrName);
CNode* FindNode(const CService& ip);
CNode* ConnectNode(CAddress addrConnect, const char *pszDest = NULL, bool darkSendMaster=false);
CNode* ConnectNode(CAddress addrConnect, const char *pszDest = NULL, bool fDarkSendMaster=false);
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
void MapPort(bool fUseUPnP);
unsigned short GetListenPort();
Expand Down Expand Up @@ -419,7 +419,7 @@ class CNode
// Whether a ping is requested.
bool fPingQueued;

CNode(SOCKET hSocketIn, const CAddress &addrIn, const std::string &addrNameIn = "", bool fInboundIn = false);
CNode(SOCKET hSocketIn, const CAddress &addrIn, const std::string &addrNameIn = "", bool fInboundIn = false, bool fNetworkNodeIn = false);
~CNode();

private:
Expand Down
5 changes: 2 additions & 3 deletions src/rpcmasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ UniValue masternode(const UniValue& params, bool fHelp)

CService addr = CService(strAddress);

CNode *pnode = ConnectNode((CAddress)addr, NULL, false);
if(pnode){
pnode->Release();
CNode *pnode = ConnectNode((CAddress)addr, NULL);
if(pnode) {
return "successfully connected";
} else {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Error connecting");
Expand Down