PIVX Core  5.6.99
P2P Digital Currency
budgetdb.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2015 The Dash developers
2 // Copyright (c) 2015-2021 The PIVX Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "budget/budgetdb.h"
7 
8 #include "chainparams.h"
9 #include "clientversion.h"
10 
11 static const int BUDGET_DB_VERSION = 1;
12 
13 //
14 // CBudgetDB
15 //
16 
18 {
19  pathDB = GetDataDir() / "budget.dat";
20  strMagicMessage = "MasternodeBudget";
21 }
22 
23 bool CBudgetDB::Write(const CBudgetManager& objToSave)
24 {
25  int64_t nStart = GetTimeMillis();
26 
27  // serialize, checksum data up to that point, then append checksum
28  CDataStream ssObj(SER_DISK, CLIENT_VERSION);
29  ssObj << BUDGET_DB_VERSION;
30  ssObj << strMagicMessage; // masternode cache file specific magic message
31  ssObj << Params().MessageStart(); // network specific magic number
32  ssObj << objToSave;
33  uint256 hash = Hash(ssObj.begin(), ssObj.end());
34  ssObj << hash;
35 
36  // open output file, and associate with CAutoFile
37  FILE* file = fsbridge::fopen(pathDB, "wb");
38  CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
39  if (fileout.IsNull())
40  return error("%s : Failed to open file %s", __func__, pathDB.string());
41 
42  // Write and commit header, data
43  try {
44  fileout << ssObj;
45  } catch (const std::exception& e) {
46  return error("%s : Serialize or I/O error - %s", __func__, e.what());
47  }
48  fileout.fclose();
49 
50  LogPrint(BCLog::MNBUDGET,"Written info to budget.dat %dms\n", GetTimeMillis() - nStart);
51 
52  return true;
53 }
54 
56 {
57  int64_t nStart = GetTimeMillis();
58  // open input file, and associate with CAutoFile
59  FILE* file = fsbridge::fopen(pathDB, "rb");
60  CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
61  if (filein.IsNull()) {
62  error("%s : Failed to open file %s", __func__, pathDB.string());
63  return FileError;
64  }
65 
66  // use file size to size memory buffer
67  int fileSize = fs::file_size(pathDB);
68  int dataSize = fileSize - sizeof(uint256);
69  // Don't try to resize to a negative number if file is small
70  if (dataSize < 0)
71  dataSize = 0;
72  std::vector<unsigned char> vchData;
73  vchData.resize(dataSize);
74  uint256 hashIn;
75 
76  // read data and checksum from file
77  try {
78  filein.read((char*)vchData.data(), dataSize);
79  filein >> hashIn;
80  } catch (const std::exception& e) {
81  error("%s : Deserialize or I/O error - %s", __func__, e.what());
82  return HashReadError;
83  }
84  filein.fclose();
85 
86  CDataStream ssObj(vchData, SER_DISK, CLIENT_VERSION);
87 
88  // verify stored checksum matches input data
89  uint256 hashTmp = Hash(ssObj.begin(), ssObj.end());
90  if (hashIn != hashTmp) {
91  error("%s : Checksum mismatch, data corrupted", __func__);
92  return IncorrectHash;
93  }
94 
95  int version;
96  std::string strMagicMessageTmp;
97  try {
98  // de-serialize file header
99  ssObj >> version;
100  ssObj >> strMagicMessageTmp;
101 
102  // ... verify the message matches predefined one
103  if (strMagicMessage != strMagicMessageTmp) {
104  error("%s : Invalid masternode cache magic message", __func__);
105  return IncorrectMagicMessage;
106  }
107 
108  // de-serialize file header (network specific magic number) and ..
109  std::vector<unsigned char> pchMsgTmp(4);
110  ssObj >> MakeSpan(pchMsgTmp);
111 
112  // ... verify the network matches ours
113  if (memcmp(pchMsgTmp.data(), Params().MessageStart(), pchMsgTmp.size()) != 0) {
114  error("%s : Invalid network magic number", __func__);
115  return IncorrectMagicNumber;
116  }
117 
118  // de-serialize data into CBudgetManager object
119  ssObj >> objToLoad;
120  } catch (const std::exception& e) {
121  objToLoad.Clear();
122  error("%s : Deserialize or I/O error - %s", __func__, e.what());
123  return IncorrectFormat;
124  }
125 
126  LogPrint(BCLog::MNBUDGET,"Loaded info from budget.dat (dbversion=%d) %dms\n", version, GetTimeMillis() - nStart);
127  LogPrint(BCLog::MNBUDGET,"%s\n", objToLoad.ToString());
128  if (!fDryRun) {
129  LogPrint(BCLog::MNBUDGET,"Budget manager - cleaning....\n");
130  objToLoad.CheckAndRemove();
131  LogPrint(BCLog::MNBUDGET,"Budget manager - result: %s\n", objToLoad.ToString());
132  }
133 
134  return Ok;
135 }
136 
137 void DumpBudgets(CBudgetManager& budgetman)
138 {
139  int64_t nStart = GetTimeMillis();
140 
141  CBudgetDB budgetdb;
142  LogPrint(BCLog::MNBUDGET,"Writing info to budget.dat...\n");
143  budgetdb.Write(budgetman);
144 
145  LogPrint(BCLog::MNBUDGET,"Budget dump finished %dms\n", GetTimeMillis() - nStart);
146 }
void DumpBudgets(CBudgetManager &budgetman)
Definition: budgetdb.cpp:137
const CChainParams & Params()
Return the currently selected parameters.
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:452
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:501
void fclose()
Definition: streams.h:474
void read(char *pch, size_t nSize)
Definition: streams.h:509
const_iterator end() const
Definition: streams.h:163
const_iterator begin() const
Definition: streams.h:161
Save Budget Manager (budget.dat)
Definition: budgetdb.h:18
@ IncorrectFormat
Definition: budgetdb.h:31
@ IncorrectMagicNumber
Definition: budgetdb.h:30
@ IncorrectMagicMessage
Definition: budgetdb.h:29
@ FileError
Definition: budgetdb.h:26
@ HashReadError
Definition: budgetdb.h:27
@ IncorrectHash
Definition: budgetdb.h:28
std::string strMagicMessage
Definition: budgetdb.h:21
bool Write(const CBudgetManager &objToSave)
Definition: budgetdb.cpp:23
fs::path pathDB
Definition: budgetdb.h:20
ReadResult Read(CBudgetManager &objToLoad, bool fDryRun=false)
Definition: budgetdb.cpp:55
std::string ToString() const
const CMessageHeader::MessageStartChars & MessageStart() const
Definition: chainparams.h:73
256-bit opaque blob.
Definition: uint256.h:138
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:173
#define LogPrint(category,...)
Definition: logging.h:163
@ MNBUDGET
Definition: logging.h:60
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:23
@ SER_DISK
Definition: serialize.h:175
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:221
const fs::path & GetDataDir(bool fNetSpecific)
Definition: system.cpp:724
bool error(const char *fmt, const Args &... args)
Definition: system.h:77
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: utiltime.cpp:61