refactor: Unify asmap version calculation and naming

Calculate the asmap version only in one place: A dedicated function in util/asmap.

The version was also referred to as asmap checksum in several places. To avoid confusion call it asmap version everywhere.
This commit is contained in:
Fabian Jahr committed 2026-01-20 23:45:29 +01:00
1 parent fa41fc6a1a
commit 385c34a052
6 files changed
+27 -15

No files matched your search

+8 -8
View File
@@ -156,7 +156,7 @@ void AddrManImpl::Serialize(Stream& s_) const
* * for each new bucket: * * for each new bucket:
* * number of elements * * number of elements
* * for each element: index in the serialized "all new addresses" * * for each element: index in the serialized "all new addresses"
* * asmap checksum * * asmap version
* *
* 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it * 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it
* as incompatible. This is necessary because it did not check the version number on * as incompatible. This is necessary because it did not check the version number on
@@ -222,9 +222,9 @@ void AddrManImpl::Serialize(Stream& s_) const
} }
} }
} }
// Store asmap checksum after bucket entries so that it // Store asmap version after bucket entries so that it
// can be ignored by older clients for backward compatibility. // can be ignored by older clients for backward compatibility.
s << m_netgroupman.GetAsmapChecksum(); s << m_netgroupman.GetAsmapVersion();
} }
template <typename Stream> template <typename Stream>
@@ -330,16 +330,16 @@ void AddrManImpl::Unserialize(Stream& s_)
} }
} }
// If the bucket count and asmap checksum haven't changed, then attempt // If the bucket count and asmap version haven't changed, then attempt
// to restore the entries to the buckets/positions they were in before // to restore the entries to the buckets/positions they were in before
// serialization. // serialization.
uint256 supplied_asmap_checksum{m_netgroupman.GetAsmapChecksum()}; uint256 supplied_asmap_version{m_netgroupman.GetAsmapVersion()};
uint256 serialized_asmap_checksum; uint256 serialized_asmap_version;
if (format >= Format::V2_ASMAP) { if (format >= Format::V2_ASMAP) {
s >> serialized_asmap_checksum; s >> serialized_asmap_version;
} }
const bool restore_bucketing{nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && const bool restore_bucketing{nUBuckets == ADDRMAN_NEW_BUCKET_COUNT &&
serialized_asmap_checksum == supplied_asmap_checksum}; serialized_asmap_version == supplied_asmap_version};
if (!restore_bucketing) { if (!restore_bucketing) {
LogDebug(BCLog::ADDRMAN, "Bucketing method was updated, re-bucketing addrman entries from disk\n"); LogDebug(BCLog::ADDRMAN, "Bucketing method was updated, re-bucketing addrman entries from disk\n");
+1 -1
View File
@@ -1581,7 +1581,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path)))); InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
return false; return false;
} }
const uint256 asmap_version = (HashWriter{} << asmap).GetHash(); const uint256 asmap_version = AsmapVersion(asmap);;
LogInfo("Using asmap version %s for IP bucketing", asmap_version.ToString()); LogInfo("Using asmap version %s for IP bucketing", asmap_version.ToString());
} else { } else {
LogInfo("Using /16 prefix for IP bucketing"); LogInfo("Using /16 prefix for IP bucketing");
+2 -4
View File
@@ -10,11 +10,9 @@
#include <cstddef> #include <cstddef>
uint256 NetGroupManager::GetAsmapChecksum() const uint256 NetGroupManager::GetAsmapVersion() const
{ {
if (!m_asmap.size()) return {}; return AsmapVersion(m_asmap);
return (HashWriter{} << m_asmap).GetHash();
} }
std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
+2 -2
View File
@@ -20,8 +20,8 @@ public:
: m_asmap{std::move(asmap)} : m_asmap{std::move(asmap)}
{} {}
/** Get a checksum identifying the asmap being used. */ /** Get the asmap version, a checksum identifying the asmap being used. */
uint256 GetAsmapChecksum() const; uint256 GetAsmapVersion() const;
/** /**
* Get the canonical identifier of the network group for address. * Get the canonical identifier of the network group for address.
+11
View File
@@ -5,9 +5,11 @@
#include <util/asmap.h> #include <util/asmap.h>
#include <clientversion.h> #include <clientversion.h>
#include <hash.h>
#include <logging.h> #include <logging.h>
#include <serialize.h> #include <serialize.h>
#include <streams.h> #include <streams.h>
#include <uint256.h>
#include <util/fs.h> #include <util/fs.h>
#include <algorithm> #include <algorithm>
@@ -223,3 +225,12 @@ std::vector<std::byte> DecodeAsmap(fs::path path)
return buffer; return buffer;
} }
uint256 AsmapVersion(const std::vector<std::byte>& data)
{
if (data.empty()) return {};
HashWriter asmap_hasher;
asmap_hasher << data;
return asmap_hasher.GetHash();
}
+3
View File
@@ -5,6 +5,7 @@
#ifndef BITCOIN_UTIL_ASMAP_H #ifndef BITCOIN_UTIL_ASMAP_H
#define BITCOIN_UTIL_ASMAP_H #define BITCOIN_UTIL_ASMAP_H
#include <uint256.h>
#include <util/fs.h> #include <util/fs.h>
#include <cstddef> #include <cstddef>
@@ -17,5 +18,7 @@ bool SanityCheckASMap(const std::vector<std::byte>& asmap, int bits);
/** Read asmap from provided binary file */ /** Read asmap from provided binary file */
std::vector<std::byte> DecodeAsmap(fs::path path); std::vector<std::byte> DecodeAsmap(fs::path path);
/** Calculate the asmap version, a checksum identifying the asmap being used. */
uint256 AsmapVersion(const std::vector<std::byte>& data);
#endif // BITCOIN_UTIL_ASMAP_H #endif // BITCOIN_UTIL_ASMAP_H