diff --git a/src/addrman.cpp b/src/addrman.cpp index 206b54118e8..2e5149093c4 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 @@ -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 6c14f501eef..198c07aec55 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 970aa2b4120..5a81cd6bd9f 100644 --- a/src/netgroup.cpp +++ b/src/netgroup.cpp @@ -10,11 +10,9 @@ #include -uint256 NetGroupManager::GetAsmapChecksum() const +uint256 NetGroupManager::GetAsmapVersion() const { - if (!m_asmap.size()) return {}; - - return (HashWriter{} << m_asmap).GetHash(); + return AsmapVersion(m_asmap); } std::vector NetGroupManager::GetGroup(const CNetAddr& address) const diff --git a/src/netgroup.h b/src/netgroup.h index 399876e5bac..7f08fef766e 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 23cf6d165ac..1993103a9d9 100644 --- a/src/util/asmap.cpp +++ b/src/util/asmap.cpp @@ -5,9 +5,11 @@ #include #include +#include #include #include #include +#include #include #include @@ -223,3 +225,12 @@ std::vector DecodeAsmap(fs::path path) return buffer; } + +uint256 AsmapVersion(const std::vector& 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 b107ad25f46..722aeec7ab5 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 #include #include @@ -17,5 +18,7 @@ bool SanityCheckASMap(const std::vector& asmap, int bits); /** Read asmap from provided binary file */ std::vector DecodeAsmap(fs::path path); +/** Calculate the asmap version, a checksum identifying the asmap being used. */ +uint256 AsmapVersion(const std::vector& data); #endif // BITCOIN_UTIL_ASMAP_H