Skip to content

Commit

Permalink
Protect CSporkManager with critical section (#2213)
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock authored and UdjinM6 committed Aug 10, 2018
1 parent 106bab1 commit 075ca09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/spork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ void CSporkManager::ProcessSpork(CNode* pfrom, const std::string& strCommand, CD
if(!chainActive.Tip()) return;
strLogMsg = strprintf("SPORK -- hash: %s id: %d value: %10d bestHeight: %d peer=%d", hash.ToString(), spork.nSporkID, spork.nValue, chainActive.Height(), pfrom->id);
}

if(mapSporksActive.count(spork.nSporkID)) {
if (mapSporksActive[spork.nSporkID].nTimeSigned >= spork.nTimeSigned) {
LogPrint("spork", "%s seen\n", strLogMsg);
return;
{
LOCK(cs); // make sure to not lock this together with cs_main
if (mapSporksActive.count(spork.nSporkID)) {
if (mapSporksActive[spork.nSporkID].nTimeSigned >= spork.nTimeSigned) {
LogPrint("spork", "%s seen\n", strLogMsg);
return;
} else {
LogPrintf("%s updated\n", strLogMsg);
}
} else {
LogPrintf("%s updated\n", strLogMsg);
LogPrintf("%s new\n", strLogMsg);
}
} else {
LogPrintf("%s new\n", strLogMsg);
}

if(!spork.CheckSignature(sporkPubKeyID)) {
Expand All @@ -65,14 +67,18 @@ void CSporkManager::ProcessSpork(CNode* pfrom, const std::string& strCommand, CD
return;
}

mapSporks[hash] = spork;
mapSporksActive[spork.nSporkID] = spork;
{
LOCK(cs); // make sure to not lock this together with cs_main
mapSporks[hash] = spork;
mapSporksActive[spork.nSporkID] = spork;
}
spork.Relay(connman);

//does a task if needed
ExecuteSpork(spork.nSporkID, spork.nValue);

} else if (strCommand == NetMsgType::GETSPORKS) {
LOCK(cs); // make sure to not lock this together with cs_main
for (const auto& pair : mapSporksActive) {
connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::SPORK, pair.second));
}
Expand Down Expand Up @@ -111,11 +117,11 @@ void CSporkManager::ExecuteSpork(int nSporkID, int nValue)

bool CSporkManager::UpdateSpork(int nSporkID, int64_t nValue, CConnman& connman)
{

CSporkMessage spork = CSporkMessage(nSporkID, nValue, GetAdjustedTime());

if(spork.Sign(sporkPrivKey)) {
spork.Relay(connman);
LOCK(cs);
mapSporks[spork.GetHash()] = spork;
mapSporksActive[nSporkID] = spork;
return true;
Expand All @@ -127,6 +133,7 @@ bool CSporkManager::UpdateSpork(int nSporkID, int64_t nValue, CConnman& connman)
// grab the spork, otherwise say it's off
bool CSporkManager::IsSporkActive(int nSporkID)
{
LOCK(cs);
int64_t r = -1;

if(mapSporksActive.count(nSporkID)){
Expand All @@ -144,6 +151,7 @@ bool CSporkManager::IsSporkActive(int nSporkID)
// grab the value of the spork on the network, or the default
int64_t CSporkManager::GetSporkValue(int nSporkID)
{
LOCK(cs);
if (mapSporksActive.count(nSporkID))
return mapSporksActive[nSporkID].nValue;

Expand Down Expand Up @@ -190,6 +198,7 @@ std::string CSporkManager::GetSporkNameByID(int nSporkID)
}

bool CSporkManager::SetSporkAddress(const std::string& strAddress) {
LOCK(cs);
CBitcoinAddress address(strAddress);
if (!address.IsValid() || !address.GetKeyID(sporkPubKeyID)) {
LogPrintf("CSporkManager::SetSporkAddress -- Failed to parse spork address\n");
Expand All @@ -214,6 +223,7 @@ bool CSporkManager::SetPrivKey(const std::string& strPrivKey)

CSporkMessage spork;
if (spork.Sign(key)) {
LOCK(cs);
// Test signing successful, proceed
LogPrintf("CSporkManager::SetPrivKey -- Successfully initialized as spork signer\n");

Expand Down
1 change: 1 addition & 0 deletions src/spork.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class CSporkMessage
class CSporkManager
{
private:
CCriticalSection cs;
std::vector<unsigned char> vchSig;
std::map<int, CSporkMessage> mapSporksActive;

Expand Down

0 comments on commit 075ca09

Please sign in to comment.