Browse Source

common rng for random shuffle

pull/2016/head
orignal 9 months ago
parent
commit
dddbca6ffb
  1. 2
      libi2pd/Tunnel.cpp
  2. 9
      libi2pd/TunnelPool.cpp
  3. 6
      libi2pd/TunnelPool.h

2
libi2pd/Tunnel.cpp

@ -52,7 +52,7 @@ namespace tunnel @@ -52,7 +52,7 @@ namespace tunnel
// shuffle records
std::vector<int> recordIndicies;
for (int i = 0; i < numRecords; i++) recordIndicies.push_back(i);
std::shuffle (recordIndicies.begin(), recordIndicies.end(), std::mt19937(std::random_device()()));
std::shuffle (recordIndicies.begin(), recordIndicies.end(), m_Pool ? m_Pool->GetRng () : std::mt19937(std::random_device()()));
// create real records
uint8_t * records = msg->GetPayload () + 1;

9
libi2pd/TunnelPool.cpp

@ -7,7 +7,6 @@ @@ -7,7 +7,6 @@
*/
#include <algorithm>
#include <random>
#include "I2PEndian.h"
#include "Crypto.h"
#include "Tunnel.h"
@ -45,7 +44,7 @@ namespace tunnel @@ -45,7 +44,7 @@ namespace tunnel
m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops),
m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels),
m_InboundVariance (inboundVariance), m_OutboundVariance (outboundVariance),
m_IsActive (true), m_CustomPeerSelector(nullptr)
m_IsActive (true), m_CustomPeerSelector(nullptr), m_Rng(m_Rd())
{
if (m_NumInboundTunnels > TUNNEL_POOL_MAX_INBOUND_TUNNELS_QUANTITY)
m_NumInboundTunnels = TUNNEL_POOL_MAX_INBOUND_TUNNELS_QUANTITY;
@ -361,22 +360,20 @@ namespace tunnel @@ -361,22 +360,20 @@ namespace tunnel
// new tests
std::vector<std::pair<std::shared_ptr<OutboundTunnel>, std::shared_ptr<InboundTunnel> > > newTests;
std::random_device rd;
std::mt19937 rnd(rd());
std::vector<std::shared_ptr<OutboundTunnel> > outboundTunnels;
{
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
for (auto& it: m_OutboundTunnels)
outboundTunnels.push_back (it);
}
std::shuffle (outboundTunnels.begin(), outboundTunnels.end(), rnd);
std::shuffle (outboundTunnels.begin(), outboundTunnels.end(), m_Rng);
std::vector<std::shared_ptr<InboundTunnel> > inboundTunnels;
{
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
for (auto& it: m_InboundTunnels)
inboundTunnels.push_back (it);
}
std::shuffle (inboundTunnels.begin(), inboundTunnels.end(), rnd);
std::shuffle (inboundTunnels.begin(), inboundTunnels.end(), m_Rng);
auto it1 = outboundTunnels.begin ();
auto it2 = inboundTunnels.begin ();
while (it1 != outboundTunnels.end () && it2 != inboundTunnels.end ())

6
libi2pd/TunnelPool.h

@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
#include <utility>
#include <mutex>
#include <memory>
#include <random>
#include "Identity.h"
#include "LeaseSet.h"
#include "RouterInfo.h"
@ -115,6 +116,8 @@ namespace tunnel @@ -115,6 +116,8 @@ namespace tunnel
std::shared_ptr<const i2p::data::RouterInfo> SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop, bool reverse, bool endpoint) const;
bool StandardSelectPeers(Path & path, int numHops, bool inbound, SelectHopFunc nextHop);
std::mt19937& GetRng () { return m_Rng; }
private:
void TestTunnels ();
@ -148,6 +151,9 @@ namespace tunnel @@ -148,6 +151,9 @@ namespace tunnel
uint64_t m_MinLatency = 0; // if > 0 this tunnel pool will try building tunnels with minimum latency by ms
uint64_t m_MaxLatency = 0; // if > 0 this tunnel pool will try building tunnels with maximum latency by ms
std::random_device m_Rd;
std::mt19937 m_Rng;
public:
// for HTTP only

Loading…
Cancel
Save