mirror of
https://github.com/dashpay/dash.git
synced 2024-12-28 13:32:47 +01:00
ef4dfa8524
(script modified to account for Dash backports, doesn't account for rebasing) ------------- BEGIN SCRIPT --------------- mkdir -p src/util git mv src/util.h src/util/system.h git mv src/util.cpp src/util/system.cpp git mv src/utilmemory.h src/util/memory.h git mv src/utilmoneystr.h src/util/moneystr.h git mv src/utilmoneystr.cpp src/util/moneystr.cpp git mv src/utilstrencodings.h src/util/strencodings.h git mv src/utilstrencodings.cpp src/util/strencodings.cpp git mv src/utiltime.h src/util/time.h git mv src/utiltime.cpp src/util/time.cpp git mv src/utilasmap.h src/util/asmap.h git mv src/utilasmap.cpp src/util/asmap.cpp git mv src/utilstring.h src/util/string.h git mv src/utilstring.cpp src/util/string.cpp gsed -i 's/<util\.h>/<util\/system\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') gsed -i 's/<utilmemory\.h>/<util\/memory\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') gsed -i 's/<utilmoneystr\.h>/<util\/moneystr\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') gsed -i 's/<utilstrencodings\.h>/<util\/strencodings\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') gsed -i 's/<utiltime\.h>/<util\/time\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') gsed -i 's/<utilasmap\.h>/<util\/asmap\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') gsed -i 's/<utilstring\.h>/<util\/string\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') gsed -i 's/BITCOIN_UTIL_H/BITCOIN_UTIL_SYSTEM_H/g' src/util/system.h gsed -i 's/BITCOIN_UTILMEMORY_H/BITCOIN_UTIL_MEMORY_H/g' src/util/memory.h gsed -i 's/BITCOIN_UTILMONEYSTR_H/BITCOIN_UTIL_MONEYSTR_H/g' src/util/moneystr.h gsed -i 's/BITCOIN_UTILSTRENCODINGS_H/BITCOIN_UTIL_STRENCODINGS_H/g' src/util/strencodings.h gsed -i 's/BITCOIN_UTILTIME_H/BITCOIN_UTIL_TIME_H/g' src/util/time.h gsed -i 's/BITCOIN_UTILASMAP_H/BITCOIN_UTIL_ASMAP_H/g' src/util/asmap.h gsed -i 's/BITCOIN_UTILSTRING_H/BITCOIN_UTIL_STRING_H/g' src/util/string.h gsed -i 's/ util\.\(h\|cpp\)/ util\/system\.\1/g' src/Makefile.am gsed -i 's/utilmemory\.\(h\|cpp\)/util\/memory\.\1/g' src/Makefile.am gsed -i 's/utilmoneystr\.\(h\|cpp\)/util\/moneystr\.\1/g' src/Makefile.am gsed -i 's/utilstrencodings\.\(h\|cpp\)/util\/strencodings\.\1/g' src/Makefile.am gsed -i 's/utiltime\.\(h\|cpp\)/util\/time\.\1/g' src/Makefile.am gsed -i 's/utilasmap\.\(h\|cpp\)/util\/asmap\.\1/g' src/Makefile.am gsed -i 's/utilstring\.\(h\|cpp\)/util\/string\.\1/g' src/Makefile.am gsed -i 's/-> util ->/-> util\/system ->/' test/lint/lint-circular-dependencies.sh gsed -i 's/src\/util\.cpp/src\/util\/system\.cpp/g' test/lint/lint-format-strings.py test/lint/lint-locale-dependence.sh gsed -i 's/src\/utilmoneystr\.cpp/src\/util\/moneystr\.cpp/g' test/lint/lint-locale-dependence.sh gsed -i 's/src\/utilstrencodings\.\(h\|cpp\)/src\/util\/strencodings\.\1/g' test/lint/lint-locale-dependence.sh ------------- END SCRIPT ---------------
229 lines
7.0 KiB
C++
229 lines
7.0 KiB
C++
// Copyright (c) 2014-2020 The Dash Core developers
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_FLAT_DATABASE_H
|
|
#define BITCOIN_FLAT_DATABASE_H
|
|
|
|
#include <chainparams.h>
|
|
#include <clientversion.h>
|
|
#include <fs.h>
|
|
#include <hash.h>
|
|
#include <streams.h>
|
|
#include <util/system.h>
|
|
|
|
/**
|
|
* Generic Dumping and Loading
|
|
* ---------------------------
|
|
*/
|
|
|
|
template<typename T>
|
|
class CFlatDB
|
|
{
|
|
private:
|
|
|
|
enum ReadResult {
|
|
Ok,
|
|
FileError,
|
|
HashReadError,
|
|
IncorrectHash,
|
|
IncorrectMagicMessage,
|
|
IncorrectMagicNumber,
|
|
IncorrectFormat
|
|
};
|
|
|
|
fs::path pathDB;
|
|
std::string strFilename;
|
|
std::string strMagicMessage;
|
|
|
|
bool Write(const T& objToSave)
|
|
{
|
|
// LOCK(objToSave.cs);
|
|
|
|
int64_t nStart = GetTimeMillis();
|
|
|
|
// serialize, checksum data up to that point, then append checksum
|
|
CDataStream ssObj(SER_DISK, CLIENT_VERSION);
|
|
ssObj << strMagicMessage; // specific magic message for this type of object
|
|
ssObj << 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 = fopen(pathDB.string().c_str(), "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 (std::exception &e) {
|
|
return error("%s: Serialize or I/O error - %s", __func__, e.what());
|
|
}
|
|
fileout.fclose();
|
|
|
|
LogPrintf("Written info to %s %dms\n", strFilename, GetTimeMillis() - nStart);
|
|
LogPrintf(" %s\n", objToSave.ToString());
|
|
|
|
return true;
|
|
}
|
|
|
|
ReadResult Read(T& objToLoad, bool fDryRun = false)
|
|
{
|
|
//LOCK(objToLoad.cs);
|
|
|
|
int64_t nStart = GetTimeMillis();
|
|
// open input file, and associate with CAutoFile
|
|
FILE *file = fopen(pathDB.string().c_str(), "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.data(), dataSize);
|
|
filein >> hashIn;
|
|
}
|
|
catch (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 (file specific magic message) and ..
|
|
ssObj >> strMagicMessageTmp;
|
|
|
|
// ... verify the message matches predefined one
|
|
if (strMagicMessage != strMagicMessageTmp)
|
|
{
|
|
error("%s: Invalid magic message", __func__);
|
|
return IncorrectMagicMessage;
|
|
}
|
|
|
|
|
|
// de-serialize file header (network specific magic number) and ..
|
|
ssObj >> 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 T object
|
|
ssObj >> objToLoad;
|
|
}
|
|
catch (std::exception &e) {
|
|
objToLoad.Clear();
|
|
error("%s: Deserialize or I/O error - %s", __func__, e.what());
|
|
return IncorrectFormat;
|
|
}
|
|
|
|
LogPrintf("Loaded info from %s %dms\n", strFilename, GetTimeMillis() - nStart);
|
|
LogPrintf(" %s\n", objToLoad.ToString());
|
|
if(!fDryRun) {
|
|
LogPrintf("%s: Cleaning....\n", __func__);
|
|
objToLoad.CheckAndRemove();
|
|
LogPrintf(" %s\n", objToLoad.ToString());
|
|
}
|
|
|
|
return Ok;
|
|
}
|
|
|
|
|
|
public:
|
|
CFlatDB(std::string strFilenameIn, std::string strMagicMessageIn)
|
|
{
|
|
pathDB = GetDataDir() / strFilenameIn;
|
|
strFilename = strFilenameIn;
|
|
strMagicMessage = strMagicMessageIn;
|
|
}
|
|
|
|
bool Load(T& objToLoad)
|
|
{
|
|
LogPrintf("Reading info from %s...\n", strFilename);
|
|
ReadResult readResult = Read(objToLoad);
|
|
if (readResult == FileError)
|
|
LogPrintf("Missing file %s, will try to recreate\n", strFilename);
|
|
else if (readResult != Ok)
|
|
{
|
|
LogPrintf("Error reading %s: ", strFilename);
|
|
if(readResult == IncorrectFormat)
|
|
{
|
|
LogPrintf("%s: Magic is ok but data has invalid format, will try to recreate\n", __func__);
|
|
}
|
|
else {
|
|
LogPrintf("%s: File format is unknown or invalid, please fix it manually\n", __func__);
|
|
// program should exit with an error
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Dump(T& objToSave)
|
|
{
|
|
int64_t nStart = GetTimeMillis();
|
|
|
|
LogPrintf("Verifying %s format...\n", strFilename);
|
|
T tmpObjToLoad;
|
|
ReadResult readResult = Read(tmpObjToLoad, true);
|
|
|
|
// there was an error and it was not an error on file opening => do not proceed
|
|
if (readResult == FileError)
|
|
LogPrintf("Missing file %s, will try to recreate\n", strFilename);
|
|
else if (readResult != Ok)
|
|
{
|
|
LogPrintf("Error reading %s: ", strFilename);
|
|
if(readResult == IncorrectFormat)
|
|
LogPrintf("%s: Magic is ok but data has invalid format, will try to recreate\n", __func__);
|
|
else
|
|
{
|
|
LogPrintf("%s: File format is unknown or invalid, please fix it manually\n", __func__);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
LogPrintf("Writing info to %s...\n", strFilename);
|
|
Write(objToSave);
|
|
LogPrintf("%s dump finished %dms\n", strFilename, GetTimeMillis() - nStart);
|
|
|
|
return true;
|
|
}
|
|
|
|
};
|
|
|
|
|
|
#endif // BITCOIN_FLAT_DATABASE_H
|