Skip to content

Commit 77ccb7f

Browse files
naumenkogsjonatack
authored andcommitted
Use std::chrono for salting when randomizing ADDR destination
1 parent e04720e commit 77ccb7f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/net_processing.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS = 288;
134134
static constexpr auto AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL{24h};
135135
/** Average delay between peer address broadcasts */
136136
static constexpr auto AVG_ADDRESS_BROADCAST_INTERVAL{30s};
137+
/** Delay between rotating the peers we relay a particular address to */
138+
static constexpr auto ROTATE_ADDR_RELAY_DEST_INTERVAL{24h};
137139
/** Average delay between trickled inventory transmissions for inbound peers.
138140
* Blocks and peers with NetPermissionFlags::NoBan permission bypass this. */
139141
static constexpr auto INBOUND_INVENTORY_BROADCAST_INTERVAL{5s};
@@ -1776,9 +1778,12 @@ void PeerManagerImpl::RelayAddress(NodeId originator,
17761778
// Use deterministic randomness to send to the same nodes for 24 hours
17771779
// at a time so the m_addr_knowns of the chosen nodes prevent repeats
17781780
const uint64_t hash_addr{CServiceHash(0, 0)(addr)};
1781+
const auto current_time{GetTime<std::chrono::seconds>()};
1782+
// Adding address hash makes exact rotation time different per address, while preserving periodicity.
1783+
const uint64_t time_addr{(static_cast<uint64_t>(count_seconds(current_time)) + hash_addr) / count_seconds(ROTATE_ADDR_RELAY_DEST_INTERVAL)};
17791784
const CSipHasher hasher{m_connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY)
17801785
.Write(hash_addr)
1781-
.Write((GetTime() + hash_addr) / (24 * 60 * 60))};
1786+
.Write(time_addr)};
17821787
FastRandomContext insecure_rand;
17831788

17841789
// Relay reachable addresses to 2 peers. Unreachable addresses are relayed randomly to 1 or 2 peers.

0 commit comments

Comments
 (0)