|
| 1 | +#include "edge_selector.hpp" |
| 2 | + |
| 3 | +#include <llarp/router/abstractrouter.hpp> |
| 4 | +#include <llarp/crypto/crypto.hpp> |
| 5 | +#include <llarp/nodedb.hpp> |
| 6 | +#include <llarp/profiling.hpp> |
| 7 | + |
| 8 | +namespace llarp::consensus |
| 9 | +{ |
| 10 | + |
| 11 | + EdgeSelector::EdgeSelector(AbstractRouter& r) : _router{r} |
| 12 | + {} |
| 13 | + |
| 14 | + std::optional<RouterContact> |
| 15 | + EdgeSelector::select_path_edge(const std::unordered_set<RouterID>& connected_now) const |
| 16 | + { |
| 17 | + auto conf = _router.GetConfig(); |
| 18 | + auto nodedb = _router.nodedb(); |
| 19 | + |
| 20 | + const auto& _keys = conf->network.m_strictConnect; |
| 21 | + // no strict hops. select a random good snode. |
| 22 | + if (_keys.empty()) |
| 23 | + { |
| 24 | + return nodedb->GetRandom([&connected_now, this](const auto& rc) { |
| 25 | + return connected_now.count(rc.pubkey) == 0 |
| 26 | + and not _router.routerProfiling().IsBadForConnect(rc.pubkey) |
| 27 | + and not _router.IsBootstrapNode(rc.pubkey); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + // select random from strict connections. |
| 32 | + std::vector<RouterID> keys{_keys.begin(), _keys.end()}; |
| 33 | + |
| 34 | + std::shuffle(keys.begin(), keys.end(), llarp::CSRNG{}); |
| 35 | + |
| 36 | + for (const auto& pk : keys) |
| 37 | + { |
| 38 | + if (_router.routerProfiling().IsBadForConnect(pk)) |
| 39 | + continue; |
| 40 | + if (connected_now.count(pk)) |
| 41 | + continue; |
| 42 | + if (auto maybe = nodedb->Get(pk)) |
| 43 | + return maybe; |
| 44 | + } |
| 45 | + return std::nullopt; |
| 46 | + } |
| 47 | + |
| 48 | + std::optional<RouterContact> |
| 49 | + EdgeSelector::select_bootstrap(const std::unordered_set<RouterID>& connected_now) const |
| 50 | + { |
| 51 | + auto conf = _router.GetConfig(); |
| 52 | + auto nodedb = _router.nodedb(); |
| 53 | + if (const auto& _keys = conf->network.m_strictConnect; not _keys.empty()) |
| 54 | + { |
| 55 | + // try bootstrapping off strict connections first if we have any. |
| 56 | + std::vector<RouterID> keys{_keys.begin(), _keys.end()}; |
| 57 | + std::shuffle(keys.begin(), keys.end(), llarp::CSRNG{}); |
| 58 | + for (const auto& pk : keys) |
| 59 | + { |
| 60 | + if (connected_now.count(pk)) |
| 61 | + continue; |
| 62 | + if (auto maybe = nodedb->Get(pk)) |
| 63 | + return maybe; |
| 64 | + } |
| 65 | + } |
| 66 | + // if we cant use a strict connection we'll just use one of our bootstrap nodes. |
| 67 | + return nodedb->GetRandom([&connected_now, this](const auto& rc) { |
| 68 | + return connected_now.count(rc.pubkey) == 0 and _router.IsBootstrapNode(rc.pubkey); |
| 69 | + }); |
| 70 | + } |
| 71 | +} // namespace llarp::consensus |
0 commit comments