Skip to content

Commit

Permalink
0rtt bump for stateless reset lifetimes, correctly prune against whit…
Browse files Browse the repository at this point in the history
…elist
  • Loading branch information
dr7ana committed Dec 5, 2024
1 parent a3ae897 commit e6cfa6d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 74 deletions.
3 changes: 2 additions & 1 deletion llarp/crypto/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,10 @@ namespace llarp
{
Ed25519SecretKey ret{};
PubKey pk;
int result = crypto_sign_ed25519_keypair(pk.data(), ret.data());
[[maybe_unused]] int result = crypto_sign_ed25519_keypair(pk.data(), ret.data());
assert(result != -1);
const PubKey sk_pk = ret.to_pubkey();
(void)sk_pk;
assert(pk == sk_pk);
return ret;
}
Expand Down
39 changes: 9 additions & 30 deletions llarp/nodedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,46 +753,24 @@ namespace llarp
});
}

void NodeDB::set_router_whitelist(
const std::vector<RouterID>& whitelist,
const std::vector<RouterID>& greylist,
const std::vector<RouterID>& greenlist)
void NodeDB::set_router_whitelist(const std::vector<RouterID>& whitelist)
{
log::critical(
logcat,
"Oxend provided {}/{}/{} (white/gray/green) routers",
whitelist.size(),
greylist.size(),
greenlist.size());
log::critical(logcat, "Oxend provided {} whitelisted routers", whitelist.size());

if (whitelist.empty())
return;

_registered_routers.clear();
_registered_routers.insert(whitelist.begin(), whitelist.end());
_registered_routers.insert(greylist.begin(), greylist.end());
_registered_routers.insert(greenlist.begin(), greenlist.end());

_router_whitelist.clear();
_router_whitelist.insert(whitelist.begin(), whitelist.end());
_router_greylist.clear();
_router_greylist.insert(greylist.begin(), greylist.end());
_router_greenlist.clear();
_router_greenlist.insert(greenlist.begin(), greenlist.end());

log::critical(
logcat,
"Service node holding {}:{} (whitelist:registered) after oxend integration",
_router_whitelist.size(),
_registered_routers.size());
logcat, "Service node holding {} registered relays after oxend integration", _registered_routers.size());
}

std::optional<RouterID> NodeDB::get_random_whitelist_router() const
std::optional<RouterID> NodeDB::get_random_registered_router() const
{
std::optional<RouterID> rand = std::nullopt;

std::sample(_router_whitelist.begin(), _router_whitelist.end(), &*rand, 1, csrng);
return rand;
std::function<bool(RouterID)> hook = [](const auto&) -> bool { return true; };
return meta::sample(_registered_routers, hook);
}

bool NodeDB::is_connection_allowed(const RouterID& remote) const
Expand All @@ -801,10 +779,11 @@ namespace llarp
{
if (_pinned_edges.size() && _pinned_edges.count(remote) == 0 && not _bootstraps.contains(remote))
return false;

return known_rids.count(remote);
}

// TESTNET: make this check an updated registry
return known_rids.count(remote) or _registered_routers.count(remote);
return known_rids.count(remote) and _registered_routers.empty() ? true : _registered_routers.count(remote);
}

bool NodeDB::is_first_hop_allowed(const RouterID& remote) const
Expand Down
21 changes: 2 additions & 19 deletions llarp/nodedb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,6 @@ namespace llarp

BootstrapList _bootstraps{};

/** RouterID lists // TODO: get rid of all these, replace with better decom/not staked
sets
- white: active routers
- gray: fully funded, but decommissioned routers
- green: registered, but not fully-staked routers
*/
std::set<RouterID> _router_whitelist{};
std::set<RouterID> _router_greylist{};
std::set<RouterID> _router_greenlist{};

// All registered relays (service nodes)
std::set<RouterID> _registered_routers;

Expand Down Expand Up @@ -250,12 +240,9 @@ namespace llarp
// variable ::known_rids
bool reselect_router_id_sources(std::set<RouterID> specific);

void set_router_whitelist(
const std::vector<RouterID>& whitelist,
const std::vector<RouterID>& greylist,
const std::vector<RouterID>& greenlist);
void set_router_whitelist(const std::vector<RouterID>& whitelist);

std::optional<RouterID> get_random_whitelist_router() const;
std::optional<RouterID> get_random_registered_router() const;

// client:
// if pinned edges were specified, connections are allowed only to those and
Expand Down Expand Up @@ -292,10 +279,6 @@ namespace llarp

void set_bootstrap_routers(BootstrapList& from_router);

const std::set<RouterID>& whitelist() const { return _router_whitelist; }

const std::set<RouterID>& greylist() const { return _router_greylist; }

std::set<RouterID>& registered_routers() { return _registered_routers; }

const std::set<RouterID>& registered_routers() const { return _registered_routers; }
Expand Down
11 changes: 4 additions & 7 deletions llarp/router/router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ namespace llarp

bool Router::appears_decommed() const
{
return _is_service_node and has_whitelist() and node_db()->greylist().count(local_rid());
return _is_service_node and has_whitelist() and not node_db()->registered_routers().count(local_rid());
}

bool Router::appears_funded() const
Expand Down Expand Up @@ -953,15 +953,12 @@ namespace llarp

const std::set<RouterID>& Router::get_whitelist() const
{
return _node_db->whitelist();
return _node_db->registered_routers();
}

void Router::set_router_whitelist(
const std::vector<RouterID>& whitelist,
const std::vector<RouterID>& greylist,
const std::vector<RouterID>& unfundedlist)
void Router::set_router_whitelist(const std::vector<RouterID>& whitelist)
{
node_db()->set_router_whitelist(whitelist, greylist, unfundedlist);
node_db()->set_router_whitelist(whitelist);
whitelist_received = true;
}

Expand Down
5 changes: 1 addition & 4 deletions llarp/router/router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,7 @@ namespace llarp

const std::set<RouterID>& get_whitelist() const;

void set_router_whitelist(
const std::vector<RouterID>& whitelist,
const std::vector<RouterID>& greylist,
const std::vector<RouterID>& unfunded);
void set_router_whitelist(const std::vector<RouterID>& whitelist);

template <std::invocable Callable>
void queue_work(Callable&& func)
Expand Down
18 changes: 6 additions & 12 deletions llarp/rpc/rpc_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ namespace llarp::rpc
void RPCClient::handle_new_service_node_list(const nlohmann::json& j)
{
std::unordered_map<RouterID, PubKey> keymap;
std::vector<RouterID> activeNodeList, decommNodeList, unfundedNodeList;
std::vector<RouterID> active_list;
if (not j.is_array())
throw std::runtime_error{"Invalid service node list: expected array of service node states"};

Expand All @@ -229,21 +229,18 @@ namespace llarp::rpc
if (active_itr == snode.end() or not active_itr->is_boolean())
continue;
const bool active = active_itr->get<bool>();
const auto funded_itr = snode.find("funded");
if (funded_itr == snode.end() or not funded_itr->is_boolean())
continue;
const bool funded = funded_itr->get<bool>();

RouterID rid;
PubKey pk;
if (not rid.FromHex(ed_itr->get<std::string_view>()) or not pk.FromHex(svc_itr->get<std::string_view>()))
continue;

keymap[rid] = pk;
(active ? activeNodeList : funded ? decommNodeList : unfundedNodeList).push_back(std::move(rid));
if (active)
active_list.emplace_back(std::move(rid));
}

if (activeNodeList.empty())
if (active_list.empty())
{
log::warning(logcat, "Received empty service node list, ignoring.");
return;
Expand All @@ -254,14 +251,11 @@ namespace llarp::rpc
{
auto& loop = router->loop();
loop->call([this,
active = std::move(activeNodeList),
decomm = std::move(decommNodeList),
unfunded = std::move(unfundedNodeList),
active = std::move(active_list),
keymap = std::move(keymap),
router = std::move(router)]() mutable {
_key_map = std::move(keymap);

router->set_router_whitelist(active, decomm, unfunded);
router->set_router_whitelist(active);
});
}
else
Expand Down

0 comments on commit e6cfa6d

Please sign in to comment.