Skip to content

Commit 5555aa2

Browse files
author
MarcoFalke
committed
refactor: Use HashWriter over legacy CHashWriter
1 parent c9273f6 commit 5555aa2

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
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/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)