Skip to content

Commit

Permalink
Merge #22915: Remove confusing CAddrDB
Browse files Browse the repository at this point in the history
fade9a1 Remove confusing CAddrDB (MarcoFalke)
fa7f77b Fix addrdb includes (MarcoFalke)
fa3f5d0 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 ACK fade9a1
  lsilva01:
    Code Review ACK fade9a1

Tree-SHA512: 7615fb0b6235d0c1e6f8cd6263dd18c4d95890567c2b797fe6fce6cb12cc85ce6eacbe07dbb6d81b05d179ef03b42edfd61c940e35a1044ce6d363b54c2dae5c
  • Loading branch information
MarcoFalke committed Sep 9, 2021
2 parents 8805e06 + fade9a1 commit 8966499
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 46 deletions.
13 changes: 5 additions & 8 deletions src/addrdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
18 changes: 6 additions & 12 deletions src/addrdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
11 changes: 2 additions & 9 deletions src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
5 changes: 2 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <net.h>

#include <addrdb.h>
#include <banman.h>
#include <clientversion.h>
#include <compat.h>
Expand All @@ -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>
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#ifndef BITCOIN_NET_H
#define BITCOIN_NET_H

#include <addrdb.h>
#include <addrman.h>
#include <amount.h>
#include <bloom.h>
Expand Down
1 change: 1 addition & 0 deletions src/qt/bantablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef BITCOIN_QT_BANTABLEMODEL_H
#define BITCOIN_QT_BANTABLEMODEL_H

#include <addrdb.h>
#include <net.h>

#include <memory>
Expand Down
1 change: 1 addition & 0 deletions src/qt/walletframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <qt/psbtoperationsdialog.h>
#include <qt/walletmodel.h>
#include <qt/walletview.h>
#include <util/system.h>

#include <cassert>

Expand Down
21 changes: 11 additions & 10 deletions src/test/addrman_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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));
}


Expand Down
3 changes: 2 additions & 1 deletion src/test/fuzz/data_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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);
}
1 change: 1 addition & 0 deletions src/test/fuzz/integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions src/test/fuzz/versionbits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 8966499

Please sign in to comment.