Skip to content

Commit

Permalink
Read config file for CLI commands (#2637)
Browse files Browse the repository at this point in the history
  • Loading branch information
wezrule committed Mar 10, 2020
1 parent f7e50b3 commit b23d7c3
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 143 deletions.
2 changes: 1 addition & 1 deletion nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3578,7 +3578,7 @@ TEST (node, bandwidth_limiter)
nano::publish message (genesis.open);
auto message_size = message.to_bytes ()->size ();
auto message_limit = 4; // must be multiple of the number of channels
nano::node_config node_config (24000, system.logging);
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.bandwidth_limit = message_limit * message_size;
auto & node = *system.add_node (node_config);
auto channel1 (node.network.udp_channels.create (node.network.endpoint ()));
Expand Down
4 changes: 2 additions & 2 deletions nano/core_test/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TEST (socket, drop_policy)
{
auto node_flags = nano::inactive_node_flag_defaults ();
node_flags.read_only = false;
nano::inactive_node inactivenode (nano::unique_path (), nano::get_available_port (), node_flags);
nano::inactive_node inactivenode (nano::unique_path (), node_flags);
auto node = inactivenode.node;

nano::thread_runner runner (node->io_ctx, 1);
Expand Down Expand Up @@ -61,7 +61,7 @@ TEST (socket, concurrent_writes)
{
auto node_flags = nano::inactive_node_flag_defaults ();
node_flags.read_only = false;
nano::inactive_node inactivenode (nano::unique_path (), nano::get_available_port (), node_flags);
nano::inactive_node inactivenode (nano::unique_path (), node_flags);
auto node = inactivenode.node;

// This gives more realistic execution than using system#poll, allowing writes to
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/wallets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ TEST (wallets, reload)
ASSERT_EQ (1, node1.wallets.items.size ());
{
nano::lock_guard<std::mutex> lock_wallet (node1.wallets.mutex);
nano::inactive_node node (node1.application_path, nano::get_available_port ());
nano::inactive_node node (node1.application_path);
auto wallet (node.node->wallets.create (one));
ASSERT_NE (wallet, nullptr);
}
Expand Down
12 changes: 3 additions & 9 deletions nano/lib/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,11 @@ void nano::move_all_files_to_dir (boost::filesystem::path const & from, boost::f
*/
void assert_internal (const char * check_expr, const char * file, unsigned int line, bool is_release_assert)
{
// Output stack trace
std::cerr << "Assertion (" << check_expr << ") failed " << file << ":" << line << "\n\n";

// Output stack trace to cerr
auto backtrace_str = nano::generate_stacktrace ();
// Windows on Actions only outputs the first line of the stacktrace from standard error, use standard output
#if (defined(_WIN32) && CI)
std::cout << backtrace_str << std::endl;
#else
std::cerr << backtrace_str << std::endl;
#endif

std::cerr << "Assertion (" << check_expr << ") failed " << file << ":" << line << "\n"
<< std::endl;

// "abort" at the end of this function will go into any signal handlers (the daemon ones will generate a stack trace and load memory address files on non-Windows systems).
// As there is no async-signal-safe way to generate stacktraces on Windows it must be done before aborting
Expand Down
120 changes: 65 additions & 55 deletions nano/nano_node/entry.cpp

Large diffs are not rendered by default.

120 changes: 72 additions & 48 deletions nano/node/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ std::error_code nano::update_flags (nano::node_flags & flags_a, boost::program_o
flags_a.disable_lazy_bootstrap = (vm.count ("disable_lazy_bootstrap") > 0);
flags_a.disable_legacy_bootstrap = (vm.count ("disable_legacy_bootstrap") > 0);
flags_a.disable_wallet_bootstrap = (vm.count ("disable_wallet_bootstrap") > 0);
flags_a.disable_bootstrap_listener = (vm.count ("disable_bootstrap_listener") > 0);
flags_a.disable_tcp_realtime = (vm.count ("disable_tcp_realtime") > 0);
if (!flags_a.inactive_node)
{
flags_a.disable_bootstrap_listener = (vm.count ("disable_bootstrap_listener") > 0);
flags_a.disable_tcp_realtime = (vm.count ("disable_tcp_realtime") > 0);
}
flags_a.disable_providing_telemetry_metrics = (vm.count ("disable_providing_telemetry_metrics") > 0);
if ((vm.count ("disable_udp") > 0) && (vm.count ("enable_udp") > 0))
{
Expand Down Expand Up @@ -193,7 +196,8 @@ bool copy_database (boost::filesystem::path const & data_path, boost::program_op

auto node_flags = nano::inactive_node_flag_defaults ();
node_flags.read_only = !needs_to_write;
nano::inactive_node node (data_path, 24000, node_flags);
nano::update_flags (node_flags, vm);
nano::inactive_node node (data_path, node_flags);
if (!node.node->init_error ())
{
if (vm.count ("unchecked_clear"))
Expand Down Expand Up @@ -253,8 +257,8 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
{
password = vm["password"].as<std::string> ();
}
inactive_node node (data_path);
auto wallet (node.node->wallets.open (wallet_id));
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto wallet (inactive_node->node->wallets.open (wallet_id));
if (wallet != nullptr)
{
auto transaction (wallet->wallets.tx_begin_write ());
Expand Down Expand Up @@ -438,7 +442,8 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as<std::string> ()) : nano::working_path ();
auto node_flags = nano::inactive_node_flag_defaults ();
node_flags.read_only = false;
nano::inactive_node node (data_path, 24000, node_flags);
nano::update_flags (node_flags, vm);
nano::inactive_node node (data_path, node_flags);
if (!node.node->init_error ())
{
auto transaction (node.node->store.tx_begin_write ());
Expand All @@ -455,7 +460,8 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as<std::string> ()) : nano::working_path ();
auto node_flags = nano::inactive_node_flag_defaults ();
node_flags.read_only = false;
nano::inactive_node node (data_path, 24000, node_flags);
nano::update_flags (node_flags, vm);
nano::inactive_node node (data_path, node_flags);
if (!node.node->init_error ())
{
auto transaction (node.node->wallets.tx_begin_write ());
Expand All @@ -472,7 +478,8 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as<std::string> ()) : nano::working_path ();
auto node_flags = nano::inactive_node_flag_defaults ();
node_flags.read_only = false;
nano::inactive_node node (data_path, 24000, node_flags);
nano::update_flags (node_flags, vm);
nano::inactive_node node (data_path, node_flags);
if (!node.node->init_error ())
{
auto transaction (node.node->store.tx_begin_write ());
Expand All @@ -489,7 +496,8 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as<std::string> ()) : nano::working_path ();
auto node_flags = nano::inactive_node_flag_defaults ();
node_flags.read_only = false;
nano::inactive_node node (data_path, 24000, node_flags);
nano::update_flags (node_flags, vm);
nano::inactive_node node (data_path, node_flags);
if (!node.node->init_error ())
{
auto transaction (node.node->store.tx_begin_write ());
Expand All @@ -506,7 +514,8 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as<std::string> ()) : nano::working_path ();
auto node_flags = nano::inactive_node_flag_defaults ();
node_flags.read_only = false;
nano::inactive_node node (data_path, 24000, node_flags);
nano::update_flags (node_flags, vm);
nano::inactive_node node (data_path, node_flags);
if (!node.node->init_error ())
{
auto account_it = vm.find ("account");
Expand Down Expand Up @@ -600,7 +609,7 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
}
else if (vm.count ("diagnostics"))
{
inactive_node node (data_path);
auto inactive_node = nano::default_inactive_node (data_path, vm);
std::cout << "Testing hash function" << std::endl;
nano::raw_key key;
key.data.clear ();
Expand All @@ -619,7 +628,7 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
environment.dump (std::cout);
std::stringstream stream;
environment.dump (stream);
node.node->logger.always_log (stream.str ());
inactive_node->node->logger.always_log (stream.str ());
}
else
{
Expand Down Expand Up @@ -663,8 +672,8 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
{
password = vm["password"].as<std::string> ();
}
inactive_node node (data_path);
auto wallet (node.node->wallets.open (wallet_id));
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto wallet (inactive_node->node->wallets.open (wallet_id));
if (wallet != nullptr)
{
auto transaction (wallet->wallets.tx_begin_write ());
Expand Down Expand Up @@ -717,8 +726,8 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
{
password = vm["password"].as<std::string> ();
}
inactive_node node (data_path);
auto wallet (node.node->wallets.open (wallet_id));
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto wallet (inactive_node->node->wallets.open (wallet_id));
if (wallet != nullptr)
{
auto transaction (wallet->wallets.tx_begin_write ());
Expand Down Expand Up @@ -799,9 +808,9 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
}
if (!ec)
{
inactive_node node (data_path);
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto wallet_key = nano::random_wallet_id ();
auto wallet (node.node->wallets.create (wallet_key));
auto wallet (inactive_node->node->wallets.create (wallet_key));
if (wallet != nullptr)
{
if (vm.count ("password") > 0)
Expand Down Expand Up @@ -841,9 +850,10 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
nano::wallet_id wallet_id;
if (!wallet_id.decode_hex (vm["wallet"].as<std::string> ()))
{
inactive_node node (data_path);
auto existing (node.node->wallets.items.find (wallet_id));
if (existing != node.node->wallets.items.end ())
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto node = inactive_node->node;
auto existing (inactive_node->node->wallets.items.find (wallet_id));
if (existing != inactive_node->node->wallets.items.end ())
{
auto transaction (existing->second->wallets.tx_begin_write ());
if (!existing->second->enter_password (transaction, password))
Expand Down Expand Up @@ -896,10 +906,11 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
nano::wallet_id wallet_id;
if (!wallet_id.decode_hex (vm["wallet"].as<std::string> ()))
{
inactive_node node (data_path);
if (node.node->wallets.items.find (wallet_id) != node.node->wallets.items.end ())
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto node = inactive_node->node;
if (node->wallets.items.find (wallet_id) != node->wallets.items.end ())
{
node.node->wallets.destroy (wallet_id);
node->wallets.destroy (wallet_id);
}
else
{
Expand Down Expand Up @@ -945,13 +956,14 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
nano::wallet_id wallet_id;
if (!wallet_id.decode_hex (vm["wallet"].as<std::string> ()))
{
inactive_node node (data_path);
auto existing (node.node->wallets.items.find (wallet_id));
if (existing != node.node->wallets.items.end ())
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto node = inactive_node->node;
auto existing (node->wallets.items.find (wallet_id));
if (existing != node->wallets.items.end ())
{
bool valid (false);
{
auto transaction (node.node->wallets.tx_begin_write ());
auto transaction (node->wallets.tx_begin_write ());
valid = existing->second->store.valid_password (transaction);
if (!valid)
{
Expand Down Expand Up @@ -987,9 +999,9 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
{
bool error (true);
{
nano::lock_guard<std::mutex> lock (node.node->wallets.mutex);
auto transaction (node.node->wallets.tx_begin_write ());
nano::wallet wallet (error, transaction, node.node->wallets, wallet_id.to_string (), contents.str ());
nano::lock_guard<std::mutex> lock (node->wallets.mutex);
auto transaction (node->wallets.tx_begin_write ());
nano::wallet wallet (error, transaction, node->wallets, wallet_id.to_string (), contents.str ());
}
if (error)
{
Expand All @@ -998,9 +1010,9 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
}
else
{
node.node->wallets.reload ();
nano::lock_guard<std::mutex> lock (node.node->wallets.mutex);
release_assert (node.node->wallets.items.find (wallet_id) != node.node->wallets.items.end ());
node->wallets.reload ();
nano::lock_guard<std::mutex> lock (node->wallets.mutex);
release_assert (node->wallets.items.find (wallet_id) != node->wallets.items.end ());
std::cout << "Import completed\n";
}
}
Expand Down Expand Up @@ -1032,8 +1044,9 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
}
else if (vm.count ("wallet_list"))
{
inactive_node node (data_path);
for (auto i (node.node->wallets.items.begin ()), n (node.node->wallets.items.end ()); i != n; ++i)
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto node = inactive_node->node;
for (auto i (node->wallets.items.begin ()), n (node->wallets.items.end ()); i != n; ++i)
{
std::cout << boost::str (boost::format ("Wallet ID: %1%\n") % i->first.to_string ());
auto transaction (i->second->wallets.tx_begin_read ());
Expand All @@ -1047,12 +1060,13 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
{
if (vm.count ("wallet") == 1 && vm.count ("account") == 1)
{
inactive_node node (data_path);
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto node = inactive_node->node;
nano::wallet_id wallet_id;
if (!wallet_id.decode_hex (vm["wallet"].as<std::string> ()))
{
auto wallet (node.node->wallets.items.find (wallet_id));
if (wallet != node.node->wallets.items.end ())
auto wallet (node->wallets.items.find (wallet_id));
if (wallet != node->wallets.items.end ())
{
nano::account account_id;
if (!account_id.decode_account (vm["account"].as<std::string> ()))
Expand Down Expand Up @@ -1100,9 +1114,10 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
nano::wallet_id wallet_id;
if (!wallet_id.decode_hex (vm["wallet"].as<std::string> ()))
{
inactive_node node (data_path);
auto wallet (node.node->wallets.items.find (wallet_id));
if (wallet != node.node->wallets.items.end ())
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto node = inactive_node->node;
auto wallet (node->wallets.items.find (wallet_id));
if (wallet != node->wallets.items.end ())
{
auto transaction (wallet->second->wallets.tx_begin_read ());
auto representative (wallet->second->store.representative (transaction));
Expand Down Expand Up @@ -1138,9 +1153,10 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
nano::account account;
if (!account.decode_account (vm["account"].as<std::string> ()))
{
inactive_node node (data_path);
auto wallet (node.node->wallets.items.find (wallet_id));
if (wallet != node.node->wallets.items.end ())
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto node = inactive_node->node;
auto wallet (node->wallets.items.find (wallet_id));
if (wallet != node->wallets.items.end ())
{
auto transaction (wallet->second->wallets.tx_begin_write ());
wallet->second->store.representative_set (transaction, account);
Expand Down Expand Up @@ -1177,9 +1193,10 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
}
else if (vm.count ("vote_dump") == 1)
{
inactive_node node (data_path);
auto transaction (node.node->store.tx_begin_read ());
for (auto i (node.node->store.vote_begin (transaction)), n (node.node->store.vote_end ()); i != n; ++i)
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto node = inactive_node->node;
auto transaction (node->store.tx_begin_read ());
for (auto i (node->store.vote_begin (transaction)), n (node->store.vote_end ()); i != n; ++i)
{
auto const & vote (i->second);
std::cerr << boost::str (boost::format ("%1%\n") % vote->to_json ());
Expand All @@ -1193,6 +1210,13 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
return ec;
}

std::unique_ptr<nano::inactive_node> nano::default_inactive_node (boost::filesystem::path const & path_a, boost::program_options::variables_map const & vm_a)
{
auto node_flags = nano::inactive_node_flag_defaults ();
nano::update_flags (node_flags, vm_a);
return std::make_unique<nano::inactive_node> (path_a, node_flags);
}

namespace
{
void reset_confirmation_heights (nano::block_store & store)
Expand Down
Loading

0 comments on commit b23d7c3

Please sign in to comment.