From fe9d74122b40d1ecc43a81bd25f73fec8719f706 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 4 Oct 2024 20:13:07 +0200 Subject: [PATCH] Add Sv2Connman Co-Authored-By: Vasil Dimov --- src/sv2/CMakeLists.txt | 2 + src/sv2/connman.cpp | 356 +++++++++++++++++++++++++++++++++ src/sv2/connman.h | 230 +++++++++++++++++++++ src/test/CMakeLists.txt | 1 + src/test/sv2_connman_tests.cpp | 201 +++++++++++++++++++ 5 files changed, 790 insertions(+) create mode 100644 src/sv2/connman.cpp create mode 100644 src/sv2/connman.h create mode 100644 src/test/sv2_connman_tests.cpp diff --git a/src/sv2/CMakeLists.txt b/src/sv2/CMakeLists.txt index e61f2f3560834..a628204612fcd 100644 --- a/src/sv2/CMakeLists.txt +++ b/src/sv2/CMakeLists.txt @@ -5,6 +5,7 @@ add_library(bitcoin_sv2 STATIC EXCLUDE_FROM_ALL noise.cpp transport.cpp + connman.cpp ) target_link_libraries(bitcoin_sv2 @@ -12,5 +13,6 @@ target_link_libraries(bitcoin_sv2 core_interface bitcoin_clientversion bitcoin_crypto + bitcoin_common # for SockMan $<$:ws2_32> ) diff --git a/src/sv2/connman.cpp b/src/sv2/connman.cpp new file mode 100644 index 0000000000000..a7d35d9981b0e --- /dev/null +++ b/src/sv2/connman.cpp @@ -0,0 +1,356 @@ +// Copyright (c) 2023-present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include +#include +#include +#include +#include + +using node::Sv2MsgType; + +Sv2Connman::~Sv2Connman() +{ + AssertLockNotHeld(m_clients_mutex); + + { + LOCK(m_clients_mutex); + for (const auto& client : m_sv2_clients) { + LogTrace(BCLog::SV2, "Disconnecting client id=%zu\n", + client.first); + CloseConnection(client.second->m_id); + client.second->m_disconnect_flag = true; + } + DisconnectFlagged(); + } + + Interrupt(); + StopThreads(); +} + +bool Sv2Connman::Start(Sv2EventsInterface* msgproc, std::string host, uint16_t port) +{ + m_msgproc = msgproc; + + if (!Bind(host, port)) return false; + + SockMan::Options sockman_options; + StartSocketsThreads(sockman_options); + + return true; +} + +bool Sv2Connman::Bind(std::string host, uint16_t port) +{ + const CService addr_bind = LookupNumeric(host, port); + + bilingual_str error; + if (!BindAndStartListening(addr_bind, error)) { + LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Template Provider failed to bind to port %d: %s\n", port, error.original); + return false; + } + + LogPrintLevel(BCLog::SV2, BCLog::Level::Info, "%s listening on %s:%d\n", SV2_PROTOCOL_NAMES.at(m_subprotocol), host, port); + + return true; +} + + +void Sv2Connman::DisconnectFlagged() +{ + AssertLockHeld(m_clients_mutex); + + // Remove clients that are flagged for disconnection. + auto it = m_sv2_clients.begin(); + while(it != m_sv2_clients.end()) { + if (it->second->m_disconnect_flag) { + it = m_sv2_clients.erase(it); + } else { + it++; + } + } +} + +void Sv2Connman::EventIOLoopCompletedForAllPeers() +{ + LOCK(m_clients_mutex); + DisconnectFlagged(); +} + +void Sv2Connman::Interrupt() +{ + interruptNet(); +} + +void Sv2Connman::StopThreads() +{ + JoinSocketsThreads(); +} + +std::shared_ptr Sv2Connman::GetClientById(NodeId node_id) const +{ + auto it{m_sv2_clients.find(node_id)}; + if (it != m_sv2_clients.end()) { + return it->second; + } + return nullptr; +} + +bool Sv2Connman::EventNewConnectionAccepted(NodeId node_id, + const CService& addr_bind_, + const CService& addr_) +{ + Assume(m_certificate); + LOCK(m_clients_mutex); + std::unique_ptr transport = std::make_unique(m_static_key, m_certificate.value()); + auto client = std::make_shared(node_id, std::move(transport)); + LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "New client id=%zu connected\n", node_id); + m_sv2_clients.emplace(node_id, std::move(client)); + return true; +} + +void Sv2Connman::EventReadyToSend(NodeId node_id, bool& cancel_recv) +{ + AssertLockNotHeld(m_clients_mutex); + + auto client{WITH_LOCK(m_clients_mutex, return GetClientById(node_id);)}; + if (client == nullptr) { + cancel_recv = true; + return; + } + + auto it = client->m_send_messages.begin(); + std::optional expected_more; + + size_t total_sent = 0; + + while(true) { + if (it != client->m_send_messages.end()) { + // If possible, move one message from the send queue to the transport. + // This fails when there is an existing message still being sent, + // or when the handshake has not yet completed. + // + // Wrap Sv2NetMsg inside CSerializedNetMsg for transport + CSerializedNetMsg net_msg{*it}; + if (client->m_transport->SetMessageToSend(net_msg)) { + ++it; + } + } + + const auto& [data, more, _m_message_type] = client->m_transport->GetBytesToSend(/*have_next_message=*/it != client->m_send_messages.end()); + + + // We rely on the 'more' value returned by GetBytesToSend to correctly predict whether more + // bytes are still to be sent, to correctly set the MSG_MORE flag. As a sanity check, + // verify that the previously returned 'more' was correct. + if (expected_more.has_value()) Assume(!data.empty() == *expected_more); + expected_more = more; + + ssize_t sent = 0; + std::string errmsg; + + if (!data.empty()) { + LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Send %d bytes to client id=%zu\n", + data.size() - total_sent, node_id); + + sent = SendBytes(node_id, data, more, errmsg); + } + + if (sent > 0) { + client->m_transport->MarkBytesSent(sent); + if (static_cast(sent) != data.size()) { + // could not send full message; stop sending more + break; + } + } else { + if (sent < 0) { + LogDebug(BCLog::NET, "socket send error for peer=%d: %s\n", node_id, errmsg); + CloseConnection(node_id); + } + break; + } + } + + // Clear messages that have been handed to transport from the queue + client->m_send_messages.erase(client->m_send_messages.begin(), it); + + // If both receiving and (non-optimistic) sending were possible, we first attempt + // sending. If that succeeds, but does not fully drain the send queue, do not + // attempt to receive. This avoids needlessly queueing data if the remote peer + // is slow at receiving data, by means of TCP flow control. We only do this when + // sending actually succeeded to make sure progress is always made; otherwise a + // deadlock would be possible when both sides have data to send, but neither is + // receiving. + // + // TODO: decide if this is useful for Sv2 + cancel_recv = total_sent > 0; // && more; +} + +void Sv2Connman::EventGotData(NodeId node_id, const uint8_t* data, size_t n) +{ + AssertLockNotHeld(m_clients_mutex); + + auto client{WITH_LOCK(m_clients_mutex, return GetClientById(node_id);)}; + if (client == nullptr) { + return; + } + + try { + auto msg_ = Span(data, n); + Span msg(reinterpret_cast(msg_.data()), msg_.size()); + while (msg.size() > 0) { + // absorb network data + if (!client->m_transport->ReceivedBytes(msg)) { + // Serious transport problem + LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Transport problem, disconnecting client id=%zu\n", + client->m_id); + CloseConnection(node_id); + // TODO: should we even bother with this? + client->m_disconnect_flag = true; + break; + } + + if (client->m_transport->ReceivedMessageComplete()) { + bool dummy_reject_message = false; + Sv2NetMsg msg = client->m_transport->GetReceivedMessage(std::chrono::milliseconds(0), dummy_reject_message); + ProcessSv2Message(msg, *client.get()); + } + } + } catch (const std::exception& e) { + LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Received error when processing client id=%zu message: %s\n", client->m_id, e.what()); + CloseConnection(node_id); + client->m_disconnect_flag = true; + } + +} + +void Sv2Connman::EventGotEOF(NodeId node_id) +{ + auto client{WITH_LOCK(m_clients_mutex, return GetClientById(node_id);)}; + if (client == nullptr) return; + CloseConnection(node_id); + client->m_disconnect_flag = true; +} + +void Sv2Connman::EventGotPermanentReadError(NodeId node_id, const std::string& errmsg) +{ + auto client{WITH_LOCK(m_clients_mutex, return GetClientById(node_id);)}; + if (client == nullptr) return; + CloseConnection(node_id); + client->m_disconnect_flag = true; +} + +void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& client) +{ + uint8_t msg_type[1] = {uint8_t(sv2_net_msg.m_msg_type)}; + LogPrintLevel(BCLog::SV2, BCLog::Level::Debug, "Received 0x%s %s from client id=%zu\n", + // After clang-17: + // std::format("{:x}", uint8_t(sv2_net_msg.m_msg_type)), + HexStr(msg_type), + node::SV2_MSG_NAMES.at(sv2_net_msg.m_msg_type), client.m_id); + + DataStream ss (sv2_net_msg.m_msg); + + switch (sv2_net_msg.m_msg_type) + { + case Sv2MsgType::SETUP_CONNECTION: + { + if (client.m_setup_connection_confirmed) { + LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Client client id=%zu connection has already been confirmed\n", + client.m_id); + return; + } + + node::Sv2SetupConnectionMsg setup_conn; + try { + ss >> setup_conn; + } catch (const std::exception& e) { + LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Received invalid SetupConnection message from client id=%zu: %s\n", + client.m_id, e.what()); + CloseConnection(client.m_id); + client.m_disconnect_flag = true; + return; + } + + // Disconnect a client that connects on the wrong subprotocol. + if (setup_conn.m_protocol != m_subprotocol) { + node::Sv2SetupConnectionErrorMsg setup_conn_err{setup_conn.m_flags, std::string{"unsupported-protocol"}}; + + LogPrintLevel(BCLog::SV2, BCLog::Level::Debug, "Send 0x02 SetupConnectionError to client id=%zu\n", + client.m_id); + client.m_send_messages.emplace_back(setup_conn_err); + + CloseConnection(client.m_id); + client.m_disconnect_flag = true; + return; + } + + // Disconnect a client if they are not running a compatible protocol version. + if ((m_protocol_version < setup_conn.m_min_version) || (m_protocol_version > setup_conn.m_max_version)) { + node::Sv2SetupConnectionErrorMsg setup_conn_err{setup_conn.m_flags, std::string{"protocol-version-mismatch"}}; + LogPrintLevel(BCLog::SV2, BCLog::Level::Debug, "Send 0x02 SetupConnection.Error to client id=%zu\n", + client.m_id); + client.m_send_messages.emplace_back(setup_conn_err); + + LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Received a connection from client id=%zu with incompatible protocol_versions: min_version: %d, max_version: %d\n", + client.m_id, setup_conn.m_min_version, setup_conn.m_max_version); + CloseConnection(client.m_id); + client.m_disconnect_flag = true; + return; + } + + LogPrintLevel(BCLog::SV2, BCLog::Level::Debug, "Send 0x01 SetupConnection.Success to client id=%zu\n", + client.m_id); + node::Sv2SetupConnectionSuccessMsg setup_success{m_protocol_version, m_optional_features}; + client.m_send_messages.emplace_back(setup_success); + + client.m_setup_connection_confirmed = true; + + break; + } + case Sv2MsgType::COINBASE_OUTPUT_DATA_SIZE: + { + if (!client.m_setup_connection_confirmed) { + CloseConnection(client.m_id); + client.m_disconnect_flag = true; + return; + } + + node::Sv2CoinbaseOutputDataSizeMsg coinbase_output_data_size; + try { + ss >> coinbase_output_data_size; + client.m_coinbase_output_data_size_recv = true; + } catch (const std::exception& e) { + LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Received invalid CoinbaseOutputDataSize message from client id=%zu: %s\n", + client.m_id, e.what()); + CloseConnection(client.m_id); + client.m_disconnect_flag = true; + return; + } + + uint32_t max_additional_size = coinbase_output_data_size.m_coinbase_output_max_additional_size; + LogPrintLevel(BCLog::SV2, BCLog::Level::Debug, "coinbase_output_max_additional_size=%d bytes\n", max_additional_size); + + if (max_additional_size > MAX_BLOCK_WEIGHT) { + LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Received impossible CoinbaseOutputDataSize from client id=%zu: %d\n", + client.m_id, max_additional_size); + CloseConnection(client.m_id); + client.m_disconnect_flag = true; + return; + } + + client.m_coinbase_tx_outputs_size = coinbase_output_data_size.m_coinbase_output_max_additional_size; + + break; + } + default: { + uint8_t msg_type[1]{uint8_t(sv2_net_msg.m_msg_type)}; + LogPrintLevel(BCLog::SV2, BCLog::Level::Warning, "Received unknown message type 0x%s from client id=%zu\n", + HexStr(msg_type), client.m_id); + break; + } + } + + m_msgproc->ReceivedMessage(client, sv2_net_msg.m_msg_type); +} diff --git a/src/sv2/connman.h b/src/sv2/connman.h new file mode 100644 index 0000000000000..8f1281ce53a33 --- /dev/null +++ b/src/sv2/connman.h @@ -0,0 +1,230 @@ +// Copyright (c) 2023-present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_SV2_CONNMAN_H +#define BITCOIN_SV2_CONNMAN_H + +#include +#include +#include +#include + +namespace { + /* + * Supported Stratum v2 subprotocols + */ + static constexpr uint8_t TP_SUBPROTOCOL{0x02}; + + static const std::map SV2_PROTOCOL_NAMES{ + {0x02, "Template Provider"}, + }; +} + +struct Sv2Client +{ + /* Ephemeral identifier for debugging purposes */ + size_t m_id; + + /** + * Transport + */ + std::unique_ptr m_transport; + + /** + * Whether the client has confirmed the connection with a successful SetupConnection. + */ + bool m_setup_connection_confirmed = false; + + /** + * Whether the client is a candidate for disconnection. + */ + bool m_disconnect_flag = false; + + /** Queue of messages to be sent */ + std::deque m_send_messages; + + /** + * Whether the client has received CoinbaseOutputDataSize message. + */ + bool m_coinbase_output_data_size_recv = false; + + /** + * Specific additional coinbase tx output size required for the client. + */ + unsigned int m_coinbase_tx_outputs_size; + + explicit Sv2Client(size_t id, std::unique_ptr transport) : + m_id{id}, m_transport{std::move(transport)} {}; + + bool IsFullyConnected() + { + return !m_disconnect_flag && m_setup_connection_confirmed; + } + + Sv2Client(Sv2Client&) = delete; + Sv2Client& operator=(const Sv2Client&) = delete; +}; + +/** + * Interface for sv2 message handling + */ +class Sv2EventsInterface +{ +public: + /** + * Generic notification that a message was received. Does not include the + * message itself. + * + * @param[in] client The client which we have received messages from. + * @param[in] msg_type the message type + */ + virtual void ReceivedMessage(Sv2Client& client, node::Sv2MsgType msg_type) = 0; + + virtual ~Sv2EventsInterface() = default; +}; + +/* + * Handle Stratum v2 connections. + * Currently only supports inbound connections. + */ +class Sv2Connman : SockMan +{ +private: + /** Interface to pass events up */ + Sv2EventsInterface* m_msgproc; + + /** + * The current protocol version of stratum v2 supported by the server. Not to be confused + * with byte value of identitying the stratum v2 subprotocol. + */ + const uint16_t m_protocol_version = 2; + + /** + * The currently supported optional features. + */ + const uint16_t m_optional_features = 0; + + /** + * The subprotocol used in setup connection messages. + * An Sv2Connman only recognizes its own subprotocol. + */ + const uint8_t m_subprotocol; + + CKey m_static_key; + + XOnlyPubKey m_authority_pubkey; + + std::optional m_certificate; + + /** + * A map of all connected stratum v2 clients. + */ + using Clients = std::unordered_map>; + Clients m_sv2_clients GUARDED_BY(m_clients_mutex); + + /** + * Creates a socket and binds the port for new stratum v2 connections. + */ + [[nodiscard]] bool Bind(std::string host, uint16_t port); + + void DisconnectFlagged() EXCLUSIVE_LOCKS_REQUIRED(m_clients_mutex); + + /** + * Create a `Sv2Client` object and add it to the `m_sv2_clients` member. + * @param[in] node_id Id of the newly accepted connection. + * @param[in] me The address and port at our side of the connection. + * @param[in] them The address and port at the peer's side of the connection. + * @retval true on success + * @retval false on failure, meaning that the associated socket and node_id should be discarded + */ + virtual bool EventNewConnectionAccepted(NodeId node_id, + const CService& me, + const CService& them) + EXCLUSIVE_LOCKS_REQUIRED(!m_clients_mutex) override; + + void EventReadyToSend(NodeId node_id, bool& cancel_recv) override + EXCLUSIVE_LOCKS_REQUIRED(!m_clients_mutex); + + virtual void EventGotData(NodeId node_id, const uint8_t* data, size_t n) override + EXCLUSIVE_LOCKS_REQUIRED(!m_clients_mutex); + + virtual void EventGotEOF(NodeId node_id) override + EXCLUSIVE_LOCKS_REQUIRED(!m_clients_mutex); + + virtual void EventGotPermanentReadError(NodeId node_id, const std::string& errmsg) override + EXCLUSIVE_LOCKS_REQUIRED(!m_clients_mutex); + + virtual void EventIOLoopCompletedForAllPeers() EXCLUSIVE_LOCKS_REQUIRED(!m_clients_mutex) override; + + /** + * Encrypt the header and message payload and send it. + * @throws std::runtime_error if encrypting the message fails. + */ + bool EncryptAndSendMessage(Sv2Client& client, node::Sv2NetMsg& net_msg); + + /** + * A helper method to read and decrypt multiple Sv2NetMsgs. + */ + std::vector ReadAndDecryptSv2NetMsgs(Sv2Client& client, Span buffer); + + std::shared_ptr GetClientById(NodeId node_id) const EXCLUSIVE_LOCKS_REQUIRED(m_clients_mutex); + +public: + Sv2Connman(uint8_t subprotocol, CKey static_key, XOnlyPubKey authority_pubkey, Sv2SignatureNoiseMessage certificate) : + m_subprotocol(subprotocol), m_static_key(static_key), m_authority_pubkey(authority_pubkey), m_certificate(certificate) {}; + + ~Sv2Connman(); + + Mutex m_clients_mutex; + + /** + * Starts the Stratum v2 server and thread. + * returns false if port is unable to bind. + */ + [[nodiscard]] bool Start(Sv2EventsInterface* msgproc, std::string host, uint16_t port); + + /** + * Triggered on interrupt signals to stop the main event loop in ThreadSv2Handler(). + */ + void Interrupt(); + + /** + * Tear down of the connman thread and any other necessary tear down. + */ + void StopThreads(); + + /** + * Main handler for all received stratum v2 messages. + */ + void ProcessSv2Message(const node::Sv2NetMsg& sv2_header, Sv2Client& client); + + using Sv2ClientFn = std::function; + /** Perform a function on each fully connected client. */ + void ForEachClient(const Sv2ClientFn& func) EXCLUSIVE_LOCKS_REQUIRED(!m_clients_mutex) + { + LOCK(m_clients_mutex); + for (const auto& client : m_sv2_clients) { + if (client.second->IsFullyConnected()) func(*client.second); + } + }; + + /** Number of clients that are not marked for disconnection, used for tests. */ + size_t ConnectedClients() EXCLUSIVE_LOCKS_REQUIRED(m_clients_mutex) + { + return std::count_if(m_sv2_clients.begin(), m_sv2_clients.end(), [](const auto& c) { + return !c.second->m_disconnect_flag; + }); + } + + /** Number of clients with m_setup_connection_confirmed, used for tests. */ + size_t FullyConnectedClients() EXCLUSIVE_LOCKS_REQUIRED(m_clients_mutex) + { + return std::count_if(m_sv2_clients.begin(), m_sv2_clients.end(), [](const auto& c) { + return c.second->IsFullyConnected(); + }); + } + +}; + +#endif // BITCOIN_SV2_CONNMAN_H diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index a519036ee0b19..4c8e7f29eb0f4 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -178,6 +178,7 @@ endif() if(WITH_SV2) target_sources(test_bitcoin PRIVATE + sv2_connman_tests.cpp sv2_noise_tests.cpp sv2_transport_tests.cpp sv2_messages_tests.cpp diff --git a/src/test/sv2_connman_tests.cpp b/src/test/sv2_connman_tests.cpp new file mode 100644 index 0000000000000..c6eb60d578708 --- /dev/null +++ b/src/test/sv2_connman_tests.cpp @@ -0,0 +1,201 @@ +#include +#include +#include +#include +#include +#include +#include + +#include + +BOOST_FIXTURE_TEST_SUITE(sv2_connman_tests, TestChain100Setup) + +/** + * A class for testing the Sv2Connman. Each ConnTester encapsulates a + * Sv2Connman (the one being tested) as well as a Sv2Cipher + * to act as the other side. + */ +class ConnTester : Sv2EventsInterface { +private: + std::unique_ptr m_remote_transport; //!< Transport for peer + // Sockets that will be returned by the Sv2Connman's listening socket Accept() method. + std::shared_ptr m_sv2connman_accepted_sockets{std::make_shared()}; + + std::shared_ptr m_current_client_pipes; + + XOnlyPubKey m_connman_authority_pubkey; + +public: + std::unique_ptr m_connman; //!< Sv2Connman being tested + + ConnTester() + { + CreateSock = [this](int, int, int) -> std::unique_ptr { + // This will be the bind/listen socket from m_connman. It will + // create other sockets via its Accept() method. + return std::make_unique(std::make_shared(), m_sv2connman_accepted_sockets); + }; + + CKey static_key; + static_key.MakeNewKey(true); + auto authority_key{GenerateRandomKey()}; + m_connman_authority_pubkey = XOnlyPubKey(authority_key.GetPubKey()); + + // Generate and sign certificate + auto now{GetTime()}; + uint16_t version = 0; + // Start validity a little bit in the past to account for clock difference + uint32_t valid_from = static_cast(std::chrono::duration_cast(now).count()) - 3600; + uint32_t valid_to = std::numeric_limits::max(); // 2106 + Sv2SignatureNoiseMessage certificate{version, valid_from, valid_to, XOnlyPubKey(static_key.GetPubKey()), authority_key}; + + m_connman = std::make_unique(TP_SUBPROTOCOL, static_key, m_connman_authority_pubkey, certificate); + + BOOST_REQUIRE(m_connman->Start(this, "127.0.0.1", 18447)); + } + + ~ConnTester() + { + CreateSock = CreateSockOS; + } + + void RemoteToLocalBytes() + { + const auto& [data, more, _m_message_type] = m_remote_transport->GetBytesToSend(/*have_next_message=*/false); + BOOST_REQUIRE(data.size() > 0); + // Schedule data to be returned by the next Recv() call from + // Sv2Connman on the socket it has accepted. + m_current_client_pipes->recv.PushBytes(data.data(), data.size()); + m_remote_transport->MarkBytesSent(data.size()); + } + + size_t LocalToRemoteBytes() + { + uint8_t buf[0x10000]; + // Get the data that has been written to the accepted socket with Send() by Sv2Connman. + // Wait until the bytes appear in the "send" pipe. + ssize_t n; + for (;;) { + n = m_current_client_pipes->send.GetBytes(buf, sizeof(buf), 0); + if (n != -1 || errno != EAGAIN) { + break; + } + UninterruptibleSleep(50ms); + } + + // Inform remote transport that some bytes have been received (sent by the local Sv2Connman). + if (n > 0) { + Span s(buf, n); + BOOST_REQUIRE(m_remote_transport->ReceivedBytes(s)); + } + + return n; + } + + /* Create a new client and perform handshake */ + void handshake() + { + m_remote_transport.reset(); + + auto peer_static_key{GenerateRandomKey()}; + m_remote_transport = std::make_unique(std::move(peer_static_key), m_connman_authority_pubkey); + + // Have Sv2Connman's listen socket's Accept() simulate a newly arrived connection. + m_current_client_pipes = std::make_shared(); + m_sv2connman_accepted_sockets->Push( + std::make_unique(m_current_client_pipes, std::make_shared())); + + // Flush transport for handshake part 1 + RemoteToLocalBytes(); + + // Read handshake part 2 from transport + BOOST_REQUIRE_EQUAL(LocalToRemoteBytes(), Sv2HandshakeState::HANDSHAKE_STEP2_SIZE); + + BOOST_REQUIRE(IsConnected()); + } + + void RemoteToLocalMsg(Sv2NetMsg& msg) + { + // Client encrypts message and puts it on the transport: + CSerializedNetMsg net_msg{std::move(msg)}; + BOOST_REQUIRE(m_remote_transport->SetMessageToSend(net_msg)); + RemoteToLocalBytes(); + } + + bool IsConnected() + { + LOCK(m_connman->m_clients_mutex); + return m_connman->ConnectedClients() > 0; + } + + bool IsFullyConnected() + { + LOCK(m_connman->m_clients_mutex); + return m_connman->FullyConnectedClients() > 0; + } + + Sv2NetMsg SetupConnectionMsg() + { + std::vector bytes{ + 0x02, // protocol + 0x02, 0x00, // min_version + 0x02, 0x00, // max_version + 0x01, 0x00, 0x00, 0x00, // flags + 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x30, // endpoint_host + 0x61, 0x21, // endpoint_port + 0x07, 0x42, 0x69, 0x74, 0x6d, 0x61, 0x69, 0x6e, // vendor + 0x08, 0x53, 0x39, 0x69, 0x20, 0x31, 0x33, 0x2e, 0x35, // hardware_version + 0x1c, 0x62, 0x72, 0x61, 0x69, 0x69, 0x6e, 0x73, 0x2d, 0x6f, 0x73, 0x2d, 0x32, 0x30, + 0x31, 0x38, 0x2d, 0x30, 0x39, 0x2d, 0x32, 0x32, 0x2d, 0x31, 0x2d, 0x68, 0x61, 0x73, + 0x68, // firmware + 0x10, 0x73, 0x6f, 0x6d, 0x65, 0x2d, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2d, 0x75, + 0x75, 0x69, 0x64, // device_id + }; + + return node::Sv2NetMsg{node::Sv2MsgType::SETUP_CONNECTION, std::move(bytes)}; + } + + void ReceivedMessage(Sv2Client& client, node::Sv2MsgType msg_type) override { + } + +}; + +BOOST_AUTO_TEST_CASE(client_tests) +{ + ConnTester tester{}; + + BOOST_REQUIRE(!tester.IsConnected()); + tester.handshake(); + BOOST_REQUIRE(!tester.IsFullyConnected()); + + // After the handshake the remote peer must send a SetupConnection message to the + // Template Provider. + + // An empty SetupConnection message should cause disconnection + node::Sv2NetMsg sv2_msg{node::Sv2MsgType::SETUP_CONNECTION, {}}; + tester.RemoteToLocalMsg(sv2_msg); + BOOST_REQUIRE_EQUAL(tester.LocalToRemoteBytes(), 0); + + BOOST_REQUIRE(!tester.IsConnected()); + + BOOST_TEST_MESSAGE("Reconnect after empty message"); + + // Reconnect + tester.handshake(); + BOOST_TEST_MESSAGE("Handshake done, send SetupConnectionMsg"); + + node::Sv2NetMsg setup{tester.SetupConnectionMsg()}; + tester.RemoteToLocalMsg(setup); + // SetupConnection.Success is 6 bytes + BOOST_REQUIRE_EQUAL(tester.LocalToRemoteBytes(), SV2_HEADER_ENCRYPTED_SIZE + 6 + Poly1305::TAGLEN); + BOOST_REQUIRE(tester.IsFullyConnected()); + + std::vector coinbase_output_max_additional_size_bytes{ + 0x01, 0x00, 0x00, 0x00 + }; + node::Sv2NetMsg msg{node::Sv2MsgType::COINBASE_OUTPUT_DATA_SIZE, std::move(coinbase_output_max_additional_size_bytes)}; + // No reply expected, not yet implemented + tester.RemoteToLocalMsg(msg); +} + +BOOST_AUTO_TEST_SUITE_END()