Skip to content

Commit 2e38a0e

Browse files
committed
net: add CServiceHash constructor so the caller can provide the salts
This new constructor will be useful if we just want to hash a `CService` object without the two `GetRand()` calls (in `RelayAddress()` in a subsequent commit).
1 parent 9720863 commit 2e38a0e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/netaddress.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,14 @@ class CService : public CNetAddr
562562
class CServiceHash
563563
{
564564
public:
565+
CServiceHash()
566+
: m_salt_k0{GetRand(std::numeric_limits<uint64_t>::max())},
567+
m_salt_k1{GetRand(std::numeric_limits<uint64_t>::max())}
568+
{
569+
}
570+
571+
CServiceHash(uint64_t salt_k0, uint64_t salt_k1) : m_salt_k0{salt_k0}, m_salt_k1{salt_k1} {}
572+
565573
size_t operator()(const CService& a) const noexcept
566574
{
567575
CSipHasher hasher(m_salt_k0, m_salt_k1);
@@ -572,8 +580,8 @@ class CServiceHash
572580
}
573581

574582
private:
575-
const uint64_t m_salt_k0 = GetRand(std::numeric_limits<uint64_t>::max());
576-
const uint64_t m_salt_k1 = GetRand(std::numeric_limits<uint64_t>::max());
583+
const uint64_t m_salt_k0;
584+
const uint64_t m_salt_k1;
577585
};
578586

579587
#endif // BITCOIN_NETADDRESS_H

src/test/fuzz/netaddress.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ FUZZ_TARGET(netaddress)
8484
(void)service.ToString();
8585
(void)service.ToStringIPPort();
8686
(void)service.ToStringPort();
87+
(void)CServiceHash()(service);
88+
(void)CServiceHash(0, 0)(service);
8789

8890
const CNetAddr other_net_addr = ConsumeNetAddr(fuzzed_data_provider);
8991
(void)net_addr.GetReachabilityFrom(&other_net_addr);

0 commit comments

Comments
 (0)