Skip to content

Commit

Permalink
Merge branch 'feature/grid_view' into feature/sessions_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
iceseer committed Apr 16, 2024
2 parents b4ef5be + 32c4420 commit 91ccf96
Show file tree
Hide file tree
Showing 19 changed files with 247 additions and 122 deletions.
8 changes: 2 additions & 6 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ CheckOptions:
value: '::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile'
- key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader
value: ''
- key: cppcoreguidelines-pro-bounds-constant-array-index.IncludeStyle
value: '0'
- key: cppcoreguidelines-pro-type-member-init.IgnoreArrays
value: '0'
- key: cppcoreguidelines-special-member-functions.AllowMissingMoveFunctions
Expand Down Expand Up @@ -164,16 +162,12 @@ CheckOptions:
value: CamelCase
- key: modernize-make-shared.IgnoreMacros
value: '1'
- key: modernize-make-shared.IncludeStyle
value: '0'
- key: modernize-make-shared.MakeSmartPtrFunction
value: 'std::make_shared'
- key: modernize-make-shared.MakeSmartPtrFunctionHeader
value: memory
- key: modernize-make-unique.IgnoreMacros
value: '1'
- key: modernize-make-unique.IncludeStyle
value: '0'
- key: modernize-make-unique.MakeSmartPtrFunction
value: 'std::make_unique'
- key: modernize-make-unique.MakeSmartPtrFunctionHeader
Expand Down Expand Up @@ -232,6 +226,8 @@ CheckOptions:
value: google
- key: performance-unnecessary-value-param.IncludeStyle
value: google
- key: performance-unnecessary-value-param.AllowedTypes
value: std::shared_ptr
- key: readability-braces-around-statements.ShortStatementLines
value: '0'
- key: readability-function-size.BranchThreshold
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ if (CLANG_FORMAT)
include(cmake/clang-format.cmake)
endif ()
if (EMBEDDINGS)
add_compile_definitions(PRIVATE USE_KAGOME_EMBEDDINGS)
add_compile_definitions(USE_KAGOME_EMBEDDINGS)
endif ()
if (NOT DEFINED PROFILING)
if (${CMAKE_BUILD_TYPE} EQUALS "Debug")
set(PROFILING ON)
endif ()
endif ()
if (PROFILING)
add_compile_definitions(PRIVATE KAGOME_PROFILING)
add_compile_definitions(KAGOME_PROFILING)
endif ()

include(GNUInstallDirs)
Expand Down
42 changes: 13 additions & 29 deletions core/consensus/beefy/impl/beefy_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "runtime/runtime_api/beefy.hpp"
#include "storage/spaced_storage.hpp"
#include "utils/block_number_key.hpp"
#include "utils/pool_handler_ready_make.hpp"

// TODO(turuslan): #1651, report equivocation
// TODO(turuslan): #1651, fetch justifications
Expand All @@ -42,7 +43,7 @@ namespace kagome::network {
};

BeefyImpl::BeefyImpl(
application::AppStateManager &app_state_manager,
std::shared_ptr<application::AppStateManager> app_state_manager,
const application::ChainSpec &chain_spec,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::BeefyApi> beefy_api,
Expand All @@ -55,28 +56,27 @@ namespace kagome::network {
std::shared_ptr<crypto::SessionKeys> session_keys,
LazySPtr<BeefyProtocol> beefy_protocol,
primitives::events::ChainSubscriptionEnginePtr chain_sub_engine)
: block_tree_{std::move(block_tree)},
: log_{log::createLogger("Beefy")},
block_tree_{std::move(block_tree)},
beefy_api_{std::move(beefy_api)},
ecdsa_{std::move(ecdsa)},
db_{db->getSpace(storage::Space::kBeefyJustification)},
main_pool_handler_{main_thread_pool.handler(app_state_manager)},
beefy_pool_handler_{beefy_thread_pool.handler(app_state_manager)},
main_pool_handler_{main_thread_pool.handler(*app_state_manager)},
beefy_pool_handler_{poolHandlerReadyMake(
this, app_state_manager, beefy_thread_pool, log_)},
scheduler_{std::move(scheduler)},
timeline_{std::move(timeline)},
session_keys_{std::move(session_keys)},
beefy_protocol_{std::move(beefy_protocol)},
min_delta_{chain_spec.beefyMinDelta()},
chain_sub_{std::move(chain_sub_engine)},
log_{log::createLogger("Beefy")} {
chain_sub_{std::move(chain_sub_engine)} {
BOOST_ASSERT(block_tree_ != nullptr);
BOOST_ASSERT(beefy_api_ != nullptr);
BOOST_ASSERT(ecdsa_ != nullptr);
BOOST_ASSERT(db_ != nullptr);
BOOST_ASSERT(main_pool_handler_ != nullptr);
BOOST_ASSERT(scheduler_ != nullptr);
BOOST_ASSERT(session_keys_ != nullptr);

app_state_manager.takeControl(*this);
}

primitives::BlockNumber BeefyImpl::finalized() const {
Expand All @@ -95,13 +95,8 @@ namespace kagome::network {

void BeefyImpl::onJustification(const primitives::BlockHash &block_hash,
primitives::Justification raw) {
beefy_pool_handler_->execute(
[weak{weak_from_this()}, block_hash, raw = std::move(raw)] {
if (auto self = weak.lock()) {
std::ignore =
self->onJustificationOutcome(block_hash, std::move(raw));
}
});
REINVOKE(*beefy_pool_handler_, onJustification, block_hash, std::move(raw));
std::ignore = onJustificationOutcome(block_hash, std::move(raw));
}

outcome::result<void> BeefyImpl::onJustificationOutcome(
Expand All @@ -124,16 +119,7 @@ namespace kagome::network {
}

void BeefyImpl::onMessage(consensus::beefy::BeefyGossipMessage message) {
beefy_pool_handler_->execute(
[weak{weak_from_this()}, message = std::move(message)] {
if (auto self = weak.lock()) {
self->onMessageStrand(std::move(message));
}
});
}

void BeefyImpl::onMessageStrand(
consensus::beefy::BeefyGossipMessage message) {
REINVOKE(*beefy_pool_handler_, onMessage, std::move(message));
if (not beefy_genesis_) {
return;
}
Expand Down Expand Up @@ -226,7 +212,7 @@ namespace kagome::network {
}
}

void BeefyImpl::prepare() {
bool BeefyImpl::tryStart() {
auto cursor = db_->cursor();
std::ignore = cursor->seekLast();
if (cursor->isValid()) {
Expand All @@ -243,15 +229,13 @@ namespace kagome::network {
});
}
});
}

void BeefyImpl::start() {
beefy_pool_handler_->execute([weak{weak_from_this()}] {
if (auto self = weak.lock()) {
std::ignore = self->update();
}
});
setTimer();
return true;
}

bool BeefyImpl::hasJustification(primitives::BlockNumber block) const {
Expand Down
14 changes: 6 additions & 8 deletions core/consensus/beefy/impl/beefy_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

namespace kagome {
class PoolHandler;
}
class PoolHandlerReady;
} // namespace kagome

namespace kagome::application {
class AppStateManager;
Expand Down Expand Up @@ -59,7 +60,7 @@ namespace kagome::network {
class BeefyImpl : public Beefy,
public std::enable_shared_from_this<BeefyImpl> {
public:
BeefyImpl(application::AppStateManager &app_state_manager,
BeefyImpl(std::shared_ptr<application::AppStateManager> app_state_manager,
const application::ChainSpec &chain_spec,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::BeefyApi> beefy_api,
Expand All @@ -73,8 +74,7 @@ namespace kagome::network {
LazySPtr<BeefyProtocol> beefy_protocol,
primitives::events::ChainSubscriptionEnginePtr chain_sub_engine);

void prepare();
void start();
bool tryStart();

primitives::BlockNumber finalized() const override;

Expand Down Expand Up @@ -103,7 +103,6 @@ namespace kagome::network {
const primitives::BlockHash &block_hash, primitives::Justification raw);
outcome::result<void> onJustification(
consensus::beefy::SignedCommitment justification);
void onMessageStrand(consensus::beefy::BeefyGossipMessage message);
void onVote(consensus::beefy::VoteMessage vote, bool broadcast);
outcome::result<void> apply(
consensus::beefy::SignedCommitment justification, bool broadcast);
Expand All @@ -116,12 +115,13 @@ namespace kagome::network {
void broadcast(consensus::beefy::BeefyGossipMessage message);
void setTimer();

log::Logger log_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<runtime::BeefyApi> beefy_api_;
std::shared_ptr<crypto::EcdsaProvider> ecdsa_;
std::shared_ptr<storage::BufferStorage> db_;
std::shared_ptr<PoolHandler> main_pool_handler_;
std::shared_ptr<PoolHandler> beefy_pool_handler_;
std::shared_ptr<PoolHandlerReady> beefy_pool_handler_;
std::shared_ptr<libp2p::basic::Scheduler> scheduler_;
LazySPtr<consensus::Timeline> timeline_;
std::shared_ptr<crypto::SessionKeys> session_keys_;
Expand All @@ -138,7 +138,5 @@ namespace kagome::network {
std::map<primitives::BlockNumber, consensus::beefy::SignedCommitment>
pending_justifications_;
libp2p::basic::Scheduler::Handle timer_;

log::Logger log_;
};
} // namespace kagome::network
26 changes: 10 additions & 16 deletions core/consensus/grandpa/impl/grandpa_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "network/synchronizer.hpp"
#include "storage/predefined_keys.hpp"
#include "utils/pool_handler.hpp"
#include "utils/pool_handler_ready_make.hpp"
#include "utils/retain_if.hpp"

namespace {
Expand Down Expand Up @@ -66,7 +67,7 @@ namespace kagome::consensus::grandpa {
constexpr std::chrono::milliseconds kGossipDuration{1000};

GrandpaImpl::GrandpaImpl(
application::AppStateManager &app_state_manager,
std::shared_ptr<application::AppStateManager> app_state_manager,
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<Environment> environment,
std::shared_ptr<crypto::Ed25519Provider> crypto_provider,
Expand Down Expand Up @@ -94,8 +95,9 @@ namespace kagome::consensus::grandpa {
timeline_{std::move(timeline)},
chain_sub_{chain_sub_engine},
db_{db.getSpace(storage::Space::kDefault)},
main_pool_handler_{main_thread_pool.handler(app_state_manager)},
grandpa_pool_handler_{grandpa_thread_pool.handler(app_state_manager)},
main_pool_handler_{main_thread_pool.handler(*app_state_manager)},
grandpa_pool_handler_{poolHandlerReadyMake(
this, app_state_manager, grandpa_thread_pool, logger_)},
scheduler_{std::make_shared<libp2p::basic::SchedulerImpl>(
std::make_shared<libp2p::basic::AsioSchedulerBackend>(
grandpa_thread_pool.io_context()),
Expand All @@ -119,19 +121,10 @@ namespace kagome::consensus::grandpa {

// allow app state manager to prepare, start and stop grandpa consensus
// pipeline
app_state_manager.takeControl(*this);
app_state_manager->takeControl(*this);
}

bool GrandpaImpl::start() {
if (!grandpa_pool_handler_->isInCurrentThread()) {
grandpa_pool_handler_->execute([wptr{weak_from_this()}] {
if (auto self = wptr.lock()) {
self->start();
}
});
return true;
}

bool GrandpaImpl::tryStart() {
if (auto r = db_->get(storage::kGrandpaVotesKey)) {
if (auto r2 = scale::decode<CachedVotes>(r.value())) {
cached_votes_ = std::move(r2.value());
Expand Down Expand Up @@ -1366,8 +1359,9 @@ namespace kagome::consensus::grandpa {
}

void GrandpaImpl::reload() {
if (not start()) {
SL_ERROR(logger_, "reload: start failed");
REINVOKE(*grandpa_pool_handler_, reload);
if (not tryStart()) {
SL_CRITICAL(logger_, "reload failed");
}
}

Expand Down
14 changes: 7 additions & 7 deletions core/consensus/grandpa/impl/grandpa_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

namespace kagome {
class PoolHandler;
}
class PoolHandlerReady;
} // namespace kagome

namespace kagome::application {
class AppStateManager;
Expand Down Expand Up @@ -103,7 +104,7 @@ namespace kagome::consensus::grandpa {
~GrandpaImpl() override = default;

GrandpaImpl(
application::AppStateManager &app_state_manager,
std::shared_ptr<application::AppStateManager> app_state_manager,
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<Environment> environment,
std::shared_ptr<crypto::Ed25519Provider> crypto_provider,
Expand All @@ -125,9 +126,8 @@ namespace kagome::consensus::grandpa {
* - Obtains authority set corresponding to the latest completed round
* - Uses obtained data to create and execute initial round
* @return true if grandpa was executed
* @see kagome::application::AppStateManager::takeControl()
*/
bool start();
bool tryStart();

/**
* Does nothing. Needed only for AppStateManager
Expand Down Expand Up @@ -322,6 +322,8 @@ namespace kagome::consensus::grandpa {
void saveCachedVotes();
void applyCachedVotes(VotingRound &round);

log::Logger logger_ = log::createLogger("Grandpa", "grandpa");

const size_t kVotesCacheSize = 5;

const Clock::Duration round_time_factor_;
Expand All @@ -341,7 +343,7 @@ namespace kagome::consensus::grandpa {
std::shared_ptr<storage::BufferStorage> db_;

std::shared_ptr<PoolHandler> main_pool_handler_;
std::shared_ptr<PoolHandler> grandpa_pool_handler_;
std::shared_ptr<PoolHandlerReady> grandpa_pool_handler_;
std::shared_ptr<libp2p::basic::Scheduler> scheduler_;

std::shared_ptr<VotingRound> current_round_;
Expand All @@ -365,8 +367,6 @@ namespace kagome::consensus::grandpa {
// Metrics
metrics::RegistryPtr metrics_registry_ = metrics::createRegistry();
metrics::Gauge *metric_highest_round_;

log::Logger logger_ = log::createLogger("Grandpa", "grandpa");
};

} // namespace kagome::consensus::grandpa
12 changes: 6 additions & 6 deletions core/dispute_coordinator/impl/dispute_coordinator_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "parachain/approval/approval_distribution.hpp"
#include "runtime/runtime_api/core.hpp"
#include "runtime/runtime_api/parachain_host.hpp"
#include "utils/pool_handler_ready_make.hpp"
#include "utils/tuple_hash.hpp"

namespace kagome::dispute {
Expand Down Expand Up @@ -108,7 +109,7 @@ namespace kagome::dispute {

DisputeCoordinatorImpl::DisputeCoordinatorImpl(
std::shared_ptr<application::ChainSpec> chain_spec,
application::AppStateManager &app_state_manager,
std::shared_ptr<application::AppStateManager> app_state_manager,
clock::SystemClock &system_clock,
clock::SteadyClock &steady_clock,
std::shared_ptr<crypto::SessionKeys> session_keys,
Expand Down Expand Up @@ -148,8 +149,9 @@ namespace kagome::dispute {
peer_view_(std::move(peer_view)),
chain_sub_{std::move(chain_sub_engine)},
timeline_(std::move(timeline)),
main_pool_handler_{main_thread_pool.handler(app_state_manager)},
dispute_thread_handler_{dispute_thread_pool.handler(app_state_manager)},
main_pool_handler_{main_thread_pool.handler(*app_state_manager)},
dispute_thread_handler_{poolHandlerReadyMake(
this, app_state_manager, dispute_thread_pool, log_)},
scheduler_{std::make_shared<libp2p::basic::SchedulerImpl>(
std::make_shared<libp2p::basic::AsioSchedulerBackend>(
dispute_thread_pool.io_context()),
Expand Down Expand Up @@ -206,11 +208,9 @@ namespace kagome::dispute {
metric_concluded_invalid_ = metrics_registry_->registerCounterMetric(
disputeConcludedMetricName,
{{"validity", "invalid"}, {"chain", chain_spec->chainType()}});

app_state_manager.takeControl(*this);
}

bool DisputeCoordinatorImpl::prepare() {
bool DisputeCoordinatorImpl::tryStart() {
auto leaves = block_tree_->getLeaves();
active_heads_.insert(leaves.begin(), leaves.end());

Expand Down
Loading

0 comments on commit 91ccf96

Please sign in to comment.