From 4709fc2019e27e74be02dc5fc123b9f6f46d7990 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Wed, 1 Sep 2021 16:34:23 +0100 Subject: [PATCH] [netgroupman] Move asmap checksum calculation to NetGroupManager --- src/addrman.cpp | 11 ++--------- src/netgroup.cpp | 8 ++++++++ src/netgroup.h | 4 ++++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index 7abec560eb3..f74729d47b0 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -218,11 +218,7 @@ void AddrManImpl::Serialize(Stream& s_) const } // Store asmap checksum after bucket entries so that it // can be ignored by older clients for backward compatibility. - uint256 asmap_checksum; - if (m_netgroupman.GetAsmap().size() != 0) { - asmap_checksum = SerializeHash(m_netgroupman.GetAsmap()); - } - s << asmap_checksum; + s << m_netgroupman.GetAsmapChecksum(); } template @@ -335,10 +331,7 @@ void AddrManImpl::Unserialize(Stream& s_) // If the bucket count and asmap checksum haven't changed, then attempt // to restore the entries to the buckets/positions they were in before // serialization. - uint256 supplied_asmap_checksum; - if (m_netgroupman.GetAsmap().size() != 0) { - supplied_asmap_checksum = SerializeHash(m_netgroupman.GetAsmap()); - } + uint256 supplied_asmap_checksum{m_netgroupman.GetAsmapChecksum()}; uint256 serialized_asmap_checksum; if (format >= Format::V2_ASMAP) { s >> serialized_asmap_checksum; diff --git a/src/netgroup.cpp b/src/netgroup.cpp index a2f1f3460f9..5f42d6c7191 100644 --- a/src/netgroup.cpp +++ b/src/netgroup.cpp @@ -4,8 +4,16 @@ #include +#include #include +uint256 NetGroupManager::GetAsmapChecksum() const +{ + if (!m_asmap.size()) return {}; + + return SerializeHash(m_asmap); +} + std::vector NetGroupManager::GetGroup(const CNetAddr& address) const { std::vector vchRet; diff --git a/src/netgroup.h b/src/netgroup.h index 4e4edb8a88a..6f495b166be 100644 --- a/src/netgroup.h +++ b/src/netgroup.h @@ -6,6 +6,7 @@ #define BITCOIN_NETGROUP_H #include +#include #include @@ -22,6 +23,9 @@ public: * exists, since the data is const. */ const std::vector& GetAsmap() const { return m_asmap; } + /** Get a checksum identifying the asmap being used. */ + uint256 GetAsmapChecksum() const; + /** * Get the canonical identifier of the network group for address. *