Skip to content

Commit

Permalink
[Spork] Protect CSporkManager with critical section
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Sep 23, 2019
1 parent c41dd7c commit cd33d3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/spork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,48 +49,58 @@ void CSporkManager::LoadSporksFromDB()

void CSporkManager::ProcessSpork(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
if (fLiteMode) return; // disable all obfuscation/masternode related functionality
if (fLiteMode || chainActive.Tip() == nullptr) return; // disable all obfuscation/masternode related functionality

if (strCommand == "spork") {
//LogPrintf("CSporkManager::spork\n");
CDataStream vMsg(vRecv);

CSporkMessage spork;
vRecv >> spork;

LOCK(cs_main);
if (chainActive.Tip() == NULL) return;

// Ignore spork messages about unknown/deleted sporks
std::string strSpork = sporkManager.GetSporkNameByID(spork.nSporkID);
if (strSpork == "Unknown") return;

uint256 hash = spork.GetHash();
if (mapSporksActive.count(spork.nSporkID)) {
if (mapSporksActive[spork.nSporkID].nTimeSigned >= spork.nTimeSigned) {
if (fDebug) LogPrintf("%s : seen %s block %d \n", __func__, hash.ToString(), chainActive.Tip()->nHeight);
return;
{
LOCK(cs);
if (mapSporksActive.count(spork.nSporkID)) {
// spork is active
if (mapSporksActive[spork.nSporkID].nTimeSigned >= spork.nTimeSigned) {
// spork in memory has been signed more recently
if (fDebug) LogPrintf("%s : seen %s block %d \n", __func__, hash.ToString(), chainActive.Tip()->nHeight);
return;
} else {
// update active spork
if (fDebug) LogPrintf("%s : got updated spork %s block %d \n", __func__, hash.ToString(), chainActive.Tip()->nHeight);
}
} else {
if (fDebug) LogPrintf("%s : got updated spork %s block %d \n", __func__, hash.ToString(), chainActive.Tip()->nHeight);
// spork is not active
if (fDebug) LogPrintf("%s : got new spork %s block %d \n", __func__, hash.ToString(), chainActive.Tip()->nHeight);
}
}

LogPrintf("%s : new %s ID %d Time %d bestHeight %d\n", __func__, hash.ToString(), spork.nSporkID, spork.nValue, chainActive.Tip()->nHeight);

bool fRequireNew = spork.nTimeSigned >= Params().NewSporkStart();
if (!spork.CheckSignature(fRequireNew)) {
LOCK(cs_main);
LogPrintf("%s : Invalid Signature\n", __func__);
Misbehaving(pfrom->GetId(), 100);
return;
}

mapSporks[hash] = spork;
mapSporksActive[spork.nSporkID] = spork;
{
LOCK(cs);
mapSporks[hash] = spork;
mapSporksActive[spork.nSporkID] = spork;
}
spork.Relay();

// PIVX: add to spork database.
pSporkDB->WriteSpork(spork.nSporkID, spork);
}
if (strCommand == "getsporks") {
LOCK(cs);
std::map<int, CSporkMessage>::iterator it = mapSporksActive.begin();

while (it != mapSporksActive.end()) {
Expand All @@ -107,6 +117,7 @@ bool CSporkManager::UpdateSpork(int nSporkID, int64_t nValue)

if(spork.Sign(strMasterPrivKey)){
spork.Relay();
LOCK(cs);
mapSporks[spork.GetHash()] = spork;
mapSporksActive[nSporkID] = spork;
return true;
Expand All @@ -126,6 +137,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);
int64_t r = -1;

if (mapSporksActive.count(nSporkID)) {
Expand Down Expand Up @@ -191,6 +203,7 @@ bool CSporkManager::SetPrivKey(std::string strPrivKey)

const bool fRequireNew = GetTime() >= Params().NewSporkStart();
if (spork.CheckSignature(fRequireNew)) {
LOCK(cs);
// Test signing successful, proceed
LogPrintf("%s : -- Successfully initialized as spork signer\n", __func__);
strMasterPrivKey = strPrivKey;
Expand Down
1 change: 1 addition & 0 deletions src/spork.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class CSporkMessage
class CSporkManager
{
private:
CCriticalSection cs;
std::vector<unsigned char> vchSig;
std::string strMasterPrivKey;
std::map<int, CSporkMessage> mapSporksActive;
Expand Down

0 comments on commit cd33d3c

Please sign in to comment.