Electroneum
daemon.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, 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"
44 #include "version.h"
45 #include "tools.h"
46 
47 
49 public:
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); }
58 
59  CHAIN_HTTP_TO_MAP2(cryptonote::core_rpc_server::connection_context); //forward http requests to uri map
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:
72 };
73 
74 class mock_daemon {
75 public:
78 
79  static constexpr const std::chrono::seconds rpc_timeout = std::chrono::seconds(60);
80 
87 
90  boost::program_options::variables_map m_vm;
91 
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 
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; }
153  cryptonote::core * core() const { return m_core; }
154 };
const char * res
Definition: hmac_keccak.cpp:41
std::atomic< bool > m_stopped
Definition: daemon.h:96
CHAIN_HTTP_TO_MAP2(cryptonote::core_rpc_server::connection_context)
#define BEGIN_URI_MAP2()
mock_daemon * set_start_zmq(bool fl)
Definition: daemon.h:129
bool m_start_p2p
Definition: daemon.h:88
boost::thread m_run_thread
Definition: daemon.h:99
bool m_start_zmq
Definition: daemon.h:89
void set_p2p_endpoint(nodetool::i_p2p_endpoint< connection_context > *p2p)
::std::string string
Definition: gtest-port.h:1097
epee::misc_utils::struct_init< response_t > response
static void init_options(boost::program_options::options_description &desc)
Definition: daemon.h:55
cryptonote::t_cryptonote_protocol_handler< cryptonote::core > t_protocol_raw
Definition: daemon.h:76
std::atomic< bool > m_terminated
Definition: daemon.h:97
uint64_t num_blocks(const std::vector< test_event_entry > &events)
Definition: chaingen.cpp:1044
bool init(const boost::program_options::variables_map &vm, const bool restricted, const std::string &port)
cryptonote::network_type m_network_type
Definition: daemon.h:85
std::atomic< bool > m_deinitalized
Definition: daemon.h:98
std::string m_zmq_bind_port
Definition: daemon.h:94
mock_daemon * set_start_p2p(bool fl)
Definition: daemon.h:128
cryptonote::core * m_core
Definition: daemon.h:81
core_rpc_server(core &cr, nodetool::node_server< cryptonote::t_cryptonote_protocol_handler< cryptonote::core > > &p2p)
cryptonote::network_type m_network_type
Definition: daemon.h:71
cryptonote::network_type nettype() const
Definition: daemon.h:152
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
#define END_URI_MAP2()
void set_cryptonote_protocol(i_cryptonote_protocol *pprotocol)
set the pointer to the cryptonote protocol object to use
std::string p2p_addr() const
Definition: daemon.h:151
handles core cryptonote functionality
unsigned __int64 uint64_t
Definition: stdint.h:136
epee::net_utils::http::http_simple_client m_http_client
Definition: daemon.h:86
nodetool::node_server< t_protocol_raw > t_node_server
Definition: daemon.h:77
std::string m_rpc_bind_port
Definition: daemon.h:93
#define false
Definition: stdbool.h:38
bool run(size_t threads_count, bool wait=true)
const command_line::arg_descriptor< bool > arg_offline
t_node_server m_server
Definition: daemon.h:84
std::string m_p2p_bind_port
Definition: daemon.h:92
epee::net_utils::connection_context_base connection_context
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
int bool
Definition: stdbool.h:36
t_protocol_raw m_protocol
Definition: daemon.h:82
void nettype(cryptonote::network_type nettype)
Definition: daemon.h:57
cryptonote::network_type nettype() const
Definition: daemon.h:56
#define MAP_URI_AUTO_JON2(s_pattern, callback_f, command_type)
std::string rpc_addr() const
Definition: daemon.h:150
mock_rpc_daemon(cryptonote::core &cr, nodetool::node_server< cryptonote::t_cryptonote_protocol_handler< cryptonote::core > > &p2p)
Definition: daemon.h:50
mock_daemon(cryptonote::core *core, boost::program_options::variables_map const &vm)
Definition: daemon.h:101
static void init_options(boost::program_options::options_description &desc)
std::string zmq_addr() const
Definition: daemon.h:149
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)
Definition: daemon.cpp:36
cryptonote::core * core() const
Definition: daemon.h:153
mock_rpc_daemon m_rpc_server
Definition: daemon.h:83
boost::program_options::variables_map m_vm
Definition: daemon.h:90