Skip to content

Commit

Permalink
Merge bitcoin#1862: [Refactoring] Budget, round 7: split masternode-b…
Browse files Browse the repository at this point in the history
…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
furszy committed Nov 17, 2020
2 parents 226aaaa + c7051e7 commit 70c1fd1
Show file tree
Hide file tree
Showing 24 changed files with 1,871 additions and 1,765 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,13 @@ set(WALLET_SOURCES
./src/wallet/db.cpp
./src/addressbook.cpp
./src/crypter.cpp
./src/budget/budgetdb.cpp
./src/budget/budgetmanager.cpp
./src/budget/budgetproposal.cpp
./src/budget/budgetvote.cpp
./src/budget/finalizedbudget.cpp
./src/budget/finalizedbudgetvote.cpp
./src/masternode.cpp
./src/masternode-budget.cpp
./src/masternode-payments.cpp
./src/masternode-sync.cpp
./src/tiertwo_networksync.cpp
Expand Down
14 changes: 12 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,15 @@ BITCOIN_CORE_H = \
logging.h \
legacy/validation_zerocoin_legacy.h \
sapling/sapling_validation.h \
budget/budgetdb.h \
budget/budgetmanager.h \
budget/budgetproposal.h \
budget/budgetvote.h \
budget/finalizedbudget.h \
budget/finalizedbudgetvote.h \
memusage.h \
masternode.h \
masternode-payments.h \
masternode-budget.h \
masternode-sync.h \
masternodeman.h \
masternodeconfig.h \
Expand Down Expand Up @@ -374,8 +379,13 @@ libbitcoin_wallet_a_SOURCES = \
addressbook.cpp \
crypter.cpp \
key_io.cpp \
budget/budgetdb.cpp \
budget/budgetmanager.cpp \
budget/budgetproposal.cpp \
budget/budgetvote.cpp \
budget/finalizedbudget.cpp \
budget/finalizedbudgetvote.cpp \
masternode.cpp \
masternode-budget.cpp \
masternode-payments.cpp \
tiertwo_networksync.cpp \
masternode-sync.cpp \
Expand Down
160 changes: 160 additions & 0 deletions src/budget/budgetdb.cpp
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);
}
39 changes: 39 additions & 0 deletions src/budget/budgetdb.h
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
Loading

0 comments on commit 70c1fd1

Please sign in to comment.