-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make lokinet client with strict-connect option provided explicitly co…
…nnect out to snodes provided. * create new component for selecting edge snodes as client (llarp::consensus::EdgeSelector) * require llarp::AbstractRouter own a llarp::consensus::EdgeSelector * make outbound_session_maker.cpp use llarp::consensus::EdgeSelector owned by llarp::AbstractRouter
- Loading branch information
jeff
committed
May 11, 2023
1 parent
a294c81
commit dea8ad2
Showing
7 changed files
with
144 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "edge_selector.hpp" | ||
|
||
#include <llarp/router/abstractrouter.hpp> | ||
#include <llarp/crypto/crypto.hpp> | ||
#include <llarp/nodedb.hpp> | ||
#include <llarp/profiling.hpp> | ||
|
||
namespace llarp::consensus | ||
{ | ||
|
||
EdgeSelector::EdgeSelector(AbstractRouter& r) : _router{r} | ||
{} | ||
|
||
std::optional<RouterContact> | ||
EdgeSelector::select_path_edge(const std::unordered_set<RouterID>& connected_now) const | ||
{ | ||
auto conf = _router.GetConfig(); | ||
auto nodedb = _router.nodedb(); | ||
|
||
const auto& _keys = conf->network.m_strictConnect; | ||
// no strict hops. select a random good snode. | ||
if (_keys.empty()) | ||
{ | ||
return nodedb->GetRandom([&connected_now, this](const auto& rc) { | ||
return connected_now.count(rc.pubkey) == 0 | ||
and not _router.routerProfiling().IsBadForConnect(rc.pubkey) | ||
and not _router.IsBootstrapNode(rc.pubkey); | ||
}); | ||
} | ||
|
||
// select random from strict connections. | ||
std::vector<RouterID> keys{_keys.begin(), _keys.end()}; | ||
|
||
std::shuffle(keys.begin(), keys.end(), llarp::CSRNG{}); | ||
|
||
for (const auto& pk : keys) | ||
{ | ||
if (_router.routerProfiling().IsBadForConnect(pk)) | ||
continue; | ||
if (connected_now.count(pk)) | ||
continue; | ||
if (auto maybe = nodedb->Get(pk)) | ||
return maybe; | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
std::optional<RouterContact> | ||
EdgeSelector::select_bootstrap(const std::unordered_set<RouterID>& connected_now) const | ||
{ | ||
auto conf = _router.GetConfig(); | ||
auto nodedb = _router.nodedb(); | ||
if (const auto& _keys = conf->network.m_strictConnect; not _keys.empty()) | ||
{ | ||
// try bootstrapping off strict connections first if we have any. | ||
std::vector<RouterID> keys{_keys.begin(), _keys.end()}; | ||
std::shuffle(keys.begin(), keys.end(), llarp::CSRNG{}); | ||
for (const auto& pk : keys) | ||
{ | ||
if (connected_now.count(pk)) | ||
continue; | ||
if (auto maybe = nodedb->Get(pk)) | ||
return maybe; | ||
} | ||
} | ||
// if we cant use a strict connection we'll just use one of our bootstrap nodes. | ||
return nodedb->GetRandom([&connected_now, this](const auto& rc) { | ||
return connected_now.count(rc.pubkey) == 0 and _router.IsBootstrapNode(rc.pubkey); | ||
}); | ||
} | ||
} // namespace llarp::consensus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include <optional> | ||
#include <unordered_set> | ||
#include <llarp/router_id.hpp> | ||
|
||
namespace llarp | ||
{ | ||
struct AbstractRouter; | ||
struct RouterContact; | ||
} // namespace llarp | ||
|
||
namespace llarp::consensus | ||
{ | ||
/// when we want to connect to and edge when we are a client, an instance of EdgeSelector acts as | ||
/// a stateless selection algorith for router contacts for each attempt at connecting out we make. | ||
class EdgeSelector | ||
{ | ||
AbstractRouter& _router; | ||
|
||
public: | ||
explicit EdgeSelector(AbstractRouter& router); | ||
|
||
/// select a candidate for connecting out to the network when we need more connections to do a | ||
/// path build. pass in an unordered set of the snode pubkeys we are currently connected to. | ||
std::optional<RouterContact> | ||
select_path_edge(const std::unordered_set<RouterID>& connected_now = {}) const; | ||
|
||
/// get a router contact of a bootstrap snode to bootstrap with if we are in need of | ||
/// bootstrapping more peers. pass in an unodereed set of bootstrap router pubkeys we are | ||
/// currently connected to. | ||
std::optional<RouterContact> | ||
select_bootstrap(const std::unordered_set<RouterID>& connected_now = {}) const; | ||
}; | ||
} // namespace llarp::consensus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters