mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
[addrman] Set m_asmap in CAddrMan initializer list
This allows us to make it const.
This commit is contained in:
parent
593247872d
commit
f572f2b204
10 changed files with 38 additions and 36 deletions
|
@ -77,8 +77,9 @@ double CAddrInfo::GetChance(int64_t nNow) const
|
|||
return fChance;
|
||||
}
|
||||
|
||||
CAddrMan::CAddrMan(bool deterministic, int32_t consistency_check_ratio)
|
||||
: insecure_rand{deterministic}
|
||||
CAddrMan::CAddrMan(std::vector<bool> asmap, bool deterministic, int32_t consistency_check_ratio)
|
||||
: m_asmap{std::move(asmap)}
|
||||
, insecure_rand{deterministic}
|
||||
, nKey{deterministic ? uint256{1} : insecure_rand.rand256()}
|
||||
, m_consistency_check_ratio{consistency_check_ratio}
|
||||
{
|
||||
|
|
|
@ -195,7 +195,7 @@ public:
|
|||
//
|
||||
// If a new asmap was provided, the existing records
|
||||
// would be re-bucketed accordingly.
|
||||
std::vector<bool> m_asmap;
|
||||
const std::vector<bool> m_asmap;
|
||||
|
||||
// Read asmap from provided binary file
|
||||
static std::vector<bool> DecodeAsmap(fs::path path);
|
||||
|
@ -471,7 +471,7 @@ public:
|
|||
Check();
|
||||
}
|
||||
|
||||
explicit CAddrMan(bool deterministic, int32_t consistency_check_ratio);
|
||||
explicit CAddrMan(std::vector<bool> asmap, bool deterministic, int32_t consistency_check_ratio);
|
||||
|
||||
~CAddrMan()
|
||||
{
|
||||
|
|
|
@ -73,14 +73,14 @@ static void AddrManAdd(benchmark::Bench& bench)
|
|||
CreateAddresses();
|
||||
|
||||
bench.run([&] {
|
||||
CAddrMan addrman{/* deterministic */ false, /* consistency_check_ratio */ 0};
|
||||
CAddrMan addrman{/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0};
|
||||
AddAddressesToAddrMan(addrman);
|
||||
});
|
||||
}
|
||||
|
||||
static void AddrManSelect(benchmark::Bench& bench)
|
||||
{
|
||||
CAddrMan addrman(/* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrMan addrman(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
|
||||
FillAddrMan(addrman);
|
||||
|
||||
|
@ -92,7 +92,7 @@ static void AddrManSelect(benchmark::Bench& bench)
|
|||
|
||||
static void AddrManGetAddr(benchmark::Bench& bench)
|
||||
{
|
||||
CAddrMan addrman(/* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrMan addrman(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
|
||||
FillAddrMan(addrman);
|
||||
|
||||
|
@ -114,7 +114,7 @@ static void AddrManGood(benchmark::Bench& bench)
|
|||
|
||||
std::vector<std::unique_ptr<CAddrMan>> addrmans(addrman_count);
|
||||
for (size_t i{0}; i < addrman_count; ++i) {
|
||||
addrmans[i] = std::make_unique<CAddrMan>(/* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
addrmans[i] = std::make_unique<CAddrMan>(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
FillAddrMan(*addrmans[i]);
|
||||
}
|
||||
|
||||
|
|
|
@ -1201,8 +1201,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
}
|
||||
|
||||
auto check_addrman = std::clamp<int32_t>(args.GetArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000);
|
||||
node.addrman = std::make_unique<CAddrMan>(/* deterministic */ false, /* consistency_check_ratio */ check_addrman);
|
||||
node.addrman->m_asmap = asmap;
|
||||
node.addrman = std::make_unique<CAddrMan>(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman);
|
||||
|
||||
// Load addresses from peers.dat
|
||||
uiInterface.InitMessage(_("Loading P2P addresses…").translated);
|
||||
|
@ -1212,8 +1211,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
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>(/* deterministic */ false, /* consistency_check_ratio */ check_addrman);
|
||||
node.addrman->m_asmap = asmap;
|
||||
node.addrman = std::make_unique<CAddrMan>(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman);
|
||||
LogPrintf("Recreating peers.dat\n");
|
||||
adb.Write(*node.addrman);
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ public:
|
|||
virtual void Serialize(CDataStream& s) const = 0;
|
||||
|
||||
CAddrManSerializationMock()
|
||||
: CAddrMan(/* deterministic */ true, /* consistency_check_ratio */ 100)
|
||||
: CAddrMan(/* asmap */ std::vector<bool>(), /* deterministic */ true, /* consistency_check_ratio */ 100)
|
||||
{}
|
||||
};
|
||||
|
||||
|
@ -82,10 +82,9 @@ private:
|
|||
public:
|
||||
explicit CAddrManTest(bool makeDeterministic = true,
|
||||
std::vector<bool> asmap = std::vector<bool>())
|
||||
: CAddrMan(makeDeterministic, /* consistency_check_ratio */ 100)
|
||||
: CAddrMan(asmap, makeDeterministic, /* consistency_check_ratio */ 100)
|
||||
{
|
||||
deterministic = makeDeterministic;
|
||||
m_asmap = asmap;
|
||||
}
|
||||
|
||||
CAddrInfo* Find(const CNetAddr& addr, int* pnId = nullptr)
|
||||
|
@ -1024,7 +1023,7 @@ BOOST_AUTO_TEST_CASE(caddrdb_read)
|
|||
// Test that the de-serialization does not throw an exception.
|
||||
CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted);
|
||||
bool exceptionThrown = false;
|
||||
CAddrMan addrman1(/* deterministic */ false, /* consistency_check_ratio */ 100);
|
||||
CAddrMan addrman1(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
|
||||
|
||||
BOOST_CHECK(addrman1.size() == 0);
|
||||
try {
|
||||
|
@ -1041,7 +1040,7 @@ BOOST_AUTO_TEST_CASE(caddrdb_read)
|
|||
// Test that CAddrDB::Read creates an addrman with the correct number of addrs.
|
||||
CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted);
|
||||
|
||||
CAddrMan addrman2(/* deterministic */ false, /* consistency_check_ratio */ 100);
|
||||
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(addrman2.size() == 3);
|
||||
|
@ -1055,7 +1054,7 @@ BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
|
|||
// Test that the de-serialization of corrupted addrman throws an exception.
|
||||
CDataStream ssPeers1 = AddrmanToStream(addrmanCorrupted);
|
||||
bool exceptionThrown = false;
|
||||
CAddrMan addrman1(/* deterministic */ false, /* consistency_check_ratio */ 100);
|
||||
CAddrMan addrman1(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
|
||||
BOOST_CHECK(addrman1.size() == 0);
|
||||
try {
|
||||
unsigned char pchMsgTmp[4];
|
||||
|
@ -1071,7 +1070,7 @@ BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
|
|||
// Test that CAddrDB::Read fails if peers.dat is corrupt
|
||||
CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted);
|
||||
|
||||
CAddrMan addrman2(/* deterministic */ false, /* consistency_check_ratio */ 100);
|
||||
CAddrMan addrman2(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
|
||||
BOOST_CHECK(addrman2.size() == 0);
|
||||
BOOST_CHECK(!CAddrDB::Read(addrman2, ssPeers2));
|
||||
}
|
||||
|
|
|
@ -28,17 +28,11 @@ class CAddrManDeterministic : public CAddrMan
|
|||
public:
|
||||
FuzzedDataProvider& m_fuzzed_data_provider;
|
||||
|
||||
explicit CAddrManDeterministic(FuzzedDataProvider& fuzzed_data_provider)
|
||||
: CAddrMan(/* deterministic */ true, /* consistency_check_ratio */ 0)
|
||||
explicit CAddrManDeterministic(std::vector<bool> asmap, FuzzedDataProvider& fuzzed_data_provider)
|
||||
: CAddrMan(std::move(asmap), /* deterministic */ true, /* consistency_check_ratio */ 0)
|
||||
, m_fuzzed_data_provider(fuzzed_data_provider)
|
||||
{
|
||||
WITH_LOCK(cs, insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
|
||||
if (fuzzed_data_provider.ConsumeBool()) {
|
||||
m_asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
|
||||
if (!SanityCheckASMap(m_asmap)) {
|
||||
m_asmap.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -228,7 +222,14 @@ FUZZ_TARGET_INIT(addrman, initialize_addrman)
|
|||
{
|
||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||
SetMockTime(ConsumeTime(fuzzed_data_provider));
|
||||
auto addr_man_ptr = std::make_unique<CAddrManDeterministic>(fuzzed_data_provider);
|
||||
std::vector<bool> asmap;
|
||||
if (fuzzed_data_provider.ConsumeBool()) {
|
||||
asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
|
||||
if (!SanityCheckASMap(asmap)) {
|
||||
asmap.clear();
|
||||
}
|
||||
}
|
||||
auto addr_man_ptr = std::make_unique<CAddrManDeterministic>(asmap, fuzzed_data_provider);
|
||||
if (fuzzed_data_provider.ConsumeBool()) {
|
||||
const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
|
||||
CDataStream ds(serialized_data, SER_DISK, INIT_PROTO_VERSION);
|
||||
|
@ -237,7 +238,7 @@ FUZZ_TARGET_INIT(addrman, initialize_addrman)
|
|||
try {
|
||||
ds >> *addr_man_ptr;
|
||||
} catch (const std::ios_base::failure&) {
|
||||
addr_man_ptr = std::make_unique<CAddrManDeterministic>(fuzzed_data_provider);
|
||||
addr_man_ptr = std::make_unique<CAddrManDeterministic>(asmap, fuzzed_data_provider);
|
||||
}
|
||||
}
|
||||
CAddrManDeterministic& addr_man = *addr_man_ptr;
|
||||
|
@ -306,9 +307,12 @@ FUZZ_TARGET_INIT(addrman_serdeser, initialize_addrman)
|
|||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||
SetMockTime(ConsumeTime(fuzzed_data_provider));
|
||||
|
||||
CAddrManDeterministic addr_man1{fuzzed_data_provider};
|
||||
CAddrManDeterministic addr_man2{fuzzed_data_provider};
|
||||
addr_man2.m_asmap = addr_man1.m_asmap;
|
||||
std::vector<bool> asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
|
||||
if (!SanityCheckASMap(asmap)) {
|
||||
asmap.clear();
|
||||
}
|
||||
CAddrManDeterministic addr_man1{asmap, fuzzed_data_provider};
|
||||
CAddrManDeterministic addr_man2{asmap, fuzzed_data_provider};
|
||||
|
||||
CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION);
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ FUZZ_TARGET_INIT(connman, initialize_connman)
|
|||
{
|
||||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
||||
SetMockTime(ConsumeTime(fuzzed_data_provider));
|
||||
CAddrMan addrman(/* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrMan addrman(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CConnman connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>(), addrman, fuzzed_data_provider.ConsumeBool()};
|
||||
CNetAddr random_netaddr;
|
||||
CNode random_node = ConsumeNode(fuzzed_data_provider);
|
||||
|
|
|
@ -21,6 +21,6 @@ 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(/* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrMan addr_man(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrDB::Read(addr_man, data_stream);
|
||||
}
|
||||
|
|
|
@ -188,7 +188,7 @@ FUZZ_TARGET_DESERIALIZE(blockmerkleroot, {
|
|||
BlockMerkleRoot(block, &mutated);
|
||||
})
|
||||
FUZZ_TARGET_DESERIALIZE(addrman_deserialize, {
|
||||
CAddrMan am(/* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
CAddrMan am(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
DeserializeFromFuzzingInput(buffer, am);
|
||||
})
|
||||
FUZZ_TARGET_DESERIALIZE(blockheader_deserialize, {
|
||||
|
|
|
@ -193,7 +193,7 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
|
|||
throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", state.ToString()));
|
||||
}
|
||||
|
||||
m_node.addrman = std::make_unique<CAddrMan>(/* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
m_node.addrman = std::make_unique<CAddrMan>(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
|
||||
m_node.banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME);
|
||||
m_node.connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman); // Deterministic randomness for tests.
|
||||
m_node.peerman = PeerManager::make(chainparams, *m_node.connman, *m_node.addrman,
|
||||
|
|
Loading…
Add table
Reference in a new issue