2016-04-16 06:13:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-07-27 00:36:45 +02:00
|
|
|
// Copyright (c) 2009-2018 The Bitcoin Core developers
|
2016-04-16 06:13:15 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <addrdb.h>
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <addrman.h>
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <clientversion.h>
|
|
|
|
#include <hash.h>
|
|
|
|
#include <random.h>
|
|
|
|
#include <streams.h>
|
|
|
|
#include <tinyformat.h>
|
2018-10-23 00:51:11 +02:00
|
|
|
#include <util/system.h>
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
namespace {
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
template <typename Stream, typename Data>
|
|
|
|
bool SerializeDB(Stream& stream, const Data& data)
|
2016-04-16 06:13:15 +02:00
|
|
|
{
|
2017-04-21 13:50:55 +02:00
|
|
|
// Write and commit header, data
|
|
|
|
try {
|
|
|
|
CHashWriter hasher(SER_DISK, CLIENT_VERSION);
|
2018-03-21 01:37:32 +01:00
|
|
|
stream << Params().MessageStart() << data;
|
|
|
|
hasher << Params().MessageStart() << data;
|
2017-04-21 13:50:55 +02:00
|
|
|
stream << hasher.GetHash();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
return error("%s: Serialize or I/O error - %s", __func__, e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
template <typename Data>
|
|
|
|
bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data)
|
2016-04-16 06:13:15 +02:00
|
|
|
{
|
|
|
|
// Generate random temporary filename
|
|
|
|
unsigned short randv = 0;
|
|
|
|
GetRandBytes((unsigned char*)&randv, sizeof(randv));
|
2017-04-21 13:50:55 +02:00
|
|
|
std::string tmpfn = strprintf("%s.%04x", prefix, randv);
|
2016-04-16 06:13:15 +02:00
|
|
|
|
|
|
|
// open temp output file, and associate with CAutoFile
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path pathTmp = GetDataDir() / tmpfn;
|
2017-03-01 17:28:39 +01:00
|
|
|
FILE *file = fsbridge::fopen(pathTmp, "wb");
|
2016-04-16 06:13:15 +02:00
|
|
|
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
|
2019-06-14 08:30:43 +02:00
|
|
|
if (fileout.IsNull()) {
|
|
|
|
fileout.fclose();
|
|
|
|
remove(pathTmp);
|
2016-04-16 06:13:15 +02:00
|
|
|
return error("%s: Failed to open file %s", __func__, pathTmp.string());
|
2019-06-14 08:30:43 +02:00
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
// Serialize
|
2019-06-14 08:30:43 +02:00
|
|
|
if (!SerializeDB(fileout, data)) {
|
|
|
|
fileout.fclose();
|
|
|
|
remove(pathTmp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!FileCommit(fileout.Get())) {
|
|
|
|
fileout.fclose();
|
|
|
|
remove(pathTmp);
|
2018-04-20 11:21:08 +02:00
|
|
|
return error("%s: Failed to flush file %s", __func__, pathTmp.string());
|
2019-06-14 08:30:43 +02:00
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
fileout.fclose();
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
// replace existing file, if any, with new file
|
2019-06-14 08:30:43 +02:00
|
|
|
if (!RenameOver(pathTmp, path)) {
|
|
|
|
remove(pathTmp);
|
2016-04-16 06:13:15 +02:00
|
|
|
return error("%s: Rename-into-place failed", __func__);
|
2019-06-14 08:30:43 +02:00
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
template <typename Stream, typename Data>
|
|
|
|
bool DeserializeDB(Stream& stream, Data& data, bool fCheckSum = true)
|
2016-04-16 06:13:15 +02:00
|
|
|
{
|
|
|
|
try {
|
2017-04-21 13:50:55 +02:00
|
|
|
CHashVerifier<Stream> verifier(&stream);
|
2016-04-16 06:13:15 +02:00
|
|
|
// de-serialize file header (network specific magic number) and ..
|
2017-04-21 13:50:55 +02:00
|
|
|
unsigned char pchMsgTmp[4];
|
2018-03-21 01:37:32 +01:00
|
|
|
verifier >> pchMsgTmp;
|
2016-04-16 06:13:15 +02:00
|
|
|
// ... verify the network matches ours
|
|
|
|
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
|
|
|
|
return error("%s: Invalid network magic number", __func__);
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
// de-serialize data
|
|
|
|
verifier >> data;
|
|
|
|
|
|
|
|
// verify checksum
|
|
|
|
if (fCheckSum) {
|
|
|
|
uint256 hashTmp;
|
|
|
|
stream >> hashTmp;
|
|
|
|
if (hashTmp != verifier.GetHash()) {
|
|
|
|
return error("%s: Checksum mismatch, data corrupted", __func__);
|
|
|
|
}
|
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception& e) {
|
|
|
|
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
template <typename Data>
|
|
|
|
bool DeserializeFileDB(const fs::path& path, Data& data)
|
2016-04-16 06:13:15 +02:00
|
|
|
{
|
2017-04-21 13:50:55 +02:00
|
|
|
// open input file, and associate with CAutoFile
|
|
|
|
FILE *file = fsbridge::fopen(path, "rb");
|
|
|
|
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
|
|
|
|
if (filein.IsNull())
|
2019-07-29 17:47:33 +02:00
|
|
|
return false;
|
2017-04-21 13:50:55 +02:00
|
|
|
|
|
|
|
return DeserializeDB(filein, data);
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-10-05 19:35:20 +02:00
|
|
|
CBanDB::CBanDB(fs::path ban_list_path) : m_ban_list_path(std::move(ban_list_path))
|
2017-04-21 13:50:55 +02:00
|
|
|
{
|
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
bool CBanDB::Write(const banmap_t& banSet)
|
|
|
|
{
|
2017-10-05 19:35:20 +02:00
|
|
|
return SerializeFileDB("banlist", m_ban_list_path, banSet);
|
2017-04-21 13:50:55 +02:00
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
bool CBanDB::Read(banmap_t& banSet)
|
|
|
|
{
|
2017-10-05 19:35:20 +02:00
|
|
|
return DeserializeFileDB(m_ban_list_path, banSet);
|
2017-04-21 13:50:55 +02:00
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
CAddrDB::CAddrDB()
|
|
|
|
{
|
|
|
|
pathAddr = GetDataDir() / "peers.dat";
|
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
bool CAddrDB::Write(const CAddrMan& addr)
|
|
|
|
{
|
|
|
|
return SerializeFileDB("peers", pathAddr, addr);
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CAddrDB::Read(CAddrMan& addr)
|
|
|
|
{
|
2017-04-21 13:50:55 +02:00
|
|
|
return DeserializeFileDB(pathAddr, addr);
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers)
|
|
|
|
{
|
2017-04-21 13:50:55 +02:00
|
|
|
bool ret = DeserializeDB(ssPeers, addr, false);
|
|
|
|
if (!ret) {
|
|
|
|
// Ensure addrman is left in a clean state
|
2016-04-16 06:13:15 +02:00
|
|
|
addr.Clear();
|
|
|
|
}
|
2017-04-21 13:50:55 +02:00
|
|
|
return ret;
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|