refactor: Unify asmap version calculation and naming
What changed, and why it matters
This is a code cleanup change with no security impact. It moves the calculation of an 'asmap version' (previously called 'asmap checksum') into a single shared helper function and renames all references from 'checksum' to 'version' for consistency. The actual math used to compute the value is unchanged.
No security action required; routine code review/merge.
Security signals we found
No strong security signals were identified.
Evidence from the diff
Pure refactor: introduces AsmapVersion() in util/asmap.cpp/h, replaces inline HashWriter-based computation in init.cpp and NetGroupManager::GetAsmapChecksum(), and renames GetAsmapChecksum() to GetAsmapVersion() plus related variables/comments. The algorithm remains identical: HashWriter{} << data).GetHash(), with the same empty-data short-circuit returning a default uint256.
Changed components
src/util/asmap.cppsrc/util/asmap.hsrc/netgroup.cppsrc/netgroup.hsrc/addrman.cppsrc/init.cppInspect captured patch +27 / −15
diff --git a/src/addrman.cpp b/src/addrman.cpp
index 206b5411..2e514909 100644
--- a/src/addrman.cpp
+++ b/src/addrman.cpp
@@ -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");
diff --git a/src/init.cpp b/src/init.cpp
index 6c14f501..198c07ae 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -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");
diff --git a/src/netgroup.cpp b/src/netgroup.cpp
index 970aa2b4..5a81cd6b 100644
--- a/src/netgroup.cpp
+++ b/src/netgroup.cpp
@@ -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
diff --git a/src/netgroup.h b/src/netgroup.h
index 399876e5..7f08fef7 100644
--- a/src/netgroup.h
+++ b/src/netgroup.h
@@ -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.
diff --git a/src/util/asmap.cpp b/src/util/asmap.cpp
index 23cf6d16..1993103a 100644
--- a/src/util/asmap.cpp
+++ b/src/util/asmap.cpp
@@ -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();
+}
diff --git a/src/util/asmap.h b/src/util/asmap.h
index b107ad25..722aeec7 100644
--- a/src/util/asmap.h
+++ b/src/util/asmap.h
@@ -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
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.