Skip to content

Commit 1c1a02b

Browse files
committed
Merge bitcoin/bitcoin#28341: refactor: Use HashWriter over legacy CHashWriter
99995cf refactor: Use HashWriter over legacy CHashWriter (via SerializeHash) (MarcoFalke) 5555aa2 refactor: Use HashWriter over legacy CHashWriter (MarcoFalke) Pull request description: `HashWriter` is a slim and less confusing version of `CHashWriter`, so use it in all places where it compiles. This should be correct, if it compiles. ACKs for top commit: sipa: That said, code review ACK 99995cf theuni: ACK 99995cf TheCharlatan: ACK 99995cf Tree-SHA512: fc967a18379bd00bd334ac3d50beb5435b65ca66a48f72623f1dcdbbce3292fd91839160cd0e69b8f4f3d98e258dcbbc6f73f5e91345f938898ee39c903a442b
2 parents db57574 + 99995cf commit 1c1a02b

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

src/addrman.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ static constexpr auto ADDRMAN_TEST_WINDOW{40min};
4545

4646
int AddrInfo::GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
4747
{
48-
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetCheapHash();
49-
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << netgroupman.GetGroup(*this) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
48+
uint64_t hash1 = (HashWriter{} << nKey << GetKey()).GetCheapHash();
49+
uint64_t hash2 = (HashWriter{} << nKey << netgroupman.GetGroup(*this) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
5050
return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
5151
}
5252

5353
int AddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const
5454
{
5555
std::vector<unsigned char> vchSourceGroupKey = netgroupman.GetGroup(src);
56-
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << netgroupman.GetGroup(*this) << vchSourceGroupKey).GetCheapHash();
57-
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
56+
uint64_t hash1 = (HashWriter{} << nKey << netgroupman.GetGroup(*this) << vchSourceGroupKey).GetCheapHash();
57+
uint64_t hash2 = (HashWriter{} << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
5858
return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
5959
}
6060

6161
int AddrInfo::GetBucketPosition(const uint256& nKey, bool fNew, int bucket) const
6262
{
63-
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? uint8_t{'N'} : uint8_t{'K'}) << bucket << GetKey()).GetCheapHash();
63+
uint64_t hash1 = (HashWriter{} << nKey << (fNew ? uint8_t{'N'} : uint8_t{'K'}) << bucket << GetKey()).GetCheapHash();
6464
return hash1 % ADDRMAN_BUCKET_SIZE;
6565
}
6666

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11951195
InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
11961196
return false;
11971197
}
1198-
const uint256 asmap_version = SerializeHash(asmap);
1198+
const uint256 asmap_version = (HashWriter{} << asmap).GetHash();
11991199
LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
12001200
} else {
12011201
LogPrintf("Using /16 prefix for IP bucketing\n");

src/netgroup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ uint256 NetGroupManager::GetAsmapChecksum() const
1111
{
1212
if (!m_asmap.size()) return {};
1313

14-
return SerializeHash(m_asmap);
14+
return (HashWriter{} << m_asmap).GetHash();
1515
}
1616

1717
std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const

src/test/addrman_tests.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket_legacy)
440440

441441
AddrInfo info1 = AddrInfo(addr1, source1);
442442

443-
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
444-
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
443+
uint256 nKey1 = (HashWriter{} << 1).GetHash();
444+
uint256 nKey2 = (HashWriter{} << 2).GetHash();
445445

446446
BOOST_CHECK_EQUAL(info1.GetTriedBucket(nKey1, EMPTY_NETGROUPMAN), 40);
447447

@@ -490,8 +490,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy)
490490

491491
AddrInfo info1 = AddrInfo(addr1, source1);
492492

493-
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
494-
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
493+
uint256 nKey1 = (HashWriter{} << 1).GetHash();
494+
uint256 nKey2 = (HashWriter{} << 2).GetHash();
495495

496496
// Test: Make sure the buckets are what we expect
497497
BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, EMPTY_NETGROUPMAN), 786);
@@ -568,8 +568,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
568568

569569
AddrInfo info1 = AddrInfo(addr1, source1);
570570

571-
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
572-
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
571+
uint256 nKey1 = (HashWriter{} << 1).GetHash();
572+
uint256 nKey2 = (HashWriter{} << 2).GetHash();
573573

574574
BOOST_CHECK_EQUAL(info1.GetTriedBucket(nKey1, ngm_asmap), 236);
575575

@@ -621,8 +621,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
621621

622622
AddrInfo info1 = AddrInfo(addr1, source1);
623623

624-
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
625-
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
624+
uint256 nKey1 = (HashWriter{} << 1).GetHash();
625+
uint256 nKey2 = (HashWriter{} << 2).GetHash();
626626

627627
// Test: Make sure the buckets are what we expect
628628
BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, ngm_asmap), 795);

0 commit comments

Comments
 (0)