Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/app/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ namespace lean::app {
return node_key_.value();
}

const std::optional<size_t> &Configuration::maxBootnodes() const {
return max_bootnodes_;
}

const Configuration::DatabaseConfig &Configuration::database() const {
return database_;
}
Expand Down
2 changes: 2 additions & 0 deletions src/app/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace lean::app {
[[nodiscard]] virtual const std::optional<libp2p::Multiaddress> &
listenMultiaddr() const;
[[nodiscard]] virtual const libp2p::crypto::KeyPair &nodeKey() const;
[[nodiscard]] virtual const std::optional<size_t> &maxBootnodes() const;

[[nodiscard]] virtual const DatabaseConfig &database() const;

Expand All @@ -66,6 +67,7 @@ namespace lean::app {
std::filesystem::path genesis_config_path_;
std::optional<libp2p::Multiaddress> listen_multiaddr_;
std::optional<libp2p::crypto::KeyPair> node_key_;
std::optional<size_t> max_bootnodes_;

DatabaseConfig database_;
MetricsConfig metrics_;
Expand Down
5 changes: 5 additions & 0 deletions src/app/configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ namespace lean::app {
("name,n", po::value<std::string>(), "Set name of node.")
("node-id", po::value<std::string>(), "Node id from validator registry (genesis/validators.yaml).")
("node-key", po::value<std::string>(), "Set secp256k1 node key as hex string (with or without 0x prefix).")
("max-bootnodes", po::value<std::string>(), "Max bootnodes count to connect to.")
("log,l", po::value<std::vector<std::string>>(),
"Sets a custom logging filter.\n"
"Syntax: <target>=<level>, e.g., -llibp2p=off.\n"
Expand Down Expand Up @@ -414,6 +415,10 @@ namespace lean::app {
fail = true;
}
});
if (auto max_bootnodes =
find_argument<size_t>(cli_values_map_, "max-bootnodes")) {
config_->max_bootnodes_ = *max_bootnodes;
}
find_argument<std::string>(
cli_values_map_, "base-path", [&](const std::string &value) {
config_->base_path_ = value;
Expand Down
21 changes: 9 additions & 12 deletions src/modules/networking/networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ namespace lean::modules {
candidates.push_back(b);
}

// If more than 20 candidates, shuffle and pick first 20
size_t max_take = 20;
if (candidates.size() > max_take) {
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(candidates.begin(), candidates.end(), gen);
candidates.erase(candidates.begin() + max_take, candidates.end());
// Randomly choose no more than `maxBootnodes` bootnodes to connect to.
if (config_->maxBootnodes().has_value()
and candidates.size() > *config_->maxBootnodes()) {
std::ranges::shuffle(candidates, std::default_random_engine{});
// `resize` doesn't work without default constructor
candidates.erase(candidates.begin() + *config_->maxBootnodes(),
candidates.end());
}

SL_INFO(logger_,
Expand Down Expand Up @@ -409,11 +409,8 @@ namespace lean::modules {
[this, type, topic, f{std::move(f)}]() -> libp2p::Coro<void> {
while (auto raw_result = co_await topic->receiveMessage()) {
auto &raw = raw_result.value();
if (auto uncompressed_res = snappyUncompress(raw.data)) {
auto &uncompressed = uncompressed_res.value();
if (auto r = decode<T>(uncompressed)) {
f(std::move(r.value()), raw.received_from);
}
if (auto r = decodeSszSnappy<T>(raw.data)) {
f(std::move(r.value()), raw.received_from);
}
}
});
Expand Down
Loading