Add versioning to spork cache (#2312)

This commit is contained in:
UdjinM6 2018-09-26 17:15:02 +03:00 committed by GitHub
parent 2c1a17909e
commit 8c0dca2826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 2 deletions

View File

@ -15,6 +15,8 @@
CSporkManager sporkManager;
const std::string CSporkManager::SERIALIZATION_VERSION_STRING = "CMasternodeMan-Version-1";
std::map<int, int64_t> mapSporkDefaults = {
{SPORK_2_INSTANTSEND_ENABLED, 0}, // ON
{SPORK_3_INSTANTSEND_BLOCK_FILTERING, 0}, // ON
@ -37,6 +39,36 @@ void CSporkManager::Clear()
sporkPrivKey = CKey();
}
void CSporkManager::CheckAndRemove()
{
LOCK(cs);
bool fSporkAddressIsSet = !sporkPubKeyID.IsNull();
assert(fSporkAddressIsSet);
auto itActive = mapSporksActive.begin();
while (itActive != mapSporksActive.end()) {
if (!itActive->second.CheckSignature(sporkPubKeyID, false)) {
if (!itActive->second.CheckSignature(sporkPubKeyID, true)) {
mapSporksByHash.erase(itActive->second.GetHash());
mapSporksActive.erase(itActive++);
continue;
}
}
++itActive;
}
auto itByHash = mapSporksByHash.begin();
while (itByHash != mapSporksByHash.end()) {
if (!itByHash->second.CheckSignature(sporkPubKeyID, false)) {
if (!itByHash->second.CheckSignature(sporkPubKeyID, true)) {
mapSporksActive.erase(itByHash->second.nSporkID);
mapSporksByHash.erase(itByHash++);
continue;
}
}
++itByHash;
}
}
void CSporkManager::ProcessSpork(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman)
{
if(fLiteMode) return; // disable all Dash specific functionality

View File

@ -86,6 +86,8 @@ public:
class CSporkManager
{
private:
static const std::string SERIALIZATION_VERSION_STRING;
mutable CCriticalSection cs;
std::map<uint256, CSporkMessage> mapSporksByHash;
std::map<int, CSporkMessage> mapSporksActive;
@ -101,6 +103,16 @@ public:
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
std::string strVersion;
if(ser_action.ForRead()) {
READWRITE(strVersion);
if (strVersion != SERIALIZATION_VERSION_STRING) {
return;
}
} else {
strVersion = SERIALIZATION_VERSION_STRING;
READWRITE(strVersion);
}
READWRITE(sporkPubKeyID);
READWRITE(mapSporksByHash);
READWRITE(mapSporksActive);
@ -108,8 +120,7 @@ public:
}
void Clear();
/// Dummy implementation for CFlatDB
void CheckAndRemove() {}
void CheckAndRemove();
void ProcessSpork(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
void ExecuteSpork(int nSporkID, int nValue);