From d016951d2ff15db16e35f579b9cfb38942247ab3 Mon Sep 17 00:00:00 2001 From: dr7ana Date: Wed, 13 Dec 2023 05:49:59 -0800 Subject: [PATCH] Fixed pending message queue weirdness --- llarp/link/connection.cpp | 2 +- llarp/link/link_manager.cpp | 32 ++++++++++++++++++-------------- llarp/link/link_manager.hpp | 25 ++++++++----------------- llarp/nodedb.cpp | 5 +++-- llarp/router/router.cpp | 5 ++--- 5 files changed, 32 insertions(+), 37 deletions(-) diff --git a/llarp/link/connection.cpp b/llarp/link/connection.cpp index 9b8760746..d5fa0abf9 100644 --- a/llarp/link/connection.cpp +++ b/llarp/link/connection.cpp @@ -5,7 +5,7 @@ namespace llarp::link Connection::Connection( const std::shared_ptr& c, std::shared_ptr& s) - : conn{c}, control_stream{s}/* , remote_rc{std::move(rc)} */ + : conn{c}, control_stream{s} /* , remote_rc{std::move(rc)} */ {} } // namespace llarp::link diff --git a/llarp/link/link_manager.cpp b/llarp/link/link_manager.cpp index 9787d774f..4f2bc3f91 100644 --- a/llarp/link/link_manager.cpp +++ b/llarp/link/link_manager.cpp @@ -74,7 +74,7 @@ namespace llarp std::advance(itr, randint() % size); RouterID rid{itr->second->conn->remote_key()}; - + if (auto maybe = link_manager.node_db->get_rc(rid)) { router = *maybe; @@ -151,7 +151,8 @@ namespace llarp for (auto& method : direct_requests) { s->register_command( - std::string{method.first}, [this, func = std::move(method.second)](oxen::quic::message m) { + std::string{method.first}, + [this, func = std::move(method.second)](oxen::quic::message m) { _router.loop()->call([this, msg = std::move(m), func = std::move(func)]() mutable { auto body = msg.body_str(); auto respond = [m = std::move(msg)](std::string response) mutable { @@ -291,7 +292,7 @@ namespace llarp endpoint = std::move(endpoint), body = std::move(body), f = std::move(func)]() { - auto pending = PendingControlMessage(std::move(body), std::move(endpoint), f); + auto pending = PendingMessage(std::move(body), std::move(endpoint), std::move(f)); auto [itr, b] = pending_conn_msg_queue.emplace(remote, MessageQueue()); itr->second.push_back(std::move(pending)); @@ -315,7 +316,7 @@ namespace llarp } _router.loop()->call([this, body = std::move(body), remote]() { - auto pending = PendingDataMessage(body); + auto pending = PendingMessage(std::move(body)); auto [itr, b] = pending_conn_msg_queue.emplace(remote, MessageQueue()); itr->second.push_back(std::move(pending)); @@ -434,17 +435,16 @@ namespace llarp while (not que.empty()) { - auto& m = que.front(); + auto& msg = que.front(); - if (m.is_control) + if (msg.is_control) { - auto& msg = reinterpret_cast(m); - log::critical(logcat, "Dispatching {} request!", msg.endpoint); - ep.conns[rid]->control_stream->command(msg.endpoint, msg.body, msg.func); + log::critical(logcat, "Dispatching {} request!", *msg.endpoint); + ep.conns[rid]->control_stream->command( + std::move(*msg.endpoint), std::move(msg.body), std::move(msg.func)); } else { - auto& msg = reinterpret_cast(m); conn_interface.send_datagram(std::move(msg.body)); } @@ -647,7 +647,7 @@ namespace llarp } log::critical(logcat, "Queuing bootstrap fetch request to {}", source.router_id()); - auto pending = PendingControlMessage(std::move(payload), "bfetch_rcs"s, std::move(f)); + auto pending = PendingMessage(std::move(payload), "bfetch_rcs"s, std::move(f)); auto [itr, b] = pending_conn_msg_queue.emplace(source.router_id(), MessageQueue()); itr->second.push_back(std::move(pending)); @@ -688,12 +688,16 @@ namespace llarp // TODO: if we are not the seed, how do we check the requester if (is_seed) { - // we already insert the + // we already insert the auto& seeds = node_db->seeds(); - + if (auto itr = seeds.find(rid); itr != seeds.end()) { - log::critical(logcat, "Bootstrap seed confirmed RID:{} is white-listed seeds; approving fetch request and saving RC!", rid); + log::critical( + logcat, + "Bootstrap seed confirmed RID:{} is white-listed seeds; approving fetch request and " + "saving RC!", + rid); node_db->put_rc(remote); } } diff --git a/llarp/link/link_manager.hpp b/llarp/link/link_manager.hpp index 69a3107da..d44b389a1 100644 --- a/llarp/link/link_manager.hpp +++ b/llarp/link/link_manager.hpp @@ -109,27 +109,18 @@ namespace llarp struct PendingMessage { std::string body; + std::optional endpoint = std::nullopt; + std::function func = nullptr; + RouterID rid; - bool is_control{false}; + bool is_control = false; - PendingMessage(std::string b, bool control = false) : body{std::move(b)}, is_control{control} + PendingMessage(std::string b) : body{std::move(b)} {} - }; - struct PendingDataMessage : PendingMessage - { - PendingDataMessage(std::string b) : PendingMessage(b) - {} - }; - - struct PendingControlMessage : PendingMessage - { - std::string endpoint; - std::function func; - - PendingControlMessage( - std::string b, std::string e, std::function f = nullptr) - : PendingMessage(b, true), endpoint{std::move(e)}, func{std::move(f)} + PendingMessage( + std::string b, std::string ep, std::function f = nullptr) + : body{std::move(b)}, endpoint{std::move(ep)}, func{std::move(f)}, is_control{true} {} }; diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index 36d58693f..9c460b624 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -668,13 +668,14 @@ namespace llarp _needs_rebootstrap = false; ++bootstrap_attempts; - log::critical( - logcat, "Dispatching BootstrapRC fetch request to {}", _bootstraps.current().view()); + log::critical(logcat, "Dispatching BootstrapRC fetch request to {}", fetch_source); _router.link_manager().fetch_bootstrap_rcs( rc, BootstrapFetchMessage::serialize(_router.router_contact, BOOTSTRAP_SOURCE_COUNT), [this](oxen::quic::message m) mutable { + log::critical(logcat, "Received response to BootstrapRC fetch request..."); + if (not m) { // ++bootstrap_attempts; diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index f35bdbfbf..a4c3fc654 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -218,9 +218,8 @@ namespace llarp std::unordered_set peer_pubkeys; - for_each_connection([&peer_pubkeys](link::Connection& conn) { - peer_pubkeys.emplace(conn.conn->remote_key()); - }); + for_each_connection( + [&peer_pubkeys](link::Connection& conn) { peer_pubkeys.emplace(conn.conn->remote_key()); }); loop()->call([this, &peer_pubkeys]() { for (auto& pk : peer_pubkeys)