add daemonizer to rpc wallet

release-v0.5.1
jcktm 6 years ago
parent d743994086
commit c336d0f217

@ -90,6 +90,8 @@ target_link_libraries(wallet_rpc_server
cncrypto cncrypto
common common
version version
daemonizer
${EPEE_READLINE}
${Boost_CHRONO_LIBRARY} ${Boost_CHRONO_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_FILESYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY}

@ -51,6 +51,7 @@ using namespace epee;
#include "mnemonics/electrum-words.h" #include "mnemonics/electrum-words.h"
#include "rpc/rpc_args.h" #include "rpc/rpc_args.h"
#include "rpc/core_rpc_server_commands_defs.h" #include "rpc/core_rpc_server_commands_defs.h"
#include "daemonizer/daemonizer.h"
#undef MONERO_DEFAULT_LOG_CATEGORY #undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "wallet.rpc" #define MONERO_DEFAULT_LOG_CATEGORY "wallet.rpc"
@ -3265,65 +3266,43 @@ namespace tools
//------------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------------
} }
int main(int argc, char** argv) { class t_daemon
namespace po = boost::program_options; {
private:
const auto arg_wallet_file = wallet_args::arg_wallet_file(); const boost::program_options::variables_map& vm;
const auto arg_from_json = wallet_args::arg_generate_from_json();
po::options_description desc_params(wallet_args::tr("Wallet options"));
tools::wallet2::init_options(desc_params);
command_line::add_arg(desc_params, arg_rpc_bind_port);
command_line::add_arg(desc_params, arg_disable_rpc_login);
command_line::add_arg(desc_params, arg_restricted);
cryptonote::rpc_args::init_options(desc_params);
command_line::add_arg(desc_params, arg_wallet_file);
command_line::add_arg(desc_params, arg_from_json);
command_line::add_arg(desc_params, arg_wallet_dir);
command_line::add_arg(desc_params, arg_prompt_for_password);
boost::optional<po::variables_map> vm; public:
bool should_terminate = false; t_daemon(boost::program_options::variables_map const & _vm)
std::tie(vm, should_terminate) = wallet_args::main( : vm(_vm)
argc, argv,
"monero-wallet-rpc [--wallet-file=<file>|--generate-from-json=<file>|--wallet-dir=<directory>] [--rpc-bind-port=<port>]",
tools::wallet_rpc_server::tr("This is the RPC monero wallet. It needs to connect to a monero\ndaemon to work correctly."),
desc_params,
po::positional_options_description(),
[](const std::string &s, bool emphasis){ epee::set_console_color(emphasis ? epee::console_color_white : epee::console_color_default, true); std::cout << s << std::endl; if (emphasis) epee::reset_console_color(); },
"monero-wallet-rpc.log",
true
);
if (!vm)
{ {
return 1;
}
if (should_terminate)
{
return 0;
} }
bool run()
{
std::unique_ptr<tools::wallet2> wal; std::unique_ptr<tools::wallet2> wal;
try try
{ {
const bool testnet = tools::wallet2::has_testnet_option(*vm); const bool testnet = tools::wallet2::has_testnet_option(vm);
const bool stagenet = tools::wallet2::has_stagenet_option(*vm); const bool stagenet = tools::wallet2::has_stagenet_option(vm);
if (testnet && stagenet) if (testnet && stagenet)
{ {
MERROR(tools::wallet_rpc_server::tr("Can't specify more than one of --testnet and --stagenet")); MERROR(tools::wallet_rpc_server::tr("Can't specify more than one of --testnet and --stagenet"));
return 1; return false;
} }
const auto wallet_file = command_line::get_arg(*vm, arg_wallet_file); const auto arg_wallet_file = wallet_args::arg_wallet_file();
const auto from_json = command_line::get_arg(*vm, arg_from_json); const auto arg_from_json = wallet_args::arg_generate_from_json();
const auto wallet_dir = command_line::get_arg(*vm, arg_wallet_dir);
const auto prompt_for_password = command_line::get_arg(*vm, arg_prompt_for_password); const auto wallet_file = command_line::get_arg(vm, arg_wallet_file);
const auto from_json = command_line::get_arg(vm, arg_from_json);
const auto wallet_dir = command_line::get_arg(vm, arg_wallet_dir);
const auto prompt_for_password = command_line::get_arg(vm, arg_prompt_for_password);
const auto password_prompt = prompt_for_password ? password_prompter : nullptr; const auto password_prompt = prompt_for_password ? password_prompter : nullptr;
if(!wallet_file.empty() && !from_json.empty()) if(!wallet_file.empty() && !from_json.empty())
{ {
LOG_ERROR(tools::wallet_rpc_server::tr("Can't specify more than one of --wallet-file and --generate-from-json")); LOG_ERROR(tools::wallet_rpc_server::tr("Can't specify more than one of --wallet-file and --generate-from-json"));
return 1; return false;
} }
if (!wallet_dir.empty()) if (!wallet_dir.empty())
@ -3335,29 +3314,29 @@ int main(int argc, char** argv) {
if (wallet_file.empty() && from_json.empty()) if (wallet_file.empty() && from_json.empty())
{ {
LOG_ERROR(tools::wallet_rpc_server::tr("Must specify --wallet-file or --generate-from-json or --wallet-dir")); LOG_ERROR(tools::wallet_rpc_server::tr("Must specify --wallet-file or --generate-from-json or --wallet-dir"));
return 1; return false;
} }
LOG_PRINT_L0(tools::wallet_rpc_server::tr("Loading wallet...")); LOG_PRINT_L0(tools::wallet_rpc_server::tr("Loading wallet..."));
if(!wallet_file.empty()) if(!wallet_file.empty())
{ {
wal = tools::wallet2::make_from_file(*vm, true, wallet_file, password_prompt).first; wal = tools::wallet2::make_from_file(vm, true, wallet_file, password_prompt).first;
} }
else else
{ {
try try
{ {
wal = tools::wallet2::make_from_json(*vm, true, from_json, password_prompt); wal = tools::wallet2::make_from_json(vm, true, from_json, password_prompt);
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
MERROR("Error creating wallet: " << e.what()); MERROR("Error creating wallet: " << e.what());
return 1; return false;
} }
} }
if (!wal) if (!wal)
{ {
return 1; return false;
} }
bool quit = false; bool quit = false;
@ -3374,20 +3353,20 @@ int main(int argc, char** argv) {
MINFO(tools::wallet_rpc_server::tr("Saving wallet...")); MINFO(tools::wallet_rpc_server::tr("Saving wallet..."));
wal->store(); wal->store();
MINFO(tools::wallet_rpc_server::tr("Successfully saved")); MINFO(tools::wallet_rpc_server::tr("Successfully saved"));
return 1; return false;
} }
MINFO(tools::wallet_rpc_server::tr("Successfully loaded")); MINFO(tools::wallet_rpc_server::tr("Successfully loaded"));
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
LOG_ERROR(tools::wallet_rpc_server::tr("Wallet initialization failed: ") << e.what()); LOG_ERROR(tools::wallet_rpc_server::tr("Wallet initialization failed: ") << e.what());
return 1; return false;
} }
just_dir: just_dir:
tools::wallet_rpc_server wrpc; tools::wallet_rpc_server wrpc;
if (wal) wrpc.set_wallet(wal.release()); if (wal) wrpc.set_wallet(wal.release());
bool r = wrpc.init(&(vm.get())); bool r = wrpc.init(&vm);
CHECK_AND_ASSERT_MES(r, 1, tools::wallet_rpc_server::tr("Failed to initialize wallet RPC server")); CHECK_AND_ASSERT_MES(r, false, tools::wallet_rpc_server::tr("Failed to initialize wallet RPC server"));
tools::signal_handler::install([&wrpc](int) { tools::signal_handler::install([&wrpc](int) {
wrpc.send_stop_signal(); wrpc.send_stop_signal();
}); });
@ -3399,7 +3378,7 @@ just_dir:
catch (const std::exception &e) catch (const std::exception &e)
{ {
LOG_ERROR(tools::wallet_rpc_server::tr("Failed to run wallet: ") << e.what()); LOG_ERROR(tools::wallet_rpc_server::tr("Failed to run wallet: ") << e.what());
return 1; return false;
} }
LOG_PRINT_L0(tools::wallet_rpc_server::tr("Stopped wallet RPC server")); LOG_PRINT_L0(tools::wallet_rpc_server::tr("Stopped wallet RPC server"));
try try
@ -3411,7 +3390,81 @@ just_dir:
catch (const std::exception& e) catch (const std::exception& e)
{ {
LOG_ERROR(tools::wallet_rpc_server::tr("Failed to save wallet: ") << e.what()); LOG_ERROR(tools::wallet_rpc_server::tr("Failed to save wallet: ") << e.what());
return false;
}
return true;
}
};
class t_executor final
{
public:
static std::string const NAME;
std::string const & name()
{
return NAME;
}
t_daemon create_daemon(boost::program_options::variables_map const & vm)
{
return t_daemon(vm);
}
bool run_non_interactive(boost::program_options::variables_map const & vm)
{
return t_daemon(vm).run();
}
bool run_interactive(boost::program_options::variables_map const & vm)
{
return t_daemon(vm).run();
}
};
std::string const t_executor::NAME = "Wallet RPC Daemon";
int main(int argc, char** argv) {
namespace po = boost::program_options;
const auto arg_wallet_file = wallet_args::arg_wallet_file();
const auto arg_from_json = wallet_args::arg_generate_from_json();
po::options_description hidden_options("Hidden");
po::options_description desc_params(wallet_args::tr("Wallet options"));
tools::wallet2::init_options(desc_params);
command_line::add_arg(desc_params, arg_rpc_bind_port);
command_line::add_arg(desc_params, arg_disable_rpc_login);
command_line::add_arg(desc_params, arg_restricted);
cryptonote::rpc_args::init_options(desc_params);
command_line::add_arg(desc_params, arg_wallet_file);
command_line::add_arg(desc_params, arg_from_json);
command_line::add_arg(desc_params, arg_wallet_dir);
command_line::add_arg(desc_params, arg_prompt_for_password);
daemonizer::init_options(hidden_options, desc_params);
boost::optional<po::variables_map> vm;
bool should_terminate = false;
std::tie(vm, should_terminate) = wallet_args::main(
argc, argv,
"monero-wallet-rpc [--wallet-file=<file>|--generate-from-json=<file>|--wallet-dir=<directory>] [--rpc-bind-port=<port>]",
tools::wallet_rpc_server::tr("This is the RPC monero wallet. It needs to connect to a monero\ndaemon to work correctly."),
desc_params,
po::positional_options_description(),
[](const std::string &s, bool emphasis){ epee::set_console_color(emphasis ? epee::console_color_white : epee::console_color_default, true); std::cout << s << std::endl; if (emphasis) epee::reset_console_color(); },
"monero-wallet-rpc.log",
true
);
if (!vm)
{
return 1; return 1;
} }
if (should_terminate)
{
return 0; return 0;
}
return daemonizer::daemonize(argc, const_cast<const char**>(argv), t_executor{}, *vm) ? 0 : 1;
} }

Loading…
Cancel
Save