Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/app/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
#include <boost/asio/ip/tcp.hpp>
#include <libp2p/crypto/key.hpp>
#include <libp2p/multi/multiaddress.hpp>
#include <utils/ctor_limiters.hpp>

#include "app/validator_keys_manifest.hpp"
#include "crypto/xmss/xmss_provider.hpp"

namespace lean::app {
class Configuration : Singleton<Configuration> {
class Configuration {
public:
using Endpoint = boost::asio::ip::tcp::endpoint;

Expand Down
13 changes: 9 additions & 4 deletions src/app/configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ namespace lean::app {
config_->database_.directory = "db";
config_->database_.cache_size = 512 << 20; // 512MiB

config_->metrics_.endpoint = {boost::asio::ip::address_v4::any(), 9615};
config_->metrics_.enabled = std::nullopt;

namespace po = boost::program_options;
Expand Down Expand Up @@ -196,6 +195,8 @@ namespace lean::app {
("version,v", "show version")
("config,c", po::value<std::string>(), "config-file path")
("log,l", po::value<std::vector<std::string>>(), "Sets a custom logging filter")
("api-host", po::value<std::string>(), "Set address for OpenMetrics over HTTP.")
("api-port", po::value<uint16_t>(), "Set port for OpenMetrics over HTTP.")
;

// clang-format on
Expand Down Expand Up @@ -250,6 +251,9 @@ namespace lean::app {
logger_cli_args_ = vm["log"].as<std::vector<std::string>>();
}

BOOST_OUTCOME_TRY(
parseEndpoint(config_->api_endpoint_, vm, "api-host", "api-port"));

return false;
}

Expand Down Expand Up @@ -294,6 +298,10 @@ namespace lean::app {
return load_default();
}

const boost::asio::ip::tcp::endpoint &Configurator::apiEndpoint() const {
return config_->apiEndpoint();
}

outcome::result<std::shared_ptr<Configuration>> Configurator::calculateConfig(
qtils::SharedRef<soralog::Logger> logger) {
logger_ = std::move(logger);
Expand Down Expand Up @@ -779,9 +787,6 @@ namespace lean::app {
config_->metrics_.enabled = false;
}

BOOST_OUTCOME_TRY(parseEndpoint(
config_->api_endpoint_, cli_values_map_, "api-host", "api-port"));

if (not config_->node_key_.has_value()) {
config_->node_key_ = randomKeyPair();
SL_INFO(logger_, "Generating random node key");
Expand Down
2 changes: 2 additions & 0 deletions src/app/configurator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#pragma once

#include <boost/asio/ip/tcp.hpp>
#include <boost/program_options.hpp>
#include <log/logger.hpp>
#include <qtils/enum_error_code.hpp>
Expand Down Expand Up @@ -53,6 +54,7 @@ namespace lean::app {
std::vector<std::string> getLoggingCliArgs() {
return logger_cli_args_;
}
const boost::asio::ip::tcp::endpoint &apiEndpoint() const;

outcome::result<std::shared_ptr<Configuration>> calculateConfig(
qtils::SharedRef<soralog::Logger> logger);
Expand Down
63 changes: 35 additions & 28 deletions src/app/impl/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "app/chain_spec.hpp"
#include "app/configuration.hpp"
#include "app/state_manager.hpp"
#include "blockchain/block_tree.hpp"
#include "blockchain/fork_choice_mutex.hpp"
#include "metrics/handler.hpp"
#include "serde/json.hpp"
Expand All @@ -23,8 +24,6 @@
#include "utils/http.hpp"

namespace lean::app {
constexpr auto *kContentTypeJson = "application/json; charset=utf-8";

struct Enabled {
bool enabled;

Expand All @@ -37,11 +36,13 @@ namespace lean::app {
qtils::SharedRef<Configuration> app_config,
qtils::SharedRef<metrics::Handler> metrics_handler,
qtils::SharedRef<app::ChainSpec> chain_spec,
qtils::SharedRef<blockchain::BlockTree> block_tree,
qtils::SharedRef<ForkChoiceStoreMutex> fork_choice_store)
: log_{logsys->getLogger("HttpServer", "http")},
app_config_{std::move(app_config)},
metrics_handler_{std::move(metrics_handler)},
chain_spec_{std::move(chain_spec)},
block_tree_{std::move(block_tree)},
fork_choice_store_{std::move(fork_choice_store)} {
state_manager->takeControl(*this);
}
Expand Down Expand Up @@ -93,11 +94,8 @@ namespace lean::app {
std::string_view{request.method_string()},
url);
if (url == "/lean/v0/health") {
response.set(boost::beast::http::field::content_type,
kContentTypeJson);
response.body() =
R"({"status":"healthy","service":"lean-rpc-api"})";
return response;
return http::respondJson(
R"({"status":"healthy","service":"lean-rpc-api"})");
}
if (url == "/lean/v0/states/finalized") {
auto finalized = self->fork_choice_store_->getLatestFinalized();
Expand All @@ -115,34 +113,27 @@ namespace lean::app {
}
if (url == "/lean/v0/checkpoints/justified") {
auto justified = self->fork_choice_store_->getLatestJustified();
response.set(boost::beast::http::field::content_type,
kContentTypeJson);
response.body() = std::format(R"({{"root":"0x{}","slot":{}}})",
justified.root.toHex(),
justified.slot);
return response;
return http::respondJson(
std::format(R"({{"root":"0x{}","slot":{}}})",
justified.root.toHex(),
justified.slot));
}
if (url == "/lean/v0/fork_choice") {
if (auto result_res =
self->fork_choice_store_->apiForkChoice()) {
auto &result = result_res.value();
response.set(boost::beast::http::field::content_type,
kContentTypeJson);
response.body() = json::encode(json::NameCase::SNAKE, result);
} else {
response.result(
boost::beast::http::status::internal_server_error);
return http::respondJson(
json::encode(json::NameCase::SNAKE, result));
}
response.result(
boost::beast::http::status::internal_server_error);
return response;
}
if (url == "/lean/v0/admin/aggregator") {
if (request.method() == boost::beast::http::verb::get) {
auto is_aggregator = self->chain_spec_->isAggregator();
response.set(boost::beast::http::field::content_type,
kContentTypeJson);
response.body() =
std::format(R"({{"is_aggregator":{}}})", is_aggregator);
return response;
return http::respondJson(
std::format(R"({{"is_aggregator":{}}})", is_aggregator));
}
if (request.method() == boost::beast::http::verb::post) {
Enabled body;
Expand All @@ -155,14 +146,30 @@ namespace lean::app {
}
auto enabled = body.enabled;
auto previous = self->chain_spec_->setIsAggregator(enabled);
response.set(boost::beast::http::field::content_type,
kContentTypeJson);
response.body() =
return http::respondJson(
std::format(R"({{"is_aggregator":{},"previous":{}}})",
enabled,
previous);
previous));
}
}
if (url == "/lean/v0/blocks/finalized") {
auto finalized = self->fork_choice_store_->getLatestFinalized();
auto block_result =
self->block_tree_->tryGetSignedBlock(finalized.root);
if (not block_result.has_value()) {
response.result(
boost::beast::http::status::internal_server_error);
return response;
}
auto &block = block_result.value();
if (not block.has_value()) {
response.result(boost::beast::http::status::not_found);
return response;
}
response.set(boost::beast::http::field::content_type,
"application/octet-stream");
response.body() = qtils::byte2str(encode(*block).value());
return response;
}
response.result(boost::beast::http::status::not_found);
return response;
Expand Down
6 changes: 6 additions & 0 deletions src/app/impl/http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ namespace lean::app {
class ChainSpec;
} // namespace lean::app

namespace lean::blockchain {
class BlockTree;
} // namespace lean::blockchain

namespace lean::metrics {
class Handler;
} // namespace lean::metrics
Expand All @@ -39,6 +43,7 @@ namespace lean::app {
qtils::SharedRef<Configuration> app_config,
qtils::SharedRef<metrics::Handler> metrics_handler,
qtils::SharedRef<app::ChainSpec> chain_spec,
qtils::SharedRef<blockchain::BlockTree> block_tree,
qtils::SharedRef<ForkChoiceStoreMutex> fork_choice_store);
~HttpServer();

Expand All @@ -50,6 +55,7 @@ namespace lean::app {
qtils::SharedRef<Configuration> app_config_;
qtils::SharedRef<metrics::Handler> metrics_handler_;
qtils::SharedRef<app::ChainSpec> chain_spec_;
qtils::SharedRef<blockchain::BlockTree> block_tree_;
qtils::SharedRef<ForkChoiceStoreMutex> fork_choice_store_;
std::shared_ptr<boost::asio::io_context> io_context_;
std::optional<std::thread> io_thread_;
Expand Down
26 changes: 26 additions & 0 deletions src/blockchain/block_production_metrics.def
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,29 @@ METRIC_COUNTER(lean_block_building_success_total,
METRIC_COUNTER(lean_block_building_failures_total,
"lean_block_building_failures_total",
"Failed block builds (exception in build_block)");

// https://github.com/leanEthereum/leanSpec/pull/753
// phase = "select_payloads" | "compact" | "stf_simulate"
METRIC_HISTOGRAM_LABELS(
lean_block_proposal_attestation_build_phase_seconds,
"lean_block_proposal_attestation_build_phase_seconds",
"Phase-level time in block-proposal attestation selection: select_payloads (greedy child-payload pick), compact (recursive merge of proofs per AttestationData), stf_simulate (candidate block STF).",
(0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2, 4, 8),
({"phase"}));
METRIC_COUNTER(
lean_block_proposal_attestation_builds_total,
"lean_block_proposal_attestation_builds_total",
"Completed block-proposal attestation selection runs (one per proposal attempt).");
METRIC_COUNTER(
lean_block_proposal_child_payloads_consumed_total,
"lean_block_proposal_child_payloads_consumed_total",
"Child aggregated payloads selected during greedy proof picking (before recursive compaction).");
METRIC_HISTOGRAM(lean_block_proposal_attestation_data_selected,
"lean_block_proposal_attestation_data_selected",
"Distinct AttestationData entries in the proposal block body.",
(0, 1, 2, 4, 8, 16, 32));
METRIC_HISTOGRAM(
lean_block_proposal_aggregates_selected,
"lean_block_proposal_aggregates_selected",
"Aggregated signature proofs in the proposal result after compaction.",
(0, 1, 2, 4, 8, 16, 32, 64, 128));
2 changes: 1 addition & 1 deletion src/blockchain/block_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ namespace lean::blockchain {
* "/leanconsensus/req/blocks_by_root/1/ssz_snappy" protocol.
*/
virtual outcome::result<std::optional<SignedBlock>> tryGetSignedBlock(
const BlockHash block_hash) const = 0;
const BlockHash &block_hash) const = 0;
};

} // namespace lean::blockchain
Loading
Loading