Skip to content

Commit

Permalink
Merge bitcoin#28341: refactor: Use HashWriter over legacy CHashWriter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fanquake committed Aug 28, 2023
2 parents db57574 + 99995cf commit 1c1a02b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ static constexpr auto ADDRMAN_TEST_WINDOW{40min};

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<unsigned char> 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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,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");
Expand Down
2 changes: 1 addition & 1 deletion src/netgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ uint256 NetGroupManager::GetAsmapChecksum() const
{
if (!m_asmap.size()) return {};

return SerializeHash(m_asmap);
return (HashWriter{} << m_asmap).GetHash();
}

std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
Expand Down
16 changes: 8 additions & 8 deletions src/test/addrman_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,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);

Expand Down Expand Up @@ -490,8 +490,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);
Expand Down Expand Up @@ -568,8 +568,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);

Expand Down Expand Up @@ -621,8 +621,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);
Expand Down

0 comments on commit 1c1a02b

Please sign in to comment.