Skip to content

Commit

Permalink
sentry: move init to node (#2355)
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr authored Sep 18, 2024
1 parent 6cdb957 commit 380b425
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 147 deletions.
10 changes: 1 addition & 9 deletions cmd/common/node_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@

#include "common.hpp"
#include "human_size_option.hpp"
#include "snapshot_options.hpp"

namespace silkworm::cmd::common {

void add_node_options(CLI::App& cli, node::Settings& settings) {
void add_node_options(CLI::App& cli, NodeSettings& settings) {
cli.add_flag("--chaindata.exclusive", settings.chaindata_env_config.exclusive,
"Chaindata database opened in exclusive mode");
cli.add_flag("--chaindata.readahead", settings.chaindata_env_config.read_ahead,
Expand Down Expand Up @@ -69,17 +68,10 @@ void add_node_options(CLI::App& cli, node::Settings& settings) {

cli.add_flag("--fakepow", settings.fake_pow, "Disables proof-of-work verification");

add_option_private_api_address(cli, settings.server_settings.address_uri);
add_option_remote_sentry_addresses(cli, settings.remote_sentry_addresses, /*is_required=*/false);

// Chain options
add_option_chain(cli, settings.network_id);

// RPC server options
add_context_pool_options(cli, settings.server_settings.context_pool_settings);

// Snapshot&Bittorrent options
add_snapshot_options(cli, settings.snapshot_settings);
}

} // namespace silkworm::cmd::common
4 changes: 2 additions & 2 deletions cmd/common/node_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

#include <CLI/CLI.hpp>

#include <silkworm/node/settings.hpp>
#include <silkworm/node/common/node_settings.hpp>

namespace silkworm::cmd::common {

void add_node_options(CLI::App& cli, node::Settings& settings);
void add_node_options(CLI::App& cli, NodeSettings& settings);

} // namespace silkworm::cmd::common
35 changes: 0 additions & 35 deletions cmd/common/settings.hpp

This file was deleted.

16 changes: 8 additions & 8 deletions cmd/dev/backend_kv_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
#include <silkworm/infra/grpc/client/client_context_pool.hpp>
#include <silkworm/node/backend/ethereum_backend.hpp>
#include <silkworm/node/backend_kv_server.hpp>
#include <silkworm/node/settings.hpp>
#include <silkworm/sentry/eth/status_data_provider.hpp>
#include <silkworm/sentry/grpc/client/sentry_client.hpp>
#include <silkworm/sentry/multi_sentry_client.hpp>
#include <silkworm/sentry/session_sentry_client.hpp>

#include "../common/common.hpp"
#include "../common/db_max_readers_option.hpp"
#include "../common/settings.hpp"
#include "../common/shutdown_signal.hpp"

using namespace silkworm;
Expand All @@ -63,15 +63,15 @@ std::string get_library_versions() {
}

//! Standalone BackEndKV server settings
struct StandaloneBackEndKVSettings : public SilkwormSettings {
struct StandaloneBackEndKVSettings : public node::Settings {
bool simulate_state_changes{false};
};

//! Parse the command-line arguments into the BackEnd and KV server settings
void parse_command_line(int argc, char* argv[], CLI::App& app, StandaloneBackEndKVSettings& settings) {
auto& log_settings = settings.log_settings;
auto& node_settings = settings.node_settings;
auto& server_settings = settings.node_settings.server_settings;
auto& server_settings = settings.server_settings;

// Node options
std::filesystem::path data_dir;
Expand All @@ -84,7 +84,7 @@ void parse_command_line(int argc, char* argv[], CLI::App& app, StandaloneBackEnd
add_option_db_max_readers(app, max_readers);

// RPC Server options
add_option_private_api_address(app, node_settings.server_settings.address_uri);
add_option_private_api_address(app, server_settings.address_uri);
add_option_remote_sentry_addresses(app, node_settings.remote_sentry_addresses, /* is_required = */ true);
add_context_pool_options(app, server_settings.context_pool_settings);

Expand Down Expand Up @@ -129,7 +129,7 @@ std::shared_ptr<silkworm::sentry::api::SentryClient> make_sentry_client(
// wrap remote client in a session client
sentry_client = std::make_shared<silkworm::sentry::SessionSentryClient>(
remote_sentry_client,
eth_status_data_provider.to_factory_function());
silkworm::sentry::eth::StatusDataProvider::to_factory_function(std::move(eth_status_data_provider)));
} else {
std::vector<std::shared_ptr<silkworm::sentry::api::SentryClient>> clients;

Expand All @@ -141,7 +141,7 @@ std::shared_ptr<silkworm::sentry::api::SentryClient> make_sentry_client(
// wrap remote client in a session client
auto session_sentry_client = std::make_shared<silkworm::sentry::SessionSentryClient>(
remote_sentry_client,
eth_status_data_provider.to_factory_function());
silkworm::sentry::eth::StatusDataProvider::to_factory_function(std::move(eth_status_data_provider)));
clients.push_back(session_sentry_client);
}

Expand All @@ -165,7 +165,7 @@ int main(int argc, char* argv[]) {

auto& log_settings = settings.log_settings;
auto& node_settings = settings.node_settings;
auto& server_settings = settings.node_settings.server_settings;
auto& server_settings = settings.server_settings;

// Initialize logging with custom settings
log::init(log_settings);
Expand All @@ -175,7 +175,7 @@ int main(int argc, char* argv[]) {
SILK_LOG << "BackEndKvServer build info: " << node_name;
SILK_LOG << "BackEndKvServer library info: " << get_library_versions();
SILK_LOG << "BackEndKvServer launched with chaindata: " << node_settings.chaindata_env_config.path
<< " address: " << node_settings.server_settings.address_uri
<< " address: " << server_settings.address_uri
<< " contexts: " << server_settings.context_pool_settings.num_contexts;

auto database_env = db::open_env(node_settings.chaindata_env_config);
Expand Down
94 changes: 36 additions & 58 deletions cmd/silkworm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,20 @@
#include <boost/asio/use_future.hpp>

#include <silkworm/buildinfo.h>
#include <silkworm/db/chain_head.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/infra/concurrency/awaitable_wait_for_all.hpp>
#include <silkworm/infra/concurrency/awaitable_wait_for_one.hpp>
#include <silkworm/infra/grpc/client/client_context_pool.hpp>
#include <silkworm/node/node.hpp>
#include <silkworm/sentry/eth/status_data_provider.hpp>
#include <silkworm/sentry/sentry_client_factory.hpp>
#include <silkworm/sentry/sentry.hpp>

#include "common/common.hpp"
#include "common/db_checklist.hpp"
#include "common/node_options.hpp"
#include "common/rpcdaemon_options.hpp"
#include "common/sentry_options.hpp"
#include "common/settings.hpp"
#include "common/shutdown_signal.hpp"
#include "common/snapshot_options.hpp"

namespace sw_db = silkworm::db;
namespace sw_log = silkworm::log;
Expand All @@ -54,13 +52,7 @@ using namespace silkworm;
using silkworm::BlockNum;
using silkworm::DataDirectory;
using silkworm::human_size;
using silkworm::cmd::common::add_logging_options;
using silkworm::cmd::common::add_node_options;
using silkworm::cmd::common::add_option_data_dir;
using silkworm::cmd::common::add_rpcdaemon_options;
using silkworm::cmd::common::add_sentry_options;
using silkworm::cmd::common::ShutdownSignal;
using silkworm::cmd::common::SilkwormSettings;

const char* current_exception_name() {
#ifdef WIN32
Expand All @@ -82,20 +74,30 @@ struct PruneModeValidator : public CLI::Validator {
}
};

void parse_silkworm_command_line(CLI::App& cli, int argc, char* argv[], SilkwormSettings& settings) {
using namespace silkworm::cmd;
void add_rpc_server_settings(CLI::App& cli, rpc::ServerSettings& server_settings) {
using namespace silkworm::cmd::common;
add_option_private_api_address(cli, server_settings.address_uri);
add_context_pool_options(cli, server_settings.context_pool_settings);
}

auto& node_settings = settings.node_settings;
void parse_silkworm_command_line(CLI::App& cli, int argc, char* argv[], node::Settings& settings) {
using namespace silkworm::cmd;
using namespace silkworm::cmd::common;

std::filesystem::path data_dir_path;
add_option_data_dir(cli, data_dir_path);

// Node settings
add_node_options(cli, node_settings);
add_node_options(cli, settings.node_settings);

// Sentry settings
add_sentry_options(cli, settings.sentry_settings);

add_rpc_server_settings(cli, settings.server_settings);

// Snapshot&Bittorrent options
add_snapshot_options(cli, settings.snapshot_settings);

// Prune options
std::string prune_mode;
auto& prune_opts = *cli.add_option_group("Prune", "Prune options to delete ancient data from DB");
Expand Down Expand Up @@ -144,8 +146,11 @@ void parse_silkworm_command_line(CLI::App& cli, int argc, char* argv[], Silkworm

// Validate and assign settings

node_settings.log_settings = settings.log_settings;
node_settings.rpcdaemon_settings = settings.rpcdaemon_settings;
// node::NodeSettings
auto& node_settings = settings.node_settings;

const auto build_info = silkworm_get_buildinfo();
node_settings.build_info = make_application_info(build_info);

const size_t chaindata_page_size = node_settings.chaindata_env_config.page_size;
if ((chaindata_page_size & (chaindata_page_size - 1)) != 0) {
Expand Down Expand Up @@ -184,9 +189,15 @@ void parse_silkworm_command_line(CLI::App& cli, int argc, char* argv[], Silkworm
olderHistory, olderReceipts, olderSenders, olderTxIndex, olderCallTraces, beforeHistory,
beforeReceipts, beforeSenders, beforeTxIndex, beforeCallTraces);

auto& snapshot_settings = node_settings.snapshot_settings;
// snapshots::SnapshotSettings
auto& snapshot_settings = settings.snapshot_settings;
snapshot_settings.repository_dir = node_settings.data_directory->snapshots().path();
snapshot_settings.bittorrent_settings.repository_path = snapshot_settings.repository_dir;

// sentry::Settings
settings.sentry_settings.client_id = sentry::Sentry::make_client_id(*build_info);
settings.sentry_settings.data_dir_path = node_settings.data_directory->path();
settings.sentry_settings.network_id = node_settings.network_id;
}

// main
Expand All @@ -213,20 +224,14 @@ int main(int argc, char* argv[]) {
cli.get_formatter()->column_width(50);

try {
SilkwormSettings settings;
node::Settings settings;
parse_silkworm_command_line(cli, argc, argv, settings);

auto& node_settings = settings.node_settings;

// Initialize logging with cli settings
sw_log::init(settings.log_settings);
sw_log::set_thread_name("main-thread");

// Output BuildInfo
const auto build_info{silkworm_get_buildinfo()};
node_settings.build_info = make_application_info(build_info);

sw_log::Info("Silkworm", build_info_as_log_args(build_info));
sw_log::Info("Silkworm", build_info_as_log_args(silkworm_get_buildinfo()));

// Output mdbx build info
auto mdbx_ver{mdbx::get_version()};
Expand All @@ -235,54 +240,27 @@ int main(int argc, char* argv[]) {
{"version", mdbx_ver.git.describe, "build", mdbx_bld.target, "compiler", mdbx_bld.compiler});

// Prepare database for takeoff
cmd::common::run_db_checklist(node_settings);
cmd::common::run_db_checklist(settings.node_settings);

mdbx::env_managed chaindata_env = db::open_env(node_settings.chaindata_env_config);
mdbx::env_managed chaindata_env = db::open_env(settings.node_settings.chaindata_env_config);

silkworm::rpc::ClientContextPool context_pool{
settings.node_settings.server_settings.context_pool_settings,
};

// Sentry: the peer-2-peer proxy server
settings.sentry_settings.client_id = sentry::Sentry::make_client_id(*build_info);
settings.sentry_settings.data_dir_path = node_settings.data_directory->path();
settings.sentry_settings.network_id = node_settings.network_id;

auto chain_head_provider = [db_access = db::ROAccess{chaindata_env}] {
return db::read_chain_head(db_access);
};
sentry::eth::StatusDataProvider eth_status_data_provider{std::move(chain_head_provider), node_settings.chain_config.value()};

auto [sentry_client, sentry_server] = sentry::SentryClientFactory::make_sentry(
std::move(settings.sentry_settings),
settings.node_settings.remote_sentry_addresses,
context_pool.as_executor_pool(),
context_pool,
eth_status_data_provider.to_factory_function());
auto embedded_sentry_run_if_needed = [](auto server) -> Task<void> {
if (server) {
co_await server->run();
}
settings.server_settings.context_pool_settings,
};

silkworm::node::Node execution_node{
context_pool.any_executor(),
settings.node_settings,
sentry_client,
context_pool,
settings,
chaindata_env, // NOLINT(cppcoreguidelines-slicing)
};

auto tasks =
execution_node.run() &&
embedded_sentry_run_if_needed(sentry_server);

// Trap OS signals
ShutdownSignal shutdown_signal{context_pool.any_executor()};

// Go!
auto run_future = boost::asio::co_spawn(
context_pool.any_executor(),
std::move(tasks) || shutdown_signal.wait(),
execution_node.run() || shutdown_signal.wait(),
boost::asio::use_future);
context_pool.start();
sw_log::Info() << "Silkworm is now running";
Expand Down
Loading

0 comments on commit 380b425

Please sign in to comment.