Browse Source

common rng for random shuffle

pull/2016/head
orignal 10 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
// shuffle records // shuffle records
std::vector<int> recordIndicies; std::vector<int> recordIndicies;
for (int i = 0; i < numRecords; i++) recordIndicies.push_back(i); 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 // create real records
uint8_t * records = msg->GetPayload () + 1; uint8_t * records = msg->GetPayload () + 1;

9
libi2pd/TunnelPool.cpp

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

6
libi2pd/TunnelPool.h

@ -15,6 +15,7 @@
#include <utility> #include <utility>
#include <mutex> #include <mutex>
#include <memory> #include <memory>
#include <random>
#include "Identity.h" #include "Identity.h"
#include "LeaseSet.h" #include "LeaseSet.h"
#include "RouterInfo.h" #include "RouterInfo.h"
@ -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; 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); bool StandardSelectPeers(Path & path, int numHops, bool inbound, SelectHopFunc nextHop);
std::mt19937& GetRng () { return m_Rng; }
private: private:
void TestTunnels (); void TestTunnels ();
@ -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_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 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: public:
// for HTTP only // for HTTP only

Loading…
Cancel
Save