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 2025-04-22 23:44:06 +02:00
parent fa41fc6a1a
commit 385c34a052
No known key found for this signature in database
GPG Key ID: F13D1E9D890798CD
6 changed files with 27 additions and 15 deletions

View File

@ -156,7 +156,7 @@ void AddrManImpl::Serialize(Stream& s_) const
* * for each new bucket:
* * number of elements
* * 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
* 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.
s << m_netgroupman.GetAsmapChecksum();
s << m_netgroupman.GetAsmapVersion();
}
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
// serialization.
uint256 supplied_asmap_checksum{m_netgroupman.GetAsmapChecksum()};
uint256 serialized_asmap_checksum;
uint256 supplied_asmap_version{m_netgroupman.GetAsmapVersion()};
uint256 serialized_asmap_version;
if (format >= Format::V2_ASMAP) {
s >> serialized_asmap_checksum;
s >> serialized_asmap_version;
}
const bool restore_bucketing{nUBuckets == ADDRMAN_NEW_BUCKET_COUNT &&
serialized_asmap_checksum == supplied_asmap_checksum};
serialized_asmap_version == supplied_asmap_version};
if (!restore_bucketing) {
LogDebug(BCLog::ADDRMAN, "Bucketing method was updated, re-bucketing addrman entries from disk\n");

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))));
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());
} else {
LogInfo("Using /16 prefix for IP bucketing");

View File

@ -10,11 +10,9 @@
#include <cstddef>
uint256 NetGroupManager::GetAsmapChecksum() const
uint256 NetGroupManager::GetAsmapVersion() const
{
if (!m_asmap.size()) return {};
return (HashWriter{} << m_asmap).GetHash();
return AsmapVersion(m_asmap);
}
std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const

View File

@ -20,8 +20,8 @@ public:
: m_asmap{std::move(asmap)}
{}
/** Get a checksum identifying the asmap being used. */
uint256 GetAsmapChecksum() const;
/** Get the asmap version, a checksum identifying the asmap being used. */
uint256 GetAsmapVersion() const;
/**
* Get the canonical identifier of the network group for address.

View File

@ -5,9 +5,11 @@
#include <util/asmap.h>
#include <clientversion.h>
#include <hash.h>
#include <logging.h>
#include <serialize.h>
#include <streams.h>
#include <uint256.h>
#include <util/fs.h>
#include <algorithm>
@ -223,3 +225,12 @@ std::vector<std::byte> DecodeAsmap(fs::path path)
return buffer;
}
uint256 AsmapVersion(const std::vector<std::byte>& data)
{
if (data.empty()) return {};
HashWriter asmap_hasher;
asmap_hasher << data;
return asmap_hasher.GetHash();
}

View File

@ -5,6 +5,7 @@
#ifndef BITCOIN_UTIL_ASMAP_H
#define BITCOIN_UTIL_ASMAP_H
#include <uint256.h>
#include <util/fs.h>
#include <cstddef>
@ -17,5 +18,7 @@ bool SanityCheckASMap(const std::vector<std::byte>& asmap, int bits);
/** Read asmap from provided binary file */
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