From cc46805ee7722511265f48654ad23bd01f399715 Mon Sep 17 00:00:00 2001 From: Steven Watanabe Date: Thu, 20 Jul 2023 08:19:11 -0600 Subject: [PATCH 01/41] Add multi-hop routing implementation. This has some basic unit tests, but is not yet integrated. --- .../net/include/psibase/direct_routing.hpp | 148 +----- .../net/include/psibase/peer_manager.hpp | 2 + .../net/include/psibase/routing_base.hpp | 169 +++++++ .../include/psibase/shortest_path_routing.hpp | 451 ++++++++++++++++++ libraries/net/test/CMakeLists.txt | 5 + libraries/net/test/connection_pair.cpp | 99 ++++ libraries/net/test/connection_pair.hpp | 12 + .../net/test/test_shortest_path_routing.cpp | 194 ++++++++ 8 files changed, 949 insertions(+), 131 deletions(-) create mode 100644 libraries/net/include/psibase/routing_base.hpp create mode 100644 libraries/net/include/psibase/shortest_path_routing.hpp create mode 100644 libraries/net/test/connection_pair.cpp create mode 100644 libraries/net/test/connection_pair.hpp create mode 100644 libraries/net/test/test_shortest_path_routing.cpp diff --git a/libraries/net/include/psibase/direct_routing.hpp b/libraries/net/include/psibase/direct_routing.hpp index 68bd0f2ca..f14ee2b4f 100644 --- a/libraries/net/include/psibase/direct_routing.hpp +++ b/libraries/net/include/psibase/direct_routing.hpp @@ -9,26 +9,15 @@ #include #include #include -#include #include +#include #include #include +#include #include namespace psibase::net { - // message type 0 is reserved to ensure that message signatures - // are disjoint from block signatures. - // message types 1-31 are used for routing messages - // message types 32-63 are used for consensus messages - struct InitMessage - { - static constexpr unsigned type = 1; - std::uint32_t version; - NodeId id; - std::string to_string() const { return "init: version=" + std::to_string(version); } - }; - PSIO_REFLECT(InitMessage, version, id); struct ProducerMessage { static constexpr unsigned type = 2; @@ -37,33 +26,24 @@ namespace psibase::net }; PSIO_REFLECT(ProducerMessage, producer) - template - concept has_block_id = requires(T& t) { t.block_id; }; - // This requires all producers to be peers template - struct direct_routing : message_serializer + struct direct_routing : routing_base { - auto& peers() { return static_cast(this)->peers(); } - auto& chain() { return static_cast(this)->chain(); } - explicit direct_routing(boost::asio::io_context& ctx) - { - std::random_device rng; - nodeId = std::uniform_int_distribution()(rng); - } - static const std::uint32_t protocol_version = 0; - auto get_message_impl() - { - return boost::mp11::mp_push_back< - typename std::remove_cvref_t< - decltype(static_cast(this)->consensus())>::message_type, - InitMessage, ProducerMessage>{}; - } + using base_type = routing_base; + using base_type::async_send; + using base_type::base_type; + using base_type::chain; + using base_type::consensus; + using base_type::peers; + using base_type::recv; + + using message_type = std::variant; + template void async_send_block(peer_id id, const Msg& msg, F&& f) { - PSIBASE_LOG(peers().logger(id), debug) << "Sending message: " << msg.to_string(); - peers().async_send(id, this->serialize_message(msg), std::forward(f)); + async_send(id, msg, f); } // Sends a message to each peer in a list // each peer will receive the message only once even if it is duplicated in the input list. @@ -108,18 +88,17 @@ namespace psibase::net struct connection; void connect(peer_id id) { - async_send_block(id, InitMessage{.version = protocol_version, .id = nodeId}, - [](const std::error_code&) {}); + base_type::connect(id); if (auto producer = static_cast(this)->consensus().producer_name(); producer != AccountNumber()) { async_send_block(id, ProducerMessage{producer}, [](const std::error_code&) {}); } - static_cast(this)->consensus().connect(id); + consensus().connect(id); } void disconnect(peer_id id) { - static_cast(this)->consensus().disconnect(id); + consensus().disconnect(id); for (auto iter = producers.begin(), end = producers.end(); iter != end;) { if (iter->second == id) @@ -132,105 +111,12 @@ namespace psibase::net } } } - template