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

feature/backing tests #2300

Closed
wants to merge 27 commits into from
Closed
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
19 changes: 17 additions & 2 deletions core/injector/application_injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
#include "metrics/impl/metrics_watcher.hpp"
#include "metrics/impl/prometheus/handler_impl.hpp"
#include "metrics/metrics.hpp"
#include "network/i_peer_view.hpp"
#include "network/impl/block_announce_transmitter_impl.hpp"
#include "network/impl/extrinsic_observer_impl.hpp"
#include "network/impl/grandpa_transmitter_impl.hpp"
Expand Down Expand Up @@ -151,6 +152,7 @@
#include "outcome/outcome.hpp"
#include "parachain/approval/approval_distribution.hpp"
#include "parachain/approval/approval_thread_pool.hpp"
#include "parachain/availability/bitfield/signer.hpp"
#include "parachain/availability/bitfield/store_impl.hpp"
#include "parachain/availability/fetch/fetch_impl.hpp"
#include "parachain/availability/recovery/recovery_impl.hpp"
Expand All @@ -163,6 +165,7 @@
#include "parachain/pvf/workers.hpp"
#include "parachain/validator/impl/parachain_observer_impl.hpp"
#include "parachain/validator/parachain_processor.hpp"
#include "parachain/validator/statement_distribution/i_statement_distribution.hpp"
#include "parachain/validator/statement_distribution/statement_distribution.hpp"
#include "runtime/binaryen/binaryen_memory_provider.hpp"
#include "runtime/binaryen/instance_environment_factory.hpp"
Expand Down Expand Up @@ -788,12 +791,24 @@ namespace {
di::bind<network::SyncProtocolObserver>.template to<network::SyncProtocolObserverImpl>(),
di::bind<network::DisputeRequestObserver>.template to<dispute::DisputeCoordinatorImpl>(),
di::bind<parachain::AvailabilityStore>.template to<parachain::AvailabilityStoreImpl>(),
di::bind<network::IPeerView>.template to<network::PeerView>(),
di::bind<parachain::IBitfieldSigner>.template to<parachain::BitfieldSigner>(),
di::bind<parachain::Fetch>.template to<parachain::FetchImpl>(),
di::bind<parachain::Recovery>.template to<parachain::RecoveryImpl>(),
di::bind<parachain::IValidatorSignerFactory>.template to<parachain::ValidatorSignerFactory>(),
di::bind<parachain::BitfieldStore>.template to<parachain::BitfieldStoreImpl>(),
di::bind<parachain::BackingStore>.template to<parachain::BackingStoreImpl>(),
di::bind<parachain::IProspectiveParachains>.template to<parachain::ProspectiveParachains>(),
di::bind<parachain::BackedCandidatesSource>.template to<parachain::ParachainProcessorImpl>(),
di::bind<network::CanDisconnect>.template to<parachain::statement_distribution::StatementDistribution>(),
di::bind<parachain::IPvfPrecheck>.template to<parachain::PvfPrecheck>(),
bind_by_lambda<network::CanDisconnect>(
[](const auto &injector) {
return injector.template create<sptr<parachain::statement_distribution::StatementDistribution>>();
}),
bind_by_lambda<parachain::statement_distribution::IStatementDistribution>(
[](const auto &injector) {
return injector.template create<sptr<parachain::statement_distribution::StatementDistribution>>();
}),
di::bind<parachain::Pvf>.template to<parachain::PvfImpl>(),
di::bind<network::CollationObserver>.template to<parachain::ParachainObserverImpl>(),
di::bind<network::ValidationObserver>.template to<parachain::ParachainObserverImpl>(),
Expand Down Expand Up @@ -939,7 +954,7 @@ namespace kagome::injector {
KagomeNodeInjector::KagomeNodeInjector(
sptr<application::AppConfiguration> app_config)
: pimpl_{std::make_unique<KagomeNodeInjectorImpl>(
makeKagomeNodeInjector(std::move(app_config)))} {}
makeKagomeNodeInjector(std::move(app_config)))} {}

sptr<application::AppConfiguration> KagomeNodeInjector::injectAppConfig() {
return pimpl_->injector_
Expand Down
3 changes: 2 additions & 1 deletion core/injector/application_injector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ namespace kagome {

namespace statement_distribution {
class StatementDistribution;
}
class IStatementDistribution;
} // namespace statement_distribution
} // namespace parachain

namespace runtime {
Expand Down
69 changes: 69 additions & 0 deletions core/network/i_peer_view.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once

#include <memory>
#include <unordered_map>

#include <libp2p/peer/peer_id.hpp>

#include "crypto/type_hasher.hpp"
#include "network/types/collator_messages.hpp"
#include "outcome/outcome.hpp"
#include "primitives/event_types.hpp"
#include "subscription/subscriber.hpp"
#include "subscription/subscription_engine.hpp"

namespace kagome::network {

using HashedBlockHeader = primitives::BlockHeader;
struct ExView {
View view;
HashedBlockHeader new_head;
std::vector<primitives::BlockHash> lost;
};

struct ExViewRef {
std::optional<std::reference_wrapper<const HashedBlockHeader>> new_head;
const std::vector<primitives::BlockHash> &lost;
};

/**
* Observable class for current heads and finalized block number tracking.
*/
class IPeerView {
public:
enum struct EventType : uint32_t { kViewUpdated, kPeerRemoved };

using PeerId = libp2p::peer::PeerId;

using MyViewSubscriptionEngine = subscription::
SubscriptionEngine<EventType, std::monostate, network::ExView>;
using MyViewSubscriptionEnginePtr =
std::shared_ptr<MyViewSubscriptionEngine>;
using MyViewSubscriber = MyViewSubscriptionEngine::SubscriberType;
using MyViewSubscriberPtr = std::shared_ptr<MyViewSubscriber>;

using PeerViewSubscriptionEngine = subscription::
SubscriptionEngine<EventType, std::monostate, PeerId, network::View>;
using PeerViewSubscriptionEnginePtr =
std::shared_ptr<PeerViewSubscriptionEngine>;
using PeerViewSubscriber = PeerViewSubscriptionEngine::SubscriberType;
using PeerViewSubscriberPtr = std::shared_ptr<PeerViewSubscriber>;

virtual ~IPeerView() = default;

virtual size_t peersCount() const = 0;
virtual MyViewSubscriptionEnginePtr getMyViewObservable() = 0;
virtual PeerViewSubscriptionEnginePtr getRemoteViewObservable() = 0;

virtual void removePeer(const PeerId &peer_id) = 0;
virtual void updateRemoteView(const PeerId &peer_id,
network::View &&view) = 0;
virtual const View &getMyView() const = 0;
};

} // namespace kagome::network
56 changes: 8 additions & 48 deletions core/network/peer_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@
*/
#pragma once

#include <memory>
#include <unordered_map>

#include <libp2p/peer/peer_id.hpp>

#include "application/app_state_manager.hpp"
#include "blockchain/block_tree.hpp"
#include "crypto/type_hasher.hpp"
#include "injector/lazy.hpp"
#include "network/types/collator_messages.hpp"
#include "outcome/outcome.hpp"
#include "primitives/event_types.hpp"
#include "subscription/subscriber.hpp"
#include "subscription/subscription_engine.hpp"
#include "network/i_peer_view.hpp"
#include "utils/non_copyable.hpp"
#include "utils/safe_object.hpp"

Expand All @@ -28,43 +18,14 @@ namespace kagome::blockchain {

namespace kagome::network {

using HashedBlockHeader = primitives::BlockHeader;
struct ExView {
View view;
HashedBlockHeader new_head;
std::vector<primitives::BlockHash> lost;
};

struct ExViewRef {
std::optional<std::reference_wrapper<const HashedBlockHeader>> new_head;
const std::vector<primitives::BlockHash> &lost;
};

/**
* Observable class for current heads and finalized block number tracking.
*/
class PeerView final : public NonCopyable,
public NonMovable,
public IPeerView,
public std::enable_shared_from_this<PeerView> {
public:
enum struct EventType : uint32_t { kViewUpdated, kPeerRemoved };

using PeerId = libp2p::peer::PeerId;

using MyViewSubscriptionEngine = subscription::
SubscriptionEngine<EventType, std::monostate, network::ExView>;
using MyViewSubscriptionEnginePtr =
std::shared_ptr<MyViewSubscriptionEngine>;
using MyViewSubscriber = MyViewSubscriptionEngine::SubscriberType;
using MyViewSubscriberPtr = std::shared_ptr<MyViewSubscriber>;

using PeerViewSubscriptionEngine = subscription::
SubscriptionEngine<EventType, std::monostate, PeerId, network::View>;
using PeerViewSubscriptionEnginePtr =
std::shared_ptr<PeerViewSubscriptionEngine>;
using PeerViewSubscriber = PeerViewSubscriptionEngine::SubscriberType;
using PeerViewSubscriberPtr = std::shared_ptr<PeerViewSubscriber>;

PeerView(primitives::events::ChainSubscriptionEnginePtr chain_events_engine,
std::shared_ptr<application::AppStateManager> app_state_manager,
LazySPtr<blockchain::BlockTree> block_tree);
Expand All @@ -76,14 +37,13 @@ namespace kagome::network {
bool prepare();
void stop();

size_t peersCount() const;

MyViewSubscriptionEnginePtr getMyViewObservable();
PeerViewSubscriptionEnginePtr getRemoteViewObservable();
size_t peersCount() const override;
MyViewSubscriptionEnginePtr getMyViewObservable() override;
PeerViewSubscriptionEnginePtr getRemoteViewObservable() override;

void removePeer(const PeerId &peer_id);
void updateRemoteView(const PeerId &peer_id, network::View &&view);
auto &getMyView() const {
void removePeer(const PeerId &peer_id) override;
void updateRemoteView(const PeerId &peer_id, network::View &&view) override;
const View &getMyView() const override {
return my_view_;
}

Expand Down
12 changes: 7 additions & 5 deletions core/parachain/availability/bitfield/signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace kagome::parachain {
broadcast_ = std::move(callback);
}

outcome::result<void> BitfieldSigner::sign(const ValidatorSigner &signer,
outcome::result<void> BitfieldSigner::sign(const IValidatorSigner &signer,
const Candidates &candidates) {
const BlockHash &relay_parent = signer.relayParent();
scale::BitVec bitfield;
Expand All @@ -70,10 +70,12 @@ namespace kagome::parachain {
}

outcome::result<void> BitfieldSigner::onBlock(const BlockHash &relay_parent) {
OUTCOME_TRY(signer, signer_factory_->at(relay_parent));
if (not signer.has_value()) {
OUTCOME_TRY(opt_signer, signer_factory_->at(relay_parent));
if (!opt_signer) {
return outcome::success();
}
auto &signer = *opt_signer;

Candidates candidates;
OUTCOME_TRY(cores, parachain_api_->availability_cores(relay_parent));
OUTCOME_TRY(
Expand Down Expand Up @@ -110,10 +112,10 @@ namespace kagome::parachain {

scheduler_->schedule(
[weak{weak_from_this()},
signer{std::move(*signer)},
signer{std::move(signer)},
candidates{std::move(candidates)}]() mutable {
if (auto self = weak.lock()) {
auto r = self->sign(signer, candidates);
auto r = self->sign(*signer, candidates);
if (r.has_error()) {
SL_WARN(self->logger_, "sign error {}", r.error());
}
Expand Down
26 changes: 19 additions & 7 deletions core/parachain/availability/bitfield/signer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@
#include "runtime/runtime_api/parachain_host.hpp"

namespace kagome::parachain {
/// Signs, stores and broadcasts bitfield for every new head.
class BitfieldSigner : public std::enable_shared_from_this<BitfieldSigner> {
class IBitfieldSigner {
public:
using BroadcastCallback = std::function<void(
const primitives::BlockHash &, const network::SignedBitfield &)>;
using Candidates = std::vector<std::optional<network::CandidateHash>>;

virtual ~IBitfieldSigner() = default;
virtual void start() = 0;

/// Sign bitfield for given block.
virtual outcome::result<void> sign(const IValidatorSigner &signer,
const Candidates &candidates) = 0;

virtual void setBroadcastCallback(BroadcastCallback &&callback) = 0;
};

/// Signs, stores and broadcasts bitfield for every new head.
class BitfieldSigner : public IBitfieldSigner,
public std::enable_shared_from_this<BitfieldSigner> {
public:
BitfieldSigner(
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<ValidatorSignerFactory> signer_factory,
Expand All @@ -36,13 +49,12 @@ namespace kagome::parachain {
primitives::events::ChainSubscriptionEnginePtr chain_sub_engine);

/// Subscribes to new heads.
void start();
void start() override;

/// Sign bitfield for given block.
outcome::result<void> sign(const ValidatorSigner &signer,
const Candidates &candidates);
outcome::result<void> sign(const IValidatorSigner &signer,
const Candidates &candidates) override;

void setBroadcastCallback(BroadcastCallback &&callback);
void setBroadcastCallback(BroadcastCallback &&callback) override;

private:
using BlockHash = primitives::BlockHash;
Expand Down
9 changes: 6 additions & 3 deletions core/parachain/pvf/precheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ namespace kagome::parachain {

outcome::result<void> PvfPrecheck::onBlock() {
auto block = block_tree_->bestBlock();
OUTCOME_TRY(signer, signer_factory_->at(block.hash));
if (not signer.has_value()) {
OUTCOME_TRY(opt_signer, signer_factory_->at(block.hash));
if (!opt_signer) {
return outcome::success();
}
auto &signer = *opt_signer;

if (not session_code_accept_.empty()
and signer->getSessionIndex() < session_code_accept_.begin()->first) {
SL_WARN(logger_, "past session");
Expand Down Expand Up @@ -100,7 +102,8 @@ namespace kagome::parachain {
auto &code_zstd = *code_zstd_res.value();
auto res = [&]() -> outcome::result<void> {
OUTCOME_TRY(config, sessionParams(*parachain_api_, block.hash));
OUTCOME_TRY(pvf_pool_->precompile(code_hash, code_zstd, config.context_params));
OUTCOME_TRY(pvf_pool_->precompile(
code_hash, code_zstd, config.context_params));
return outcome::success();
}();
if (res) {
Expand Down
15 changes: 12 additions & 3 deletions core/parachain/pvf/precheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,22 @@ namespace kagome::runtime {
namespace kagome::parachain {
class PvfPool;

/// Signs pvf check statement for every new head.
class PvfPrecheck : public std::enable_shared_from_this<PvfPrecheck> {
class IPvfPrecheck {
public:
using BroadcastCallback = std::function<void(
const primitives::BlockHash &, const network::SignedBitfield &)>;
using Candidates = std::vector<std::optional<network::CandidateHash>>;

virtual ~IPvfPrecheck() = default;

/// Subscribes to new heads.
virtual void start() = 0;
};

/// Signs pvf check statement for every new head.
class PvfPrecheck : public IPvfPrecheck,
public std::enable_shared_from_this<PvfPrecheck> {
public:
PvfPrecheck(
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<blockchain::BlockTree> block_tree,
Expand All @@ -60,7 +69,7 @@ namespace kagome::parachain {
primitives::events::ChainSubscriptionEnginePtr chain_sub_engine);

/// Subscribes to new heads.
void start();
void start() override;

private:
using BlockHash = primitives::BlockHash;
Expand Down
2 changes: 1 addition & 1 deletion core/parachain/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ namespace kagome::network {
} // namespace kagome::network

namespace kagome::parachain::fragment {
enum UpgradeRestriction {
enum UpgradeRestriction : uint8_t {
/// There is an upgrade restriction and there are no details about its
/// specifics nor how long
/// it could last.
Expand Down
Loading