Skip to content

Commit 4a67a4b

Browse files
committed
refactored ProcessSpork code into ProcessSporkMsg and ProcessGetSporks.
1 parent db9b093 commit 4a67a4b

File tree

2 files changed

+82
-65
lines changed

2 files changed

+82
-65
lines changed

src/spork.cpp

Lines changed: 77 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -80,81 +80,93 @@ void CSporkManager::ProcessSpork(CNode* pfrom, std::string& strCommand, CDataStr
8080
if (fLiteMode) return; // disable all masternode related functionality
8181

8282
if (strCommand == NetMsgType::SPORK) {
83-
CSporkMessage spork;
84-
vRecv >> spork;
85-
86-
// Ignore spork messages about unknown/deleted sporks
87-
std::string strSpork = sporkManager.GetSporkNameByID(spork.nSporkID);
88-
if (strSpork == "Unknown") return;
89-
90-
// Do not accept sporks signed way too far into the future
91-
if (spork.nTimeSigned > GetAdjustedTime() + 2 * 60 * 60) {
92-
LOCK(cs_main);
93-
LogPrintf("%s : ERROR: too far into the future\n", __func__);
94-
Misbehaving(pfrom->GetId(), 100);
95-
return;
96-
}
83+
ProcessSporkMsg(pfrom, strCommand, vRecv);
84+
}
85+
if (strCommand == NetMsgType::GETSPORKS) {
86+
ProcessGetSporks(pfrom, strCommand, vRecv);
87+
}
88+
}
9789

98-
// reject old signature version
99-
if (spork.nMessVersion != MessageVersion::MESS_VER_HASH) {
100-
LogPrintf("%s : nMessVersion=%d not accepted anymore\n", __func__, spork.nMessVersion);
101-
return;
102-
}
90+
void CSporkManager::ProcessSporkMsg(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
91+
{
92+
CSporkMessage spork;
93+
vRecv >> spork;
94+
95+
// Ignore spork messages about unknown/deleted sporks
96+
std::string strSpork = sporkManager.GetSporkNameByID(spork.nSporkID);
97+
if (strSpork == "Unknown") return;
98+
99+
// Do not accept sporks signed way too far into the future
100+
if (spork.nTimeSigned > GetAdjustedTime() + 2 * 60 * 60) {
101+
LogPrint(BCLog::NET, "%s : ERROR: too far into the future\n", __func__);
102+
LOCK(cs_main);
103+
Misbehaving(pfrom->GetId(), 100);
104+
return;
105+
}
103106

104-
uint256 hash = spork.GetHash();
105-
std::string sporkName = sporkManager.GetSporkNameByID(spork.nSporkID);
106-
{
107-
LOCK(cs);
108-
if (mapSporksActive.count(spork.nSporkID)) {
109-
// spork is active
110-
if (mapSporksActive[spork.nSporkID].nTimeSigned >= spork.nTimeSigned) {
111-
// spork in memory has been signed more recently
112-
LogPrintf("%s : spork %d (%s) in memory is more recent: %d >= %d\n", __func__,
113-
spork.nSporkID, sporkName,
114-
mapSporksActive[spork.nSporkID].nTimeSigned, spork.nTimeSigned);
115-
return;
116-
} else {
117-
// update active spork
118-
LogPrintf("%s : got updated spork %d (%s) with value %d (signed at %d) \n", __func__,
119-
spork.nSporkID, sporkName, spork.nValue, spork.nTimeSigned);
120-
}
121-
} else {
122-
// spork is not active
123-
LogPrintf("%s : got new spork %d (%s) with value %d (signed at %d) \n", __func__,
124-
spork.nSporkID, sporkName, spork.nValue, spork.nTimeSigned);
125-
}
126-
}
107+
// reject old signature version
108+
if (spork.nMessVersion != MessageVersion::MESS_VER_HASH) {
109+
LogPrint(BCLog::NET, "%s : nMessVersion=%d not accepted anymore\n", __func__, spork.nMessVersion);
110+
return;
111+
}
127112

128-
const bool fRequireNew = spork.nTimeSigned >= Params().GetConsensus().nTime_EnforceNewSporkKey;
129-
bool fValidSig = spork.CheckSignature();
130-
if (!fValidSig && !fRequireNew) {
131-
// See if window is open that allows for old spork key to sign messages
132-
if (GetAdjustedTime() < Params().GetConsensus().nTime_RejectOldSporkKey) {
133-
CPubKey pubkeyold = spork.GetPublicKeyOld();
134-
fValidSig = spork.CheckSignature(pubkeyold);
113+
uint256 hash = spork.GetHash();
114+
std::string sporkName = sporkManager.GetSporkNameByID(spork.nSporkID);
115+
{
116+
LOCK(cs);
117+
if (mapSporksActive.count(spork.nSporkID)) {
118+
// spork is active
119+
if (mapSporksActive[spork.nSporkID].nTimeSigned >= spork.nTimeSigned) {
120+
// spork in memory has been signed more recently
121+
LogPrint(BCLog::NET, "%s : spork %d (%s) in memory is more recent: %d >= %d\n", __func__,
122+
spork.nSporkID, sporkName,
123+
mapSporksActive[spork.nSporkID].nTimeSigned, spork.nTimeSigned);
124+
return;
125+
} else {
126+
// update active spork
127+
LogPrint(BCLog::NET, "%s : got updated spork %d (%s) with value %d (signed at %d)\n", __func__,
128+
spork.nSporkID, sporkName, spork.nValue, spork.nTimeSigned);
135129
}
130+
} else {
131+
// spork is not active
132+
LogPrint(BCLog::NET, "%s : got new spork %d (%s) with value %d (signed at %d)\n", __func__,
133+
spork.nSporkID, sporkName, spork.nValue, spork.nTimeSigned);
136134
}
135+
}
137136

138-
if (!fValidSig) {
139-
LOCK(cs_main);
140-
LogPrintf("%s : Invalid Signature\n", __func__);
141-
Misbehaving(pfrom->GetId(), 100);
142-
return;
143-
}
144-
145-
{
146-
LOCK(cs);
147-
mapSporks[hash] = spork;
148-
mapSporksActive[spork.nSporkID] = spork;
137+
const bool fRequireNew = spork.nTimeSigned >= Params().GetConsensus().nTime_EnforceNewSporkKey;
138+
bool fValidSig = spork.CheckSignature();
139+
if (!fValidSig && !fRequireNew) {
140+
// See if window is open that allows for old spork key to sign messages
141+
if (GetAdjustedTime() < Params().GetConsensus().nTime_RejectOldSporkKey) {
142+
CPubKey pubkeyold = spork.GetPublicKeyOld();
143+
fValidSig = spork.CheckSignature(pubkeyold);
149144
}
150-
spork.Relay();
145+
}
151146

152-
// PIVX: add to spork database.
153-
pSporkDB->WriteSpork(spork.nSporkID, spork);
147+
if (!fValidSig) {
148+
LogPrint(BCLog::NET, "%s : Invalid Signature\n", __func__);
149+
LOCK(cs_main);
150+
Misbehaving(pfrom->GetId(), 100);
151+
return;
154152
}
155-
if (strCommand == NetMsgType::GETSPORKS) {
153+
154+
{
156155
LOCK(cs);
157-
std::map<SporkId, CSporkMessage>::iterator it = mapSporksActive.begin();
156+
mapSporks[hash] = spork;
157+
mapSporksActive[spork.nSporkID] = spork;
158+
}
159+
spork.Relay();
160+
161+
// PIVX: add to spork database.
162+
pSporkDB->WriteSpork(spork.nSporkID, spork);
163+
}
164+
165+
void CSporkManager::ProcessGetSporks(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
166+
{
167+
LOCK(cs);
168+
169+
std::map<SporkId, CSporkMessage>::iterator it = mapSporksActive.begin();
158170

159171
while (it != mapSporksActive.end()) {
160172
g_connman->PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::SPORK, it->second));

src/spork.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ class CSporkManager
118118

119119
bool SetPrivKey(std::string strPrivKey);
120120
std::string ToString() const;
121+
122+
// Process SPORK message
123+
void ProcessSporkMsg(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
124+
// Process GETSPORKS message
125+
void ProcessGetSporks(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
121126
};
122127

123128
#endif

0 commit comments

Comments
 (0)