Add support for remote scanning via custom TCP (#118)

This commit is contained in:
Lee *!* Clagett
2024-09-22 19:55:28 -04:00
committed by Lee *!* Clagett
parent a5d802cd9b
commit cd62461578
31 changed files with 2950 additions and 500 deletions

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2020, The Monero Project
# Copyright (c) 2020-2024, The Monero Project
#
# All rights reserved.
#
@@ -26,6 +26,8 @@
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
add_subdirectory(scanner)
set(monero-lws-rpc_sources admin.cpp client.cpp daemon_pub.cpp daemon_zmq.cpp light_wallet.cpp lws_pub.cpp rates.cpp)
set(monero-lws-rpc_headers admin.h client.h daemon_pub.h daemon_zmq.h fwd.h json.h light_wallet.h lws_pub.h rates.h)

View File

@@ -223,63 +223,6 @@ namespace rpc
};
} // detail
expect<account_push> account_push::make(std::shared_ptr<detail::context> ctx) noexcept
{
MONERO_PRECOND(ctx != nullptr);
account_push out{ctx};
out.sock.reset(zmq_socket(ctx->comm.get(), ZMQ_PUSH));
if (out.sock == nullptr)
return {net::zmq::get_error_code()};
const std::string bind = account_endpoint + std::to_string(++ctx->account_counter);
MONERO_CHECK(do_set_option(out.sock.get(), ZMQ_LINGER, account_zmq_linger));
MONERO_ZMQ_CHECK(zmq_bind(out.sock.get(), bind.c_str()));
return {std::move(out)};
}
account_push::~account_push() noexcept
{}
expect<void> account_push::push(epee::span<const lws::account> accounts, std::chrono::seconds timeout)
{
MONERO_PRECOND(ctx != nullptr);
assert(sock.get() != nullptr);
for (const lws::account& account : accounts)
{
// use integer id values (quick and fast)
wire::msgpack_slice_writer dest{true};
try
{
wire_write::bytes(dest, account);
}
catch (const wire::exception& e)
{
return {e.code()};
}
epee::byte_slice message{dest.take_sink()};
/* This is being pushed by the thread that monitors for shutdown, so
no signal is expected. */
expect<void> sent;
const auto start = std::chrono::steady_clock::now();
while (!(sent = net::zmq::send(message.clone(), sock.get(), ZMQ_DONTWAIT)))
{
if (sent != net::zmq::make_error_code(EAGAIN))
return sent.error();
if (!scanner::is_running())
return {error::signal_abort_process};
const auto elapsed = std::chrono::steady_clock::now() - start;
if (timeout <= elapsed)
return {error::daemon_timeout};
boost::this_thread::sleep_for(boost::chrono::milliseconds{10});
}
}
return success();
}
expect<void> client::get_response(cryptonote::rpc::Message& response, const std::chrono::seconds timeout, const source_location loc)
{
expect<std::string> message = get_message(timeout);
@@ -376,18 +319,6 @@ namespace rpc
return do_subscribe(signal_sub.get(), abort_scan_signal);
}
expect<void> client::enable_pull_accounts()
{
detail::socket new_sock{zmq_socket(ctx->comm.get(), ZMQ_PULL)};
if (new_sock == nullptr)
return {net::zmq::get_error_code()};
const std::string connect =
account_endpoint + std::to_string(ctx->account_counter);
MONERO_ZMQ_CHECK(zmq_connect(new_sock.get(), connect.c_str()));
account_pull = std::move(new_sock);
return success();
}
expect<std::vector<std::pair<client::topic, std::string>>> client::wait_for_block()
{
MONERO_PRECOND(ctx != nullptr);
@@ -501,32 +432,6 @@ namespace rpc
return rc;
}
expect<std::vector<lws::account>> client::pull_accounts()
{
MONERO_PRECOND(ctx != nullptr);
if (!account_pull)
MONERO_CHECK(enable_pull_accounts());
std::vector<lws::account> out{};
for (;;)
{
expect<std::string> next = net::zmq::receive(account_pull.get(), ZMQ_DONTWAIT);
if (!next)
{
if (net::zmq::make_error_code(EAGAIN))
break;
return next.error();
}
out.emplace_back();
const std::error_code error =
wire::msgpack::from_bytes(epee::byte_slice{std::move(*next)}, out.back());
if (error)
return error;
}
return {std::move(out)};
}
expect<rates> client::get_rates() const
{
MONERO_PRECOND(ctx != nullptr);
@@ -643,6 +548,13 @@ namespace rpc
return ctx->daemon_addr;
}
std::chrono::minutes context::cache_interval() const
{
if (ctx == nullptr)
MONERO_THROW(common_error::kInvalidArgument, "Invalid lws::rpc::context");
return ctx->cache_interval;
}
expect<void> context::raise_abort_scan() noexcept
{
MONERO_PRECOND(ctx != nullptr);

View File

@@ -68,31 +68,6 @@ namespace rpc
std::string routing;
};
//! Every scanner "reset", a new socket is created so old messages are discarded
class account_push
{
std::shared_ptr<detail::context> ctx;
detail::socket sock;
explicit account_push(std::shared_ptr<detail::context> ctx) noexcept
: ctx(std::move(ctx)), sock()
{}
public:
static expect<account_push> make(std::shared_ptr<detail::context> ctx) noexcept;
account_push(const account_push&) = delete;
account_push(account_push&&) = default;
~account_push() noexcept;
account_push& operator=(const account_push&) = delete;
account_push& operator=(account_push&&) = default;
//! Push new `accounts` to worker threads. Each account is sent in unique message
expect<void> push(epee::span<const lws::account> accounts, std::chrono::seconds timeout);
};
//! Abstraction for ZMQ RPC client. Only `get_rates()` thread-safe; use `clone()`.
class client
{
@@ -100,10 +75,9 @@ namespace rpc
detail::socket daemon;
detail::socket daemon_sub;
detail::socket signal_sub;
detail::socket account_pull;
explicit client(std::shared_ptr<detail::context> ctx) noexcept
: ctx(std::move(ctx)), daemon(), daemon_sub(), signal_sub(), account_pull()
: ctx(std::move(ctx)), daemon(), daemon_sub(), signal_sub()
{}
//! Expect `response` as the next message payload unless error.
@@ -156,9 +130,6 @@ namespace rpc
//! `wait`, `send`, and `receive` will watch for `raise_abort_scan()`.
expect<void> watch_scan_signals() noexcept;
//! Register `this` client as listening for new accounts
expect<void> enable_pull_accounts();
//! Wait for new block announce or internal timeout.
expect<std::vector<std::pair<topic, std::string>>> wait_for_block();
@@ -259,18 +230,15 @@ namespace rpc
//! \return The full address of the monerod ZMQ daemon.
std::string const& daemon_address() const;
//! \return Exchange rate checking interval
std::chrono::minutes cache_interval() const;
//! \return Client connection. Thread-safe.
expect<client> connect() const noexcept
{
return client::make(ctx);
}
//! Create a new account push state
expect<account_push> bind_push() const noexcept
{
return account_push::make(ctx);
}
/*!
All block `client::send`, `client::receive`, and `client::wait` calls
originating from `this` object AND whose `watch_scan_signal` method was

View File

@@ -34,4 +34,5 @@ namespace lws
class client;
}
struct rates;
class scan_manager;
}

View File

@@ -0,0 +1,37 @@
# Copyright (c) 2024, The Monero Project
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be
# used to endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set(monero-lws-rpc-scanner_sources
client.cpp commands.cpp connection.cpp queue.cpp server.cpp write_commands.cpp
)
set(monero-lws-rpc-scanner_headers
client.h commands.h connection.h fwd.h queue.h read_commands.h server.h write_commands.h
)
add_library(monero-lws-rpc-scanner ${monero-lws-rpc-scanner_sources} ${monero-lws-rpc-scanner_headers})
target_link_libraries(monero-lws-rpc-scanner monero::libraries monero-lws-wire-msgpack)

254
src/rpc/scanner/client.cpp Normal file
View File

@@ -0,0 +1,254 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "client.h"
#include <boost/asio/coroutine.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <chrono>
#include "common/expect.h" // monero/src
#include "misc_log_ex.h" // monero/contrib/epee/include
#include "net/net_utils_base.h" // monero/contrib/epee/include
#include "rpc/scanner/commands.h"
#include "rpc/scanner/connection.h"
#include "rpc/scanner/read_commands.h"
#include "rpc/scanner/server.h"
#include "rpc/scanner/write_commands.h"
namespace lws { namespace rpc { namespace scanner
{
namespace
{
//! Connection completion timeout
constexpr const std::chrono::seconds connect_timeout{5};
//! Retry connection timeout
constexpr const std::chrono::seconds reconnect_interval{10};
struct push_accounts_handler
{
using input = push_accounts;
static bool handle(const std::shared_ptr<client>& self, input msg)
{
if (!self)
return false;
if (msg.users.empty())
return true;
client::push_accounts(self, std::move(msg.users));
return true;
}
};
struct replace_accounts_handler
{
using input = replace_accounts;
static bool handle(const std::shared_ptr<client>& self, input msg)
{
if (!self)
return false;
// push empty accounts too, indicates we should stop scanning
client::replace_accounts(self, std::move(msg.users));
return true;
}
};
} // anonymous
//! \brief Closes the socket, forcing all outstanding ops to cancel.
struct client::close
{
std::shared_ptr<client> self_;
void operator()(const boost::system::error_code& error = {}) const
{
if (self_ && error != boost::asio::error::operation_aborted)
{
// The `cleanup()` function is meant to cleanup then destruct connection
assert(self_->strand_.running_in_this_thread());
boost::system::error_code error{};
self_->sock_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
error = boost::system::error_code{};
self_->sock_.close(error);
if (error)
MERROR("Error when closing socket: " << error.message());
}
}
};
//! \brief
class client::connector : public boost::asio::coroutine
{
std::shared_ptr<client> self_;
public:
explicit connector(std::shared_ptr<client> self)
: boost::asio::coroutine(), self_(std::move(self))
{}
void operator()(const boost::system::error_code& error = {})
{
if (!self_ || error == boost::asio::error::operation_aborted)
return;
assert(self_->strand_.running_in_this_thread());
BOOST_ASIO_CORO_REENTER(*this)
{
for (;;)
{
MINFO("Attempting connection to " << self_->server_address_);
self_->connect_timer_.expires_from_now(connect_timeout);
self_->connect_timer_.async_wait(self_->strand_.wrap(close{self_}));
BOOST_ASIO_CORO_YIELD self_->sock_.async_connect(self_->server_address_, self_->strand_.wrap(*this));
if (!self_->connect_timer_.cancel() || error)
{
MERROR("Connection attempt failed: " << error.message());
close{self_}();
}
else
break;
MINFO("Retrying connection in " << std::chrono::seconds{reconnect_interval}.count() << " seconds");
self_->connect_timer_.expires_from_now(reconnect_interval);
BOOST_ASIO_CORO_YIELD self_->connect_timer_.async_wait(self_->strand_.wrap(*this));
}
MINFO("Connection made to " << self_->server_address_);
self_->connected_ = true;
const auto threads = boost::numeric_cast<std::uint32_t>(self_->local_.size());
write_command(self_, initialize{self_->pass_, threads});
read_commands(self_);
}
}
};
client::client(boost::asio::io_service& io, const std::string& address, std::string pass, std::vector<std::shared_ptr<queue>> local)
: connection(io),
local_(std::move(local)),
pass_(std::move(pass)),
next_push_(0),
connect_timer_(io),
server_address_(rpc::scanner::server::get_endpoint(address)),
connected_(false)
{
for (const auto& queue : local_)
{
if (!queue)
MONERO_THROW(common_error::kInvalidArgument, "nullptr local queue");
}
}
client::~client()
{}
//! \return Handlers for commands from server
const std::array<client::command, 2>& client::commands() noexcept
{
static constexpr const std::array<command, 2> value{{
call<push_accounts_handler, client>,
call<replace_accounts_handler, client>
}};
static_assert(push_accounts_handler::input::id() == 0);
static_assert(replace_accounts_handler::input::id() == 1);
return value;
}
void client::connect(const std::shared_ptr<client>& self)
{
if (!self)
MONERO_THROW(common_error::kInvalidArgument, "nullptr self");
self->strand_.dispatch([self] ()
{
if (!self->sock_.is_open())
connector{self}();
});
}
void client::push_accounts(const std::shared_ptr<client>& self, std::vector<lws::account> users)
{
if (!self)
MONERO_THROW(common_error::kInvalidArgument, "nullptr self");
self->strand_.dispatch([self, users = std::move(users)] () mutable
{
/* Keep this algorithm simple, one user at a time. A little more difficult
to do multiples at once */
MINFO("Adding " << users.size() << " new accounts to workload");
for (std::size_t i = 0; i < users.size(); ++i)
{
self->local_[self->next_push_++]->push_accounts(
std::make_move_iterator(users.begin() + i),
std::make_move_iterator(users.begin() + i + 1)
);
self->next_push_ %= self->local_.size();
}
});
}
void client::replace_accounts(const std::shared_ptr<client>& self, std::vector<lws::account> users)
{
if (!self)
MONERO_THROW(common_error::kInvalidArgument, "nullptr self");
self->strand_.dispatch([self, users = std::move(users)] () mutable
{
MINFO("Received " << users.size() << " accounts as new workload");
for (std::size_t i = 0; i < self->local_.size(); ++i)
{
// count == 0 is OK. This will tell the thread to stop working
const auto count = users.size() / (self->local_.size() - i);
std::vector<lws::account> next{
std::make_move_iterator(users.end() - count),
std::make_move_iterator(users.end())
};
users.erase(users.end() - count, users.end());
self->local_[i]->replace_accounts(std::move(next));
}
self->next_push_ = 0;
});
}
void client::send_update(const std::shared_ptr<client>& self, std::vector<lws::account> users, std::vector<crypto::hash> blocks)
{
if (!self)
MONERO_THROW(common_error::kInvalidArgument, "nullptr self");
self->strand_.dispatch([self, users = std::move(users), blocks = std::move(blocks)] () mutable
{
if (!self->connected_)
MONERO_THROW(common_error::kInvalidArgument, "not connected");
write_command(self, update_accounts{std::move(users), std::move(blocks)});
});
}
void client::cleanup()
{
base_cleanup();
GET_IO_SERVICE(sock_).stop();
}
}}} // lws // rpc // scanner

87
src/rpc/scanner/client.h Normal file
View File

@@ -0,0 +1,87 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/steady_timer.hpp>
#include <cstddef>
#include <memory>
#include <string>
#include <vector>
#include "crypto/hash.h" // monero/src
#include "db/fwd.h"
#include "rpc/scanner/connection.h"
#include "rpc/scanner/queue.h"
namespace lws { namespace rpc { namespace scanner
{
//! \brief
class client : public connection
{
const std::vector<std::shared_ptr<queue>> local_;
const std::string pass_;
std::size_t next_push_;
boost::asio::steady_timer connect_timer_;
const boost::asio::ip::tcp::endpoint server_address_;
bool connected_;
struct close;
class connector;
public:
using command = bool(*)(const std::shared_ptr<client>&);
//! Does not start connection to `address`, see `connect`.
explicit client(boost::asio::io_context& io, const std::string& address, std::string pass, std::vector<std::shared_ptr<queue>> local);
client(const client&) = delete;
client(client&&) = delete;
~client();
client& operator=(const client&) = delete;
client& operator=(client&&) = delete;
//! \return Handlers for client commands
static const std::array<command, 2>& commands() noexcept;
//! Start a connect loop on `self`.
static void connect(const std::shared_ptr<client>& self);
//! Push `users` to local queues. Synchronizes with latest connection.
static void push_accounts(const std::shared_ptr<client>& self, std::vector<lws::account> users);
//! Replace `users` on local queues. Synchronizes with latest connection.
static void replace_accounts(const std::shared_ptr<client>& self, std::vector<lws::account> users);
//! Send `users` upstream for disk storage
static void send_update(const std::shared_ptr<client>& self, std::vector<lws::account> users, std::vector<crypto::hash> blocks);
//! Closes socket and calls stop on `io_service`.
void cleanup();
};
}}} // lws // rpc // scanner

View File

@@ -0,0 +1,85 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "commands.h"
#include "db/account.h"
#include "db/data.h"
#include "wire/adapted/crypto.h"
#include "wire/vector.h"
#include "wire/msgpack.h"
#include "wire/wrapper/trusted_array.h"
namespace lws { namespace rpc { namespace scanner
{
namespace
{
template<typename F, typename T>
void map_initialize(F& format, T& self)
{
wire::object(format, WIRE_FIELD_ID(0, pass), WIRE_FIELD_ID(1, threads));
}
}
WIRE_MSGPACK_DEFINE_OBJECT(initialize, map_initialize);
namespace
{
template<typename F, typename T>
void map_account_update(F& format, T& self)
{
wire::object(format,
wire::optional_field<0>("users", wire::trusted_array(std::ref(self.users))),
wire::optional_field<1>("blocks", wire::trusted_array(std::ref(self.blocks)))
);
}
}
WIRE_MSGPACK_DEFINE_OBJECT(update_accounts, map_account_update)
namespace
{
template<typename F, typename T>
void map_push_accounts(F& format, T& self)
{
wire::object(format,
wire::optional_field<0>("users", wire::trusted_array(std::ref(self.users)))
);
}
}
WIRE_MSGPACK_DEFINE_OBJECT(push_accounts, map_push_accounts);
namespace
{
template<typename F, typename T>
void map_replace_accounts(F& format, T& self)
{
wire::object(format,
wire::optional_field<0>("users", wire::trusted_array(std::ref(self.users)))
);
}
}
WIRE_MSGPACK_DEFINE_OBJECT(replace_accounts, map_replace_accounts);
}}} // lws // rpc // scanner

101
src/rpc/scanner/commands.h Normal file
View File

@@ -0,0 +1,101 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <boost/endian/buffers.hpp>
#include <cstdint>
#include <vector>
#include "crypto/hash.h" // monero/src
#include "db/fwd.h"
#include "wire/msgpack/fwd.h"
namespace lws { namespace rpc { namespace scanner
{
/*
`monero-lws-daemon` is "server" and `monero-lws-scanner` is "client".
*/
//! \brief Data sent before msgpack payload
struct header
{
using length_type = boost::endian::little_uint32_buf_t;
header() = delete;
std::uint8_t version;
std::uint8_t id;
length_type length;
};
static_assert(sizeof(header) == 6);
/*
Client to server messages.
*/
//! \brief Inform server of info needed to spawn work to client.
struct initialize
{
initialize() = delete;
static constexpr std::uint8_t id() noexcept { return 0; }
std::string pass;
std::uint32_t threads;
};
WIRE_MSGPACK_DECLARE_OBJECT(initialize);
//! Command that updates database account records
struct update_accounts
{
update_accounts() = delete;
static constexpr std::uint8_t id() noexcept { return 1; }
std::vector<lws::account> users;
std::vector<crypto::hash> blocks;
};
WIRE_MSGPACK_DECLARE_OBJECT(update_accounts);
/*
Server to client messages.
*/
//! \brief New accounts to add/push to scanning list
struct push_accounts
{
push_accounts() = delete;
static constexpr std::uint8_t id() noexcept { return 0; }
std::vector<lws::account> users;
};
WIRE_MSGPACK_DECLARE_OBJECT(push_accounts);
//! \brief Replace account scanning list with this new one
struct replace_accounts
{
replace_accounts() = delete;
static constexpr const std::uint8_t id() noexcept { return 1; }
std::vector<lws::account> users;
};
WIRE_MSGPACK_DECLARE_OBJECT(replace_accounts);
}}} // lws // rpc // scanner

View File

@@ -0,0 +1,83 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "connection.h"
#include "misc_log_ex.h" // monero/contrib/epee/include
namespace lws { namespace rpc { namespace scanner
{
connection::connection(boost::asio::io_service& io)
: read_buf_(),
write_bufs_(),
sock_(io),
write_timeout_(io),
strand_(io),
next_{},
cleanup_(false)
{}
connection::~connection()
{}
boost::asio::ip::tcp::endpoint connection::remote_endpoint()
{
boost::system::error_code error{};
return sock_.remote_endpoint(error);
}
boost::asio::mutable_buffer connection::read_buffer(const std::size_t size)
{
assert(strand_.running_in_this_thread());
read_buf_.clear();
read_buf_.put_n(0, size);
return boost::asio::mutable_buffer(read_buf_.data(), size);
}
boost::asio::const_buffer connection::write_buffer() const
{
assert(strand_.running_in_this_thread());
if (write_bufs_.empty())
return boost::asio::const_buffer(nullptr, 0);
return boost::asio::const_buffer(write_bufs_.front().data(), write_bufs_.front().size());
}
void connection::base_cleanup()
{
assert(strand_.running_in_this_thread());
if (!cleanup_)
MINFO("Disconnected from " << remote_endpoint() << " / " << this);
cleanup_ = true;
boost::system::error_code error{};
sock_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
error = boost::system::error_code{};
sock_.close(error);
if (error)
MERROR("Error when closing socket: " << error.message());
write_timeout_.cancel();
}
}}} // lws // rpc // scanner

View File

@@ -0,0 +1,71 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <atomic>
#include <boost/asio/buffer.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/strand.hpp>
#include <cstdint>
#include <deque>
#include <string>
#include "byte_slice.h" // monero/contrib/epee/include
#include "byte_stream.h" // monero/contrib/epee/include
#include "rpc/scanner/commands.h"
namespace lws { namespace rpc { namespace scanner
{
//! \brief Base class for `client_connection` and `server_connection`. Always use `strand_`.
struct connection
{
// Leave public for coroutines `read_commands` and `write_commands`
epee::byte_stream read_buf_;
std::deque<epee::byte_slice> write_bufs_;
boost::asio::ip::tcp::socket sock_;
boost::asio::steady_timer write_timeout_;
boost::asio::io_service::strand strand_;
header next_;
bool cleanup_;
explicit connection(boost::asio::io_service& io);
~connection();
boost::asio::ip::tcp::endpoint remote_endpoint();
//! \return ASIO compatible read buffer of `size`.
boost::asio::mutable_buffer read_buffer(const std::size_t size);
//! \return ASIO compatible write buffer
boost::asio::const_buffer write_buffer() const;
//! Cancels operations on socket and timer. Also updates `cleanup_ = true`.
void base_cleanup();
};
}}} // lws // rpc // scanner

42
src/rpc/scanner/fwd.h Normal file
View File

@@ -0,0 +1,42 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
namespace lws { namespace rpc { namespace scanner
{
class client;
struct connection;
struct give_accounts;
struct header;
struct push_accounts;
class queue;
template<typename> struct do_read_commands;
class server;
struct take_accounts;
struct update_accounts;
template<typename> struct write_commands;
}}} // lws // rpc // scanner

91
src/rpc/scanner/queue.cpp Normal file
View File

@@ -0,0 +1,91 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "queue.h"
#include "db/account.h"
namespace lws { namespace rpc { namespace scanner
{
queue::status queue::do_get_accounts()
{
status out{
std::move(replace_), std::move(push_), user_count_
};
replace_ = std::nullopt;
push_.clear();
push_.shrink_to_fit();
return out;
}
queue::queue()
: replace_(), push_(), user_count_(0), sync_(), poll_(), stop_(false)
{}
queue::~queue()
{}
void queue::stop()
{
{
const boost::lock_guard<boost::mutex> lock{sync_};
stop_ = true;
}
poll_.notify_all();
}
std::size_t queue::user_count()
{
const boost::lock_guard<boost::mutex> lock{sync_};
return user_count_;
}
queue::status queue::get_accounts()
{
const boost::lock_guard<boost::mutex> lock{sync_};
return do_get_accounts();
}
queue::status queue::wait_for_accounts()
{
boost::unique_lock<boost::mutex> lock{sync_};
if (!replace_ && push_.empty() && !stop_)
poll_.wait(lock, [this] () { return replace_ || !push_.empty() || stop_; });
return do_get_accounts();
}
void queue::replace_accounts(std::vector<lws::account> users)
{
{
const boost::lock_guard<boost::mutex> lock{sync_};
replace_ = std::move(users);
user_count_ = replace_->size();
push_.clear();
}
poll_.notify_all();
}
}}} // lws // rpc // scanner

94
src/rpc/scanner/queue.h Normal file
View File

@@ -0,0 +1,94 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/lock_guard.hpp>
#include <boost/thread/mutex.hpp>
#include <cstddef>
#include <optional>
#include <vector>
#include "db/fwd.h"
#include "rpc/scanner/commands.h"
namespace lws { namespace rpc { namespace scanner
{
//! Notifies worker thread of new accounts to scan. All functions thread-safe.
class queue
{
public:
//! Status of upstream scan requests.
struct status
{
std::optional<std::vector<lws::account>> replace; //!< Empty optional means replace **not** requested.
std::vector<lws::account> push;
std::size_t user_count;
};
private:
std::optional<std::vector<lws::account>> replace_;
std::vector<lws::account> push_;
std::size_t user_count_;
boost::mutex sync_;
boost::condition_variable poll_;
bool stop_;
status do_get_accounts();
public:
queue();
~queue();
//! `wait_for_accounts()` will return immediately, permanently.
void stop();
//! \return Total number of users given to this local thread
std::size_t user_count();
//! \return Replacement and "push" accounts
status get_accounts();
//! Blocks until replace or push accounts become available OR `stop()` is called.
status wait_for_accounts();
//! Replace existing accounts on thread with new `users`
void replace_accounts(std::vector<lws::account> users);
//! Push/add new accounts to scan on thread
template<typename T>
void push_accounts(T begin, T end)
{
{
const boost::lock_guard<boost::mutex> lock{sync_};
user_count_ += (end - begin);
push_.insert(push_.end(), std::make_move_iterator(begin), std::make_move_iterator(end));
}
poll_.notify_all();
}
};
}}} // lws // rpc // scanner

View File

@@ -0,0 +1,148 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <boost/asio/coroutine.hpp>
#include <boost/asio/read.hpp>
#include <cstring>
#include <limits>
#include <memory>
#include <system_error>
#include <type_traits>
#include <utility>
#include <vector>
#include "byte_slice.h" // monero/contrib/epee/include
#include "db/account.h"
#include "misc_log_ex.h"
#include "rpc/scanner/connection.h"
#include "wire/msgpack/base.h"
#include "wire/msgpack/read.h"
namespace lws { namespace rpc { namespace scanner
{
/*! Function for binding to command callables. Must be exeucting "inside of"
connection strand.
\tparam F concept requirements:
* Must have inner `typedef` named `input` which specifies a type
that can read from msgpack bytes.
* Must have static function `handle` with interface
`bool(std::shared_ptr<T>, F::input)`.
\tparam T concept requirements:
* Must be derived from `lws::rpc::scanner::connection`. */
template<typename F, typename T>
bool call(const std::shared_ptr<T>& self)
{
static_assert(std::is_base_of<connection, T>{});
if (!self)
return false;
assert(self->strand_.running_in_this_thread());
typename F::input data{};
const std::error_code error =
wire::msgpack::from_bytes(epee::byte_slice{std::move(self->read_buf_)}, data);
self->read_buf_.clear();
if (error)
{
MERROR("Failed to unpack message (from " << self->remote_endpoint() << "): " << error.message());
return false;
}
return F::handle(self, std::move(data));
}
/*! \brief ASIO coroutine for reading remote client OR server commands.
\tparam T concept requirements:
* Must be derived from `lws::rpc::scanner::connection`.
* Must have `cleanup()` function that invokes `base_cleanup()`, and
does any other necessary work given that the socket connection is being
terminated.
* Must have a static `commands()` function, which returns a `std::array`
of `bool(std::shared_ptr<T>)` callables. The position in the array
determines the command number. */
template<typename T>
class do_read_commands : public boost::asio::coroutine
{
static_assert(std::is_base_of<connection, T>{});
const std::shared_ptr<T> self_;
public:
explicit do_read_commands(std::shared_ptr<T> self)
: boost::asio::coroutine(), self_(std::move(self))
{}
//! Invoke with no arguments to start read commands loop
void operator()(const boost::system::error_code& error = {}, const std::size_t transferred = 0)
{
if (!self_)
return;
assert(self_->strand_.running_in_this_thread());
if (error)
{
if (error != boost::asio::error::operation_aborted)
{
MERROR("Read error on socket (" << self_->remote_endpoint() << "): " << error.message());
self_->cleanup();
}
return;
}
if (self_->cleanup_)
return; // callback queued before cancellation
BOOST_ASIO_CORO_REENTER(*this)
{
for (;;) // multiple commands
{
// indefinite read timeout (waiting for next command)
BOOST_ASIO_CORO_YIELD boost::asio::async_read(self_->sock_, self_->read_buffer(sizeof(self_->next_)), self_->strand_.wrap(*this));
std::memcpy(std::addressof(self_->next_), self_->read_buf_.data(), sizeof(self_->next_));
static_assert(std::numeric_limits<header::length_type::value_type>::max() <= std::numeric_limits<std::size_t>::max());
BOOST_ASIO_CORO_YIELD boost::asio::async_read(self_->sock_, self_->read_buffer(self_->next_.length.value()), self_->strand_.wrap(*this));
const auto& commands = T::commands();
if (commands.size() <= self_->next_.id || !commands[self_->next_.id](self_))
{
self_->cleanup();
return; // stop reading commands
}
}
}
}
};
template<typename T>
bool read_commands(const std::shared_ptr<T>& self)
{
if (!self)
return false;
self->strand_.dispatch(do_read_commands{self});
return true;
}
}}} // lws // rpc // scanner

528
src/rpc/scanner/server.cpp Normal file
View File

@@ -0,0 +1,528 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "server.h"
#include <boost/asio/coroutine.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <sodium/utils.h>
#include <sodium/randombytes.h>
#include <vector>
#include "byte_slice.h" // monero/contrib/epee/include
#include "byte_stream.h" // monero/contrib/epee/include
#include "common/expect.h" // monero/src/
#include "error.h"
#include "misc_log_ex.h" // monero/contrib/epee/include
#include "net/net_utils_base.h" // monero/contrib/epee/include
#include "rpc/scanner/commands.h"
#include "rpc/scanner/connection.h"
#include "rpc/scanner/read_commands.h"
#include "rpc/scanner/write_commands.h"
#include "scanner.h"
namespace lws { namespace rpc { namespace scanner
{
namespace
{
//! Use remote scanning only if users-per-local-thread exceeds this
constexpr const std::size_t remote_threshold = 100;
//! Threshold for resetting/replacing state instead of pushing
constexpr const std::size_t replace_threshold = 10000;
//! \brief Handler for server to initialize new scanner
struct initialize_handler
{
using input = initialize;
static bool handle(const std::shared_ptr<server_connection>& self, input msg);
};
//! \brief Handler for request to update accounts
struct update_accounts_handler
{
using input = update_accounts;
static bool handle(const std::shared_ptr<server_connection>& self, input msg);
};
using command = bool(*)(const std::shared_ptr<server_connection>&);
} // anonymous
//! \brief Context/state for remote `monero-lws-scanner` instance.
struct server_connection : connection
{
const std::shared_ptr<server> parent_;
std::size_t threads_; //!< Number of scan threads at remote process
public:
explicit server_connection(std::shared_ptr<server> parent, boost::asio::io_service& io)
: connection(io),
parent_(std::move(parent)),
threads_(0)
{
if (!parent_)
MONERO_THROW(common_error::kInvalidArgument, "nullptr parent");
}
//! \return Handlers for commands from client
static const std::array<command, 2>& commands() noexcept
{
static constexpr const std::array<command, 2> value{{
call<initialize_handler, server_connection>,
call<update_accounts_handler, server_connection>
}};
static_assert(initialize_handler::input::id() == 0);
static_assert(update_accounts_handler::input::id() == 1);
return value;
}
//! Cancels pending operations and "pushes" accounts to other processes
void cleanup()
{
base_cleanup();
}
};
namespace
{
bool initialize_handler::handle(const std::shared_ptr<server_connection>& self, const input msg)
{
if (!self)
return false;
assert(self->strand_.running_in_this_thread());
if (self->threads_)
{
MERROR("Client ( " << self->remote_endpoint() << ") invoked initialize twice, closing connection");
return false;
}
if (!msg.threads)
{
MERROR("Client (" << self->remote_endpoint() << ") intialized with 0 threads");
return false;
}
if (!self->parent_->check_pass(msg.pass))
{
MERROR("Client (" << self->remote_endpoint() << ") provided invalid pass");
return false;
}
self->threads_ = boost::numeric_cast<std::size_t>(msg.threads);
server::replace_users(self->parent_);
return true;
}
bool update_accounts_handler::handle(const std::shared_ptr<server_connection>& self, input msg)
{
if (!self)
return false;
if (msg.users.empty())
return true;
server::store(self->parent_, std::move(msg.users), std::move(msg.blocks));
return true;
}
} // anonymous
class server::acceptor : public boost::asio::coroutine
{
std::shared_ptr<server> self_;
std::shared_ptr<server_connection> next_;
public:
explicit acceptor(std::shared_ptr<server> self)
: boost::asio::coroutine(), self_(std::move(self)), next_(nullptr)
{}
void operator()(const boost::system::error_code& error = {})
{
if (!self_ || error)
{
if (error == boost::asio::error::operation_aborted)
return; // exiting
MONERO_THROW(error, "server acceptor failed");
}
assert(self_->strand_.running_in_this_thread());
BOOST_ASIO_CORO_REENTER(*this)
{
for (;;)
{
next_ = std::make_shared<server_connection>(self_, GET_IO_SERVICE(self_->check_timer_));
BOOST_ASIO_CORO_YIELD self_->acceptor_.async_accept(next_->sock_, self_->strand_.wrap(*this));
MINFO("New connection to " << next_->remote_endpoint() << " / " << next_.get());
self_->remote_.emplace(next_);
read_commands(std::move(next_));
}
}
}
};
struct server::check_users
{
std::shared_ptr<server> self_;
void operator()(const boost::system::error_code& error = {}) const
{
if (!self_ || error == boost::asio::error::operation_aborted)
return;
assert(self_->strand_.running_in_this_thread());
self_->check_timer_.expires_from_now(account_poll_interval);
self_->check_timer_.async_wait(self_->strand_.wrap(*this));
std::size_t total_threads = self_->local_.size();
std::vector<std::shared_ptr<server_connection>> remotes{};
remotes.reserve(self_->remote_.size());
for (auto& remote : self_->remote_)
{
auto conn = remote.lock();
if (!conn)
{
// connection loss detected, re-shuffle accounts
self_->do_replace_users();
return;
}
if (std::numeric_limits<std::size_t>::max() - total_threads < conn->threads_)
MONERO_THROW(error::configuration, "Exceeded max threads (size_t) across all systems");
total_threads += conn->threads_;
remotes.push_back(std::move(conn));
}
if (!total_threads)
{
MWARNING("Currently no worker threads, waiting for new clients");
return;
}
auto reader = self_->disk_.start_read(std::move(self_->read_txn_));
if (!reader)
{
if (reader.matches(std::errc::no_lock_available))
{
MWARNING("Failed to open DB read handle, retrying later");
return;
}
MONERO_THROW(reader.error(), "Failed to open DB read handle");
}
auto current_users = MONERO_UNWRAP(
reader->get_accounts(db::account_status::active, std::move(self_->accounts_cur_))
);
if (current_users.count() < self_->active_.size())
{
// a shrinking user base, re-shuffle
self_->do_replace_users();
return;
}
std::vector<db::account_id> active_copy = self_->active_;
std::vector<lws::account> new_accounts;
for (auto user = current_users.make_iterator(); !user.is_end(); ++user)
{
const db::account_id user_id = user.get_value<MONERO_FIELD(db::account, id)>();
const auto loc = std::lower_bound(active_copy.begin(), active_copy.end(), user_id);
if (loc == active_copy.end() || *loc != user_id)
{
new_accounts.push_back(MONERO_UNWRAP(reader->get_full_account(user.get_value<db::account>())));
if (replace_threshold < new_accounts.size())
{
self_->do_replace_users();
return;
}
self_->active_.insert(
std::lower_bound(self_->active_.begin(), self_->active_.end(), user_id),
user_id
);
}
else
active_copy.erase(loc);
}
if (!active_copy.empty())
{
self_->do_replace_users();
return;
}
self_->next_thread_ %= total_threads;
while (!new_accounts.empty())
{
if (self_->next_thread_ < self_->local_.size())
{
self_->local_[self_->next_thread_]->push_accounts(
std::make_move_iterator(new_accounts.end() - 1),
std::make_move_iterator(new_accounts.end())
);
new_accounts.erase(new_accounts.end() - 1);
++self_->next_thread_;
}
else
{
std::size_t j = 0;
for (auto offset = self_->local_.size(); j < remotes.size(); ++j)
{
if (self_->next_thread_ <= offset)
break;
offset += remotes[j]->threads_;
}
const auto user_count = std::min(new_accounts.size(), remotes[j]->threads_);
std::vector<lws::account> next{
std::make_move_iterator(new_accounts.end() - user_count),
std::make_move_iterator(new_accounts.end())
};
new_accounts.erase(new_accounts.end() - user_count);
write_command(remotes[j], push_accounts{std::move(next)});
self_->next_thread_ += remotes[j]->threads_;
}
self_->next_thread_ %= total_threads;
}
self_->read_txn_ = reader->finish_read();
self_->accounts_cur_ = current_users.give_cursor();
}
};
void server::do_replace_users()
{
assert(strand_.running_in_this_thread());
MINFO("Updating/replacing user account(s) on worker thread(s)");
std::size_t remaining_threads = local_.size();
std::vector<std::shared_ptr<server_connection>> remotes;
remotes.reserve(remote_.size());
for (auto remote = remote_.begin(); remote != remote_.end(); )
{
auto conn = remote->lock();
if (conn)
{
if (std::numeric_limits<std::size_t>::max() - remaining_threads < conn->threads_)
MONERO_THROW(error::configuration, "Exceeded max threads (size_t) across all systems");
remaining_threads += conn->threads_;
remotes.push_back(std::move(conn));
++remote;
}
else
remote = remote_.erase(remote);
}
if (!remaining_threads)
{
MWARNING("Currently no worker threads, waiting for new clients");
return;
}
std::vector<db::account_id> active{};
std::vector<db::account> users{};
auto reader = MONERO_UNWRAP(disk_.start_read());
{
auto active_users = MONERO_UNWRAP(reader.get_accounts(db::account_status::active));
const auto active_count = active_users.count();
active.reserve(active_count);
users.reserve(active_count);
for (auto user : active_users.make_range())
{
active.insert(std::lower_bound(active.begin(), active.end(), user.id), user.id);
users.insert(std::lower_bound(users.begin(), users.end(), user, by_height{}), user);
}
}
// if under `remote_threshold` users per thread, use local scanning only
if (local_.size() && (users.size() / local_.size()) < remote_threshold)
remaining_threads = local_.size();
// make sure to notify of zero users too!
for (auto& local : local_)
{
const auto user_count = users.size() / remaining_threads;
std::vector<lws::account> next{};
next.reserve(user_count);
for (std::size_t j = 0; !users.empty() && j < user_count; ++j)
{
next.push_back(MONERO_UNWRAP(reader.get_full_account(users.back())));
users.erase(users.end() - 1);
}
local->replace_accounts(std::move(next));
--remaining_threads;
}
// make sure to notify of zero users too!
for (auto& remote : remotes)
{
const auto users_per_thread = users.size() / std::max(std::size_t(1), remaining_threads);
const auto user_count = std::max(std::size_t(1), users_per_thread) * remote->threads_;
std::vector<lws::account> next{};
next.reserve(user_count);
for (std::size_t j = 0; !users.empty() && j < user_count; ++j)
{
next.push_back(MONERO_UNWRAP(reader.get_full_account(users.back())));
users.erase(users.end() - 1);
}
write_command(remote, replace_accounts{std::move(next)});
remaining_threads -= std::min(remaining_threads, remote->threads_);
}
next_thread_ = 0;
active_ = std::move(active);
}
boost::asio::ip::tcp::endpoint server::get_endpoint(const std::string& address)
{
std::string host;
std::string port;
{
const auto split = address.rfind(':');
if (split == std::string::npos)
{
host = "0.0.0.0";
port = address;
}
else
{
host = address.substr(0, split);
port = address.substr(split + 1);
}
}
return boost::asio::ip::tcp::endpoint{
boost::asio::ip::address::from_string(host), boost::lexical_cast<unsigned short>(port)
};
}
server::server(boost::asio::io_service& io, db::storage disk, rpc::client zclient, std::vector<std::shared_ptr<queue>> local, std::vector<db::account_id> active, ssl_verification_t webhook_verify)
: strand_(io),
check_timer_(io),
acceptor_(io),
remote_(),
local_(std::move(local)),
active_(std::move(active)),
disk_(std::move(disk)),
zclient_(std::move(zclient)),
read_txn_{},
accounts_cur_{},
next_thread_(0),
pass_hashed_(),
pass_salt_(),
webhook_verify_(webhook_verify)
{
std::sort(active_.begin(), active_.end());
for (const auto& local : local_)
{
if (!local)
MONERO_THROW(common_error::kInvalidArgument, "given nullptr local queue");
}
std::memset(pass_hashed_.data(), 0, pass_hashed_.size());
randombytes_buf(pass_salt_.data(), pass_salt_.size());
}
server::~server() noexcept
{}
bool server::check_pass(const std::string& pass) const noexcept
{
std::array<unsigned char, 32> out;
std::memset(out.data(), 0, out.size());
compute_hash(out, pass);
return sodium_memcmp(out.data(), pass_hashed_.data(), out.size()) == 0;
}
void server::compute_hash(std::array<unsigned char, 32>& out, const std::string& pass) const noexcept
{
if (out.size() < crypto_pwhash_BYTES_MIN)
MONERO_THROW(error::crypto_failure, "Invalid output size");
if (crypto_pwhash_BYTES_MAX < out.size())
MONERO_THROW(error::crypto_failure, "Invalid output size");
if (pass.size() < crypto_pwhash_PASSWD_MIN && crypto_pwhash_PASSWD_MAX < pass.size())
MONERO_THROW(error::crypto_failure, "Invalid password size");
if (crypto_pwhash(out.data(), out.size(), pass.data(), pass.size(), pass_salt_.data(),
crypto_pwhash_OPSLIMIT_MIN, crypto_pwhash_MEMLIMIT_MIN, crypto_pwhash_ALG_DEFAULT) != 0)
MONERO_THROW(error::crypto_failure, "Failed password hashing");
}
void server::start_acceptor(const std::shared_ptr<server>& self, const std::string& address, std::string pass)
{
if (!self)
MONERO_THROW(common_error::kInvalidArgument, "nullptr self");
if (address.empty())
return;
auto endpoint = get_endpoint(address);
self->strand_.dispatch([self, endpoint = std::move(endpoint), pass = std::move(pass)] ()
{
self->acceptor_.close();
self->acceptor_.open(endpoint.protocol());
self->acceptor_.bind(endpoint);
self->acceptor_.listen();
MINFO("Listening at " << endpoint << " for scanner clients");
self->compute_hash(self->pass_hashed_, pass);
acceptor{std::move(self)}();
});
}
void server::start_user_checking(const std::shared_ptr<server>& self)
{
if (!self)
MONERO_THROW(common_error::kInvalidArgument, "nullptr self");
self->strand_.dispatch(check_users{self});
}
void server::replace_users(const std::shared_ptr<server>& self)
{
if (!self)
MONERO_THROW(common_error::kInvalidArgument, "nullptr self");
self->strand_.dispatch([self] () { self->do_replace_users(); });
}
void server::store(const std::shared_ptr<server>& self, std::vector<lws::account> users, std::vector<crypto::hash> blocks)
{
if (!self)
MONERO_THROW(common_error::kInvalidArgument, "nullptr self");
std::sort(users.begin(), users.end(), by_height{});
self->strand_.dispatch([self, users = std::move(users), blocks = std::move(blocks)] ()
{
const lws::scanner_options opts{self->webhook_verify_, false, false};
if (!lws::user_data::store(self->disk_, self->zclient_, epee::to_span(blocks), epee::to_span(users), nullptr, opts))
GET_IO_SERVICE(self->check_timer_).stop();
});
}
}}} // lws // rpc // scanner

110
src/rpc/scanner/server.h Normal file
View File

@@ -0,0 +1,110 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <array>
#include <boost/asio/io_service.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/steady_timer.hpp>
#include <memory>
#include <set>
#include <sodium/crypto_pwhash.h>
#include <string>
#include "db/fwd.h"
#include "db/storage.h"
#include "net/net_ssl.h" // monero/contrib/epee/include
#include "rpc/client.h"
#include "rpc/scanner/queue.h"
namespace lws { namespace rpc { namespace scanner
{
//! Checking frequency for local user db changes
constexpr const std::chrono::seconds account_poll_interval{10};
using ssl_verification_t = epee::net_utils::ssl_verification_t;
struct server_connection;
/*!
\brief Manages local and remote scanning for the primary daemon.
\note HTTP and ZMQ were not used because a two-way messaging system were
needed (basically a REST server on either end). */
class server
{
boost::asio::io_service::strand strand_;
boost::asio::steady_timer check_timer_;
boost::asio::ip::tcp::acceptor acceptor_;
std::set<std::weak_ptr<server_connection>, std::owner_less<std::weak_ptr<server_connection>>> remote_;
std::vector<std::shared_ptr<queue>> local_;
std::vector<db::account_id> active_;
db::storage disk_;
rpc::client zclient_;
lmdb::suspended_txn read_txn_;
db::cursor::accounts accounts_cur_;
std::size_t next_thread_;
std::array<unsigned char, 32> pass_hashed_;
std::array<unsigned char, crypto_pwhash_SALTBYTES> pass_salt_;
const ssl_verification_t webhook_verify_;
//! Async acceptor routine
class acceptor;
struct check_users;
//! Reset `local_` and `remote_` scanners. Must be called in `strand_`.
void do_replace_users();
public:
static boost::asio::ip::tcp::endpoint get_endpoint(const std::string& address);
explicit server(boost::asio::io_service& io, db::storage disk, rpc::client zclient, std::vector<std::shared_ptr<queue>> local, std::vector<db::account_id> active, ssl_verification_t webhook_verify);
server(const server&) = delete;
server(server&&) = delete;
~server() noexcept;
server& operator=(const server&) = delete;
server& operator=(server&&) = delete;
//! \return True if `pass` matches expected
bool check_pass(const std::string& pass) const noexcept;
void compute_hash(std::array<unsigned char, 32>& out, const std::string& pass) const noexcept;
//! Start listening for incoming connections on `address`.
static void start_acceptor(const std::shared_ptr<server>& self, const std::string& address, std::string pass);
//! Start timed checks of local DB for change in user state
static void start_user_checking(const std::shared_ptr<server>& self);
//! Replace users/accounts on all local and remote threads
static void replace_users(const std::shared_ptr<server>& self);
//! Update `users` information on local DB
static void store(const std::shared_ptr<server>& self, std::vector<lws::account> users, std::vector<crypto::hash> blocks);
};
}}} // lws // rpc // scanner

View File

@@ -0,0 +1,55 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "write_commands.h"
#include <cstring>
#include <limits>
namespace lws { namespace rpc { namespace scanner
{
epee::byte_slice complete_command(const std::uint8_t id, epee::byte_stream sink)
{
if (sink.size() < sizeof(header))
{
MERROR("Message sink was unexpectedly shrunk on message");
return nullptr;
}
using value_type = header::length_type::value_type;
if (std::numeric_limits<value_type>::max() < sink.size() - sizeof(header))
{
MERROR("Message to exceeds max size");
return nullptr;
}
const header head{0, id, header::length_type{value_type(sink.size() - sizeof(header))}};
std::memcpy(sink.data(), std::addressof(head), sizeof(head));
return epee::byte_slice{std::move(sink)};
}
}}} // lws // rpc // scanner

View File

@@ -0,0 +1,209 @@
// Copyright (c) 2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <boost/asio/coroutine.hpp>
#include <boost/asio/write.hpp>
#include <chrono>
#include <memory>
#include <system_error>
#include <type_traits>
#include <vector>
#include "byte_slice.h" // monero/contrib/epee/include
#include "byte_stream.h" // monero/contrib/epee/include
#include "commands.h"
#include "common/expect.h"// monero/src
#include "crypto/hash.h" // monero/src
#include "db/account.h"
#include "misc_log_ex.h"
#include "rpc/scanner/commands.h"
#include "rpc/scanner/connection.h"
#include "wire/msgpack/write.h"
namespace lws { namespace rpc { namespace scanner
{
constexpr const std::size_t max_write_buffers = 100;
/* \brief ASIO handler for write timeouts
\tparam T concept requirements:
* Must be derived from `lws::rpc::scanner::connection`.
* Must have `cleanup()` function that invokes `base_cleanup()`, and
does any other necessary work given that the socket connection is being
terminated. */
template<typename T>
struct timeout
{
static_assert(std::is_base_of<connection, T>{});
std::shared_ptr<T> self_;
void operator()(const boost::system::error_code& error) const
{
if (self_ && error != boost::asio::error::operation_aborted)
{
assert(self_->strand_.running_in_this_thread());
MERROR("Write timeout on socket (" << self_->remote_endpoint() << ")");
self_->cleanup();
}
}
};
/*! \brief ASIO coroutine for write client OR server commands.
\tparam T concept requirements:
* Must be derived from `lws::rpc::scanner::connection`.
* Must have `cleanup()` function that invokes `base_cleanup()`, and
does any other necessary work given that the socket connection is being
terminated. */
template<typename T>
class write_buffers : public boost::asio::coroutine
{
static_assert(std::is_base_of<connection, T>{});
std::shared_ptr<T> self_;
public:
explicit write_buffers(std::shared_ptr<T> self)
: boost::asio::coroutine(), self_(std::move(self))
{}
write_buffers(write_buffers&&) = default;
write_buffers(const write_buffers&) = default;
void operator()(const boost::system::error_code& error = {}, std::size_t = 0)
{
if (!self_)
return;
assert(self_->strand_.running_in_this_thread());
if (error)
{
if (error != boost::asio::error::operation_aborted)
{
MERROR("Write error on socket (" << self_->remote_endpoint() << "): " << error.message());
self_->cleanup();
}
self_->write_timeout_.cancel();
return;
}
if (self_->cleanup_)
return; // callback queued before cancellation
BOOST_ASIO_CORO_REENTER(*this)
{
while (!self_->write_bufs_.empty())
{
self_->write_timeout_.expires_from_now(std::chrono::seconds{10});
self_->write_timeout_.async_wait(self_->strand_.wrap(timeout<T>{self_}));
BOOST_ASIO_CORO_YIELD boost::asio::async_write(self_->sock_, self_->write_buffer(), self_->strand_.wrap(*this));
self_->write_timeout_.cancel();
self_->write_bufs_.pop_front();
}
}
}
};
//! \return Completed message using `sink` as source.
epee::byte_slice complete_command(std::uint8_t id, epee::byte_stream sink);
/*! Writes "raw" `header` then `data` as msgpack, and queues for writing to
`self`. Also starts ASIO async writing (via `write_buffers`) if the queue
was empty before queueing `data`.
\tparam T must meet concept requirements for `T` outlined in
`write_commands`.
\tparam U concept requirements:
* must be serializable to msgpack using `wire` engine.
* must have static function `id` which returns an `std::uint8_t` to
identify the command on the remote side. */
template<typename T, typename U>
void write_command(const std::shared_ptr<T>& self, const U& data)
{
static_assert(std::is_base_of<connection, T>{});
if (!self)
MONERO_THROW(common_error::kInvalidArgument, "nullptr self");
epee::byte_slice msg = nullptr;
try
{
epee::byte_stream sink{};
sink.put_n(0, sizeof(header));
// use integer keys for speed (default to_bytes uses strings)
wire::msgpack_slice_writer dest{std::move(sink), true};
wire_write::bytes(dest, data);
msg = complete_command(U::id(), dest.take_sink());
}
catch (const wire::exception& e)
{
MERROR("Failed to serialize msgpack for remote (" << self.get() << ") command: " << e.what());
throw; // this should rarely happen, so just shutdown
}
if (msg.empty())
{
self->cleanup();
return;
}
class queue_slice
{
std::shared_ptr<T> self_;
epee::byte_slice msg_;
public:
explicit queue_slice(std::shared_ptr<T> self, epee::byte_slice msg)
: self_(std::move(self)), msg_(std::move(msg))
{}
queue_slice(queue_slice&&) = default;
queue_slice(const queue_slice& rhs)
: self_(rhs.self_), msg_(rhs.msg_.clone())
{}
void operator()()
{
if (!self_)
return;
const bool queue = self_->write_bufs_.empty();
self_->write_bufs_.push_back(std::move(msg_));
if (queue)
write_buffers{self_}();
else if (max_write_buffers <= self_->write_bufs_.size())
{
MERROR("Exceeded max buffer size for connection: " << self_->remote_endpoint());
self_->cleanup();
}
}
};
self->strand_.dispatch(queue_slice{self, std::move(msg)});
}
}}} // lws // rpc // scanner