Merge pull request #273 from UdjinM6/v0.11.2.x_mncache_strmagic

V0.11.2.x use strMagicMessage for mncache.dat instead of full format verificaiton
This commit is contained in:
evan82 2015-03-24 09:19:35 -07:00
commit 6ea58711b4
3 changed files with 36 additions and 9 deletions

View File

@ -1159,7 +1159,13 @@ bool AppInit2(boost::thread_group& threadGroup)
if (readResult == CMasternodeDB::FileError) if (readResult == CMasternodeDB::FileError)
LogPrintf("Missing masternode cache file - mncache.dat, will try to recreate\n"); LogPrintf("Missing masternode cache file - mncache.dat, will try to recreate\n");
else if (readResult != CMasternodeDB::Ok) else if (readResult != CMasternodeDB::Ok)
LogPrintf("Masternode cache file mncache.dat has invalid format\n"); {
LogPrintf("Error reading mncache.dat: ");
if(readResult == CMasternodeDB::IncorrectFormat)
LogPrintf("magic is ok but data has invalid format, will try to recreate\n");
else
LogPrintf("file format is unknown or invalid, please fix it manually\n");
}
fMasterNode = GetBoolArg("-masternode", false); fMasterNode = GetBoolArg("-masternode", false);
if(fMasterNode) { if(fMasterNode) {

View File

@ -40,15 +40,17 @@ struct CompareValueOnlyMN
CMasternodeDB::CMasternodeDB() CMasternodeDB::CMasternodeDB()
{ {
pathMN = GetDataDir() / "mncache.dat"; pathMN = GetDataDir() / "mncache.dat";
strMagicMessage = "MasternodeCache";
} }
bool CMasternodeDB::Write(const CMasternodeMan& mnodemanToSave) bool CMasternodeDB::Write(const CMasternodeMan& mnodemanToSave)
{ {
int64_t nStart = GetTimeMillis(); int64_t nStart = GetTimeMillis();
// serialize addresses, checksum data up to that point, then append csum // serialize, checksum data up to that point, then append checksum
CDataStream ssMasternodes(SER_DISK, CLIENT_VERSION); CDataStream ssMasternodes(SER_DISK, CLIENT_VERSION);
ssMasternodes << FLATDATA(Params().MessageStart()); ssMasternodes << strMagicMessage; // masternode cache file specific magic message
ssMasternodes << FLATDATA(Params().MessageStart()); // network specific magic number
ssMasternodes << mnodemanToSave; ssMasternodes << mnodemanToSave;
uint256 hash = Hash(ssMasternodes.begin(), ssMasternodes.end()); uint256 hash = Hash(ssMasternodes.begin(), ssMasternodes.end());
ssMasternodes << hash; ssMasternodes << hash;
@ -119,7 +121,19 @@ CMasternodeDB::ReadResult CMasternodeDB::Read(CMasternodeMan& mnodemanToLoad)
} }
unsigned char pchMsgTmp[4]; unsigned char pchMsgTmp[4];
std::string strMagicMessageTmp;
try { try {
// de-serialize file header (masternode cache file specific magic message) and ..
ssMasternodes >> strMagicMessageTmp;
// ... verify the message matches predefined one
if (strMagicMessage != strMagicMessageTmp)
{
error("%s : Invalid masternode cache magic message", __func__);
return IncorrectMagicMessage;
}
// de-serialize file header (network specific magic number) and .. // de-serialize file header (network specific magic number) and ..
ssMasternodes >> FLATDATA(pchMsgTmp); ssMasternodes >> FLATDATA(pchMsgTmp);
@ -127,10 +141,9 @@ CMasternodeDB::ReadResult CMasternodeDB::Read(CMasternodeMan& mnodemanToLoad)
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
{ {
error("%s : Invalid network magic number", __func__); error("%s : Invalid network magic number", __func__);
return IncorrectMagic; return IncorrectMagicNumber;
} }
// de-serialize data into CMasternodeMan object
// de-serialize address data into one CMnList object
ssMasternodes >> mnodemanToLoad; ssMasternodes >> mnodemanToLoad;
} }
catch (std::exception &e) { catch (std::exception &e) {
@ -160,9 +173,15 @@ void DumpMasternodes()
LogPrintf("Missing masternode cache file - mncache.dat, will try to recreate\n"); LogPrintf("Missing masternode cache file - mncache.dat, will try to recreate\n");
else if (readResult != CMasternodeDB::Ok) else if (readResult != CMasternodeDB::Ok)
{ {
LogPrintf("Masternode cache file mncache.dat has invalid format\n"); LogPrintf("Error reading mncache.dat: ");
if(readResult == CMasternodeDB::IncorrectFormat)
LogPrintf("magic is ok but data has invalid format, will try to recreate\n");
else
{
LogPrintf("file format is unknown or invalid, please fix it manually\n");
return; return;
} }
}
LogPrintf("Writting info to mncache.dat...\n"); LogPrintf("Writting info to mncache.dat...\n");
mndb.Write(mnodeman); mndb.Write(mnodeman);

View File

@ -32,13 +32,15 @@ class CMasternodeDB
{ {
private: private:
boost::filesystem::path pathMN; boost::filesystem::path pathMN;
std::string strMagicMessage;
public: public:
enum ReadResult { enum ReadResult {
Ok, Ok,
FileError, FileError,
HashReadError, HashReadError,
IncorrectHash, IncorrectHash,
IncorrectMagic, IncorrectMagicMessage,
IncorrectMagicNumber,
IncorrectFormat IncorrectFormat
}; };