aboutsummaryrefslogtreecommitdiff
path: root/src/daemon
diff options
context:
space:
mode:
authorAntonio Juarez <antonio.maria.juarez@live.com>2014-03-03 22:07:58 +0000
committerAntonio Juarez <antonio.maria.juarez@live.com>2014-03-03 22:07:58 +0000
commit296ae46ed8f8f6e5f986f978febad302e3df231a (patch)
tree1629164454a239308f33c9e12afb22e7f3cd8eeb /src/daemon
parentchanged name (diff)
downloadmonero-296ae46ed8f8f6e5f986f978febad302e3df231a.tar.xz
moved all stuff to github
Diffstat (limited to 'src/daemon')
-rw-r--r--src/daemon/daemon.cpp234
-rw-r--r--src/daemon/daemon_commands_handler.h291
2 files changed, 525 insertions, 0 deletions
diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp
new file mode 100644
index 000000000..d1ac3714b
--- /dev/null
+++ b/src/daemon/daemon.cpp
@@ -0,0 +1,234 @@
+// 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.
+
+// node.cpp : Defines the entry point for the console application.
+//
+
+
+#include "include_base_utils.h"
+#include "version.h"
+
+using namespace epee;
+
+#include <boost/program_options.hpp>
+
+#include "crypto/hash.h"
+#include "console_handler.h"
+#include "p2p/net_node.h"
+#include "cryptonote_core/checkpoints_create.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
+
+namespace po = boost::program_options;
+
+
+BOOST_CLASS_VERSION(nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >, 1);
+
+namespace
+{
+ 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};
+}
+
+bool command_line_preprocessor(const boost::program_options::variables_map& vm);
+
+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...");
+
+ TRY_ENTRY();
+
+ po::options_description desc_cmd_only("Command line options");
+ po::options_description desc_cmd_sett("Command line options and settings options");
+
+ 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, tools::get_default_data_dir());
+ command_line::add_arg(desc_cmd_only, arg_config_file);
+
+ command_line::add_arg(desc_cmd_sett, arg_log_file);
+ command_line::add_arg(desc_cmd_sett, arg_log_level);
+
+ 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);
+
+ if (command_line::get_arg(vm, command_line::arg_help))
+ {
+ std::cout << CRYPTONOTE_NAME << " v" << PROJECT_VERSION_LONG << ENDL << ENDL;
+ std::cout << desc_options << std::endl;
+ return false;
+ }
+
+ std::string data_dir = command_line::get_arg(vm, command_line::arg_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))
+ {
+ po::store(po::parse_config_file<char>(config_path.string<std::string>().c_str(), desc_cmd_sett), vm);
+ }
+ po::notify(vm);
+
+ return true;
+ });
+ if (!r)
+ return 1;
+
+ //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());
+
+ if (command_line_preprocessor(vm))
+ {
+ 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");
+
+ //create objects and link them
+ cryptonote::core ccore(NULL);
+ ccore.set_checkpoints(std::move(checkpoints));
+ cryptonote::t_cryptonote_protocol_handler<cryptonote::core> cprotocol(ccore, NULL);
+ nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> > p2psrv(cprotocol);
+ cryptonote::core_rpc_server rpc_server(ccore, p2psrv);
+ cprotocol.set_p2p_endpoint(&p2psrv);
+ ccore.set_cryptonote_protocol(&cprotocol);
+ daemon_cmmands_handler dch(p2psrv);
+
+ //initialize objects
+ LOG_PRINT_L0("Initializing p2p server...");
+ res = p2psrv.init(vm);
+ CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize p2p server.");
+ LOG_PRINT_L0("P2p server initialized OK");
+
+ LOG_PRINT_L0("Initializing cryptonote protocol...");
+ res = cprotocol.init(vm);
+ CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize cryptonote protocol.");
+ LOG_PRINT_L0("Cryptonote 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);
+
+ //initialize core here
+ LOG_PRINT_L0("Initializing core...");
+ res = ccore.init(vm);
+ CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize core");
+ LOG_PRINT_L0("Core initialized OK");
+
+ // start components
+ dch.start_handling();
+
+ 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");
+
+ LOG_PRINT_L0("Starting p2p net loop...");
+ p2psrv.run();
+ LOG_PRINT_L0("p2p net loop stopped");
+
+ //stop components
+ 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 cryptonote_protocol...");
+ cprotocol.deinit();
+ LOG_PRINT_L0("Deinitializing p2p...");
+ p2psrv.deinit();
+
+
+ ccore.set_cryptonote_protocol(NULL);
+ cprotocol.set_p2p_endpoint(NULL);
+
+ LOG_PRINT("Node stopped.", LOG_LEVEL_0);
+ return 0;
+
+ CATCH_ENTRY_L0("main", 1);
+}
+
+bool command_line_preprocessor(const boost::program_options::variables_map& vm)
+{
+ bool exit = false;
+ if (command_line::get_arg(vm, command_line::arg_version))
+ {
+ std::cout << CRYPTONOTE_NAME << PROJECT_VERSION_LONG << ENDL;
+ exit = true;
+ }
+ if (command_line::get_arg(vm, arg_os_version))
+ {
+ std::cout << "OS: " << tools::get_os_version_string() << ENDL;
+ exit = true;
+ }
+
+ if (exit)
+ {
+ return true;
+ }
+
+ 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)
+ {
+ 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);
+ LOG_PRINT_L0("LOG_LEVEL set to " << new_log_level);
+ }
+
+ return false;
+}
diff --git a/src/daemon/daemon_commands_handler.h b/src/daemon/daemon_commands_handler.h
new file mode 100644
index 000000000..2edd5d414
--- /dev/null
+++ b/src/daemon/daemon_commands_handler.h
@@ -0,0 +1,291 @@
+// 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>
+
+#include "console_handler.h"
+#include "p2p/net_node.h"
+#include "cryptonote_protocol/cryptonote_protocol_handler.h"
+#include "common/util.h"
+#include "crypto/hash.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):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");
+ m_cmd_binder.set_handler("print_cn", boost::bind(&daemon_cmmands_handler::print_cn, this, _1), "Print connections");
+ m_cmd_binder.set_handler("print_bc", boost::bind(&daemon_cmmands_handler::print_bc, this, _1), "Print blockchain info in a given blocks range, print_bc <begin_height> [<end_height>]");
+ //m_cmd_binder.set_handler("print_bci", boost::bind(&daemon_cmmands_handler::print_bci, this, _1));
+ //m_cmd_binder.set_handler("print_bc_outs", boost::bind(&daemon_cmmands_handler::print_bc_outs, this, _1));
+ m_cmd_binder.set_handler("print_block", boost::bind(&daemon_cmmands_handler::print_block, this, _1), "Print block, print_block <block_hash> | <block_height>");
+ m_cmd_binder.set_handler("print_tx", boost::bind(&daemon_cmmands_handler::print_tx, this, _1), "Print transaction, print_tx <transaction_hash>");
+ m_cmd_binder.set_handler("start_mining", boost::bind(&daemon_cmmands_handler::start_mining, this, _1), "Start mining for specified address, start_mining <addr> [threads=1]");
+ m_cmd_binder.set_handler("stop_mining", boost::bind(&daemon_cmmands_handler::stop_mining, this, _1), "Stop mining");
+ m_cmd_binder.set_handler("print_pool", boost::bind(&daemon_cmmands_handler::print_pool, this, _1), "Print transaction pool (long format)");
+ m_cmd_binder.set_handler("print_pool_sh", boost::bind(&daemon_cmmands_handler::print_pool_sh, this, _1), "Print transaction pool (short format)");
+ m_cmd_binder.set_handler("show_hr", boost::bind(&daemon_cmmands_handler::show_hr, this, _1), "Start showing hash rate");
+ m_cmd_binder.set_handler("hide_hr", boost::bind(&daemon_cmmands_handler::hide_hr, this, _1), "Stop showing hash rate");
+ m_cmd_binder.set_handler("save", boost::bind(&daemon_cmmands_handler::save, this, _1), "Save blockchain");
+ }
+
+ bool start_handling()
+ {
+ m_cmd_binder.start_handling(&m_srv, "");
+ return true;
+ }
+
+private:
+ epee::srv_console_handlers_binder<nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> > > m_cmd_binder;
+
+ //--------------------------------------------------------------------------------
+ std::string get_commands_str()
+ {
+ std::stringstream ss;
+ ss << "Commands: " << ENDL;
+ std::string usage = m_cmd_binder.get_usage();
+ boost::replace_all(usage, "\n", "\n ");
+ usage.insert(0, " ");
+ ss << usage << ENDL;
+ return ss.str();
+ }
+ //--------------------------------------------------------------------------------
+ bool help(const std::vector<std::string>& args)
+ {
+ std::cout << get_commands_str() << ENDL;
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_pl(const std::vector<std::string>& args)
+ {
+ m_srv.log_peerlist();
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool save(const std::vector<std::string>& args)
+ {
+ m_srv.get_payload_object().get_core().get_blockchain_storage().store_blockchain();
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool show_hr(const std::vector<std::string>& args)
+ {
+ m_srv.get_payload_object().get_core().get_miner().do_print_hashrate(true);
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool hide_hr(const std::vector<std::string>& args)
+ {
+ m_srv.get_payload_object().get_core().get_miner().do_print_hashrate(false);
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_bc_outs(const std::vector<std::string>& args)
+ {
+ if(args.size() != 1)
+ {
+ std::cout << "need file path as parameter" << ENDL;
+ return true;
+ }
+ m_srv.get_payload_object().get_core().print_blockchain_outs(args[0]);
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_cn(const std::vector<std::string>& args)
+ {
+ m_srv.log_connections();
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_bc(const std::vector<std::string>& args)
+ {
+ if(!args.size())
+ {
+ std::cout << "need block index parameter" << ENDL;
+ return false;
+ }
+ uint64_t start_index = 0;
+ uint64_t end_block_parametr = m_srv.get_payload_object().get_core().get_current_blockchain_height();
+ if(!string_tools::get_xtype_from_string(start_index, args[0]))
+ {
+ std::cout << "wrong starter block index parameter" << ENDL;
+ return false;
+ }
+ if(args.size() >1 && !string_tools::get_xtype_from_string(end_block_parametr, args[1]))
+ {
+ std::cout << "wrong end block index parameter" << ENDL;
+ return false;
+ }
+
+ m_srv.get_payload_object().get_core().print_blockchain(start_index, end_block_parametr);
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_bci(const std::vector<std::string>& args)
+ {
+ m_srv.get_payload_object().get_core().print_blockchain_index();
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ template <typename T>
+ static bool print_as_json(T& obj)
+ {
+ std::cout << cryptonote::obj_to_json_str(obj) << ENDL;
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_block_by_height(uint64_t height)
+ {
+ std::list<cryptonote::block> blocks;
+ m_srv.get_payload_object().get_core().get_blocks(height, 1, blocks);
+
+ if (1 == blocks.size())
+ {
+ cryptonote::block& block = blocks.front();
+ std::cout << "block_id: " << get_block_hash(block) << ENDL;
+ print_as_json(block);
+ }
+ else
+ {
+ uint64_t current_height;
+ crypto::hash top_id;
+ m_srv.get_payload_object().get_core().get_blockchain_top(current_height, top_id);
+ std::cout << "block wasn't found. Current block chain height: " << current_height << ", requested: " << height << std::endl;
+ return false;
+ }
+
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_block_by_hash(const std::string& arg)
+ {
+ crypto::hash block_hash;
+ if (!parse_hash256(arg, block_hash))
+ {
+ return false;
+ }
+
+ std::list<crypto::hash> block_ids;
+ block_ids.push_back(block_hash);
+ std::list<cryptonote::block> blocks;
+ std::list<crypto::hash> missed_ids;
+ m_srv.get_payload_object().get_core().get_blocks(block_ids, blocks, missed_ids);
+
+ if (1 == blocks.size())
+ {
+ cryptonote::block block = blocks.front();
+ print_as_json(block);
+ }
+ else
+ {
+ std::cout << "block wasn't found: " << arg << std::endl;
+ return false;
+ }
+
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_block(const std::vector<std::string>& args)
+ {
+ if (args.empty())
+ {
+ std::cout << "expected: print_block (<block_hash> | <block_height>)" << std::endl;
+ return true;
+ }
+
+ const std::string& arg = args.front();
+ try
+ {
+ uint64_t height = boost::lexical_cast<uint64_t>(arg);
+ print_block_by_height(height);
+ }
+ catch (boost::bad_lexical_cast&)
+ {
+ print_block_by_hash(arg);
+ }
+
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_tx(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))
+ {
+ return true;
+ }
+
+ std::vector<crypto::hash> tx_ids;
+ tx_ids.push_back(tx_hash);
+ std::list<cryptonote::transaction> txs;
+ std::list<crypto::hash> missed_ids;
+ m_srv.get_payload_object().get_core().get_transactions(tx_ids, txs, missed_ids);
+
+ if (1 == txs.size())
+ {
+ cryptonote::transaction tx = txs.front();
+ print_as_json(tx);
+ }
+ else
+ {
+ std::cout << "transaction wasn't found: <" << str_hash << '>' << std::endl;
+ }
+
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_pool(const std::vector<std::string>& args)
+ {
+ LOG_PRINT_L0("Pool state: " << ENDL << m_srv.get_payload_object().get_core().print_pool(false));
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool print_pool_sh(const std::vector<std::string>& args)
+ {
+ LOG_PRINT_L0("Pool state: " << ENDL << m_srv.get_payload_object().get_core().print_pool(true));
+ return true;
+ } //--------------------------------------------------------------------------------
+ bool start_mining(const std::vector<std::string>& args)
+ {
+ if(!args.size())
+ {
+ std::cout << "target account address for mining is not set" << std::endl;
+ return true;
+ }
+
+ cryptonote::account_public_address adr;
+ if(!cryptonote::get_account_address_from_str(adr, args.front()))
+ {
+ std::cout << "target account address has wrong format" << std::endl;
+ return true;
+ }
+ size_t threads_count = 1;
+ if(args.size() > 1)
+ {
+ string_tools::get_xtype_from_string(threads_count, args[1]);
+ }
+
+ m_srv.get_payload_object().get_core().get_miner().start(adr, threads_count);
+ return true;
+ }
+ //--------------------------------------------------------------------------------
+ bool stop_mining(const std::vector<std::string>& args)
+ {
+ m_srv.get_payload_object().get_core().get_miner().stop();
+ return true;
+ }
+};