From 7da74ffcd56305555aecc4a3d6b2be9e3c53cedf Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Fri, 25 Aug 2023 16:57:52 +0200 Subject: [PATCH] merge bitcoin#28341: Use HashWriter over legacy CHashWriter --- src/addrman.cpp | 10 +++++----- src/init.cpp | 2 +- src/netgroup.cpp | 2 +- src/test/addrman_tests.cpp | 16 ++++++++-------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index 190dbda749..95bf5eb9c6 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -45,22 +45,22 @@ static constexpr int64_t ADDRMAN_TEST_WINDOW{40*60}; // 40 minutes int AddrInfo::GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const { - uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetCheapHash(); - uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << netgroupman.GetGroup(*this) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash(); + uint64_t hash1 = (HashWriter{} << nKey << GetKey()).GetCheapHash(); + uint64_t hash2 = (HashWriter{} << nKey << netgroupman.GetGroup(*this) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash(); return hash2 % ADDRMAN_TRIED_BUCKET_COUNT; } int AddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const { std::vector vchSourceGroupKey = netgroupman.GetGroup(src); - uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << netgroupman.GetGroup(*this) << vchSourceGroupKey).GetCheapHash(); - uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash(); + uint64_t hash1 = (HashWriter{} << nKey << netgroupman.GetGroup(*this) << vchSourceGroupKey).GetCheapHash(); + uint64_t hash2 = (HashWriter{} << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash(); return hash2 % ADDRMAN_NEW_BUCKET_COUNT; } int AddrInfo::GetBucketPosition(const uint256& nKey, bool fNew, int bucket) const { - uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? uint8_t{'N'} : uint8_t{'K'}) << bucket << GetKey()).GetCheapHash(); + uint64_t hash1 = (HashWriter{} << nKey << (fNew ? uint8_t{'N'} : uint8_t{'K'}) << bucket << GetKey()).GetCheapHash(); return hash1 % ADDRMAN_BUCKET_SIZE; } diff --git a/src/init.cpp b/src/init.cpp index 9bcb41832d..18decacf04 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1566,7 +1566,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 = SerializeHash(asmap); + const uint256 asmap_version = (HashWriter{} << asmap).GetHash(); LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString()); } else { LogPrintf("Using /16 prefix for IP bucketing\n"); diff --git a/src/netgroup.cpp b/src/netgroup.cpp index 5f42d6c719..3dc59725e1 100644 --- a/src/netgroup.cpp +++ b/src/netgroup.cpp @@ -11,7 +11,7 @@ uint256 NetGroupManager::GetAsmapChecksum() const { if (!m_asmap.size()) return {}; - return SerializeHash(m_asmap); + return (HashWriter{} << m_asmap).GetHash(); } std::vector NetGroupManager::GetGroup(const CNetAddr& address) const diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index 6a69c8eac2..4167c2b867 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -437,8 +437,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket_legacy) AddrInfo info1 = AddrInfo(addr1, source1); - uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash(); - uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash(); + uint256 nKey1 = (HashWriter{} << 1).GetHash(); + uint256 nKey2 = (HashWriter{} << 2).GetHash(); BOOST_CHECK_EQUAL(info1.GetTriedBucket(nKey1, EMPTY_NETGROUPMAN), 40); @@ -487,8 +487,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy) AddrInfo info1 = AddrInfo(addr1, source1); - uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash(); - uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash(); + uint256 nKey1 = (HashWriter{} << 1).GetHash(); + uint256 nKey2 = (HashWriter{} << 2).GetHash(); // Test: Make sure the buckets are what we expect BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, EMPTY_NETGROUPMAN), 786); @@ -565,8 +565,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) AddrInfo info1 = AddrInfo(addr1, source1); - uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash(); - uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash(); + uint256 nKey1 = (HashWriter{} << 1).GetHash(); + uint256 nKey2 = (HashWriter{} << 2).GetHash(); BOOST_CHECK_EQUAL(info1.GetTriedBucket(nKey1, ngm_asmap), 236); @@ -618,8 +618,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) AddrInfo info1 = AddrInfo(addr1, source1); - uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash(); - uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash(); + uint256 nKey1 = (HashWriter{} << 1).GetHash(); + uint256 nKey2 = (HashWriter{} << 2).GetHash(); // Test: Make sure the buckets are what we expect BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, ngm_asmap), 795);