oxen-core/src/daemon/daemon.cpp

408 lines
16 KiB
C++
Raw Normal View History

daemon & daemonize overhaul This commit continues the complete replacement of the spaghetti code mess that was inside daemon/ and daemonize/ which started in #1138, and looked like a entry level Java programmer threw up inside the code base. This greatly simplifies it, removing a whole pile of useless abstraction layers that don't actually abstract anything, and results in considerably simpler code. (Many of the changes here were also carried out in #1138; this commit updates them with the merged result which amends some things from that PR and goes further in some places). In detail: - the `--detach` (and related `--pidfile`) options are gone. (--detach is still handled, but now just prints a fatal error). Detaching a process is an archaic unix mechanism that has no place on a modern system. If you *really* want to do it anyway, `nohup lokid &` will do the job. (The Windows service control code, which is probably seldom used, is kept because it seems potentially useful for Windows users). - Many of the `t_whatever` classes in daemon/* are just deleted (mostly done in #1138); each one was a bunch of junk code that wraps 3-4 lines but forces an extra layer (not even a generic abstraction, just a useless wrapper) for no good reason and made the daemon code painfully hard to understand and work with. - All of the remaining `t_whatever` classes in daemon/* are either renamed to `whatever` (because prefixing every class with `t_` is moronic). - Some stupid related code (e.g. epee's command handler returning an unsuitable "usage" string that has to be string modified into what we want) was replaced with more generic, useful code. - Replaced boost mutexes/cvs with std ones in epee command handler, and deleted some commented out code. - The `--public-node` option handling was terrible: it was being handled in main, but main doesn't know anything about options, so then it forced it through the spaghetti objects *beside* the pack of all options that got passed along. Moved it to a more sane location (core_rpc_server) and parse it out with some sanity. - Changed a bunch of std::bind's to lambdas because, at least for small lambdas (i.e. with only one-or-two pointers for captures) they will generally be more efficient as the values can be stored in std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
// Copyright (c) 2018-2020, The Loki Project
// Copyright (c) 2014-2019, The Monero Project
2023-04-13 15:50:13 +02:00
//
2014-07-23 15:03:52 +02:00
// All rights reserved.
//
2014-07-23 15:03:52 +02:00
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
2014-07-23 15:03:52 +02:00
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
2014-07-23 15:03:52 +02:00
// 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.
//
2014-07-23 15:03:52 +02:00
// 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.
//
2014-07-23 15:03:52 +02:00
// 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.
//
2014-07-23 15:03:52 +02:00
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
2014-03-03 23:07:58 +01:00
2023-04-13 15:50:13 +02:00
#include <fmt/color.h>
#include <oxenmq/oxenmq.h>
#include <boost/asio/ip/address.hpp>
#include <memory>
#include <stdexcept>
Replace epee http rpc server with uWebSockets This replaces the NIH epee http server which does not work all that well with an external C++ library called uWebSockets. Fundamentally this gives the following advantages: - Much less code to maintain - Just one thread for handling HTTP connections versus epee's pool of threads - Uses existing LokiMQ job server and existing thread pool for handling the actual tasks; they are processed/scheduled in the same "rpc" or "admin" queues as lokimq rpc calls. One notable benefit is that "admin" rpc commands get their own queue (and thus cannot be delayed by long rpc commands). Currently the lokimq threads and the http rpc thread pool and the p2p thread pool and the job queue thread pool and the dns lookup thread pool and... are *all* different thread pools; this is a step towards consolidating them. - Very little mutex contention (which has been a major problem with epee RPC in the past): there is one mutex (inside uWebSockets) for putting responses back into the thread managing the connection; everything internally gets handled through (lock-free) lokimq inproc sockets. - Faster RPC performance on average, and much better worst case performance. Epee's http interface seems to have some race condition that ocassionally stalls a request (even a very simple one) for a dozen or more seconds for no good reason. - Long polling gets redone here to no longer need threads; instead we just store the request and respond when the thread pool, or else in a timer (that runs once/second) for timing out long polls. --- The basic idea of how this works from a high level: We launch a single thread to handle HTTP RPC requests and response data. This uWebSockets thread is essentially running an event loop: it never actually handles any logic; it only serves to shuttle data that arrives in a request to some other thread, and then, at some later point, to send some reply back to that waiting connection. Everything is asynchronous and non-blocking here: the basic uWebSockets event loop just operates as things arrive, passes it off immediately, and goes back to waiting for the next thing to arrive. The basic flow is like this: 0. uWS thread -- listens on localhost:22023 1. uWS thread -- incoming request on localhost:22023 2. uWS thread -- fires callback, which injects the task into the LokiMQ job queue 3. LMQ main loop -- schedules it as an RPC job 4. LMQ rpc thread -- Some LokiMQ thread runs it, gets the result 5. LMQ rpc thread -- Result gets queued up for the uWS thread 6. uWS thread -- takes the request and starts sending it (asynchronously) back to the requestor. In more detail: uWebSockets has registered has registered handlers for non-jsonrpc requests (legacy JSON or binary). If the port is restricted then admin commands get mapped to a "Access denied" response handler, otherwise public commands (and admin commands on an unrestricted port) go to the rpc command handler. POST requests to /json_rpc have their own handler; this is a little different than the above because it has to parse the request before it can determine whether it is allowed or not, but once this is done it continues roughly the same as legacy/binary requests. uWebSockets then listens on the given IP/port for new incoming requests, and starts listening for requests in a thread (we own this thread). When a request arrives, it fires the event handler for that request. (This may happen multiple times, if the client is sending a bunch of data in a POST request). Once we have the full request, we then queue the job in LokiMQ, putting it in the "rpc" or "admin" command categories. (The one practical different here is that "admin" is configured to be allowed to start up its own thread if all other threads are busy, while "rpc" commands are prioritized along with everything else.) LokiMQ then schedules this, along with native LokiMQ "rpc." or "admin." requests. When a LMQ worker thread becomes available, the RPC command gets called in it and runs. Whatever output it produces (or error message, if it throws) then gets wrapped up in jsonrpc boilerplate (if necessary), and delivered to the uWebSockets thread to be sent in reply to that request. uWebSockets picks up the data and sends whatever it can without blocking, then buffers whatever it couldn't send to be sent again in a later event loop iteration once the requestor can accept more data. (This part is outside lokid; we only have to give uWS the data and let it worry about delivery). --- PR specifics: Things removed from this PR: 1. ssl settings; with this PR the HTTP RPC interface is plain-text. The previous default generated a self-signed certificate for the server on startup and then the client accepted any certificate. This is actually *worse* than unencrypted because it is entirely MITM-readable and yet might make people think that their RPC communication is encrypted, and setting up actual certificates is difficult enough that I think most people don't bother. uWebSockets *does* support HTTPS, and we could glue the existing options into it, but I'm not convinced it's worthwhile: it works much better to put HTTPS in a front-end proxy holding the certificate that proxies requests to the backend (which can then listen in restricted mode on some localhost port). One reason this is better is that it is much easier to reload and/or restart such a front-end server, while certificate updates with lokid require a full restart. Another reason is that you get an error page instead of a timeout if something is wrong with the backend. Finally we also save having to generate a temporary certificate on *every* lokid invocation. 2. HTTP Digest authentication. Digest authentication is obsolete (and was already obsolete when it got added to Monero). HTTP-Digest was originally an attempt to provide a password authentication mechanism that does not leak the password in transit, but still required that the server know the password. It only has marginal value against replay attacks, and is made entirely obsolete by sending traffic over HTTPS instead. No client out there supports Digest but *not* Basic auth, and so given the limited usefulness it seems pointless to support more than Basic auth for HTTP RPC login. What's worse is that epee's HTTP Digest authentication is a terrible implementation: it uses boost::spirit -- a recursive descent parser meant for building complex language grammars -- just to parse a single HTTP header for Digest auth. This is a big load of crap that should never have been accepted upstream, and that we should get rid of (even if we wanted to support Digest auth it takes less than 100 lines of code to do it when *not* using a recursive descent parser).
2020-06-29 01:23:06 +02:00
#include <utility>
RPC overhaul High-level details: This redesigns the RPC layer to make it much easier to work with, decouples it from an embedded HTTP server, and gets the vast majority of the RPC serialization and dispatch code out of a very commonly included header. There is unfortunately rather a lot of interconnected code here that cannot be easily separated out into separate commits. The full details of what happens here are as follows: Major details: - All of the RPC code is now in a `cryptonote::rpc` namespace; this renames quite a bit to be less verbose: e.g. CORE_RPC_STATUS_OK becomes `rpc::STATUS_OK`, and `cryptonote::COMMAND_RPC_SOME_LONG_NAME` becomes `rpc::SOME_LONG_NAME` (or just SOME_LONG_NAME for code already working in the `rpc` namespace). - `core_rpc_server` is now completely decoupled from providing any request protocol: it is now *just* the core RPC call handler. - The HTTP RPC interface now lives in a new rpc/http_server.h; this code handles listening for HTTP requests and dispatching them to core_rpc_server, then sending the results back to the caller. - There is similarly a rpc/lmq_server.h for LMQ RPC code; more details on this (and other LMQ specifics) below. - RPC implementing code now returns the response object and throws when things go wrong which simplifies much of the rpc error handling. They can throw anything; generic exceptions get logged and a generic "internal error" message gets returned to the caller, but there is also an `rpc_error` class to return an error code and message used by some json-rpc commands. - RPC implementing functions now overload `core_rpc_server::invoke` following the pattern: RPC_BLAH_BLAH::response core_rpc_server::invoke(RPC_BLAH_BLAH::request&& req, rpc_context context); This overloading makes the code vastly simpler: all instantiations are now done with a small amount of generic instantiation code in a single .cpp rather than needing to go to hell and back with a nest of epee macros in a core header. - each RPC endpoint is now defined by the RPC types themselves, including its accessible names and permissions, in core_rpc_server_commands_defs.h: - every RPC structure now has a static `names()` function that returns the names by which the end point is accessible. (The first one is the primary, the others are for deprecated aliases). - RPC command wrappers define their permissions and type by inheriting from special tag classes: - rpc::RPC_COMMAND is a basic, admin-only, JSON command, available via JSON RPC. *All* JSON commands are now available via JSON RPC, instead of the previous mix of some being at /foo and others at /json_rpc. (Ones that were previously at /foo are still there for backwards compatibility; see `rpc::LEGACY` below). - rpc::PUBLIC specifies that the command should be available via a restricted RPC connection. - rpc::BINARY specifies that the command is not JSON, but rather is accessible as /name and takes and returns values in the magic epee binary "portable storage" (lol) data format. - rpc::LEGACY specifies that the command should be available via the non-json-rpc interface at `/name` for backwards compatibility (in addition to the JSON-RPC interface). - some epee serialization got unwrapped and de-templatized so that it can be moved into a .cpp file with just declarations in the .h. (This makes a *huge* difference for core_rpc_server_commands_defs.h and for every compilation unit that includes it which previously had to compile all the serialization code and then throw all by one copy away at link time). This required some new macros so as to not break a ton of places that will use the old way putting everything in the headers; The RPC code uses this as does a few other places; there are comments in contrib/epee/include/serialization/keyvalue_serialization.h as to how to use it. - Detemplatized a bunch of epee/storages code. Most of it should have have been using templates at all (because it can only ever be called with one type!), and now it isn't. This broke some things that didn't properly compile because of missing headers or (in one case) a messed up circular dependency. - Significantly simplified a bunch of over-templatized serialization code. - All RPC serialization definitions is now out of core_rpc_server_commands_defs.h and into a single .cpp file (core_rpc_server_commands_defs.cpp). - core RPC no longer uses the disgusting BEGIN_URI_MAP2/MAP_URI_BLAH_BLAH macros. This was a terrible design that forced slamming tons of code into a common header that didn't need to be there. - epee::struct_init is gone. It was a horrible hack that instiated multiple templates just so the coder could be so lazy and write `some_type var;` instead of properly value initializing with `some_type var{};`. - Removed a bunch of useless crap from epee. In particular, forcing extra template instantiations all over the place in order to nest return objects inside JSON RPC values is no longer needed, as are a bunch of stuff related to the above de-macroization of the code. - get_all_service_nodes, get_service_nodes, and get_n_service_nodes are now combined into a single `get_service_nodes` (with deprecated aliases for the others), which eliminates a fair amount of duplication. The biggest obstacle here was getting the requested fields reference passed through: this is now done by a new ability to stash a context in the serialization object that can be retrieved by a sub-serialized type. LMQ-specifics: - The LokiMQ instance moves into `cryptonote::core` rather than being inside cryptonote_protocol. Currently the instance is used both for qnet and rpc calls (and so needs to be in a common place), but I also intend future PRs to use the batching code for job processing (replacing the current threaded job queue). - rpc/lmq_server.h handles the actual LMQ-request-to-core-RPC glue. Unlike http_server it isn't technically running the whole LMQ stack from here, but the parallel name with http_server seemed appropriate. - All RPC endpoints are supported by LMQ under the same names as defined generically, but prefixed with `rpc.` for public commands and `admin.` for restricted ones. - service node keys are now always available, even when not running in `--service-node` mode: this is because we want the x25519 key for being able to offer CURVE encryption for lmq RPC end-points, and because it doesn't hurt to have them available all the time. In the RPC layer this is now called "get_service_keys" (with "get_service_node_key" as an alias) since they aren't strictly only for service nodes. This also means code needs to check m_service_node, and not m_service_node_keys, to tell if it is running as a service node. (This is also easier to notice because m_service_node_keys got renamed to `m_service_keys`). - Added block and mempool monitoring LMQ RPC endpoints: `sub.block` and `sub.mempool` subscribes the connection for new block and new mempool TX notifications. The latter can notify on just blink txes, or all new mempool txes (but only new ones -- txes dumped from a block don't trigger it). The client gets pushed a [`notify.block`, `height`, `hash`] or [`notify.tx`, `txhash`, `blob`] message when something arrives. Minor details: - rpc::version_t is now a {major,minor} pair. Forcing everyone to pack and unpack a uint32_t was gross. - Changed some macros to constexprs (e.g. CORE_RPC_ERROR_CODE_...). (This immediately revealed a couple of bugs in the RPC code that was assigning CORE_RPC_ERROR_CODE_... to a string, and it worked because the macro allows implicit conversion to a char). - De-templatizing useless templates in epee (i.e. a bunch of templated types that were never invoked with different types) revealed a painful circular dependency between epee and non-epee code for tor_address and i2p_address. This crap is now handled in a suitably named `net/epee_network_address_hack.cpp` hack because it really isn't trivial to extricate this mess. - Removed `epee/include/serialization/serialize_base.h`. Amazingly the code somehow still all works perfectly with this previously vital header removed. - Removed bitrotted, unused epee "crypted_storage" and "gzipped_inmemstorage" code. - Replaced a bunch of epee::misc_utils::auto_scope_leave_caller with LOKI_DEFERs. The epee version involves quite a bit more instantiation and is ugly as sin. Also made the `loki::defer` class invokable for some edge cases that need calling before destruction in particular conditions. - Moved the systemd code around; it makes much more sense to do the systemd started notification as in daemon.cpp as late as possible rather than in core (when we can still have startup failures, e.g. if the RPC layer can't start). - Made the systemd short status string available in the get_info RPC (and no longer require building with systemd). - during startup, print (only) the x25519 when not in SN mode, and continue to print all three when in SN mode. - DRYed out some RPC implementation code (such as set_limit) - Made wallet_rpc stop using a raw m_wallet pointer
2020-04-28 01:25:43 +02:00
#include "cryptonote_config.h"
#include "cryptonote_core/cryptonote_core.h"
#include "epee/misc_log_ex.h"
daemon & daemonize overhaul This commit continues the complete replacement of the spaghetti code mess that was inside daemon/ and daemonize/ which started in #1138, and looked like a entry level Java programmer threw up inside the code base. This greatly simplifies it, removing a whole pile of useless abstraction layers that don't actually abstract anything, and results in considerably simpler code. (Many of the changes here were also carried out in #1138; this commit updates them with the merged result which amends some things from that PR and goes further in some places). In detail: - the `--detach` (and related `--pidfile`) options are gone. (--detach is still handled, but now just prints a fatal error). Detaching a process is an archaic unix mechanism that has no place on a modern system. If you *really* want to do it anyway, `nohup lokid &` will do the job. (The Windows service control code, which is probably seldom used, is kept because it seems potentially useful for Windows users). - Many of the `t_whatever` classes in daemon/* are just deleted (mostly done in #1138); each one was a bunch of junk code that wraps 3-4 lines but forces an extra layer (not even a generic abstraction, just a useless wrapper) for no good reason and made the daemon code painfully hard to understand and work with. - All of the remaining `t_whatever` classes in daemon/* are either renamed to `whatever` (because prefixing every class with `t_` is moronic). - Some stupid related code (e.g. epee's command handler returning an unsuitable "usage" string that has to be string modified into what we want) was replaced with more generic, useful code. - Replaced boost mutexes/cvs with std ones in epee command handler, and deleted some commented out code. - The `--public-node` option handling was terrible: it was being handled in main, but main doesn't know anything about options, so then it forced it through the spaghetti objects *beside* the pack of all options that got passed along. Moved it to a more sane location (core_rpc_server) and parse it out with some sanity. - Changed a bunch of std::bind's to lambdas because, at least for small lambdas (i.e. with only one-or-two pointers for captures) they will generally be more efficient as the values can be stored in std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
#if defined(PER_BLOCK_CHECKPOINT)
#include "blocks/blocks.h"
#endif
2023-04-13 15:50:13 +02:00
#include <functional>
2014-03-03 23:07:58 +01:00
2023-04-13 15:50:13 +02:00
#include "command_line_args.h"
#include "command_server.h"
#include "common/password.h"
#include "common/signal_handler.h"
2023-04-13 15:50:13 +02:00
#include "cryptonote_core/uptime_proof.h"
#include "cryptonote_protocol/quorumnet.h"
#include "daemon.h"
daemon & daemonize overhaul This commit continues the complete replacement of the spaghetti code mess that was inside daemon/ and daemonize/ which started in #1138, and looked like a entry level Java programmer threw up inside the code base. This greatly simplifies it, removing a whole pile of useless abstraction layers that don't actually abstract anything, and results in considerably simpler code. (Many of the changes here were also carried out in #1138; this commit updates them with the merged result which amends some things from that PR and goes further in some places). In detail: - the `--detach` (and related `--pidfile`) options are gone. (--detach is still handled, but now just prints a fatal error). Detaching a process is an archaic unix mechanism that has no place on a modern system. If you *really* want to do it anyway, `nohup lokid &` will do the job. (The Windows service control code, which is probably seldom used, is kept because it seems potentially useful for Windows users). - Many of the `t_whatever` classes in daemon/* are just deleted (mostly done in #1138); each one was a bunch of junk code that wraps 3-4 lines but forces an extra layer (not even a generic abstraction, just a useless wrapper) for no good reason and made the daemon code painfully hard to understand and work with. - All of the remaining `t_whatever` classes in daemon/* are either renamed to `whatever` (because prefixing every class with `t_` is moronic). - Some stupid related code (e.g. epee's command handler returning an unsuitable "usage" string that has to be string modified into what we want) was replaced with more generic, useful code. - Replaced boost mutexes/cvs with std ones in epee command handler, and deleted some commented out code. - The `--public-node` option handling was terrible: it was being handled in main, but main doesn't know anything about options, so then it forced it through the spaghetti objects *beside* the pack of all options that got passed along. Moved it to a more sane location (core_rpc_server) and parse it out with some sanity. - Changed a bunch of std::bind's to lambdas because, at least for small lambdas (i.e. with only one-or-two pointers for captures) they will generally be more efficient as the values can be stored in std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
#include "net/parse.h"
2023-04-13 15:50:13 +02:00
#include "rpc/common/rpc_args.h"
#include "rpc/http_server.h"
#include "rpc/omq_server.h"
2014-03-03 23:07:58 +01:00
#include "version.h"
RPC overhaul High-level details: This redesigns the RPC layer to make it much easier to work with, decouples it from an embedded HTTP server, and gets the vast majority of the RPC serialization and dispatch code out of a very commonly included header. There is unfortunately rather a lot of interconnected code here that cannot be easily separated out into separate commits. The full details of what happens here are as follows: Major details: - All of the RPC code is now in a `cryptonote::rpc` namespace; this renames quite a bit to be less verbose: e.g. CORE_RPC_STATUS_OK becomes `rpc::STATUS_OK`, and `cryptonote::COMMAND_RPC_SOME_LONG_NAME` becomes `rpc::SOME_LONG_NAME` (or just SOME_LONG_NAME for code already working in the `rpc` namespace). - `core_rpc_server` is now completely decoupled from providing any request protocol: it is now *just* the core RPC call handler. - The HTTP RPC interface now lives in a new rpc/http_server.h; this code handles listening for HTTP requests and dispatching them to core_rpc_server, then sending the results back to the caller. - There is similarly a rpc/lmq_server.h for LMQ RPC code; more details on this (and other LMQ specifics) below. - RPC implementing code now returns the response object and throws when things go wrong which simplifies much of the rpc error handling. They can throw anything; generic exceptions get logged and a generic "internal error" message gets returned to the caller, but there is also an `rpc_error` class to return an error code and message used by some json-rpc commands. - RPC implementing functions now overload `core_rpc_server::invoke` following the pattern: RPC_BLAH_BLAH::response core_rpc_server::invoke(RPC_BLAH_BLAH::request&& req, rpc_context context); This overloading makes the code vastly simpler: all instantiations are now done with a small amount of generic instantiation code in a single .cpp rather than needing to go to hell and back with a nest of epee macros in a core header. - each RPC endpoint is now defined by the RPC types themselves, including its accessible names and permissions, in core_rpc_server_commands_defs.h: - every RPC structure now has a static `names()` function that returns the names by which the end point is accessible. (The first one is the primary, the others are for deprecated aliases). - RPC command wrappers define their permissions and type by inheriting from special tag classes: - rpc::RPC_COMMAND is a basic, admin-only, JSON command, available via JSON RPC. *All* JSON commands are now available via JSON RPC, instead of the previous mix of some being at /foo and others at /json_rpc. (Ones that were previously at /foo are still there for backwards compatibility; see `rpc::LEGACY` below). - rpc::PUBLIC specifies that the command should be available via a restricted RPC connection. - rpc::BINARY specifies that the command is not JSON, but rather is accessible as /name and takes and returns values in the magic epee binary "portable storage" (lol) data format. - rpc::LEGACY specifies that the command should be available via the non-json-rpc interface at `/name` for backwards compatibility (in addition to the JSON-RPC interface). - some epee serialization got unwrapped and de-templatized so that it can be moved into a .cpp file with just declarations in the .h. (This makes a *huge* difference for core_rpc_server_commands_defs.h and for every compilation unit that includes it which previously had to compile all the serialization code and then throw all by one copy away at link time). This required some new macros so as to not break a ton of places that will use the old way putting everything in the headers; The RPC code uses this as does a few other places; there are comments in contrib/epee/include/serialization/keyvalue_serialization.h as to how to use it. - Detemplatized a bunch of epee/storages code. Most of it should have have been using templates at all (because it can only ever be called with one type!), and now it isn't. This broke some things that didn't properly compile because of missing headers or (in one case) a messed up circular dependency. - Significantly simplified a bunch of over-templatized serialization code. - All RPC serialization definitions is now out of core_rpc_server_commands_defs.h and into a single .cpp file (core_rpc_server_commands_defs.cpp). - core RPC no longer uses the disgusting BEGIN_URI_MAP2/MAP_URI_BLAH_BLAH macros. This was a terrible design that forced slamming tons of code into a common header that didn't need to be there. - epee::struct_init is gone. It was a horrible hack that instiated multiple templates just so the coder could be so lazy and write `some_type var;` instead of properly value initializing with `some_type var{};`. - Removed a bunch of useless crap from epee. In particular, forcing extra template instantiations all over the place in order to nest return objects inside JSON RPC values is no longer needed, as are a bunch of stuff related to the above de-macroization of the code. - get_all_service_nodes, get_service_nodes, and get_n_service_nodes are now combined into a single `get_service_nodes` (with deprecated aliases for the others), which eliminates a fair amount of duplication. The biggest obstacle here was getting the requested fields reference passed through: this is now done by a new ability to stash a context in the serialization object that can be retrieved by a sub-serialized type. LMQ-specifics: - The LokiMQ instance moves into `cryptonote::core` rather than being inside cryptonote_protocol. Currently the instance is used both for qnet and rpc calls (and so needs to be in a common place), but I also intend future PRs to use the batching code for job processing (replacing the current threaded job queue). - rpc/lmq_server.h handles the actual LMQ-request-to-core-RPC glue. Unlike http_server it isn't technically running the whole LMQ stack from here, but the parallel name with http_server seemed appropriate. - All RPC endpoints are supported by LMQ under the same names as defined generically, but prefixed with `rpc.` for public commands and `admin.` for restricted ones. - service node keys are now always available, even when not running in `--service-node` mode: this is because we want the x25519 key for being able to offer CURVE encryption for lmq RPC end-points, and because it doesn't hurt to have them available all the time. In the RPC layer this is now called "get_service_keys" (with "get_service_node_key" as an alias) since they aren't strictly only for service nodes. This also means code needs to check m_service_node, and not m_service_node_keys, to tell if it is running as a service node. (This is also easier to notice because m_service_node_keys got renamed to `m_service_keys`). - Added block and mempool monitoring LMQ RPC endpoints: `sub.block` and `sub.mempool` subscribes the connection for new block and new mempool TX notifications. The latter can notify on just blink txes, or all new mempool txes (but only new ones -- txes dumped from a block don't trigger it). The client gets pushed a [`notify.block`, `height`, `hash`] or [`notify.tx`, `txhash`, `blob`] message when something arrives. Minor details: - rpc::version_t is now a {major,minor} pair. Forcing everyone to pack and unpack a uint32_t was gross. - Changed some macros to constexprs (e.g. CORE_RPC_ERROR_CODE_...). (This immediately revealed a couple of bugs in the RPC code that was assigning CORE_RPC_ERROR_CODE_... to a string, and it worked because the macro allows implicit conversion to a char). - De-templatizing useless templates in epee (i.e. a bunch of templated types that were never invoked with different types) revealed a painful circular dependency between epee and non-epee code for tor_address and i2p_address. This crap is now handled in a suitably named `net/epee_network_address_hack.cpp` hack because it really isn't trivial to extricate this mess. - Removed `epee/include/serialization/serialize_base.h`. Amazingly the code somehow still all works perfectly with this previously vital header removed. - Removed bitrotted, unused epee "crypted_storage" and "gzipped_inmemstorage" code. - Replaced a bunch of epee::misc_utils::auto_scope_leave_caller with LOKI_DEFERs. The epee version involves quite a bit more instantiation and is ugly as sin. Also made the `loki::defer` class invokable for some edge cases that need calling before destruction in particular conditions. - Moved the systemd code around; it makes much more sense to do the systemd started notification as in daemon.cpp as late as possible rather than in core (when we can still have startup failures, e.g. if the RPC layer can't start). - Made the systemd short status string available in the get_info RPC (and no longer require building with systemd). - during startup, print (only) the x25519 when not in SN mode, and continue to print all three when in SN mode. - DRYed out some RPC implementation code (such as set_limit) - Made wallet_rpc stop using a raw m_wallet pointer
2020-04-28 01:25:43 +02:00
#ifdef ENABLE_SYSTEMD
extern "C" {
2023-04-13 15:50:13 +02:00
#include <systemd/sd-daemon.h>
RPC overhaul High-level details: This redesigns the RPC layer to make it much easier to work with, decouples it from an embedded HTTP server, and gets the vast majority of the RPC serialization and dispatch code out of a very commonly included header. There is unfortunately rather a lot of interconnected code here that cannot be easily separated out into separate commits. The full details of what happens here are as follows: Major details: - All of the RPC code is now in a `cryptonote::rpc` namespace; this renames quite a bit to be less verbose: e.g. CORE_RPC_STATUS_OK becomes `rpc::STATUS_OK`, and `cryptonote::COMMAND_RPC_SOME_LONG_NAME` becomes `rpc::SOME_LONG_NAME` (or just SOME_LONG_NAME for code already working in the `rpc` namespace). - `core_rpc_server` is now completely decoupled from providing any request protocol: it is now *just* the core RPC call handler. - The HTTP RPC interface now lives in a new rpc/http_server.h; this code handles listening for HTTP requests and dispatching them to core_rpc_server, then sending the results back to the caller. - There is similarly a rpc/lmq_server.h for LMQ RPC code; more details on this (and other LMQ specifics) below. - RPC implementing code now returns the response object and throws when things go wrong which simplifies much of the rpc error handling. They can throw anything; generic exceptions get logged and a generic "internal error" message gets returned to the caller, but there is also an `rpc_error` class to return an error code and message used by some json-rpc commands. - RPC implementing functions now overload `core_rpc_server::invoke` following the pattern: RPC_BLAH_BLAH::response core_rpc_server::invoke(RPC_BLAH_BLAH::request&& req, rpc_context context); This overloading makes the code vastly simpler: all instantiations are now done with a small amount of generic instantiation code in a single .cpp rather than needing to go to hell and back with a nest of epee macros in a core header. - each RPC endpoint is now defined by the RPC types themselves, including its accessible names and permissions, in core_rpc_server_commands_defs.h: - every RPC structure now has a static `names()` function that returns the names by which the end point is accessible. (The first one is the primary, the others are for deprecated aliases). - RPC command wrappers define their permissions and type by inheriting from special tag classes: - rpc::RPC_COMMAND is a basic, admin-only, JSON command, available via JSON RPC. *All* JSON commands are now available via JSON RPC, instead of the previous mix of some being at /foo and others at /json_rpc. (Ones that were previously at /foo are still there for backwards compatibility; see `rpc::LEGACY` below). - rpc::PUBLIC specifies that the command should be available via a restricted RPC connection. - rpc::BINARY specifies that the command is not JSON, but rather is accessible as /name and takes and returns values in the magic epee binary "portable storage" (lol) data format. - rpc::LEGACY specifies that the command should be available via the non-json-rpc interface at `/name` for backwards compatibility (in addition to the JSON-RPC interface). - some epee serialization got unwrapped and de-templatized so that it can be moved into a .cpp file with just declarations in the .h. (This makes a *huge* difference for core_rpc_server_commands_defs.h and for every compilation unit that includes it which previously had to compile all the serialization code and then throw all by one copy away at link time). This required some new macros so as to not break a ton of places that will use the old way putting everything in the headers; The RPC code uses this as does a few other places; there are comments in contrib/epee/include/serialization/keyvalue_serialization.h as to how to use it. - Detemplatized a bunch of epee/storages code. Most of it should have have been using templates at all (because it can only ever be called with one type!), and now it isn't. This broke some things that didn't properly compile because of missing headers or (in one case) a messed up circular dependency. - Significantly simplified a bunch of over-templatized serialization code. - All RPC serialization definitions is now out of core_rpc_server_commands_defs.h and into a single .cpp file (core_rpc_server_commands_defs.cpp). - core RPC no longer uses the disgusting BEGIN_URI_MAP2/MAP_URI_BLAH_BLAH macros. This was a terrible design that forced slamming tons of code into a common header that didn't need to be there. - epee::struct_init is gone. It was a horrible hack that instiated multiple templates just so the coder could be so lazy and write `some_type var;` instead of properly value initializing with `some_type var{};`. - Removed a bunch of useless crap from epee. In particular, forcing extra template instantiations all over the place in order to nest return objects inside JSON RPC values is no longer needed, as are a bunch of stuff related to the above de-macroization of the code. - get_all_service_nodes, get_service_nodes, and get_n_service_nodes are now combined into a single `get_service_nodes` (with deprecated aliases for the others), which eliminates a fair amount of duplication. The biggest obstacle here was getting the requested fields reference passed through: this is now done by a new ability to stash a context in the serialization object that can be retrieved by a sub-serialized type. LMQ-specifics: - The LokiMQ instance moves into `cryptonote::core` rather than being inside cryptonote_protocol. Currently the instance is used both for qnet and rpc calls (and so needs to be in a common place), but I also intend future PRs to use the batching code for job processing (replacing the current threaded job queue). - rpc/lmq_server.h handles the actual LMQ-request-to-core-RPC glue. Unlike http_server it isn't technically running the whole LMQ stack from here, but the parallel name with http_server seemed appropriate. - All RPC endpoints are supported by LMQ under the same names as defined generically, but prefixed with `rpc.` for public commands and `admin.` for restricted ones. - service node keys are now always available, even when not running in `--service-node` mode: this is because we want the x25519 key for being able to offer CURVE encryption for lmq RPC end-points, and because it doesn't hurt to have them available all the time. In the RPC layer this is now called "get_service_keys" (with "get_service_node_key" as an alias) since they aren't strictly only for service nodes. This also means code needs to check m_service_node, and not m_service_node_keys, to tell if it is running as a service node. (This is also easier to notice because m_service_node_keys got renamed to `m_service_keys`). - Added block and mempool monitoring LMQ RPC endpoints: `sub.block` and `sub.mempool` subscribes the connection for new block and new mempool TX notifications. The latter can notify on just blink txes, or all new mempool txes (but only new ones -- txes dumped from a block don't trigger it). The client gets pushed a [`notify.block`, `height`, `hash`] or [`notify.tx`, `txhash`, `blob`] message when something arrives. Minor details: - rpc::version_t is now a {major,minor} pair. Forcing everyone to pack and unpack a uint32_t was gross. - Changed some macros to constexprs (e.g. CORE_RPC_ERROR_CODE_...). (This immediately revealed a couple of bugs in the RPC code that was assigning CORE_RPC_ERROR_CODE_... to a string, and it worked because the macro allows implicit conversion to a char). - De-templatizing useless templates in epee (i.e. a bunch of templated types that were never invoked with different types) revealed a painful circular dependency between epee and non-epee code for tor_address and i2p_address. This crap is now handled in a suitably named `net/epee_network_address_hack.cpp` hack because it really isn't trivial to extricate this mess. - Removed `epee/include/serialization/serialize_base.h`. Amazingly the code somehow still all works perfectly with this previously vital header removed. - Removed bitrotted, unused epee "crypted_storage" and "gzipped_inmemstorage" code. - Replaced a bunch of epee::misc_utils::auto_scope_leave_caller with LOKI_DEFERs. The epee version involves quite a bit more instantiation and is ugly as sin. Also made the `loki::defer` class invokable for some edge cases that need calling before destruction in particular conditions. - Moved the systemd code around; it makes much more sense to do the systemd started notification as in daemon.cpp as late as possible rather than in core (when we can still have startup failures, e.g. if the RPC layer can't start). - Made the systemd short status string available in the get_info RPC (and no longer require building with systemd). - during startup, print (only) the x25519 when not in SN mode, and continue to print all three when in SN mode. - DRYed out some RPC implementation code (such as set_limit) - Made wallet_rpc stop using a raw m_wallet pointer
2020-04-28 01:25:43 +02:00
}
#endif
Replace epee http rpc server with uWebSockets This replaces the NIH epee http server which does not work all that well with an external C++ library called uWebSockets. Fundamentally this gives the following advantages: - Much less code to maintain - Just one thread for handling HTTP connections versus epee's pool of threads - Uses existing LokiMQ job server and existing thread pool for handling the actual tasks; they are processed/scheduled in the same "rpc" or "admin" queues as lokimq rpc calls. One notable benefit is that "admin" rpc commands get their own queue (and thus cannot be delayed by long rpc commands). Currently the lokimq threads and the http rpc thread pool and the p2p thread pool and the job queue thread pool and the dns lookup thread pool and... are *all* different thread pools; this is a step towards consolidating them. - Very little mutex contention (which has been a major problem with epee RPC in the past): there is one mutex (inside uWebSockets) for putting responses back into the thread managing the connection; everything internally gets handled through (lock-free) lokimq inproc sockets. - Faster RPC performance on average, and much better worst case performance. Epee's http interface seems to have some race condition that ocassionally stalls a request (even a very simple one) for a dozen or more seconds for no good reason. - Long polling gets redone here to no longer need threads; instead we just store the request and respond when the thread pool, or else in a timer (that runs once/second) for timing out long polls. --- The basic idea of how this works from a high level: We launch a single thread to handle HTTP RPC requests and response data. This uWebSockets thread is essentially running an event loop: it never actually handles any logic; it only serves to shuttle data that arrives in a request to some other thread, and then, at some later point, to send some reply back to that waiting connection. Everything is asynchronous and non-blocking here: the basic uWebSockets event loop just operates as things arrive, passes it off immediately, and goes back to waiting for the next thing to arrive. The basic flow is like this: 0. uWS thread -- listens on localhost:22023 1. uWS thread -- incoming request on localhost:22023 2. uWS thread -- fires callback, which injects the task into the LokiMQ job queue 3. LMQ main loop -- schedules it as an RPC job 4. LMQ rpc thread -- Some LokiMQ thread runs it, gets the result 5. LMQ rpc thread -- Result gets queued up for the uWS thread 6. uWS thread -- takes the request and starts sending it (asynchronously) back to the requestor. In more detail: uWebSockets has registered has registered handlers for non-jsonrpc requests (legacy JSON or binary). If the port is restricted then admin commands get mapped to a "Access denied" response handler, otherwise public commands (and admin commands on an unrestricted port) go to the rpc command handler. POST requests to /json_rpc have their own handler; this is a little different than the above because it has to parse the request before it can determine whether it is allowed or not, but once this is done it continues roughly the same as legacy/binary requests. uWebSockets then listens on the given IP/port for new incoming requests, and starts listening for requests in a thread (we own this thread). When a request arrives, it fires the event handler for that request. (This may happen multiple times, if the client is sending a bunch of data in a POST request). Once we have the full request, we then queue the job in LokiMQ, putting it in the "rpc" or "admin" command categories. (The one practical different here is that "admin" is configured to be allowed to start up its own thread if all other threads are busy, while "rpc" commands are prioritized along with everything else.) LokiMQ then schedules this, along with native LokiMQ "rpc." or "admin." requests. When a LMQ worker thread becomes available, the RPC command gets called in it and runs. Whatever output it produces (or error message, if it throws) then gets wrapped up in jsonrpc boilerplate (if necessary), and delivered to the uWebSockets thread to be sent in reply to that request. uWebSockets picks up the data and sends whatever it can without blocking, then buffers whatever it couldn't send to be sent again in a later event loop iteration once the requestor can accept more data. (This part is outside lokid; we only have to give uWS the data and let it worry about delivery). --- PR specifics: Things removed from this PR: 1. ssl settings; with this PR the HTTP RPC interface is plain-text. The previous default generated a self-signed certificate for the server on startup and then the client accepted any certificate. This is actually *worse* than unencrypted because it is entirely MITM-readable and yet might make people think that their RPC communication is encrypted, and setting up actual certificates is difficult enough that I think most people don't bother. uWebSockets *does* support HTTPS, and we could glue the existing options into it, but I'm not convinced it's worthwhile: it works much better to put HTTPS in a front-end proxy holding the certificate that proxies requests to the backend (which can then listen in restricted mode on some localhost port). One reason this is better is that it is much easier to reload and/or restart such a front-end server, while certificate updates with lokid require a full restart. Another reason is that you get an error page instead of a timeout if something is wrong with the backend. Finally we also save having to generate a temporary certificate on *every* lokid invocation. 2. HTTP Digest authentication. Digest authentication is obsolete (and was already obsolete when it got added to Monero). HTTP-Digest was originally an attempt to provide a password authentication mechanism that does not leak the password in transit, but still required that the server know the password. It only has marginal value against replay attacks, and is made entirely obsolete by sending traffic over HTTPS instead. No client out there supports Digest but *not* Basic auth, and so given the limited usefulness it seems pointless to support more than Basic auth for HTTP RPC login. What's worse is that epee's HTTP Digest authentication is a terrible implementation: it uses boost::spirit -- a recursive descent parser meant for building complex language grammars -- just to parse a single HTTP header for Digest auth. This is a big load of crap that should never have been accepted upstream, and that we should get rid of (even if we wanted to support Digest auth it takes less than 100 lines of code to do it when *not* using a recursive descent parser).
2020-06-29 01:23:06 +02:00
using namespace std::literals;
RPC overhaul High-level details: This redesigns the RPC layer to make it much easier to work with, decouples it from an embedded HTTP server, and gets the vast majority of the RPC serialization and dispatch code out of a very commonly included header. There is unfortunately rather a lot of interconnected code here that cannot be easily separated out into separate commits. The full details of what happens here are as follows: Major details: - All of the RPC code is now in a `cryptonote::rpc` namespace; this renames quite a bit to be less verbose: e.g. CORE_RPC_STATUS_OK becomes `rpc::STATUS_OK`, and `cryptonote::COMMAND_RPC_SOME_LONG_NAME` becomes `rpc::SOME_LONG_NAME` (or just SOME_LONG_NAME for code already working in the `rpc` namespace). - `core_rpc_server` is now completely decoupled from providing any request protocol: it is now *just* the core RPC call handler. - The HTTP RPC interface now lives in a new rpc/http_server.h; this code handles listening for HTTP requests and dispatching them to core_rpc_server, then sending the results back to the caller. - There is similarly a rpc/lmq_server.h for LMQ RPC code; more details on this (and other LMQ specifics) below. - RPC implementing code now returns the response object and throws when things go wrong which simplifies much of the rpc error handling. They can throw anything; generic exceptions get logged and a generic "internal error" message gets returned to the caller, but there is also an `rpc_error` class to return an error code and message used by some json-rpc commands. - RPC implementing functions now overload `core_rpc_server::invoke` following the pattern: RPC_BLAH_BLAH::response core_rpc_server::invoke(RPC_BLAH_BLAH::request&& req, rpc_context context); This overloading makes the code vastly simpler: all instantiations are now done with a small amount of generic instantiation code in a single .cpp rather than needing to go to hell and back with a nest of epee macros in a core header. - each RPC endpoint is now defined by the RPC types themselves, including its accessible names and permissions, in core_rpc_server_commands_defs.h: - every RPC structure now has a static `names()` function that returns the names by which the end point is accessible. (The first one is the primary, the others are for deprecated aliases). - RPC command wrappers define their permissions and type by inheriting from special tag classes: - rpc::RPC_COMMAND is a basic, admin-only, JSON command, available via JSON RPC. *All* JSON commands are now available via JSON RPC, instead of the previous mix of some being at /foo and others at /json_rpc. (Ones that were previously at /foo are still there for backwards compatibility; see `rpc::LEGACY` below). - rpc::PUBLIC specifies that the command should be available via a restricted RPC connection. - rpc::BINARY specifies that the command is not JSON, but rather is accessible as /name and takes and returns values in the magic epee binary "portable storage" (lol) data format. - rpc::LEGACY specifies that the command should be available via the non-json-rpc interface at `/name` for backwards compatibility (in addition to the JSON-RPC interface). - some epee serialization got unwrapped and de-templatized so that it can be moved into a .cpp file with just declarations in the .h. (This makes a *huge* difference for core_rpc_server_commands_defs.h and for every compilation unit that includes it which previously had to compile all the serialization code and then throw all by one copy away at link time). This required some new macros so as to not break a ton of places that will use the old way putting everything in the headers; The RPC code uses this as does a few other places; there are comments in contrib/epee/include/serialization/keyvalue_serialization.h as to how to use it. - Detemplatized a bunch of epee/storages code. Most of it should have have been using templates at all (because it can only ever be called with one type!), and now it isn't. This broke some things that didn't properly compile because of missing headers or (in one case) a messed up circular dependency. - Significantly simplified a bunch of over-templatized serialization code. - All RPC serialization definitions is now out of core_rpc_server_commands_defs.h and into a single .cpp file (core_rpc_server_commands_defs.cpp). - core RPC no longer uses the disgusting BEGIN_URI_MAP2/MAP_URI_BLAH_BLAH macros. This was a terrible design that forced slamming tons of code into a common header that didn't need to be there. - epee::struct_init is gone. It was a horrible hack that instiated multiple templates just so the coder could be so lazy and write `some_type var;` instead of properly value initializing with `some_type var{};`. - Removed a bunch of useless crap from epee. In particular, forcing extra template instantiations all over the place in order to nest return objects inside JSON RPC values is no longer needed, as are a bunch of stuff related to the above de-macroization of the code. - get_all_service_nodes, get_service_nodes, and get_n_service_nodes are now combined into a single `get_service_nodes` (with deprecated aliases for the others), which eliminates a fair amount of duplication. The biggest obstacle here was getting the requested fields reference passed through: this is now done by a new ability to stash a context in the serialization object that can be retrieved by a sub-serialized type. LMQ-specifics: - The LokiMQ instance moves into `cryptonote::core` rather than being inside cryptonote_protocol. Currently the instance is used both for qnet and rpc calls (and so needs to be in a common place), but I also intend future PRs to use the batching code for job processing (replacing the current threaded job queue). - rpc/lmq_server.h handles the actual LMQ-request-to-core-RPC glue. Unlike http_server it isn't technically running the whole LMQ stack from here, but the parallel name with http_server seemed appropriate. - All RPC endpoints are supported by LMQ under the same names as defined generically, but prefixed with `rpc.` for public commands and `admin.` for restricted ones. - service node keys are now always available, even when not running in `--service-node` mode: this is because we want the x25519 key for being able to offer CURVE encryption for lmq RPC end-points, and because it doesn't hurt to have them available all the time. In the RPC layer this is now called "get_service_keys" (with "get_service_node_key" as an alias) since they aren't strictly only for service nodes. This also means code needs to check m_service_node, and not m_service_node_keys, to tell if it is running as a service node. (This is also easier to notice because m_service_node_keys got renamed to `m_service_keys`). - Added block and mempool monitoring LMQ RPC endpoints: `sub.block` and `sub.mempool` subscribes the connection for new block and new mempool TX notifications. The latter can notify on just blink txes, or all new mempool txes (but only new ones -- txes dumped from a block don't trigger it). The client gets pushed a [`notify.block`, `height`, `hash`] or [`notify.tx`, `txhash`, `blob`] message when something arrives. Minor details: - rpc::version_t is now a {major,minor} pair. Forcing everyone to pack and unpack a uint32_t was gross. - Changed some macros to constexprs (e.g. CORE_RPC_ERROR_CODE_...). (This immediately revealed a couple of bugs in the RPC code that was assigning CORE_RPC_ERROR_CODE_... to a string, and it worked because the macro allows implicit conversion to a char). - De-templatizing useless templates in epee (i.e. a bunch of templated types that were never invoked with different types) revealed a painful circular dependency between epee and non-epee code for tor_address and i2p_address. This crap is now handled in a suitably named `net/epee_network_address_hack.cpp` hack because it really isn't trivial to extricate this mess. - Removed `epee/include/serialization/serialize_base.h`. Amazingly the code somehow still all works perfectly with this previously vital header removed. - Removed bitrotted, unused epee "crypted_storage" and "gzipped_inmemstorage" code. - Replaced a bunch of epee::misc_utils::auto_scope_leave_caller with LOKI_DEFERs. The epee version involves quite a bit more instantiation and is ugly as sin. Also made the `loki::defer` class invokable for some edge cases that need calling before destruction in particular conditions. - Moved the systemd code around; it makes much more sense to do the systemd started notification as in daemon.cpp as late as possible rather than in core (when we can still have startup failures, e.g. if the RPC layer can't start). - Made the systemd short status string available in the get_info RPC (and no longer require building with systemd). - during startup, print (only) the x25519 when not in SN mode, and continue to print all three when in SN mode. - DRYed out some RPC implementation code (such as set_limit) - Made wallet_rpc stop using a raw m_wallet pointer
2020-04-28 01:25:43 +02:00
namespace daemonize {
namespace log = oxen::log;
static auto logcat = log::Cat("daemon");
2023-04-13 15:50:13 +02:00
std::pair<std::string, uint16_t> parse_ip_port(
std::string_view ip_port, const std::string& argname) {
std::pair<std::string, uint16_t> result;
auto& [ip, port] = result;
if (auto colon = ip_port.rfind(":");
colon != std::string::npos && tools::parse_int(ip_port.substr(colon + 1), port))
ip_port.remove_suffix(ip_port.size() - colon);
else
throw std::runtime_error{
"Invalid IP/port value specified to " + argname + ": " + std::string(ip_port)};
if (!ip_port.empty() && ip_port.front() == '[' && ip_port.back() == ']') {
ip_port.remove_prefix(1);
ip_port.remove_suffix(1);
}
daemon & daemonize overhaul This commit continues the complete replacement of the spaghetti code mess that was inside daemon/ and daemonize/ which started in #1138, and looked like a entry level Java programmer threw up inside the code base. This greatly simplifies it, removing a whole pile of useless abstraction layers that don't actually abstract anything, and results in considerably simpler code. (Many of the changes here were also carried out in #1138; this commit updates them with the merged result which amends some things from that PR and goes further in some places). In detail: - the `--detach` (and related `--pidfile`) options are gone. (--detach is still handled, but now just prints a fatal error). Detaching a process is an archaic unix mechanism that has no place on a modern system. If you *really* want to do it anyway, `nohup lokid &` will do the job. (The Windows service control code, which is probably seldom used, is kept because it seems potentially useful for Windows users). - Many of the `t_whatever` classes in daemon/* are just deleted (mostly done in #1138); each one was a bunch of junk code that wraps 3-4 lines but forces an extra layer (not even a generic abstraction, just a useless wrapper) for no good reason and made the daemon code painfully hard to understand and work with. - All of the remaining `t_whatever` classes in daemon/* are either renamed to `whatever` (because prefixing every class with `t_` is moronic). - Some stupid related code (e.g. epee's command handler returning an unsuitable "usage" string that has to be string modified into what we want) was replaced with more generic, useful code. - Replaced boost mutexes/cvs with std ones in epee command handler, and deleted some commented out code. - The `--public-node` option handling was terrible: it was being handled in main, but main doesn't know anything about options, so then it forced it through the spaghetti objects *beside* the pack of all options that got passed along. Moved it to a more sane location (core_rpc_server) and parse it out with some sanity. - Changed a bunch of std::bind's to lambdas because, at least for small lambdas (i.e. with only one-or-two pointers for captures) they will generally be more efficient as the values can be stored in std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
2023-04-13 15:50:13 +02:00
std::string ip_str{ip_port};
boost::system::error_code ec;
auto addr =
#if BOOST_VERSION >= 106600
2023-04-13 15:50:13 +02:00
boost::asio::ip::make_address
#else
2023-04-13 15:50:13 +02:00
boost::asio::ip::address::from_string
#endif
2023-04-13 15:50:13 +02:00
(ip_str, ec);
if (ec)
throw std::runtime_error{"Invalid IP address specified: " + ip_str};
daemon & daemonize overhaul This commit continues the complete replacement of the spaghetti code mess that was inside daemon/ and daemonize/ which started in #1138, and looked like a entry level Java programmer threw up inside the code base. This greatly simplifies it, removing a whole pile of useless abstraction layers that don't actually abstract anything, and results in considerably simpler code. (Many of the changes here were also carried out in #1138; this commit updates them with the merged result which amends some things from that PR and goes further in some places). In detail: - the `--detach` (and related `--pidfile`) options are gone. (--detach is still handled, but now just prints a fatal error). Detaching a process is an archaic unix mechanism that has no place on a modern system. If you *really* want to do it anyway, `nohup lokid &` will do the job. (The Windows service control code, which is probably seldom used, is kept because it seems potentially useful for Windows users). - Many of the `t_whatever` classes in daemon/* are just deleted (mostly done in #1138); each one was a bunch of junk code that wraps 3-4 lines but forces an extra layer (not even a generic abstraction, just a useless wrapper) for no good reason and made the daemon code painfully hard to understand and work with. - All of the remaining `t_whatever` classes in daemon/* are either renamed to `whatever` (because prefixing every class with `t_` is moronic). - Some stupid related code (e.g. epee's command handler returning an unsuitable "usage" string that has to be string modified into what we want) was replaced with more generic, useful code. - Replaced boost mutexes/cvs with std ones in epee command handler, and deleted some commented out code. - The `--public-node` option handling was terrible: it was being handled in main, but main doesn't know anything about options, so then it forced it through the spaghetti objects *beside* the pack of all options that got passed along. Moved it to a more sane location (core_rpc_server) and parse it out with some sanity. - Changed a bunch of std::bind's to lambdas because, at least for small lambdas (i.e. with only one-or-two pointers for captures) they will generally be more efficient as the values can be stored in std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
2023-04-13 15:50:13 +02:00
ip = addr.to_string();
daemon & daemonize overhaul This commit continues the complete replacement of the spaghetti code mess that was inside daemon/ and daemonize/ which started in #1138, and looked like a entry level Java programmer threw up inside the code base. This greatly simplifies it, removing a whole pile of useless abstraction layers that don't actually abstract anything, and results in considerably simpler code. (Many of the changes here were also carried out in #1138; this commit updates them with the merged result which amends some things from that PR and goes further in some places). In detail: - the `--detach` (and related `--pidfile`) options are gone. (--detach is still handled, but now just prints a fatal error). Detaching a process is an archaic unix mechanism that has no place on a modern system. If you *really* want to do it anyway, `nohup lokid &` will do the job. (The Windows service control code, which is probably seldom used, is kept because it seems potentially useful for Windows users). - Many of the `t_whatever` classes in daemon/* are just deleted (mostly done in #1138); each one was a bunch of junk code that wraps 3-4 lines but forces an extra layer (not even a generic abstraction, just a useless wrapper) for no good reason and made the daemon code painfully hard to understand and work with. - All of the remaining `t_whatever` classes in daemon/* are either renamed to `whatever` (because prefixing every class with `t_` is moronic). - Some stupid related code (e.g. epee's command handler returning an unsuitable "usage" string that has to be string modified into what we want) was replaced with more generic, useful code. - Replaced boost mutexes/cvs with std ones in epee command handler, and deleted some commented out code. - The `--public-node` option handling was terrible: it was being handled in main, but main doesn't know anything about options, so then it forced it through the spaghetti objects *beside* the pack of all options that got passed along. Moved it to a more sane location (core_rpc_server) and parse it out with some sanity. - Changed a bunch of std::bind's to lambdas because, at least for small lambdas (i.e. with only one-or-two pointers for captures) they will generally be more efficient as the values can be stored in std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
2023-04-13 15:50:13 +02:00
return result;
}
daemon & daemonize overhaul This commit continues the complete replacement of the spaghetti code mess that was inside daemon/ and daemonize/ which started in #1138, and looked like a entry level Java programmer threw up inside the code base. This greatly simplifies it, removing a whole pile of useless abstraction layers that don't actually abstract anything, and results in considerably simpler code. (Many of the changes here were also carried out in #1138; this commit updates them with the merged result which amends some things from that PR and goes further in some places). In detail: - the `--detach` (and related `--pidfile`) options are gone. (--detach is still handled, but now just prints a fatal error). Detaching a process is an archaic unix mechanism that has no place on a modern system. If you *really* want to do it anyway, `nohup lokid &` will do the job. (The Windows service control code, which is probably seldom used, is kept because it seems potentially useful for Windows users). - Many of the `t_whatever` classes in daemon/* are just deleted (mostly done in #1138); each one was a bunch of junk code that wraps 3-4 lines but forces an extra layer (not even a generic abstraction, just a useless wrapper) for no good reason and made the daemon code painfully hard to understand and work with. - All of the remaining `t_whatever` classes in daemon/* are either renamed to `whatever` (because prefixing every class with `t_` is moronic). - Some stupid related code (e.g. epee's command handler returning an unsuitable "usage" string that has to be string modified into what we want) was replaced with more generic, useful code. - Replaced boost mutexes/cvs with std ones in epee command handler, and deleted some commented out code. - The `--public-node` option handling was terrible: it was being handled in main, but main doesn't know anything about options, so then it forced it through the spaghetti objects *beside* the pack of all options that got passed along. Moved it to a more sane location (core_rpc_server) and parse it out with some sanity. - Changed a bunch of std::bind's to lambdas because, at least for small lambdas (i.e. with only one-or-two pointers for captures) they will generally be more efficient as the values can be stored in std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
daemon::daemon(boost::program_options::variables_map vm_) :
2023-04-13 15:50:13 +02:00
vm{std::move(vm_)},
core{std::make_unique<cryptonote::core>()},
protocol{std::make_unique<protocol_handler>(
*core, command_line::get_arg(vm, cryptonote::arg_offline))},
p2p{std::make_unique<node_server>(*protocol)},
rpc{std::make_unique<cryptonote::rpc::core_rpc_server>(*core, *p2p)} {
log::info(logcat, fg(fmt::terminal_color::blue), "Initializing daemon objects...");
log::info(logcat, "- cryptonote protocol");
if (!protocol->init(vm))
throw std::runtime_error("Failed to initialize cryptonote protocol.");
log::info(logcat, "- p2p");
if (!p2p->init(vm))
throw std::runtime_error("Failed to initialize p2p server.");
// Handle circular dependencies
protocol->set_p2p_endpoint(p2p.get());
core->set_cryptonote_protocol(protocol.get());
auto rpc_config = cryptonote::rpc_args::process(vm);
bool new_rpc_options = !is_arg_defaulted(vm, cryptonote::rpc::http_server::arg_rpc_admin) ||
!is_arg_defaulted(vm, cryptonote::rpc::http_server::arg_rpc_public);
// TODO: Remove these options, perhaps starting in oxen 9.0
bool deprecated_rpc_options =
!is_arg_defaulted(vm, cryptonote::rpc::http_server::arg_rpc_bind_port) ||
!is_arg_defaulted(vm, cryptonote::rpc::http_server::arg_rpc_restricted_bind_port) ||
!is_arg_defaulted(vm, cryptonote::rpc::http_server::arg_restricted_rpc) ||
!is_arg_defaulted(vm, cryptonote::rpc::http_server::arg_public_node) ||
rpc_config.bind_ip.has_value() || rpc_config.bind_ipv6_address.has_value() ||
rpc_config.use_ipv6;
constexpr std::string_view deprecated_option_names =
"--rpc-bind-ip/--rpc-bind-port/--rpc-restricted-bind-port/--restricted-rpc/--public-node/--rpc-use-ipv6"sv;
if (new_rpc_options && deprecated_rpc_options)
throw std::runtime_error{
"Failed to initialize rpc settings: --rpc-public/--rpc-admin cannot be combined "
"with deprecated " +
std::string{deprecated_option_names} + " options"};
// bind ip, listen addr, required
std::vector<std::tuple<std::string, uint16_t, bool>> rpc_listen_admin, rpc_listen_public;
if (deprecated_rpc_options) {
log::info(
logcat,
fg(fmt::terminal_color::red),
"{} options are deprecated and will be removed from a future oxend version; use "
"--rpc-public/--rpc-admin instead",
deprecated_option_names);
// These old options from Monero are really janky: --restricted-rpc turns the main port
// restricted, but then we also have --rpc-restricted-bind-port but both are stuck with
// --rpc-bind-ip, and then half of the options get parsed here but the IP option used to get
// parsed in the http_server code.
auto restricted =
command_line::get_arg(vm, cryptonote::rpc::http_server::arg_restricted_rpc);
auto main_rpc_port =
command_line::get_arg(vm, cryptonote::rpc::http_server::arg_rpc_bind_port);
auto restricted_rpc_port = command_line::get_arg(
vm, cryptonote::rpc::http_server::arg_rpc_restricted_bind_port);
if (main_rpc_port == 0) {
if (restricted && restricted_rpc_port != 0)
std::swap(main_rpc_port, restricted_rpc_port);
else if (command_line::get_arg(vm, cryptonote::arg_testnet_on))
main_rpc_port = cryptonote::config::testnet::RPC_DEFAULT_PORT;
else if (command_line::get_arg(vm, cryptonote::arg_devnet_on))
main_rpc_port = cryptonote::config::devnet::RPC_DEFAULT_PORT;
else
main_rpc_port = cryptonote::config::RPC_DEFAULT_PORT;
}
if (main_rpc_port && main_rpc_port == restricted_rpc_port)
restricted = true;
std::vector<uint16_t> public_ports;
if (restricted)
public_ports.push_back(main_rpc_port);
if (restricted_rpc_port && restricted_rpc_port != main_rpc_port)
public_ports.push_back(restricted_rpc_port);
for (uint16_t port : public_ports) {
rpc_listen_public.emplace_back(
rpc_config.bind_ip.value_or("127.0.0.1"),
main_rpc_port,
rpc_config.require_ipv4);
if (rpc_config.bind_ipv6_address || rpc_config.use_ipv6)
rpc_listen_public.emplace_back(
rpc_config.bind_ipv6_address.value_or("::1"), main_rpc_port, true);
}
if (!restricted && main_rpc_port) {
rpc_listen_admin.emplace_back(
rpc_config.bind_ip.value_or("127.0.0.1"),
main_rpc_port,
rpc_config.require_ipv4);
if (rpc_config.bind_ipv6_address || rpc_config.use_ipv6)
rpc_listen_public.emplace_back(
rpc_config.bind_ipv6_address.value_or("::1"), main_rpc_port, true);
}
} else { // no deprecated options
for (auto& bind : command_line::get_arg(vm, cryptonote::rpc::http_server::arg_rpc_admin)) {
if (bind == "none")
continue;
auto [ip, port] = parse_ip_port(bind, "--rpc-admin");
bool ipv4 = ip.find(':') == std::string::npos;
// If using the default admin setting then don't require the bind to IPv6 localhost, or
// the IPv4 localhost bind if --rpc-ignore-ipv4 is given.
bool required = !command_line::is_arg_defaulted(
vm, cryptonote::rpc::http_server::arg_rpc_admin) ||
(ipv4 && rpc_config.require_ipv4);
rpc_listen_admin.emplace_back(std::move(ip), port, required);
}
for (auto& bind : command_line::get_arg(vm, cryptonote::rpc::http_server::arg_rpc_public)) {
// Much simpler, since this is default empty: everything specified is required.
auto [ip, port] = parse_ip_port(bind, "--rpc-public");
rpc_listen_public.emplace_back(std::move(ip), port, true);
}
}
2023-04-13 15:50:13 +02:00
if (!rpc_listen_admin.empty()) {
log::info(logcat, "- admin HTTP RPC server");
http_rpc_admin.emplace(
*rpc, rpc_config, false /*not restricted*/, std::move(rpc_listen_admin));
}
2023-04-13 15:50:13 +02:00
if (!rpc_listen_public.empty()) {
log::info(logcat, "- public HTTP RPC server");
http_rpc_public.emplace(
*rpc, rpc_config, true /*restricted*/, std::move(rpc_listen_public));
}
2023-04-13 15:50:13 +02:00
log::info(logcat, fg(fmt::terminal_color::blue), "Done daemon object initialization");
}
daemon::~daemon() {
log::info(logcat, fg(fmt::terminal_color::blue), "Deinitializing daemon objects...");
if (http_rpc_public) {
log::info(logcat, "- public HTTP RPC server");
http_rpc_public.reset();
}
2023-04-13 15:50:13 +02:00
if (http_rpc_admin) {
log::info(logcat, "- admin HTTP RPC server");
http_rpc_admin.reset();
}
2023-04-13 15:50:13 +02:00
log::info(logcat, "- p2p");
try {
p2p->deinit();
} catch (const std::exception& e) {
log::error(logcat, "Failed to deinitialize p2p: {}", e.what());
}
daemon & daemonize overhaul This commit continues the complete replacement of the spaghetti code mess that was inside daemon/ and daemonize/ which started in #1138, and looked like a entry level Java programmer threw up inside the code base. This greatly simplifies it, removing a whole pile of useless abstraction layers that don't actually abstract anything, and results in considerably simpler code. (Many of the changes here were also carried out in #1138; this commit updates them with the merged result which amends some things from that PR and goes further in some places). In detail: - the `--detach` (and related `--pidfile`) options are gone. (--detach is still handled, but now just prints a fatal error). Detaching a process is an archaic unix mechanism that has no place on a modern system. If you *really* want to do it anyway, `nohup lokid &` will do the job. (The Windows service control code, which is probably seldom used, is kept because it seems potentially useful for Windows users). - Many of the `t_whatever` classes in daemon/* are just deleted (mostly done in #1138); each one was a bunch of junk code that wraps 3-4 lines but forces an extra layer (not even a generic abstraction, just a useless wrapper) for no good reason and made the daemon code painfully hard to understand and work with. - All of the remaining `t_whatever` classes in daemon/* are either renamed to `whatever` (because prefixing every class with `t_` is moronic). - Some stupid related code (e.g. epee's command handler returning an unsuitable "usage" string that has to be string modified into what we want) was replaced with more generic, useful code. - Replaced boost mutexes/cvs with std ones in epee command handler, and deleted some commented out code. - The `--public-node` option handling was terrible: it was being handled in main, but main doesn't know anything about options, so then it forced it through the spaghetti objects *beside* the pack of all options that got passed along. Moved it to a more sane location (core_rpc_server) and parse it out with some sanity. - Changed a bunch of std::bind's to lambdas because, at least for small lambdas (i.e. with only one-or-two pointers for captures) they will generally be more efficient as the values can be stored in std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
2023-04-13 15:50:13 +02:00
log::info(logcat, "- core");
try {
core->deinit();
core->set_cryptonote_protocol(nullptr);
} catch (const std::exception& e) {
log::error(logcat, "Failed to deinitialize core: {}", e.what());
}
2014-09-08 23:30:50 +02:00
2023-04-13 15:50:13 +02:00
log::info(logcat, "- cryptonote protocol");
try {
protocol->deinit();
protocol->set_p2p_endpoint(nullptr);
} catch (const std::exception& e) {
log::error(logcat, "Failed to stop cryptonote protocol: {}", e.what());
}
log::info(logcat, fg(fmt::terminal_color::blue), "Deinitialization complete");
daemon & daemonize overhaul This commit continues the complete replacement of the spaghetti code mess that was inside daemon/ and daemonize/ which started in #1138, and looked like a entry level Java programmer threw up inside the code base. This greatly simplifies it, removing a whole pile of useless abstraction layers that don't actually abstract anything, and results in considerably simpler code. (Many of the changes here were also carried out in #1138; this commit updates them with the merged result which amends some things from that PR and goes further in some places). In detail: - the `--detach` (and related `--pidfile`) options are gone. (--detach is still handled, but now just prints a fatal error). Detaching a process is an archaic unix mechanism that has no place on a modern system. If you *really* want to do it anyway, `nohup lokid &` will do the job. (The Windows service control code, which is probably seldom used, is kept because it seems potentially useful for Windows users). - Many of the `t_whatever` classes in daemon/* are just deleted (mostly done in #1138); each one was a bunch of junk code that wraps 3-4 lines but forces an extra layer (not even a generic abstraction, just a useless wrapper) for no good reason and made the daemon code painfully hard to understand and work with. - All of the remaining `t_whatever` classes in daemon/* are either renamed to `whatever` (because prefixing every class with `t_` is moronic). - Some stupid related code (e.g. epee's command handler returning an unsuitable "usage" string that has to be string modified into what we want) was replaced with more generic, useful code. - Replaced boost mutexes/cvs with std ones in epee command handler, and deleted some commented out code. - The `--public-node` option handling was terrible: it was being handled in main, but main doesn't know anything about options, so then it forced it through the spaghetti objects *beside* the pack of all options that got passed along. Moved it to a more sane location (core_rpc_server) and parse it out with some sanity. - Changed a bunch of std::bind's to lambdas because, at least for small lambdas (i.e. with only one-or-two pointers for captures) they will generally be more efficient as the values can be stored in std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
}
2023-04-13 15:50:13 +02:00
void daemon::init_options(
boost::program_options::options_description& option_spec,
boost::program_options::options_description& hidden) {
static bool called = false;
if (called)
throw std::logic_error("daemon::init_options must only be called once");
else
called = true;
cryptonote::core::init_options(option_spec);
node_server::init_options(option_spec, hidden);
cryptonote::rpc::core_rpc_server::init_options(option_spec, hidden);
cryptonote::rpc::http_server::init_options(option_spec, hidden);
cryptonote::rpc::init_omq_options(option_spec);
quorumnet::init_core_callbacks();
}
2023-04-13 15:50:13 +02:00
bool daemon::run(bool interactive) {
if (!core)
throw std::runtime_error{"Can't run stopped daemon"};
std::atomic<bool> stop_sig(false), shutdown(false);
std::thread stop_thread{[&stop_sig, &shutdown, this] {
while (!stop_sig)
std::this_thread::sleep_for(100ms);
if (shutdown)
stop();
}};
OXEN_DEFER {
stop_sig = true;
stop_thread.join();
};
tools::signal_handler::install([&stop_sig, &shutdown](int) {
stop_sig = true;
shutdown = true;
});
try {
log::info(logcat, fg(fmt::terminal_color::blue), "Starting up oxend services...");
cryptonote::GetCheckpointsCallback get_checkpoints;
#if defined(PER_BLOCK_CHECKPOINT)
2023-04-13 15:50:13 +02:00
get_checkpoints = blocks::GetCheckpointsData;
#endif
2023-04-13 15:50:13 +02:00
log::info(logcat, "Starting core");
if (!core->init(vm, nullptr, get_checkpoints))
throw std::runtime_error("Failed to start core");
log::info(logcat, "Starting OxenMQ");
omq_rpc = std::make_unique<cryptonote::rpc::omq_rpc>(*core, *rpc, vm);
core->start_oxenmq();
if (http_rpc_admin) {
log::info(logcat, "Starting admin HTTP RPC server");
http_rpc_admin->start();
}
if (http_rpc_public) {
log::info(logcat, "Starting public HTTP RPC server");
http_rpc_public->start();
}
std::optional<daemonize::command_server> rpc_commands;
if (interactive) {
log::info(logcat, "Starting command-line processor");
auto& omq = core->get_omq();
std::promise<void> p;
auto conn = omq.connect_inproc(
[&p](oxenmq::ConnectionID) { p.set_value(); },
[&p](oxenmq::ConnectionID, std::string_view err) {
try {
throw std::runtime_error{
"Internal oxend RPC connection failed: " + std::string{err}};
} catch (...) {
p.set_exception(std::current_exception());
}
});
p.get_future().get();
rpc_commands.emplace(omq, std::move(conn));
rpc_commands->start_handling([this] { stop(); });
}
log::info(logcat, fg(fmt::terminal_color::green), "Starting up main network");
RPC overhaul High-level details: This redesigns the RPC layer to make it much easier to work with, decouples it from an embedded HTTP server, and gets the vast majority of the RPC serialization and dispatch code out of a very commonly included header. There is unfortunately rather a lot of interconnected code here that cannot be easily separated out into separate commits. The full details of what happens here are as follows: Major details: - All of the RPC code is now in a `cryptonote::rpc` namespace; this renames quite a bit to be less verbose: e.g. CORE_RPC_STATUS_OK becomes `rpc::STATUS_OK`, and `cryptonote::COMMAND_RPC_SOME_LONG_NAME` becomes `rpc::SOME_LONG_NAME` (or just SOME_LONG_NAME for code already working in the `rpc` namespace). - `core_rpc_server` is now completely decoupled from providing any request protocol: it is now *just* the core RPC call handler. - The HTTP RPC interface now lives in a new rpc/http_server.h; this code handles listening for HTTP requests and dispatching them to core_rpc_server, then sending the results back to the caller. - There is similarly a rpc/lmq_server.h for LMQ RPC code; more details on this (and other LMQ specifics) below. - RPC implementing code now returns the response object and throws when things go wrong which simplifies much of the rpc error handling. They can throw anything; generic exceptions get logged and a generic "internal error" message gets returned to the caller, but there is also an `rpc_error` class to return an error code and message used by some json-rpc commands. - RPC implementing functions now overload `core_rpc_server::invoke` following the pattern: RPC_BLAH_BLAH::response core_rpc_server::invoke(RPC_BLAH_BLAH::request&& req, rpc_context context); This overloading makes the code vastly simpler: all instantiations are now done with a small amount of generic instantiation code in a single .cpp rather than needing to go to hell and back with a nest of epee macros in a core header. - each RPC endpoint is now defined by the RPC types themselves, including its accessible names and permissions, in core_rpc_server_commands_defs.h: - every RPC structure now has a static `names()` function that returns the names by which the end point is accessible. (The first one is the primary, the others are for deprecated aliases). - RPC command wrappers define their permissions and type by inheriting from special tag classes: - rpc::RPC_COMMAND is a basic, admin-only, JSON command, available via JSON RPC. *All* JSON commands are now available via JSON RPC, instead of the previous mix of some being at /foo and others at /json_rpc. (Ones that were previously at /foo are still there for backwards compatibility; see `rpc::LEGACY` below). - rpc::PUBLIC specifies that the command should be available via a restricted RPC connection. - rpc::BINARY specifies that the command is not JSON, but rather is accessible as /name and takes and returns values in the magic epee binary "portable storage" (lol) data format. - rpc::LEGACY specifies that the command should be available via the non-json-rpc interface at `/name` for backwards compatibility (in addition to the JSON-RPC interface). - some epee serialization got unwrapped and de-templatized so that it can be moved into a .cpp file with just declarations in the .h. (This makes a *huge* difference for core_rpc_server_commands_defs.h and for every compilation unit that includes it which previously had to compile all the serialization code and then throw all by one copy away at link time). This required some new macros so as to not break a ton of places that will use the old way putting everything in the headers; The RPC code uses this as does a few other places; there are comments in contrib/epee/include/serialization/keyvalue_serialization.h as to how to use it. - Detemplatized a bunch of epee/storages code. Most of it should have have been using templates at all (because it can only ever be called with one type!), and now it isn't. This broke some things that didn't properly compile because of missing headers or (in one case) a messed up circular dependency. - Significantly simplified a bunch of over-templatized serialization code. - All RPC serialization definitions is now out of core_rpc_server_commands_defs.h and into a single .cpp file (core_rpc_server_commands_defs.cpp). - core RPC no longer uses the disgusting BEGIN_URI_MAP2/MAP_URI_BLAH_BLAH macros. This was a terrible design that forced slamming tons of code into a common header that didn't need to be there. - epee::struct_init is gone. It was a horrible hack that instiated multiple templates just so the coder could be so lazy and write `some_type var;` instead of properly value initializing with `some_type var{};`. - Removed a bunch of useless crap from epee. In particular, forcing extra template instantiations all over the place in order to nest return objects inside JSON RPC values is no longer needed, as are a bunch of stuff related to the above de-macroization of the code. - get_all_service_nodes, get_service_nodes, and get_n_service_nodes are now combined into a single `get_service_nodes` (with deprecated aliases for the others), which eliminates a fair amount of duplication. The biggest obstacle here was getting the requested fields reference passed through: this is now done by a new ability to stash a context in the serialization object that can be retrieved by a sub-serialized type. LMQ-specifics: - The LokiMQ instance moves into `cryptonote::core` rather than being inside cryptonote_protocol. Currently the instance is used both for qnet and rpc calls (and so needs to be in a common place), but I also intend future PRs to use the batching code for job processing (replacing the current threaded job queue). - rpc/lmq_server.h handles the actual LMQ-request-to-core-RPC glue. Unlike http_server it isn't technically running the whole LMQ stack from here, but the parallel name with http_server seemed appropriate. - All RPC endpoints are supported by LMQ under the same names as defined generically, but prefixed with `rpc.` for public commands and `admin.` for restricted ones. - service node keys are now always available, even when not running in `--service-node` mode: this is because we want the x25519 key for being able to offer CURVE encryption for lmq RPC end-points, and because it doesn't hurt to have them available all the time. In the RPC layer this is now called "get_service_keys" (with "get_service_node_key" as an alias) since they aren't strictly only for service nodes. This also means code needs to check m_service_node, and not m_service_node_keys, to tell if it is running as a service node. (This is also easier to notice because m_service_node_keys got renamed to `m_service_keys`). - Added block and mempool monitoring LMQ RPC endpoints: `sub.block` and `sub.mempool` subscribes the connection for new block and new mempool TX notifications. The latter can notify on just blink txes, or all new mempool txes (but only new ones -- txes dumped from a block don't trigger it). The client gets pushed a [`notify.block`, `height`, `hash`] or [`notify.tx`, `txhash`, `blob`] message when something arrives. Minor details: - rpc::version_t is now a {major,minor} pair. Forcing everyone to pack and unpack a uint32_t was gross. - Changed some macros to constexprs (e.g. CORE_RPC_ERROR_CODE_...). (This immediately revealed a couple of bugs in the RPC code that was assigning CORE_RPC_ERROR_CODE_... to a string, and it worked because the macro allows implicit conversion to a char). - De-templatizing useless templates in epee (i.e. a bunch of templated types that were never invoked with different types) revealed a painful circular dependency between epee and non-epee code for tor_address and i2p_address. This crap is now handled in a suitably named `net/epee_network_address_hack.cpp` hack because it really isn't trivial to extricate this mess. - Removed `epee/include/serialization/serialize_base.h`. Amazingly the code somehow still all works perfectly with this previously vital header removed. - Removed bitrotted, unused epee "crypted_storage" and "gzipped_inmemstorage" code. - Replaced a bunch of epee::misc_utils::auto_scope_leave_caller with LOKI_DEFERs. The epee version involves quite a bit more instantiation and is ugly as sin. Also made the `loki::defer` class invokable for some edge cases that need calling before destruction in particular conditions. - Moved the systemd code around; it makes much more sense to do the systemd started notification as in daemon.cpp as late as possible rather than in core (when we can still have startup failures, e.g. if the RPC layer can't start). - Made the systemd short status string available in the get_info RPC (and no longer require building with systemd). - during startup, print (only) the x25519 when not in SN mode, and continue to print all three when in SN mode. - DRYed out some RPC implementation code (such as set_limit) - Made wallet_rpc stop using a raw m_wallet pointer
2020-04-28 01:25:43 +02:00
#ifdef ENABLE_SYSTEMD
2023-04-13 15:50:13 +02:00
sd_notify(0, ("READY=1\nSTATUS=" + core->get_status_string()).c_str());
RPC overhaul High-level details: This redesigns the RPC layer to make it much easier to work with, decouples it from an embedded HTTP server, and gets the vast majority of the RPC serialization and dispatch code out of a very commonly included header. There is unfortunately rather a lot of interconnected code here that cannot be easily separated out into separate commits. The full details of what happens here are as follows: Major details: - All of the RPC code is now in a `cryptonote::rpc` namespace; this renames quite a bit to be less verbose: e.g. CORE_RPC_STATUS_OK becomes `rpc::STATUS_OK`, and `cryptonote::COMMAND_RPC_SOME_LONG_NAME` becomes `rpc::SOME_LONG_NAME` (or just SOME_LONG_NAME for code already working in the `rpc` namespace). - `core_rpc_server` is now completely decoupled from providing any request protocol: it is now *just* the core RPC call handler. - The HTTP RPC interface now lives in a new rpc/http_server.h; this code handles listening for HTTP requests and dispatching them to core_rpc_server, then sending the results back to the caller. - There is similarly a rpc/lmq_server.h for LMQ RPC code; more details on this (and other LMQ specifics) below. - RPC implementing code now returns the response object and throws when things go wrong which simplifies much of the rpc error handling. They can throw anything; generic exceptions get logged and a generic "internal error" message gets returned to the caller, but there is also an `rpc_error` class to return an error code and message used by some json-rpc commands. - RPC implementing functions now overload `core_rpc_server::invoke` following the pattern: RPC_BLAH_BLAH::response core_rpc_server::invoke(RPC_BLAH_BLAH::request&& req, rpc_context context); This overloading makes the code vastly simpler: all instantiations are now done with a small amount of generic instantiation code in a single .cpp rather than needing to go to hell and back with a nest of epee macros in a core header. - each RPC endpoint is now defined by the RPC types themselves, including its accessible names and permissions, in core_rpc_server_commands_defs.h: - every RPC structure now has a static `names()` function that returns the names by which the end point is accessible. (The first one is the primary, the others are for deprecated aliases). - RPC command wrappers define their permissions and type by inheriting from special tag classes: - rpc::RPC_COMMAND is a basic, admin-only, JSON command, available via JSON RPC. *All* JSON commands are now available via JSON RPC, instead of the previous mix of some being at /foo and others at /json_rpc. (Ones that were previously at /foo are still there for backwards compatibility; see `rpc::LEGACY` below). - rpc::PUBLIC specifies that the command should be available via a restricted RPC connection. - rpc::BINARY specifies that the command is not JSON, but rather is accessible as /name and takes and returns values in the magic epee binary "portable storage" (lol) data format. - rpc::LEGACY specifies that the command should be available via the non-json-rpc interface at `/name` for backwards compatibility (in addition to the JSON-RPC interface). - some epee serialization got unwrapped and de-templatized so that it can be moved into a .cpp file with just declarations in the .h. (This makes a *huge* difference for core_rpc_server_commands_defs.h and for every compilation unit that includes it which previously had to compile all the serialization code and then throw all by one copy away at link time). This required some new macros so as to not break a ton of places that will use the old way putting everything in the headers; The RPC code uses this as does a few other places; there are comments in contrib/epee/include/serialization/keyvalue_serialization.h as to how to use it. - Detemplatized a bunch of epee/storages code. Most of it should have have been using templates at all (because it can only ever be called with one type!), and now it isn't. This broke some things that didn't properly compile because of missing headers or (in one case) a messed up circular dependency. - Significantly simplified a bunch of over-templatized serialization code. - All RPC serialization definitions is now out of core_rpc_server_commands_defs.h and into a single .cpp file (core_rpc_server_commands_defs.cpp). - core RPC no longer uses the disgusting BEGIN_URI_MAP2/MAP_URI_BLAH_BLAH macros. This was a terrible design that forced slamming tons of code into a common header that didn't need to be there. - epee::struct_init is gone. It was a horrible hack that instiated multiple templates just so the coder could be so lazy and write `some_type var;` instead of properly value initializing with `some_type var{};`. - Removed a bunch of useless crap from epee. In particular, forcing extra template instantiations all over the place in order to nest return objects inside JSON RPC values is no longer needed, as are a bunch of stuff related to the above de-macroization of the code. - get_all_service_nodes, get_service_nodes, and get_n_service_nodes are now combined into a single `get_service_nodes` (with deprecated aliases for the others), which eliminates a fair amount of duplication. The biggest obstacle here was getting the requested fields reference passed through: this is now done by a new ability to stash a context in the serialization object that can be retrieved by a sub-serialized type. LMQ-specifics: - The LokiMQ instance moves into `cryptonote::core` rather than being inside cryptonote_protocol. Currently the instance is used both for qnet and rpc calls (and so needs to be in a common place), but I also intend future PRs to use the batching code for job processing (replacing the current threaded job queue). - rpc/lmq_server.h handles the actual LMQ-request-to-core-RPC glue. Unlike http_server it isn't technically running the whole LMQ stack from here, but the parallel name with http_server seemed appropriate. - All RPC endpoints are supported by LMQ under the same names as defined generically, but prefixed with `rpc.` for public commands and `admin.` for restricted ones. - service node keys are now always available, even when not running in `--service-node` mode: this is because we want the x25519 key for being able to offer CURVE encryption for lmq RPC end-points, and because it doesn't hurt to have them available all the time. In the RPC layer this is now called "get_service_keys" (with "get_service_node_key" as an alias) since they aren't strictly only for service nodes. This also means code needs to check m_service_node, and not m_service_node_keys, to tell if it is running as a service node. (This is also easier to notice because m_service_node_keys got renamed to `m_service_keys`). - Added block and mempool monitoring LMQ RPC endpoints: `sub.block` and `sub.mempool` subscribes the connection for new block and new mempool TX notifications. The latter can notify on just blink txes, or all new mempool txes (but only new ones -- txes dumped from a block don't trigger it). The client gets pushed a [`notify.block`, `height`, `hash`] or [`notify.tx`, `txhash`, `blob`] message when something arrives. Minor details: - rpc::version_t is now a {major,minor} pair. Forcing everyone to pack and unpack a uint32_t was gross. - Changed some macros to constexprs (e.g. CORE_RPC_ERROR_CODE_...). (This immediately revealed a couple of bugs in the RPC code that was assigning CORE_RPC_ERROR_CODE_... to a string, and it worked because the macro allows implicit conversion to a char). - De-templatizing useless templates in epee (i.e. a bunch of templated types that were never invoked with different types) revealed a painful circular dependency between epee and non-epee code for tor_address and i2p_address. This crap is now handled in a suitably named `net/epee_network_address_hack.cpp` hack because it really isn't trivial to extricate this mess. - Removed `epee/include/serialization/serialize_base.h`. Amazingly the code somehow still all works perfectly with this previously vital header removed. - Removed bitrotted, unused epee "crypted_storage" and "gzipped_inmemstorage" code. - Replaced a bunch of epee::misc_utils::auto_scope_leave_caller with LOKI_DEFERs. The epee version involves quite a bit more instantiation and is ugly as sin. Also made the `loki::defer` class invokable for some edge cases that need calling before destruction in particular conditions. - Moved the systemd code around; it makes much more sense to do the systemd started notification as in daemon.cpp as late as possible rather than in core (when we can still have startup failures, e.g. if the RPC layer can't start). - Made the systemd short status string available in the get_info RPC (and no longer require building with systemd). - during startup, print (only) the x25519 when not in SN mode, and continue to print all three when in SN mode. - DRYed out some RPC implementation code (such as set_limit) - Made wallet_rpc stop using a raw m_wallet pointer
2020-04-28 01:25:43 +02:00
#endif
2023-04-13 15:50:13 +02:00
p2p->run(); // blocks until p2p goes down
log::info(logcat, fg(fmt::terminal_color::yellow), "Main network stopped");
if (rpc_commands) {
log::info(logcat, "Stopping RPC command processor");
rpc_commands->stop_handling();
rpc_commands.reset();
}
if (http_rpc_public) {
log::info(logcat, "Stopping public HTTP RPC server...");
http_rpc_public->shutdown();
}
if (http_rpc_admin) {
log::info(logcat, "Stopping admin HTTP RPC server...");
http_rpc_admin->shutdown();
}
log::info(logcat, "Node stopped.");
return true;
} catch (std::exception const& ex) {
log::error(logcat, ex.what());
return false;
} catch (...) {
log::error(logcat, "Unknown exception occured!");
return false;
}
}
2023-04-13 15:50:13 +02:00
void daemon::stop() {
if (!core)
throw std::logic_error{"Can't send stop signal to a stopped daemon"};
2014-03-03 23:07:58 +01:00
2023-04-13 15:50:13 +02:00
p2p->send_stop_signal(); // Make p2p stop so that `run()` above continues with tear down
2015-06-14 01:30:04 +02:00
}
2023-04-13 15:50:13 +02:00
} // namespace daemonize