mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-09 11:27:28 -03:00
Merge bitcoin/bitcoin#22915: Remove confusing CAddrDB
fade9a1a4d
Remove confusing CAddrDB (MarcoFalke)fa7f77b7d1
Fix addrdb includes (MarcoFalke)fa3f5d0dae
Move addrman includes from .h to .cpp (MarcoFalke) Pull request description: Split out from #22762 to avoid having to carry it around in (an)other rebase(s) ACKs for top commit: ryanofsky: Code review ACKfade9a1a4d
lsilva01: Code Review ACKfade9a1a4d
Tree-SHA512: 7615fb0b6235d0c1e6f8cd6263dd18c4d95890567c2b797fe6fce6cb12cc85ce6eacbe07dbb6d81b05d179ef03b42edfd61c940e35a1044ce6d363b54c2dae5c
This commit is contained in:
commit
896649996b
13 changed files with 37 additions and 46 deletions
|
@ -170,22 +170,19 @@ bool CBanDB::Read(banmap_t& banSet)
|
|||
return true;
|
||||
}
|
||||
|
||||
CAddrDB::CAddrDB()
|
||||
{
|
||||
pathAddr = gArgs.GetDataDirNet() / "peers.dat";
|
||||
}
|
||||
|
||||
bool CAddrDB::Write(const CAddrMan& addr)
|
||||
bool DumpPeerAddresses(const ArgsManager& args, const CAddrMan& addr)
|
||||
{
|
||||
const auto pathAddr = args.GetDataDirNet() / "peers.dat";
|
||||
return SerializeFileDB("peers", pathAddr, addr, CLIENT_VERSION);
|
||||
}
|
||||
|
||||
bool CAddrDB::Read(CAddrMan& addr)
|
||||
bool ReadPeerAddresses(const ArgsManager& args, CAddrMan& addr)
|
||||
{
|
||||
const auto pathAddr = args.GetDataDirNet() / "peers.dat";
|
||||
return DeserializeFileDB(pathAddr, addr, CLIENT_VERSION);
|
||||
}
|
||||
|
||||
bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers)
|
||||
bool ReadFromStream(CAddrMan& addr, CDataStream& ssPeers)
|
||||
{
|
||||
return DeserializeDB(ssPeers, addr, false);
|
||||
}
|
||||
|
|
18
src/addrdb.h
18
src/addrdb.h
|
@ -12,21 +12,15 @@
|
|||
|
||||
#include <vector>
|
||||
|
||||
class CAddress;
|
||||
class ArgsManager;
|
||||
class CAddrMan;
|
||||
class CAddress;
|
||||
class CDataStream;
|
||||
|
||||
/** Access to the (IP) address database (peers.dat) */
|
||||
class CAddrDB
|
||||
{
|
||||
private:
|
||||
fs::path pathAddr;
|
||||
public:
|
||||
CAddrDB();
|
||||
bool Write(const CAddrMan& addr);
|
||||
bool Read(CAddrMan& addr);
|
||||
static bool Read(CAddrMan& addr, CDataStream& ssPeers);
|
||||
};
|
||||
bool DumpPeerAddresses(const ArgsManager& args, const CAddrMan& addr);
|
||||
bool ReadPeerAddresses(const ArgsManager& args, CAddrMan& addr);
|
||||
/** Only used by tests. */
|
||||
bool ReadFromStream(CAddrMan& addr, CDataStream& ssPeers);
|
||||
|
||||
/** Access to the banlist database (banlist.json) */
|
||||
class CBanDB
|
||||
|
|
|
@ -5,10 +5,12 @@
|
|||
|
||||
#include <addrman.h>
|
||||
|
||||
#include <clientversion.h>
|
||||
#include <hash.h>
|
||||
#include <logging.h>
|
||||
#include <netaddress.h>
|
||||
#include <serialize.h>
|
||||
#include <streams.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <optional>
|
||||
|
|
|
@ -6,23 +6,16 @@
|
|||
#ifndef BITCOIN_ADDRMAN_H
|
||||
#define BITCOIN_ADDRMAN_H
|
||||
|
||||
#include <clientversion.h>
|
||||
#include <config/bitcoin-config.h>
|
||||
#include <fs.h>
|
||||
#include <hash.h>
|
||||
#include <logging.h>
|
||||
#include <netaddress.h>
|
||||
#include <protocol.h>
|
||||
#include <random.h>
|
||||
#include <streams.h>
|
||||
#include <sync.h>
|
||||
#include <timedata.h>
|
||||
#include <tinyformat.h>
|
||||
#include <util/system.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <stdint.h>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
|
|
|
@ -1206,14 +1206,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
// Load addresses from peers.dat
|
||||
uiInterface.InitMessage(_("Loading P2P addresses…").translated);
|
||||
int64_t nStart = GetTimeMillis();
|
||||
CAddrDB adb;
|
||||
if (adb.Read(*node.addrman)) {
|
||||
if (ReadPeerAddresses(args, *node.addrman)) {
|
||||
LogPrintf("Loaded %i addresses from peers.dat %dms\n", node.addrman->size(), GetTimeMillis() - nStart);
|
||||
} else {
|
||||
// Addrman can be in an inconsistent state after failure, reset it
|
||||
node.addrman = std::make_unique<CAddrMan>(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman);
|
||||
LogPrintf("Recreating peers.dat\n");
|
||||
adb.Write(*node.addrman);
|
||||
DumpPeerAddresses(args, *node.addrman);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include <net.h>
|
||||
|
||||
#include <addrdb.h>
|
||||
#include <banman.h>
|
||||
#include <clientversion.h>
|
||||
#include <compat.h>
|
||||
|
@ -24,6 +25,7 @@
|
|||
#include <scheduler.h>
|
||||
#include <util/sock.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/system.h>
|
||||
#include <util/thread.h>
|
||||
#include <util/trace.h>
|
||||
#include <util/translation.h>
|
||||
|
@ -1747,8 +1749,7 @@ void CConnman::DumpAddresses()
|
|||
{
|
||||
int64_t nStart = GetTimeMillis();
|
||||
|
||||
CAddrDB adb;
|
||||
adb.Write(addrman);
|
||||
DumpPeerAddresses(::gArgs, addrman);
|
||||
|
||||
LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
|
||||
addrman.size(), GetTimeMillis() - nStart);
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
#ifndef BITCOIN_NET_H
|
||||
#define BITCOIN_NET_H
|
||||
|
||||
#include <addrdb.h>
|
||||
#include <addrman.h>
|
||||
#include <amount.h>
|
||||
#include <bloom.h>
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#ifndef BITCOIN_QT_BANTABLEMODEL_H
|
||||
#define BITCOIN_QT_BANTABLEMODEL_H
|
||||
|
||||
#include <addrdb.h>
|
||||
#include <net.h>
|
||||
|
||||
#include <memory>
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <qt/psbtoperationsdialog.h>
|
||||
#include <qt/walletmodel.h>
|
||||
#include <qt/walletview.h>
|
||||
#include <util/system.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
|
|
@ -5,13 +5,14 @@
|
|||
#include <addrdb.h>
|
||||
#include <addrman.h>
|
||||
#include <chainparams.h>
|
||||
#include <clientversion.h>
|
||||
#include <hash.h>
|
||||
#include <netbase.h>
|
||||
#include <random.h>
|
||||
#include <test/data/asmap.raw.h>
|
||||
#include <test/util/setup_common.h>
|
||||
#include <util/asmap.h>
|
||||
#include <util/string.h>
|
||||
#include <hash.h>
|
||||
#include <netbase.h>
|
||||
#include <random.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
|
@ -1002,7 +1003,7 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks)
|
|||
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(caddrdb_read)
|
||||
BOOST_AUTO_TEST_CASE(load_addrman)
|
||||
{
|
||||
CAddrManUncorrupted addrmanUncorrupted;
|
||||
|
||||
|
@ -1037,17 +1038,17 @@ BOOST_AUTO_TEST_CASE(caddrdb_read)
|
|||
BOOST_CHECK(addrman1.size() == 3);
|
||||
BOOST_CHECK(exceptionThrown == false);
|
||||
|
||||
// Test that CAddrDB::Read creates an addrman with the correct number of addrs.
|
||||
// Test that ReadFromStream creates an addrman with the correct number of addrs.
|
||||
CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted);
|
||||
|
||||
CAddrMan addrman2(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
|
||||
BOOST_CHECK(addrman2.size() == 0);
|
||||
BOOST_CHECK(CAddrDB::Read(addrman2, ssPeers2));
|
||||
BOOST_CHECK(ReadFromStream(addrman2, ssPeers2));
|
||||
BOOST_CHECK(addrman2.size() == 3);
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
|
||||
BOOST_AUTO_TEST_CASE(load_addrman_corrupted)
|
||||
{
|
||||
CAddrManCorrupted addrmanCorrupted;
|
||||
|
||||
|
@ -1063,16 +1064,16 @@ BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
|
|||
} catch (const std::exception&) {
|
||||
exceptionThrown = true;
|
||||
}
|
||||
// Even through de-serialization failed addrman is not left in a clean state.
|
||||
// Even though de-serialization failed addrman is not left in a clean state.
|
||||
BOOST_CHECK(addrman1.size() == 1);
|
||||
BOOST_CHECK(exceptionThrown);
|
||||
|
||||
// Test that CAddrDB::Read fails if peers.dat is corrupt
|
||||
// Test that ReadFromStream fails if peers.dat is corrupt
|
||||
CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted);
|
||||
|
||||
CAddrMan addrman2(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
|
||||
BOOST_CHECK(addrman2.size() == 0);
|
||||
BOOST_CHECK(!CAddrDB::Read(addrman2, ssPeers2));
|
||||
BOOST_CHECK(!ReadFromStream(addrman2, ssPeers2));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <addrdb.h>
|
||||
#include <addrman.h>
|
||||
#include <net.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
|
@ -22,5 +23,5 @@ FUZZ_TARGET_INIT(data_stream_addr_man, initialize_data_stream_addr_man)
|
|||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
||||
CDataStream data_stream = ConsumeDataStream(fuzzed_data_provider);
|
||||
CAddrMan addr_man(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrDB::Read(addr_man, data_stream);
|
||||
ReadFromStream(addr_man, data_stream);
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <test/fuzz/fuzz.h>
|
||||
#include <test/fuzz/util.h>
|
||||
#include <uint256.h>
|
||||
#include <univalue.h>
|
||||
#include <util/check.h>
|
||||
#include <util/moneystr.h>
|
||||
#include <util/strencodings.h>
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <chainparams.h>
|
||||
#include <consensus/params.h>
|
||||
#include <primitives/block.h>
|
||||
#include <util/system.h>
|
||||
#include <versionbits.h>
|
||||
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
|
|
Loading…
Reference in a new issue