Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix roles getters #2223

Merged
merged 2 commits into from
Sep 27, 2024
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
6 changes: 2 additions & 4 deletions core/application/impl/kagome_application_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ namespace kagome::application {
}

#ifdef __linux__
if (!app_config_->disableSecureMode()
&& app_config_->usePvfSubprocess()
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
&& app_config_->roles().flags.authority) {
if (not app_config_->disableSecureMode() and app_config_->usePvfSubprocess()
and app_config_->roles().isAuthority()) {
auto res = parachain::runSecureModeCheckProcess(
*injector_.injectIoContext(), app_config_->runtimeCacheDirPath());
if (!res) {
Expand Down
3 changes: 1 addition & 2 deletions core/authority_discovery/publisher/address_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ namespace kagome::authority_discovery {
if (not libp2p_key_) {
return true;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
if (not roles_.flags.authority) {
if (not roles_.isAuthority()) {
return true;
}
interval_.start([weak_self{weak_from_this()}] {
Expand Down
7 changes: 3 additions & 4 deletions core/blockchain/impl/block_tree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,16 @@ namespace kagome::blockchain {

} block_tree_data_;

primitives::events::ExtrinsicSubscriptionEnginePtr
extrinsic_events_engine_ = {};
primitives::events::ChainSubscriptionEnginePtr chain_events_engine_ = {};
primitives::events::ChainSubscriptionEnginePtr chain_events_engine_;
std::shared_ptr<PoolHandler> main_pool_handler_;
turuslan marked this conversation as resolved.
Show resolved Hide resolved
primitives::events::ExtrinsicSubscriptionEnginePtr extrinsic_events_engine_;
log::Logger log_ = log::createLogger("BlockTree", "block_tree");

// Metrics
metrics::RegistryPtr metrics_registry_ = metrics::createRegistry();
metrics::Gauge *metric_best_block_height_;
metrics::Gauge *metric_finalized_block_height_;
metrics::Gauge *metric_known_chain_leaves_;
std::shared_ptr<PoolHandler> main_pool_handler_;
telemetry::Telemetry telemetry_ = telemetry::createTelemetryService();
};
} // namespace kagome::blockchain
3 changes: 1 addition & 2 deletions core/consensus/babe/impl/babe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ namespace kagome::consensus::babe {
offchain_worker_pool_(std::move(offchain_worker_pool)),
main_pool_handler_{main_thread_pool.handler(app_state_manager)},
worker_pool_handler_{worker_thread_pool.handler(app_state_manager)},
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
is_validator_by_config_(app_config.roles().flags.authority != 0),
is_validator_by_config_(app_config.roles().isAuthority()),
telemetry_{telemetry::createTelemetryService()} {
BOOST_ASSERT(block_tree_);
BOOST_ASSERT(config_repo_);
Expand Down
3 changes: 1 addition & 2 deletions core/crypto/key_store/session_keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ namespace kagome::crypto {
const KeySuiteStore<T> &store,
const std::vector<A> &authorities,
const Eq &eq) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
if (not roles_.flags.authority) {
if (not roles_.isAuthority()) {
return std::nullopt;
}
if (cache) {
Expand Down
13 changes: 4 additions & 9 deletions core/network/impl/peer_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,7 @@ namespace kagome::network {
network::CollationVersion
proto_version) { // network::CollationVersion::VStaging
/// If validator start validation protocol
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
if (peer_state.roles.flags.authority) {
if (peer_state.roles.isAuthority()) {
auto validation_protocol = [&]() -> std::shared_ptr<ProtocolBase> {
return router_->getValidationProtocolVStaging();
}();
Expand Down Expand Up @@ -842,16 +841,14 @@ namespace kagome::network {
if (peer_state.has_value()) {
auto &state = peer_state->get();
if (not out) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
if (state.roles.flags.full == 1) {
if (state.roles.isFull()) {
if (self->countPeers(PeerType::PEER_TYPE_IN)
>= self->app_config_.inPeers()) {
self->connecting_peers_.erase(peer_info.id);
self->disconnectFromPeer(peer_info.id);
return;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
} else if (state.roles.flags.light == 1) {
} else {
if (self->countPeers(PeerType::PEER_TYPE_IN, IsLight(true))
>= self->app_config_.inPeersLight()) {
self->connecting_peers_.erase(peer_info.id);
Expand Down Expand Up @@ -887,9 +884,7 @@ namespace kagome::network {
}

void PeerManagerImpl::reserveStatusStreams(const PeerId &peer_id) const {
if (auto ps = getPeerState(peer_id);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
ps && ps->get().roles.flags.authority) {
if (auto ps = getPeerState(peer_id); ps && ps->get().roles.isAuthority()) {
auto proto_val_vstaging = router_->getValidationProtocolVStaging();
BOOST_ASSERT_MSG(proto_val_vstaging,
"Router did not provide validation protocol vstaging");
Expand Down
10 changes: 3 additions & 7 deletions core/network/impl/protocols/grandpa_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ namespace kagome::network {

auto filter = [&, &msg = vote_message](const PeerId &peer_id,
const PeerState &info) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
if (info.roles.flags.light != 0) {
if (info.roles.isLight()) {
return false;
}

Expand Down Expand Up @@ -263,8 +262,7 @@ namespace kagome::network {
}
const auto &info = info_opt.value().get();

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
if (not set_changed and info.roles.flags.light) {
if (not set_changed and info.roles.isLight()) {
return false;
}

Expand Down Expand Up @@ -538,9 +536,7 @@ namespace kagome::network {
if (not predicate(peer, info)) {
return;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
(info.roles.flags.authority != 0 ? authorities : any)
.emplace_back(peer);
(info.roles.isAuthority() ? authorities : any).emplace_back(peer);
}
});
auto hash = getHash(*message);
Expand Down
55 changes: 36 additions & 19 deletions core/network/types/roles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,53 @@

#include "scale/tie.hpp"

// NOLINTBEGIN(cppcoreguidelines-pro-type-union-access)
namespace kagome::network {

union Roles {
struct Roles {
SCALE_TIE_ONLY(value);

struct {
/**
* Full node, does not participate in consensus.
*/
uint8_t full : 1;
union {
struct {
/**
* Full node, does not participate in consensus.
*/
uint8_t full : 1;

/**
* Light client node.
*/
uint8_t light : 1;
/**
* Light client node.
*/
uint8_t light : 1;

/**
* Act as an authority
*/
uint8_t authority : 1;
/**
* Act as an authority
*/
uint8_t authority : 1;

} flags;
uint8_t value;
} flags;
uint8_t value;
};

Roles() : value(0) {}
Roles(uint8_t v) : value(v) {}

// https://github.com/paritytech/polkadot-sdk/blob/6c3219ebe9231a0305f53c7b33cb558d46058062/substrate/client/network/common/src/role.rs#L101
bool isFull() const {
return flags.full != 0 or isAuthority();
}

bool isAuthority() const {
return flags.authority != 0;
}

// https://github.com/paritytech/polkadot-sdk/blob/6c3219ebe9231a0305f53c7b33cb558d46058062/substrate/client/network/common/src/role.rs#L111
bool isLight() const {
return not isFull();
}
};

inline std::string to_string(Roles r) {
switch (r.value) { // NOLINT(cppcoreguidelines-pro-type-union-access)
switch (r.value) {
case 0:
return "none";
case 1:
Expand All @@ -47,7 +64,7 @@ namespace kagome::network {
case 4:
return "authority";
}
return to_string(
r.value); // NOLINT(cppcoreguidelines-pro-type-union-access)
return to_string(r.value);
}
} // namespace kagome::network
// NOLINTEND(cppcoreguidelines-pro-type-union-access)
4 changes: 1 addition & 3 deletions core/offchain/impl/offchain_worker_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ namespace kagome::offchain {
}

bool OffchainWorkerImpl::isValidator() const {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
bool isValidator = app_config_.roles().flags.authority == 1;
return isValidator;
return app_config_.roles().isAuthority();
}

Result<Success, Failure> OffchainWorkerImpl::submitTransaction(
Expand Down
3 changes: 1 addition & 2 deletions core/parachain/validator/impl/parachain_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4677,8 +4677,7 @@ namespace kagome::parachain {
}

bool ParachainProcessorImpl::isValidatingNode() const {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
return (app_config_.roles().flags.authority == 1);
return app_config_.roles().isAuthority();
}

void ParachainProcessorImpl::onValidationComplete(
Expand Down
3 changes: 1 addition & 2 deletions core/runtime/runtime_api/impl/offchain_worker_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ namespace kagome::runtime {
// but offchain workers runs when node is validating
if (app_config_.offchainWorkerMode()
== application::AppConfiguration::OffchainWorkerMode::WhenValidating) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
if (app_config_.roles().flags.authority != 1) {
if (not app_config_.roles().isAuthority()) {
return outcome::success();
}
}
Expand Down
5 changes: 1 addition & 4 deletions core/telemetry/impl/service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,7 @@ namespace kagome::telemetry {
rapidjson::Value payload(rapidjson::kObjectType);
payload
.AddMember(
"authority",
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
static_cast<bool>(app_configuration_.roles().flags.authority),
allocator)
"authority", app_configuration_.roles().isAuthority(), allocator)
.AddMember("chain", str_val(chain_spec_.name()), allocator)
.AddMember("config", str_val(""), allocator)
.AddMember("genesis_hash", str_val(genesis_hash_), allocator)
Expand Down