MCPcopy Create free account
hub / github.com/PurpleI2P/i2pd / GetExploratoryNonFloodfill

Method GetExploratoryNonFloodfill

libi2pd/NetDb.cpp:1330–1374  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1328 }
1329
1330 std::vector<IdentHash> NetDb::GetExploratoryNonFloodfill (const IdentHash& destination,
1331 size_t num, const std::unordered_set<IdentHash>& excluded)
1332 {
1333 std::vector<IdentHash> ret;
1334 if (!num || m_RouterInfos.empty ()) return ret; // empty list
1335 auto ts = i2p::util::GetMonotonicSeconds ();
1336 if (ts > m_LastExploratorySelectionUpdateTime + NETDB_EXPLORATORY_SELECTION_UPDATE_INTERVAL)
1337 {
1338 // update selection
1339 m_ExploratorySelection.clear ();
1340 std::vector<std::shared_ptr<const RouterInfo> > eligible;
1341 eligible.reserve (m_RouterInfos.size ());
1342 {
1343 // collect eligible from current netdb
1344 bool checkIsReal = i2p::tunnel::tunnels.GetPreciseTunnelCreationSuccessRate () < NETDB_TUNNEL_CREATION_RATE_THRESHOLD; // too low rate
1345 std::lock_guard<std::mutex> l(m_RouterInfosMutex);
1346 for (const auto& it: m_RouterInfos)
1347 if (!it.second->IsDeclaredFloodfill () &&
1348 (!checkIsReal || (it.second->HasProfile () && it.second->GetProfile ()->IsReal ())))
1349 eligible.push_back (it.second);
1350 }
1351 if (eligible.size () > NETDB_MAX_EXPLORATORY_SELECTION_SIZE)
1352 {
1353 std::sample (eligible.begin(), eligible.end(), std::back_inserter(m_ExploratorySelection),
1354 NETDB_MAX_EXPLORATORY_SELECTION_SIZE, m_Rng);
1355 }
1356 else
1357 std::swap (m_ExploratorySelection, eligible);
1358 m_LastExploratorySelectionUpdateTime = ts;
1359 }
1360
1361 // sort by distance
1362 IdentHash destKey = CreateRoutingKey (destination);
1363 std::map<XORMetric, std::shared_ptr<const RouterInfo> > sorted;
1364 for (const auto& it: m_ExploratorySelection)
1365 if (!excluded.count (it->GetIdentHash ()))
1366 sorted.emplace (destKey ^ it->GetIdentHash (), it);
1367 // return first num closest routers
1368 for (const auto& it: sorted)
1369 {
1370 ret.push_back (it.second->GetIdentHash ());
1371 if (ret.size () >= num) break;
1372 }
1373 return ret;
1374 }
1375
1376 void NetDb::ManageRouterInfos ()
1377 {

Callers

nothing calls this directly

Calls 8

GetMonotonicSecondsFunction · 0.85
CreateRoutingKeyFunction · 0.85
IsDeclaredFloodfillMethod · 0.80
HasProfileMethod · 0.80
IsRealMethod · 0.80
GetProfileMethod · 0.80
push_backMethod · 0.80

Tested by

no test coverage detected