user134486: I read that bitcoin uses random delay when relaying transaction to other peers in order to protect privacy. When I read the source code of the newest Bitcoin Core, I found a function called rand_uniform_delay. When I searched for its usage, I found it only used in random_test.cpp. I wonder where the code for the random delay in Bitcoin Core can be found.


user133407: Code used for better privacy when relaying transactions in \src\net_processing.cpp:
        if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
                LOCK(tx_relay->m_tx_inventory_mutex);
                // Check whether periodic sends should happen
                bool fSendTrickle = pto->HasPermission(NetPermissionFlags::NoBan);
                if (tx_relay->m_next_inv_send_time < current_time) {
                    fSendTrickle = true;
                    if (pto->IsInboundConn()) {
                        tx_relay->m_next_inv_send_time = NextInvToInbounds(current_time, INBOUND_INVENTORY_BROADCAST_INTERVAL);
                    } else {
                        tx_relay->m_next_inv_send_time = GetExponentialRand(current_time, OUTBOUND_INVENTORY_BROADCAST_INTERVAL);
                    }
                }

This was initially implemented by Pieter Wuille in #7125, later improved in #7840 and #13298

Related
Transaction Relay and trickling in Bitcoin

134486: <p>I read that bitcoin uses random delay when relaying transaction to other peers in order to protect privacy. When I read the <a href="https://github.com/bitcoin/bitcoin" rel="nofollow noreferrer">source code of the newest Bitcoin Core</a>, I found a function called <code>rand_uniform_delay</code>. When I searched for its usage, I found it only used in <code>random_test.cpp</code>. I wonder where the code for the random delay in Bitcoin Core can be found.</p>
<p><a href="https://i.stack.imgur.com/NHHka.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NHHka.png" alt="enter image description here" /></a></p>
 133407: <p>Code used for better privacy when relaying transactions in <code>\src\net_processing.cpp</code>:</p>
<pre><code>        if (auto tx_relay = peer-&gt;GetTxRelay(); tx_relay != nullptr) {
                LOCK(tx_relay-&gt;m_tx_inventory_mutex);
                // Check whether periodic sends should happen
                bool fSendTrickle = pto-&gt;HasPermission(NetPermissionFlags::NoBan);
                if (tx_relay-&gt;m_next_inv_send_time &lt; current_time) {
                    fSendTrickle = true;
                    if (pto-&gt;IsInboundConn()) {
                        tx_relay-&gt;m_next_inv_send_time = NextInvToInbounds(current_time, INBOUND_INVENTORY_BROADCAST_INTERVAL);
                    } else {
                        tx_relay-&gt;m_next_inv_send_time = GetExponentialRand(current_time, OUTBOUND_INVENTORY_BROADCAST_INTERVAL);
                    }
                }
</code></pre>
<p>This was initially implemented by Pieter Wuille in <a href="https://github.com/bitcoin/bitcoin/pull/7125" rel="nofollow noreferrer">#7125</a>, later improved in <a href="https://github.com/bitcoin/bitcoin/pull/7840" rel="nofollow noreferrer">#7840</a> and <a href="https://github.com/bitcoin/bitcoin/pull/13298" rel="nofollow noreferrer">#13298</a></p>
<hr />
<p>Related</p>
<p><a href="https://bitcoin.stackexchange.com/questions/83018/transaction-relay-and-trickling-in-bitcoin">Transaction Relay and trickling in Bitcoin</a></p>
