forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge bitcoin#1862: [Refactoring] Budget, round 7: split masternode-b…
…udget files c7051e7 [Cleanup] Remove masternode-budget.cpp/.h (random-zebra) e4f1f5e [MOVE-ONLY] Move CBudgetDB to budgetdb.h/cpp (random-zebra) 682067b [MOVE-ONLY] Move CBudgetManager to budgetmanager.h/cpp (random-zebra) 6053dc3 [MOVE-ONLY] move CBudgetProposal/CFinalizedBudget to separate files (random-zebra) 3d8ecaf [MOVE-ONLY] move CBudgetVote/CFinalizedBudgetVote to separate files (random-zebra) Pull request description: Based on top of - [x] bitcoin#1861 Final round for this first cleanup of the budget classes. MOVE-ONLY commits. Split masternode-budget.h/.cpp in single files inside the `budget` subdir: - `budget/budgetdb.*` (CBudgetDB) - `budget/budgetmanager.*` (CBudgetManager) - `budget/budgetproposal.*` (CBudgetProposal) - `budget/budgetvote.*` (CBudgetVote) - `budget/finalizedbudget.*` (CFinalizedBudget) - `budget/finalizedbudgetvote.*` (CFinalizedBudgetVote) ACKs for top commit: furszy: looking good, utACK c7051e7 Fuzzbawls: ACK c7051e7 Tree-SHA512: 59d009110c615650cd4107b5ae0f8ed0a84652b033eb7e6b35b37d42f18631451c42326ac153e8c97818bf5b54020d2eca383ff2e12f274edcaa9d2bcf7137ea
- Loading branch information
Showing
24 changed files
with
1,871 additions
and
1,765 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// Copyright (c) 2014-2015 The Dash developers | ||
// Copyright (c) 2015-2020 The PIVX developers | ||
// Distributed under the MIT/X11 software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include "budget/budgetdb.h" | ||
|
||
#include "chainparams.h" | ||
#include "clientversion.h" | ||
|
||
|
||
// | ||
// CBudgetDB | ||
// | ||
|
||
CBudgetDB::CBudgetDB() | ||
{ | ||
pathDB = GetDataDir() / "budget.dat"; | ||
strMagicMessage = "MasternodeBudget"; | ||
} | ||
|
||
bool CBudgetDB::Write(const CBudgetManager& objToSave) | ||
{ | ||
int64_t nStart = GetTimeMillis(); | ||
|
||
// serialize, checksum data up to that point, then append checksum | ||
CDataStream ssObj(SER_DISK, CLIENT_VERSION); | ||
ssObj << strMagicMessage; // masternode cache file specific magic message | ||
ssObj << FLATDATA(Params().MessageStart()); // network specific magic number | ||
ssObj << objToSave; | ||
uint256 hash = Hash(ssObj.begin(), ssObj.end()); | ||
ssObj << hash; | ||
|
||
// open output file, and associate with CAutoFile | ||
FILE* file = fsbridge::fopen(pathDB, "wb"); | ||
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); | ||
if (fileout.IsNull()) | ||
return error("%s : Failed to open file %s", __func__, pathDB.string()); | ||
|
||
// Write and commit header, data | ||
try { | ||
fileout << ssObj; | ||
} catch (const std::exception& e) { | ||
return error("%s : Serialize or I/O error - %s", __func__, e.what()); | ||
} | ||
fileout.fclose(); | ||
|
||
LogPrint(BCLog::MNBUDGET,"Written info to budget.dat %dms\n", GetTimeMillis() - nStart); | ||
|
||
return true; | ||
} | ||
|
||
CBudgetDB::ReadResult CBudgetDB::Read(CBudgetManager& objToLoad, bool fDryRun) | ||
{ | ||
int64_t nStart = GetTimeMillis(); | ||
// open input file, and associate with CAutoFile | ||
FILE* file = fsbridge::fopen(pathDB, "rb"); | ||
CAutoFile filein(file, SER_DISK, CLIENT_VERSION); | ||
if (filein.IsNull()) { | ||
error("%s : Failed to open file %s", __func__, pathDB.string()); | ||
return FileError; | ||
} | ||
|
||
// use file size to size memory buffer | ||
int fileSize = fs::file_size(pathDB); | ||
int dataSize = fileSize - sizeof(uint256); | ||
// Don't try to resize to a negative number if file is small | ||
if (dataSize < 0) | ||
dataSize = 0; | ||
std::vector<unsigned char> vchData; | ||
vchData.resize(dataSize); | ||
uint256 hashIn; | ||
|
||
// read data and checksum from file | ||
try { | ||
filein.read((char*)&vchData[0], dataSize); | ||
filein >> hashIn; | ||
} catch (const std::exception& e) { | ||
error("%s : Deserialize or I/O error - %s", __func__, e.what()); | ||
return HashReadError; | ||
} | ||
filein.fclose(); | ||
|
||
CDataStream ssObj(vchData, SER_DISK, CLIENT_VERSION); | ||
|
||
// verify stored checksum matches input data | ||
uint256 hashTmp = Hash(ssObj.begin(), ssObj.end()); | ||
if (hashIn != hashTmp) { | ||
error("%s : Checksum mismatch, data corrupted", __func__); | ||
return IncorrectHash; | ||
} | ||
|
||
|
||
unsigned char pchMsgTmp[4]; | ||
std::string strMagicMessageTmp; | ||
try { | ||
// de-serialize file header (masternode cache file specific magic message) and .. | ||
ssObj >> 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 .. | ||
ssObj >> FLATDATA(pchMsgTmp); | ||
|
||
// ... verify the network matches ours | ||
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) { | ||
error("%s : Invalid network magic number", __func__); | ||
return IncorrectMagicNumber; | ||
} | ||
|
||
// de-serialize data into CBudgetManager object | ||
ssObj >> objToLoad; | ||
} catch (const std::exception& e) { | ||
objToLoad.Clear(); | ||
error("%s : Deserialize or I/O error - %s", __func__, e.what()); | ||
return IncorrectFormat; | ||
} | ||
|
||
LogPrint(BCLog::MNBUDGET,"Loaded info from budget.dat %dms\n", GetTimeMillis() - nStart); | ||
LogPrint(BCLog::MNBUDGET,"%s\n", objToLoad.ToString()); | ||
if (!fDryRun) { | ||
LogPrint(BCLog::MNBUDGET,"Budget manager - cleaning....\n"); | ||
objToLoad.CheckAndRemove(); | ||
LogPrint(BCLog::MNBUDGET,"Budget manager - result: %s\n", objToLoad.ToString()); | ||
} | ||
|
||
return Ok; | ||
} | ||
|
||
void DumpBudgets(CBudgetManager& budgetman) | ||
{ | ||
int64_t nStart = GetTimeMillis(); | ||
|
||
CBudgetDB budgetdb; | ||
CBudgetManager tempBudget; | ||
|
||
LogPrint(BCLog::MNBUDGET,"Verifying budget.dat format...\n"); | ||
CBudgetDB::ReadResult readResult = budgetdb.Read(tempBudget, true); | ||
// there was an error and it was not an error on file opening => do not proceed | ||
if (readResult == CBudgetDB::FileError) | ||
LogPrint(BCLog::MNBUDGET,"Missing budgets file - budget.dat, will try to recreate\n"); | ||
else if (readResult != CBudgetDB::Ok) { | ||
LogPrint(BCLog::MNBUDGET,"Error reading budget.dat: "); | ||
if (readResult == CBudgetDB::IncorrectFormat) | ||
LogPrint(BCLog::MNBUDGET,"magic is ok but data has invalid format, will try to recreate\n"); | ||
else { | ||
LogPrint(BCLog::MNBUDGET,"file format is unknown or invalid, please fix it manually\n"); | ||
return; | ||
} | ||
} | ||
LogPrint(BCLog::MNBUDGET,"Writting info to budget.dat...\n"); | ||
budgetdb.Write(budgetman); | ||
|
||
LogPrint(BCLog::MNBUDGET,"Budget dump finished %dms\n", GetTimeMillis() - nStart); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2014-2015 The Dash developers | ||
// Copyright (c) 2015-2020 The PIVX developers | ||
// Distributed under the MIT/X11 software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BUDGET_DB_H | ||
#define BUDGET_DB_H | ||
|
||
#include "budget/budgetmanager.h" | ||
#include "fs.h" | ||
|
||
void DumpBudgets(CBudgetManager& budgetman); | ||
|
||
|
||
/** Save Budget Manager (budget.dat) | ||
*/ | ||
class CBudgetDB | ||
{ | ||
private: | ||
fs::path pathDB; | ||
std::string strMagicMessage; | ||
|
||
public: | ||
enum ReadResult { | ||
Ok, | ||
FileError, | ||
HashReadError, | ||
IncorrectHash, | ||
IncorrectMagicMessage, | ||
IncorrectMagicNumber, | ||
IncorrectFormat | ||
}; | ||
|
||
CBudgetDB(); | ||
bool Write(const CBudgetManager& objToSave); | ||
ReadResult Read(CBudgetManager& objToLoad, bool fDryRun = false); | ||
}; | ||
|
||
#endif // BUDGET_DB_H |
Oops, something went wrong.