aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrfree2monero <rfreemonero@op.pl>2015-04-01 18:24:45 +0200
committerrfree2monero <rfreemonero@op.pl>2015-04-01 18:24:45 +0200
commit3cbdf198f168e113f12fa49d016133c434964ed7 (patch)
tree050c74077de8e1b6e431f7d0671b57b5afcb01ab /src
parentfix locking in count-peers thread (2) (diff)
parentMerge pull request #248 (diff)
downloadmonero-3cbdf198f168e113f12fa49d016133c434964ed7.tar.xz
Merge remote-tracking branch 'monero-official/master' into network-1.6-work1
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/common/CMakeLists.txt3
-rw-r--r--src/common/dns_utils.cpp66
-rw-r--r--src/common/http_connection.h42
-rw-r--r--src/common/rpc_client.h132
-rw-r--r--src/common/scoped_message_writer.h95
-rw-r--r--src/common/util.cpp2
-rw-r--r--src/common/util.h12
-rw-r--r--src/cryptonote_config.h4
-rw-r--r--src/cryptonote_core/blockchain_storage.cpp23
-rw-r--r--src/cryptonote_core/blockchain_storage.h3
-rw-r--r--src/cryptonote_core/checkpoints_create.cpp41
-rw-r--r--src/cryptonote_core/checkpoints_create.h2
-rw-r--r--src/cryptonote_core/cryptonote_basic_impl.cpp4
-rw-r--r--src/cryptonote_core/cryptonote_core.cpp41
-rw-r--r--src/cryptonote_core/cryptonote_core.h7
-rw-r--r--src/daemon/CMakeLists.txt39
-rw-r--r--src/daemon/command_line_args.h76
-rw-r--r--src/daemon/command_parser_executor.cpp299
-rw-r--r--src/daemon/command_parser_executor.h98
-rw-r--r--src/daemon/command_server.cpp213
-rw-r--r--src/daemon/command_server.h75
-rw-r--r--src/daemon/core.h99
-rw-r--r--src/daemon/daemon.cpp377
-rw-r--r--src/daemon/daemon.h54
-rw-r--r--src/daemon/daemon_commands_handler.h191
-rw-r--r--src/daemon/executor.cpp71
-rw-r--r--src/daemon/executor.h60
-rw-r--r--src/daemon/main.cpp238
-rw-r--r--src/daemon/p2p.h99
-rw-r--r--src/daemon/protocol.h88
-rw-r--r--src/daemon/rpc.h101
-rw-r--r--src/daemon/rpc_command_executor.cpp692
-rw-r--r--src/daemon/rpc_command_executor.h111
-rw-r--r--src/daemonizer/CMakeLists.txt74
-rw-r--r--src/daemonizer/daemonizer.h38
-rw-r--r--src/daemonizer/posix_daemonizer.inl60
-rw-r--r--src/daemonizer/posix_fork.cpp108
-rw-r--r--src/daemonizer/posix_fork.h11
-rw-r--r--src/daemonizer/windows_daemonizer.inl156
-rw-r--r--src/daemonizer/windows_service.cpp341
-rw-r--r--src/daemonizer/windows_service.h36
-rw-r--r--src/daemonizer/windows_service_runner.h157
-rw-r--r--src/mnemonics/english.h4
-rw-r--r--src/p2p/net_node.h20
-rw-r--r--src/p2p/net_node.inl8
-rw-r--r--src/rpc/core_rpc_server.cpp155
-rw-r--r--src/rpc/core_rpc_server.h64
-rw-r--r--src/rpc/core_rpc_server_commands_defs.h175
-rw-r--r--src/simplewallet/simplewallet.cpp27
-rw-r--r--src/wallet/wallet_rpc_server.cpp18
-rw-r--r--src/wallet/wallet_rpc_server.h18
52 files changed, 4456 insertions, 473 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 60501cefa..efc9c02f6 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -98,4 +98,5 @@ add_subdirectory(cryptonote_protocol)
add_subdirectory(connectivity_tool)
add_subdirectory(miner)
add_subdirectory(simplewallet)
+add_subdirectory(daemonizer)
add_subdirectory(daemon)
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 16c4b0ae3..620e1add9 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -39,8 +39,11 @@ set(common_private_headers
boost_serialization_helper.h
command_line.h
dns_utils.h
+ http_connection.h
int-util.h
pod-class.h
+ rpc_client.h
+ scoped_message_writer.h
unordered_containers_boost_serialization.h
util.h
varint.h)
diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp
index 3e50b6af8..4ab93cce8 100644
--- a/src/common/dns_utils.cpp
+++ b/src/common/dns_utils.cpp
@@ -34,7 +34,65 @@
#include <stdlib.h>
#include "include_base_utils.h"
+#include <boost/filesystem/fstream.hpp>
using namespace epee;
+namespace bf = boost::filesystem;
+
+namespace
+{
+
+/*
+ * The following two functions were taken from unbound-anchor.c, from
+ * the unbound library packaged with this source. The license and source
+ * can be found in $PROJECT_ROOT/external/unbound
+ */
+
+/* Cert builtin commented out until it's used, as the compiler complains
+
+// return the built in root update certificate
+static const char*
+get_builtin_cert(void)
+{
+ return
+// The ICANN CA fetched at 24 Sep 2010. Valid to 2028
+"-----BEGIN CERTIFICATE-----\n"
+"MIIDdzCCAl+gAwIBAgIBATANBgkqhkiG9w0BAQsFADBdMQ4wDAYDVQQKEwVJQ0FO\n"
+"TjEmMCQGA1UECxMdSUNBTk4gQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkxFjAUBgNV\n"
+"BAMTDUlDQU5OIFJvb3QgQ0ExCzAJBgNVBAYTAlVTMB4XDTA5MTIyMzA0MTkxMloX\n"
+"DTI5MTIxODA0MTkxMlowXTEOMAwGA1UEChMFSUNBTk4xJjAkBgNVBAsTHUlDQU5O\n"
+"IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MRYwFAYDVQQDEw1JQ0FOTiBSb290IENB\n"
+"MQswCQYDVQQGEwJVUzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKDb\n"
+"cLhPNNqc1NB+u+oVvOnJESofYS9qub0/PXagmgr37pNublVThIzyLPGCJ8gPms9S\n"
+"G1TaKNIsMI7d+5IgMy3WyPEOECGIcfqEIktdR1YWfJufXcMReZwU4v/AdKzdOdfg\n"
+"ONiwc6r70duEr1IiqPbVm5T05l1e6D+HkAvHGnf1LtOPGs4CHQdpIUcy2kauAEy2\n"
+"paKcOcHASvbTHK7TbbvHGPB+7faAztABLoneErruEcumetcNfPMIjXKdv1V1E3C7\n"
+"MSJKy+jAqqQJqjZoQGB0necZgUMiUv7JK1IPQRM2CXJllcyJrm9WFxY0c1KjBO29\n"
+"iIKK69fcglKcBuFShUECAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8B\n"
+"Af8EBAMCAf4wHQYDVR0OBBYEFLpS6UmDJIZSL8eZzfyNa2kITcBQMA0GCSqGSIb3\n"
+"DQEBCwUAA4IBAQAP8emCogqHny2UYFqywEuhLys7R9UKmYY4suzGO4nkbgfPFMfH\n"
+"6M+Zj6owwxlwueZt1j/IaCayoKU3QsrYYoDRolpILh+FPwx7wseUEV8ZKpWsoDoD\n"
+"2JFbLg2cfB8u/OlE4RYmcxxFSmXBg0yQ8/IoQt/bxOcEEhhiQ168H2yE5rxJMt9h\n"
+"15nu5JBSewrCkYqYYmaxyOC3WrVGfHZxVI7MpIFcGdvSb2a1uyuua8l0BKgk3ujF\n"
+"0/wsHNeP22qNyVO+XVBzrM8fk8BSUFuiT/6tZTYXRtEt5aKQZgXbKU5dUF3jT9qg\n"
+"j/Br5BZw3X/zd325TvnswzMC1+ljLzHnQGGk\n"
+"-----END CERTIFICATE-----\n"
+ ;
+}
+*/
+
+/** return the built in root DS trust anchor */
+static const char*
+get_builtin_ds(void)
+{
+ return
+". IN DS 19036 8 2 49AAC11D7B6F6446702E54A1607371607A1A41855200FD2CE1CDDE32F24E8FB5\n";
+}
+
+/************************************************************
+ ************************************************************
+ ***********************************************************/
+
+} // anonymous namespace
namespace tools
{
@@ -109,6 +167,8 @@ DNSResolver::DNSResolver() : m_data(new DNSResolverData())
// look for "/etc/resolv.conf" and "/etc/hosts" or platform equivalent
ub_ctx_resolvconf(m_data->m_ub_context, &empty_string);
ub_ctx_hosts(m_data->m_ub_context, &empty_string);
+
+ ub_ctx_add_ta(m_data->m_ub_context, ::get_builtin_ds());
}
DNSResolver::~DNSResolver()
@@ -143,6 +203,8 @@ std::vector<std::string> DNSResolver::get_ipv4(const std::string& url, bool& dns
// call DNS resolver, blocking. if return value not zero, something went wrong
if (!ub_resolve(m_data->m_ub_context, urlC, DNS_TYPE_A, DNS_CLASS_IN, &(result.ptr)))
{
+ dnssec_available = (result.ptr->secure || (!result.ptr->secure && result.ptr->bogus));
+ dnssec_valid = !result.ptr->bogus;
if (result.ptr->havedata)
{
for (size_t i=0; result.ptr->data[i] != NULL; i++)
@@ -175,6 +237,8 @@ std::vector<std::string> DNSResolver::get_ipv6(const std::string& url, bool& dns
// call DNS resolver, blocking. if return value not zero, something went wrong
if (!ub_resolve(m_data->m_ub_context, urlC, DNS_TYPE_AAAA, DNS_CLASS_IN, &(result.ptr)))
{
+ dnssec_available = (result.ptr->secure || (!result.ptr->secure && result.ptr->bogus));
+ dnssec_valid = !result.ptr->bogus;
if (result.ptr->havedata)
{
for (size_t i=0; result.ptr->data[i] != NULL; i++)
@@ -207,6 +271,8 @@ std::vector<std::string> DNSResolver::get_txt_record(const std::string& url, boo
// call DNS resolver, blocking. if return value not zero, something went wrong
if (!ub_resolve(m_data->m_ub_context, urlC, DNS_TYPE_TXT, DNS_CLASS_IN, &(result.ptr)))
{
+ dnssec_available = (result.ptr->secure || (!result.ptr->secure && result.ptr->bogus));
+ dnssec_valid = !result.ptr->bogus;
if (result.ptr->havedata)
{
for (size_t i=0; result.ptr->data[i] != NULL; i++)
diff --git a/src/common/http_connection.h b/src/common/http_connection.h
new file mode 100644
index 000000000..759145009
--- /dev/null
+++ b/src/common/http_connection.h
@@ -0,0 +1,42 @@
+#pragma once
+
+#include "string_tools.h"
+#include "net/http_client.h"
+
+namespace tools {
+
+class t_http_connection {
+private:
+ epee::net_utils::http::http_simple_client * mp_http_client;
+ bool m_ok;
+public:
+ static unsigned int const TIMEOUT = 200000;
+
+ t_http_connection(
+ epee::net_utils::http::http_simple_client * p_http_client
+ , uint32_t ip
+ , uint16_t port
+ )
+ : mp_http_client(p_http_client)
+ {
+ // TODO fix http client so that it accepts properly typed arguments
+ std::string ip_str = epee::string_tools::get_ip_string_from_int32(ip);
+ std::string port_str = boost::lexical_cast<std::string>(port);
+ m_ok = mp_http_client->connect(ip_str, port_str, TIMEOUT);
+ }
+
+ ~t_http_connection()
+ {
+ if (m_ok)
+ {
+ mp_http_client->disconnect();
+ }
+ }
+
+ bool is_open()
+ {
+ return m_ok;
+ }
+}; // class t_http_connection
+
+} // namespace tools
diff --git a/src/common/rpc_client.h b/src/common/rpc_client.h
new file mode 100644
index 000000000..a6d4b6cc1
--- /dev/null
+++ b/src/common/rpc_client.h
@@ -0,0 +1,132 @@
+#pragma once
+
+#include "common/http_connection.h"
+#include "common/scoped_message_writer.h"
+#include "rpc/core_rpc_server_commands_defs.h"
+#include "storages/http_abstract_invoke.h"
+#include "net/http_client.h"
+#include "string_tools.h"
+#include <boost/lexical_cast.hpp>
+
+namespace tools
+{
+ class t_rpc_client final
+ {
+ private:
+ epee::net_utils::http::http_simple_client m_http_client;
+ uint32_t m_ip;
+ uint16_t m_port;
+ public:
+ t_rpc_client(
+ uint32_t ip
+ , uint16_t port
+ )
+ : m_http_client{}
+ , m_ip{ip}
+ , m_port{port}
+ {}
+
+ std::string build_url(std::string const & relative_url)
+ {
+ std::string result =
+ "http://"
+ + epee::string_tools::get_ip_string_from_int32(m_ip)
+ + ":"
+ + boost::lexical_cast<std::string>(m_port)
+ + relative_url;
+ return result;
+ }
+
+ template <typename T_req, typename T_res>
+ bool basic_json_rpc_request(
+ T_req & req
+ , T_res & res
+ , std::string const & method_name
+ )
+ {
+ std::string rpc_url = build_url("/json_rpc");
+ t_http_connection connection(&m_http_client, m_ip, m_port);
+
+ bool ok = connection.is_open();
+ if (!ok)
+ {
+ fail_msg_writer() << "Couldn't connect to daemon";
+ return false;
+ }
+ ok = ok && epee::net_utils::invoke_http_json_rpc(rpc_url, method_name, req, res, m_http_client);
+ if (!ok)
+ {
+ fail_msg_writer() << "Daemon request failed";
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+
+ template <typename T_req, typename T_res>
+ bool json_rpc_request(
+ T_req & req
+ , T_res & res
+ , std::string const & method_name
+ , std::string const & fail_msg
+ )
+ {
+ std::string rpc_url = build_url("/json_rpc");
+ t_http_connection connection(&m_http_client, m_ip, m_port);
+
+ bool ok = connection.is_open();
+ ok = ok && epee::net_utils::invoke_http_json_rpc(rpc_url, method_name, req, res, m_http_client);
+ if (!ok)
+ {
+ fail_msg_writer() << "Couldn't connect to daemon";
+ return false;
+ }
+ else if (res.status != CORE_RPC_STATUS_OK) // TODO - handle CORE_RPC_STATUS_BUSY ?
+ {
+ fail_msg_writer() << fail_msg << " -- " << res.status;
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+
+ template <typename T_req, typename T_res>
+ bool rpc_request(
+ T_req & req
+ , T_res & res
+ , std::string const & relative_url
+ , std::string const & fail_msg
+ )
+ {
+ std::string rpc_url = build_url(relative_url);
+ t_http_connection connection(&m_http_client, m_ip, m_port);
+
+ bool ok = connection.is_open();
+ ok = ok && epee::net_utils::invoke_http_json_remote_command2(rpc_url, req, res, m_http_client);
+ if (!ok)
+ {
+ fail_msg_writer() << "Couldn't connect to daemon";
+ return false;
+ }
+ else if (res.status != CORE_RPC_STATUS_OK) // TODO - handle CORE_RPC_STATUS_BUSY ?
+ {
+ fail_msg_writer() << fail_msg << " -- " << res.status;
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+
+ bool check_connection()
+ {
+ t_http_connection connection(&m_http_client, m_ip, m_port);
+ return connection.is_open();
+ }
+ };
+}
diff --git a/src/common/scoped_message_writer.h b/src/common/scoped_message_writer.h
new file mode 100644
index 000000000..70db54eae
--- /dev/null
+++ b/src/common/scoped_message_writer.h
@@ -0,0 +1,95 @@
+#pragma once
+
+#include "misc_log_ex.h"
+#include <iostream>
+
+namespace tools
+{
+
+class scoped_message_writer
+{
+private:
+ bool m_flush;
+ std::stringstream m_oss;
+ epee::log_space::console_colors m_color;
+ bool m_bright;
+ int m_log_level;
+public:
+ scoped_message_writer(
+ epee::log_space::console_colors color = epee::log_space::console_color_default
+ , bool bright = false
+ , std::string&& prefix = std::string()
+ , int log_level = LOG_LEVEL_2
+ )
+ : m_flush(true)
+ , m_color(color)
+ , m_bright(bright)
+ , m_log_level(log_level)
+ {
+ m_oss << prefix;
+ }
+
+ scoped_message_writer(scoped_message_writer&& rhs)
+ : m_flush(std::move(rhs.m_flush))
+#if defined(_MSC_VER)
+ , m_oss(std::move(rhs.m_oss))
+#else
+ // GCC bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316
+ , m_oss(rhs.m_oss.str(), std::ios_base::out | std::ios_base::ate)
+#endif
+ , m_color(std::move(rhs.m_color))
+ , m_log_level(std::move(rhs.m_log_level))
+ {
+ rhs.m_flush = false;
+ }
+
+ scoped_message_writer(scoped_message_writer& rhs) = delete;
+ scoped_message_writer& operator=(scoped_message_writer& rhs) = delete;
+ scoped_message_writer& operator=(scoped_message_writer&& rhs) = delete;
+
+ template<typename T>
+ std::ostream& operator<<(const T& val)
+ {
+ m_oss << val;
+ return m_oss;
+ }
+
+ ~scoped_message_writer()
+ {
+ if (m_flush)
+ {
+ m_flush = false;
+
+ LOG_PRINT(m_oss.str(), m_log_level)
+
+ if (epee::log_space::console_color_default == m_color)
+ {
+ std::cout << m_oss.str();
+ }
+ else
+ {
+ epee::log_space::set_console_color(m_color, m_bright);
+ std::cout << m_oss.str();
+ epee::log_space::reset_console_color();
+ }
+ std::cout << std::endl;
+ }
+ }
+};
+
+inline scoped_message_writer success_msg_writer()
+{
+ return scoped_message_writer(epee::log_space::console_color_green, false, std::string(), LOG_LEVEL_2);
+}
+
+inline scoped_message_writer msg_writer(epee::log_space::console_colors color = epee::log_space::console_color_default)
+{
+ return scoped_message_writer(color, false, std::string(), LOG_LEVEL_2);
+}
+
+inline scoped_message_writer fail_msg_writer()
+{
+ return scoped_message_writer(epee::log_space::console_color_red, true, "Error: ", LOG_LEVEL_0);
+}
+
+} // namespace tools
diff --git a/src/common/util.cpp b/src/common/util.cpp
index 94c1e042e..7d39bc4f4 100644
--- a/src/common/util.cpp
+++ b/src/common/util.cpp
@@ -326,7 +326,7 @@ std::string get_nix_version_display_string()
std::string config_folder;
#ifdef WIN32
- config_folder = get_special_folder_path(CSIDL_APPDATA, true) + "\\" + CRYPTONOTE_NAME;
+ config_folder = get_special_folder_path(CSIDL_COMMON_APPDATA, true) + "\\" + CRYPTONOTE_NAME;
#else
std::string pathRet;
char* pszHome = getenv("HOME");
diff --git a/src/common/util.h b/src/common/util.h
index 0d23135b0..883fe1e0f 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -58,6 +58,18 @@ namespace tools
*/
std::string get_default_data_dir();
+#ifdef WIN32
+ /**
+ * @brief
+ *
+ * @param nfolder
+ * @param iscreate
+ *
+ * @return
+ */
+ std::string get_special_folder_path(int nfolder, bool iscreate);
+#endif
+
/*! \brief Returns the OS version string
*
* \details This is a wrapper around the primitives
diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h
index e26945086..e5df844e7 100644
--- a/src/cryptonote_config.h
+++ b/src/cryptonote_config.h
@@ -51,6 +51,7 @@
// MONEY_SUPPLY - total number coins to be generated
#define MONEY_SUPPLY ((uint64_t)(-1))
#define EMISSION_SPEED_FACTOR (20)
+#define FINAL_SUBSIDY_PER_MINUTE ((uint64_t)300000000000) // 3 * pow(10, 11)
#define CRYPTONOTE_REWARD_BLOCKS_WINDOW 100
#define CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE 20000 //size of block (bytes) after which reward for block calculated using block size
@@ -61,9 +62,6 @@
#define FEE_PER_KB ((uint64_t)10000000000) // pow(10, 10)
-// temporarily to allow backward compatibility during the switch to per-kb
-//#define MINING_ALLOWED_LEGACY_FEE ((uint64_t)100000000000) // pow(10, 11)
-
#define ORPHANED_BLOCKS_MAX_COUNT 100
diff --git a/src/cryptonote_core/blockchain_storage.cpp b/src/cryptonote_core/blockchain_storage.cpp
index 4e669daa5..136d4f1d1 100644
--- a/src/cryptonote_core/blockchain_storage.cpp
+++ b/src/cryptonote_core/blockchain_storage.cpp
@@ -89,6 +89,7 @@ bool blockchain_storage::init(const std::string& config_folder, bool testnet)
{
CRITICAL_REGION_LOCAL(m_blockchain_lock);
m_config_folder = config_folder;
+ m_testnet = testnet;
LOG_PRINT_L0("Loading blockchain...");
const std::string filename = m_config_folder + "/" CRYPTONOTE_BLOCKCHAINDATA_FILENAME;
if(tools::unserialize_obj_from_file(*this, filename))
@@ -1365,7 +1366,7 @@ bool blockchain_storage::pop_transaction_from_global_index(const transaction& tx
return true;
}
//------------------------------------------------------------------
-bool blockchain_storage::add_transaction_from_block(const transaction& tx, const crypto::hash& tx_id, const crypto::hash& bl_id, uint64_t bl_height)
+bool blockchain_storage::add_transaction_from_block(const transaction& tx, const crypto::hash& tx_id, const crypto::hash& bl_id, uint64_t bl_height, size_t blob_size)
{
CRITICAL_REGION_LOCAL(m_blockchain_lock);
struct add_transaction_input_visitor: public boost::static_visitor<bool>
@@ -1404,6 +1405,7 @@ bool blockchain_storage::add_transaction_from_block(const transaction& tx, const
}
transaction_chain_entry ch_e;
ch_e.m_keeper_block_height = bl_height;
+ ch_e.m_blob_size = blob_size;
ch_e.tx = tx;
auto i_r = m_transactions.insert(std::pair<crypto::hash, transaction_chain_entry>(tx_id, ch_e));
if(!i_r.second)
@@ -1669,10 +1671,12 @@ bool blockchain_storage::handle_block_to_main_chain(const block& bl, const crypt
bvc.m_verifivation_failed = true;
return false;
}
- size_t coinbase_blob_size = get_object_blobsize(bl.miner_tx);
+ crypto::hash coinbase_hash = null_hash;
+ size_t coinbase_blob_size = 0;
+ get_transaction_hash(bl.miner_tx, coinbase_hash, coinbase_blob_size);
size_t cumulative_block_size = coinbase_blob_size;
//process transactions
- if(!add_transaction_from_block(bl.miner_tx, get_transaction_hash(bl.miner_tx), id, get_current_blockchain_height()))
+ if(!add_transaction_from_block(bl.miner_tx, coinbase_hash, id, get_current_blockchain_height(), coinbase_blob_size))
{
LOG_PRINT_L1("Block with id: " << id << " failed to add transaction to blockchain storage");
bvc.m_verifivation_failed = true;
@@ -1706,7 +1710,7 @@ bool blockchain_storage::handle_block_to_main_chain(const block& bl, const crypt
return false;
}
- if(!add_transaction_from_block(tx, tx_id, id, get_current_blockchain_height()))
+ if(!add_transaction_from_block(tx, tx_id, id, get_current_blockchain_height(), blob_size))
{
LOG_PRINT_L1("Block with id: " << id << " failed to add transaction to blockchain storage");
cryptonote::tx_verification_context tvc = AUTO_VAL_INIT(tvc);
@@ -1736,7 +1740,14 @@ bool blockchain_storage::handle_block_to_main_chain(const block& bl, const crypt
bei.bl = bl;
bei.block_cumulative_size = cumulative_block_size;
bei.cumulative_difficulty = current_diffic;
- bei.already_generated_coins = already_generated_coins + base_reward;
+
+ // In the "tail" state when the minimum subsidy (implemented in get_block_reward) is in effect, the number of
+ // coins will eventually exceed MONEY_SUPPLY and overflow a uint64. To prevent overflow, cap already_generated_coins
+ // at MONEY_SUPPLY. already_generated_coins is only used to compute the block subsidy and MONEY_SUPPLY yields a
+ // subsidy of 0 under the base formula and therefore the minimum subsidy >0 in the tail state.
+
+ bei.already_generated_coins = base_reward < (MONEY_SUPPLY-already_generated_coins) ? already_generated_coins + base_reward : MONEY_SUPPLY;
+
if(m_blocks.size())
bei.cumulative_difficulty += m_blocks.back().cumulative_difficulty;
@@ -1862,7 +1873,7 @@ bool blockchain_storage::update_checkpoints(const std::string& file_path, bool c
else if (check_dns)
{
checkpoints dns_points;
- cryptonote::load_checkpoints_from_dns(dns_points);
+ cryptonote::load_checkpoints_from_dns(dns_points, m_testnet);
if (m_checkpoints.check_for_conflicts(dns_points))
{
check_against_checkpoints(dns_points, false);
diff --git a/src/cryptonote_core/blockchain_storage.h b/src/cryptonote_core/blockchain_storage.h
index 38bdfbce7..505ed4574 100644
--- a/src/cryptonote_core/blockchain_storage.h
+++ b/src/cryptonote_core/blockchain_storage.h
@@ -219,6 +219,7 @@ namespace cryptonote
std::atomic<bool> m_is_blockchain_storing;
bool m_enforce_dns_checkpoints;
+ bool m_testnet;
bool switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& alt_chain, bool discard_disconnected_chain);
bool pop_block_from_blockchain();
@@ -234,7 +235,7 @@ namespace cryptonote
bool validate_miner_transaction(const block& b, size_t cumulative_block_size, uint64_t fee, uint64_t& base_reward, uint64_t already_generated_coins);
bool validate_transaction(const block& b, uint64_t height, const transaction& tx);
bool rollback_blockchain_switching(std::list<block>& original_chain, size_t rollback_height);
- bool add_transaction_from_block(const transaction& tx, const crypto::hash& tx_id, const crypto::hash& bl_id, uint64_t bl_height);
+ bool add_transaction_from_block(const transaction& tx, const crypto::hash& tx_id, const crypto::hash& bl_id, uint64_t bl_height, size_t blob_size);
bool push_transaction_to_global_outs_index(const transaction& tx, const crypto::hash& tx_id, std::vector<uint64_t>& global_indexes);
bool pop_transaction_from_global_index(const transaction& tx, const crypto::hash& tx_id);
bool get_last_n_blocks_sizes(std::vector<size_t>& sz, size_t count);
diff --git a/src/cryptonote_core/checkpoints_create.cpp b/src/cryptonote_core/checkpoints_create.cpp
index 31ae9655b..43f926682 100644
--- a/src/cryptonote_core/checkpoints_create.cpp
+++ b/src/cryptonote_core/checkpoints_create.cpp
@@ -76,6 +76,7 @@ bool create_checkpoints(cryptonote::checkpoints& checkpoints)
ADD_CHECKPOINT(231350, "b5add137199b820e1ea26640e5c3e121fd85faa86a1e39cf7e6cc097bdeb1131");
ADD_CHECKPOINT(232150, "955de8e6b6508af2c24f7334f97beeea651d78e9ade3ab18fec3763be3201aa8");
ADD_CHECKPOINT(249380, "654fb0a81ce3e5caf7e3264a70f447d4bd07586c08fa50f6638cc54da0a52b2d");
+ ADD_CHECKPOINT(460000, "75037a7aed3e765db96c75bcf908f59d690a5f3390baebb9edeafd336a1c4831");
return true;
}
@@ -112,7 +113,7 @@ bool load_checkpoints_from_json(cryptonote::checkpoints& checkpoints, std::strin
return true;
}
-bool load_checkpoints_from_dns(cryptonote::checkpoints& checkpoints)
+bool load_checkpoints_from_dns(cryptonote::checkpoints& checkpoints, bool testnet)
{
// All four MoneroPulse domains have DNSSEC on and valid
static const std::vector<std::string> dns_urls = { "checkpoints.moneropulse.se"
@@ -120,6 +121,12 @@ bool load_checkpoints_from_dns(cryptonote::checkpoints& checkpoints)
, "checkpoints.moneropulse.net"
, "checkpoints.moneropulse.co"
};
+
+ static const std::vector<std::string> testnet_dns_urls = { "testpoints.moneropulse.se"
+ , "testpoints.moneropulse.org"
+ , "testpoints.moneropulse.net"
+ , "testpoints.moneropulse.co"
+ };
bool avail, valid;
std::vector<std::string> records;
@@ -131,14 +138,34 @@ bool load_checkpoints_from_dns(cryptonote::checkpoints& checkpoints)
size_t cur_index = first_index;
do
{
- records = tools::DNSResolver::instance().get_txt_record(dns_urls[cur_index], avail, valid);
- if (records.size() == 0 || (avail && !valid))
+ std::string url;
+ if (testnet)
+ {
+ url = testnet_dns_urls[cur_index];
+ }
+ else
+ {
+ url = dns_urls[cur_index];
+ }
+
+ records = tools::DNSResolver::instance().get_txt_record(url, avail, valid);
+ if (!avail)
+ {
+ LOG_PRINT_L2("DNSSEC not available for checkpoint update at URL: " << url << ", skipping.");
+ }
+ if (!valid)
+ {
+ LOG_PRINT_L2("DNSSEC validation failed for checkpoint update at URL: " << url << ", skipping.");
+ }
+
+ if (records.size() == 0 || !avail || !valid)
{
cur_index++;
if (cur_index == dns_urls.size())
{
cur_index = 0;
}
+ records.clear();
continue;
}
break;
@@ -146,13 +173,7 @@ bool load_checkpoints_from_dns(cryptonote::checkpoints& checkpoints)
if (records.size() == 0)
{
- LOG_PRINT_L1("Fetching MoneroPulse checkpoints failed, no TXT records available.");
- return true;
- }
-
- if (avail && !valid)
- {
- LOG_PRINT_L0("WARNING: MoneroPulse failed DNSSEC validation and/or returned no records");
+ LOG_PRINT_L0("WARNING: All MoneroPulse checkpoint URLs failed DNSSEC validation and/or returned no records");
return true;
}
diff --git a/src/cryptonote_core/checkpoints_create.h b/src/cryptonote_core/checkpoints_create.h
index 569d437cf..8422e2b33 100644
--- a/src/cryptonote_core/checkpoints_create.h
+++ b/src/cryptonote_core/checkpoints_create.h
@@ -42,7 +42,7 @@ namespace cryptonote
bool create_checkpoints(cryptonote::checkpoints& checkpoints);
bool load_checkpoints_from_json(cryptonote::checkpoints& checkpoints, std::string json_hashfile_fullpath);
- bool load_checkpoints_from_dns(cryptonote::checkpoints& checkpoints);
+ bool load_checkpoints_from_dns(cryptonote::checkpoints& checkpoints, bool testnet = false);
bool load_new_checkpoints(cryptonote::checkpoints& checkpoints, std::string json_hashfile_fullpath);
} // namespace cryptonote
diff --git a/src/cryptonote_core/cryptonote_basic_impl.cpp b/src/cryptonote_core/cryptonote_basic_impl.cpp
index a7416a7e4..f2d862e57 100644
--- a/src/cryptonote_core/cryptonote_basic_impl.cpp
+++ b/src/cryptonote_core/cryptonote_basic_impl.cpp
@@ -60,6 +60,10 @@ namespace cryptonote {
//-----------------------------------------------------------------------------------------------
bool get_block_reward(size_t median_size, size_t current_block_size, uint64_t already_generated_coins, uint64_t &reward) {
uint64_t base_reward = (MONEY_SUPPLY - already_generated_coins) >> EMISSION_SPEED_FACTOR;
+ if (base_reward < FINAL_SUBSIDY_PER_MINUTE)
+ {
+ base_reward = FINAL_SUBSIDY_PER_MINUTE;
+ }
//make it soft
if (median_size < CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE) {
diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp
index 49ce63065..505feb563 100644
--- a/src/cryptonote_core/cryptonote_core.cpp
+++ b/src/cryptonote_core/cryptonote_core.cpp
@@ -42,6 +42,8 @@ using namespace epee;
#include "cryptonote_format_utils.h"
#include "misc_language.h"
#include <csignal>
+#include "daemon/command_line_args.h"
+#include "cryptonote_core/checkpoints_create.h"
DISABLE_VS_WARNINGS(4355)
@@ -108,14 +110,41 @@ namespace cryptonote
return res;
}
//-----------------------------------------------------------------------------------
+ void core::stop()
+ {
+ graceful_exit();
+ }
+ //-----------------------------------------------------------------------------------
void core::init_options(boost::program_options::options_description& /*desc*/)
{
}
//-----------------------------------------------------------------------------------------------
- bool core::handle_command_line(const boost::program_options::variables_map& vm, bool testnet)
+ bool core::handle_command_line(const boost::program_options::variables_map& vm)
{
- auto data_dir_arg = testnet ? command_line::arg_testnet_data_dir : command_line::arg_data_dir;
+ m_testnet = command_line::get_arg(vm, daemon_args::arg_testnet_on);
+
+ auto data_dir_arg = m_testnet ? command_line::arg_testnet_data_dir : command_line::arg_data_dir;
m_config_folder = command_line::get_arg(vm, data_dir_arg);
+
+ auto data_dir = boost::filesystem::path(m_config_folder);
+
+ if (!m_testnet)
+ {
+ cryptonote::checkpoints checkpoints;
+ if (!cryptonote::create_checkpoints(checkpoints))
+ {
+ throw std::runtime_error("Failed to initialize checkpoints");
+ }
+ set_checkpoints(std::move(checkpoints));
+
+ boost::filesystem::path json(JSON_HASH_FILE_NAME);
+ boost::filesystem::path checkpoint_json_hashfile_fullpath = data_dir / json;
+
+ set_checkpoints_file_path(checkpoint_json_hashfile_fullpath.string());
+ }
+
+
+ set_enforce_dns_checkpoints(command_line::get_arg(vm, daemon_args::arg_dns_checkpoints));
return true;
}
//-----------------------------------------------------------------------------------------------
@@ -154,21 +183,21 @@ namespace cryptonote
return m_blockchain_storage.get_alternative_blocks_count();
}
//-----------------------------------------------------------------------------------------------
- bool core::init(const boost::program_options::variables_map& vm, bool testnet)
+ bool core::init(const boost::program_options::variables_map& vm)
{
- bool r = handle_command_line(vm, testnet);
+ bool r = handle_command_line(vm);
r = m_mempool.init(m_config_folder);
CHECK_AND_ASSERT_MES(r, false, "Failed to initialize memory pool");
- r = m_blockchain_storage.init(m_config_folder, testnet);
+ r = m_blockchain_storage.init(m_config_folder, m_testnet);
CHECK_AND_ASSERT_MES(r, false, "Failed to initialize blockchain storage");
// load json & DNS checkpoints, and verify them
// with respect to what blocks we already have
CHECK_AND_ASSERT_MES(update_checkpoints(), false, "One or more checkpoints loaded from json or dns conflicted with existing checkpoints.");
- r = m_miner.init(vm, testnet);
+ r = m_miner.init(vm, m_testnet);
CHECK_AND_ASSERT_MES(r, false, "Failed to initialize blockchain storage");
return load_state_data();
diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h
index 795a6d214..bb53ecb81 100644
--- a/src/cryptonote_core/cryptonote_core.h
+++ b/src/cryptonote_core/cryptonote_core.h
@@ -72,7 +72,7 @@ namespace cryptonote
miner& get_miner(){return m_miner;}
static void init_options(boost::program_options::options_description& desc);
- bool init(const boost::program_options::variables_map& vm, bool testnet);
+ bool init(const boost::program_options::variables_map& vm);
bool set_genesis_block(const block& b);
bool deinit();
static void set_fast_exit();
@@ -131,6 +131,8 @@ namespace cryptonote
bool update_checkpoints();
+ void stop();
+
private:
bool add_new_tx(const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block);
bool add_new_tx(const transaction& tx, tx_verification_context& tvc, bool keeped_by_block);
@@ -148,7 +150,7 @@ namespace cryptonote
bool check_tx_ring_signature(const txin_to_key& tx, const crypto::hash& tx_prefix_hash, const std::vector<crypto::signature>& sig);
bool is_tx_spendtime_unlocked(uint64_t unlock_time);
bool update_miner_block_template();
- bool handle_command_line(const boost::program_options::variables_map& vm, bool testnet);
+ bool handle_command_line(const boost::program_options::variables_map& vm);
bool on_update_blocktemplate_interval();
bool check_tx_inputs_keyimages_diff(const transaction& tx);
void graceful_exit();
@@ -171,6 +173,7 @@ namespace cryptonote
uint64_t m_target_blockchain_height;
+ bool m_testnet;
std::string m_checkpoints_path;
time_t m_last_dns_checkpoints_update;
time_t m_last_json_checkpoints_update;
diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txt
index 4a0dcb148..4de8b82b8 100644
--- a/src/daemon/CMakeLists.txt
+++ b/src/daemon/CMakeLists.txt
@@ -27,12 +27,43 @@
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set(daemon_sources
- daemon.cpp)
+ command_parser_executor.cpp
+ command_server.cpp
+ daemon.cpp
+ executor.cpp
+ main.cpp
+ rpc_command_executor.cpp
+)
set(daemon_headers)
set(daemon_private_headers
- daemon_commands_handler.h)
+ command_parser_executor.h
+ command_server.h
+ core.h
+ daemon.h
+ daemon_commands_handler.h
+ executor.h
+ p2p.h
+ protocol.h
+ rpc.h
+ rpc_command_executor.h
+
+ # cryptonote_protocol
+ ../cryptonote_protocol/blobdatatype.h
+ ../cryptonote_protocol/cryptonote_protocol_defs.h
+ ../cryptonote_protocol/cryptonote_protocol_handler.h
+ ../cryptonote_protocol/cryptonote_protocol_handler.inl
+ ../cryptonote_protocol/cryptonote_protocol_handler_common.h
+
+ # p2p
+ ../p2p/net_node.h
+ ../p2p/net_node.inl
+ ../p2p/net_node_common.h
+ ../p2p/net_peerlist.h
+ ../p2p/net_peerlist_boost_serialization.h
+ ../p2p/p2p_protocol_defs.h
+ ../p2p/stdafx.h)
bitmonero_private_headers(daemon
${daemon_private_headers})
@@ -46,9 +77,7 @@ target_link_libraries(daemon
cryptonote_core
crypto
common
- otshell_utils
- p2p
- cryptonote_protocol
+ daemonizer
${Boost_CHRONO_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
diff --git a/src/daemon/command_line_args.h b/src/daemon/command_line_args.h
new file mode 100644
index 000000000..bcf599128
--- /dev/null
+++ b/src/daemon/command_line_args.h
@@ -0,0 +1,76 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef DAEMON_COMMAND_LINE_ARGS_H
+#define DAEMON_COMMAND_LINE_ARGS_H
+
+#include "common/command_line.h"
+#include "cryptonote_config.h"
+#include <boost/program_options.hpp>
+
+namespace daemon_args
+{
+ std::string const WINDOWS_SERVICE_NAME = "Monero Daemon";
+
+ const command_line::arg_descriptor<std::string> arg_config_file = {
+ "config-file"
+ , "Specify configuration file"
+ , std::string(CRYPTONOTE_NAME ".conf")
+ };
+ const command_line::arg_descriptor<std::string> arg_log_file = {
+ "log-file"
+ , "Specify log file"
+ , ""
+ };
+ const command_line::arg_descriptor<int> arg_log_level = {
+ "log-level"
+ , ""
+ , LOG_LEVEL_0
+ };
+ const command_line::arg_descriptor<std::vector<std::string>> arg_command = {
+ "daemon_command"
+ , "Hidden"
+ };
+ const command_line::arg_descriptor<bool> arg_os_version = {
+ "os-version"
+ , "OS for which this executable was compiled"
+ };
+ const command_line::arg_descriptor<bool> arg_testnet_on = {
+ "testnet"
+ , "Run on testnet. The wallet must be launched with --testnet flag."
+ , false
+ };
+ const command_line::arg_descriptor<bool> arg_dns_checkpoints = {
+ "enforce-dns-checkpointing"
+ , "checkpoints from DNS server will be enforced"
+ , false
+ };
+
+} // namespace daemon_args
+
+#endif // DAEMON_COMMAND_LINE_ARGS_H
diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp
new file mode 100644
index 000000000..8f023da9a
--- /dev/null
+++ b/src/daemon/command_parser_executor.cpp
@@ -0,0 +1,299 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "cryptonote_core/cryptonote_basic_impl.h"
+#include "daemon/command_parser_executor.h"
+
+namespace daemonize {
+
+t_command_parser_executor::t_command_parser_executor(
+ uint32_t ip
+ , uint16_t port
+ , bool is_rpc
+ , cryptonote::core_rpc_server* rpc_server
+ )
+ : m_executor(ip, port, is_rpc, rpc_server)
+{}
+
+bool t_command_parser_executor::print_peer_list(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.print_peer_list();
+}
+
+bool t_command_parser_executor::save_blockchain(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.save_blockchain();
+}
+
+bool t_command_parser_executor::show_hash_rate(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.show_hash_rate();
+}
+
+bool t_command_parser_executor::hide_hash_rate(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.hide_hash_rate();
+}
+
+bool t_command_parser_executor::show_difficulty(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.show_difficulty();
+}
+
+bool t_command_parser_executor::print_connections(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.print_connections();
+}
+
+bool t_command_parser_executor::print_blockchain_info(const std::vector<std::string>& args)
+{
+ if(!args.size())
+ {
+ std::cout << "need block index parameter" << std::endl;
+ return false;
+ }
+ uint64_t start_index = 0;
+ uint64_t end_index = 0;
+ if(!epee::string_tools::get_xtype_from_string(start_index, args[0]))
+ {
+ std::cout << "wrong starter block index parameter" << std::endl;
+ return false;
+ }
+ if(args.size() >1 && !epee::string_tools::get_xtype_from_string(end_index, args[1]))
+ {
+ std::cout << "wrong end block index parameter" << std::endl;
+ return false;
+ }
+
+ return m_executor.print_blockchain_info(start_index, end_index);
+}
+
+bool t_command_parser_executor::set_log_level(const std::vector<std::string>& args)
+{
+ if(args.size() != 1)
+ {
+ std::cout << "use: set_log <log_level_number_0-4>" << std::endl;
+ return true;
+ }
+
+ uint16_t l = 0;
+ if(!epee::string_tools::get_xtype_from_string(l, args[0]))
+ {
+ std::cout << "wrong number format, use: set_log <log_level_number_0-4>" << std::endl;
+ return true;
+ }
+
+ if(LOG_LEVEL_4 < l)
+ {
+ std::cout << "wrong number range, use: set_log <log_level_number_0-4>" << std::endl;
+ return true;
+ }
+
+ return m_executor.set_log_level(l);
+}
+
+bool t_command_parser_executor::print_height(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.print_height();
+}
+
+bool t_command_parser_executor::print_block(const std::vector<std::string>& args)
+{
+ if (args.empty())
+ {
+ std::cout << "expected: print_block (<block_hash> | <block_height>)" << std::endl;
+ return false;
+ }
+
+ const std::string& arg = args.front();
+ try
+ {
+ uint64_t height = boost::lexical_cast<uint64_t>(arg);
+ return m_executor.print_block_by_height(height);
+ }
+ catch (boost::bad_lexical_cast&)
+ {
+ crypto::hash block_hash;
+ if (parse_hash256(arg, block_hash))
+ {
+ return m_executor.print_block_by_hash(block_hash);
+ }
+ }
+
+ return false;
+}
+
+bool t_command_parser_executor::print_transaction(const std::vector<std::string>& args)
+{
+ if (args.empty())
+ {
+ std::cout << "expected: print_tx <transaction hash>" << std::endl;
+ return true;
+ }
+
+ const std::string& str_hash = args.front();
+ crypto::hash tx_hash;
+ if (parse_hash256(str_hash, tx_hash))
+ {
+ m_executor.print_transaction(tx_hash);
+ }
+
+ return true;
+}
+
+bool t_command_parser_executor::print_transaction_pool_long(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.print_transaction_pool_long();
+}
+
+bool t_command_parser_executor::print_transaction_pool_short(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.print_transaction_pool_short();
+}
+
+bool t_command_parser_executor::start_mining(const std::vector<std::string>& args)
+{
+ if(!args.size())
+ {
+ std::cout << "Please specify a wallet address to mine for: start_mining <addr> [threads=1]" << std::endl;
+ return true;
+ }
+
+ cryptonote::account_public_address adr;
+ if(!cryptonote::get_account_address_from_str(adr, false, args.front()))
+ {
+ if(!cryptonote::get_account_address_from_str(adr, true, args.front()))
+ {
+ std::cout << "target account address has wrong format" << std::endl;
+ return true;
+ }
+ std::cout << "Mining to a testnet address, make sure this is intentional!" << std::endl;
+ }
+ uint64_t threads_count = 1;
+ if(args.size() > 2)
+ {
+ return false;
+ }
+ else if(args.size() == 2)
+ {
+ bool ok = epee::string_tools::get_xtype_from_string(threads_count, args[1]);
+ threads_count = (ok && 0 < threads_count) ? threads_count : 1;
+ }
+
+ m_executor.start_mining(adr, threads_count);
+
+ return true;
+}
+
+bool t_command_parser_executor::stop_mining(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.stop_mining();
+}
+
+bool t_command_parser_executor::stop_daemon(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.stop_daemon();
+}
+
+bool t_command_parser_executor::print_status(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.print_status();
+}
+
+bool t_command_parser_executor::set_limit(const std::vector<std::string>& args)
+{
+ if(args.size()!=1) return false;
+ int limit;
+ try {
+ limit = std::stoi(args[0]);
+ }
+ catch(std::invalid_argument& ex) {
+ return false;
+ }
+ if (limit==-1) limit=128;
+ limit *= 1024;
+
+ return m_executor.set_limit(limit);
+}
+
+bool t_command_parser_executor::set_limit_up(const std::vector<std::string>& args)
+{
+ if(args.size()!=1) return false;
+ int limit;
+ try {
+ limit = std::stoi(args[0]);
+ }
+ catch(std::invalid_argument& ex) {
+ return false;
+ }
+ if (limit==-1) limit=128;
+ limit *= 1024;
+
+ return m_executor.set_limit_up(limit);
+}
+
+bool t_command_parser_executor::set_limit_down(const std::vector<std::string>& args)
+{
+ if(args.size()!=1) return false;
+ int limit;
+ try {
+ limit = std::stoi(args[0]);
+ }
+ catch(std::invalid_argument& ex) {
+ return false;
+ }
+ if (limit==-1) limit=128;
+ limit *= 1024;
+
+ return m_executor.set_limit_down(limit);
+}
+} // namespace daemonize
diff --git a/src/daemon/command_parser_executor.h b/src/daemon/command_parser_executor.h
new file mode 100644
index 000000000..07d2e70a9
--- /dev/null
+++ b/src/daemon/command_parser_executor.h
@@ -0,0 +1,98 @@
+/**
+@file
+@details
+
+@image html images/other/runtime-commands.png
+
+*/
+
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#pragma once
+
+#include "daemon/rpc_command_executor.h"
+#include "rpc/core_rpc_server.h"
+
+namespace daemonize {
+
+class t_command_parser_executor final
+{
+private:
+ t_rpc_command_executor m_executor;
+public:
+ t_command_parser_executor(
+ uint32_t ip
+ , uint16_t port
+ , bool is_rpc
+ , cryptonote::core_rpc_server* rpc_server = NULL
+ );
+
+ bool print_peer_list(const std::vector<std::string>& args);
+
+ bool save_blockchain(const std::vector<std::string>& args);
+
+ bool show_hash_rate(const std::vector<std::string>& args);
+
+ bool hide_hash_rate(const std::vector<std::string>& args);
+
+ bool show_difficulty(const std::vector<std::string>& args);
+
+ bool print_connections(const std::vector<std::string>& args);
+
+ bool print_blockchain_info(const std::vector<std::string>& args);
+
+ bool set_log_level(const std::vector<std::string>& args);
+
+ bool print_height(const std::vector<std::string>& args);
+
+ bool print_block(const std::vector<std::string>& args);
+
+ bool print_transaction(const std::vector<std::string>& args);
+
+ bool print_transaction_pool_long(const std::vector<std::string>& args);
+
+ bool print_transaction_pool_short(const std::vector<std::string>& args);
+
+ bool start_mining(const std::vector<std::string>& args);
+
+ bool stop_mining(const std::vector<std::string>& args);
+
+ bool stop_daemon(const std::vector<std::string>& args);
+
+ bool print_status(const std::vector<std::string>& args);
+
+ bool set_limit(const std::vector<std::string>& args);
+
+ bool set_limit_up(const std::vector<std::string>& args);
+
+ bool set_limit_down(const std::vector<std::string>& args);
+
+};
+
+} // namespace daemonize
diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp
new file mode 100644
index 000000000..601b12d57
--- /dev/null
+++ b/src/daemon/command_server.cpp
@@ -0,0 +1,213 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "cryptonote_config.h"
+#include "version.h"
+#include "daemon/command_server.h"
+
+namespace daemonize {
+
+namespace p = std::placeholders;
+
+t_command_server::t_command_server(
+ uint32_t ip
+ , uint16_t port
+ , bool is_rpc
+ , cryptonote::core_rpc_server* rpc_server
+ )
+ : m_parser(ip, port, is_rpc, rpc_server)
+ , m_command_lookup()
+ , m_is_rpc(is_rpc)
+{
+ m_command_lookup.set_handler(
+ "q"
+ , [] (const std::vector<std::string>& args) {return true;}
+ , "ignored"
+ );
+ m_command_lookup.set_handler(
+ "help"
+ , std::bind(&t_command_server::help, this, p::_1)
+ , "Show this help"
+ );
+ m_command_lookup.set_handler(
+ "print_height"
+ , std::bind(&t_command_parser_executor::print_height, &m_parser, p::_1)
+ , "Print local blockchain height"
+ );
+ m_command_lookup.set_handler(
+ "print_pl"
+ , std::bind(&t_command_parser_executor::print_peer_list, &m_parser, p::_1)
+ , "Print peer list"
+ );
+ m_command_lookup.set_handler(
+ "print_cn"
+ , std::bind(&t_command_parser_executor::print_connections, &m_parser, p::_1)
+ , "Print connections"
+ );
+ m_command_lookup.set_handler(
+ "print_bc"
+ , std::bind(&t_command_parser_executor::print_blockchain_info, &m_parser, p::_1)
+ , "Print blockchain info in a given blocks range, print_bc <begin_height> [<end_height>]"
+ );
+ m_command_lookup.set_handler(
+ "print_block"
+ , std::bind(&t_command_parser_executor::print_block, &m_parser, p::_1)
+ , "Print block, print_block <block_hash> | <block_height>"
+ );
+ m_command_lookup.set_handler(
+ "print_tx"
+ , std::bind(&t_command_parser_executor::print_transaction, &m_parser, p::_1)
+ , "Print transaction, print_tx <transaction_hash>"
+ );
+ m_command_lookup.set_handler(
+ "start_mining"
+ , std::bind(&t_command_parser_executor::start_mining, &m_parser, p::_1)
+ , "Start mining for specified address, start_mining <addr> [threads=1]"
+ );
+ m_command_lookup.set_handler(
+ "stop_mining"
+ , std::bind(&t_command_parser_executor::stop_mining, &m_parser, p::_1)
+ , "Stop mining"
+ );
+ m_command_lookup.set_handler(
+ "print_pool"
+ , std::bind(&t_command_parser_executor::print_transaction_pool_long, &m_parser, p::_1)
+ , "Print transaction pool (long format)"
+ );
+ m_command_lookup.set_handler(
+ "print_pool_sh"
+ , std::bind(&t_command_parser_executor::print_transaction_pool_short, &m_parser, p::_1)
+ , "Print transaction pool (short format)"
+ );
+ m_command_lookup.set_handler(
+ "show_hr"
+ , std::bind(&t_command_parser_executor::show_hash_rate, &m_parser, p::_1)
+ , "Start showing hash rate"
+ );
+ m_command_lookup.set_handler(
+ "hide_hr"
+ , std::bind(&t_command_parser_executor::hide_hash_rate, &m_parser, p::_1)
+ , "Stop showing hash rate"
+ );
+ m_command_lookup.set_handler(
+ "save"
+ , std::bind(&t_command_parser_executor::save_blockchain, &m_parser, p::_1)
+ , "Save blockchain"
+ );
+ m_command_lookup.set_handler(
+ "set_log"
+ , std::bind(&t_command_parser_executor::set_log_level, &m_parser, p::_1)
+ , "set_log <level> - Change current log detalization level, <level> is a number 0-4"
+ );
+ m_command_lookup.set_handler(
+ "diff"
+ , std::bind(&t_command_parser_executor::show_difficulty, &m_parser, p::_1)
+ , "Show difficulty"
+ );
+ m_command_lookup.set_handler(
+ "stop_daemon"
+ , std::bind(&t_command_parser_executor::stop_daemon, &m_parser, p::_1)
+ , "Stop the daemon"
+ );
+ m_command_lookup.set_handler(
+ "exit"
+ , std::bind(&t_command_parser_executor::stop_daemon, &m_parser, p::_1)
+ , "Stop the daemon"
+ );
+ m_command_lookup.set_handler(
+ "print_status"
+ , std::bind(&t_command_parser_executor::print_status, &m_parser, p::_1)
+ , "Prints daemon status"
+ );
+ m_command_lookup.set_handler(
+ "limit"
+ , std::bind(&t_command_parser_executor::set_limit, &m_parser, p::_1)
+ , "limit <kB/s> - Set download and upload limit"
+ );
+ m_command_lookup.set_handler(
+ "limit-up"
+ , std::bind(&t_command_parser_executor::set_limit_up, &m_parser, p::_1)
+ , "limit <kB/s> - Set upload limit"
+ );
+ m_command_lookup.set_handler(
+ "limit-down"
+ , std::bind(&t_command_parser_executor::set_limit_down, &m_parser, p::_1)
+ , "limit <kB/s> - Set download limit"
+ );
+}
+
+bool t_command_server::process_command_str(const std::string& cmd)
+{
+ return m_command_lookup.process_command_str(cmd);
+}
+
+bool t_command_server::process_command_vec(const std::vector<std::string>& cmd)
+{
+ bool result = m_command_lookup.process_command_vec(cmd);
+ if (!result)
+ {
+ help(std::vector<std::string>());
+ }
+ return result;
+}
+
+bool t_command_server::start_handling()
+{
+ if (m_is_rpc) return false;
+
+ m_command_lookup.start_handling("", get_commands_str());
+
+ return true;
+}
+
+void t_command_server::stop_handling()
+{
+ if (m_is_rpc) return;
+
+ m_command_lookup.stop_handling();
+}
+
+bool t_command_server::help(const std::vector<std::string>& args)
+{
+ std::cout << get_commands_str() << std::endl;
+ return true;
+}
+
+std::string t_command_server::get_commands_str()
+{
+ std::stringstream ss;
+ ss << CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL << std::endl;
+ ss << "Commands: " << std::endl;
+ std::string usage = m_command_lookup.get_usage();
+ boost::replace_all(usage, "\n", "\n ");
+ usage.insert(0, " ");
+ ss << usage << std::endl;
+ return ss.str();
+}
+
+} // namespace daemonize
diff --git a/src/daemon/command_server.h b/src/daemon/command_server.h
new file mode 100644
index 000000000..494f44d8b
--- /dev/null
+++ b/src/daemon/command_server.h
@@ -0,0 +1,75 @@
+/**
+@file
+@details
+
+
+Passing RPC commands:
+
+@image html images/other/runtime-commands.png
+
+*/
+
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#pragma once
+
+#include "console_handler.h"
+#include "daemon/command_parser_executor.h"
+
+namespace daemonize {
+
+class t_command_server {
+private:
+ t_command_parser_executor m_parser;
+ epee::console_handlers_binder m_command_lookup;
+ bool m_is_rpc;
+
+public:
+ t_command_server(
+ uint32_t ip
+ , uint16_t port
+ , bool is_rpc = true
+ , cryptonote::core_rpc_server* rpc_server = NULL
+ );
+
+ bool process_command_str(const std::string& cmd);
+
+ bool process_command_vec(const std::vector<std::string>& cmd);
+
+ bool start_handling();
+
+ void stop_handling();
+
+private:
+ bool help(const std::vector<std::string>& args);
+
+ std::string get_commands_str();
+};
+
+} // namespace daemonize
diff --git a/src/daemon/core.h b/src/daemon/core.h
new file mode 100644
index 000000000..6564e5314
--- /dev/null
+++ b/src/daemon/core.h
@@ -0,0 +1,99 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#pragma once
+
+#include "cryptonote_core/checkpoints_create.h"
+#include "cryptonote_core/cryptonote_core.h"
+#include "cryptonote_protocol/cryptonote_protocol_handler.h"
+#include "misc_log_ex.h"
+#include <stdexcept>
+#include <boost/program_options.hpp>
+#include "daemon/command_line_args.h"
+
+namespace daemonize
+{
+
+class t_core final
+{
+public:
+ static void init_options(boost::program_options::options_description & option_spec)
+ {
+ cryptonote::core::init_options(option_spec);
+ cryptonote::miner::init_options(option_spec);
+ }
+private:
+ typedef cryptonote::t_cryptonote_protocol_handler<cryptonote::core> t_protocol_raw;
+ cryptonote::core m_core;
+ // TEMPORARY HACK - Yes, this creates a copy, but otherwise the original
+ // variable map could go out of scope before the run method is called
+ boost::program_options::variables_map const m_vm_HACK;
+public:
+ t_core(
+ boost::program_options::variables_map const & vm
+ )
+ : m_core{nullptr}
+ , m_vm_HACK{vm}
+ {
+ }
+
+ // TODO - get rid of circular dependencies in internals
+ void set_protocol(t_protocol_raw & protocol)
+ {
+ m_core.set_cryptonote_protocol(&protocol);
+ }
+
+ void run()
+ {
+ //initialize core here
+ LOG_PRINT_L0("Initializing core...");
+ if (!m_core.init(m_vm_HACK))
+ {
+ throw std::runtime_error("Failed to initialize core");
+ }
+ LOG_PRINT_L0("Core initialized OK");
+ }
+
+ cryptonote::core & get()
+ {
+ return m_core;
+ }
+
+ ~t_core()
+ {
+ LOG_PRINT_L0("Deinitializing core...");
+ try {
+ m_core.deinit();
+ m_core.set_cryptonote_protocol(nullptr);
+ } catch (...) {
+ LOG_PRINT_L0("Failed to deinitialize core...");
+ }
+ }
+};
+
+}
diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp
index 4a93a0db8..ec12c281c 100644
--- a/src/daemon/daemon.cpp
+++ b/src/daemon/daemon.cpp
@@ -1,21 +1,21 @@
-// Copyright (c) 2014-2015, The Monero Project
-//
+// Copyright (c) 2014, The Monero Project
+//
// All rights reserved.
-//
+//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
-//
+//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
-//
+//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
-//
+//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
-//
+//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
@@ -25,305 +25,136 @@
// 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.
-//
+//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
-// node.cpp : Defines the entry point for the console application.
-// Does this file exist?
+#include "daemon/daemon.h"
-
-#include "include_base_utils.h"
+#include "common/util.h"
+#include "daemon/core.h"
+#include "daemon/p2p.h"
+#include "daemon/protocol.h"
+#include "daemon/rpc.h"
+#include "daemon/command_server.h"
+#include "misc_log_ex.h"
#include "version.h"
-#include "../../contrib/epee/include/syncobj.h"
-
-using namespace epee;
-
#include <boost/program_options.hpp>
+#include <functional>
+#include <memory>
+
+namespace daemonize {
+
+struct t_internals {
+private:
+ t_protocol protocol;
+public:
+ t_core core;
+ t_p2p p2p;
+ t_rpc rpc;
+
+ t_internals(
+ boost::program_options::variables_map const & vm
+ )
+ : core{vm}
+ , protocol{vm, core}
+ , p2p{vm, protocol}
+ , rpc{vm, core, p2p}
+ {
+ // Handle circular dependencies
+ protocol.set_p2p_endpoint(p2p.get());
+ core.set_protocol(protocol.get());
+ }
+};
-#include "crypto/hash.h"
-#include "console_handler.h"
-#include "p2p/net_node.h"
-#include "cryptonote_config.h"
-#include "cryptonote_core/checkpoints_create.h"
-#include "cryptonote_core/checkpoints.h"
-#include "cryptonote_core/cryptonote_core.h"
-#include "rpc/core_rpc_server.h"
-#include "cryptonote_protocol/cryptonote_protocol_handler.h"
-#include "daemon_commands_handler.h"
-#include "version.h"
-
-#if defined(WIN32)
-#include <crtdbg.h>
-#endif
+void t_daemon::init_options(boost::program_options::options_description & option_spec)
+{
+ t_core::init_options(option_spec);
+ t_p2p::init_options(option_spec);
+ t_rpc::init_options(option_spec);
+}
-namespace po = boost::program_options;
+t_daemon::t_daemon(
+ boost::program_options::variables_map const & vm
+ )
+ : mp_internals{new t_internals{vm}}
+{}
-unsigned int epee::g_test_dbg_lock_sleep = 0;
+t_daemon::~t_daemon() = default;
-namespace
+// MSVC is brain-dead and can't default this...
+t_daemon::t_daemon(t_daemon && other)
{
- const command_line::arg_descriptor<std::string> arg_config_file = {"config-file", "Specify configuration file", std::string(CRYPTONOTE_NAME ".conf")};
- const command_line::arg_descriptor<bool> arg_os_version = {"os-version", ""};
- const command_line::arg_descriptor<std::string> arg_log_file = {"log-file", "", ""};
- const command_line::arg_descriptor<int> arg_log_level = {"log-level", "", LOG_LEVEL_0};
- const command_line::arg_descriptor<bool> arg_console = {"no-console", "Disable daemon console commands"};
- const command_line::arg_descriptor<bool> arg_testnet_on = {
- "testnet"
- , "Run on testnet. The wallet must be launched with --testnet flag."
- , false
- };
- const command_line::arg_descriptor<bool> arg_dns_checkpoints = {"enforce-dns-checkpointing", "checkpoints from DNS server will be enforced", false};
- const command_line::arg_descriptor<bool> arg_test_drop_download = {"test-drop-download", "For net tests: in download, discard ALL blocks instead checking/saving them (very fast)"};
- const command_line::arg_descriptor<uint64_t> arg_test_drop_download_height = {"test-drop-download-height", "Like test-drop-download but disards only after around certain height", 0};
- const command_line::arg_descriptor<bool> arg_save_graph = {"save-graph", "Save data for dr monero", false};
- const command_line::arg_descriptor<int> test_dbg_lock_sleep = {"test-dbg-lock-sleep", "Sleep time in ms, defaults to 0 (off), used to debug before/after locking mutex. Values 100 to 1000 are good for tests.", 0};
+ if (this != &other)
+ {
+ mp_internals = std::move(other.mp_internals);
+ other.mp_internals.reset(nullptr);
+ }
}
-bool command_line_preprocessor(const boost::program_options::variables_map& vm)
+// or this
+t_daemon & t_daemon::operator=(t_daemon && other)
{
- bool exit = false;
- if (command_line::get_arg(vm, command_line::arg_version))
- {
- std::cout << CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL << ENDL;
- exit = true;
- }
- if (command_line::get_arg(vm, arg_os_version))
+ if (this != &other)
{
- std::cout << "OS: " << tools::get_os_version_string() << ENDL;
- exit = true;
+ mp_internals = std::move(other.mp_internals);
+ other.mp_internals.reset(nullptr);
}
+ return *this;
+}
- if (exit)
+bool t_daemon::run(bool interactive)
+{
+ if (nullptr == mp_internals)
{
- return true;
+ throw std::runtime_error{"Can't run stopped daemon"};
}
+ tools::signal_handler::install(std::bind(&daemonize::t_daemon::stop, this));
- int new_log_level = command_line::get_arg(vm, arg_log_level);
- if(new_log_level < LOG_LEVEL_MIN || new_log_level > LOG_LEVEL_MAX)
+ try
{
- LOG_PRINT_L0("Wrong log level value: ");
- }
- else if (log_space::get_set_log_detalisation_level(false) != new_log_level)
- {
- log_space::get_set_log_detalisation_level(true, new_log_level);
- int otshell_utils_log_level = 100 - (new_log_level * 25);
- gCurrentLogger.setDebugLevel(otshell_utils_log_level);
- LOG_PRINT_L0("LOG_LEVEL set to " << new_log_level);
- }
-
- return false;
-}
-
-int main(int argc, char* argv[])
-{
- string_tools::set_module_name_and_folder(argv[0]);
-#ifdef WIN32
- _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
-#endif
- log_space::get_set_log_detalisation_level(true, LOG_LEVEL_0);
- log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL);
- LOG_PRINT_L0("Starting...");
-
- nOT::nUtils::cFilesystemUtils::CreateDirTree("log/dr-monero/net/");
- _warn_c("test","Starting program (a test message)");
- _warn_c("main/program","Starting program");
-
- TRY_ENTRY();
+ mp_internals->core.run();
+ mp_internals->rpc.run();
- boost::filesystem::path default_data_path {tools::get_default_data_dir()};
- boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"};
+ daemonize::t_command_server* rpc_commands;
- po::options_description desc_cmd_only("Command line options");
- po::options_description desc_cmd_sett("Command line options and settings options");
+ if (interactive)
+ {
+ rpc_commands = new daemonize::t_command_server(0, 0, false, mp_internals->rpc.get_server());
+ rpc_commands->start_handling();
+ }
- command_line::add_arg(desc_cmd_only, command_line::arg_help);
- command_line::add_arg(desc_cmd_only, command_line::arg_version);
- command_line::add_arg(desc_cmd_only, arg_os_version);
- // tools::get_default_data_dir() can't be called during static initialization
- command_line::add_arg(desc_cmd_only, command_line::arg_data_dir, default_data_path.string());
- command_line::add_arg(desc_cmd_only, command_line::arg_testnet_data_dir, default_testnet_data_path.string());
- command_line::add_arg(desc_cmd_only, arg_config_file);
+ mp_internals->p2p.run(); // blocks until p2p goes down
- command_line::add_arg(desc_cmd_sett, arg_log_file);
- command_line::add_arg(desc_cmd_sett, arg_log_level);
- command_line::add_arg(desc_cmd_sett, arg_console);
- command_line::add_arg(desc_cmd_sett, arg_testnet_on);
- command_line::add_arg(desc_cmd_sett, arg_dns_checkpoints);
- command_line::add_arg(desc_cmd_sett, arg_test_drop_download);
- command_line::add_arg(desc_cmd_sett, arg_test_drop_download_height);
- command_line::add_arg(desc_cmd_sett, arg_save_graph);
- command_line::add_arg(desc_cmd_sett, test_dbg_lock_sleep);
-
- cryptonote::core::init_options(desc_cmd_sett);
- cryptonote::core_rpc_server::init_options(desc_cmd_sett);
- nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >::init_options(desc_cmd_sett);
- cryptonote::miner::init_options(desc_cmd_sett);
-
- po::options_description desc_options("Allowed options");
- desc_options.add(desc_cmd_only).add(desc_cmd_sett);
-
- po::variables_map vm;
- bool r = command_line::handle_error_helper(desc_options, [&]()
- {
- po::store(po::parse_command_line(argc, argv, desc_options), vm);
- po::notify(vm);
+ if (interactive)
+ {
+ rpc_commands->stop_handling();
+ }
+ mp_internals->rpc.stop();
+ LOG_PRINT("Node stopped.", LOG_LEVEL_0);
return true;
- });
- if (!r)
- return 1;
-
- if (command_line::get_arg(vm, command_line::arg_help))
- {
- std::cout << CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL << ENDL << ENDL;
- std::cout << desc_options << std::endl;
- return false;
}
-
- bool testnet_mode = command_line::get_arg(vm, arg_testnet_on);
-
- auto data_dir_arg = testnet_mode ? command_line::arg_testnet_data_dir : command_line::arg_data_dir;
-
- std::string data_dir = command_line::get_arg(vm, data_dir_arg);
- tools::create_directories_if_necessary(data_dir);
- std::string config = command_line::get_arg(vm, arg_config_file);
-
- boost::filesystem::path data_dir_path(data_dir);
- boost::filesystem::path config_path(config);
- if (!config_path.has_parent_path())
- {
- config_path = data_dir_path / config_path;
- }
-
- boost::system::error_code ec;
- if (boost::filesystem::exists(config_path, ec))
+ catch (std::exception const & ex)
{
- po::store(po::parse_config_file<char>(config_path.string<std::string>().c_str(), desc_cmd_sett), vm);
+ LOG_ERROR("Uncaught exception! " << ex.what());
+ return false;
}
-
- //set up logging options
- boost::filesystem::path log_file_path(command_line::get_arg(vm, arg_log_file));
- if (log_file_path.empty())
- log_file_path = log_space::log_singletone::get_default_log_file();
- std::string log_dir;
- log_dir = log_file_path.has_parent_path() ? log_file_path.parent_path().string() : log_space::log_singletone::get_default_log_folder();
-
- log_space::log_singletone::add_logger(LOGGER_FILE, log_file_path.filename().string().c_str(), log_dir.c_str());
- LOG_PRINT_L0(CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL);
-
- if (command_line_preprocessor(vm))
+ catch (...)
{
- return 0;
- }
-
- LOG_PRINT("Module folder: " << argv[0], LOG_LEVEL_0);
-
- bool res = true;
- cryptonote::checkpoints checkpoints;
- res = cryptonote::create_checkpoints(checkpoints);
- CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize checkpoints");
- boost::filesystem::path json(JSON_HASH_FILE_NAME);
- boost::filesystem::path checkpoint_json_hashfile_fullpath = data_dir / json;
-
- //create objects and link them
- cryptonote::core ccore(NULL);
-
- // tell core if we're enforcing dns checkpoints
- bool enforce_dns = command_line::get_arg(vm, arg_dns_checkpoints);
- ccore.set_enforce_dns_checkpoints(enforce_dns);
-
- if (testnet_mode) {
- LOG_PRINT_L0("Starting in testnet mode!");
- } else {
- ccore.set_checkpoints(std::move(checkpoints));
- ccore.set_checkpoints_file_path(checkpoint_json_hashfile_fullpath.string());
+ LOG_ERROR("Uncaught exception!");
+ return false;
}
+}
- cryptonote::t_cryptonote_protocol_handler<cryptonote::core> cprotocol(ccore, NULL);
- nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> > p2psrv {
- cprotocol
- , testnet_mode ? std::move(config::testnet::NETWORK_ID) : std::move(config::NETWORK_ID)
- };
- cryptonote::core_rpc_server rpc_server {ccore, p2psrv, testnet_mode};
- cprotocol.set_p2p_endpoint(&p2psrv);
- ccore.set_cryptonote_protocol(&cprotocol);
- std::shared_ptr<daemon_cmmands_handler> dch(new daemon_cmmands_handler(p2psrv, testnet_mode));
- if(command_line::has_arg(vm, arg_save_graph))
- p2psrv.set_save_graph(true);
-
- epee::g_test_dbg_lock_sleep = command_line::get_arg(vm, test_dbg_lock_sleep);
-
- //initialize core here
- LOG_PRINT_L0("Initializing core...");
- res = ccore.init(vm, testnet_mode);
- CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize core");
- if (command_line::get_arg(vm, arg_test_drop_download))
- ccore.test_drop_download();
-
- ccore.test_drop_download_height(command_line::get_arg(vm, arg_test_drop_download_height));
- LOG_PRINT_L0("Core initialized OK");
-
- //initialize objects
- LOG_PRINT_L0("Initializing P2P server...");
- res = p2psrv.init(vm, testnet_mode);
- CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize P2P server.");
- LOG_PRINT_L0("P2P server initialized OK");
-
- LOG_PRINT_L0("Initializing protocol...");
- res = cprotocol.init(vm);
- CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize protocol.");
- LOG_PRINT_L0("Protocol initialized OK");
-
- LOG_PRINT_L0("Initializing core RPC server...");
- res = rpc_server.init(vm);
- CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize core RPC server.");
- LOG_PRINT_GREEN("Core RPC server initialized OK on port: " << rpc_server.get_binded_port(), LOG_LEVEL_0);
-
- // start components
- if(!command_line::has_arg(vm, arg_console))
+void t_daemon::stop()
+{
+ if (nullptr == mp_internals)
{
- dch->start_handling();
+ throw std::runtime_error{"Can't stop stopped daemon"};
}
-
- LOG_PRINT_L0("Starting core RPC server...");
- res = rpc_server.run(2, false);
- CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize core RPC server.");
- LOG_PRINT_L0("Core RPC server started ok");
-
- tools::signal_handler::install([&dch, &p2psrv] {
- dch->stop_handling();
- p2psrv.send_stop_signal();
- });
-
- LOG_PRINT_L0("Starting P2P net loop...");
- p2psrv.run();
- LOG_PRINT_L0("P2P net loop stopped");
-
- //stop components
- dch->stop_handling();
- dch.reset();
- LOG_PRINT_L0("Stopping core rpc server...");
- rpc_server.send_stop_signal();
- rpc_server.timed_wait_server_stop(5000);
-
- //deinitialize components
- LOG_PRINT_L0("Deinitializing core...");
- ccore.deinit();
- LOG_PRINT_L0("Deinitializing RPC server ...");
- rpc_server.deinit();
- LOG_PRINT_L0("Deinitializing protocol...");
- cprotocol.deinit();
- LOG_PRINT_L0("Deinitializing P2P...");
- p2psrv.deinit();
-
-
- ccore.set_cryptonote_protocol(NULL);
- cprotocol.set_p2p_endpoint(NULL);
-
- epee::net_utils::data_logger::kill_instance();
- LOG_PRINT("Node stopped.", LOG_LEVEL_0);
- return 0;
-
- CATCH_ENTRY_L0("main", 1);
+ mp_internals->p2p.stop();
+ mp_internals->rpc.stop();
+ mp_internals.reset(nullptr); // Ensure resources are cleaned up before we return
}
+} // namespace daemonize
diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h
new file mode 100644
index 000000000..b0869b25b
--- /dev/null
+++ b/src/daemon/daemon.h
@@ -0,0 +1,54 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#pragma once
+
+#include <memory>
+#include <boost/program_options.hpp>
+
+namespace daemonize {
+
+class t_internals;
+
+class t_daemon final {
+public:
+ static void init_options(boost::program_options::options_description & option_spec);
+private:
+ std::unique_ptr<t_internals> mp_internals;
+public:
+ t_daemon(
+ boost::program_options::variables_map const & vm
+ );
+ t_daemon(t_daemon && other);
+ t_daemon & operator=(t_daemon && other);
+ ~t_daemon();
+
+ bool run(bool interactive = false);
+ void stop();
+};
+}
diff --git a/src/daemon/daemon_commands_handler.h b/src/daemon/daemon_commands_handler.h
index c23df0443..7baca596d 100644
--- a/src/daemon/daemon_commands_handler.h
+++ b/src/daemon/daemon_commands_handler.h
@@ -1,35 +1,7 @@
-// Copyright (c) 2014-2015, The Monero Project
-//
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without modification, are
-// permitted provided that the following conditions are met:
-//
-// 1. Redistributions of source code must retain the above copyright notice, this list of
-// conditions and the following disclaimer.
-//
-// 2. Redistributions in binary form must reproduce the above copyright notice, this list
-// of conditions and the following disclaimer in the documentation and/or other
-// materials provided with the distribution.
-//
-// 3. Neither the name of the copyright holder nor the names of its contributors may be
-// used to endorse or promote products derived from this software without specific
-// prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
-// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
-// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
-// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
-// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
-// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-//
-// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
-
-
-/* This isn't a header file, may want to refactor this... */
+// Copyright (c) 2012-2013 The Cryptonote developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
#pragma once
#include <boost/lexical_cast.hpp>
@@ -42,21 +14,18 @@
#include "version.h"
#include "../../contrib/otshell_utils/utils.hpp"
-/*!
- * \brief I don't really know right now
- *
- *
- */
+//#include "net/net_helper.h"
+//#include "../p2p/p2p_protocol_defs.h"
+//#include "../p2p/net_peerlist_boost_serialization.h"
+//#include "net/local_ip.h"
+//#include "crypto/crypto.h"
+//#include "storages/levin_abstract_invoke2.h"
+
class daemon_cmmands_handler
{
nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >& m_srv;
public:
- daemon_cmmands_handler(
- nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >& srv
- , bool testnet
- )
- : m_srv(srv)
- , m_testnet(testnet)
+ daemon_cmmands_handler(nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >& srv):m_srv(srv)
{
m_cmd_binder.set_handler("help", boost::bind(&daemon_cmmands_handler::help, this, _1), "Show this help");
m_cmd_binder.set_handler("print_pl", boost::bind(&daemon_cmmands_handler::print_pl, this, _1), "Print peer list");
@@ -75,14 +44,10 @@ public:
m_cmd_binder.set_handler("save", boost::bind(&daemon_cmmands_handler::save, this, _1), "Save blockchain");
m_cmd_binder.set_handler("set_log", boost::bind(&daemon_cmmands_handler::set_log, this, _1), "set_log <level> - Change current log detalization level, <level> is a number 0-4");
m_cmd_binder.set_handler("diff", boost::bind(&daemon_cmmands_handler::diff, this, _1), "Show difficulty");
+ m_cmd_binder.set_handler("limit-up", boost::bind(&daemon_cmmands_handler::limit_up, this, _1), "Set upload limit");
+ m_cmd_binder.set_handler("limit-down", boost::bind(&daemon_cmmands_handler::limit_down, this, _1), "Set download limit");
+ m_cmd_binder.set_handler("limit", boost::bind(&daemon_cmmands_handler::limit, this, _1), "Set download and upload limit");
m_cmd_binder.set_handler("out_peers", boost::bind(&daemon_cmmands_handler::out_peers_limit, this, _1), "Set max limit of out peers");
- m_cmd_binder.set_handler("limit_up", boost::bind(&daemon_cmmands_handler::limit_up, this, _1), "Set upload limit [kB/s]");
- m_cmd_binder.set_handler("limit_down", boost::bind(&daemon_cmmands_handler::limit_down, this, _1), "Set download limit [kB/s]");
- m_cmd_binder.set_handler("limit", boost::bind(&daemon_cmmands_handler::limit, this, _1), "Set download and upload limit [kB/s]");
- m_cmd_binder.set_handler("fast_exit", boost::bind(&daemon_cmmands_handler::fast_exit, this, _1), "Exit");
- m_cmd_binder.set_handler("test_drop_download", boost::bind(&daemon_cmmands_handler::test_drop_download, this, _1), "For network testing, drop downloaded blocks instead checking/adding them to blockchain. Can fake-download blocks very fast.");
- m_cmd_binder.set_handler("start_save_graph", boost::bind(&daemon_cmmands_handler::start_save_graph, this, _1), "");
- m_cmd_binder.set_handler("stop_save_graph", boost::bind(&daemon_cmmands_handler::stop_save_graph, this, _1), "");
}
bool start_handling()
@@ -98,7 +63,7 @@ public:
private:
epee::srv_console_handlers_binder<nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> > > m_cmd_binder;
- bool m_testnet;
+
//--------------------------------------------------------------------------------
std::string get_commands_str()
@@ -131,6 +96,122 @@ private:
return true;
}
//--------------------------------------------------------------------------------
+ bool limit_up(const std::vector<std::string>& args)
+ {
+ if(args.size()!=1) {
+ std::cout << "Usage: limit_up <speed>" << ENDL;
+ return false;
+ }
+
+ int limit;
+ try {
+ limit = std::stoi(args[0]);
+ }
+ catch(std::invalid_argument& ex) {
+ return false;
+ }
+
+ if (limit==-1) {
+ limit=128;
+ //this->islimitup=false;
+ }
+
+ limit *= 1024;
+
+
+ //nodetool::epee::net_utils::connection<epee::levin::async_protocol_handler<nodetool::p2p_connection_context> >::set_rate_up_limit( limit );
+ epee::net_utils::connection_basic::set_rate_up_limit( limit );
+ std::cout << "Set limit-up to " << limit/1024 << " kB/s" << std::endl;
+
+ return true;
+ }
+
+ //--------------------------------------------------------------------------------
+ bool limit_down(const std::vector<std::string>& args)
+ {
+
+ if(args.size()!=1) {
+ std::cout << "Usage: limit_down <speed>" << ENDL;
+ return true;
+ }
+
+ int limit;
+ try {
+ limit = std::stoi(args[0]);
+ }
+
+ catch(std::invalid_argument& ex) {
+ return false;
+ }
+
+ if (limit==-1) {
+ limit=128;
+ //this->islimitup=false;
+ }
+
+ limit *= 1024;
+
+
+ //nodetool::epee::net_utils::connection<epee::levin::async_protocol_handler<nodetool::p2p_connection_context> >::set_rate_up_limit( limit );
+ epee::net_utils::connection_basic::set_rate_down_limit( limit );
+ std::cout << "Set limit-down to " << limit/1024 << " kB/s" << std::endl;
+
+ return true;
+ }
+
+//--------------------------------------------------------------------------------
+ bool limit(const std::vector<std::string>& args)
+ {
+ if(args.size()!=1) {
+ std::cout << "Usage: limit_down <speed>" << ENDL;
+ return true;
+ }
+
+ int limit;
+ try {
+ limit = std::stoi(args[0]);
+ }
+ catch(std::invalid_argument& ex) {
+ return false;
+ }
+
+ if (limit==-1) {
+ limit=128;
+ //this->islimitup=false;
+ }
+
+ limit *= 1024;
+
+
+ //nodetool::epee::net_utils::connection<epee::levin::async_protocol_handler<nodetool::p2p_connection_context> >::set_rate_up_limit( limit );
+ epee::net_utils::connection_basic::set_rate_down_limit( limit );
+ epee::net_utils::connection_basic::set_rate_up_limit( limit );
+ std::cout << "Set limit-down to " << limit/1024 << " kB/s" << std::endl;
+ std::cout << "Set limit-up to " << limit/1024 << " kB/s" << std::endl;
+
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool out_peers_limit(const std::vector<std::string>& args) {
+ if(args.size()!=1) {
+ std::cout << "Usage: limit_down <speed>" << ENDL;
+ return true;
+ }
+
+ int limit;
+ try {
+ limit = std::stoi(args[0]);
+ }
+
+ catch(std::invalid_argument& ex) {
+ return false;
+ }
+
+ LOG_PRINT_RED_L0("connections_count: " << limit);
+ m_srv.m_config.m_net_config.connections_count = limit;
+ return true;
+ }
+ //--------------------------------------------------------------------------------
bool show_hr(const std::vector<std::string>& args)
{
if(!m_srv.get_payload_object().get_core().get_miner().is_mining())
@@ -243,9 +324,11 @@ private:
return true;
}
+ // TODO what the hell causes compilation warning in following code line
+PUSH_WARNINGS
+DISABLE_GCC_WARNING(maybe-uninitialized)
log_space::log_singletone::get_set_log_detalisation_level(true, l);
- int otshell_utils_log_level = 100 - (l * 25);
- gCurrentLogger.setDebugLevel(otshell_utils_log_level);
+POP_WARNINGS
return true;
}
@@ -385,7 +468,7 @@ private:
}
cryptonote::account_public_address adr;
- if(!cryptonote::get_account_address_from_str(adr, m_testnet, args.front()))
+ if(!cryptonote::get_account_address_from_str(adr, args.front()))
{
std::cout << "target account address has wrong format" << std::endl;
return true;
diff --git a/src/daemon/executor.cpp b/src/daemon/executor.cpp
new file mode 100644
index 000000000..bb188c174
--- /dev/null
+++ b/src/daemon/executor.cpp
@@ -0,0 +1,71 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "daemon/executor.h"
+
+#include "misc_log_ex.h"
+
+#include "common/command_line.h"
+#include "cryptonote_config.h"
+#include "version.h"
+
+#include <string>
+
+namespace daemonize
+{
+ std::string const t_executor::NAME = "Monero Daemon";
+
+ void t_executor::init_options(
+ boost::program_options::options_description & configurable_options
+ )
+ {
+ t_daemon::init_options(configurable_options);
+ }
+
+ std::string const & t_executor::name()
+ {
+ return NAME;
+ }
+
+ t_daemon t_executor::create_daemon(
+ boost::program_options::variables_map const & vm
+ )
+ {
+ LOG_PRINT_L0(CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL);
+ return t_daemon{vm};
+ }
+
+ bool t_executor::run_interactive(
+ boost::program_options::variables_map const & vm
+ )
+ {
+ epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL);
+ return t_daemon{vm}.run(true);
+ }
+}
+
diff --git a/src/daemon/executor.h b/src/daemon/executor.h
new file mode 100644
index 000000000..553896875
--- /dev/null
+++ b/src/daemon/executor.h
@@ -0,0 +1,60 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#pragma once
+
+#include "daemon/daemon.h"
+#include <boost/program_options/options_description.hpp>
+#include <boost/program_options/variables_map.hpp>
+#include <string>
+#include <vector>
+
+namespace daemonize
+{
+ class t_executor final
+ {
+ public:
+ typedef ::daemonize::t_daemon t_daemon;
+
+ static std::string const NAME;
+
+ static void init_options(
+ boost::program_options::options_description & configurable_options
+ );
+
+ std::string const & name();
+
+ t_daemon create_daemon(
+ boost::program_options::variables_map const & vm
+ );
+
+ bool run_interactive(
+ boost::program_options::variables_map const & vm
+ );
+ };
+}
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
new file mode 100644
index 000000000..5d8baf497
--- /dev/null
+++ b/src/daemon/main.cpp
@@ -0,0 +1,238 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
+
+#include "common/command_line.h"
+#include "common/scoped_message_writer.h"
+#include "common/util.h"
+#include "cryptonote_core/cryptonote_core.h"
+#include "cryptonote_core/miner.h"
+#include "daemon/command_server.h"
+#include "daemon/daemon.h"
+#include "daemon/executor.h"
+#include "daemonizer/daemonizer.h"
+#include "misc_log_ex.h"
+#include "p2p/net_node.h"
+#include "rpc/core_rpc_server.h"
+#include <boost/program_options.hpp>
+#include "daemon/command_line_args.h"
+
+namespace po = boost::program_options;
+namespace bf = boost::filesystem;
+
+int main(int argc, char const * argv[])
+{
+ try {
+
+ epee::string_tools::set_module_name_and_folder(argv[0]);
+
+ // Build argument description
+ po::options_description all_options("All");
+ po::options_description hidden_options("Hidden");
+ po::options_description visible_options("Options");
+ po::options_description core_settings("Settings");
+ po::positional_options_description positional_options;
+ {
+ bf::path default_data_dir = daemonizer::get_default_data_dir();
+ bf::path default_testnet_data_dir = {default_data_dir / "testnet"};
+
+ // Misc Options
+
+ command_line::add_arg(visible_options, command_line::arg_help);
+ command_line::add_arg(visible_options, command_line::arg_version);
+ command_line::add_arg(visible_options, daemon_args::arg_os_version);
+ command_line::add_arg(visible_options, command_line::arg_data_dir, default_data_dir.string());
+ command_line::add_arg(visible_options, command_line::arg_testnet_data_dir, default_testnet_data_dir.string());
+ bf::path default_conf = default_data_dir / std::string(CRYPTONOTE_NAME ".conf");
+ command_line::add_arg(visible_options, daemon_args::arg_config_file, default_conf.string());
+
+ // Settings
+ bf::path default_log = default_data_dir / std::string(CRYPTONOTE_NAME ".log");
+ command_line::add_arg(core_settings, daemon_args::arg_log_file, default_log.string());
+ command_line::add_arg(core_settings, daemon_args::arg_log_level);
+ command_line::add_arg(core_settings, daemon_args::arg_testnet_on);
+ command_line::add_arg(core_settings, daemon_args::arg_dns_checkpoints);
+ daemonizer::init_options(hidden_options, visible_options);
+ daemonize::t_executor::init_options(core_settings);
+
+ // Hidden options
+ command_line::add_arg(hidden_options, daemon_args::arg_command);
+
+ visible_options.add(core_settings);
+ all_options.add(visible_options);
+ all_options.add(hidden_options);
+
+ // Positional
+ positional_options.add(daemon_args::arg_command.name, -1); // -1 for unlimited arguments
+ }
+
+ // Do command line parsing
+ po::variables_map vm;
+ bool ok = command_line::handle_error_helper(visible_options, [&]()
+ {
+ boost::program_options::store(
+ boost::program_options::command_line_parser(argc, argv)
+ .options(all_options).positional(positional_options).run()
+ , vm
+ );
+
+ return true;
+ });
+ if (!ok) return 1;
+
+ if (command_line::get_arg(vm, command_line::arg_help))
+ {
+ std::cout << CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL << ENDL << ENDL;
+ std::cout << "Usage: " + std::string{argv[0]} + " [options|settings] [daemon_command...]" << std::endl << std::endl;
+ std::cout << visible_options << std::endl;
+ return 0;
+ }
+
+ // Monero Version
+ if (command_line::get_arg(vm, command_line::arg_version))
+ {
+ std::cout << CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL << ENDL;
+ return 0;
+ }
+
+ // OS
+ if (command_line::get_arg(vm, daemon_args::arg_os_version))
+ {
+ std::cout << "OS: " << tools::get_os_version_string() << ENDL;
+ return 0;
+ }
+
+ bool testnet_mode = command_line::get_arg(vm, daemon_args::arg_testnet_on);
+
+ auto data_dir_arg = testnet_mode ? command_line::arg_testnet_data_dir : command_line::arg_data_dir;
+
+ // Create data dir if it doesn't exist
+ boost::filesystem::path data_dir = boost::filesystem::absolute(
+ command_line::get_arg(vm, data_dir_arg));
+ tools::create_directories_if_necessary(data_dir.string());
+
+ // FIXME: not sure on windows implementation default, needs further review
+ //bf::path relative_path_base = daemonizer::get_relative_path_base(vm);
+ bf::path relative_path_base = data_dir;
+
+ std::string config = command_line::get_arg(vm, daemon_args::arg_config_file);
+
+ boost::filesystem::path data_dir_path(data_dir);
+ boost::filesystem::path config_path(config);
+ if (!config_path.has_parent_path())
+ {
+ config_path = data_dir / config_path;
+ }
+
+ boost::system::error_code ec;
+ if (bf::exists(config_path, ec))
+ {
+ po::store(po::parse_config_file<char>(config_path.string<std::string>().c_str(), core_settings), vm);
+ }
+ po::notify(vm);
+
+ // If there are positional options, we're running a daemon command
+ {
+ auto command = command_line::get_arg(vm, daemon_args::arg_command);
+
+ if (command.size())
+ {
+ auto rpc_ip_str = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_ip);
+ auto rpc_port_str = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_port);
+ if (testnet_mode)
+ {
+ rpc_port_str = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_testnet_rpc_bind_port);
+ }
+
+ uint32_t rpc_ip;
+ uint16_t rpc_port;
+ if (!epee::string_tools::get_ip_int32_from_string(rpc_ip, rpc_ip_str))
+ {
+ std::cerr << "Invalid IP: " << rpc_ip_str << std::endl;
+ return 1;
+ }
+ if (!epee::string_tools::get_xtype_from_string(rpc_port, rpc_port_str))
+ {
+ std::cerr << "Invalid port: " << rpc_port_str << std::endl;
+ return 1;
+ }
+
+ daemonize::t_command_server rpc_commands{rpc_ip, rpc_port};
+ if (rpc_commands.process_command_vec(command))
+ {
+ return 0;
+ }
+ else
+ {
+ std::cerr << "Unknown command" << std::endl;
+ return 1;
+ }
+ }
+ }
+
+ // Start with log level 0
+ epee::log_space::get_set_log_detalisation_level(true, LOG_LEVEL_0);
+
+ // Set log level
+ {
+ int new_log_level = command_line::get_arg(vm, daemon_args::arg_log_level);
+ if(new_log_level < LOG_LEVEL_MIN || new_log_level > LOG_LEVEL_MAX)
+ {
+ LOG_PRINT_L0("Wrong log level value: " << new_log_level);
+ }
+ else if (epee::log_space::get_set_log_detalisation_level(false) != new_log_level)
+ {
+ epee::log_space::get_set_log_detalisation_level(true, new_log_level);
+ LOG_PRINT_L0("LOG_LEVEL set to " << new_log_level);
+ }
+ }
+
+ // Set log file
+ {
+ bf::path log_file_path{bf::absolute(command_line::get_arg(vm, daemon_args::arg_log_file), relative_path_base)};
+
+ epee::log_space::log_singletone::add_logger(
+ LOGGER_FILE
+ , log_file_path.filename().string().c_str()
+ , log_file_path.parent_path().string().c_str()
+ );
+ }
+
+ return daemonizer::daemonize(argc, argv, daemonize::t_executor{}, vm);
+ }
+ catch (std::exception const & ex)
+ {
+ LOG_ERROR("Exception in main! " << ex.what());
+ }
+ catch (...)
+ {
+ LOG_ERROR("Exception in main!");
+ }
+ return 1;
+}
diff --git a/src/daemon/p2p.h b/src/daemon/p2p.h
new file mode 100644
index 000000000..355a784b1
--- /dev/null
+++ b/src/daemon/p2p.h
@@ -0,0 +1,99 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
+
+#pragma once
+
+#include "cryptonote_protocol/cryptonote_protocol_handler.h"
+#include "daemon/protocol.h"
+#include "misc_log_ex.h"
+#include "p2p/net_node.h"
+#include <stdexcept>
+#include <boost/program_options.hpp>
+
+namespace daemonize
+{
+
+class t_p2p final
+{
+private:
+ typedef cryptonote::t_cryptonote_protocol_handler<cryptonote::core> t_protocol_raw;
+ typedef nodetool::node_server<t_protocol_raw> t_node_server;
+public:
+ static void init_options(boost::program_options::options_description & option_spec)
+ {
+ t_node_server::init_options(option_spec);
+ }
+private:
+ t_node_server m_server;
+public:
+ t_p2p(
+ boost::program_options::variables_map const & vm
+ , t_protocol & protocol
+ )
+ : m_server{protocol.get()}
+ {
+ //initialize objects
+ LOG_PRINT_L0("Initializing p2p server...");
+ if (!m_server.init(vm))
+ {
+ throw std::runtime_error("Failed to initialize p2p server.");
+ }
+ LOG_PRINT_L0("P2p server initialized OK");
+ }
+
+ t_node_server & get()
+ {
+ return m_server;
+ }
+
+ void run()
+ {
+ LOG_PRINT_L0("Starting p2p net loop...");
+ m_server.run();
+ LOG_PRINT_L0("p2p net loop stopped");
+ }
+
+ void stop()
+ {
+ m_server.send_stop_signal();
+ }
+
+ ~t_p2p()
+ {
+ LOG_PRINT_L0("Deinitializing p2p...");
+ try {
+ m_server.deinit();
+ } catch (...) {
+ LOG_PRINT_L0("Failed to deinitialize p2p...");
+ }
+ }
+};
+
+}
diff --git a/src/daemon/protocol.h b/src/daemon/protocol.h
new file mode 100644
index 000000000..2f6a66f49
--- /dev/null
+++ b/src/daemon/protocol.h
@@ -0,0 +1,88 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
+
+#pragma once
+
+#include "cryptonote_protocol/cryptonote_protocol_handler.h"
+#include "misc_log_ex.h"
+#include "p2p/net_node.h"
+#include <stdexcept>
+#include <boost/program_options.hpp>
+
+namespace daemonize
+{
+
+class t_protocol final
+{
+private:
+ typedef cryptonote::t_cryptonote_protocol_handler<cryptonote::core> t_protocol_raw;
+ typedef nodetool::node_server<t_protocol_raw> t_node_server;
+
+ t_protocol_raw m_protocol;
+public:
+ t_protocol(
+ boost::program_options::variables_map const & vm
+ , t_core & core
+ )
+ : m_protocol{core.get(), nullptr}
+ {
+ LOG_PRINT_L0("Initializing cryptonote protocol...");
+ if (!m_protocol.init(vm))
+ {
+ throw std::runtime_error("Failed to initialize cryptonote protocol.");
+ }
+ LOG_PRINT_L0("Cryptonote protocol initialized OK");
+ }
+
+ t_protocol_raw & get()
+ {
+ return m_protocol;
+ }
+
+ void set_p2p_endpoint(
+ t_node_server & server
+ )
+ {
+ m_protocol.set_p2p_endpoint(&server);
+ }
+
+ ~t_protocol()
+ {
+ LOG_PRINT_L0("Deinitializing cryptonote_protocol...");
+ try {
+ m_protocol.deinit();
+ m_protocol.set_p2p_endpoint(nullptr);
+ } catch (...) {
+ LOG_PRINT_L0("Failed to deinitialize protocol...");
+ }
+ }
+};
+
+}
diff --git a/src/daemon/rpc.h b/src/daemon/rpc.h
new file mode 100644
index 000000000..6477f440e
--- /dev/null
+++ b/src/daemon/rpc.h
@@ -0,0 +1,101 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
+
+#pragma once
+
+#include "daemon/core.h"
+#include "daemon/p2p.h"
+#include "misc_log_ex.h"
+#include "rpc/core_rpc_server.h"
+#include <boost/program_options.hpp>
+#include <stdexcept>
+
+namespace daemonize
+{
+
+class t_rpc final
+{
+public:
+ static void init_options(boost::program_options::options_description & option_spec)
+ {
+ cryptonote::core_rpc_server::init_options(option_spec);
+ }
+private:
+ cryptonote::core_rpc_server m_server;
+public:
+ t_rpc(
+ boost::program_options::variables_map const & vm
+ , t_core & core
+ , t_p2p & p2p
+ )
+ : m_server{core.get(), p2p.get()}
+ {
+ LOG_PRINT_L0("Initializing core rpc server...");
+ if (!m_server.init(vm))
+ {
+ throw std::runtime_error("Failed to initialize core rpc server.");
+ }
+ LOG_PRINT_GREEN("Core rpc server initialized OK on port: " << m_server.get_binded_port(), LOG_LEVEL_0);
+ }
+
+ void run()
+ {
+ LOG_PRINT_L0("Starting core rpc server...");
+ if (!m_server.run(2, false))
+ {
+ throw std::runtime_error("Failed to start core rpc server.");
+ }
+ LOG_PRINT_L0("Core rpc server started ok");
+ }
+
+ void stop()
+ {
+ LOG_PRINT_L0("Stopping core rpc server...");
+ m_server.send_stop_signal();
+ m_server.timed_wait_server_stop(5000);
+ }
+
+ cryptonote::core_rpc_server* get_server()
+ {
+ return &m_server;
+ }
+
+ ~t_rpc()
+ {
+ LOG_PRINT_L0("Deinitializing rpc server...");
+ try {
+ m_server.deinit();
+ } catch (...) {
+ LOG_PRINT_L0("Failed to deinitialize rpc server...");
+ }
+ }
+};
+
+}
diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp
new file mode 100644
index 000000000..f06f48544
--- /dev/null
+++ b/src/daemon/rpc_command_executor.cpp
@@ -0,0 +1,692 @@
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
+
+#include "string_tools.h"
+#include "common/scoped_message_writer.h"
+#include "daemon/rpc_command_executor.h"
+#include "rpc/core_rpc_server_commands_defs.h"
+#include <boost/format.hpp>
+#include <ctime>
+
+namespace daemonize {
+
+namespace {
+ void print_peer(std::string const & prefix, cryptonote::peer const & peer)
+ {
+ time_t now;
+ time(&now);
+ time_t last_seen = static_cast<time_t>(peer.last_seen);
+
+ std::string id_str;
+ std::string port_str;
+ std::string elapsed = epee::misc_utils::get_time_interval_string(now - last_seen);
+ std::string ip_str = epee::string_tools::get_ip_string_from_int32(peer.ip);
+ epee::string_tools::xtype_to_string(peer.id, id_str);
+ epee::string_tools::xtype_to_string(peer.port, port_str);
+ std::string addr_str = ip_str + ":" + port_str;
+ tools::msg_writer() << boost::format("%-10s %-25s %-25s %s") % prefix % id_str % addr_str % elapsed;
+ }
+
+ void print_block_header(cryptonote::block_header_responce const & header)
+ {
+ tools::success_msg_writer()
+ << "timestamp: " << boost::lexical_cast<std::string>(header.timestamp) << std::endl
+ << "previous hash: " << header.prev_hash << std::endl
+ << "nonce: " << boost::lexical_cast<std::string>(header.nonce) << std::endl
+ << "is orphan: " << header.orphan_status << std::endl
+ << "height: " << boost::lexical_cast<std::string>(header.height) << std::endl
+ << "depth: " << boost::lexical_cast<std::string>(header.depth) << std::endl
+ << "hash: " << header.hash
+ << "difficulty: " << boost::lexical_cast<std::string>(header.difficulty) << std::endl
+ << "reward: " << boost::lexical_cast<std::string>(header.reward);
+ }
+}
+
+t_rpc_command_executor::t_rpc_command_executor(
+ uint32_t ip
+ , uint16_t port
+ , bool is_rpc
+ , cryptonote::core_rpc_server* rpc_server
+ )
+ : m_rpc_client(NULL), m_rpc_server(rpc_server)
+{
+ if (is_rpc)
+ {
+ m_rpc_client = new tools::t_rpc_client(ip, port);
+ }
+ else
+ {
+ if (rpc_server == NULL)
+ {
+ throw std::runtime_error("If not calling commands via RPC, rpc_server pointer must be non-null");
+ }
+ }
+
+ m_is_rpc = is_rpc;
+}
+
+t_rpc_command_executor::~t_rpc_command_executor()
+{
+ if (m_rpc_client != NULL)
+ {
+ delete m_rpc_client;
+ }
+}
+
+bool t_rpc_command_executor::print_peer_list() {
+ cryptonote::COMMAND_RPC_GET_PEER_LIST::request req;
+ cryptonote::COMMAND_RPC_GET_PEER_LIST::response res;
+
+ std::string failure_message = "Couldn't retrieve peer list";
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/get_peer_list", failure_message.c_str()))
+ {
+ return false;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_peer_list(req, res))
+ {
+ tools::fail_msg_writer() << failure_message;
+ return false;
+ }
+ }
+
+ for (auto & peer : res.white_list)
+ {
+ print_peer("white", peer);
+ }
+
+ for (auto & peer : res.gray_list)
+ {
+ print_peer("gray", peer);
+ }
+
+ return true;
+}
+
+bool t_rpc_command_executor::save_blockchain() {
+ cryptonote::COMMAND_RPC_SAVE_BC::request req;
+ cryptonote::COMMAND_RPC_SAVE_BC::response res;
+
+ std::string fail_message = "Couldn't save blockchain";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/save_bc", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_save_bc(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ }
+ }
+
+ tools::success_msg_writer() << "Blockchain saved";
+
+ return true;
+}
+
+bool t_rpc_command_executor::show_hash_rate() {
+ cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::request req;
+ cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::response res;
+ req.visible = true;
+
+ std::string fail_message = "Unsuccessful";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/set_log_hash_rate", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_set_log_hash_rate(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ }
+ }
+
+ tools::success_msg_writer() << "Hash rate logging is on";
+
+ return true;
+}
+
+bool t_rpc_command_executor::hide_hash_rate() {
+ cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::request req;
+ cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::response res;
+ req.visible = false;
+
+ std::string fail_message = "Unsuccessful";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/set_log_hash_rate", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_set_log_hash_rate(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ tools::success_msg_writer() << "Hash rate logging is off";
+
+ return true;
+}
+
+bool t_rpc_command_executor::show_difficulty() {
+ cryptonote::COMMAND_RPC_GET_INFO::request req;
+ cryptonote::COMMAND_RPC_GET_INFO::response res;
+
+ std::string fail_message = "Problem fetching info";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/getinfo", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_info(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ tools::success_msg_writer() << "BH: " << res.height
+ << ", DIFF: " << res.difficulty
+ << ", HR: " << (int) res.difficulty / 60L << " H/s";
+
+ return true;
+}
+
+bool t_rpc_command_executor::print_connections() {
+ cryptonote::COMMAND_RPC_GET_CONNECTIONS::request req;
+ cryptonote::COMMAND_RPC_GET_CONNECTIONS::response res;
+ epee::json_rpc::error error_resp;
+
+ std::string fail_message = "Unsuccessful";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->json_rpc_request(req, res, "/get_connections", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_connections(req, res, error_resp))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ for (auto & info : res.connections)
+ {
+ std::string address = info.ip + ":" + info.port;
+ std::string in_out = info.incoming ? "INC" : "OUT";
+ tools::msg_writer() << boost::format("%-25s peer_id: %-25s %s") % address % info.peer_id % in_out;
+ }
+
+ return true;
+}
+
+bool t_rpc_command_executor::print_blockchain_info(uint64_t start_block_index, uint64_t end_block_index) {
+
+// this function appears to not exist in the json rpc api, and so is commented
+// until such a time as it does.
+
+/*
+ cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::request req;
+ cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::response res;
+ epee::json_rpc::error error_resp;
+
+ req.start_height = start_block_index;
+ req.end_height = end_block_index;
+
+ std::string fail_message = "Unsuccessful";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->json_rpc_request(req, res, "getblockheadersrange", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_getblockheadersrange(req, res, error_resp))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ for (auto & header : res.headers)
+ {
+ std::cout
+ << "major version: " << header.major_version << std::endl
+ << "minor version: " << header.minor_version << std::endl
+ << "height: " << header.height << ", timestamp: " << header.timestamp << ", difficulty: " << header.difficulty << std::endl
+ << "block id: " << header.hash << std::endl
+ << "previous block id: " << header.prev_hash << std::endl
+ << "difficulty: " << header.difficulty << ", nonce " << header.nonce << std::endl;
+ }
+
+*/
+ return true;
+}
+
+bool t_rpc_command_executor::set_log_level(int8_t level) {
+ cryptonote::COMMAND_RPC_SET_LOG_LEVEL::request req;
+ cryptonote::COMMAND_RPC_SET_LOG_LEVEL::response res;
+ req.level = level;
+
+ std::string fail_message = "Unsuccessful";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/set_log_level", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_set_log_level(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ tools::success_msg_writer() << "Log level is now " << boost::lexical_cast<std::string>(level);
+
+ return true;
+}
+
+bool t_rpc_command_executor::print_height() {
+ cryptonote::COMMAND_RPC_GET_HEIGHT::request req;
+ cryptonote::COMMAND_RPC_GET_HEIGHT::response res;
+
+ std::string fail_message = "Unsuccessful";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/getheight", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_height(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ tools::success_msg_writer() << boost::lexical_cast<std::string>(res.height);
+
+ return true;
+}
+
+bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash) {
+ cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request req;
+ cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response res;
+ epee::json_rpc::error error_resp;
+
+ req.hash = epee::string_tools::pod_to_hex(block_hash);
+
+ std::string fail_message = "Unsuccessful";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->json_rpc_request(req, res, "getblockheaderbyhash", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_block_header_by_hash(req, res, error_resp))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ print_block_header(res.block_header);
+
+ return true;
+}
+
+bool t_rpc_command_executor::print_block_by_height(uint64_t height) {
+ cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request req;
+ cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response res;
+ epee::json_rpc::error error_resp;
+
+ req.height = height;
+
+ std::string fail_message = "Unsuccessful";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->json_rpc_request(req, res, "getblockheaderbyheight", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_block_header_by_height(req, res, error_resp))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ print_block_header(res.block_header);
+
+ return true;
+}
+
+bool t_rpc_command_executor::print_transaction(crypto::hash transaction_hash) {
+ cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req;
+ cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res;
+
+ std::string fail_message = "Problem fetching transaction";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/gettransactions", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_transactions(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ if (1 == res.txs_as_hex.size())
+ {
+ tools::success_msg_writer() << res.txs_as_hex.front();
+ }
+ else
+ {
+ tools::fail_msg_writer() << "transaction wasn't found: <" << transaction_hash << '>' << std::endl;
+ }
+
+ return true;
+}
+
+bool t_rpc_command_executor::print_transaction_pool_long() {
+ cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request req;
+ cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response res;
+
+ std::string fail_message = "Problem fetching transaction pool";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/get_transaction_pool", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_transaction_pool(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ if (res.transactions.empty())
+ {
+ tools::msg_writer() << "Pool is empty" << std::endl;
+ }
+ for (auto & tx_info : res.transactions)
+ {
+ tools::msg_writer() << "id: " << tx_info.id_hash << std::endl
+ << "blob_size: " << tx_info.blob_size << std::endl
+ << "fee: " << tx_info.fee << std::endl
+ << "kept_by_block: " << tx_info.kept_by_block << std::endl
+ << "max_used_block_height: " << tx_info.max_used_block_height << std::endl
+ << "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl
+ << "last_failed_height: " << tx_info.last_failed_height << std::endl
+ << "last_failed_id: " << tx_info.last_failed_id_hash << std::endl;
+ }
+
+ return true;
+}
+
+bool t_rpc_command_executor::print_transaction_pool_short() {
+ cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request req;
+ cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response res;
+
+ std::string fail_message = "Problem fetching transaction pool";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/get_transaction_pool", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_transaction_pool(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ if (res.transactions.empty())
+ {
+ tools::msg_writer() << "Pool is empty" << std::endl;
+ }
+ for (auto & tx_info : res.transactions)
+ {
+ tools::msg_writer() << "id: " << tx_info.id_hash << std::endl
+ << tx_info.tx_json << std::endl
+ << "blob_size: " << tx_info.blob_size << std::endl
+ << "fee: " << tx_info.fee << std::endl
+ << "kept_by_block: " << tx_info.kept_by_block << std::endl
+ << "max_used_block_height: " << tx_info.max_used_block_height << std::endl
+ << "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl
+ << "last_failed_height: " << tx_info.last_failed_height << std::endl
+ << "last_failed_id: " << tx_info.last_failed_id_hash << std::endl;
+ }
+
+ return true;
+}
+
+// TODO: update this for testnet
+bool t_rpc_command_executor::start_mining(cryptonote::account_public_address address, uint64_t num_threads) {
+ cryptonote::COMMAND_RPC_START_MINING::request req;
+ cryptonote::COMMAND_RPC_START_MINING::response res;
+ req.miner_address = cryptonote::get_account_address_as_str(false, address);
+ req.threads_count = num_threads;
+
+ if (m_rpc_client->rpc_request(req, res, "/start_mining", "Mining did not start"))
+ {
+ tools::success_msg_writer() << "Mining started";
+ }
+ return true;
+}
+
+bool t_rpc_command_executor::stop_mining() {
+ cryptonote::COMMAND_RPC_STOP_MINING::request req;
+ cryptonote::COMMAND_RPC_STOP_MINING::response res;
+
+ std::string fail_message = "Mining did not stop";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(req, res, "/stop_mining", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_stop_mining(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ tools::success_msg_writer() << "Mining stopped";
+ return true;
+}
+
+bool t_rpc_command_executor::stop_daemon()
+{
+ cryptonote::COMMAND_RPC_STOP_DAEMON::request req;
+ cryptonote::COMMAND_RPC_STOP_DAEMON::response res;
+
+//# ifdef WIN32
+// // Stop via service API
+// // TODO - this is only temporary! Get rid of hard-coded constants!
+// bool ok = windows::stop_service("BitMonero Daemon");
+// ok = windows::uninstall_service("BitMonero Daemon");
+// //bool ok = windows::stop_service(SERVICE_NAME);
+// //ok = windows::uninstall_service(SERVICE_NAME);
+// if (ok)
+// {
+// return true;
+// }
+//# endif
+
+ // Stop via RPC
+ std::string fail_message = "Daemon did not stop";
+
+ if (m_is_rpc)
+ {
+ if(!m_rpc_client->rpc_request(req, res, "/stop_daemon", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_stop_daemon(req, res))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ tools::success_msg_writer() << "Stop signal sent";
+
+ return true;
+}
+
+bool t_rpc_command_executor::print_status()
+{
+ if (!m_is_rpc)
+ {
+ tools::success_msg_writer() << "print_status makes no sense in interactive mode";
+ return true;
+ }
+
+ bool daemon_is_alive = m_rpc_client->check_connection();
+
+ if(daemon_is_alive) {
+ tools::success_msg_writer() << "bitmonerod is running";
+ }
+ else {
+ tools::fail_msg_writer() << "bitmonerod is NOT running";
+ }
+
+ return true;
+}
+
+bool t_rpc_command_executor::set_limit(int limit)
+{
+/*
+ epee::net_utils::connection_basic::set_rate_down_limit( limit );
+ epee::net_utils::connection_basic::set_rate_up_limit( limit );
+ std::cout << "Set limit-down to " << limit/1024 << " kB/s" << std::endl;
+ std::cout << "Set limit-up to " << limit/1024 << " kB/s" << std::endl;
+*/
+
+ return true;
+}
+
+bool t_rpc_command_executor::set_limit_up(int limit)
+{
+/*
+ epee::net_utils::connection_basic::set_rate_up_limit( limit );
+ std::cout << "Set limit-up to " << limit/1024 << " kB/s" << std::endl;
+*/
+
+ return true;
+}
+
+bool t_rpc_command_executor::set_limit_down(int limit)
+{
+/*
+ epee::net_utils::connection_basic::set_rate_down_limit( limit );
+ std::cout << "Set limit-down to " << limit/1024 << " kB/s" << std::endl;
+*/
+
+ return true;
+}
+
+}// namespace daemonize
diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h
new file mode 100644
index 000000000..fe0181e62
--- /dev/null
+++ b/src/daemon/rpc_command_executor.h
@@ -0,0 +1,111 @@
+/**
+@file
+@details
+
+@image html images/other/runtime-commands.png
+
+*/
+
+// Copyright (c) 2014, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
+
+#pragma once
+
+#include "common/rpc_client.h"
+#include "misc_log_ex.h"
+#include "cryptonote_core/cryptonote_core.h"
+#include "cryptonote_protocol/cryptonote_protocol_handler.h"
+#include "p2p/net_node.h"
+#include "rpc/core_rpc_server.h"
+
+namespace daemonize {
+
+class t_rpc_command_executor final {
+private:
+ tools::t_rpc_client* m_rpc_client;
+ cryptonote::core_rpc_server* m_rpc_server;
+ bool m_is_rpc;
+
+public:
+ t_rpc_command_executor(
+ uint32_t ip
+ , uint16_t port
+ , bool is_rpc = true
+ , cryptonote::core_rpc_server* rpc_server = NULL
+ );
+
+ ~t_rpc_command_executor();
+
+ bool print_peer_list();
+
+ bool save_blockchain();
+
+ bool show_hash_rate();
+
+ bool hide_hash_rate();
+
+ bool show_difficulty();
+
+ bool print_connections();
+
+ bool print_blockchain_info(uint64_t start_block_index, uint64_t end_block_index);
+
+ bool set_log_level(int8_t level);
+
+ bool print_height();
+
+ bool print_block_by_hash(crypto::hash block_hash);
+
+ bool print_block_by_height(uint64_t height);
+
+ bool print_transaction(crypto::hash transaction_hash);
+
+ bool print_transaction_pool_long();
+
+ bool print_transaction_pool_short();
+
+ bool start_mining(cryptonote::account_public_address address, uint64_t num_threads);
+
+ bool stop_mining();
+
+ bool stop_daemon();
+
+ bool print_status();
+
+ bool set_limit(int limit);
+
+ bool set_limit_up(int limit);
+
+ bool set_limit_down(int limit);
+
+
+};
+
+} // namespace daemonize
diff --git a/src/daemonizer/CMakeLists.txt b/src/daemonizer/CMakeLists.txt
new file mode 100644
index 000000000..d5830111c
--- /dev/null
+++ b/src/daemonizer/CMakeLists.txt
@@ -0,0 +1,74 @@
+# Copyright (c) 2014-2015, The Monero Project
+#
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without modification, are
+# permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice, this list of
+# conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice, this list
+# of conditions and the following disclaimer in the documentation and/or other
+# materials provided with the distribution.
+#
+# 3. Neither the name of the copyright holder nor the names of its contributors may be
+# used to endorse or promote products derived from this software without specific
+# prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+if(MSVC OR MINGW)
+ set(daemonizer_sources
+ windows_service.cpp
+ windows_daemonizer.inl
+ )
+else()
+ set(daemonizer_sources
+ posix_fork.cpp
+ posix_daemonizer.inl
+ )
+endif()
+
+set(daemonizer_headers
+)
+
+if(MSVC OR MINGW)
+ set(daemonizer_private_headers
+ daemonizer.h
+ windows_service.h
+ windows_service_runner.h
+ )
+else()
+ set(daemonizer_private_headers
+ daemonizer.h
+ posix_fork.h
+ )
+endif()
+
+bitmonero_private_headers(daemonizer
+ ${daemonizer_private_headers})
+bitmonero_add_library(daemonizer
+ ${daemonizer_sources}
+ ${daemonizer_headers}
+ ${daemonizer_private_headers})
+target_link_libraries(daemonizer
+ LINK_PRIVATE
+ common
+ ${Boost_CHRONO_LIBRARY}
+ ${Boost_FILESYSTEM_LIBRARY}
+ ${Boost_PROGRAM_OPTIONS_LIBRARY}
+ ${Boost_REGEX_LIBRARY}
+ ${Boost_SYSTEM_LIBRARY}
+ ${Boost_THREAD_LIBRARY}
+ ${CMAKE_THREAD_LIBS_INIT}
+ ${UPNP_LIBRARIES}
+ ${EXTRA_LIBRARIES})
diff --git a/src/daemonizer/daemonizer.h b/src/daemonizer/daemonizer.h
new file mode 100644
index 000000000..6097a58f6
--- /dev/null
+++ b/src/daemonizer/daemonizer.h
@@ -0,0 +1,38 @@
+#pragma once
+
+#include <boost/filesystem/path.hpp>
+#include <boost/program_options/options_description.hpp>
+#include <boost/program_options/variables_map.hpp>
+
+namespace daemonizer
+{
+ void init_options(
+ boost::program_options::options_description & hidden_options
+ , boost::program_options::options_description & normal_options
+ );
+
+ boost::filesystem::path get_default_data_dir();
+
+ boost::filesystem::path get_relative_path_base(
+ boost::program_options::variables_map const & vm
+ );
+
+ /**
+ * @arg create_before_detach - this indicates that the daemon should be
+ * created before the fork, giving it a chance to report initialization
+ * errors. At the time of this writing, this is not possible in the primary
+ * daemon (likely due to the size of the blockchain in memory).
+ */
+ template <typename T_executor>
+ bool daemonize(
+ int argc, char const * argv[]
+ , T_executor && executor // universal ref
+ , boost::program_options::variables_map const & vm
+ );
+}
+
+#ifdef WIN32
+# include "daemonizer/windows_daemonizer.inl"
+#else
+# include "daemonizer/posix_daemonizer.inl"
+#endif
diff --git a/src/daemonizer/posix_daemonizer.inl b/src/daemonizer/posix_daemonizer.inl
new file mode 100644
index 000000000..e06d43d61
--- /dev/null
+++ b/src/daemonizer/posix_daemonizer.inl
@@ -0,0 +1,60 @@
+#pragma once
+
+#include "common/scoped_message_writer.h"
+#include "common/util.h"
+#include "daemonizer/posix_fork.h"
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+
+namespace daemonizer
+{
+ namespace
+ {
+ const command_line::arg_descriptor<bool> arg_detach = {
+ "detach"
+ , "Run as daemon"
+ };
+ }
+
+ inline void init_options(
+ boost::program_options::options_description & hidden_options
+ , boost::program_options::options_description & normal_options
+ )
+ {
+ command_line::add_arg(normal_options, arg_detach);
+ }
+
+ inline boost::filesystem::path get_default_data_dir()
+ {
+ return boost::filesystem::absolute(tools::get_default_data_dir());
+ }
+
+ inline boost::filesystem::path get_relative_path_base(
+ boost::program_options::variables_map const & vm
+ )
+ {
+ return boost::filesystem::current_path();
+ }
+
+ template <typename T_executor>
+ inline bool daemonize(
+ int argc, char const * argv[]
+ , T_executor && executor // universal ref
+ , boost::program_options::variables_map const & vm
+ )
+ {
+ if (command_line::has_arg(vm, arg_detach))
+ {
+ auto daemon = executor.create_daemon(vm);
+ tools::success_msg_writer() << "Forking to background...";
+ posix::fork();
+ return daemon.run();
+ }
+ else
+ {
+ //LOG_PRINT_L0(CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL);
+ return executor.run_interactive(vm);
+ }
+ }
+}
diff --git a/src/daemonizer/posix_fork.cpp b/src/daemonizer/posix_fork.cpp
new file mode 100644
index 000000000..c068912ec
--- /dev/null
+++ b/src/daemonizer/posix_fork.cpp
@@ -0,0 +1,108 @@
+// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include "daemonizer/posix_fork.h"
+#include "misc_log_ex.h"
+
+#include <cstdlib>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdexcept>
+#include <string>
+#include <sys/stat.h>
+
+namespace posix {
+
+namespace {
+ void quit(std::string const & message)
+ {
+ LOG_ERROR(message);
+ throw std::runtime_error(message);
+ }
+}
+
+void fork()
+{
+ // Fork the process and have the parent exit. If the process was started
+ // from a shell, this returns control to the user. Forking a new process is
+ // also a prerequisite for the subsequent call to setsid().
+ if (pid_t pid = ::fork())
+ {
+ if (pid > 0)
+ {
+ // We're in the parent process and need to exit.
+ //
+ // When the exit() function is used, the program terminates without
+ // invoking local variables' destructors. Only global variables are
+ // destroyed.
+ exit(0);
+ }
+ else
+ {
+ quit("First fork failed");
+ }
+ }
+
+ // Make the process a new session leader. This detaches it from the
+ // terminal.
+ setsid();
+
+ // A process inherits its working directory from its parent. This could be
+ // on a mounted filesystem, which means that the running daemon would
+ // prevent this filesystem from being unmounted. Changing to the root
+ // directory avoids this problem.
+ if (chdir("/") < 0)
+ {
+ quit("Unable to change working directory to root");
+ }
+
+ // The file mode creation mask is also inherited from the parent process.
+ // We don't want to restrict the permissions on files created by the
+ // daemon, so the mask is cleared.
+ umask(0);
+
+ // A second fork ensures the process cannot acquire a controlling terminal.
+ if (pid_t pid = ::fork())
+ {
+ if (pid > 0)
+ {
+ exit(0);
+ }
+ else
+ {
+ quit("Second fork failed");
+ }
+ }
+
+ // Close the standard streams. This decouples the daemon from the terminal
+ // that started it.
+ close(0);
+ close(1);
+ close(2);
+
+ // We don't want the daemon to have any standard input.
+ if (open("/dev/null", O_RDONLY) < 0)
+ {
+ quit("Unable to open /dev/null");
+ }
+
+ // Send standard output to a log file.
+ const char* output = "/tmp/bitmonero.daemon.stdout.stderr";
+ const int flags = O_WRONLY | O_CREAT | O_APPEND;
+ const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
+ if (open(output, flags, mode) < 0)
+ {
+ quit("Unable to open output file: " + std::string(output));
+ }
+
+ // Also send standard error to the same log file.
+ if (dup(1) < 0)
+ {
+ quit("Unable to dup output descriptor");
+ }
+}
+
+} // namespace posix
diff --git a/src/daemonizer/posix_fork.h b/src/daemonizer/posix_fork.h
new file mode 100644
index 000000000..f099685e9
--- /dev/null
+++ b/src/daemonizer/posix_fork.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#ifndef WIN32
+
+namespace posix {
+
+void fork();
+
+}
+
+#endif
diff --git a/src/daemonizer/windows_daemonizer.inl b/src/daemonizer/windows_daemonizer.inl
new file mode 100644
index 000000000..c099f4097
--- /dev/null
+++ b/src/daemonizer/windows_daemonizer.inl
@@ -0,0 +1,156 @@
+#pragma once
+
+#include "common/util.h"
+#include "daemonizer/windows_service.h"
+#include "daemonizer/windows_service_runner.h"
+
+#include <shlobj.h>
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+
+namespace daemonizer
+{
+ namespace
+ {
+ const command_line::arg_descriptor<bool> arg_install_service = {
+ "install-service"
+ , "Install Windows service"
+ };
+ const command_line::arg_descriptor<bool> arg_uninstall_service = {
+ "uninstall-service"
+ , "Uninstall Windows service"
+ };
+ const command_line::arg_descriptor<bool> arg_start_service = {
+ "start-service"
+ , "Start Windows service"
+ };
+ const command_line::arg_descriptor<bool> arg_stop_service = {
+ "stop-service"
+ , "Stop Windows service"
+ };
+ const command_line::arg_descriptor<bool> arg_is_service = {
+ "run-as-service"
+ , "Hidden -- true if running as windows service"
+ };
+
+ std::string get_argument_string(int argc, char const * argv[])
+ {
+ std::string result = "";
+ for (int i = 1; i < argc; ++i)
+ {
+ result += " " + std::string{argv[i]};
+ }
+ return result;
+ }
+ }
+
+ inline void init_options(
+ boost::program_options::options_description & hidden_options
+ , boost::program_options::options_description & normal_options
+ )
+ {
+ command_line::add_arg(normal_options, arg_install_service);
+ command_line::add_arg(normal_options, arg_uninstall_service);
+ command_line::add_arg(normal_options, arg_start_service);
+ command_line::add_arg(normal_options, arg_stop_service);
+ command_line::add_arg(hidden_options, arg_is_service);
+ }
+
+ inline boost::filesystem::path get_default_data_dir()
+ {
+ bool admin;
+ if (!windows::check_admin(admin))
+ {
+ admin = false;
+ }
+ if (admin)
+ {
+ return boost::filesystem::absolute(
+ tools::get_special_folder_path(CSIDL_COMMON_APPDATA, true) + "\\" + CRYPTONOTE_NAME
+ );
+ }
+ else
+ {
+ return boost::filesystem::absolute(
+ tools::get_special_folder_path(CSIDL_APPDATA, true) + "\\" + CRYPTONOTE_NAME
+ );
+ }
+ }
+
+ inline boost::filesystem::path get_relative_path_base(
+ boost::program_options::variables_map const & vm
+ )
+ {
+ if (command_line::has_arg(vm, arg_is_service))
+ {
+ if (command_line::has_arg(vm, command_line::arg_data_dir))
+ {
+ return command_line::get_arg(vm, command_line::arg_data_dir);
+ }
+ else
+ {
+ return tools::get_default_data_dir();
+ }
+ }
+ else
+ {
+ return boost::filesystem::current_path();
+ }
+ }
+
+ template <typename T_executor>
+ inline bool daemonize(
+ int argc, char const * argv[]
+ , T_executor && executor // universal ref
+ , boost::program_options::variables_map const & vm
+ )
+ {
+ std::string arguments = get_argument_string(argc, argv);
+
+ if (command_line::has_arg(vm, arg_is_service))
+ {
+ // TODO - Set the service status here for return codes
+ windows::t_service_runner<typename T_executor::t_daemon>::run(
+ executor.name()
+ , executor.create_daemon(vm)
+ );
+ return true;
+ }
+ else if (command_line::has_arg(vm, arg_install_service))
+ {
+ if (windows::ensure_admin(arguments))
+ {
+ arguments += " --run-as-service";
+ return windows::install_service(executor.name(), arguments);
+ }
+ }
+ else if (command_line::has_arg(vm, arg_uninstall_service))
+ {
+ if (windows::ensure_admin(arguments))
+ {
+ return windows::uninstall_service(executor.name());
+ }
+ }
+ else if (command_line::has_arg(vm, arg_start_service))
+ {
+ if (windows::ensure_admin(arguments))
+ {
+ return windows::start_service(executor.name());
+ }
+ }
+ else if (command_line::has_arg(vm, arg_stop_service))
+ {
+ if (windows::ensure_admin(arguments))
+ {
+ return windows::stop_service(executor.name());
+ }
+ }
+ else // interactive
+ {
+ //LOG_PRINT_L0(CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL);
+ return executor.run_interactive(vm);
+ }
+
+ return false;
+ }
+}
diff --git a/src/daemonizer/windows_service.cpp b/src/daemonizer/windows_service.cpp
new file mode 100644
index 000000000..1b0ee2ef4
--- /dev/null
+++ b/src/daemonizer/windows_service.cpp
@@ -0,0 +1,341 @@
+#undef UNICODE
+#undef _UNICODE
+
+#include "common/scoped_message_writer.h"
+#include "daemonizer/windows_service.h"
+#include "string_tools.h"
+#include <chrono>
+#include <iostream>
+#include <utility>
+#include <memory>
+#include <shellapi.h>
+#include <thread>
+#include <windows.h>
+
+namespace windows {
+
+namespace {
+ typedef std::unique_ptr<std::remove_pointer<SC_HANDLE>::type, decltype(&::CloseServiceHandle)> service_handle;
+
+ std::string get_last_error()
+ {
+ LPSTR p_error_text = nullptr;
+
+ FormatMessage(
+ FORMAT_MESSAGE_FROM_SYSTEM
+ | FORMAT_MESSAGE_ALLOCATE_BUFFER
+ | FORMAT_MESSAGE_IGNORE_INSERTS
+ , nullptr
+ , GetLastError()
+ , MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
+ , reinterpret_cast<LPSTR>(&p_error_text)
+ , 0
+ , nullptr
+ );
+
+ if (nullptr == p_error_text)
+ {
+ return "";
+ }
+ else
+ {
+ return std::string{p_error_text};
+ LocalFree(p_error_text);
+ }
+ }
+
+ bool relaunch_as_admin(
+ std::string const & command
+ , std::string const & arguments
+ )
+ {
+ SHELLEXECUTEINFO info{};
+ info.cbSize = sizeof(info);
+ info.lpVerb = "runas";
+ info.lpFile = command.c_str();
+ info.lpParameters = arguments.c_str();
+ info.hwnd = nullptr;
+ info.nShow = SW_SHOWNORMAL;
+ if (!ShellExecuteEx(&info))
+ {
+ tools::fail_msg_writer() << "Admin relaunch failed: " << get_last_error();
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+
+ // When we relaunch as admin, Windows opens a new window. This just pauses
+ // to allow the user to read any output.
+ void pause_to_display_admin_window_messages()
+ {
+ std::chrono::milliseconds how_long{1500};
+ std::this_thread::sleep_for(how_long);
+ }
+}
+
+bool check_admin(bool & result)
+{
+ BOOL is_admin = FALSE;
+ PSID p_administrators_group = nullptr;
+
+ SID_IDENTIFIER_AUTHORITY nt_authority = SECURITY_NT_AUTHORITY;
+
+ if (!AllocateAndInitializeSid(
+ &nt_authority
+ , 2
+ , SECURITY_BUILTIN_DOMAIN_RID
+ , DOMAIN_ALIAS_RID_ADMINS
+ , 0, 0, 0, 0, 0, 0
+ , &p_administrators_group
+ ))
+ {
+ tools::fail_msg_writer() << "Security Identifier creation failed: " << get_last_error();
+ return false;
+ }
+
+ if (!CheckTokenMembership(
+ nullptr
+ , p_administrators_group
+ , &is_admin
+ ))
+ {
+ tools::fail_msg_writer() << "Permissions check failed: " << get_last_error();
+ return false;
+ }
+
+ result = is_admin ? true : false;
+
+ return true;
+}
+
+bool ensure_admin(
+ std::string const & arguments
+ )
+{
+ bool admin;
+
+ if (!check_admin(admin))
+ {
+ return false;
+ }
+
+ if (admin)
+ {
+ return true;
+ }
+ else
+ {
+ std::string command = epee::string_tools::get_current_module_path();
+ relaunch_as_admin(command, arguments);
+ return false;
+ }
+}
+
+bool install_service(
+ std::string const & service_name
+ , std::string const & arguments
+ )
+{
+ std::string command = epee::string_tools::get_current_module_path();
+ std::string full_command = command + arguments;
+
+ service_handle p_manager{
+ OpenSCManager(
+ nullptr
+ , nullptr
+ , SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE
+ )
+ , &::CloseServiceHandle
+ };
+ if (p_manager == nullptr)
+ {
+ tools::fail_msg_writer() << "Couldn't connect to service manager: " << get_last_error();
+ return false;
+ }
+
+ service_handle p_service{
+ CreateService(
+ p_manager.get()
+ , service_name.c_str()
+ , service_name.c_str()
+ , 0
+ //, GENERIC_EXECUTE | GENERIC_READ
+ , SERVICE_WIN32_OWN_PROCESS
+ , SERVICE_DEMAND_START
+ , SERVICE_ERROR_NORMAL
+ , full_command.c_str()
+ , nullptr
+ , nullptr
+ , ""
+ //, "NT AUTHORITY\\LocalService"
+ , nullptr // Implies LocalSystem account
+ , nullptr
+ )
+ , &::CloseServiceHandle
+ };
+ if (p_service == nullptr)
+ {
+ tools::fail_msg_writer() << "Couldn't create service: " << get_last_error();
+ return false;
+ }
+
+ tools::success_msg_writer() << "Service installed";
+
+ pause_to_display_admin_window_messages();
+
+ return true;
+}
+
+bool start_service(
+ std::string const & service_name
+ )
+{
+ tools::msg_writer() << "Starting service";
+
+ SERVICE_STATUS_PROCESS service_status = {};
+ DWORD unused = 0;
+
+ service_handle p_manager{
+ OpenSCManager(
+ nullptr
+ , nullptr
+ , SC_MANAGER_CONNECT
+ )
+ , &::CloseServiceHandle
+ };
+ if (p_manager == nullptr)
+ {
+ tools::fail_msg_writer() << "Couldn't connect to service manager: " << get_last_error();
+ return false;
+ }
+
+ service_handle p_service{
+ OpenService(
+ p_manager.get()
+ , service_name.c_str()
+ //, SERVICE_START | SERVICE_QUERY_STATUS
+ , SERVICE_START
+ )
+ , &::CloseServiceHandle
+ };
+ if (p_service == nullptr)
+ {
+ tools::fail_msg_writer() << "Couldn't find service: " << get_last_error();
+ return false;
+ }
+
+ if (!StartService(
+ p_service.get()
+ , 0
+ , nullptr
+ ))
+ {
+ tools::fail_msg_writer() << "Service start request failed: " << get_last_error();
+ return false;
+ }
+
+ tools::success_msg_writer() << "Service started";
+
+ pause_to_display_admin_window_messages();
+
+ return true;
+}
+
+bool stop_service(
+ std::string const & service_name
+ )
+{
+ tools::msg_writer() << "Stopping service";
+
+ service_handle p_manager{
+ OpenSCManager(
+ nullptr
+ , nullptr
+ , SC_MANAGER_CONNECT
+ )
+ , &::CloseServiceHandle
+ };
+ if (p_manager == nullptr)
+ {
+ tools::fail_msg_writer() << "Couldn't connect to service manager: " << get_last_error();
+ return false;
+ }
+
+ service_handle p_service{
+ OpenService(
+ p_manager.get()
+ , service_name.c_str()
+ , SERVICE_STOP | SERVICE_QUERY_STATUS
+ )
+ , &::CloseServiceHandle
+ };
+ if (p_service == nullptr)
+ {
+ tools::fail_msg_writer() << "Couldn't find service: " << get_last_error();
+ return false;
+ }
+
+ SERVICE_STATUS status = {};
+ if (!ControlService(p_service.get(), SERVICE_CONTROL_STOP, &status))
+ {
+ tools::fail_msg_writer() << "Couldn't request service stop: " << get_last_error();
+ return false;
+ }
+
+ tools::success_msg_writer() << "Service stopped";
+
+ pause_to_display_admin_window_messages();
+
+ return true;
+}
+
+bool uninstall_service(
+ std::string const & service_name
+ )
+{
+ service_handle p_manager{
+ OpenSCManager(
+ nullptr
+ , nullptr
+ , SC_MANAGER_CONNECT
+ )
+ , &::CloseServiceHandle
+ };
+ if (p_manager == nullptr)
+ {
+ tools::fail_msg_writer() << "Couldn't connect to service manager: " << get_last_error();
+ return false;
+ }
+
+ service_handle p_service{
+ OpenService(
+ p_manager.get()
+ , service_name.c_str()
+ , SERVICE_QUERY_STATUS | DELETE
+ )
+ , &::CloseServiceHandle
+ };
+ if (p_service == nullptr)
+ {
+ tools::fail_msg_writer() << "Couldn't find service: " << get_last_error();
+ return false;
+ }
+
+ SERVICE_STATUS status = {};
+ if (!DeleteService(p_service.get()))
+ {
+ tools::fail_msg_writer() << "Couldn't uninstall service: " << get_last_error();
+ return false;
+ }
+
+ tools::success_msg_writer() << "Service uninstalled";
+
+ pause_to_display_admin_window_messages();
+
+ return true;
+}
+
+} // namespace windows
diff --git a/src/daemonizer/windows_service.h b/src/daemonizer/windows_service.h
new file mode 100644
index 000000000..11f5fdcdc
--- /dev/null
+++ b/src/daemonizer/windows_service.h
@@ -0,0 +1,36 @@
+#pragma once
+
+#ifdef WIN32
+
+#undef UNICODE
+#undef _UNICODE
+
+#include <string>
+#include <windows.h>
+
+namespace windows
+{
+ bool check_admin(bool & result);
+
+ bool ensure_admin(
+ std::string const & arguments
+ );
+
+ bool install_service(
+ std::string const & service_name
+ , std::string const & arguments
+ );
+
+ bool uninstall_service(
+ std::string const & service_name
+ );
+
+ bool start_service(
+ std::string const & service_name
+ );
+
+ bool stop_service(
+ std::string const & service_name
+ );
+}
+#endif
diff --git a/src/daemonizer/windows_service_runner.h b/src/daemonizer/windows_service_runner.h
new file mode 100644
index 000000000..79b10b136
--- /dev/null
+++ b/src/daemonizer/windows_service_runner.h
@@ -0,0 +1,157 @@
+#pragma once
+
+#ifdef WIN32
+
+#undef UNICODE
+#undef _UNICODE
+
+#include "daemonizer/windows_service.h"
+#include <memory>
+#include <string>
+#include <vector>
+#include <windows.h>
+
+namespace windows {
+ namespace
+ {
+ std::vector<char> vecstring(std::string const & str)
+ {
+ std::vector<char> result{str.begin(), str.end()};
+ result.push_back('\0');
+ return result;
+ }
+ }
+
+ template <typename T_handler>
+ class t_service_runner final
+ {
+ private:
+ SERVICE_STATUS_HANDLE m_status_handle{nullptr};
+ SERVICE_STATUS m_status{};
+ std::mutex m_lock{};
+ std::string m_name;
+ T_handler m_handler;
+
+ static std::unique_ptr<t_service_runner<T_handler>> sp_instance;
+ public:
+ t_service_runner(
+ std::string name
+ , T_handler handler
+ )
+ : m_name{std::move(name)}
+ , m_handler{std::move(handler)}
+ {
+ m_status.dwServiceType = SERVICE_WIN32;
+ m_status.dwCurrentState = SERVICE_STOPPED;
+ m_status.dwControlsAccepted = 0;
+ m_status.dwWin32ExitCode = NO_ERROR;
+ m_status.dwServiceSpecificExitCode = NO_ERROR;
+ m_status.dwCheckPoint = 0;
+ m_status.dwWaitHint = 0;
+ }
+
+ t_service_runner & operator=(t_service_runner && other)
+ {
+ if (this != &other)
+ {
+ m_status_handle = std::move(other.m_status_handle);
+ m_status = std::move(other.m_status);
+ m_name = std::move(other.m_name);
+ m_handler = std::move(other.m_handler);
+ }
+ return *this;
+ }
+
+ static void run(
+ std::string name
+ , T_handler handler
+ )
+ {
+ sp_instance.reset(new t_service_runner<T_handler>{
+ std::move(name)
+ , std::move(handler)
+ });
+
+ sp_instance->run_();
+ }
+
+ private:
+ void run_()
+ {
+ SERVICE_TABLE_ENTRY table[] =
+ {
+ { vecstring(m_name).data(), &service_main }
+ , { 0, 0 }
+ };
+
+ StartServiceCtrlDispatcher(table);
+ }
+
+ void report_status(DWORD status)
+ {
+ m_status.dwCurrentState = status;
+ if (status == SERVICE_RUNNING)
+ {
+ m_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
+ }
+ else if(status == SERVICE_STOP_PENDING)
+ {
+ m_status.dwControlsAccepted = 0;
+ }
+ SetServiceStatus(m_status_handle, &m_status);
+ }
+
+ static void WINAPI service_main(DWORD argc, LPSTR * argv)
+ {
+ sp_instance->service_main_(argc, argv);
+ }
+
+ void service_main_(DWORD argc, LPSTR * argv)
+ {
+ m_status_handle = RegisterServiceCtrlHandler(m_name.c_str(), &on_state_change_request);
+ if (m_status_handle == nullptr) return;
+
+ report_status(SERVICE_START_PENDING);
+
+ report_status(SERVICE_RUNNING);
+
+ m_handler.run();
+
+ on_state_change_request_(SERVICE_CONTROL_STOP);
+
+ // Ensure that the service is uninstalled
+ uninstall_service(m_name);
+ }
+
+ static void WINAPI on_state_change_request(DWORD control_code)
+ {
+ sp_instance->on_state_change_request_(control_code);
+ }
+
+ void on_state_change_request_(DWORD control_code)
+ {
+ switch (control_code)
+ {
+ case SERVICE_CONTROL_INTERROGATE:
+ break;
+ case SERVICE_CONTROL_SHUTDOWN:
+ case SERVICE_CONTROL_STOP:
+ report_status(SERVICE_STOP_PENDING);
+ m_handler.stop();
+ report_status(SERVICE_STOPPED);
+ break;
+ case SERVICE_CONTROL_PAUSE:
+ break;
+ case SERVICE_CONTROL_CONTINUE:
+ break;
+ default:
+ break;
+ }
+ }
+ };
+
+ template <typename T_handler>
+ std::unique_ptr<t_service_runner<T_handler>> t_service_runner<T_handler>::sp_instance;
+}
+
+#endif
diff --git a/src/mnemonics/english.h b/src/mnemonics/english.h
index e70849fda..fee428817 100644
--- a/src/mnemonics/english.h
+++ b/src/mnemonics/english.h
@@ -728,7 +728,7 @@ namespace Language
"initiate",
"injury",
"inkling",
- "incline",
+ "inline",
"inmate",
"innocent",
"inorganic",
@@ -1073,7 +1073,7 @@ namespace Language
"ouch",
"ought",
"ounce",
- "launchpad",
+ "ourselves",
"oust",
"outbreak",
"oval",
diff --git a/src/p2p/net_node.h b/src/p2p/net_node.h
index 5b034ce25..f94fedae0 100644
--- a/src/p2p/net_node.h
+++ b/src/p2p/net_node.h
@@ -80,22 +80,18 @@ namespace nodetool
public:
typedef t_payload_net_handler payload_net_handler;
- node_server(t_payload_net_handler& payload_handler, boost::uuids::uuid network_id)
- :m_payload_handler(payload_handler),
- m_allow_local_ip(false),
- m_no_igd(false),
- m_hide_my_port(false),
- m_network_id(std::move(network_id))
- {
- m_current_number_of_out_peers = 0;
- m_save_graph = false;
- is_closing = false;
- }
+ node_server(
+ t_payload_net_handler& payload_handler
+ )
+ : m_payload_handler(payload_handler)
+ , m_allow_local_ip(false)
+ , m_hide_my_port(false)
+ {}
static void init_options(boost::program_options::options_description& desc);
bool run();
- bool init(const boost::program_options::variables_map& vm, bool testnet);
+ bool init(const boost::program_options::variables_map& vm);
bool deinit();
bool send_stop_signal();
uint32_t get_this_peer_port(){return m_listenning_port;}
diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl
index afc6436f3..ef158d9a1 100644
--- a/src/p2p/net_node.inl
+++ b/src/p2p/net_node.inl
@@ -46,7 +46,7 @@
#include "net/local_ip.h"
#include "crypto/crypto.h"
#include "storages/levin_abstract_invoke2.h"
-#include "data_logger.hpp"
+#include "daemon/command_line_args.h"
// We have to look for miniupnpc headers in different places, dependent on if its compiled or external
#ifdef UPNP_STATIC
@@ -273,16 +273,20 @@ namespace nodetool
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
- bool node_server<t_payload_net_handler>::init(const boost::program_options::variables_map& vm, bool testnet)
+ bool node_server<t_payload_net_handler>::init(const boost::program_options::variables_map& vm)
{
+ bool testnet = command_line::get_arg(vm, daemon_args::arg_testnet_on);
+
if (testnet)
{
+ memcpy(&m_network_id, &::config::testnet::NETWORK_ID, 16);
append_net_address(m_seed_nodes, "107.152.187.202:28080");
append_net_address(m_seed_nodes, "197.242.158.240:28080");
append_net_address(m_seed_nodes, "107.152.130.98:28080");
}
else
{
+ memcpy(&m_network_id, &::config::NETWORK_ID, 16);
// for each hostname in the seed nodes list, attempt to DNS resolve and
// add the result addresses as seed nodes
// TODO: at some point add IPv6 support, but that won't be relevant
diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp
index 7a41e9b9f..f29d0c604 100644
--- a/src/rpc/core_rpc_server.cpp
+++ b/src/rpc/core_rpc_server.cpp
@@ -40,29 +40,10 @@ using namespace epee;
#include "misc_language.h"
#include "crypto/hash.h"
#include "core_rpc_server_error_codes.h"
+#include "daemon/command_line_args.h"
namespace cryptonote
{
- namespace
- {
- const command_line::arg_descriptor<std::string> arg_rpc_bind_ip = {
- "rpc-bind-ip"
- , "IP for RPC server"
- , "127.0.0.1"
- };
-
- const command_line::arg_descriptor<std::string> arg_rpc_bind_port = {
- "rpc-bind-port"
- , "Port for RPC server"
- , std::to_string(config::RPC_DEFAULT_PORT)
- };
-
- const command_line::arg_descriptor<std::string> arg_testnet_rpc_bind_port = {
- "testnet-rpc-bind-port"
- , "Port for testnet RPC server"
- , std::to_string(config::testnet::RPC_DEFAULT_PORT)
- };
- }
//-----------------------------------------------------------------------------------
void core_rpc_server::init_options(boost::program_options::options_description& desc)
@@ -75,11 +56,9 @@ namespace cryptonote
core_rpc_server::core_rpc_server(
core& cr
, nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >& p2p
- , bool testnet
)
: m_core(cr)
, m_p2p(p2p)
- , m_testnet(testnet)
{}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::handle_command_line(
@@ -97,6 +76,8 @@ namespace cryptonote
const boost::program_options::variables_map& vm
)
{
+ m_testnet = command_line::get_arg(vm, daemon_args::arg_testnet_on);
+
m_net_server.set_threads_prefix("RPC");
bool r = handle_command_line(vm);
CHECK_AND_ASSERT_MES(r, false, "Failed to process command line in core_rpc_server");
@@ -124,7 +105,7 @@ namespace cryptonote
#define CHECK_CORE_READY() do { if(!check_core_ready()){res.status = CORE_RPC_STATUS_BUSY;return true;} } while(0)
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_height(const COMMAND_RPC_GET_HEIGHT::request& req, COMMAND_RPC_GET_HEIGHT::response& res, connection_context& cntx)
+ bool core_rpc_server::on_get_height(const COMMAND_RPC_GET_HEIGHT::request& req, COMMAND_RPC_GET_HEIGHT::response& res)
{
CHECK_CORE_BUSY();
res.height = m_core.get_current_blockchain_height();
@@ -132,7 +113,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_info(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, connection_context& cntx)
+ bool core_rpc_server::on_get_info(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res)
{
CHECK_CORE_BUSY();
res.height = m_core.get_current_blockchain_height();
@@ -150,7 +131,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_blocks(const COMMAND_RPC_GET_BLOCKS_FAST::request& req, COMMAND_RPC_GET_BLOCKS_FAST::response& res, connection_context& cntx)
+ bool core_rpc_server::on_get_blocks(const COMMAND_RPC_GET_BLOCKS_FAST::request& req, COMMAND_RPC_GET_BLOCKS_FAST::response& res)
{
CHECK_CORE_BUSY();
std::list<std::pair<block, std::list<transaction> > > bs;
@@ -175,7 +156,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_random_outs(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res, connection_context& cntx)
+ bool core_rpc_server::on_get_random_outs(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res)
{
CHECK_CORE_BUSY();
res.status = "Failed";
@@ -204,7 +185,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_indexes(const COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::request& req, COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response& res, connection_context& cntx)
+ bool core_rpc_server::on_get_indexes(const COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::request& req, COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response& res)
{
CHECK_CORE_BUSY();
bool r = m_core.get_tx_outputs_gindexs(req.txid, res.o_indexes);
@@ -218,7 +199,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_transactions(const COMMAND_RPC_GET_TRANSACTIONS::request& req, COMMAND_RPC_GET_TRANSACTIONS::response& res, connection_context& cntx)
+ bool core_rpc_server::on_get_transactions(const COMMAND_RPC_GET_TRANSACTIONS::request& req, COMMAND_RPC_GET_TRANSACTIONS::response& res)
{
CHECK_CORE_BUSY();
std::vector<crypto::hash> vh;
@@ -260,7 +241,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_send_raw_tx(const COMMAND_RPC_SEND_RAW_TX::request& req, COMMAND_RPC_SEND_RAW_TX::response& res, connection_context& cntx)
+ bool core_rpc_server::on_send_raw_tx(const COMMAND_RPC_SEND_RAW_TX::request& req, COMMAND_RPC_SEND_RAW_TX::response& res)
{
CHECK_CORE_READY();
@@ -303,7 +284,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_start_mining(const COMMAND_RPC_START_MINING::request& req, COMMAND_RPC_START_MINING::response& res, connection_context& cntx)
+ bool core_rpc_server::on_start_mining(const COMMAND_RPC_START_MINING::request& req, COMMAND_RPC_START_MINING::response& res)
{
CHECK_CORE_READY();
account_public_address adr;
@@ -325,7 +306,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_stop_mining(const COMMAND_RPC_STOP_MINING::request& req, COMMAND_RPC_STOP_MINING::response& res, connection_context& cntx)
+ bool core_rpc_server::on_stop_mining(const COMMAND_RPC_STOP_MINING::request& req, COMMAND_RPC_STOP_MINING::response& res)
{
if(!m_core.get_miner().stop())
{
@@ -336,7 +317,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_mining_status(const COMMAND_RPC_MINING_STATUS::request& req, COMMAND_RPC_MINING_STATUS::response& res, connection_context& cntx)
+ bool core_rpc_server::on_mining_status(const COMMAND_RPC_MINING_STATUS::request& req, COMMAND_RPC_MINING_STATUS::response& res)
{
CHECK_CORE_READY();
@@ -354,7 +335,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_save_bc(const COMMAND_RPC_SAVE_BC::request& req, COMMAND_RPC_SAVE_BC::response& res, connection_context& cntx)
+ bool core_rpc_server::on_save_bc(const COMMAND_RPC_SAVE_BC::request& req, COMMAND_RPC_SAVE_BC::response& res)
{
CHECK_CORE_BUSY();
if( !m_core.get_blockchain_storage().store_blockchain() )
@@ -366,7 +347,76 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_getblockcount(const COMMAND_RPC_GETBLOCKCOUNT::request& req, COMMAND_RPC_GETBLOCKCOUNT::response& res, connection_context& cntx)
+ bool core_rpc_server::on_get_peer_list(const COMMAND_RPC_GET_PEER_LIST::request& req, COMMAND_RPC_GET_PEER_LIST::response& res)
+ {
+ /*
+ std::list<nodetool::peerlist_entry> white_list;
+ std::list<nodetool::peerlist_entry> gray_list;
+ m_p2p.get_peerlist(white_list, gray_list);
+
+ for (auto & entry : white_list)
+ {
+ res.white_list.emplace_back(entry.id, entry.adr.ip, entry.adr.port, entry.last_seen);
+ }
+
+ for (auto & entry : gray_list)
+ {
+ res.gray_list.emplace_back(entry.id, entry.adr.ip, entry.adr.port, entry.last_seen);
+ }
+
+ */
+ res.status = CORE_RPC_STATUS_OK;
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
+ bool core_rpc_server::on_set_log_hash_rate(const COMMAND_RPC_SET_LOG_HASH_RATE::request& req, COMMAND_RPC_SET_LOG_HASH_RATE::response& res)
+ {
+ if(m_core.get_miner().is_mining())
+ {
+ m_core.get_miner().do_print_hashrate(req.visible);
+ res.status = CORE_RPC_STATUS_OK;
+ }
+ else
+ {
+ res.status = CORE_RPC_STATUS_NOT_MINING;
+ }
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
+ bool core_rpc_server::on_set_log_level(const COMMAND_RPC_SET_LOG_LEVEL::request& req, COMMAND_RPC_SET_LOG_LEVEL::response& res)
+ {
+ if (req.level < LOG_LEVEL_MIN || req.level > LOG_LEVEL_MAX)
+ {
+ res.status = "Error: log level not valid";
+ }
+ else
+ {
+ epee::log_space::log_singletone::get_set_log_detalisation_level(true, req.level);
+ res.status = CORE_RPC_STATUS_OK;
+ }
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
+ bool core_rpc_server::on_get_transaction_pool(const COMMAND_RPC_GET_TRANSACTION_POOL::request& req, COMMAND_RPC_GET_TRANSACTION_POOL::response& res)
+ {
+ /*
+ CHECK_CORE_BUSY();
+ res.transactions = m_core.transaction_pool_info();
+ */
+ res.status = CORE_RPC_STATUS_OK;
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
+ bool core_rpc_server::on_stop_daemon(const COMMAND_RPC_STOP_DAEMON::request& req, COMMAND_RPC_STOP_DAEMON::response& res)
+ {
+ // FIXME: replace back to original m_p2p.send_stop_signal() after
+ // investigating why that isn't working quite right.
+ m_p2p.send_stop_signal();
+ res.status = CORE_RPC_STATUS_OK;
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
+ bool core_rpc_server::on_getblockcount(const COMMAND_RPC_GETBLOCKCOUNT::request& req, COMMAND_RPC_GETBLOCKCOUNT::response& res)
{
CHECK_CORE_BUSY();
res.count = m_core.get_current_blockchain_height();
@@ -374,7 +424,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_getblockhash(const COMMAND_RPC_GETBLOCKHASH::request& req, COMMAND_RPC_GETBLOCKHASH::response& res, epee::json_rpc::error& error_resp, connection_context& cntx)
+ bool core_rpc_server::on_getblockhash(const COMMAND_RPC_GETBLOCKHASH::request& req, COMMAND_RPC_GETBLOCKHASH::response& res, epee::json_rpc::error& error_resp)
{
if(!check_core_busy())
{
@@ -415,7 +465,7 @@ namespace cryptonote
return 0;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_getblocktemplate(const COMMAND_RPC_GETBLOCKTEMPLATE::request& req, COMMAND_RPC_GETBLOCKTEMPLATE::response& res, epee::json_rpc::error& error_resp, connection_context& cntx)
+ bool core_rpc_server::on_getblocktemplate(const COMMAND_RPC_GETBLOCKTEMPLATE::request& req, COMMAND_RPC_GETBLOCKTEMPLATE::response& res, epee::json_rpc::error& error_resp)
{
if(!check_core_ready())
{
@@ -481,7 +531,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_submitblock(const COMMAND_RPC_SUBMITBLOCK::request& req, COMMAND_RPC_SUBMITBLOCK::response& res, epee::json_rpc::error& error_resp, connection_context& cntx)
+ bool core_rpc_server::on_submitblock(const COMMAND_RPC_SUBMITBLOCK::request& req, COMMAND_RPC_SUBMITBLOCK::response& res, epee::json_rpc::error& error_resp)
{
CHECK_CORE_READY();
if(req.size()!=1)
@@ -553,7 +603,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res, epee::json_rpc::error& error_resp, connection_context& cntx)
+ bool core_rpc_server::on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res, epee::json_rpc::error& error_resp)
{
if(!check_core_busy())
{
@@ -589,7 +639,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_block_header_by_hash(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response& res, epee::json_rpc::error& error_resp, connection_context& cntx){
+ bool core_rpc_server::on_get_block_header_by_hash(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response& res, epee::json_rpc::error& error_resp){
if(!check_core_busy())
{
error_resp.code = CORE_RPC_ERROR_CODE_CORE_BUSY;
@@ -630,7 +680,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_block_header_by_height(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response& res, epee::json_rpc::error& error_resp, connection_context& cntx){
+ bool core_rpc_server::on_get_block_header_by_height(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response& res, epee::json_rpc::error& error_resp){
if(!check_core_busy())
{
error_resp.code = CORE_RPC_ERROR_CODE_CORE_BUSY;
@@ -663,7 +713,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp, connection_context& cntx)
+ bool core_rpc_server::on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp)
{
if(!check_core_busy())
{
@@ -679,7 +729,7 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::on_get_info_json(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, epee::json_rpc::error& error_resp, connection_context& cntx)
+ bool core_rpc_server::on_get_info_json(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, epee::json_rpc::error& error_resp)
{
if(!check_core_busy())
{
@@ -703,4 +753,23 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
-}
+
+ const command_line::arg_descriptor<std::string> core_rpc_server::arg_rpc_bind_ip = {
+ "rpc-bind-ip"
+ , "IP for RPC server"
+ , "127.0.0.1"
+ };
+
+ const command_line::arg_descriptor<std::string> core_rpc_server::arg_rpc_bind_port = {
+ "rpc-bind-port"
+ , "Port for RPC server"
+ , std::to_string(config::RPC_DEFAULT_PORT)
+ };
+
+ const command_line::arg_descriptor<std::string> core_rpc_server::arg_testnet_rpc_bind_port = {
+ "testnet-rpc-bind-port"
+ , "Port for testnet RPC server"
+ , std::to_string(config::testnet::RPC_DEFAULT_PORT)
+ };
+
+} // namespace cryptonote
diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h
index cd9a0f162..6e603acbd 100644
--- a/src/rpc/core_rpc_server.h
+++ b/src/rpc/core_rpc_server.h
@@ -39,6 +39,10 @@
#include "p2p/net_node.h"
#include "cryptonote_protocol/cryptonote_protocol_handler.h"
+// yes, epee doesn't properly use its full namespace when calling its
+// functions from macros. *sigh*
+using namespace epee;
+
namespace cryptonote
{
/************************************************************************/
@@ -47,19 +51,22 @@ namespace cryptonote
class core_rpc_server: public epee::http_server_impl_base<core_rpc_server>
{
public:
+
+ static const command_line::arg_descriptor<std::string> arg_rpc_bind_ip;
+ static const command_line::arg_descriptor<std::string> arg_rpc_bind_port;
+ static const command_line::arg_descriptor<std::string> arg_testnet_rpc_bind_port;
+
typedef epee::net_utils::connection_context_base connection_context;
core_rpc_server(
core& cr
, nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >& p2p
- , bool testnet
);
static void init_options(boost::program_options::options_description& desc);
bool init(
const boost::program_options::variables_map& vm
);
- private:
CHAIN_HTTP_TO_MAP2(connection_context); //forward http requests to uri map
@@ -74,6 +81,11 @@ namespace cryptonote
MAP_URI_AUTO_JON2("/stop_mining", on_stop_mining, COMMAND_RPC_STOP_MINING)
MAP_URI_AUTO_JON2("/mining_status", on_mining_status, COMMAND_RPC_MINING_STATUS)
MAP_URI_AUTO_JON2("/save_bc", on_save_bc, COMMAND_RPC_SAVE_BC)
+ MAP_URI_AUTO_JON2("/get_peer_list", on_get_peer_list, COMMAND_RPC_GET_PEER_LIST)
+ MAP_URI_AUTO_JON2("/set_log_hash_rate", on_set_log_hash_rate, COMMAND_RPC_SET_LOG_HASH_RATE)
+ MAP_URI_AUTO_JON2("/set_log_level", on_set_log_level, COMMAND_RPC_SET_LOG_LEVEL)
+ MAP_URI_AUTO_JON2("/get_transaction_pool", on_get_transaction_pool, COMMAND_RPC_GET_TRANSACTION_POOL)
+ MAP_URI_AUTO_JON2("/stop_daemon", on_stop_daemon, COMMAND_RPC_STOP_DAEMON)
MAP_URI_AUTO_JON2("/getinfo", on_get_info, COMMAND_RPC_GET_INFO)
BEGIN_JSON_RPC_MAP("/json_rpc")
MAP_JON_RPC("getblockcount", on_getblockcount, COMMAND_RPC_GETBLOCKCOUNT)
@@ -88,29 +100,37 @@ namespace cryptonote
END_JSON_RPC_MAP()
END_URI_MAP2()
- bool on_get_height(const COMMAND_RPC_GET_HEIGHT::request& req, COMMAND_RPC_GET_HEIGHT::response& res, connection_context& cntx);
- bool on_get_blocks(const COMMAND_RPC_GET_BLOCKS_FAST::request& req, COMMAND_RPC_GET_BLOCKS_FAST::response& res, connection_context& cntx);
- bool on_get_transactions(const COMMAND_RPC_GET_TRANSACTIONS::request& req, COMMAND_RPC_GET_TRANSACTIONS::response& res, connection_context& cntx);
- bool on_get_indexes(const COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::request& req, COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response& res, connection_context& cntx);
- bool on_send_raw_tx(const COMMAND_RPC_SEND_RAW_TX::request& req, COMMAND_RPC_SEND_RAW_TX::response& res, connection_context& cntx);
- bool on_start_mining(const COMMAND_RPC_START_MINING::request& req, COMMAND_RPC_START_MINING::response& res, connection_context& cntx);
- bool on_stop_mining(const COMMAND_RPC_STOP_MINING::request& req, COMMAND_RPC_STOP_MINING::response& res, connection_context& cntx);
- bool on_mining_status(const COMMAND_RPC_MINING_STATUS::request& req, COMMAND_RPC_MINING_STATUS::response& res, connection_context& cntx);
- bool on_get_random_outs(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res, connection_context& cntx);
- bool on_get_info(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, connection_context& cntx);
- bool on_save_bc(const COMMAND_RPC_SAVE_BC::request& req, COMMAND_RPC_SAVE_BC::response& res, connection_context& cntx);
+ bool on_get_height(const COMMAND_RPC_GET_HEIGHT::request& req, COMMAND_RPC_GET_HEIGHT::response& res);
+ bool on_get_blocks(const COMMAND_RPC_GET_BLOCKS_FAST::request& req, COMMAND_RPC_GET_BLOCKS_FAST::response& res);
+ bool on_get_transactions(const COMMAND_RPC_GET_TRANSACTIONS::request& req, COMMAND_RPC_GET_TRANSACTIONS::response& res);
+ bool on_get_indexes(const COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::request& req, COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response& res);
+ bool on_send_raw_tx(const COMMAND_RPC_SEND_RAW_TX::request& req, COMMAND_RPC_SEND_RAW_TX::response& res);
+ bool on_start_mining(const COMMAND_RPC_START_MINING::request& req, COMMAND_RPC_START_MINING::response& res);
+ bool on_stop_mining(const COMMAND_RPC_STOP_MINING::request& req, COMMAND_RPC_STOP_MINING::response& res);
+ bool on_mining_status(const COMMAND_RPC_MINING_STATUS::request& req, COMMAND_RPC_MINING_STATUS::response& res);
+ bool on_get_random_outs(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res);
+ bool on_get_info(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res);
+ bool on_save_bc(const COMMAND_RPC_SAVE_BC::request& req, COMMAND_RPC_SAVE_BC::response& res);
+ bool on_get_peer_list(const COMMAND_RPC_GET_PEER_LIST::request& req, COMMAND_RPC_GET_PEER_LIST::response& res);
+ bool on_set_log_hash_rate(const COMMAND_RPC_SET_LOG_HASH_RATE::request& req, COMMAND_RPC_SET_LOG_HASH_RATE::response& res);
+ bool on_set_log_level(const COMMAND_RPC_SET_LOG_LEVEL::request& req, COMMAND_RPC_SET_LOG_LEVEL::response& res);
+ bool on_get_transaction_pool(const COMMAND_RPC_GET_TRANSACTION_POOL::request& req, COMMAND_RPC_GET_TRANSACTION_POOL::response& res);
+ bool on_stop_daemon(const COMMAND_RPC_STOP_DAEMON::request& req, COMMAND_RPC_STOP_DAEMON::response& res);
//json_rpc
- bool on_getblockcount(const COMMAND_RPC_GETBLOCKCOUNT::request& req, COMMAND_RPC_GETBLOCKCOUNT::response& res, connection_context& cntx);
- bool on_getblockhash(const COMMAND_RPC_GETBLOCKHASH::request& req, COMMAND_RPC_GETBLOCKHASH::response& res, epee::json_rpc::error& error_resp, connection_context& cntx);
- bool on_getblocktemplate(const COMMAND_RPC_GETBLOCKTEMPLATE::request& req, COMMAND_RPC_GETBLOCKTEMPLATE::response& res, epee::json_rpc::error& error_resp, connection_context& cntx);
- bool on_submitblock(const COMMAND_RPC_SUBMITBLOCK::request& req, COMMAND_RPC_SUBMITBLOCK::response& res, epee::json_rpc::error& error_resp, connection_context& cntx);
- bool on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res, epee::json_rpc::error& error_resp, connection_context& cntx);
- bool on_get_block_header_by_hash(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response& res, epee::json_rpc::error& error_resp, connection_context& cntx);
- bool on_get_block_header_by_height(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response& res, epee::json_rpc::error& error_resp, connection_context& cntx);
- bool on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp, connection_context& cntx);
- bool on_get_info_json(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, epee::json_rpc::error& error_resp, connection_context& cntx);
+ bool on_getblockcount(const COMMAND_RPC_GETBLOCKCOUNT::request& req, COMMAND_RPC_GETBLOCKCOUNT::response& res);
+ bool on_getblockhash(const COMMAND_RPC_GETBLOCKHASH::request& req, COMMAND_RPC_GETBLOCKHASH::response& res, epee::json_rpc::error& error_resp);
+ bool on_getblocktemplate(const COMMAND_RPC_GETBLOCKTEMPLATE::request& req, COMMAND_RPC_GETBLOCKTEMPLATE::response& res, epee::json_rpc::error& error_resp);
+ bool on_submitblock(const COMMAND_RPC_SUBMITBLOCK::request& req, COMMAND_RPC_SUBMITBLOCK::response& res, epee::json_rpc::error& error_resp);
+ bool on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res, epee::json_rpc::error& error_resp);
+ bool on_get_block_header_by_hash(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response& res, epee::json_rpc::error& error_resp);
+ bool on_get_block_header_by_height(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response& res, epee::json_rpc::error& error_resp);
+ bool on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp);
+ bool on_get_info_json(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, epee::json_rpc::error& error_resp);
//-----------------------
+
+private:
+
bool handle_command_line(
const boost::program_options::variables_map& vm
);
diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h
index 9fea933cb..5cb547521 100644
--- a/src/rpc/core_rpc_server_commands_defs.h
+++ b/src/rpc/core_rpc_server_commands_defs.h
@@ -39,6 +39,7 @@ namespace cryptonote
//-----------------------------------------------
#define CORE_RPC_STATUS_OK "OK"
#define CORE_RPC_STATUS_BUSY "BUSY"
+#define CORE_RPC_STATUS_NOT_MINING "NOT MINING"
struct COMMAND_RPC_GET_HEIGHT
{
@@ -440,7 +441,7 @@ namespace cryptonote
KV_SERIALIZE(reward)
END_KV_SERIALIZE_MAP()
};
-
+
struct COMMAND_RPC_GET_LAST_BLOCK_HEADER
{
struct request
@@ -510,6 +511,135 @@ namespace cryptonote
};
+ struct peer {
+ uint64_t id;
+ uint32_t ip;
+ uint16_t port;
+ uint64_t last_seen;
+
+ peer() = default;
+
+ peer(uint64_t id, uint32_t ip, uint16_t port, uint64_t last_seen)
+ : id(id), ip(ip), port(port), last_seen(last_seen)
+ {}
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(id)
+ KV_SERIALIZE(ip)
+ KV_SERIALIZE(port)
+ KV_SERIALIZE(last_seen)
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct COMMAND_RPC_GET_PEER_LIST
+ {
+ struct request
+ {
+ BEGIN_KV_SERIALIZE_MAP()
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::string status;
+ std::vector<peer> white_list;
+ std::vector<peer> gray_list;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ KV_SERIALIZE(white_list)
+ KV_SERIALIZE(gray_list)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
+
+ struct COMMAND_RPC_SET_LOG_HASH_RATE
+ {
+ struct request
+ {
+ bool visible;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(visible)
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::string status;
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
+
+ struct COMMAND_RPC_SET_LOG_LEVEL
+ {
+ struct request
+ {
+ int8_t level;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(level)
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::string status;
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
+
+ struct tx_info
+ {
+ std::string id_hash;
+ std::string tx_json; // TODO - expose this data directly
+ uint64_t blob_size;
+ uint64_t fee;
+ std::string max_used_block_id_hash;
+ uint64_t max_used_block_height;
+ bool kept_by_block;
+ uint64_t last_failed_height;
+ std::string last_failed_id_hash;
+ uint64_t receive_time;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(id_hash)
+ KV_SERIALIZE(tx_json)
+ KV_SERIALIZE(blob_size)
+ KV_SERIALIZE(fee)
+ KV_SERIALIZE(max_used_block_id_hash)
+ KV_SERIALIZE(max_used_block_height)
+ KV_SERIALIZE(kept_by_block)
+ KV_SERIALIZE(last_failed_height)
+ KV_SERIALIZE(last_failed_id_hash)
+ KV_SERIALIZE(receive_time)
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct COMMAND_RPC_GET_TRANSACTION_POOL
+ {
+ struct request
+ {
+ BEGIN_KV_SERIALIZE_MAP()
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::string status;
+ std::vector<tx_info> transactions;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ KV_SERIALIZE(transactions)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
+
struct COMMAND_RPC_GET_CONNECTIONS
{
struct request
@@ -530,5 +660,48 @@ namespace cryptonote
};
};
+
+ struct COMMAND_RPC_GET_BLOCK_HEADERS_RANGE
+ {
+ struct request
+ {
+ uint64_t start_height;
+ uint64_t end_height;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(start_height)
+ KV_SERIALIZE(end_height)
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::string status;
+ std::vector<block_header_responce> headers;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ KV_SERIALIZE(headers)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
+
+ struct COMMAND_RPC_STOP_DAEMON
+ {
+ struct request
+ {
+ BEGIN_KV_SERIALIZE_MAP()
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::string status;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
}
diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp
index 2a0e0e2f4..4ecf00c92 100644
--- a/src/simplewallet/simplewallet.cpp
+++ b/src/simplewallet/simplewallet.cpp
@@ -67,7 +67,7 @@ namespace po = boost::program_options;
#define EXTENDED_LOGS_FILE "wallet_details.log"
-unsigned int epee::g_test_dbg_lock_sleep = 0;
+#define DEFAULT_MIX 3
namespace
{
@@ -281,7 +281,7 @@ simple_wallet::simple_wallet()
m_cmd_binder.set_handler("incoming_transfers", boost::bind(&simple_wallet::show_incoming_transfers, this, _1), "incoming_transfers [available|unavailable] - Show incoming transfers - all of them or filter them by availability");
m_cmd_binder.set_handler("payments", boost::bind(&simple_wallet::show_payments, this, _1), "payments <payment_id_1> [<payment_id_2> ... <payment_id_N>] - Show payments <payment_id_1>, ... <payment_id_N>");
m_cmd_binder.set_handler("bc_height", boost::bind(&simple_wallet::show_blockchain_height, this, _1), "Show blockchain height");
- m_cmd_binder.set_handler("transfer", boost::bind(&simple_wallet::transfer, this, _1), "transfer <mixin_count> <addr_1> <amount_1> [<addr_2> <amount_2> ... <addr_N> <amount_N>] [payment_id] - Transfer <amount_1>,... <amount_N> to <address_1>,... <address_N>, respectively. <mixin_count> is the number of transactions yours is indistinguishable from (from 0 to maximum available)");
+ m_cmd_binder.set_handler("transfer", boost::bind(&simple_wallet::transfer, this, _1), "transfer [<mixin_count>] <addr_1> <amount_1> [<addr_2> <amount_2> ... <addr_N> <amount_N>] [payment_id] - Transfer <amount_1>,... <amount_N> to <address_1>,... <address_N>, respectively. <mixin_count> is the number of transactions yours is indistinguishable from (from 0 to maximum available)");
m_cmd_binder.set_handler("set_log", boost::bind(&simple_wallet::set_log, this, _1), "set_log <level> - Change current log detalization level, <level> is a number 0-4");
m_cmd_binder.set_handler("address", boost::bind(&simple_wallet::print_address, this, _1), "Show current wallet public address");
m_cmd_binder.set_handler("save", boost::bind(&simple_wallet::save, this, _1), "Save wallet synchronized data");
@@ -1068,19 +1068,24 @@ bool simple_wallet::transfer(const std::vector<std::string> &args_)
return true;
std::vector<std::string> local_args = args_;
- if(local_args.size() < 3)
- {
- fail_msg_writer() << "wrong number of arguments, expected at least 3, got " << local_args.size();
- return true;
- }
size_t fake_outs_count;
- if(!epee::string_tools::get_xtype_from_string(fake_outs_count, local_args[0]))
+ if(local_args.size() > 0) {
+ if(!epee::string_tools::get_xtype_from_string(fake_outs_count, local_args[0]))
+ {
+ fake_outs_count = DEFAULT_MIX;
+ }
+ else
+ {
+ local_args.erase(local_args.begin());
+ }
+ }
+
+ if(local_args.size() < 2)
{
- fail_msg_writer() << "mixin_count should be non-negative integer, got " << local_args[0];
- return true;
+ fail_msg_writer() << "wrong number of arguments";
+ return true;
}
- local_args.erase(local_args.begin());
std::vector<uint8_t> extra;
if (1 == local_args.size() % 2)
diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp
index e7940d57b..e39d9ba7b 100644
--- a/src/wallet/wallet_rpc_server.cpp
+++ b/src/wallet/wallet_rpc_server.cpp
@@ -85,7 +85,7 @@ namespace tools
return epee::http_server_impl_base<wallet_rpc_server, connection_context>::init(m_port, m_bind_ip);
}
//------------------------------------------------------------------------------------------------------------------------------
- bool wallet_rpc_server::on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er, connection_context& cntx)
+ bool wallet_rpc_server::on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er)
{
try
{
@@ -101,7 +101,7 @@ namespace tools
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool wallet_rpc_server::on_getaddress(const wallet_rpc::COMMAND_RPC_GET_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS::response& res, epee::json_rpc::error& er, connection_context& cntx)
+ bool wallet_rpc_server::on_getaddress(const wallet_rpc::COMMAND_RPC_GET_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS::response& res, epee::json_rpc::error& er)
{
try
{
@@ -161,7 +161,7 @@ namespace tools
}
//------------------------------------------------------------------------------------------------------------------------------
- bool wallet_rpc_server::on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::request& req, wallet_rpc::COMMAND_RPC_TRANSFER::response& res, epee::json_rpc::error& er, connection_context& cntx)
+ bool wallet_rpc_server::on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::request& req, wallet_rpc::COMMAND_RPC_TRANSFER::response& res, epee::json_rpc::error& er)
{
std::vector<cryptonote::tx_destination_entry> dsts;
@@ -219,7 +219,7 @@ namespace tools
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool wallet_rpc_server::on_transfer_split(const wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT::request& req, wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT::response& res, epee::json_rpc::error& er, connection_context& cntx)
+ bool wallet_rpc_server::on_transfer_split(const wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT::request& req, wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT::response& res, epee::json_rpc::error& er)
{
std::vector<cryptonote::tx_destination_entry> dsts;
@@ -273,7 +273,7 @@ namespace tools
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool wallet_rpc_server::on_store(const wallet_rpc::COMMAND_RPC_STORE::request& req, wallet_rpc::COMMAND_RPC_STORE::response& res, epee::json_rpc::error& er, connection_context& cntx)
+ bool wallet_rpc_server::on_store(const wallet_rpc::COMMAND_RPC_STORE::request& req, wallet_rpc::COMMAND_RPC_STORE::response& res, epee::json_rpc::error& er)
{
if (m_wallet.restricted())
{
@@ -295,7 +295,7 @@ namespace tools
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool wallet_rpc_server::on_get_payments(const wallet_rpc::COMMAND_RPC_GET_PAYMENTS::request& req, wallet_rpc::COMMAND_RPC_GET_PAYMENTS::response& res, epee::json_rpc::error& er, connection_context& cntx)
+ bool wallet_rpc_server::on_get_payments(const wallet_rpc::COMMAND_RPC_GET_PAYMENTS::request& req, wallet_rpc::COMMAND_RPC_GET_PAYMENTS::response& res, epee::json_rpc::error& er)
{
crypto::hash payment_id;
cryptonote::blobdata payment_id_blob;
@@ -332,7 +332,7 @@ namespace tools
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool wallet_rpc_server::on_get_bulk_payments(const wallet_rpc::COMMAND_RPC_GET_BULK_PAYMENTS::request& req, wallet_rpc::COMMAND_RPC_GET_BULK_PAYMENTS::response& res, epee::json_rpc::error& er, connection_context& cntx)
+ bool wallet_rpc_server::on_get_bulk_payments(const wallet_rpc::COMMAND_RPC_GET_BULK_PAYMENTS::request& req, wallet_rpc::COMMAND_RPC_GET_BULK_PAYMENTS::response& res, epee::json_rpc::error& er)
{
res.payments.clear();
@@ -397,7 +397,7 @@ namespace tools
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool wallet_rpc_server::on_incoming_transfers(const wallet_rpc::COMMAND_RPC_INCOMING_TRANSFERS::request& req, wallet_rpc::COMMAND_RPC_INCOMING_TRANSFERS::response& res, epee::json_rpc::error& er, connection_context& cntx)
+ bool wallet_rpc_server::on_incoming_transfers(const wallet_rpc::COMMAND_RPC_INCOMING_TRANSFERS::request& req, wallet_rpc::COMMAND_RPC_INCOMING_TRANSFERS::response& res, epee::json_rpc::error& er)
{
if(req.transfer_type.compare("all") != 0 && req.transfer_type.compare("available") != 0 && req.transfer_type.compare("unavailable") != 0)
{
@@ -450,7 +450,7 @@ namespace tools
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool wallet_rpc_server::on_query_key(const wallet_rpc::COMMAND_RPC_QUERY_KEY::request& req, wallet_rpc::COMMAND_RPC_QUERY_KEY::response& res, epee::json_rpc::error& er, connection_context& cntx)
+ bool wallet_rpc_server::on_query_key(const wallet_rpc::COMMAND_RPC_QUERY_KEY::request& req, wallet_rpc::COMMAND_RPC_QUERY_KEY::response& res, epee::json_rpc::error& er)
{
if (m_wallet.restricted())
{
diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h
index 06ebd23bf..cfdc1c147 100644
--- a/src/wallet/wallet_rpc_server.h
+++ b/src/wallet/wallet_rpc_server.h
@@ -74,20 +74,20 @@ namespace tools
END_URI_MAP2()
//json_rpc
- bool on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er, connection_context& cntx);
- bool on_getaddress(const wallet_rpc::COMMAND_RPC_GET_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS::response& res, epee::json_rpc::error& er, connection_context& cntx);
+ bool on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er);
+ bool on_getaddress(const wallet_rpc::COMMAND_RPC_GET_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS::response& res, epee::json_rpc::error& er);
bool validate_transfer(const std::list<wallet_rpc::transfer_destination> destinations, const std::string payment_id, std::vector<cryptonote::tx_destination_entry>& dsts, std::vector<uint8_t>& extra, epee::json_rpc::error& er);
- bool on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::request& req, wallet_rpc::COMMAND_RPC_TRANSFER::response& res, epee::json_rpc::error& er, connection_context& cntx);
- bool on_transfer_split(const wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT::request& req, wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT::response& res, epee::json_rpc::error& er, connection_context& cntx);
- bool on_store(const wallet_rpc::COMMAND_RPC_STORE::request& req, wallet_rpc::COMMAND_RPC_STORE::response& res, epee::json_rpc::error& er, connection_context& cntx);
- bool on_get_payments(const wallet_rpc::COMMAND_RPC_GET_PAYMENTS::request& req, wallet_rpc::COMMAND_RPC_GET_PAYMENTS::response& res, epee::json_rpc::error& er, connection_context& cntx);
- bool on_get_bulk_payments(const wallet_rpc::COMMAND_RPC_GET_BULK_PAYMENTS::request& req, wallet_rpc::COMMAND_RPC_GET_BULK_PAYMENTS::response& res, epee::json_rpc::error& er, connection_context& cntx);
- bool on_incoming_transfers(const wallet_rpc::COMMAND_RPC_INCOMING_TRANSFERS::request& req, wallet_rpc::COMMAND_RPC_INCOMING_TRANSFERS::response& res, epee::json_rpc::error& er, connection_context& cntx);
+ bool on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::request& req, wallet_rpc::COMMAND_RPC_TRANSFER::response& res, epee::json_rpc::error& er);
+ bool on_transfer_split(const wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT::request& req, wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT::response& res, epee::json_rpc::error& er);
+ bool on_store(const wallet_rpc::COMMAND_RPC_STORE::request& req, wallet_rpc::COMMAND_RPC_STORE::response& res, epee::json_rpc::error& er);
+ bool on_get_payments(const wallet_rpc::COMMAND_RPC_GET_PAYMENTS::request& req, wallet_rpc::COMMAND_RPC_GET_PAYMENTS::response& res, epee::json_rpc::error& er);
+ bool on_get_bulk_payments(const wallet_rpc::COMMAND_RPC_GET_BULK_PAYMENTS::request& req, wallet_rpc::COMMAND_RPC_GET_BULK_PAYMENTS::response& res, epee::json_rpc::error& er);
+ bool on_incoming_transfers(const wallet_rpc::COMMAND_RPC_INCOMING_TRANSFERS::request& req, wallet_rpc::COMMAND_RPC_INCOMING_TRANSFERS::response& res, epee::json_rpc::error& er);
bool handle_command_line(const boost::program_options::variables_map& vm);
//json rpc v2
- bool on_query_key(const wallet_rpc::COMMAND_RPC_QUERY_KEY::request& req, wallet_rpc::COMMAND_RPC_QUERY_KEY::response& res, epee::json_rpc::error& er, connection_context& cntx);
+ bool on_query_key(const wallet_rpc::COMMAND_RPC_QUERY_KEY::request& req, wallet_rpc::COMMAND_RPC_QUERY_KEY::response& res, epee::json_rpc::error& er);
wallet2& m_wallet;
std::string m_port;