merge bitcoin#22911: Minor cleanups to asmap

This commit is contained in:
Kittywhiskers Van Gogh 2024-05-29 20:31:11 +00:00
parent 5706edad5f
commit 29f4482e01
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
11 changed files with 50 additions and 52 deletions

View File

@ -1048,30 +1048,3 @@ CAddrInfo CAddrMan::SelectTriedCollision_()
return mapInfo[id_old];
}
std::vector<bool> CAddrMan::DecodeAsmap(fs::path path)
{
std::vector<bool> bits;
FILE *filestr = fsbridge::fopen(path, "rb");
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
if (file.IsNull()) {
LogPrintf("Failed to open asmap file from disk\n");
return bits;
}
fseek(filestr, 0, SEEK_END);
int length = ftell(filestr);
LogPrintf("Opened asmap file %s (%d bytes) from disk\n", path, length);
fseek(filestr, 0, SEEK_SET);
uint8_t cur_byte;
for (int i = 0; i < length; ++i) {
file >> cur_byte;
for (int bit = 0; bit < 8; ++bit) {
bits.push_back((cur_byte >> bit) & 1);
}
}
if (!SanityCheckASMap(bits)) {
LogPrintf("Sanity check of asmap file %s failed\n", path);
return {};
}
return bits;
}

View File

@ -142,9 +142,6 @@ static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
class CAddrMan
{
public:
// Read asmap from provided binary file
static std::vector<bool> DecodeAsmap(fs::path path);
template <typename Stream>
void Serialize(Stream& s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);

View File

@ -1681,7 +1681,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
InitError(strprintf(_("Could not find asmap file %s"), asmap_path));
return false;
}
asmap = CAddrMan::DecodeAsmap(asmap_path);
asmap = DecodeAsmap(asmap_path);
if (asmap.size() == 0) {
InitError(strprintf(_("Could not parse asmap file %s"), asmap_path));
return false;

View File

@ -666,14 +666,13 @@ Network CNode::ConnectedThroughNetwork() const
#undef X
#define X(name) stats.name = name
void CNode::CopyStats(CNodeStats& stats, const std::vector<bool>& asmap)
void CNode::CopyStats(CNodeStats& stats)
{
stats.nodeid = this->GetId();
X(nServices);
X(addr);
X(addrBind);
stats.m_network = ConnectedThroughNetwork();
stats.m_mapped_as = addr.GetMappedAS(asmap);
X(m_last_send);
X(m_last_recv);
X(nLastTXTime);
@ -3871,7 +3870,8 @@ void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats) const
continue;
}
vstats.emplace_back();
pnode->CopyStats(vstats.back(), addrman.GetAsmap());
pnode->CopyStats(vstats.back());
vstats.back().m_mapped_as = pnode->addr.GetMappedAS(addrman.GetAsmap());
}
}

View File

@ -678,7 +678,7 @@ public:
void CloseSocketDisconnect(CConnman* connman) EXCLUSIVE_LOCKS_REQUIRED(!cs_hSocket);
void CopyStats(CNodeStats& stats, const std::vector<bool>& asmap) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex, !m_addr_local_mutex, !cs_vSend, !cs_vRecv);
void CopyStats(CNodeStats& stats) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex, !m_addr_local_mutex, !cs_vSend, !cs_vRecv);
ServiceFlags GetLocalServices() const
{
@ -855,7 +855,6 @@ public:
std::vector<std::string> m_specified_outgoing;
std::vector<std::string> m_added_nodes;
SocketEventsMode socketEventsMode = SocketEventsMode::Select;
std::vector<bool> m_asmap;
bool m_i2p_accept_incoming;
};

View File

@ -1253,8 +1253,3 @@ bool operator<(const CSubNet& a, const CSubNet& b)
{
return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
}
bool SanityCheckASMap(const std::vector<bool>& asmap)
{
return SanityCheckASMap(asmap, 128); // For IP address lookups, the input is 128 bits
}

View File

@ -583,6 +583,4 @@ public:
}
};
bool SanityCheckASMap(const std::vector<bool>& asmap);
#endif // BITCOIN_NETADDRESS_H

View File

@ -221,7 +221,7 @@ public:
[[nodiscard]] inline std::vector<bool> ConsumeAsmap(FuzzedDataProvider& fuzzed_data_provider) noexcept
{
std::vector<bool> asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
if (!SanityCheckASMap(asmap)) asmap.clear();
if (!SanityCheckASMap(asmap, 128)) asmap.clear();
return asmap;
}

View File

@ -14,6 +14,7 @@
#include <test/fuzz/util.h>
#include <test/util/net.h>
#include <test/util/setup_common.h>
#include <util/asmap.h>
#include <cstdint>
#include <optional>
@ -41,12 +42,8 @@ FUZZ_TARGET_INIT(net, initialize_net)
node.CloseSocketDisconnect(&connman);
},
[&] {
const std::vector<bool> asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
if (!SanityCheckASMap(asmap)) {
return;
}
CNodeStats stats;
node.CopyStats(stats, asmap);
node.CopyStats(stats);
},
[&] {
const CNode* add_ref_node = node.AddRef();

View File

@ -2,10 +2,16 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <assert.h>
#include <util/asmap.h>
#include <clientversion.h>
#include <crypto/common.h>
#include <logging.h>
#include <streams.h>
#include <cassert>
#include <map>
#include <vector>
#include <crypto/common.h>
namespace {
@ -183,3 +189,31 @@ bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
}
return false; // Reached EOF without RETURN instruction
}
std::vector<bool> DecodeAsmap(fs::path path)
{
std::vector<bool> bits;
FILE *filestr = fsbridge::fopen(path, "rb");
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
if (file.IsNull()) {
LogPrintf("Failed to open asmap file from disk\n");
return bits;
}
fseek(filestr, 0, SEEK_END);
int length = ftell(filestr);
LogPrintf("Opened asmap file %s (%d bytes) from disk\n", path, length);
fseek(filestr, 0, SEEK_SET);
uint8_t cur_byte;
for (int i = 0; i < length; ++i) {
file >> cur_byte;
for (int bit = 0; bit < 8; ++bit) {
bits.push_back((cur_byte >> bit) & 1);
}
}
if (!SanityCheckASMap(bits, 128)) {
LogPrintf("Sanity check of asmap file %s failed\n", path);
return {};
}
return bits;
}

View File

@ -5,11 +5,16 @@
#ifndef BITCOIN_UTIL_ASMAP_H
#define BITCOIN_UTIL_ASMAP_H
#include <stdint.h>
#include <fs.h>
#include <cstdint>
#include <vector>
uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip);
bool SanityCheckASMap(const std::vector<bool>& asmap, int bits);
/** Read asmap from provided binary file */
std::vector<bool> DecodeAsmap(fs::path path);
#endif // BITCOIN_UTIL_ASMAP_H