Changeset View
Changeset View
Standalone View
Standalone View
src/addrdb.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <addrdb.h> | #include <addrdb.h> | ||||
#include <addrman.h> | #include <addrman.h> | ||||
#include <chainparams.h> | #include <chainparams.h> | ||||
#include <clientversion.h> | #include <clientversion.h> | ||||
#include <hash.h> | #include <hash.h> | ||||
#include <random.h> | #include <random.h> | ||||
#include <streams.h> | #include <streams.h> | ||||
#include <tinyformat.h> | #include <tinyformat.h> | ||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <cstdint> | |||||
namespace { | namespace { | ||||
template <typename Stream, typename Data> | template <typename Stream, typename Data> | ||||
bool SerializeDB(const CChainParams &chainParams, Stream &stream, | bool SerializeDB(const CChainParams &chainParams, Stream &stream, | ||||
const Data &data) { | const Data &data) { | ||||
// Write and commit header, data | // Write and commit header, data | ||||
try { | try { | ||||
CHashWriter hasher(SER_DISK, CLIENT_VERSION); | CHashWriter hasher(SER_DISK, CLIENT_VERSION); | ||||
stream << chainParams.DiskMagic() << data; | stream << chainParams.DiskMagic() << data; | ||||
hasher << chainParams.DiskMagic() << data; | hasher << chainParams.DiskMagic() << data; | ||||
stream << hasher.GetHash(); | stream << hasher.GetHash(); | ||||
} catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
return error("%s: Serialize or I/O error - %s", __func__, e.what()); | return error("%s: Serialize or I/O error - %s", __func__, e.what()); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
template <typename Data> | template <typename Data> | ||||
bool SerializeFileDB(const CChainParams &chainParams, const std::string &prefix, | bool SerializeFileDB(const CChainParams &chainParams, const std::string &prefix, | ||||
const fs::path &path, const Data &data) { | const fs::path &path, const Data &data) { | ||||
// Generate random temporary filename | // Generate random temporary filename | ||||
unsigned short randv = 0; | uint16_t randv = 0; | ||||
GetRandBytes((uint8_t *)&randv, sizeof(randv)); | GetRandBytes((uint8_t *)&randv, sizeof(randv)); | ||||
std::string tmpfn = strprintf("%s.%04x", prefix, randv); | std::string tmpfn = strprintf("%s.%04x", prefix, randv); | ||||
// open temp output file, and associate with CAutoFile | // open temp output file, and associate with CAutoFile | ||||
fs::path pathTmp = GetDataDir() / tmpfn; | fs::path pathTmp = GetDataDir() / tmpfn; | ||||
FILE *file = fsbridge::fopen(pathTmp, "wb"); | FILE *file = fsbridge::fopen(pathTmp, "wb"); | ||||
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); | CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); | ||||
if (fileout.IsNull()) { | if (fileout.IsNull()) { | ||||
▲ Show 20 Lines • Show All 106 Lines • Show Last 20 Lines |