Debian Codegraph @

tests/trezor/daemon.h

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #pragma once
30
31 #include "misc_log_ex.h"
32 #include "daemon/daemon.h"
33 #include "rpc/daemon_handler.h"
34 #include "rpc/zmq_server.h"
35 #include "common/password.h"
36 #include "common/util.h"
37 #include "daemon/core.h"
38 #include "daemon/p2p.h"
39 #include "daemon/protocol.h"
40 #include "daemon/rpc.h"
41 #include "daemon/command_server.h"
42 #include "daemon/command_server.h"
43 #include "daemon/command_line_args.h"
44 #include "version.h"
45 #include "tools.h"
46
47
48 class mock_rpc_daemon : public cryptonote::core_rpc_server {
49 public:
50 mock_rpc_daemon(
51 cryptonote::core& cr
52 , nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >& p2p
53 ): cryptonote::core_rpc_server(cr, p2p) {}
54
55 static void init_options(boost::program_options::options_description& desc){ cryptonote::core_rpc_server::init_options(desc); }
56 cryptonote::network_type nettype() const { return m_network_type; }
57 void nettype(cryptonote::network_type nettype) { m_network_type = nettype; }
58
59 CHAIN_HTTP_TO_MAP2(cryptonote::core_rpc_server::connection_context); //forward http requests to uri map
60 BEGIN_URI_MAP2()
61 MAP_URI_AUTO_JON2("/send_raw_transaction", on_send_raw_tx_2, cryptonote::COMMAND_RPC_SEND_RAW_TX)
62 MAP_URI_AUTO_JON2("/sendrawtransaction", on_send_raw_tx_2, cryptonote::COMMAND_RPC_SEND_RAW_TX)
63 else { // Default to parent for non-overriden callbacks
64 return cryptonote::core_rpc_server::handle_http_request_map(query_info, response_info, m_conn_context);
65 }
66 END_URI_MAP2()
67
68 bool on_send_raw_tx_2(const cryptonote::COMMAND_RPC_SEND_RAW_TX::request& req, cryptonote::COMMAND_RPC_SEND_RAW_TX::response& res, const cryptonote::core_rpc_server::connection_context *ctx);
69
70 protected:
71 cryptonote::network_type m_network_type;
72 };
73
74 class mock_daemon {
75 public:
76 typedef cryptonote::t_cryptonote_protocol_handler<cryptonote::core> t_protocol_raw;
77 typedef nodetool::node_server<t_protocol_raw> t_node_server;
78
79 static constexpr const std::chrono::seconds rpc_timeout = std::chrono::seconds(120);
80
81 cryptonote::core * m_core;
82 t_protocol_raw m_protocol;
83 mock_rpc_daemon m_rpc_server;
84 t_node_server m_server;
85 cryptonote::network_type m_network_type;
86 epee::net_utils::http::http_simple_client m_http_client;
87
88 bool m_start_p2p;
89 bool m_start_zmq;
90 boost::program_options::variables_map m_vm;
91
92 std::string m_p2p_bind_port;
93 std::string m_rpc_bind_port;
94 std::string m_zmq_bind_port;
95
96 std::atomic<bool> m_stopped;
97 std::atomic<bool> m_terminated;
98 std::atomic<bool> m_deinitalized;
99 boost::thread m_run_thread;
100
101 mock_daemon(
102 cryptonote::core * core,
103 boost::program_options::variables_map const & vm
104 )
105 : m_core(core)
106 , m_vm(vm)
107 , m_start_p2p(false)
108 , m_start_zmq(false)
109 , m_terminated(false)
110 , m_deinitalized(false)
111 , m_stopped(false)
112 , m_protocol{*core, nullptr, command_line::get_arg(vm, cryptonote::arg_offline)}
113 , m_server{m_protocol}
114 , m_rpc_server{*core, m_server}
115 {
116 // Handle circular dependencies
117 m_protocol.set_p2p_endpoint(&m_server);
118 m_core->set_cryptonote_protocol(&m_protocol);
119 load_params(vm);
120 }
121
122 virtual ~mock_daemon();
123
124 static void init_options(boost::program_options::options_description & option_spec);
125 static void default_options(boost::program_options::variables_map & vm);
126 static void set_ports(boost::program_options::variables_map & vm, unsigned initial_port);
127
128 mock_daemon * set_start_p2p(bool fl) { m_start_p2p = fl; return this; }
129 mock_daemon * set_start_zmq(bool fl) { m_start_zmq = fl; return this; }
130
131 void init();
132 void deinit();
133 void run();
134 bool run_main();
135 void stop();
136 void stop_p2p();
137 void stop_rpc();
138 void init_and_run();
139 void stop_and_deinit();
140 void try_init_and_run(boost::optional<unsigned> initial_port=boost::none);
141
142 void mine_blocks(size_t num_blocks, const std::string &miner_address);
143 void start_mining(const std::string &miner_address, uint64_t threads_count=1, bool do_background_mining=false, bool ignore_battery=true);
144 void stop_mining();
145 uint64_t get_height();
146
147 void load_params(boost::program_options::variables_map const & vm);
148
149 std::string zmq_addr() const { return std::string("127.0.0.1:") + m_zmq_bind_port; }
150 std::string rpc_addr() const { return std::string("127.0.0.1:") + m_rpc_bind_port; }
151 std::string p2p_addr() const { return std::string("127.0.0.1:") + m_p2p_bind_port; }
152 cryptonote::network_type nettype() const { return m_network_type; }
153 cryptonote::core * core() const { return m_core; }
154 };