From 5125a475c053473861befdcda4f711fa771511e3 Mon Sep 17 00:00:00 2001 From: iceseer Date: Sun, 1 Dec 2024 18:47:17 +0300 Subject: [PATCH 01/27] [feature] interface for StatementDistribution Signed-off-by: iceseer --- core/injector/application_injector.cpp | 10 +- core/injector/application_injector.hpp | 1 + .../validator/impl/parachain_processor.cpp | 2 +- .../validator/parachain_processor.hpp | 6 +- .../i_statement_distribution.hpp | 64 +++++++ .../statement_distribution.hpp | 16 +- test/core/parachain/CMakeLists.txt | 12 ++ test/core/parachain/backing.cpp | 162 ++++++++++++++++++ .../core/parachain/parachain_test_harness.hpp | 6 + .../core/parachain/prospective_parachains.cpp | 5 - 10 files changed, 267 insertions(+), 17 deletions(-) create mode 100644 core/parachain/validator/statement_distribution/i_statement_distribution.hpp create mode 100644 test/core/parachain/backing.cpp diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index 1ea840fb79..75f09c5bf5 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -155,6 +155,7 @@ #include "parachain/availability/fetch/fetch_impl.hpp" #include "parachain/availability/recovery/recovery_impl.hpp" #include "parachain/availability/store/store_impl.hpp" +#include "parachain/validator/statement_distribution/i_statement_distribution.hpp" #include "parachain/backing/store_impl.hpp" #include "parachain/pvf/module_precompiler.hpp" #include "parachain/pvf/pool.hpp" @@ -793,7 +794,14 @@ namespace { di::bind.template to(), di::bind.template to(), di::bind.template to(), - di::bind.template to(), + bind_by_lambda( + [](const auto &injector) { + return injector.template create>(); + }), + bind_by_lambda( + [](const auto &injector) { + return injector.template create>(); + }), di::bind.template to(), di::bind.template to(), di::bind.template to(), diff --git a/core/injector/application_injector.hpp b/core/injector/application_injector.hpp index e891b8a64c..305953dc65 100644 --- a/core/injector/application_injector.hpp +++ b/core/injector/application_injector.hpp @@ -63,6 +63,7 @@ namespace kagome { namespace statement_distribution { class StatementDistribution; + class IStatementDistribution; } } // namespace parachain diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index f272db5419..e94efcaa37 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -156,7 +156,7 @@ namespace kagome::parachain { std::shared_ptr block_tree, LazySPtr slots_util, std::shared_ptr babe_config_repo, - std::shared_ptr sd) + std::shared_ptr sd) : pm_(std::move(pm)), runtime_info_(std::move(runtime_info)), crypto_provider_(std::move(crypto_provider)), diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index 29a50db570..1e42e177f9 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -39,7 +39,7 @@ #include "parachain/validator/collations.hpp" #include "parachain/validator/prospective_parachains/prospective_parachains.hpp" #include "parachain/validator/signer.hpp" -#include "parachain/validator/statement_distribution/statement_distribution.hpp" +#include "parachain/validator/statement_distribution/i_statement_distribution.hpp" #include "parachain/validator/statement_distribution/types.hpp" #include "primitives/common.hpp" #include "primitives/event_types.hpp" @@ -191,7 +191,7 @@ namespace kagome::parachain { std::shared_ptr block_tree, LazySPtr slots_util, std::shared_ptr babe_config_repo, - std::shared_ptr + std::shared_ptr statement_distribution); ~ParachainProcessorImpl() = default; @@ -817,7 +817,7 @@ namespace kagome::parachain { std::default_random_engine random_; std::shared_ptr prospective_parachains_; std::shared_ptr block_tree_; - std::shared_ptr + std::shared_ptr statement_distribution; std::shared_ptr> per_session; diff --git a/core/parachain/validator/statement_distribution/i_statement_distribution.hpp b/core/parachain/validator/statement_distribution/i_statement_distribution.hpp new file mode 100644 index 0000000000..36afa75512 --- /dev/null +++ b/core/parachain/validator/statement_distribution/i_statement_distribution.hpp @@ -0,0 +1,64 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include "authority_discovery/query/query.hpp" +#include "common/ref_cache.hpp" +#include "consensus/babe/babe_config_repository.hpp" +#include "consensus/babe/impl/babe_digests_util.hpp" +#include "consensus/timeline/slots_util.hpp" +#include "network/can_disconnect.hpp" +#include "network/peer_manager.hpp" +#include "network/peer_view.hpp" +#include "network/router.hpp" +#include "parachain/approval/approval_thread_pool.hpp" +#include "parachain/validator/impl/candidates.hpp" +#include "parachain/validator/network_bridge.hpp" +#include "parachain/validator/signer.hpp" +#include "parachain/validator/statement_distribution/peer_state.hpp" +#include "parachain/validator/statement_distribution/per_session_state.hpp" +#include "parachain/validator/statement_distribution/types.hpp" +#include "utils/pool_handler_ready_make.hpp" + +namespace kagome::parachain { + class ParachainProcessorImpl; +} + +namespace kagome::parachain::statement_distribution { + + class IStatementDistribution { + public: + virtual ~IStatementDistribution() = default; + + virtual void OnFetchAttestedCandidateRequest( + const network::vstaging::AttestedCandidateRequest &request, + std::shared_ptr stream) = 0; + + virtual void store_parachain_processor(std::weak_ptr pp) = 0; + + virtual void handle_incoming_manifest( + const libp2p::peer::PeerId &peer_id, + const network::vstaging::BackedCandidateManifest &msg) = 0; + + virtual void handle_incoming_acknowledgement( + const libp2p::peer::PeerId &peer_id, + const network::vstaging::BackedCandidateAcknowledgement + &acknowledgement) = 0; + + virtual void handle_incoming_statement( + const libp2p::peer::PeerId &peer_id, + const network::vstaging::StatementDistributionMessageStatement &stm) = 0; + + virtual void handle_backed_candidate_message(const CandidateHash &candidate_hash) = 0; + + virtual void share_local_statement(const primitives::BlockHash &relay_parent, + const SignedFullStatementWithPVD &statement) = 0; + }; + +} // namespace kagome::parachain::statement_distribution diff --git a/core/parachain/validator/statement_distribution/statement_distribution.hpp b/core/parachain/validator/statement_distribution/statement_distribution.hpp index cad21d2bec..43eff621e8 100644 --- a/core/parachain/validator/statement_distribution/statement_distribution.hpp +++ b/core/parachain/validator/statement_distribution/statement_distribution.hpp @@ -22,6 +22,7 @@ #include "parachain/validator/network_bridge.hpp" #include "parachain/validator/signer.hpp" #include "parachain/validator/statement_distribution/peer_state.hpp" +#include "parachain/validator/statement_distribution/i_statement_distribution.hpp" #include "parachain/validator/statement_distribution/per_session_state.hpp" #include "parachain/validator/statement_distribution/types.hpp" #include "utils/pool_handler_ready_make.hpp" @@ -34,6 +35,7 @@ namespace kagome::parachain::statement_distribution { class StatementDistribution : public std::enable_shared_from_this, + public IStatementDistribution, public network::CanDisconnect { public: enum class Error : uint8_t { @@ -97,11 +99,11 @@ namespace kagome::parachain::statement_distribution { // outcome::result void OnFetchAttestedCandidateRequest( const network::vstaging::AttestedCandidateRequest &request, - std::shared_ptr stream); + std::shared_ptr stream) override; // CanDisconnect bool can_disconnect(const libp2p::PeerId &) const override; - void store_parachain_processor(std::weak_ptr pp) { + void store_parachain_processor(std::weak_ptr pp) override { BOOST_ASSERT(!pp.expired()); parachain_processor = std::move(pp); } @@ -113,21 +115,21 @@ namespace kagome::parachain::statement_distribution { // validators group or sends a request to fetch the attested candidate. void handle_incoming_manifest( const libp2p::peer::PeerId &peer_id, - const network::vstaging::BackedCandidateManifest &msg); + const network::vstaging::BackedCandidateManifest &msg) override; void handle_incoming_acknowledgement( const libp2p::peer::PeerId &peer_id, const network::vstaging::BackedCandidateAcknowledgement - &acknowledgement); + &acknowledgement) override; void handle_incoming_statement( const libp2p::peer::PeerId &peer_id, - const network::vstaging::StatementDistributionMessageStatement &stm); + const network::vstaging::StatementDistributionMessageStatement &stm) override; - void handle_backed_candidate_message(const CandidateHash &candidate_hash); + void handle_backed_candidate_message(const CandidateHash &candidate_hash) override; void share_local_statement(const primitives::BlockHash &relay_parent, - const SignedFullStatementWithPVD &statement); + const SignedFullStatementWithPVD &statement) override; private: struct ManifestImportSuccess { diff --git a/test/core/parachain/CMakeLists.txt b/test/core/parachain/CMakeLists.txt index c6d2b59f20..cf83a4eb66 100644 --- a/test/core/parachain/CMakeLists.txt +++ b/test/core/parachain/CMakeLists.txt @@ -30,6 +30,18 @@ target_link_libraries(scope_test logger ) +addtest(backing_test + backing.cpp + ) + +target_link_libraries(backing_test + prospective_parachains + log_configurator + base_fs_test + key_store + logger + ) + addtest(fragment_chain_test fragment_chain.cpp ) diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp new file mode 100644 index 0000000000..1860809ca5 --- /dev/null +++ b/test/core/parachain/backing.cpp @@ -0,0 +1,162 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "core/parachain/parachain_test_harness.hpp" +#include "parachain/validator/parachain_processor.hpp" + +using namespace kagome::parachain; +namespace runtime = kagome::runtime; + +class BackingTest : public ProspectiveParachainsTestHarness { + void SetUp() override { + ProspectiveParachainsTestHarness::SetUp(); + parachain_api_ = std::make_shared(); + } + + void TearDown() override { + parachain_api_.reset(); + ProspectiveParachainsTestHarness::TearDown(); + } + + public: + std::shared_ptr parachain_api_; + struct TestState { + std::vector chain_ids; + + TestState() { + const ParachainId chain_a(1); + const ParachainId chain_b(2); + + chain_ids = {chain_a, chain_b}; + } + }; + + struct TestLeaf { + BlockNumber number; + Hash hash; + std::vector> min_relay_parents; + }; + + void activate_leaf(const TestLeaf &leaf, const TestState &test_state) { + const auto &[leaf_number, leaf_hash, min_relay_parents] = leaf; + network::ExView update{ + .view = {}, + .new_head = + BlockHeader{ + .number = leaf_number, + .parent_hash = get_parent_hash(leaf_hash), + .state_root = {}, + .extrinsics_root = {}, + .digest = {}, + .hash_opt = {}, + }, + .lost = {}, + }; + update.new_head.hash_opt = leaf_hash; + + EXPECT_CALL(*parachain_api_, staging_async_backing_params(leaf_hash)) + .WillRepeatedly(Return(outcome::success(fragment::AsyncBackingParams{ + .max_candidate_depth = 4, .allowed_ancestry_len = 3}))); + + const BlockNumber min_min = [&]() -> BlockNumber { + std::optional min_min; + for (const auto &[_, block_num] : min_relay_parents) { + min_min = (min_min ? std::min(*min_min, block_num) : block_num); + } + if (min_min) { + return *min_min; + } + return leaf_number; + }(); + + const auto ancestry_len = leaf_number + 1 - min_min; + std::vector ancestry_hashes; + std::vector ancestry_numbers; + + Hash d = leaf_hash; + for (BlockNumber x = 0; x < ancestry_len; ++x) { + ancestry_hashes.emplace_back(d); + ancestry_numbers.push_back(leaf_number - x); + d = get_parent_hash(d); + } + ASSERT_EQ(ancestry_hashes.size(), ancestry_numbers.size()); + + // std::cout << "---------------------------" << std::endl; + // for(size_t i = 0; i < ancestry_hashes.size(); ++i) { + // const auto &h = ancestry_hashes[i]; + // const auto &n = ancestry_numbers[i]; + // + // std::cout << + // fmt::format("{} {}", n, h) << std::endl; + //} + + size_t requested_len = 0; + for (size_t i = 0; i < ancestry_hashes.size(); ++i) { + const auto &hash = ancestry_hashes[i]; + const auto &number = ancestry_numbers[i]; + const auto parent_hash = + ((i == ancestry_hashes.size() - 1) ? get_parent_hash(hash) + : ancestry_hashes[i + 1]); + + EXPECT_CALL(*block_tree_, getBlockHeader(hash)) + .WillRepeatedly(Return(BlockHeader{ + .number = number, + .parent_hash = parent_hash, + .state_root = {}, + .extrinsics_root = {}, + .digest = {}, + .hash_opt = {}, + })); + + if (requested_len == 0) { + assert_matches!( + virtual_overseer.recv().await, + AllMessages::ProspectiveParachains( + ProspectiveParachainsMessage::GetMinimumRelayParents(parent, tx) + ) if parent == leaf_hash => { + tx.send(min_relay_parents.clone()).unwrap(); + } + ); + } + + requested_len += 1; + } + } +}; + +TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { + TestState test_state; + + const BlockNumber LEAF_A_BLOCK_NUMBER = 100; + const BlockNumber LEAF_A_ANCESTRY_LEN = 3; + const auto para_id = test_state.chain_ids[0]; + + // `a` is grandparent of `b`. + const auto leaf_a_hash = fromNumber(130); + const auto leaf_a_parent = get_parent_hash(leaf_a_hash); + // const auto activated = new_leaf(leaf_a_hash, LEAF_A_BLOCK_NUMBER); + const TestLeaf test_leaf_a{ + .number = LEAF_A_BLOCK_NUMBER, + .hash = leaf_a_hash, + .min_relay_parents = {{para_id, + LEAF_A_BLOCK_NUMBER - LEAF_A_ANCESTRY_LEN}}, + }; + + const BlockNumber LEAF_B_BLOCK_NUMBER = LEAF_A_BLOCK_NUMBER + 2; + const BlockNumber LEAF_B_ANCESTRY_LEN = 4; + + const auto leaf_b_hash = fromNumber(128); + // let activated = new_leaf(, ); + const TestLeaf test_leaf_b{ + .number = LEAF_B_BLOCK_NUMBER, + .hash = leaf_b_hash, + .min_relay_parents = {{para_id, + LEAF_B_BLOCK_NUMBER - LEAF_B_ANCESTRY_LEN}}, + }; + + activate_leaf(test_leaf_a, test_state); + activate_leaf(test_leaf_b, test_state); +} diff --git a/test/core/parachain/parachain_test_harness.hpp b/test/core/parachain/parachain_test_harness.hpp index f28745b5ab..1716cceea9 100644 --- a/test/core/parachain/parachain_test_harness.hpp +++ b/test/core/parachain/parachain_test_harness.hpp @@ -150,4 +150,10 @@ class ProspectiveParachainsTestHarness : public testing::Test { memset(&h[0], n, 32); return h; } + + static Hash get_parent_hash(const Hash &parent) { + const auto val = *(uint8_t *)&parent[0]; + return fromNumber(val + 1); + } + }; diff --git a/test/core/parachain/prospective_parachains.cpp b/test/core/parachain/prospective_parachains.cpp index 5219de061a..5c04d8c4c7 100644 --- a/test/core/parachain/prospective_parachains.cpp +++ b/test/core/parachain/prospective_parachains.cpp @@ -108,11 +108,6 @@ class ProspectiveParachainsTest : public ProspectiveParachainsTestHarness { }; } - static Hash get_parent_hash(const Hash &parent) { - const auto val = *(uint8_t *)&parent[0]; - return fromNumber(val + 1); - } - void handle_leaf_activation_2( const network::ExView &update, const TestLeaf &leaf, From 9eb135c8d0f2b70b09cd0b21c57c7fa7fb1224c5 Mon Sep 17 00:00:00 2001 From: iceseer Date: Sun, 1 Dec 2024 19:19:46 +0300 Subject: [PATCH 02/27] [feature] IPeerView Signed-off-by: iceseer --- core/injector/application_injector.cpp | 2 + core/network/i_peer_view.hpp | 68 +++++++++++++++++++ core/network/peer_view.hpp | 62 +++-------------- .../validator/impl/parachain_processor.cpp | 6 +- .../validator/parachain_processor.hpp | 10 ++- 5 files changed, 87 insertions(+), 61 deletions(-) create mode 100644 core/network/i_peer_view.hpp diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index 75f09c5bf5..28a1ca7712 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -59,6 +59,7 @@ #include "blockchain/impl/block_header_repository_impl.hpp" #include "blockchain/impl/block_storage_impl.hpp" #include "blockchain/impl/block_tree_impl.hpp" +#include "network/i_peer_view.hpp" #include "blockchain/impl/justification_storage_policy.hpp" #include "clock/impl/basic_waitable_timer.hpp" #include "clock/impl/clock_impl.hpp" @@ -789,6 +790,7 @@ namespace { di::bind.template to(), di::bind.template to(), di::bind.template to(), + di::bind.template to(), di::bind.template to(), di::bind.template to(), di::bind.template to(), diff --git a/core/network/i_peer_view.hpp b/core/network/i_peer_view.hpp new file mode 100644 index 0000000000..630ce12a14 --- /dev/null +++ b/core/network/i_peer_view.hpp @@ -0,0 +1,68 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include + +#include + +#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 lost; + }; + + struct ExViewRef { + std::optional> new_head; + const std::vector &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; + using MyViewSubscriptionEnginePtr = + std::shared_ptr; + using MyViewSubscriber = MyViewSubscriptionEngine::SubscriberType; + using MyViewSubscriberPtr = std::shared_ptr; + + using PeerViewSubscriptionEngine = subscription:: + SubscriptionEngine; + using PeerViewSubscriptionEnginePtr = + std::shared_ptr; + using PeerViewSubscriber = PeerViewSubscriptionEngine::SubscriberType; + using PeerViewSubscriberPtr = std::shared_ptr; + + 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 diff --git a/core/network/peer_view.hpp b/core/network/peer_view.hpp index 60d768321b..db07ebbc43 100644 --- a/core/network/peer_view.hpp +++ b/core/network/peer_view.hpp @@ -5,22 +5,12 @@ */ #pragma once -#include -#include - -#include - -#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" +#include "injector/lazy.hpp" +#include "application/app_state_manager.hpp" +#include "blockchain/block_tree.hpp" namespace kagome::blockchain { class BlockTree; @@ -28,43 +18,14 @@ namespace kagome::blockchain { namespace kagome::network { - using HashedBlockHeader = primitives::BlockHeader; - struct ExView { - View view; - HashedBlockHeader new_head; - std::vector lost; - }; - - struct ExViewRef { - std::optional> new_head; - const std::vector &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 { public: - enum struct EventType : uint32_t { kViewUpdated, kPeerRemoved }; - - using PeerId = libp2p::peer::PeerId; - - using MyViewSubscriptionEngine = subscription:: - SubscriptionEngine; - using MyViewSubscriptionEnginePtr = - std::shared_ptr; - using MyViewSubscriber = MyViewSubscriptionEngine::SubscriberType; - using MyViewSubscriberPtr = std::shared_ptr; - - using PeerViewSubscriptionEngine = subscription:: - SubscriptionEngine; - using PeerViewSubscriptionEnginePtr = - std::shared_ptr; - using PeerViewSubscriber = PeerViewSubscriptionEngine::SubscriberType; - using PeerViewSubscriberPtr = std::shared_ptr; - PeerView(primitives::events::ChainSubscriptionEnginePtr chain_events_engine, std::shared_ptr app_state_manager, LazySPtr block_tree); @@ -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_; } diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index e94efcaa37..ceae885024 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -132,12 +132,11 @@ namespace kagome::parachain { ParachainProcessorImpl::ParachainProcessorImpl( std::shared_ptr pm, - std::shared_ptr runtime_info, std::shared_ptr crypto_provider, std::shared_ptr router, common::MainThreadPool &main_thread_pool, std::shared_ptr hasher, - std::shared_ptr peer_view, + std::shared_ptr peer_view, common::WorkerThreadPool &worker_thread_pool, std::shared_ptr bitfield_signer, std::shared_ptr pvf_precheck, @@ -158,7 +157,6 @@ namespace kagome::parachain { std::shared_ptr babe_config_repo, std::shared_ptr sd) : pm_(std::move(pm)), - runtime_info_(std::move(runtime_info)), crypto_provider_(std::move(crypto_provider)), router_(std::move(router)), main_pool_handler_{main_thread_pool.handler(app_state_manager)}, @@ -273,7 +271,7 @@ namespace kagome::parachain { // view. my_view_sub_ = primitives::events::subscribe( peer_view_->getMyViewObservable(), - network::PeerView::EventType::kViewUpdated, + network::IPeerView::EventType::kViewUpdated, [WEAK_SELF](const network::ExView &event) { WEAK_LOCK(self); self->onViewUpdated(event); diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index 1e42e177f9..1c5456e01e 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -23,7 +23,7 @@ #include "crypto/hasher.hpp" #include "metrics/metrics.hpp" #include "network/peer_manager.hpp" -#include "network/peer_view.hpp" +#include "network/i_peer_view.hpp" #include "network/protocols/req_collation_protocol.hpp" #include "network/router.hpp" #include "network/types/collator_messages_vstaging.hpp" @@ -166,12 +166,11 @@ namespace kagome::parachain { ParachainProcessorImpl( std::shared_ptr pm, - std::shared_ptr runtime_info, std::shared_ptr crypto_provider, std::shared_ptr router, common::MainThreadPool &main_thread_pool, std::shared_ptr hasher, - std::shared_ptr peer_view, + std::shared_ptr peer_view, common::WorkerThreadPool &worker_thread_pool, std::shared_ptr bitfield_signer, std::shared_ptr pvf_precheck, @@ -764,7 +763,6 @@ namespace kagome::parachain { const network::SignedStatement &statement); std::shared_ptr pm_; - std::shared_ptr runtime_info_; std::shared_ptr crypto_provider_; std::shared_ptr router_; log::Logger logger_ = @@ -793,8 +791,8 @@ namespace kagome::parachain { std::shared_ptr main_pool_handler_; std::shared_ptr hasher_; - std::shared_ptr peer_view_; - network::PeerView::MyViewSubscriberPtr my_view_sub_; + std::shared_ptr peer_view_; + network::IPeerView::MyViewSubscriberPtr my_view_sub_; std::shared_ptr pvf_; std::shared_ptr signer_factory_; From 42b1040aa2f573d024d58bc9bd0eb9f8f0dd7019 Mon Sep 17 00:00:00 2001 From: iceseer Date: Sun, 1 Dec 2024 19:29:15 +0300 Subject: [PATCH 03/27] [feature] IBitfieldSigner Signed-off-by: iceseer --- core/injector/application_injector.cpp | 2 ++ .../availability/bitfield/signer.hpp | 29 +++++++++++++------ .../validator/impl/parachain_processor.cpp | 2 +- .../validator/parachain_processor.hpp | 4 +-- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index 28a1ca7712..86688e283a 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -153,6 +153,7 @@ #include "parachain/approval/approval_distribution.hpp" #include "parachain/approval/approval_thread_pool.hpp" #include "parachain/availability/bitfield/store_impl.hpp" +#include "parachain/availability/bitfield/signer.hpp" #include "parachain/availability/fetch/fetch_impl.hpp" #include "parachain/availability/recovery/recovery_impl.hpp" #include "parachain/availability/store/store_impl.hpp" @@ -791,6 +792,7 @@ namespace { di::bind.template to(), di::bind.template to(), di::bind.template to(), + di::bind.template to(), di::bind.template to(), di::bind.template to(), di::bind.template to(), diff --git a/core/parachain/availability/bitfield/signer.hpp b/core/parachain/availability/bitfield/signer.hpp index 32d871771d..53782e27d8 100644 --- a/core/parachain/availability/bitfield/signer.hpp +++ b/core/parachain/availability/bitfield/signer.hpp @@ -18,13 +18,25 @@ #include "runtime/runtime_api/parachain_host.hpp" namespace kagome::parachain { + class IBitfieldSigner { + public: + using BroadcastCallback = std::function; + using Candidates = std::vector>; + + virtual ~IBitfieldSigner() = default; + virtual void start() = 0; + + /// Sign bitfield for given block. + virtual outcome::result sign(const ValidatorSigner &signer, + const Candidates &candidates) = 0; + + virtual void setBroadcastCallback(BroadcastCallback &&callback) = 0; + }; + /// Signs, stores and broadcasts bitfield for every new head. - class BitfieldSigner : public std::enable_shared_from_this { + class BitfieldSigner : public IBitfieldSigner, public std::enable_shared_from_this { public: - using BroadcastCallback = std::function; - using Candidates = std::vector>; - BitfieldSigner( std::shared_ptr hasher, std::shared_ptr signer_factory, @@ -36,13 +48,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 sign(const ValidatorSigner &signer, - const Candidates &candidates); + const Candidates &candidates) override; - void setBroadcastCallback(BroadcastCallback &&callback); + void setBroadcastCallback(BroadcastCallback &&callback) override; private: using BlockHash = primitives::BlockHash; diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index ceae885024..f58e49e299 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -138,7 +138,7 @@ namespace kagome::parachain { std::shared_ptr hasher, std::shared_ptr peer_view, common::WorkerThreadPool &worker_thread_pool, - std::shared_ptr bitfield_signer, + std::shared_ptr bitfield_signer, std::shared_ptr pvf_precheck, std::shared_ptr bitfield_store, std::shared_ptr backing_store, diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index 1c5456e01e..8f9191dc3c 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -172,7 +172,7 @@ namespace kagome::parachain { std::shared_ptr hasher, std::shared_ptr peer_view, common::WorkerThreadPool &worker_thread_pool, - std::shared_ptr bitfield_signer, + std::shared_ptr bitfield_signer, std::shared_ptr pvf_precheck, std::shared_ptr bitfield_store, std::shared_ptr backing_store, @@ -796,7 +796,7 @@ namespace kagome::parachain { std::shared_ptr pvf_; std::shared_ptr signer_factory_; - std::shared_ptr bitfield_signer_; + std::shared_ptr bitfield_signer_; std::shared_ptr pvf_precheck_; std::shared_ptr bitfield_store_; std::shared_ptr backing_store_; From d4b4cd445c076d27ddcfc15f404969fee7ada3fd Mon Sep 17 00:00:00 2001 From: iceseer Date: Sun, 1 Dec 2024 19:38:57 +0300 Subject: [PATCH 04/27] [feature] IValidatorSignerFactory Signed-off-by: iceseer --- core/injector/application_injector.cpp | 1 + .../validator/impl/parachain_processor.cpp | 2 +- .../validator/parachain_processor.hpp | 4 ++-- core/parachain/validator/signer.hpp | 19 ++++++++++++++++--- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index 86688e283a..f7b9e277fa 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -795,6 +795,7 @@ namespace { di::bind.template to(), di::bind.template to(), di::bind.template to(), + di::bind.template to(), di::bind.template to(), di::bind.template to(), di::bind.template to(), diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index f58e49e299..f3fe528326 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -145,7 +145,7 @@ namespace kagome::parachain { std::shared_ptr pvf, std::shared_ptr av_store, std::shared_ptr parachain_host, - std::shared_ptr signer_factory, + std::shared_ptr signer_factory, const application::AppConfiguration &app_config, application::AppStateManager &app_state_manager, primitives::events::ChainSubscriptionEnginePtr chain_sub_engine, diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index 8f9191dc3c..1b0b7d7513 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -179,7 +179,7 @@ namespace kagome::parachain { std::shared_ptr pvf, std::shared_ptr av_store, std::shared_ptr parachain_host, - std::shared_ptr signer_factory, + std::shared_ptr signer_factory, const application::AppConfiguration &app_config, application::AppStateManager &app_state_manager, primitives::events::ChainSubscriptionEnginePtr chain_sub_engine, @@ -795,7 +795,7 @@ namespace kagome::parachain { network::IPeerView::MyViewSubscriberPtr my_view_sub_; std::shared_ptr pvf_; - std::shared_ptr signer_factory_; + std::shared_ptr signer_factory_; std::shared_ptr bitfield_signer_; std::shared_ptr pvf_precheck_; std::shared_ptr bitfield_store_; diff --git a/core/parachain/validator/signer.hpp b/core/parachain/validator/signer.hpp index 2151f0dce1..b0e59a4c2e 100644 --- a/core/parachain/validator/signer.hpp +++ b/core/parachain/validator/signer.hpp @@ -100,7 +100,20 @@ namespace kagome::parachain { }; /// Creates validator signer. - class ValidatorSignerFactory { + class IValidatorSignerFactory { + public: + virtual ~IValidatorSignerFactory() = default; + + /// Create validator signer if keypair belongs to validator at given block. + virtual outcome::result> at( + const primitives::BlockHash &relay_parent) = 0; + + virtual outcome::result> getAuthorityValidatorIndex( + const primitives::BlockHash &relay_parent) = 0; + }; + + /// Creates validator signer. + class ValidatorSignerFactory : public IValidatorSignerFactory, std::enable_shared_from_this { public: ValidatorSignerFactory( std::shared_ptr parachain_api, @@ -110,10 +123,10 @@ namespace kagome::parachain { /// Create validator signer if keypair belongs to validator at given block. outcome::result> at( - const primitives::BlockHash &relay_parent); + const primitives::BlockHash &relay_parent) override; outcome::result> getAuthorityValidatorIndex( - const primitives::BlockHash &relay_parent); + const primitives::BlockHash &relay_parent) override; private: std::shared_ptr parachain_api_; From faf43fa1dcb4f026bed75c7268cf44dd3a9ee2f1 Mon Sep 17 00:00:00 2001 From: iceseer Date: Sun, 1 Dec 2024 19:46:21 +0300 Subject: [PATCH 05/27] [feature] IPvfPrecheck Signed-off-by: iceseer --- core/injector/application_injector.cpp | 1 + core/parachain/pvf/precheck.hpp | 14 +++++++++++--- .../validator/impl/parachain_processor.cpp | 2 +- core/parachain/validator/parachain_processor.hpp | 4 ++-- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index f7b9e277fa..ac8f0d9708 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -799,6 +799,7 @@ namespace { di::bind.template to(), di::bind.template to(), di::bind.template to(), + di::bind.template to(), bind_by_lambda( [](const auto &injector) { return injector.template create>(); diff --git a/core/parachain/pvf/precheck.hpp b/core/parachain/pvf/precheck.hpp index 97abcad342..06b147691f 100644 --- a/core/parachain/pvf/precheck.hpp +++ b/core/parachain/pvf/precheck.hpp @@ -39,13 +39,21 @@ namespace kagome::runtime { namespace kagome::parachain { class PvfPool; - /// Signs pvf check statement for every new head. - class PvfPrecheck : public std::enable_shared_from_this { + class IPvfPrecheck { public: using BroadcastCallback = std::function; using Candidates = std::vector>; + 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 { + public: PvfPrecheck( std::shared_ptr hasher, std::shared_ptr block_tree, @@ -60,7 +68,7 @@ namespace kagome::parachain { primitives::events::ChainSubscriptionEnginePtr chain_sub_engine); /// Subscribes to new heads. - void start(); + void start() override; private: using BlockHash = primitives::BlockHash; diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index f3fe528326..acabee5144 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -139,7 +139,7 @@ namespace kagome::parachain { std::shared_ptr peer_view, common::WorkerThreadPool &worker_thread_pool, std::shared_ptr bitfield_signer, - std::shared_ptr pvf_precheck, + std::shared_ptr pvf_precheck, std::shared_ptr bitfield_store, std::shared_ptr backing_store, std::shared_ptr pvf, diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index 1b0b7d7513..637d48e530 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -173,7 +173,7 @@ namespace kagome::parachain { std::shared_ptr peer_view, common::WorkerThreadPool &worker_thread_pool, std::shared_ptr bitfield_signer, - std::shared_ptr pvf_precheck, + std::shared_ptr pvf_precheck, std::shared_ptr bitfield_store, std::shared_ptr backing_store, std::shared_ptr pvf, @@ -797,7 +797,7 @@ namespace kagome::parachain { std::shared_ptr pvf_; std::shared_ptr signer_factory_; std::shared_ptr bitfield_signer_; - std::shared_ptr pvf_precheck_; + std::shared_ptr pvf_precheck_; std::shared_ptr bitfield_store_; std::shared_ptr backing_store_; std::shared_ptr av_store_; From 38922863c2ee978fc427a5e72014a7bef0efa6b1 Mon Sep 17 00:00:00 2001 From: iceseer Date: Sun, 1 Dec 2024 20:01:31 +0300 Subject: [PATCH 06/27] [feature] IProspectiveParachains Signed-off-by: iceseer --- core/injector/application_injector.cpp | 1 + .../validator/backing_implicit_view.cpp | 2 +- .../validator/backing_implicit_view.hpp | 6 +- .../validator/impl/parachain_processor.cpp | 2 +- .../validator/parachain_processor.hpp | 4 +- .../prospective_parachains.hpp | 71 ++++++++++++++++--- .../statement_distribution.cpp | 2 +- .../statement_distribution.hpp | 4 +- 8 files changed, 71 insertions(+), 21 deletions(-) diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index ac8f0d9708..55d4978b55 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -798,6 +798,7 @@ namespace { di::bind.template to(), di::bind.template to(), di::bind.template to(), + di::bind.template to(), di::bind.template to(), di::bind.template to(), bind_by_lambda( diff --git a/core/parachain/validator/backing_implicit_view.cpp b/core/parachain/validator/backing_implicit_view.cpp index 7021e6c901..231b7567fa 100644 --- a/core/parachain/validator/backing_implicit_view.cpp +++ b/core/parachain/validator/backing_implicit_view.cpp @@ -30,7 +30,7 @@ OUTCOME_CPP_DEFINE_CATEGORY(kagome::parachain, ImplicitView::Error, e) { namespace kagome::parachain { ImplicitView::ImplicitView( - std::weak_ptr prospective_parachains, + std::weak_ptr prospective_parachains, std::shared_ptr parachain_host_, std::shared_ptr block_tree, std::optional collating_for_) diff --git a/core/parachain/validator/backing_implicit_view.hpp b/core/parachain/validator/backing_implicit_view.hpp index dbaa04f226..b559a1cdbb 100644 --- a/core/parachain/validator/backing_implicit_view.hpp +++ b/core/parachain/validator/backing_implicit_view.hpp @@ -22,7 +22,7 @@ namespace kagome::parachain { - class ProspectiveParachains; + class IProspectiveParachains; // Always aim to retain 1 block before the active leaves. constexpr BlockNumber MINIMUM_RETAIN_LENGTH = 2ull; @@ -124,7 +124,7 @@ namespace kagome::parachain { block_info_storage.size()); } - ImplicitView(std::weak_ptr prospective_parachains, + ImplicitView(std::weak_ptr prospective_parachains, std::shared_ptr parachain_host_, std::shared_ptr block_tree, std::optional collating_for_); @@ -161,7 +161,7 @@ namespace kagome::parachain { std::shared_ptr parachain_host; std::optional collating_for; - std::weak_ptr prospective_parachains_; + std::weak_ptr prospective_parachains_; std::shared_ptr block_tree_; log::Logger logger = log::createLogger("BackingImplicitView", "parachain"); }; diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index acabee5144..e51fbf92ea 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -151,7 +151,7 @@ namespace kagome::parachain { primitives::events::ChainSubscriptionEnginePtr chain_sub_engine, primitives::events::SyncStateSubscriptionEnginePtr sync_state_observable, std::shared_ptr query_audi, - std::shared_ptr prospective_parachains, + std::shared_ptr prospective_parachains, std::shared_ptr block_tree, LazySPtr slots_util, std::shared_ptr babe_config_repo, diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index 637d48e530..e490c69f6c 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -186,7 +186,7 @@ namespace kagome::parachain { primitives::events::SyncStateSubscriptionEnginePtr sync_state_observable, std::shared_ptr query_audi, - std::shared_ptr prospective_parachains, + std::shared_ptr prospective_parachains, std::shared_ptr block_tree, LazySPtr slots_util, std::shared_ptr babe_config_repo, @@ -813,7 +813,7 @@ namespace kagome::parachain { primitives::events::ChainSub chain_sub_; std::shared_ptr worker_pool_handler_; std::default_random_engine random_; - std::shared_ptr prospective_parachains_; + std::shared_ptr prospective_parachains_; std::shared_ptr block_tree_; std::shared_ptr statement_distribution; diff --git a/core/parachain/validator/prospective_parachains/prospective_parachains.hpp b/core/parachain/validator/prospective_parachains/prospective_parachains.hpp index ec250fb0bc..75f6da4f14 100644 --- a/core/parachain/validator/prospective_parachains/prospective_parachains.hpp +++ b/core/parachain/validator/prospective_parachains/prospective_parachains.hpp @@ -32,8 +32,57 @@ namespace kagome::parachain { using ParentHeadData = boost::variant; + class IProspectiveParachains { + public: + virtual ~IProspectiveParachains() = default; + + // Debug print of all internal buffers load. + virtual void printStoragesLoad() = 0; + + virtual std::shared_ptr getBlockTree() = 0; + + virtual std::vector> + answerMinimumRelayParentsRequest(const RelayHash &relay_parent) = 0; + + virtual std::vector> answerGetBackableCandidates( + const RelayHash &relay_parent, + ParachainId para, + uint32_t count, + const fragment::Ancestors &ancestors) = 0; + + virtual outcome::result> + answerProspectiveValidationDataRequest( + const RelayHash &candidate_relay_parent, + const ParentHeadData &parent_head_data, + ParachainId para_id) = 0; + + virtual std::optional prospectiveParachainsMode( + const RelayHash &relay_parent) = 0; + + virtual outcome::result onActiveLeavesUpdate( + const network::ExViewRef &update) = 0; + + virtual std::vector< + std::pair> + answer_hypothetical_membership_request( + const std::span &candidates, + const std::optional> + &fragment_tree_relay_parent) = 0; + + virtual void candidate_backed(ParachainId para, + const CandidateHash &candidate_hash) = 0; + + virtual bool introduce_seconded_candidate( + ParachainId para, + const network::CommittedCandidateReceipt &candidate, + const crypto::Hashed> &pvd, + const CandidateHash &candidate_hash) = 0; + }; + class ProspectiveParachains - : public std::enable_shared_from_this { + : public IProspectiveParachains, public std::enable_shared_from_this { #ifdef CFG_TESTING public: #endif // CFG_TESTING @@ -86,27 +135,27 @@ namespace kagome::parachain { std::shared_ptr block_tree); // Debug print of all internal buffers load. - void printStoragesLoad(); + void printStoragesLoad() override; - std::shared_ptr getBlockTree(); + std::shared_ptr getBlockTree() override; std::vector> - answerMinimumRelayParentsRequest(const RelayHash &relay_parent); + answerMinimumRelayParentsRequest(const RelayHash &relay_parent) override; std::vector> answerGetBackableCandidates( const RelayHash &relay_parent, ParachainId para, uint32_t count, - const fragment::Ancestors &ancestors); + const fragment::Ancestors &ancestors) override; outcome::result> answerProspectiveValidationDataRequest( const RelayHash &candidate_relay_parent, const ParentHeadData &parent_head_data, - ParachainId para_id); + ParachainId para_id) override; std::optional prospectiveParachainsMode( - const RelayHash &relay_parent); + const RelayHash &relay_parent) override; outcome::result onActiveLeavesUpdate( - const network::ExViewRef &update); + const network::ExViewRef &update) override; /// @brief calculates hypothetical candidate and fragment tree membership /// @param candidates Candidates, in arbitrary order, which should be @@ -144,10 +193,10 @@ namespace kagome::parachain { answer_hypothetical_membership_request( const std::span &candidates, const std::optional> - &fragment_tree_relay_parent); + &fragment_tree_relay_parent) override; void candidate_backed(ParachainId para, - const CandidateHash &candidate_hash); + const CandidateHash &candidate_hash) override; bool introduce_seconded_candidate( ParachainId para, @@ -155,7 +204,7 @@ namespace kagome::parachain { const crypto::Hashed> &pvd, - const CandidateHash &candidate_hash); + const CandidateHash &candidate_hash) override; }; } // namespace kagome::parachain diff --git a/core/parachain/validator/statement_distribution/statement_distribution.cpp b/core/parachain/validator/statement_distribution/statement_distribution.cpp index c177bb9829..bd35124a1a 100644 --- a/core/parachain/validator/statement_distribution/statement_distribution.cpp +++ b/core/parachain/validator/statement_distribution/statement_distribution.cpp @@ -106,7 +106,7 @@ namespace kagome::parachain::statement_distribution { std::shared_ptr sf, std::shared_ptr app_state_manager, StatementDistributionThreadPool &statements_distribution_thread_pool, - std::shared_ptr _prospective_parachains, + std::shared_ptr _prospective_parachains, std::shared_ptr _parachain_host, std::shared_ptr _block_tree, std::shared_ptr _query_audi, diff --git a/core/parachain/validator/statement_distribution/statement_distribution.hpp b/core/parachain/validator/statement_distribution/statement_distribution.hpp index 43eff621e8..48877e0bf6 100644 --- a/core/parachain/validator/statement_distribution/statement_distribution.hpp +++ b/core/parachain/validator/statement_distribution/statement_distribution.hpp @@ -76,7 +76,7 @@ namespace kagome::parachain::statement_distribution { std::shared_ptr sf, std::shared_ptr app_state_manager, StatementDistributionThreadPool &statements_distribution_thread_pool, - std::shared_ptr prospective_parachains, + std::shared_ptr prospective_parachains, std::shared_ptr parachain_host, std::shared_ptr block_tree, std::shared_ptr query_audi, @@ -367,7 +367,7 @@ namespace kagome::parachain::statement_distribution { std::shared_ptr router; std::shared_ptr main_pool_handler; std::shared_ptr hasher; - std::shared_ptr prospective_parachains; + std::shared_ptr prospective_parachains; std::shared_ptr parachain_host; std::shared_ptr crypto_provider; std::shared_ptr peer_view; From 71c1e8010bdfa8837413fc4a99489f0b68746fcb Mon Sep 17 00:00:00 2001 From: iceseer Date: Sun, 1 Dec 2024 20:05:16 +0300 Subject: [PATCH 07/27] formatting Signed-off-by: iceseer --- core/injector/application_injector.cpp | 8 +++--- core/injector/application_injector.hpp | 2 +- core/network/i_peer_view.hpp | 3 ++- core/network/peer_view.hpp | 6 ++--- .../availability/bitfield/signer.hpp | 23 +++++++++-------- core/parachain/pvf/precheck.hpp | 3 ++- .../validator/parachain_processor.hpp | 2 +- .../prospective_parachains.hpp | 23 +++++++++-------- core/parachain/validator/signer.hpp | 8 +++--- .../i_statement_distribution.hpp | 14 +++++++---- .../statement_distribution.cpp | 6 +++-- .../statement_distribution.hpp | 16 +++++++----- test/core/parachain/backing.cpp | 25 +++++++++---------- .../core/parachain/parachain_test_harness.hpp | 1 - 14 files changed, 77 insertions(+), 63 deletions(-) diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index 55d4978b55..c8e9130623 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -59,7 +59,6 @@ #include "blockchain/impl/block_header_repository_impl.hpp" #include "blockchain/impl/block_storage_impl.hpp" #include "blockchain/impl/block_tree_impl.hpp" -#include "network/i_peer_view.hpp" #include "blockchain/impl/justification_storage_policy.hpp" #include "clock/impl/basic_waitable_timer.hpp" #include "clock/impl/clock_impl.hpp" @@ -114,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" @@ -152,12 +152,11 @@ #include "outcome/outcome.hpp" #include "parachain/approval/approval_distribution.hpp" #include "parachain/approval/approval_thread_pool.hpp" -#include "parachain/availability/bitfield/store_impl.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" #include "parachain/availability/store/store_impl.hpp" -#include "parachain/validator/statement_distribution/i_statement_distribution.hpp" #include "parachain/backing/store_impl.hpp" #include "parachain/pvf/module_precompiler.hpp" #include "parachain/pvf/pool.hpp" @@ -166,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" @@ -954,7 +954,7 @@ namespace kagome::injector { KagomeNodeInjector::KagomeNodeInjector( sptr app_config) : pimpl_{std::make_unique( - makeKagomeNodeInjector(std::move(app_config)))} {} + makeKagomeNodeInjector(std::move(app_config)))} {} sptr KagomeNodeInjector::injectAppConfig() { return pimpl_->injector_ diff --git a/core/injector/application_injector.hpp b/core/injector/application_injector.hpp index 305953dc65..b5608e5a68 100644 --- a/core/injector/application_injector.hpp +++ b/core/injector/application_injector.hpp @@ -64,7 +64,7 @@ namespace kagome { namespace statement_distribution { class StatementDistribution; class IStatementDistribution; - } + } // namespace statement_distribution } // namespace parachain namespace runtime { diff --git a/core/network/i_peer_view.hpp b/core/network/i_peer_view.hpp index 630ce12a14..fb1f1417bc 100644 --- a/core/network/i_peer_view.hpp +++ b/core/network/i_peer_view.hpp @@ -61,7 +61,8 @@ namespace kagome::network { virtual PeerViewSubscriptionEnginePtr getRemoteViewObservable() = 0; virtual void removePeer(const PeerId &peer_id) = 0; - virtual void updateRemoteView(const PeerId &peer_id, network::View &&view) = 0; + virtual void updateRemoteView(const PeerId &peer_id, + network::View &&view) = 0; virtual const View &getMyView() const = 0; }; diff --git a/core/network/peer_view.hpp b/core/network/peer_view.hpp index db07ebbc43..1336912c8a 100644 --- a/core/network/peer_view.hpp +++ b/core/network/peer_view.hpp @@ -5,12 +5,12 @@ */ #pragma once +#include "application/app_state_manager.hpp" +#include "blockchain/block_tree.hpp" +#include "injector/lazy.hpp" #include "network/i_peer_view.hpp" #include "utils/non_copyable.hpp" #include "utils/safe_object.hpp" -#include "injector/lazy.hpp" -#include "application/app_state_manager.hpp" -#include "blockchain/block_tree.hpp" namespace kagome::blockchain { class BlockTree; diff --git a/core/parachain/availability/bitfield/signer.hpp b/core/parachain/availability/bitfield/signer.hpp index 53782e27d8..946e48d875 100644 --- a/core/parachain/availability/bitfield/signer.hpp +++ b/core/parachain/availability/bitfield/signer.hpp @@ -19,23 +19,24 @@ namespace kagome::parachain { class IBitfieldSigner { - public: - using BroadcastCallback = std::function; - using Candidates = std::vector>; + public: + using BroadcastCallback = std::function; + using Candidates = std::vector>; - virtual ~IBitfieldSigner() = default; - virtual void start() = 0; + virtual ~IBitfieldSigner() = default; + virtual void start() = 0; - /// Sign bitfield for given block. - virtual outcome::result sign(const ValidatorSigner &signer, - const Candidates &candidates) = 0; + /// Sign bitfield for given block. + virtual outcome::result sign(const ValidatorSigner &signer, + const Candidates &candidates) = 0; - virtual void setBroadcastCallback(BroadcastCallback &&callback) = 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 { + class BitfieldSigner : public IBitfieldSigner, + public std::enable_shared_from_this { public: BitfieldSigner( std::shared_ptr hasher, diff --git a/core/parachain/pvf/precheck.hpp b/core/parachain/pvf/precheck.hpp index 06b147691f..ff53769c6f 100644 --- a/core/parachain/pvf/precheck.hpp +++ b/core/parachain/pvf/precheck.hpp @@ -52,7 +52,8 @@ namespace kagome::parachain { }; /// Signs pvf check statement for every new head. - class PvfPrecheck : public IPvfPrecheck, public std::enable_shared_from_this { + class PvfPrecheck : public IPvfPrecheck, + public std::enable_shared_from_this { public: PvfPrecheck( std::shared_ptr hasher, diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index e490c69f6c..2d5502df4f 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -22,8 +22,8 @@ #include "consensus/timeline/slots_util.hpp" #include "crypto/hasher.hpp" #include "metrics/metrics.hpp" -#include "network/peer_manager.hpp" #include "network/i_peer_view.hpp" +#include "network/peer_manager.hpp" #include "network/protocols/req_collation_protocol.hpp" #include "network/router.hpp" #include "network/types/collator_messages_vstaging.hpp" diff --git a/core/parachain/validator/prospective_parachains/prospective_parachains.hpp b/core/parachain/validator/prospective_parachains/prospective_parachains.hpp index 75f6da4f14..01127d0cf1 100644 --- a/core/parachain/validator/prospective_parachains/prospective_parachains.hpp +++ b/core/parachain/validator/prospective_parachains/prospective_parachains.hpp @@ -32,9 +32,9 @@ namespace kagome::parachain { using ParentHeadData = boost::variant; - class IProspectiveParachains { - public: - virtual ~IProspectiveParachains() = default; + class IProspectiveParachains { + public: + virtual ~IProspectiveParachains() = default; // Debug print of all internal buffers load. virtual void printStoragesLoad() = 0; @@ -44,11 +44,11 @@ namespace kagome::parachain { virtual std::vector> answerMinimumRelayParentsRequest(const RelayHash &relay_parent) = 0; - virtual std::vector> answerGetBackableCandidates( - const RelayHash &relay_parent, - ParachainId para, - uint32_t count, - const fragment::Ancestors &ancestors) = 0; + virtual std::vector> + answerGetBackableCandidates(const RelayHash &relay_parent, + ParachainId para, + uint32_t count, + const fragment::Ancestors &ancestors) = 0; virtual outcome::result> answerProspectiveValidationDataRequest( @@ -70,7 +70,7 @@ namespace kagome::parachain { &fragment_tree_relay_parent) = 0; virtual void candidate_backed(ParachainId para, - const CandidateHash &candidate_hash) = 0; + const CandidateHash &candidate_hash) = 0; virtual bool introduce_seconded_candidate( ParachainId para, @@ -79,10 +79,11 @@ namespace kagome::parachain { 32, crypto::Blake2b_StreamHasher<32>> &pvd, const CandidateHash &candidate_hash) = 0; - }; + }; class ProspectiveParachains - : public IProspectiveParachains, public std::enable_shared_from_this { + : public IProspectiveParachains, + public std::enable_shared_from_this { #ifdef CFG_TESTING public: #endif // CFG_TESTING diff --git a/core/parachain/validator/signer.hpp b/core/parachain/validator/signer.hpp index b0e59a4c2e..1c2d8af446 100644 --- a/core/parachain/validator/signer.hpp +++ b/core/parachain/validator/signer.hpp @@ -108,12 +108,14 @@ namespace kagome::parachain { virtual outcome::result> at( const primitives::BlockHash &relay_parent) = 0; - virtual outcome::result> getAuthorityValidatorIndex( - const primitives::BlockHash &relay_parent) = 0; + virtual outcome::result> + getAuthorityValidatorIndex(const primitives::BlockHash &relay_parent) = 0; }; /// Creates validator signer. - class ValidatorSignerFactory : public IValidatorSignerFactory, std::enable_shared_from_this { + class ValidatorSignerFactory + : public IValidatorSignerFactory, + std::enable_shared_from_this { public: ValidatorSignerFactory( std::shared_ptr parachain_api, diff --git a/core/parachain/validator/statement_distribution/i_statement_distribution.hpp b/core/parachain/validator/statement_distribution/i_statement_distribution.hpp index 36afa75512..d8ded36026 100644 --- a/core/parachain/validator/statement_distribution/i_statement_distribution.hpp +++ b/core/parachain/validator/statement_distribution/i_statement_distribution.hpp @@ -40,7 +40,8 @@ namespace kagome::parachain::statement_distribution { const network::vstaging::AttestedCandidateRequest &request, std::shared_ptr stream) = 0; - virtual void store_parachain_processor(std::weak_ptr pp) = 0; + virtual void store_parachain_processor( + std::weak_ptr pp) = 0; virtual void handle_incoming_manifest( const libp2p::peer::PeerId &peer_id, @@ -53,12 +54,15 @@ namespace kagome::parachain::statement_distribution { virtual void handle_incoming_statement( const libp2p::peer::PeerId &peer_id, - const network::vstaging::StatementDistributionMessageStatement &stm) = 0; + const network::vstaging::StatementDistributionMessageStatement + &stm) = 0; - virtual void handle_backed_candidate_message(const CandidateHash &candidate_hash) = 0; + virtual void handle_backed_candidate_message( + const CandidateHash &candidate_hash) = 0; - virtual void share_local_statement(const primitives::BlockHash &relay_parent, - const SignedFullStatementWithPVD &statement) = 0; + virtual void share_local_statement( + const primitives::BlockHash &relay_parent, + const SignedFullStatementWithPVD &statement) = 0; }; } // namespace kagome::parachain::statement_distribution diff --git a/core/parachain/validator/statement_distribution/statement_distribution.cpp b/core/parachain/validator/statement_distribution/statement_distribution.cpp index bd35124a1a..4c422c8463 100644 --- a/core/parachain/validator/statement_distribution/statement_distribution.cpp +++ b/core/parachain/validator/statement_distribution/statement_distribution.cpp @@ -119,8 +119,10 @@ namespace kagome::parachain::statement_distribution { LazySPtr _slots_util, std::shared_ptr _babe_config_repo, primitives::events::PeerSubscriptionEnginePtr _peer_events_engine) - : implicit_view( - _prospective_parachains, _parachain_host, _block_tree, std::nullopt), + : implicit_view(_prospective_parachains, + _parachain_host, + _block_tree, + std::nullopt), per_session(RefCache::create()), signer_factory(std::move(sf)), peer_use_count( diff --git a/core/parachain/validator/statement_distribution/statement_distribution.hpp b/core/parachain/validator/statement_distribution/statement_distribution.hpp index 48877e0bf6..e1557c606d 100644 --- a/core/parachain/validator/statement_distribution/statement_distribution.hpp +++ b/core/parachain/validator/statement_distribution/statement_distribution.hpp @@ -21,8 +21,8 @@ #include "parachain/validator/impl/candidates.hpp" #include "parachain/validator/network_bridge.hpp" #include "parachain/validator/signer.hpp" -#include "parachain/validator/statement_distribution/peer_state.hpp" #include "parachain/validator/statement_distribution/i_statement_distribution.hpp" +#include "parachain/validator/statement_distribution/peer_state.hpp" #include "parachain/validator/statement_distribution/per_session_state.hpp" #include "parachain/validator/statement_distribution/types.hpp" #include "utils/pool_handler_ready_make.hpp" @@ -103,7 +103,8 @@ namespace kagome::parachain::statement_distribution { // CanDisconnect bool can_disconnect(const libp2p::PeerId &) const override; - void store_parachain_processor(std::weak_ptr pp) override { + void store_parachain_processor( + std::weak_ptr pp) override { BOOST_ASSERT(!pp.expired()); parachain_processor = std::move(pp); } @@ -124,12 +125,15 @@ namespace kagome::parachain::statement_distribution { void handle_incoming_statement( const libp2p::peer::PeerId &peer_id, - const network::vstaging::StatementDistributionMessageStatement &stm) override; + const network::vstaging::StatementDistributionMessageStatement &stm) + override; - void handle_backed_candidate_message(const CandidateHash &candidate_hash) override; + void handle_backed_candidate_message( + const CandidateHash &candidate_hash) override; - void share_local_statement(const primitives::BlockHash &relay_parent, - const SignedFullStatementWithPVD &statement) override; + void share_local_statement( + const primitives::BlockHash &relay_parent, + const SignedFullStatementWithPVD &statement) override; private: struct ManifestImportSuccess { diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 1860809ca5..3e0600abf0 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -93,7 +93,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { // fmt::format("{} {}", n, h) << std::endl; //} - size_t requested_len = 0; + size_t requested_len = 0; for (size_t i = 0; i < ancestry_hashes.size(); ++i) { const auto &hash = ancestry_hashes[i]; const auto &number = ancestry_numbers[i]; @@ -111,18 +111,17 @@ class BackingTest : public ProspectiveParachainsTestHarness { .hash_opt = {}, })); - if (requested_len == 0) { - assert_matches!( - virtual_overseer.recv().await, - AllMessages::ProspectiveParachains( - ProspectiveParachainsMessage::GetMinimumRelayParents(parent, tx) - ) if parent == leaf_hash => { - tx.send(min_relay_parents.clone()).unwrap(); - } - ); - } - - requested_len += 1; + if (requested_len == 0) { + assert_matches !( + virtual_overseer.recv().await, + AllMessages::ProspectiveParachains( + ProspectiveParachainsMessage::GetMinimumRelayParents( + parent, tx)) if parent + == leaf_hash = + > { tx.send(min_relay_parents.clone()).unwrap(); }); + } + + requested_len += 1; } } }; diff --git a/test/core/parachain/parachain_test_harness.hpp b/test/core/parachain/parachain_test_harness.hpp index 1716cceea9..7af6e9255e 100644 --- a/test/core/parachain/parachain_test_harness.hpp +++ b/test/core/parachain/parachain_test_harness.hpp @@ -155,5 +155,4 @@ class ProspectiveParachainsTestHarness : public testing::Test { const auto val = *(uint8_t *)&parent[0]; return fromNumber(val + 1); } - }; From 15c4449da0f494b5d2b4a8c5836156f911b9b38a Mon Sep 17 00:00:00 2001 From: iceseer Date: Mon, 2 Dec 2024 12:50:01 +0300 Subject: [PATCH 08/27] backing tests Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 15 ++-- .../parachain/prospective_parachains_mock.hpp | 84 +++++++++++++++++++ 2 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 test/mock/core/parachain/prospective_parachains_mock.hpp diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 3e0600abf0..cf867ee21a 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -6,6 +6,7 @@ #include "core/parachain/parachain_test_harness.hpp" #include "parachain/validator/parachain_processor.hpp" +#include "mock/core/parachain/prospective_parachains_mock.hpp" using namespace kagome::parachain; namespace runtime = kagome::runtime; @@ -112,13 +113,13 @@ class BackingTest : public ProspectiveParachainsTestHarness { })); if (requested_len == 0) { - assert_matches !( - virtual_overseer.recv().await, - AllMessages::ProspectiveParachains( - ProspectiveParachainsMessage::GetMinimumRelayParents( - parent, tx)) if parent - == leaf_hash = - > { tx.send(min_relay_parents.clone()).unwrap(); }); + //assert_matches !( + // virtual_overseer.recv().await, + // AllMessages::ProspectiveParachains( + // ProspectiveParachainsMessage::GetMinimumRelayParents( + // parent, tx)) if parent + // == leaf_hash = + // > { tx.send(min_relay_parents.clone()).unwrap(); }); } requested_len += 1; diff --git a/test/mock/core/parachain/prospective_parachains_mock.hpp b/test/mock/core/parachain/prospective_parachains_mock.hpp new file mode 100644 index 0000000000..aaba126cc2 --- /dev/null +++ b/test/mock/core/parachain/prospective_parachains_mock.hpp @@ -0,0 +1,84 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "parachain/validator/prospective_parachains/prospective_parachains.hpp" + +#include + +namespace kagome::parachain { + + class ProspectiveParachainsMock : public IProspectiveParachains { + public: + MOCK_METHOD(void, printStoragesLoad, (), (override)); + + MOCK_METHOD(std::shared_ptr, + getBlockTree, + (), + (override)); + + MOCK_METHOD((std::vector>), + answerMinimumRelayParentsRequest, + (const RelayHash &), + (override)); + + MOCK_METHOD( + (std::vector>), + answerGetBackableCandidates, + (const RelayHash &, ParachainId, uint32_t, const fragment::Ancestors &), + (override)); + + MOCK_METHOD( + outcome::result>, + answerProspectiveValidationDataRequest, + (const RelayHash &, const ParentHeadData &, ParachainId), + (override)); + + MOCK_METHOD(std::optional, + prospectiveParachainsMode, + (const RelayHash &), + (override)); + + MOCK_METHOD(outcome::result, + onActiveLeavesUpdate, + (const network::ExViewRef &), + (override)); + + MOCK_METHOD((std::vector>), + answer_hypothetical_membership_request, + (const std::span &, + const std::optional> &), + (override)); + + MOCK_METHOD(void, + candidate_backed, + (ParachainId, const CandidateHash &), + (override)); + + MOCK_METHOD(bool, + introduce_seconded_candidate, + (ParachainId, + const network::CommittedCandidateReceipt &, + (const crypto::Hashed> &), + const CandidateHash &), + (override)); + }; + +} // namespace kagome::parachain + +namespace boost { + inline auto &operator<<(std::ostream &s, const kagome::parachain::ParentHeadData &) { + return s; + } + inline auto &operator<<(std::ostream &s, const kagome::parachain::HypotheticalCandidate &) { + return s; + } +} // kagome::parachain + From 362c5e7adcd6faa44a534af9e024e9bacfc3c6f5 Mon Sep 17 00:00:00 2001 From: iceseer Date: Tue, 3 Dec 2024 11:25:06 +0300 Subject: [PATCH 09/27] [feature] peerviewmock Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 70 +++++++++++++++++++++++ test/mock/core/network/peer_view_mock.hpp | 46 +++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 test/mock/core/network/peer_view_mock.hpp diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index cf867ee21a..7612b3ac5d 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -6,24 +6,94 @@ #include "core/parachain/parachain_test_harness.hpp" #include "parachain/validator/parachain_processor.hpp" +#include "common/main_thread_pool.hpp" +#include "common/worker_thread_pool.hpp" #include "mock/core/parachain/prospective_parachains_mock.hpp" +#include "mock/core/application/app_configuration_mock.hpp" +#include "mock/core/application/app_state_manager_mock.hpp" +#include "mock/core/network/peer_view_mock.hpp" +#include "mock/core/network/peer_manager_mock.hpp" +#include "mock/core/network/router_mock.hpp" +#include "mock/core/crypto/sr25519_provider_mock.hpp" using namespace kagome::parachain; namespace runtime = kagome::runtime; +using kagome::Watchdog; +using kagome::application::AppConfigurationMock; +using kagome::application::StartApp; +using kagome::common::MainThreadPool; +using kagome::common::WorkerThreadPool; +using kagome::network::PeerViewMock; +using kagome::network::PeerManagerMock; +using kagome::network::RouterMock; +using kagome::crypto::Sr25519ProviderMock; + + class BackingTest : public ProspectiveParachainsTestHarness { void SetUp() override { ProspectiveParachainsTestHarness::SetUp(); parachain_api_ = std::make_shared(); + + watchdog_ = std::make_shared(std::chrono::milliseconds(1)); + main_thread_pool_ = std::make_shared(watchdog_, std::make_shared()); + worker_thread_pool_ = std::make_shared(watchdog_, 1); + peer_manager_ = std::make_shared(); + sr25519_provider_ = std::make_shared(); + router_ = std::make_shared(); + peer_view_ = std::make_shared(); + + StartApp app_state_manager; + + + + ParachainProcessorImpl( + peer_manager_, + sr25519_provider_, + router_, + *main_thread_pool_, + hasher_, + std::shared_ptr peer_view, + common::WorkerThreadPool &worker_thread_pool, + std::shared_ptr bitfield_signer, + std::shared_ptr pvf_precheck, + std::shared_ptr bitfield_store, + std::shared_ptr backing_store, + std::shared_ptr pvf, + std::shared_ptr av_store, + std::shared_ptr parachain_host, + std::shared_ptr signer_factory, + const application::AppConfiguration &app_config, + application::AppStateManager &app_state_manager, + primitives::events::ChainSubscriptionEnginePtr chain_sub_engine, + primitives::events::SyncStateSubscriptionEnginePtr + sync_state_observable, + std::shared_ptr query_audi, + std::shared_ptr prospective_parachains, + std::shared_ptr block_tree, + LazySPtr slots_util, + std::shared_ptr babe_config_repo, + std::shared_ptr + statement_distribution); } void TearDown() override { + watchdog_->stop(); parachain_api_.reset(); ProspectiveParachainsTestHarness::TearDown(); } public: std::shared_ptr parachain_api_; + AppConfigurationMock app_config_; + std::shared_ptr watchdog_; + std::shared_ptr main_thread_pool_; + std::shared_ptr worker_thread_pool_; + std::shared_ptr peer_manager_; + std::shared_ptr sr25519_provider_; + std::shared_ptr router_; + std::shared_ptr peer_view_; + struct TestState { std::vector chain_ids; diff --git a/test/mock/core/network/peer_view_mock.hpp b/test/mock/core/network/peer_view_mock.hpp new file mode 100644 index 0000000000..50bee5b548 --- /dev/null +++ b/test/mock/core/network/peer_view_mock.hpp @@ -0,0 +1,46 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "network/peer_view.hpp" + +#include + +namespace kagome::network { + class PeerViewMock : public IPeerView { + public: + MOCK_METHOD(size_t, + peersCount, + (), + (const, override)); + + MOCK_METHOD(MyViewSubscriptionEnginePtr, + getMyViewObservable, + (), + (override)); + + MOCK_METHOD(PeerViewSubscriptionEnginePtr, + getRemoteViewObservable, + (), + (override)); + + MOCK_METHOD(void, + removePeer, + (const PeerId &), + (override)); + + MOCK_METHOD(void, + updateRemoteView, + (const PeerId &, network::View &&), + (override)); + + MOCK_METHOD(const View &, + getMyView, + (), + (const, override)); + }; +} // namespace kagome::network From 962d959e744a452bef6cccf70d87ddcd1d6d83cd Mon Sep 17 00:00:00 2001 From: iceseer Date: Tue, 3 Dec 2024 11:35:53 +0300 Subject: [PATCH 10/27] [feature] BitfieldSignerMock Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 13 +++++--- .../core/parachain/bitfield_signer_mock.hpp | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 test/mock/core/parachain/bitfield_signer_mock.hpp diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 7612b3ac5d..96ddbe6b5a 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -9,6 +9,7 @@ #include "common/main_thread_pool.hpp" #include "common/worker_thread_pool.hpp" #include "mock/core/parachain/prospective_parachains_mock.hpp" +#include "mock/core/parachain/bitfield_signer_mock.hpp" #include "mock/core/application/app_configuration_mock.hpp" #include "mock/core/application/app_state_manager_mock.hpp" #include "mock/core/network/peer_view_mock.hpp" @@ -42,20 +43,21 @@ class BackingTest : public ProspectiveParachainsTestHarness { sr25519_provider_ = std::make_shared(); router_ = std::make_shared(); peer_view_ = std::make_shared(); + bitfield_signer_ = std::make_shared(); StartApp app_state_manager; - ParachainProcessorImpl( + /*ParachainProcessorImpl( peer_manager_, sr25519_provider_, router_, *main_thread_pool_, hasher_, - std::shared_ptr peer_view, - common::WorkerThreadPool &worker_thread_pool, - std::shared_ptr bitfield_signer, + peer_view_, + *worker_thread_pool_, + bitfield_signer_, std::shared_ptr pvf_precheck, std::shared_ptr bitfield_store, std::shared_ptr backing_store, @@ -74,7 +76,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { LazySPtr slots_util, std::shared_ptr babe_config_repo, std::shared_ptr - statement_distribution); + statement_distribution);*/ } void TearDown() override { @@ -93,6 +95,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::shared_ptr sr25519_provider_; std::shared_ptr router_; std::shared_ptr peer_view_; + std::shared_ptr bitfield_signer_; struct TestState { std::vector chain_ids; diff --git a/test/mock/core/parachain/bitfield_signer_mock.hpp b/test/mock/core/parachain/bitfield_signer_mock.hpp new file mode 100644 index 0000000000..fe5214aaa5 --- /dev/null +++ b/test/mock/core/parachain/bitfield_signer_mock.hpp @@ -0,0 +1,33 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#include "parachain/availability/bitfield/signer.hpp" + +namespace kagome::parachain { + + class BitfieldSignerMock : public IBitfieldSigner { + public: + MOCK_METHOD(void, + start, + (), + (override)); + + MOCK_METHOD(outcome::result, + sign, + (const ValidatorSigner &, const Candidates &), + (override)); + + MOCK_METHOD(void, + setBroadcastCallback, + (BroadcastCallback &&), + (override)); + }; + +} // namespace kagome::parachain From c401eca1b7633e64e31d12fcd173b69e10a433b4 Mon Sep 17 00:00:00 2001 From: iceseer Date: Tue, 3 Dec 2024 11:39:15 +0300 Subject: [PATCH 11/27] [feature] PvfPrecheckMock Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 3 +++ .../mock/core/parachain/pvf_precheck_mock.hpp | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/mock/core/parachain/pvf_precheck_mock.hpp diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 96ddbe6b5a..ad6e57ceb0 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -16,6 +16,7 @@ #include "mock/core/network/peer_manager_mock.hpp" #include "mock/core/network/router_mock.hpp" #include "mock/core/crypto/sr25519_provider_mock.hpp" +#include "mock/core/parachain/pvf_precheck_mock.hpp" using namespace kagome::parachain; namespace runtime = kagome::runtime; @@ -44,6 +45,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { router_ = std::make_shared(); peer_view_ = std::make_shared(); bitfield_signer_ = std::make_shared(); + pvf_precheck_ = std::make_shared(); StartApp app_state_manager; @@ -96,6 +98,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::shared_ptr router_; std::shared_ptr peer_view_; std::shared_ptr bitfield_signer_; + std::shared_ptr pvf_precheck_; struct TestState { std::vector chain_ids; diff --git a/test/mock/core/parachain/pvf_precheck_mock.hpp b/test/mock/core/parachain/pvf_precheck_mock.hpp new file mode 100644 index 0000000000..294f69b820 --- /dev/null +++ b/test/mock/core/parachain/pvf_precheck_mock.hpp @@ -0,0 +1,23 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#include "parachain/pvf/precheck.hpp" + +namespace kagome::parachain { + + class PvfPrecheckMock : public IPvfPrecheck { + public: + MOCK_METHOD(void, + start, + (), + (override)); + }; + +} // namespace kagome::parachain From 91ddee8a30597db6a8eff5888c5419c49a35036c Mon Sep 17 00:00:00 2001 From: iceseer Date: Tue, 3 Dec 2024 11:53:06 +0300 Subject: [PATCH 12/27] [feature] pvf_mock Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 18 ++++++++++++----- test/mock/core/parachain/pvf_mock.hpp | 29 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 test/mock/core/parachain/pvf_mock.hpp diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index ad6e57ceb0..afac8c647b 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -17,6 +17,9 @@ #include "mock/core/network/router_mock.hpp" #include "mock/core/crypto/sr25519_provider_mock.hpp" #include "mock/core/parachain/pvf_precheck_mock.hpp" +#include "mock/core/parachain/pvf_mock.hpp" +#include "mock/core/parachain/bitfield_store_mock.hpp" +#include "mock/core/parachain/backing_store_mock.hpp" using namespace kagome::parachain; namespace runtime = kagome::runtime; @@ -31,7 +34,6 @@ using kagome::network::PeerManagerMock; using kagome::network::RouterMock; using kagome::crypto::Sr25519ProviderMock; - class BackingTest : public ProspectiveParachainsTestHarness { void SetUp() override { ProspectiveParachainsTestHarness::SetUp(); @@ -46,6 +48,9 @@ class BackingTest : public ProspectiveParachainsTestHarness { peer_view_ = std::make_shared(); bitfield_signer_ = std::make_shared(); pvf_precheck_ = std::make_shared(); + bitfield_store_ = std::make_shared(); + backing_store_ = std::make_shared(); + pvf_ = std::make_shared(); StartApp app_state_manager; @@ -60,10 +65,10 @@ class BackingTest : public ProspectiveParachainsTestHarness { peer_view_, *worker_thread_pool_, bitfield_signer_, - std::shared_ptr pvf_precheck, - std::shared_ptr bitfield_store, - std::shared_ptr backing_store, - std::shared_ptr pvf, + pvf_precheck_, + bitfield_store_, + backing_store_, + pvf_, std::shared_ptr av_store, std::shared_ptr parachain_host, std::shared_ptr signer_factory, @@ -99,6 +104,9 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::shared_ptr peer_view_; std::shared_ptr bitfield_signer_; std::shared_ptr pvf_precheck_; + std::shared_ptr bitfield_store_; + std::shared_ptr backing_store_; + std::shared_ptr pvf_; struct TestState { std::vector chain_ids; diff --git a/test/mock/core/parachain/pvf_mock.hpp b/test/mock/core/parachain/pvf_mock.hpp new file mode 100644 index 0000000000..3d903436e8 --- /dev/null +++ b/test/mock/core/parachain/pvf_mock.hpp @@ -0,0 +1,29 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#include "parachain/pvf/pvf.hpp" + +namespace kagome::parachain { + + class PvfMock : public Pvf { + public: + + MOCK_METHOD(void, + pvf, + (const CandidateReceipt &, const ParachainBlock &, const runtime::PersistedValidationData &, Cb), + (const, override)); + + MOCK_METHOD(void, + pvfValidate, + (const PersistedValidationData &, const ParachainBlock &, const CandidateReceipt &, const ParachainRuntime &, runtime::PvfExecTimeoutKind, Cb), + (const, override)); + }; + +} // namespace kagome::parachain From 89e97c4b2ae898b3513075d793576f2dd0bb77ae Mon Sep 17 00:00:00 2001 From: iceseer Date: Tue, 3 Dec 2024 12:46:48 +0300 Subject: [PATCH 13/27] [feature] signer_factory_mock Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 24 ++++++++++++---- .../core/parachain/signer_factory_mock.hpp | 28 +++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 test/mock/core/parachain/signer_factory_mock.hpp diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index afac8c647b..20d30e203d 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -19,7 +19,11 @@ #include "mock/core/parachain/pvf_precheck_mock.hpp" #include "mock/core/parachain/pvf_mock.hpp" #include "mock/core/parachain/bitfield_store_mock.hpp" +#include "mock/core/parachain/availability_store_mock.hpp" +#include "mock/core/parachain/signer_factory_mock.hpp" #include "mock/core/parachain/backing_store_mock.hpp" +#include "mock/core/runtime/parachain_host_mock.hpp" +#include "primitives/event_types.hpp" using namespace kagome::parachain; namespace runtime = kagome::runtime; @@ -33,6 +37,8 @@ using kagome::network::PeerViewMock; using kagome::network::PeerManagerMock; using kagome::network::RouterMock; using kagome::crypto::Sr25519ProviderMock; +using kagome::runtime::ParachainHostMock; +using kagome::primitives::events::ChainSubscriptionEngine; class BackingTest : public ProspectiveParachainsTestHarness { void SetUp() override { @@ -51,6 +57,10 @@ class BackingTest : public ProspectiveParachainsTestHarness { bitfield_store_ = std::make_shared(); backing_store_ = std::make_shared(); pvf_ = std::make_shared(); + av_store_ = std::make_shared(); + parachain_host_ = std::make_shared(); + signer_factory_ = std::make_shared(); + chain_sub_engine_ = std::make_shared(); StartApp app_state_manager; @@ -69,11 +79,11 @@ class BackingTest : public ProspectiveParachainsTestHarness { bitfield_store_, backing_store_, pvf_, - std::shared_ptr av_store, - std::shared_ptr parachain_host, - std::shared_ptr signer_factory, - const application::AppConfiguration &app_config, - application::AppStateManager &app_state_manager, + av_store_, + parachain_host_, + signer_factory_, + app_config_, + app_state_manager, primitives::events::ChainSubscriptionEnginePtr chain_sub_engine, primitives::events::SyncStateSubscriptionEnginePtr sync_state_observable, @@ -107,6 +117,10 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::shared_ptr bitfield_store_; std::shared_ptr backing_store_; std::shared_ptr pvf_; + std::shared_ptr av_store_; + std::shared_ptr parachain_host_; + std::shared_ptr signer_factory_; + std::shared_ptr chain_sub_engine_; struct TestState { std::vector chain_ids; diff --git a/test/mock/core/parachain/signer_factory_mock.hpp b/test/mock/core/parachain/signer_factory_mock.hpp new file mode 100644 index 0000000000..3b9dfaa8c0 --- /dev/null +++ b/test/mock/core/parachain/signer_factory_mock.hpp @@ -0,0 +1,28 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#include "parachain/validator/signer.hpp" + +namespace kagome::parachain { + + class ValidatorSignerFactoryMock : public IValidatorSignerFactory { + public: + MOCK_METHOD(outcome::result>, + at, + (const primitives::BlockHash &), + (override)); + + MOCK_METHOD(outcome::result>, + getAuthorityValidatorIndex, + (const primitives::BlockHash &), + (override)); + }; + +} // namespace kagome::parachain From 3eab5d0496c172af5dfcd4d8cd54ad2a836d4a8d Mon Sep 17 00:00:00 2001 From: iceseer Date: Tue, 3 Dec 2024 13:00:01 +0300 Subject: [PATCH 14/27] [feature] building Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 37 ++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 20d30e203d..04cf88fb11 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -23,6 +23,10 @@ #include "mock/core/parachain/signer_factory_mock.hpp" #include "mock/core/parachain/backing_store_mock.hpp" #include "mock/core/runtime/parachain_host_mock.hpp" +#include "mock/core/consensus/timeline/slots_util_mock.hpp" +#include "mock/core/consensus/babe/babe_config_repository_mock.hpp" +#include "mock/core/blockchain/block_tree_mock.hpp" +#include "mock/core/authority_discovery/query_mock.hpp" #include "primitives/event_types.hpp" using namespace kagome::parachain; @@ -39,6 +43,11 @@ using kagome::network::RouterMock; using kagome::crypto::Sr25519ProviderMock; using kagome::runtime::ParachainHostMock; using kagome::primitives::events::ChainSubscriptionEngine; +using kagome::primitives::events::SyncStateSubscriptionEnginePtr; +using kagome::primitives::events::SyncStateSubscriptionEngine; +using kagome::authority_discovery::QueryMock; +using kagome::blockchain::BlockTreeMock; +using kagome::consensus::SlotsUtilMock; class BackingTest : public ProspectiveParachainsTestHarness { void SetUp() override { @@ -61,6 +70,12 @@ class BackingTest : public ProspectiveParachainsTestHarness { parachain_host_ = std::make_shared(); signer_factory_ = std::make_shared(); chain_sub_engine_ = std::make_shared(); + sync_state_observable_ = std::make_shared(); + query_audi_ = std::make_shared(); + prospective_parachains_ = std::make_shared(); + block_tree_ = std::make_shared(); + slots_util_ = std::make_shared(); + babe_config_repo_ = std::make_shared(); StartApp app_state_manager; @@ -84,14 +99,14 @@ class BackingTest : public ProspectiveParachainsTestHarness { signer_factory_, app_config_, app_state_manager, - primitives::events::ChainSubscriptionEnginePtr chain_sub_engine, - primitives::events::SyncStateSubscriptionEnginePtr - sync_state_observable, - std::shared_ptr query_audi, - std::shared_ptr prospective_parachains, - std::shared_ptr block_tree, - LazySPtr slots_util, - std::shared_ptr babe_config_repo, + chain_sub_engine_, + sync_state_observable_, + query_audi_, + prospective_parachains_, + block_tree_, + slots_util_, + babe_config_repo_, + std::shared_ptr statement_distribution);*/ } @@ -121,6 +136,12 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::shared_ptr parachain_host_; std::shared_ptr signer_factory_; std::shared_ptr chain_sub_engine_; + SyncStateSubscriptionEnginePtr sync_state_observable_; + std::shared_ptr query_audi_; + std::shared_ptr prospective_parachains_; + std::shared_ptr block_tree_; + std::shared_ptr slots_util_; + std::shared_ptr babe_config_repo_; struct TestState { std::vector chain_ids; From 6aed23fc4e87998adfac3531ebe5a3c0ac040161 Mon Sep 17 00:00:00 2001 From: iceseer Date: Tue, 3 Dec 2024 13:27:13 +0300 Subject: [PATCH 15/27] bind Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 51 +++++++++++++++--- .../parachain/statement_distribution_mock.hpp | 53 +++++++++++++++++++ 2 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 test/mock/core/parachain/statement_distribution_mock.hpp diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 04cf88fb11..444d4a9a11 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include "core/parachain/parachain_test_harness.hpp" #include "parachain/validator/parachain_processor.hpp" #include "common/main_thread_pool.hpp" @@ -17,6 +18,7 @@ #include "mock/core/network/router_mock.hpp" #include "mock/core/crypto/sr25519_provider_mock.hpp" #include "mock/core/parachain/pvf_precheck_mock.hpp" +#include "mock/core/parachain/statement_distribution_mock.hpp" #include "mock/core/parachain/pvf_mock.hpp" #include "mock/core/parachain/bitfield_store_mock.hpp" #include "mock/core/parachain/availability_store_mock.hpp" @@ -28,6 +30,7 @@ #include "mock/core/blockchain/block_tree_mock.hpp" #include "mock/core/authority_discovery/query_mock.hpp" #include "primitives/event_types.hpp" +#include "injector/bind_by_lambda.hpp" using namespace kagome::parachain; namespace runtime = kagome::runtime; @@ -48,6 +51,7 @@ using kagome::primitives::events::SyncStateSubscriptionEngine; using kagome::authority_discovery::QueryMock; using kagome::blockchain::BlockTreeMock; using kagome::consensus::SlotsUtilMock; +using kagome::consensus::babe::BabeConfigRepositoryMock; class BackingTest : public ProspectiveParachainsTestHarness { void SetUp() override { @@ -76,12 +80,47 @@ class BackingTest : public ProspectiveParachainsTestHarness { block_tree_ = std::make_shared(); slots_util_ = std::make_shared(); babe_config_repo_ = std::make_shared(); + statement_distribution_ = std::make_shared(); - StartApp app_state_manager; + const auto injector = boost::di::make_injector( + kagome::injector::bind_by_lambda( + [](const auto &injector) { + return std::make_shared(std::chrono::milliseconds(1)); + }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(watchdog_, std::make_shared()); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(watchdog_, 1); }), + + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + //kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + //kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), + + di::bind.template to(), + //di::bind.to(42)[boost::di::override], + //di::bind.to(87.0), + //useBind() + ); - - /*ParachainProcessorImpl( + + StartApp app_state_manager; + /*parachain_processor_ = std::make_shared( peer_manager_, sr25519_provider_, router_, @@ -106,9 +145,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { block_tree_, slots_util_, babe_config_repo_, - - std::shared_ptr - statement_distribution);*/ + statement_distribution_);*/ } void TearDown() override { @@ -142,6 +179,8 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::shared_ptr block_tree_; std::shared_ptr slots_util_; std::shared_ptr babe_config_repo_; + std::shared_ptr statement_distribution_; + std::shared_ptr parachain_processor_; struct TestState { std::vector chain_ids; diff --git a/test/mock/core/parachain/statement_distribution_mock.hpp b/test/mock/core/parachain/statement_distribution_mock.hpp new file mode 100644 index 0000000000..4e9cfa7bdf --- /dev/null +++ b/test/mock/core/parachain/statement_distribution_mock.hpp @@ -0,0 +1,53 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#include "parachain/validator/statement_distribution/i_statement_distribution.hpp" + +namespace kagome::parachain::statement_distribution { + + class StatementDistributionMock : public IStatementDistribution { + public: + MOCK_METHOD(void, + OnFetchAttestedCandidateRequest, + (const network::vstaging::AttestedCandidateRequest &, std::shared_ptr), + (override)); + + MOCK_METHOD(void, + store_parachain_processor, + (std::weak_ptr), + (override)); + + MOCK_METHOD(void, + handle_incoming_manifest, + (const libp2p::peer::PeerId &, const network::vstaging::BackedCandidateManifest &), + (override)); + + MOCK_METHOD(void, + handle_incoming_acknowledgement, + (const libp2p::peer::PeerId &, const network::vstaging::BackedCandidateAcknowledgement &), + (override)); + + MOCK_METHOD(void, + handle_incoming_statement, + (const libp2p::peer::PeerId &, const network::vstaging::StatementDistributionMessageStatement &), + (override)); + + MOCK_METHOD(void, + handle_backed_candidate_message, + (const CandidateHash &), + (override)); + + MOCK_METHOD(void, + share_local_statement, + (const primitives::BlockHash &, const SignedFullStatementWithPVD &), + (override)); + }; + +} // namespace kagome::parachain::statement_distribution From 6a5a47631049fb303cc9a59cc52e5fae6a7833d3 Mon Sep 17 00:00:00 2001 From: iceseer Date: Tue, 3 Dec 2024 15:55:36 +0300 Subject: [PATCH 16/27] init Signed-off-by: iceseer --- test/core/parachain/CMakeLists.txt | 3 ++ test/core/parachain/backing.cpp | 62 +++++++++++------------------- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/test/core/parachain/CMakeLists.txt b/test/core/parachain/CMakeLists.txt index cf83a4eb66..0c44591743 100644 --- a/test/core/parachain/CMakeLists.txt +++ b/test/core/parachain/CMakeLists.txt @@ -35,6 +35,9 @@ addtest(backing_test ) target_link_libraries(backing_test + Boost::Boost.DI + Boost::boost + validator_parachain prospective_parachains log_configurator base_fs_test diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 444d4a9a11..4c6f24c598 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +//#include #include "core/parachain/parachain_test_harness.hpp" #include "parachain/validator/parachain_processor.hpp" #include "common/main_thread_pool.hpp" @@ -31,27 +31,38 @@ #include "mock/core/authority_discovery/query_mock.hpp" #include "primitives/event_types.hpp" #include "injector/bind_by_lambda.hpp" +#include "testutil/lazy.hpp" using namespace kagome::parachain; namespace runtime = kagome::runtime; using kagome::Watchdog; using kagome::application::AppConfigurationMock; +using kagome::application::AppConfiguration; using kagome::application::StartApp; using kagome::common::MainThreadPool; using kagome::common::WorkerThreadPool; using kagome::network::PeerViewMock; +using kagome::network::IPeerView; using kagome::network::PeerManagerMock; +using kagome::network::PeerManager; using kagome::network::RouterMock; +using kagome::network::Router; using kagome::crypto::Sr25519ProviderMock; +using kagome::crypto::Sr25519Provider; using kagome::runtime::ParachainHostMock; +using kagome::runtime::ParachainHost; using kagome::primitives::events::ChainSubscriptionEngine; using kagome::primitives::events::SyncStateSubscriptionEnginePtr; using kagome::primitives::events::SyncStateSubscriptionEngine; using kagome::authority_discovery::QueryMock; +using kagome::authority_discovery::Query; using kagome::blockchain::BlockTreeMock; +using kagome::blockchain::BlockTree; using kagome::consensus::SlotsUtilMock; +using kagome::consensus::SlotsUtil; using kagome::consensus::babe::BabeConfigRepositoryMock; +using kagome::consensus::babe::BabeConfigRepository; class BackingTest : public ProspectiveParachainsTestHarness { void SetUp() override { @@ -82,45 +93,14 @@ class BackingTest : public ProspectiveParachainsTestHarness { babe_config_repo_ = std::make_shared(); statement_distribution_ = std::make_shared(); - const auto injector = boost::di::make_injector( - kagome::injector::bind_by_lambda( - [](const auto &injector) { - return std::make_shared(std::chrono::milliseconds(1)); - }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(watchdog_, std::make_shared()); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(watchdog_, 1); }), - - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - //kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - //kagome::injector::bind_by_lambda([](const auto &injector) { return std::make_shared(); }), - - di::bind.template to(), - - //di::bind.to(42)[boost::di::override], - //di::bind.to(87.0), - //useBind() - ); + my_view_observable_ = std::make_shared(); + + EXPECT_CALL(*peer_view_, getMyViewObservable()) + .WillRepeatedly(Return(my_view_observable_)); StartApp app_state_manager; - /*parachain_processor_ = std::make_shared( + parachain_processor_ = std::make_shared( peer_manager_, sr25519_provider_, router_, @@ -143,9 +123,11 @@ class BackingTest : public ProspectiveParachainsTestHarness { query_audi_, prospective_parachains_, block_tree_, - slots_util_, + testutil::sptr_to_lazy(slots_util_), babe_config_repo_, - statement_distribution_);*/ + statement_distribution_); + + app_state_manager.start(); } void TearDown() override { @@ -182,6 +164,8 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::shared_ptr statement_distribution_; std::shared_ptr parachain_processor_; + PeerViewMock::MyViewSubscriptionEnginePtr my_view_observable_; + struct TestState { std::vector chain_ids; From 76992628c5cd38b768a3d6bed928b341a215f3d3 Mon Sep 17 00:00:00 2001 From: iceseer Date: Wed, 4 Dec 2024 11:02:18 +0300 Subject: [PATCH 17/27] tests Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 4c6f24c598..8a7dc78822 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -168,12 +168,16 @@ class BackingTest : public ProspectiveParachainsTestHarness { struct TestState { std::vector chain_ids; + std::unordered_map head_data; TestState() { const ParachainId chain_a(1); const ParachainId chain_b(2); chain_ids = {chain_a, chain_b}; + + head_data[chain_a] = {4, 5, 6}; + head_data[chain_b] = {5, 6, 7}; } }; @@ -266,7 +270,63 @@ class BackingTest : public ProspectiveParachainsTestHarness { requested_len += 1; } + + chain_sub_engine_->notify(kagome::primitives::events::ChainEventType::kNewHeads, update.new_head); + } + + runtime::PersistedValidationData dummy_pvd() { + return runtime::PersistedValidationData{ + .parent_head = {7, 8, 9}, + .relay_parent_number = 0, + .relay_parent_storage_root = fromNumber(0), + .max_pov_size = 1024, + }; + } + + template + inline Hash hash_of(T &&t) { + return hasher_->blake2b_256(scale::encode(std::forward(t)).value()); + } + + template + static Hash hash_of(const kagome::crypto::HasherImpl &hasher, T &&t) { + return hasher.blake2b_256(scale::encode(std::forward(t)).value()); } + + struct TestCandidateBuilder { + ParachainId para_id; + HeadData head_data; + Hash pov_hash; + Hash relay_parent; + Hash erasure_root; + Hash persisted_validation_data_hash; + std::vector validation_code; + + network::CommittedCandidateReceipt build(const kagome::crypto::HasherImpl &hasher) { + return network::CommittedCandidateReceipt { + .descriptor = network::CandidateDescriptor { + .para_id = para_id, + .relay_parent = relay_parent, + .collator_id = {}, + .persisted_data_hash = persisted_validation_data_hash, + .pov_hash = pov_hash, + .erasure_encoding_root = erasure_root, + .signature = {}, + .para_head_hash = hash_of(hasher, head_data), + .validation_code_hash = hash_of(hasher, kagome::runtime::ValidationCode(validation_code)), + }, + .commitments = network::CandidateCommitments { + .upward_msgs = {}, + .outbound_hor_msgs = {}, + .opt_para_runtime = std::nullopt, + .para_head = head_data, + .downward_msgs_count = 0, + .watermark = 0, + }, + }; + } + }; + }; TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { @@ -301,4 +361,25 @@ TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { activate_leaf(test_leaf_a, test_state); activate_leaf(test_leaf_b, test_state); + + kagome::network::PoV pov{ + .payload = {42, 43, 44} + }; + const auto pvd = dummy_pvd(); + kagome::runtime::ValidationCode validation_code = {1, 2, 3}; + + const auto &expected_head_data = test_state.head_data[para_id]; + const auto pov_hash = hash_of(pov); + + //let candidate = TestCandidateBuilder { + // para_id, + // relay_parent: leaf_a_parent, + // pov_hash, + // head_data: expected_head_data.clone(), + // erasure_root: make_erasure_root(&test_state, pov.clone(), pvd.clone()), + // persisted_validation_data_hash: pvd.hash(), + // validation_code: validation_code.0.clone(), + //} + //.build(); + } From 2bbd6e4e8005c498696759bd0f98c4b9112be596 Mon Sep 17 00:00:00 2001 From: iceseer Date: Wed, 4 Dec 2024 11:55:57 +0300 Subject: [PATCH 18/27] tests Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 61 ++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 8a7dc78822..a441e0b6f0 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -32,6 +32,11 @@ #include "primitives/event_types.hpp" #include "injector/bind_by_lambda.hpp" #include "testutil/lazy.hpp" +#include "parachain/availability/chunks.hpp" +#include "parachain/availability/proof.hpp" +#include "crypto/bip39/impl/bip39_provider_impl.hpp" +#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/pbkdf2/impl/pbkdf2_provider_impl.hpp" using namespace kagome::parachain; namespace runtime = kagome::runtime; @@ -169,6 +174,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { struct TestState { std::vector chain_ids; std::unordered_map head_data; + std::vector validators; TestState() { const ParachainId chain_a(1); @@ -178,6 +184,27 @@ class BackingTest : public ProspectiveParachainsTestHarness { head_data[chain_a] = {4, 5, 6}; head_data[chain_b] = {5, 6, 7}; + + crypto::Bip39ProviderImpl bip_provider{ + std::make_shared(), + std::make_shared(), + }; + + auto sr25519_provider = std::make_shared(); + auto f = [&](std::string_view phrase) { + auto bip = bip_provider.generateSeed(phrase).value(); + auto keys = + sr25519_provider + ->generateKeypair(crypto::Sr25519Seed::from(bip.seed), bip.junctions) + .value(); + return keys.public_key; + }; + validators.emplace_back(f("//Alice")); + validators.emplace_back(f("//Bob")); + validators.emplace_back(f("//Charlie")); + validators.emplace_back(f("//Dave")); + validators.emplace_back(f("//Ferdie")); + validators.emplace_back(f("//One")); } }; @@ -289,10 +316,21 @@ class BackingTest : public ProspectiveParachainsTestHarness { } template - static Hash hash_of(const kagome::crypto::HasherImpl &hasher, T &&t) { + static Hash hash_of(const kagome::crypto::Hasher &hasher, T &&t) { return hasher.blake2b_256(scale::encode(std::forward(t)).value()); } + Hash make_erasure_root(const TestState &test, const network::PoV &pov, const runtime::PersistedValidationData &validation_data) { + const runtime::AvailableData available_data { + .pov = pov, + .validation_data = validation_data, + }; + + auto chunks = toChunks(test.validators.size(), available_data).value(); + return makeTrieProof(chunks); + } + + struct TestCandidateBuilder { ParachainId para_id; HeadData head_data; @@ -302,7 +340,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { Hash persisted_validation_data_hash; std::vector validation_code; - network::CommittedCandidateReceipt build(const kagome::crypto::HasherImpl &hasher) { + network::CommittedCandidateReceipt build(const kagome::crypto::Hasher &hasher) { return network::CommittedCandidateReceipt { .descriptor = network::CandidateDescriptor { .para_id = para_id, @@ -371,15 +409,14 @@ TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { const auto &expected_head_data = test_state.head_data[para_id]; const auto pov_hash = hash_of(pov); - //let candidate = TestCandidateBuilder { - // para_id, - // relay_parent: leaf_a_parent, - // pov_hash, - // head_data: expected_head_data.clone(), - // erasure_root: make_erasure_root(&test_state, pov.clone(), pvd.clone()), - // persisted_validation_data_hash: pvd.hash(), - // validation_code: validation_code.0.clone(), - //} - //.build(); + const auto candidate = TestCandidateBuilder{ + .para_id = para_id, + .head_data = expected_head_data, + .pov_hash = pov_hash, + .relay_parent = leaf_a_parent, + .erasure_root = make_erasure_root(test_state, pov, pvd), + .persisted_validation_data_hash = hash_of(pvd), + .validation_code = validation_code, + }.build(*hasher_); } From f78611aee2fcbd30399d71d738a364949f3ce7c8 Mon Sep 17 00:00:00 2001 From: iceseer Date: Wed, 4 Dec 2024 11:56:54 +0300 Subject: [PATCH 19/27] formatting Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 224 +++++++++--------- test/mock/core/network/peer_view_mock.hpp | 15 +- .../core/parachain/bitfield_signer_mock.hpp | 10 +- .../parachain/prospective_parachains_mock.hpp | 9 +- test/mock/core/parachain/pvf_mock.hpp | 13 +- .../mock/core/parachain/pvf_precheck_mock.hpp | 5 +- .../parachain/statement_distribution_mock.hpp | 22 +- 7 files changed, 151 insertions(+), 147 deletions(-) diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index a441e0b6f0..ed39ad971c 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -4,70 +4,70 @@ * SPDX-License-Identifier: Apache-2.0 */ -//#include -#include "core/parachain/parachain_test_harness.hpp" -#include "parachain/validator/parachain_processor.hpp" +// #include #include "common/main_thread_pool.hpp" #include "common/worker_thread_pool.hpp" -#include "mock/core/parachain/prospective_parachains_mock.hpp" -#include "mock/core/parachain/bitfield_signer_mock.hpp" +#include "core/parachain/parachain_test_harness.hpp" +#include "crypto/bip39/impl/bip39_provider_impl.hpp" +#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/pbkdf2/impl/pbkdf2_provider_impl.hpp" +#include "injector/bind_by_lambda.hpp" #include "mock/core/application/app_configuration_mock.hpp" #include "mock/core/application/app_state_manager_mock.hpp" -#include "mock/core/network/peer_view_mock.hpp" +#include "mock/core/authority_discovery/query_mock.hpp" +#include "mock/core/blockchain/block_tree_mock.hpp" +#include "mock/core/consensus/babe/babe_config_repository_mock.hpp" +#include "mock/core/consensus/timeline/slots_util_mock.hpp" +#include "mock/core/crypto/sr25519_provider_mock.hpp" #include "mock/core/network/peer_manager_mock.hpp" +#include "mock/core/network/peer_view_mock.hpp" #include "mock/core/network/router_mock.hpp" -#include "mock/core/crypto/sr25519_provider_mock.hpp" -#include "mock/core/parachain/pvf_precheck_mock.hpp" -#include "mock/core/parachain/statement_distribution_mock.hpp" -#include "mock/core/parachain/pvf_mock.hpp" -#include "mock/core/parachain/bitfield_store_mock.hpp" #include "mock/core/parachain/availability_store_mock.hpp" -#include "mock/core/parachain/signer_factory_mock.hpp" #include "mock/core/parachain/backing_store_mock.hpp" +#include "mock/core/parachain/bitfield_signer_mock.hpp" +#include "mock/core/parachain/bitfield_store_mock.hpp" +#include "mock/core/parachain/prospective_parachains_mock.hpp" +#include "mock/core/parachain/pvf_mock.hpp" +#include "mock/core/parachain/pvf_precheck_mock.hpp" +#include "mock/core/parachain/signer_factory_mock.hpp" +#include "mock/core/parachain/statement_distribution_mock.hpp" #include "mock/core/runtime/parachain_host_mock.hpp" -#include "mock/core/consensus/timeline/slots_util_mock.hpp" -#include "mock/core/consensus/babe/babe_config_repository_mock.hpp" -#include "mock/core/blockchain/block_tree_mock.hpp" -#include "mock/core/authority_discovery/query_mock.hpp" -#include "primitives/event_types.hpp" -#include "injector/bind_by_lambda.hpp" -#include "testutil/lazy.hpp" #include "parachain/availability/chunks.hpp" #include "parachain/availability/proof.hpp" -#include "crypto/bip39/impl/bip39_provider_impl.hpp" -#include "crypto/hasher/hasher_impl.hpp" -#include "crypto/pbkdf2/impl/pbkdf2_provider_impl.hpp" +#include "parachain/validator/parachain_processor.hpp" +#include "primitives/event_types.hpp" +#include "testutil/lazy.hpp" using namespace kagome::parachain; namespace runtime = kagome::runtime; using kagome::Watchdog; -using kagome::application::AppConfigurationMock; using kagome::application::AppConfiguration; +using kagome::application::AppConfigurationMock; using kagome::application::StartApp; +using kagome::authority_discovery::Query; +using kagome::authority_discovery::QueryMock; +using kagome::blockchain::BlockTree; +using kagome::blockchain::BlockTreeMock; using kagome::common::MainThreadPool; using kagome::common::WorkerThreadPool; -using kagome::network::PeerViewMock; +using kagome::consensus::SlotsUtil; +using kagome::consensus::SlotsUtilMock; +using kagome::consensus::babe::BabeConfigRepository; +using kagome::consensus::babe::BabeConfigRepositoryMock; +using kagome::crypto::Sr25519Provider; +using kagome::crypto::Sr25519ProviderMock; using kagome::network::IPeerView; -using kagome::network::PeerManagerMock; using kagome::network::PeerManager; -using kagome::network::RouterMock; +using kagome::network::PeerManagerMock; +using kagome::network::PeerViewMock; using kagome::network::Router; -using kagome::crypto::Sr25519ProviderMock; -using kagome::crypto::Sr25519Provider; -using kagome::runtime::ParachainHostMock; -using kagome::runtime::ParachainHost; +using kagome::network::RouterMock; using kagome::primitives::events::ChainSubscriptionEngine; -using kagome::primitives::events::SyncStateSubscriptionEnginePtr; using kagome::primitives::events::SyncStateSubscriptionEngine; -using kagome::authority_discovery::QueryMock; -using kagome::authority_discovery::Query; -using kagome::blockchain::BlockTreeMock; -using kagome::blockchain::BlockTree; -using kagome::consensus::SlotsUtilMock; -using kagome::consensus::SlotsUtil; -using kagome::consensus::babe::BabeConfigRepositoryMock; -using kagome::consensus::babe::BabeConfigRepository; +using kagome::primitives::events::SyncStateSubscriptionEnginePtr; +using kagome::runtime::ParachainHost; +using kagome::runtime::ParachainHostMock; class BackingTest : public ProspectiveParachainsTestHarness { void SetUp() override { @@ -75,7 +75,8 @@ class BackingTest : public ProspectiveParachainsTestHarness { parachain_api_ = std::make_shared(); watchdog_ = std::make_shared(std::chrono::milliseconds(1)); - main_thread_pool_ = std::make_shared(watchdog_, std::make_shared()); + main_thread_pool_ = std::make_shared( + watchdog_, std::make_shared()); worker_thread_pool_ = std::make_shared(watchdog_, 1); peer_manager_ = std::make_shared(); sr25519_provider_ = std::make_shared(); @@ -96,13 +97,14 @@ class BackingTest : public ProspectiveParachainsTestHarness { block_tree_ = std::make_shared(); slots_util_ = std::make_shared(); babe_config_repo_ = std::make_shared(); - statement_distribution_ = std::make_shared(); + statement_distribution_ = + std::make_shared(); - my_view_observable_ = std::make_shared(); - - EXPECT_CALL(*peer_view_, getMyViewObservable()) - .WillRepeatedly(Return(my_view_observable_)); + my_view_observable_ = + std::make_shared(); + EXPECT_CALL(*peer_view_, getMyViewObservable()) + .WillRepeatedly(Return(my_view_observable_)); StartApp app_state_manager; parachain_processor_ = std::make_shared( @@ -132,7 +134,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { babe_config_repo_, statement_distribution_); - app_state_manager.start(); + app_state_manager.start(); } void TearDown() override { @@ -166,15 +168,16 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::shared_ptr block_tree_; std::shared_ptr slots_util_; std::shared_ptr babe_config_repo_; - std::shared_ptr statement_distribution_; + std::shared_ptr + statement_distribution_; std::shared_ptr parachain_processor_; PeerViewMock::MyViewSubscriptionEnginePtr my_view_observable_; struct TestState { std::vector chain_ids; - std::unordered_map head_data; - std::vector validators; + std::unordered_map head_data; + std::vector validators; TestState() { const ParachainId chain_a(1); @@ -193,10 +196,10 @@ class BackingTest : public ProspectiveParachainsTestHarness { auto sr25519_provider = std::make_shared(); auto f = [&](std::string_view phrase) { auto bip = bip_provider.generateSeed(phrase).value(); - auto keys = - sr25519_provider - ->generateKeypair(crypto::Sr25519Seed::from(bip.seed), bip.junctions) - .value(); + auto keys = sr25519_provider + ->generateKeypair(crypto::Sr25519Seed::from(bip.seed), + bip.junctions) + .value(); return keys.public_key; }; validators.emplace_back(f("//Alice")); @@ -286,19 +289,20 @@ class BackingTest : public ProspectiveParachainsTestHarness { })); if (requested_len == 0) { - //assert_matches !( - // virtual_overseer.recv().await, - // AllMessages::ProspectiveParachains( - // ProspectiveParachainsMessage::GetMinimumRelayParents( - // parent, tx)) if parent - // == leaf_hash = - // > { tx.send(min_relay_parents.clone()).unwrap(); }); + // assert_matches !( + // virtual_overseer.recv().await, + // AllMessages::ProspectiveParachains( + // ProspectiveParachainsMessage::GetMinimumRelayParents( + // parent, tx)) if parent + // == leaf_hash = + // > { tx.send(min_relay_parents.clone()).unwrap(); }); } requested_len += 1; } - chain_sub_engine_->notify(kagome::primitives::events::ChainEventType::kNewHeads, update.new_head); + chain_sub_engine_->notify( + kagome::primitives::events::ChainEventType::kNewHeads, update.new_head); } runtime::PersistedValidationData dummy_pvd() { @@ -310,27 +314,29 @@ class BackingTest : public ProspectiveParachainsTestHarness { }; } - template + template inline Hash hash_of(T &&t) { return hasher_->blake2b_256(scale::encode(std::forward(t)).value()); } - template + template static Hash hash_of(const kagome::crypto::Hasher &hasher, T &&t) { return hasher.blake2b_256(scale::encode(std::forward(t)).value()); } - Hash make_erasure_root(const TestState &test, const network::PoV &pov, const runtime::PersistedValidationData &validation_data) { - const runtime::AvailableData available_data { - .pov = pov, - .validation_data = validation_data, + Hash make_erasure_root( + const TestState &test, + const network::PoV &pov, + const runtime::PersistedValidationData &validation_data) { + const runtime::AvailableData available_data{ + .pov = pov, + .validation_data = validation_data, }; auto chunks = toChunks(test.validators.size(), available_data).value(); return makeTrieProof(chunks); } - struct TestCandidateBuilder { ParachainId para_id; HeadData head_data; @@ -340,31 +346,34 @@ class BackingTest : public ProspectiveParachainsTestHarness { Hash persisted_validation_data_hash; std::vector validation_code; - network::CommittedCandidateReceipt build(const kagome::crypto::Hasher &hasher) { - return network::CommittedCandidateReceipt { - .descriptor = network::CandidateDescriptor { - .para_id = para_id, - .relay_parent = relay_parent, - .collator_id = {}, - .persisted_data_hash = persisted_validation_data_hash, - .pov_hash = pov_hash, - .erasure_encoding_root = erasure_root, - .signature = {}, - .para_head_hash = hash_of(hasher, head_data), - .validation_code_hash = hash_of(hasher, kagome::runtime::ValidationCode(validation_code)), - }, - .commitments = network::CandidateCommitments { - .upward_msgs = {}, - .outbound_hor_msgs = {}, - .opt_para_runtime = std::nullopt, - .para_head = head_data, - .downward_msgs_count = 0, - .watermark = 0, - }, + network::CommittedCandidateReceipt build( + const kagome::crypto::Hasher &hasher) { + return network::CommittedCandidateReceipt{ + .descriptor = + network::CandidateDescriptor{ + .para_id = para_id, + .relay_parent = relay_parent, + .collator_id = {}, + .persisted_data_hash = persisted_validation_data_hash, + .pov_hash = pov_hash, + .erasure_encoding_root = erasure_root, + .signature = {}, + .para_head_hash = hash_of(hasher, head_data), + .validation_code_hash = hash_of( + hasher, kagome::runtime::ValidationCode(validation_code)), + }, + .commitments = + network::CandidateCommitments{ + .upward_msgs = {}, + .outbound_hor_msgs = {}, + .opt_para_runtime = std::nullopt, + .para_head = head_data, + .downward_msgs_count = 0, + .watermark = 0, + }, }; } }; - }; TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { @@ -400,23 +409,22 @@ TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { activate_leaf(test_leaf_a, test_state); activate_leaf(test_leaf_b, test_state); - kagome::network::PoV pov{ - .payload = {42, 43, 44} - }; - const auto pvd = dummy_pvd(); - kagome::runtime::ValidationCode validation_code = {1, 2, 3}; - - const auto &expected_head_data = test_state.head_data[para_id]; - const auto pov_hash = hash_of(pov); - - const auto candidate = TestCandidateBuilder{ - .para_id = para_id, - .head_data = expected_head_data, - .pov_hash = pov_hash, - .relay_parent = leaf_a_parent, - .erasure_root = make_erasure_root(test_state, pov, pvd), - .persisted_validation_data_hash = hash_of(pvd), - .validation_code = validation_code, - }.build(*hasher_); + kagome::network::PoV pov{.payload = {42, 43, 44}}; + const auto pvd = dummy_pvd(); + kagome::runtime::ValidationCode validation_code = {1, 2, 3}; + + const auto &expected_head_data = test_state.head_data[para_id]; + const auto pov_hash = hash_of(pov); + const auto candidate = + TestCandidateBuilder{ + .para_id = para_id, + .head_data = expected_head_data, + .pov_hash = pov_hash, + .relay_parent = leaf_a_parent, + .erasure_root = make_erasure_root(test_state, pov, pvd), + .persisted_validation_data_hash = hash_of(pvd), + .validation_code = validation_code, + } + .build(*hasher_); } diff --git a/test/mock/core/network/peer_view_mock.hpp b/test/mock/core/network/peer_view_mock.hpp index 50bee5b548..692e18fcb9 100644 --- a/test/mock/core/network/peer_view_mock.hpp +++ b/test/mock/core/network/peer_view_mock.hpp @@ -13,10 +13,7 @@ namespace kagome::network { class PeerViewMock : public IPeerView { public: - MOCK_METHOD(size_t, - peersCount, - (), - (const, override)); + MOCK_METHOD(size_t, peersCount, (), (const, override)); MOCK_METHOD(MyViewSubscriptionEnginePtr, getMyViewObservable, @@ -28,19 +25,13 @@ namespace kagome::network { (), (override)); - MOCK_METHOD(void, - removePeer, - (const PeerId &), - (override)); + MOCK_METHOD(void, removePeer, (const PeerId &), (override)); MOCK_METHOD(void, updateRemoteView, (const PeerId &, network::View &&), (override)); - MOCK_METHOD(const View &, - getMyView, - (), - (const, override)); + MOCK_METHOD(const View &, getMyView, (), (const, override)); }; } // namespace kagome::network diff --git a/test/mock/core/parachain/bitfield_signer_mock.hpp b/test/mock/core/parachain/bitfield_signer_mock.hpp index fe5214aaa5..458f056dfa 100644 --- a/test/mock/core/parachain/bitfield_signer_mock.hpp +++ b/test/mock/core/parachain/bitfield_signer_mock.hpp @@ -14,20 +14,14 @@ namespace kagome::parachain { class BitfieldSignerMock : public IBitfieldSigner { public: - MOCK_METHOD(void, - start, - (), - (override)); + MOCK_METHOD(void, start, (), (override)); MOCK_METHOD(outcome::result, sign, (const ValidatorSigner &, const Candidates &), (override)); - MOCK_METHOD(void, - setBroadcastCallback, - (BroadcastCallback &&), - (override)); + MOCK_METHOD(void, setBroadcastCallback, (BroadcastCallback &&), (override)); }; } // namespace kagome::parachain diff --git a/test/mock/core/parachain/prospective_parachains_mock.hpp b/test/mock/core/parachain/prospective_parachains_mock.hpp index aaba126cc2..42ff2c9927 100644 --- a/test/mock/core/parachain/prospective_parachains_mock.hpp +++ b/test/mock/core/parachain/prospective_parachains_mock.hpp @@ -74,11 +74,12 @@ namespace kagome::parachain { } // namespace kagome::parachain namespace boost { - inline auto &operator<<(std::ostream &s, const kagome::parachain::ParentHeadData &) { + inline auto &operator<<(std::ostream &s, + const kagome::parachain::ParentHeadData &) { return s; } - inline auto &operator<<(std::ostream &s, const kagome::parachain::HypotheticalCandidate &) { + inline auto &operator<<(std::ostream &s, + const kagome::parachain::HypotheticalCandidate &) { return s; } -} // kagome::parachain - +} // namespace boost diff --git a/test/mock/core/parachain/pvf_mock.hpp b/test/mock/core/parachain/pvf_mock.hpp index 3d903436e8..5a9f7c0149 100644 --- a/test/mock/core/parachain/pvf_mock.hpp +++ b/test/mock/core/parachain/pvf_mock.hpp @@ -14,15 +14,22 @@ namespace kagome::parachain { class PvfMock : public Pvf { public: - MOCK_METHOD(void, pvf, - (const CandidateReceipt &, const ParachainBlock &, const runtime::PersistedValidationData &, Cb), + (const CandidateReceipt &, + const ParachainBlock &, + const runtime::PersistedValidationData &, + Cb), (const, override)); MOCK_METHOD(void, pvfValidate, - (const PersistedValidationData &, const ParachainBlock &, const CandidateReceipt &, const ParachainRuntime &, runtime::PvfExecTimeoutKind, Cb), + (const PersistedValidationData &, + const ParachainBlock &, + const CandidateReceipt &, + const ParachainRuntime &, + runtime::PvfExecTimeoutKind, + Cb), (const, override)); }; diff --git a/test/mock/core/parachain/pvf_precheck_mock.hpp b/test/mock/core/parachain/pvf_precheck_mock.hpp index 294f69b820..e6b4236c0a 100644 --- a/test/mock/core/parachain/pvf_precheck_mock.hpp +++ b/test/mock/core/parachain/pvf_precheck_mock.hpp @@ -14,10 +14,7 @@ namespace kagome::parachain { class PvfPrecheckMock : public IPvfPrecheck { public: - MOCK_METHOD(void, - start, - (), - (override)); + MOCK_METHOD(void, start, (), (override)); }; } // namespace kagome::parachain diff --git a/test/mock/core/parachain/statement_distribution_mock.hpp b/test/mock/core/parachain/statement_distribution_mock.hpp index 4e9cfa7bdf..8494786f05 100644 --- a/test/mock/core/parachain/statement_distribution_mock.hpp +++ b/test/mock/core/parachain/statement_distribution_mock.hpp @@ -16,7 +16,8 @@ namespace kagome::parachain::statement_distribution { public: MOCK_METHOD(void, OnFetchAttestedCandidateRequest, - (const network::vstaging::AttestedCandidateRequest &, std::shared_ptr), + (const network::vstaging::AttestedCandidateRequest &, + std::shared_ptr), (override)); MOCK_METHOD(void, @@ -26,18 +27,22 @@ namespace kagome::parachain::statement_distribution { MOCK_METHOD(void, handle_incoming_manifest, - (const libp2p::peer::PeerId &, const network::vstaging::BackedCandidateManifest &), + (const libp2p::peer::PeerId &, + const network::vstaging::BackedCandidateManifest &), (override)); MOCK_METHOD(void, handle_incoming_acknowledgement, - (const libp2p::peer::PeerId &, const network::vstaging::BackedCandidateAcknowledgement &), + (const libp2p::peer::PeerId &, + const network::vstaging::BackedCandidateAcknowledgement &), (override)); - MOCK_METHOD(void, - handle_incoming_statement, - (const libp2p::peer::PeerId &, const network::vstaging::StatementDistributionMessageStatement &), - (override)); + MOCK_METHOD( + void, + handle_incoming_statement, + (const libp2p::peer::PeerId &, + const network::vstaging::StatementDistributionMessageStatement &), + (override)); MOCK_METHOD(void, handle_backed_candidate_message, @@ -46,7 +51,8 @@ namespace kagome::parachain::statement_distribution { MOCK_METHOD(void, share_local_statement, - (const primitives::BlockHash &, const SignedFullStatementWithPVD &), + (const primitives::BlockHash &, + const SignedFullStatementWithPVD &), (override)); }; From 87db410f491257bb9e34e3960961e4814b28ff18 Mon Sep 17 00:00:00 2001 From: iceseer Date: Wed, 4 Dec 2024 17:14:27 +0300 Subject: [PATCH 20/27] tests Signed-off-by: iceseer --- .../validator/parachain_processor.hpp | 12 +++ test/core/parachain/backing.cpp | 84 +++++++++++++++++-- 2 files changed, 91 insertions(+), 5 deletions(-) diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index 2d5502df4f..e3aa053b7b 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -821,6 +821,18 @@ namespace kagome::parachain { metrics::RegistryPtr metrics_registry_ = metrics::createRegistry(); metrics::Gauge *metric_is_parachain_validator_; + + public: + void handle_second_message(const network::CandidateReceipt &candidate, + const network::ParachainBlock &pov, + const runtime::PersistedValidationData &pvd, + const primitives::BlockHash &relay_parent) { + validateAsync( + candidate, + network::ParachainBlock(pov), + runtime::PersistedValidationData(pvd), + relay_parent); + } }; } // namespace kagome::parachain diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index ed39ad971c..0330ddc8b0 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -103,9 +103,6 @@ class BackingTest : public ProspectiveParachainsTestHarness { my_view_observable_ = std::make_shared(); - EXPECT_CALL(*peer_view_, getMyViewObservable()) - .WillRepeatedly(Return(my_view_observable_)); - StartApp app_state_manager; parachain_processor_ = std::make_shared( peer_manager_, @@ -134,6 +131,13 @@ class BackingTest : public ProspectiveParachainsTestHarness { babe_config_repo_, statement_distribution_); + EXPECT_CALL(*peer_view_, getMyViewObservable()) + .WillRepeatedly(Return(my_view_observable_)); + EXPECT_CALL(*statement_distribution_, store_parachain_processor(testing::_)).Times(1); + EXPECT_CALL(*bitfield_signer_, setBroadcastCallback(testing::_)).Times(1); + EXPECT_CALL(app_config_, roles()) + .WillRepeatedly(Return(network::Roles(0xff))); + app_state_manager.start(); } @@ -178,6 +182,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::vector chain_ids; std::unordered_map head_data; std::vector validators; + //std::vector validator_public: , TestState() { const ParachainId chain_a(1); @@ -238,6 +243,20 @@ class BackingTest : public ProspectiveParachainsTestHarness { .WillRepeatedly(Return(outcome::success(fragment::AsyncBackingParams{ .max_candidate_depth = 4, .allowed_ancestry_len = 3}))); + EXPECT_CALL(*prospective_parachains_, prospectiveParachainsMode(leaf_hash)) + .WillRepeatedly(Return(ProspectiveParachainsMode { + .max_candidate_depth = 4, + .allowed_ancestry_len = 3, + })); + + +// network::ExViewRef ev_ref{ +// .new_head = {update.new_head}, +// .lost = update.lost, +// }; + EXPECT_CALL(*prospective_parachains_, onActiveLeavesUpdate(testing::_)) + .WillRepeatedly(Return(outcome::success())); + const BlockNumber min_min = [&]() -> BlockNumber { std::optional min_min; for (const auto &[_, block_num] : min_relay_parents) { @@ -288,6 +307,10 @@ class BackingTest : public ProspectiveParachainsTestHarness { .hash_opt = {}, })); + EXPECT_CALL(*parachain_api_, validators(hash)) + .WillRepeatedly(Return(test_state.validators)); + + if (requested_len == 0) { // assert_matches !( // virtual_overseer.recv().await, @@ -301,8 +324,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { requested_len += 1; } - chain_sub_engine_->notify( - kagome::primitives::events::ChainEventType::kNewHeads, update.new_head); + my_view_observable_->notify(PeerViewMock::EventType::kViewUpdated, update); } runtime::PersistedValidationData dummy_pvd() { @@ -337,6 +359,35 @@ class BackingTest : public ProspectiveParachainsTestHarness { return makeTrieProof(chunks); } + void assert_validation_requests(const runtime::ValidationCode &validation_code) { + } + + void assert_validate_seconded_candidate( + const Hash &relay_parent, + const network::CommittedCandidateReceipt &candidate, + const network::PoV &assert_pov, + const runtime::PersistedValidationData &assert_pvd, + const runtime::ValidationCode &assert_validation_code, + const HeadData &expected_head_data, + bool fetch_pov) { + assert_validation_requests(assert_validation_code); + + if (fetch_pov) { + //assert_matches!( + // virtual_overseer.recv().await, + // AllMessages::AvailabilityDistribution( + // AvailabilityDistributionMessage::FetchPoV { + // relay_parent: hash, + // tx, + // .. + // } + // ) if hash == relay_parent => { + // tx.send(assert_pov.clone()).unwrap(); + // } + //); + }// + } + struct TestCandidateBuilder { ParachainId para_id; HeadData head_data; @@ -406,6 +457,10 @@ TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { LEAF_B_BLOCK_NUMBER - LEAF_B_ANCESTRY_LEN}}, }; + sync_state_observable_->notify( + kagome::primitives::events::SyncStateEventType::kSyncState, + kagome::primitives::events::SyncStateEventParams::SYNCHRONIZED); + activate_leaf(test_leaf_a, test_state); activate_leaf(test_leaf_b, test_state); @@ -427,4 +482,23 @@ TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { .validation_code = validation_code, } .build(*hasher_); + + std::cout << fmt::format("======> {}", candidate.descriptor.erasure_encoding_root) << std::endl; + parachain_processor_->handle_second_message( + candidate.to_plain(*hasher_), + pov, + pvd, + leaf_a_hash + ); + + assert_validate_seconded_candidate( + leaf_a_parent, + candidate, + pov, + pvd, + validation_code, + expected_head_data, + false + ); + } From 8a86690b918966d4e4eee0e6b5c144c75865228a Mon Sep 17 00:00:00 2001 From: iceseer Date: Thu, 5 Dec 2024 11:24:19 +0300 Subject: [PATCH 21/27] tests Signed-off-by: iceseer --- core/parachain/types.hpp | 2 +- test/core/parachain/backing.cpp | 120 ++++++++++++++++++++++++++++++-- 2 files changed, 115 insertions(+), 7 deletions(-) diff --git a/core/parachain/types.hpp b/core/parachain/types.hpp index eb7e7cb530..4f8473dc11 100644 --- a/core/parachain/types.hpp +++ b/core/parachain/types.hpp @@ -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. diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 0330ddc8b0..3c7f2163ca 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -69,10 +69,93 @@ using kagome::primitives::events::SyncStateSubscriptionEnginePtr; using kagome::runtime::ParachainHost; using kagome::runtime::ParachainHostMock; +static std::vector read_binary_file (const std::string filename) +{ + // binary mode is only for switching off newline translation + std::ifstream file(filename, std::ios::binary); + file.unsetf(std::ios::skipws); + + std::streampos file_size; + file.seekg(0, std::ios::end); + file_size = file.tellg(); + file.seekg(0, std::ios::beg); + + std::vector vec; + vec.reserve(file_size); + vec.insert(vec.begin(), + std::istream_iterator(file), + std::istream_iterator()); + return (vec); +} + + struct CCC { + SCALE_TIE(14); + enum class Error { + DISALLOWED_HRMP_WATERMARK, + NO_SUCH_HRMP_CHANNEL, + HRMP_BYTES_OVERFLOW, + HRMP_MESSAGE_OVERFLOW, + UMP_MESSAGE_OVERFLOW, + UMP_BYTES_OVERFLOW, + DMP_MESSAGE_UNDERFLOW, + APPLIED_NONEXISTENT_CODE_UPGRADE, + }; + + /// The minimum relay-parent number accepted under these constraints. + BlockNumber min_relay_parent_number; + /// The maximum Proof-of-Validity size allowed, in bytes. + uint32_t max_pov_size; + /// The maximum new validation code size allowed, in bytes. + uint32_t max_code_size; + /// The amount of UMP messages remaining. + uint32_t ump_remaining; + /// The amount of UMP bytes remaining. + uint32_t ump_remaining_bytes; + /// The maximum number of UMP messages allowed per candidate. + uint32_t max_ump_num_per_candidate; + /// Remaining DMP queue. Only includes sent-at block numbers. + std::vector dmp_remaining_messages; + /// The limitations of all registered inbound HRMP channels. + kagome::parachain::fragment::InboundHrmpLimitations hrmp_inbound; + /// The limitations of all registered outbound HRMP channels. + std::unordered_map + hrmp_channels_out; + /// The maximum number of HRMP messages allowed per candidate. + uint32_t max_hrmp_num_per_candidate; + /// The required parent head-data of the parachain. + HeadData required_parent; + /// The expected validation-code-hash of this parachain. + ValidationCodeHash validation_code_hash; + /// The code upgrade restriction signal as-of this parachain. + std::optional upgrade_restriction; + /// The future validation code hash, if any, and at what relay-parent + /// number the upgrade would be minimally applied. + std::optional> future_validation_code; + + outcome::result apply_modifications( + const kagome::parachain::fragment::ConstraintModifications &modifications) const; + + outcome::result check_modifications( + const kagome::parachain::fragment::ConstraintModifications &modifications) const; + }; + + + struct BS { + SCALE_TIE(2); + CCC constraints; + std::vector pending_availability; + }; + + class BackingTest : public ProspectiveParachainsTestHarness { void SetUp() override { ProspectiveParachainsTestHarness::SetUp(); - parachain_api_ = std::make_shared(); + + const auto data = read_binary_file("/home/iceseer/Tmp/scale"); + auto res = scale::decode>(data); + assert(res.has_value()); + + auto k = res.value(); watchdog_ = std::make_shared(std::chrono::milliseconds(1)); main_thread_pool_ = std::make_shared( @@ -137,18 +220,18 @@ class BackingTest : public ProspectiveParachainsTestHarness { EXPECT_CALL(*bitfield_signer_, setBroadcastCallback(testing::_)).Times(1); EXPECT_CALL(app_config_, roles()) .WillRepeatedly(Return(network::Roles(0xff))); + EXPECT_CALL(*prospective_parachains_, getBlockTree()).WillRepeatedly(Return(block_tree_)); app_state_manager.start(); } void TearDown() override { watchdog_->stop(); - parachain_api_.reset(); + parachain_host_.reset(); ProspectiveParachainsTestHarness::TearDown(); } public: - std::shared_ptr parachain_api_; AppConfigurationMock app_config_; std::shared_ptr watchdog_; std::shared_ptr main_thread_pool_; @@ -183,6 +266,10 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::unordered_map head_data; std::vector validators; //std::vector validator_public: , + struct { + std::vector groups; + runtime::GroupDescriptor group_rotation; + } validator_groups; TestState() { const ParachainId chain_a(1); @@ -213,6 +300,18 @@ class BackingTest : public ProspectiveParachainsTestHarness { validators.emplace_back(f("//Dave")); validators.emplace_back(f("//Ferdie")); validators.emplace_back(f("//One")); + + validator_groups.groups = {runtime::ValidatorGroup { + .validators = {2, 0, 3, 5}, + }, + runtime::ValidatorGroup { + .validators = {1}, + }}; + validator_groups.group_rotation = runtime::GroupDescriptor { + .session_start_block = 0, + .group_rotation_frequency = 100, + .now_block_num = 1, + }; } }; @@ -239,7 +338,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { }; update.new_head.hash_opt = leaf_hash; - EXPECT_CALL(*parachain_api_, staging_async_backing_params(leaf_hash)) + EXPECT_CALL(*parachain_host_, staging_async_backing_params(leaf_hash)) .WillRepeatedly(Return(outcome::success(fragment::AsyncBackingParams{ .max_candidate_depth = 4, .allowed_ancestry_len = 3}))); @@ -307,11 +406,21 @@ class BackingTest : public ProspectiveParachainsTestHarness { .hash_opt = {}, })); - EXPECT_CALL(*parachain_api_, validators(hash)) + EXPECT_CALL(*parachain_host_, validators(hash)) .WillRepeatedly(Return(test_state.validators)); + EXPECT_CALL(*parachain_host_, validator_groups(hash)) + .WillRepeatedly(Return(std::make_tuple( + test_state.validator_groups.groups, + runtime::GroupDescriptor { + .session_start_block = test_state.validator_groups.group_rotation.session_start_block, + .group_rotation_frequency = test_state.validator_groups.group_rotation.group_rotation_frequency, + .now_block_num = number, + }))); if (requested_len == 0) { + EXPECT_CALL(*prospective_parachains_, answerMinimumRelayParentsRequest(leaf_hash)) + .WillRepeatedly(Return(min_relay_parents)); // assert_matches !( // virtual_overseer.recv().await, // AllMessages::ProspectiveParachains( @@ -483,7 +592,6 @@ TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { } .build(*hasher_); - std::cout << fmt::format("======> {}", candidate.descriptor.erasure_encoding_root) << std::endl; parachain_processor_->handle_second_message( candidate.to_plain(*hasher_), pov, From db6d8c3efa80591d8ee98ea2916b97287150074f Mon Sep 17 00:00:00 2001 From: iceseer Date: Thu, 5 Dec 2024 12:13:54 +0300 Subject: [PATCH 22/27] validator signer Signed-off-by: iceseer --- .../availability/bitfield/signer.cpp | 12 ++++--- .../availability/bitfield/signer.hpp | 4 +-- core/parachain/pvf/precheck.cpp | 6 ++-- .../validator/impl/parachain_processor.cpp | 10 +++--- .../validator/parachain_processor.hpp | 4 +-- core/parachain/validator/signer.cpp | 8 ++--- core/parachain/validator/signer.hpp | 36 ++++++++++++++----- .../statement_distribution.cpp | 4 +-- .../statement_distribution.hpp | 2 +- test/core/parachain/backing.cpp | 16 ++++++++- .../core/parachain/bitfield_signer_mock.hpp | 2 +- .../core/parachain/signer_factory_mock.hpp | 2 +- 12 files changed, 73 insertions(+), 33 deletions(-) diff --git a/core/parachain/availability/bitfield/signer.cpp b/core/parachain/availability/bitfield/signer.cpp index f138065aed..2e3ced84f3 100644 --- a/core/parachain/availability/bitfield/signer.cpp +++ b/core/parachain/availability/bitfield/signer.cpp @@ -49,7 +49,7 @@ namespace kagome::parachain { broadcast_ = std::move(callback); } - outcome::result BitfieldSigner::sign(const ValidatorSigner &signer, + outcome::result BitfieldSigner::sign(const IValidatorSigner &signer, const Candidates &candidates) { const BlockHash &relay_parent = signer.relayParent(); scale::BitVec bitfield; @@ -70,10 +70,12 @@ namespace kagome::parachain { } outcome::result 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( @@ -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()); } diff --git a/core/parachain/availability/bitfield/signer.hpp b/core/parachain/availability/bitfield/signer.hpp index 946e48d875..214adca9db 100644 --- a/core/parachain/availability/bitfield/signer.hpp +++ b/core/parachain/availability/bitfield/signer.hpp @@ -28,7 +28,7 @@ namespace kagome::parachain { virtual void start() = 0; /// Sign bitfield for given block. - virtual outcome::result sign(const ValidatorSigner &signer, + virtual outcome::result sign(const IValidatorSigner &signer, const Candidates &candidates) = 0; virtual void setBroadcastCallback(BroadcastCallback &&callback) = 0; @@ -51,7 +51,7 @@ namespace kagome::parachain { /// Subscribes to new heads. void start() override; - outcome::result sign(const ValidatorSigner &signer, + outcome::result sign(const IValidatorSigner &signer, const Candidates &candidates) override; void setBroadcastCallback(BroadcastCallback &&callback) override; diff --git a/core/parachain/pvf/precheck.cpp b/core/parachain/pvf/precheck.cpp index 35bf99ac1d..5aa06e95ea 100644 --- a/core/parachain/pvf/precheck.cpp +++ b/core/parachain/pvf/precheck.cpp @@ -69,10 +69,12 @@ namespace kagome::parachain { outcome::result 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"); diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index e51fbf92ea..6a748221e7 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -400,7 +400,7 @@ namespace kagome::parachain { } } - outcome::result> + outcome::result>> ParachainProcessorImpl::isParachainValidator( const primitives::BlockHash &relay_parent) const { return signer_factory_->at(relay_parent); @@ -444,6 +444,8 @@ namespace kagome::parachain { * group. Finally, it returns a `RelayParentState` object that contains the * assignment, validator index, required collator, and table context. */ + std::cout << fmt::format("===> ask data with {}", relay_parent) << std::endl; + bool is_parachain_validator = false; ::libp2p::common::FinalAction metric_updater{ [&] { metric_is_parachain_validator_->set(is_parachain_validator); }}; @@ -492,7 +494,7 @@ namespace kagome::parachain { std::optional validator_index; if (validator) { - validator_index = validator->validatorIndex(); + validator_index = (*validator)->validatorIndex(); } OUTCOME_TRY(global_v_index, @@ -1360,7 +1362,7 @@ namespace kagome::parachain { const auto our_index = utils::map( table_context.validator, - [](const auto &signer) { return signer.validatorIndex(); }); + [](const auto &signer) { return signer->validatorIndex(); }); if (our_index && *our_index == statement.payload.ix) { return std::nullopt; } @@ -2088,7 +2090,7 @@ namespace kagome::parachain { /// TODO(iceseer): /// https://github.com/paritytech/polkadot/blob/master/primitives/src/v2/mod.rs#L1535-L1545 auto sign_result = - parachain_state.table_context.validator->sign(std::forward(payload)); + (*parachain_state.table_context.validator)->sign(std::forward(payload)); if (sign_result.has_error()) { logger_->error( "Unable to sign Commited Candidate Receipt. Failed with error: {}", diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index e3aa053b7b..a24c42c5b4 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -325,7 +325,7 @@ namespace kagome::parachain { }; struct TableContext { - std::optional validator; + std::optional> validator; std::unordered_map> groups; std::vector validators; @@ -531,7 +531,7 @@ namespace kagome::parachain { AttestedCandidate &&attested, TableContext &table_context, bool inject_core_index); - outcome::result> isParachainValidator( + outcome::result>> isParachainValidator( const primitives::BlockHash &relay_parent) const; /* diff --git a/core/parachain/validator/signer.cpp b/core/parachain/validator/signer.cpp index b005fd123d..3bf6d42131 100644 --- a/core/parachain/validator/signer.cpp +++ b/core/parachain/validator/signer.cpp @@ -50,7 +50,7 @@ namespace kagome::parachain { hasher_{std::move(hasher)}, sr25519_provider_{std::move(sr25519_provider)} {} - outcome::result> ValidatorSignerFactory::at( + outcome::result>> ValidatorSignerFactory::at( const primitives::BlockHash &relay_parent) { OUTCOME_TRY(validators, parachain_api_->validators(relay_parent)); auto keypair = session_keys_->getParaKeyPair(validators); @@ -58,13 +58,13 @@ namespace kagome::parachain { return std::nullopt; } OUTCOME_TRY(context, SigningContext::make(parachain_api_, relay_parent)); - return ValidatorSigner{ + return std::make_shared( keypair->second, context, std::move(keypair->first), hasher_, - sr25519_provider_, - }; + sr25519_provider_ + ); } outcome::result> diff --git a/core/parachain/validator/signer.hpp b/core/parachain/validator/signer.hpp index 1c2d8af446..783d7d054d 100644 --- a/core/parachain/validator/signer.hpp +++ b/core/parachain/validator/signer.hpp @@ -57,8 +57,21 @@ namespace kagome::parachain { primitives::BlockHash relay_parent; }; + class IValidatorSigner { + public: + virtual ~IValidatorSigner() = default; + + virtual outcome::result> sign(const network::Statement &payload) const = 0; + virtual outcome::result> sign(const scale::BitVec &payload) const = 0; + + virtual ValidatorIndex validatorIndex() const = 0; + virtual SessionIndex getSessionIndex() const = 0; + virtual const primitives::BlockHash &relayParent() const = 0; + virtual outcome::result signRaw(common::BufferView data) const = 0; + }; + /// Signs payload with signing context and validator keypair. - class ValidatorSigner { + class ValidatorSigner : public IValidatorSigner { public: using ValidatorIndex = network::ValidatorIndex; @@ -68,9 +81,16 @@ namespace kagome::parachain { std::shared_ptr hasher, std::shared_ptr sr25519_provider); + outcome::result> sign(const network::Statement &payload) const override { + return sign_obj(payload); + } + outcome::result> sign(const scale::BitVec &payload) const override { + return sign_obj(payload); + } + /// Sign payload. template - outcome::result> sign(T payload) const { + outcome::result> sign_obj(T payload) const { auto data = context_.signable(*hasher_, payload); OUTCOME_TRY(signature, sr25519_provider_->sign(*keypair_, data)); return parachain::IndexedAndSigned{ @@ -79,17 +99,17 @@ namespace kagome::parachain { }; } - outcome::result signRaw(common::BufferView data) const { + outcome::result signRaw(common::BufferView data) const override { return sr25519_provider_->sign(*keypair_, data); } - SessionIndex getSessionIndex() const; + SessionIndex getSessionIndex() const override; /// Get validator index. - ValidatorIndex validatorIndex() const; + ValidatorIndex validatorIndex() const override; /// Get relay parent hash. - const primitives::BlockHash &relayParent() const; + const primitives::BlockHash &relayParent() const override; private: ValidatorIndex validator_index_; @@ -105,7 +125,7 @@ namespace kagome::parachain { virtual ~IValidatorSignerFactory() = default; /// Create validator signer if keypair belongs to validator at given block. - virtual outcome::result> at( + virtual outcome::result>> at( const primitives::BlockHash &relay_parent) = 0; virtual outcome::result> @@ -124,7 +144,7 @@ namespace kagome::parachain { std::shared_ptr sr25519_provider); /// Create validator signer if keypair belongs to validator at given block. - outcome::result> at( + outcome::result>> at( const primitives::BlockHash &relay_parent) override; outcome::result> getAuthorityValidatorIndex( diff --git a/core/parachain/validator/statement_distribution/statement_distribution.cpp b/core/parachain/validator/statement_distribution/statement_distribution.cpp index 4c422c8463..6d8c4e9a9a 100644 --- a/core/parachain/validator/statement_distribution/statement_distribution.cpp +++ b/core/parachain/validator/statement_distribution/statement_distribution.cpp @@ -232,7 +232,7 @@ namespace kagome::parachain::statement_distribution { peers.erase(peer); } - outcome::result> + outcome::result>> StatementDistribution::is_parachain_validator( const primitives::BlockHash &relay_parent) const { BOOST_ASSERT(main_pool_handler->isInCurrentThread()); @@ -269,7 +269,7 @@ namespace kagome::parachain::statement_distribution { if (auto res = is_parachain_validator(new_relay_parent); res.has_value()) { validator_index = utils::map(res.value(), [](const auto &signer) { - return signer.validatorIndex(); + return signer->validatorIndex(); }); } diff --git a/core/parachain/validator/statement_distribution/statement_distribution.hpp b/core/parachain/validator/statement_distribution/statement_distribution.hpp index e1557c606d..cfa42efb81 100644 --- a/core/parachain/validator/statement_distribution/statement_distribution.hpp +++ b/core/parachain/validator/statement_distribution/statement_distribution.hpp @@ -327,7 +327,7 @@ namespace kagome::parachain::statement_distribution { std::vector> frontier); - outcome::result> is_parachain_validator( + outcome::result>> is_parachain_validator( const primitives::BlockHash &relay_parent) const; std::unordered_map> diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 3c7f2163ca..e0866ceee8 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -265,7 +265,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::vector chain_ids; std::unordered_map head_data; std::vector validators; - //std::vector validator_public: , + std::vector availability_cores; struct { std::vector groups; runtime::GroupDescriptor group_rotation; @@ -312,6 +312,17 @@ class BackingTest : public ProspectiveParachainsTestHarness { .group_rotation_frequency = 100, .now_block_num = 1, }; + + availability_cores = { + runtime::ScheduledCore { + .para_id = chain_a, + .collator = std::nullopt, + }, + runtime::ScheduledCore { + .para_id = chain_b, + .collator = std::nullopt, + } + }; } }; @@ -418,6 +429,9 @@ class BackingTest : public ProspectiveParachainsTestHarness { .now_block_num = number, }))); + EXPECT_CALL(*parachain_host_, availability_cores(hash)) + .WillRepeatedly(Return(test_state.availability_cores)); + if (requested_len == 0) { EXPECT_CALL(*prospective_parachains_, answerMinimumRelayParentsRequest(leaf_hash)) .WillRepeatedly(Return(min_relay_parents)); diff --git a/test/mock/core/parachain/bitfield_signer_mock.hpp b/test/mock/core/parachain/bitfield_signer_mock.hpp index 458f056dfa..ef956a5921 100644 --- a/test/mock/core/parachain/bitfield_signer_mock.hpp +++ b/test/mock/core/parachain/bitfield_signer_mock.hpp @@ -18,7 +18,7 @@ namespace kagome::parachain { MOCK_METHOD(outcome::result, sign, - (const ValidatorSigner &, const Candidates &), + (const IValidatorSigner &, const Candidates &), (override)); MOCK_METHOD(void, setBroadcastCallback, (BroadcastCallback &&), (override)); diff --git a/test/mock/core/parachain/signer_factory_mock.hpp b/test/mock/core/parachain/signer_factory_mock.hpp index 3b9dfaa8c0..dde34e731d 100644 --- a/test/mock/core/parachain/signer_factory_mock.hpp +++ b/test/mock/core/parachain/signer_factory_mock.hpp @@ -14,7 +14,7 @@ namespace kagome::parachain { class ValidatorSignerFactoryMock : public IValidatorSignerFactory { public: - MOCK_METHOD(outcome::result>, + MOCK_METHOD(outcome::result>>, at, (const primitives::BlockHash &), (override)); From 1c36a70afc0e22a7bc36e5734570e63c7dc48f39 Mon Sep 17 00:00:00 2001 From: iceseer Date: Thu, 5 Dec 2024 12:27:38 +0300 Subject: [PATCH 23/27] signing context Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 19 +++++++++++ .../core/parachain/signer_factory_mock.hpp | 33 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index e0866ceee8..189d4e1708 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -182,6 +182,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { babe_config_repo_ = std::make_shared(); statement_distribution_ = std::make_shared(); + signer_ = std::make_shared(); my_view_observable_ = std::make_shared(); @@ -258,6 +259,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::shared_ptr statement_distribution_; std::shared_ptr parachain_processor_; + std::shared_ptr signer_; PeerViewMock::MyViewSubscriptionEnginePtr my_view_observable_; @@ -266,6 +268,8 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::unordered_map head_data; std::vector validators; std::vector availability_cores; + SigningContext signing_context; + struct { std::vector groups; runtime::GroupDescriptor group_rotation; @@ -323,6 +327,14 @@ class BackingTest : public ProspectiveParachainsTestHarness { .collator = std::nullopt, } }; + + const auto relay_parent = fromNumber(5); + + signing_context = SigningContext { + .session_index = 1, + .relay_parent = relay_parent, + }; + } }; @@ -407,6 +419,9 @@ class BackingTest : public ProspectiveParachainsTestHarness { ((i == ancestry_hashes.size() - 1) ? get_parent_hash(hash) : ancestry_hashes[i + 1]); + EXPECT_CALL(*parachain_host_, session_index_for_child(hash)) + .WillRepeatedly(Return(test_state.signing_context.session_index)); + EXPECT_CALL(*block_tree_, getBlockHeader(hash)) .WillRepeatedly(Return(BlockHeader{ .number = number, @@ -432,6 +447,10 @@ class BackingTest : public ProspectiveParachainsTestHarness { EXPECT_CALL(*parachain_host_, availability_cores(hash)) .WillRepeatedly(Return(test_state.availability_cores)); + EXPECT_CALL(*signer_factory_, at(hash)) + .WillRepeatedly(Return(signer_)); + + if (requested_len == 0) { EXPECT_CALL(*prospective_parachains_, answerMinimumRelayParentsRequest(leaf_hash)) .WillRepeatedly(Return(min_relay_parents)); diff --git a/test/mock/core/parachain/signer_factory_mock.hpp b/test/mock/core/parachain/signer_factory_mock.hpp index dde34e731d..5fdf1a2871 100644 --- a/test/mock/core/parachain/signer_factory_mock.hpp +++ b/test/mock/core/parachain/signer_factory_mock.hpp @@ -12,6 +12,39 @@ namespace kagome::parachain { + class ValidatorSignerMock : public IValidatorSigner { + public: + MOCK_METHOD(outcome::result>, + sign, + (const network::Statement &), + (const, override)); + + MOCK_METHOD(outcome::result>, + sign, + (const scale::BitVec &), + (const, override)); + + MOCK_METHOD(ValidatorIndex, + validatorIndex, + (), + (const, override)); + + MOCK_METHOD(SessionIndex, + getSessionIndex, + (), + (const, override)); + + MOCK_METHOD(const primitives::BlockHash &, + relayParent, + (), + (const, override)); + + MOCK_METHOD(outcome::result, + signRaw, + (common::BufferView), + (const, override)); + }; + class ValidatorSignerFactoryMock : public IValidatorSignerFactory { public: MOCK_METHOD(outcome::result>>, From 8d0dbf8838cf3d2855eb7c75ef3d5b2dce74edfe Mon Sep 17 00:00:00 2001 From: iceseer Date: Thu, 5 Dec 2024 12:28:40 +0300 Subject: [PATCH 24/27] formatting Signed-off-by: iceseer --- core/parachain/pvf/precheck.cpp | 5 +- core/parachain/types.hpp | 2 +- .../validator/impl/parachain_processor.cpp | 14 +- .../validator/parachain_processor.hpp | 12 +- core/parachain/validator/signer.cpp | 16 +- core/parachain/validator/signer.hpp | 21 +- .../statement_distribution.hpp | 4 +- test/core/parachain/backing.cpp | 322 +++++++++--------- .../core/parachain/signer_factory_mock.hpp | 21 +- 9 files changed, 208 insertions(+), 209 deletions(-) diff --git a/core/parachain/pvf/precheck.cpp b/core/parachain/pvf/precheck.cpp index 5aa06e95ea..af1d4d586a 100644 --- a/core/parachain/pvf/precheck.cpp +++ b/core/parachain/pvf/precheck.cpp @@ -74,7 +74,7 @@ namespace kagome::parachain { 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"); @@ -102,7 +102,8 @@ namespace kagome::parachain { auto &code_zstd = *code_zstd_res.value(); auto res = [&]() -> outcome::result { 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) { diff --git a/core/parachain/types.hpp b/core/parachain/types.hpp index 4f8473dc11..9b0e5b3c63 100644 --- a/core/parachain/types.hpp +++ b/core/parachain/types.hpp @@ -222,7 +222,7 @@ namespace kagome::network { } // namespace kagome::network namespace kagome::parachain::fragment { - enum UpgradeRestriction: uint8_t { + enum UpgradeRestriction : uint8_t { /// There is an upgrade restriction and there are no details about its /// specifics nor how long /// it could last. diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index 6a748221e7..ffd8627f3f 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -444,7 +444,8 @@ namespace kagome::parachain { * group. Finally, it returns a `RelayParentState` object that contains the * assignment, validator index, required collator, and table context. */ - std::cout << fmt::format("===> ask data with {}", relay_parent) << std::endl; + std::cout << fmt::format("===> ask data with {}", relay_parent) + << std::endl; bool is_parachain_validator = false; ::libp2p::common::FinalAction metric_updater{ @@ -1360,9 +1361,10 @@ namespace kagome::parachain { return std::nullopt; } - const auto our_index = utils::map( - table_context.validator, - [](const auto &signer) { return signer->validatorIndex(); }); + const auto our_index = + utils::map(table_context.validator, [](const auto &signer) { + return signer->validatorIndex(); + }); if (our_index && *our_index == statement.payload.ix) { return std::nullopt; } @@ -2089,8 +2091,8 @@ namespace kagome::parachain { T &&payload, RelayParentState ¶chain_state) { /// TODO(iceseer): /// https://github.com/paritytech/polkadot/blob/master/primitives/src/v2/mod.rs#L1535-L1545 - auto sign_result = - (*parachain_state.table_context.validator)->sign(std::forward(payload)); + auto sign_result = (*parachain_state.table_context.validator) + ->sign(std::forward(payload)); if (sign_result.has_error()) { logger_->error( "Unable to sign Commited Candidate Receipt. Failed with error: {}", diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index a24c42c5b4..1eec064f6e 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -531,8 +531,8 @@ namespace kagome::parachain { AttestedCandidate &&attested, TableContext &table_context, bool inject_core_index); - outcome::result>> isParachainValidator( - const primitives::BlockHash &relay_parent) const; + outcome::result>> + isParachainValidator(const primitives::BlockHash &relay_parent) const; /* * Logic. @@ -822,11 +822,11 @@ namespace kagome::parachain { metrics::RegistryPtr metrics_registry_ = metrics::createRegistry(); metrics::Gauge *metric_is_parachain_validator_; - public: + public: void handle_second_message(const network::CandidateReceipt &candidate, - const network::ParachainBlock &pov, - const runtime::PersistedValidationData &pvd, - const primitives::BlockHash &relay_parent) { + const network::ParachainBlock &pov, + const runtime::PersistedValidationData &pvd, + const primitives::BlockHash &relay_parent) { validateAsync( candidate, network::ParachainBlock(pov), diff --git a/core/parachain/validator/signer.cpp b/core/parachain/validator/signer.cpp index 3bf6d42131..82400c945b 100644 --- a/core/parachain/validator/signer.cpp +++ b/core/parachain/validator/signer.cpp @@ -50,21 +50,19 @@ namespace kagome::parachain { hasher_{std::move(hasher)}, sr25519_provider_{std::move(sr25519_provider)} {} - outcome::result>> ValidatorSignerFactory::at( - const primitives::BlockHash &relay_parent) { + outcome::result>> + ValidatorSignerFactory::at(const primitives::BlockHash &relay_parent) { OUTCOME_TRY(validators, parachain_api_->validators(relay_parent)); auto keypair = session_keys_->getParaKeyPair(validators); if (not keypair) { return std::nullopt; } OUTCOME_TRY(context, SigningContext::make(parachain_api_, relay_parent)); - return std::make_shared( - keypair->second, - context, - std::move(keypair->first), - hasher_, - sr25519_provider_ - ); + return std::make_shared(keypair->second, + context, + std::move(keypair->first), + hasher_, + sr25519_provider_); } outcome::result> diff --git a/core/parachain/validator/signer.hpp b/core/parachain/validator/signer.hpp index 783d7d054d..ea7768ced2 100644 --- a/core/parachain/validator/signer.hpp +++ b/core/parachain/validator/signer.hpp @@ -58,16 +58,19 @@ namespace kagome::parachain { }; class IValidatorSigner { - public: + public: virtual ~IValidatorSigner() = default; - virtual outcome::result> sign(const network::Statement &payload) const = 0; - virtual outcome::result> sign(const scale::BitVec &payload) const = 0; + virtual outcome::result> sign( + const network::Statement &payload) const = 0; + virtual outcome::result> sign( + const scale::BitVec &payload) const = 0; virtual ValidatorIndex validatorIndex() const = 0; virtual SessionIndex getSessionIndex() const = 0; virtual const primitives::BlockHash &relayParent() const = 0; - virtual outcome::result signRaw(common::BufferView data) const = 0; + virtual outcome::result signRaw( + common::BufferView data) const = 0; }; /// Signs payload with signing context and validator keypair. @@ -81,10 +84,12 @@ namespace kagome::parachain { std::shared_ptr hasher, std::shared_ptr sr25519_provider); - outcome::result> sign(const network::Statement &payload) const override { + outcome::result> sign( + const network::Statement &payload) const override { return sign_obj(payload); } - outcome::result> sign(const scale::BitVec &payload) const override { + outcome::result> sign( + const scale::BitVec &payload) const override { return sign_obj(payload); } @@ -125,8 +130,8 @@ namespace kagome::parachain { virtual ~IValidatorSignerFactory() = default; /// Create validator signer if keypair belongs to validator at given block. - virtual outcome::result>> at( - const primitives::BlockHash &relay_parent) = 0; + virtual outcome::result>> + at(const primitives::BlockHash &relay_parent) = 0; virtual outcome::result> getAuthorityValidatorIndex(const primitives::BlockHash &relay_parent) = 0; diff --git a/core/parachain/validator/statement_distribution/statement_distribution.hpp b/core/parachain/validator/statement_distribution/statement_distribution.hpp index cfa42efb81..51e5b03206 100644 --- a/core/parachain/validator/statement_distribution/statement_distribution.hpp +++ b/core/parachain/validator/statement_distribution/statement_distribution.hpp @@ -327,8 +327,8 @@ namespace kagome::parachain::statement_distribution { std::vector> frontier); - outcome::result>> is_parachain_validator( - const primitives::BlockHash &relay_parent) const; + outcome::result>> + is_parachain_validator(const primitives::BlockHash &relay_parent) const; std::unordered_map> determine_groups_per_para( diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 189d4e1708..74b66c3f37 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -69,83 +69,87 @@ using kagome::primitives::events::SyncStateSubscriptionEnginePtr; using kagome::runtime::ParachainHost; using kagome::runtime::ParachainHostMock; -static std::vector read_binary_file (const std::string filename) -{ - // binary mode is only for switching off newline translation - std::ifstream file(filename, std::ios::binary); - file.unsetf(std::ios::skipws); - - std::streampos file_size; - file.seekg(0, std::ios::end); - file_size = file.tellg(); - file.seekg(0, std::ios::beg); - - std::vector vec; - vec.reserve(file_size); - vec.insert(vec.begin(), - std::istream_iterator(file), - std::istream_iterator()); - return (vec); +static std::vector read_binary_file(const std::string filename) { + // binary mode is only for switching off newline translation + std::ifstream file(filename, std::ios::binary); + file.unsetf(std::ios::skipws); + + std::streampos file_size; + file.seekg(0, std::ios::end); + file_size = file.tellg(); + file.seekg(0, std::ios::beg); + + std::vector vec; + vec.reserve(file_size); + vec.insert(vec.begin(), + std::istream_iterator(file), + std::istream_iterator()); + return (vec); } - struct CCC { - SCALE_TIE(14); - enum class Error { - DISALLOWED_HRMP_WATERMARK, - NO_SUCH_HRMP_CHANNEL, - HRMP_BYTES_OVERFLOW, - HRMP_MESSAGE_OVERFLOW, - UMP_MESSAGE_OVERFLOW, - UMP_BYTES_OVERFLOW, - DMP_MESSAGE_UNDERFLOW, - APPLIED_NONEXISTENT_CODE_UPGRADE, - }; - - /// The minimum relay-parent number accepted under these constraints. - BlockNumber min_relay_parent_number; - /// The maximum Proof-of-Validity size allowed, in bytes. - uint32_t max_pov_size; - /// The maximum new validation code size allowed, in bytes. - uint32_t max_code_size; - /// The amount of UMP messages remaining. - uint32_t ump_remaining; - /// The amount of UMP bytes remaining. - uint32_t ump_remaining_bytes; - /// The maximum number of UMP messages allowed per candidate. - uint32_t max_ump_num_per_candidate; - /// Remaining DMP queue. Only includes sent-at block numbers. - std::vector dmp_remaining_messages; - /// The limitations of all registered inbound HRMP channels. - kagome::parachain::fragment::InboundHrmpLimitations hrmp_inbound; - /// The limitations of all registered outbound HRMP channels. - std::unordered_map - hrmp_channels_out; - /// The maximum number of HRMP messages allowed per candidate. - uint32_t max_hrmp_num_per_candidate; - /// The required parent head-data of the parachain. - HeadData required_parent; - /// The expected validation-code-hash of this parachain. - ValidationCodeHash validation_code_hash; - /// The code upgrade restriction signal as-of this parachain. - std::optional upgrade_restriction; - /// The future validation code hash, if any, and at what relay-parent - /// number the upgrade would be minimally applied. - std::optional> future_validation_code; - - outcome::result apply_modifications( - const kagome::parachain::fragment::ConstraintModifications &modifications) const; - - outcome::result check_modifications( - const kagome::parachain::fragment::ConstraintModifications &modifications) const; +struct CCC { + SCALE_TIE(14); + enum class Error { + DISALLOWED_HRMP_WATERMARK, + NO_SUCH_HRMP_CHANNEL, + HRMP_BYTES_OVERFLOW, + HRMP_MESSAGE_OVERFLOW, + UMP_MESSAGE_OVERFLOW, + UMP_BYTES_OVERFLOW, + DMP_MESSAGE_UNDERFLOW, + APPLIED_NONEXISTENT_CODE_UPGRADE, }; + /// The minimum relay-parent number accepted under these constraints. + BlockNumber min_relay_parent_number; + /// The maximum Proof-of-Validity size allowed, in bytes. + uint32_t max_pov_size; + /// The maximum new validation code size allowed, in bytes. + uint32_t max_code_size; + /// The amount of UMP messages remaining. + uint32_t ump_remaining; + /// The amount of UMP bytes remaining. + uint32_t ump_remaining_bytes; + /// The maximum number of UMP messages allowed per candidate. + uint32_t max_ump_num_per_candidate; + /// Remaining DMP queue. Only includes sent-at block numbers. + std::vector dmp_remaining_messages; + /// The limitations of all registered inbound HRMP channels. + kagome::parachain::fragment::InboundHrmpLimitations hrmp_inbound; + /// The limitations of all registered outbound HRMP channels. + std::unordered_map< + ParachainId, + kagome::parachain::fragment::OutboundHrmpChannelLimitations> + hrmp_channels_out; + /// The maximum number of HRMP messages allowed per candidate. + uint32_t max_hrmp_num_per_candidate; + /// The required parent head-data of the parachain. + HeadData required_parent; + /// The expected validation-code-hash of this parachain. + ValidationCodeHash validation_code_hash; + /// The code upgrade restriction signal as-of this parachain. + std::optional + upgrade_restriction; + /// The future validation code hash, if any, and at what relay-parent + /// number the upgrade would be minimally applied. + std::optional> + future_validation_code; + + outcome::result apply_modifications( + const kagome::parachain::fragment::ConstraintModifications &modifications) + const; + + outcome::result check_modifications( + const kagome::parachain::fragment::ConstraintModifications &modifications) + const; +}; - struct BS { - SCALE_TIE(2); - CCC constraints; - std::vector pending_availability; - }; - +struct BS { + SCALE_TIE(2); + CCC constraints; + std::vector + pending_availability; +}; class BackingTest : public ProspectiveParachainsTestHarness { void SetUp() override { @@ -217,11 +221,13 @@ class BackingTest : public ProspectiveParachainsTestHarness { EXPECT_CALL(*peer_view_, getMyViewObservable()) .WillRepeatedly(Return(my_view_observable_)); - EXPECT_CALL(*statement_distribution_, store_parachain_processor(testing::_)).Times(1); + EXPECT_CALL(*statement_distribution_, store_parachain_processor(testing::_)) + .Times(1); EXPECT_CALL(*bitfield_signer_, setBroadcastCallback(testing::_)).Times(1); EXPECT_CALL(app_config_, roles()) - .WillRepeatedly(Return(network::Roles(0xff))); - EXPECT_CALL(*prospective_parachains_, getBlockTree()).WillRepeatedly(Return(block_tree_)); + .WillRepeatedly(Return(network::Roles(0xff))); + EXPECT_CALL(*prospective_parachains_, getBlockTree()) + .WillRepeatedly(Return(block_tree_)); app_state_manager.start(); } @@ -271,7 +277,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { SigningContext signing_context; struct { - std::vector groups; + std::vector groups; runtime::GroupDescriptor group_rotation; } validator_groups; @@ -305,36 +311,33 @@ class BackingTest : public ProspectiveParachainsTestHarness { validators.emplace_back(f("//Ferdie")); validators.emplace_back(f("//One")); - validator_groups.groups = {runtime::ValidatorGroup { - .validators = {2, 0, 3, 5}, - }, - runtime::ValidatorGroup { - .validators = {1}, - }}; - validator_groups.group_rotation = runtime::GroupDescriptor { - .session_start_block = 0, - .group_rotation_frequency = 100, - .now_block_num = 1, + validator_groups.groups = {runtime::ValidatorGroup{ + .validators = {2, 0, 3, 5}, + }, + runtime::ValidatorGroup{ + .validators = {1}, + }}; + validator_groups.group_rotation = runtime::GroupDescriptor{ + .session_start_block = 0, + .group_rotation_frequency = 100, + .now_block_num = 1, }; - availability_cores = { - runtime::ScheduledCore { - .para_id = chain_a, - .collator = std::nullopt, - }, - runtime::ScheduledCore { - .para_id = chain_b, - .collator = std::nullopt, - } - }; + availability_cores = {runtime::ScheduledCore{ + .para_id = chain_a, + .collator = std::nullopt, + }, + runtime::ScheduledCore{ + .para_id = chain_b, + .collator = std::nullopt, + }}; - const auto relay_parent = fromNumber(5); + const auto relay_parent = fromNumber(5); - signing_context = SigningContext { - .session_index = 1, - .relay_parent = relay_parent, + signing_context = SigningContext{ + .session_index = 1, + .relay_parent = relay_parent, }; - } }; @@ -366,18 +369,17 @@ class BackingTest : public ProspectiveParachainsTestHarness { .max_candidate_depth = 4, .allowed_ancestry_len = 3}))); EXPECT_CALL(*prospective_parachains_, prospectiveParachainsMode(leaf_hash)) - .WillRepeatedly(Return(ProspectiveParachainsMode { - .max_candidate_depth = 4, - .allowed_ancestry_len = 3, - })); - - -// network::ExViewRef ev_ref{ -// .new_head = {update.new_head}, -// .lost = update.lost, -// }; + .WillRepeatedly(Return(ProspectiveParachainsMode{ + .max_candidate_depth = 4, + .allowed_ancestry_len = 3, + })); + + // network::ExViewRef ev_ref{ + // .new_head = {update.new_head}, + // .lost = update.lost, + // }; EXPECT_CALL(*prospective_parachains_, onActiveLeavesUpdate(testing::_)) - .WillRepeatedly(Return(outcome::success())); + .WillRepeatedly(Return(outcome::success())); const BlockNumber min_min = [&]() -> BlockNumber { std::optional min_min; @@ -436,23 +438,26 @@ class BackingTest : public ProspectiveParachainsTestHarness { .WillRepeatedly(Return(test_state.validators)); EXPECT_CALL(*parachain_host_, validator_groups(hash)) - .WillRepeatedly(Return(std::make_tuple( - test_state.validator_groups.groups, - runtime::GroupDescriptor { - .session_start_block = test_state.validator_groups.group_rotation.session_start_block, - .group_rotation_frequency = test_state.validator_groups.group_rotation.group_rotation_frequency, - .now_block_num = number, - }))); + .WillRepeatedly(Return( + std::make_tuple(test_state.validator_groups.groups, + runtime::GroupDescriptor{ + .session_start_block = + test_state.validator_groups.group_rotation + .session_start_block, + .group_rotation_frequency = + test_state.validator_groups.group_rotation + .group_rotation_frequency, + .now_block_num = number, + }))); EXPECT_CALL(*parachain_host_, availability_cores(hash)) .WillRepeatedly(Return(test_state.availability_cores)); - EXPECT_CALL(*signer_factory_, at(hash)) - .WillRepeatedly(Return(signer_)); - + EXPECT_CALL(*signer_factory_, at(hash)).WillRepeatedly(Return(signer_)); if (requested_len == 0) { - EXPECT_CALL(*prospective_parachains_, answerMinimumRelayParentsRequest(leaf_hash)) + EXPECT_CALL(*prospective_parachains_, + answerMinimumRelayParentsRequest(leaf_hash)) .WillRepeatedly(Return(min_relay_parents)); // assert_matches !( // virtual_overseer.recv().await, @@ -501,33 +506,33 @@ class BackingTest : public ProspectiveParachainsTestHarness { return makeTrieProof(chunks); } - void assert_validation_requests(const runtime::ValidationCode &validation_code) { - } + void assert_validation_requests( + const runtime::ValidationCode &validation_code) {} void assert_validate_seconded_candidate( - const Hash &relay_parent, - const network::CommittedCandidateReceipt &candidate, - const network::PoV &assert_pov, - const runtime::PersistedValidationData &assert_pvd, - const runtime::ValidationCode &assert_validation_code, - const HeadData &expected_head_data, - bool fetch_pov) { + const Hash &relay_parent, + const network::CommittedCandidateReceipt &candidate, + const network::PoV &assert_pov, + const runtime::PersistedValidationData &assert_pvd, + const runtime::ValidationCode &assert_validation_code, + const HeadData &expected_head_data, + bool fetch_pov) { assert_validation_requests(assert_validation_code); if (fetch_pov) { - //assert_matches!( - // virtual_overseer.recv().await, - // AllMessages::AvailabilityDistribution( - // AvailabilityDistributionMessage::FetchPoV { - // relay_parent: hash, - // tx, - // .. - // } - // ) if hash == relay_parent => { - // tx.send(assert_pov.clone()).unwrap(); - // } + // assert_matches!( + // virtual_overseer.recv().await, + // AllMessages::AvailabilityDistribution( + // AvailabilityDistributionMessage::FetchPoV { + // relay_parent: hash, + // tx, + // .. + // } + // ) if hash == relay_parent => { + // tx.send(assert_pov.clone()).unwrap(); + // } //); - }// + } // } struct TestCandidateBuilder { @@ -600,8 +605,8 @@ TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { }; sync_state_observable_->notify( - kagome::primitives::events::SyncStateEventType::kSyncState, - kagome::primitives::events::SyncStateEventParams::SYNCHRONIZED); + kagome::primitives::events::SyncStateEventType::kSyncState, + kagome::primitives::events::SyncStateEventParams::SYNCHRONIZED); activate_leaf(test_leaf_a, test_state); activate_leaf(test_leaf_b, test_state); @@ -626,20 +631,13 @@ TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { .build(*hasher_); parachain_processor_->handle_second_message( - candidate.to_plain(*hasher_), - pov, - pvd, - leaf_a_hash - ); - - assert_validate_seconded_candidate( - leaf_a_parent, - candidate, - pov, - pvd, - validation_code, - expected_head_data, - false - ); - + candidate.to_plain(*hasher_), pov, pvd, leaf_a_hash); + + assert_validate_seconded_candidate(leaf_a_parent, + candidate, + pov, + pvd, + validation_code, + expected_head_data, + false); } diff --git a/test/mock/core/parachain/signer_factory_mock.hpp b/test/mock/core/parachain/signer_factory_mock.hpp index 5fdf1a2871..676549f672 100644 --- a/test/mock/core/parachain/signer_factory_mock.hpp +++ b/test/mock/core/parachain/signer_factory_mock.hpp @@ -13,7 +13,7 @@ namespace kagome::parachain { class ValidatorSignerMock : public IValidatorSigner { - public: + public: MOCK_METHOD(outcome::result>, sign, (const network::Statement &), @@ -24,15 +24,9 @@ namespace kagome::parachain { (const scale::BitVec &), (const, override)); - MOCK_METHOD(ValidatorIndex, - validatorIndex, - (), - (const, override)); + MOCK_METHOD(ValidatorIndex, validatorIndex, (), (const, override)); - MOCK_METHOD(SessionIndex, - getSessionIndex, - (), - (const, override)); + MOCK_METHOD(SessionIndex, getSessionIndex, (), (const, override)); MOCK_METHOD(const primitives::BlockHash &, relayParent, @@ -47,10 +41,11 @@ namespace kagome::parachain { class ValidatorSignerFactoryMock : public IValidatorSignerFactory { public: - MOCK_METHOD(outcome::result>>, - at, - (const primitives::BlockHash &), - (override)); + MOCK_METHOD( + outcome::result>>, + at, + (const primitives::BlockHash &), + (override)); MOCK_METHOD(outcome::result>, getAuthorityValidatorIndex, From ac40ec17b8d131b6d8b0f0ef1dd1f3ad789127d0 Mon Sep 17 00:00:00 2001 From: iceseer Date: Thu, 5 Dec 2024 13:37:37 +0300 Subject: [PATCH 25/27] builded Signed-off-by: iceseer --- test/core/parachain/backing.cpp | 21 +++++++++++++++++++ .../core/parachain/parachain_test_harness.hpp | 1 + 2 files changed, 22 insertions(+) diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 74b66c3f37..18657991ad 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -275,6 +275,7 @@ class BackingTest : public ProspectiveParachainsTestHarness { std::vector validators; std::vector availability_cores; SigningContext signing_context; + uint32_t minimum_backing_votes; struct { std::vector groups; @@ -338,6 +339,8 @@ class BackingTest : public ProspectiveParachainsTestHarness { .session_index = 1, .relay_parent = relay_parent, }; + + minimum_backing_votes = LEGACY_MIN_BACKING_VOTES; } }; @@ -454,6 +457,24 @@ class BackingTest : public ProspectiveParachainsTestHarness { .WillRepeatedly(Return(test_state.availability_cores)); EXPECT_CALL(*signer_factory_, at(hash)).WillRepeatedly(Return(signer_)); + EXPECT_CALL(*signer_factory_, getAuthorityValidatorIndex(hash)) + .WillRepeatedly(Return(0)); + + runtime::SessionInfo si; + si.validators = test_state.validators; + si.discovery_keys = test_state.validators; + EXPECT_CALL(*parachain_host_, + session_info(hash, test_state.signing_context.session_index)) + .WillRepeatedly(Return(si)); + + EXPECT_CALL(*parachain_host_, + node_features(hash, test_state.signing_context.session_index)) + .WillRepeatedly(Return(runtime::ParachainHost::NodeFeatures())); + + EXPECT_CALL( + *parachain_host_, + minimum_backing_votes(hash, test_state.signing_context.session_index)) + .WillRepeatedly(Return(test_state.minimum_backing_votes)); if (requested_len == 0) { EXPECT_CALL(*prospective_parachains_, diff --git a/test/core/parachain/parachain_test_harness.hpp b/test/core/parachain/parachain_test_harness.hpp index 7af6e9255e..d58ed3a620 100644 --- a/test/core/parachain/parachain_test_harness.hpp +++ b/test/core/parachain/parachain_test_harness.hpp @@ -64,6 +64,7 @@ class ProspectiveParachainsTestHarness : public testing::Test { static constexpr uint64_t ALLOWED_ANCESTRY_LEN = 3ull; static constexpr uint32_t MAX_POV_SIZE = 1000000; + static constexpr uint32_t LEGACY_MIN_BACKING_VOTES = 2; Hash hashFromStrData(std::span data) { return ghashFromStrData(hasher_, data); From e6c78b6a395304e11fce49002dedc6fbd5d2c59a Mon Sep 17 00:00:00 2001 From: iceseer Date: Thu, 5 Dec 2024 22:44:22 +0300 Subject: [PATCH 26/27] fixup Signed-off-by: iceseer --- .../validator/impl/parachain_processor.cpp | 12 ++++++++---- test/core/parachain/backing.cpp | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index ffd8627f3f..3977bddfa7 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -284,6 +284,7 @@ namespace kagome::parachain { REINVOKE(*main_pool_handler_, onViewUpdated, event); CHECK_OR_RET(canProcessParachains().has_value()); const auto &relay_parent = event.new_head.hash(); + std::cout << fmt::format("===> ACTIVE LEAF {}\n", relay_parent); /// init `prospective_parachains` subsystem if (const auto r = @@ -342,6 +343,7 @@ namespace kagome::parachain { mode ? std::move(pruned_h) : std::vector{removed}; for (const auto &removed : pruned) { + std::cout << fmt::format("---> PRUNED {}\n", removed); our_current_state_.state_by_relay_parent.erase(removed); { /// remove cancelations @@ -433,6 +435,7 @@ namespace kagome::parachain { ParachainProcessorImpl::construct_per_relay_parent_state( const primitives::BlockHash &relay_parent, const ProspectiveParachainsModeOpt &mode) { + std::cout << fmt::format("===> CONSTRUCT RELAY PARENT {}\n", relay_parent); /** * It first checks if our node is a parachain validator for the relay * parent. If it is not, it returns an error. If the node is a validator, it @@ -444,8 +447,6 @@ namespace kagome::parachain { * group. Finally, it returns a `RelayParentState` object that contains the * assignment, validator index, required collator, and table context. */ - std::cout << fmt::format("===> ask data with {}", relay_parent) - << std::endl; bool is_parachain_validator = false; ::libp2p::common::FinalAction metric_updater{ @@ -646,6 +647,7 @@ namespace kagome::parachain { ++it; } else { _keeper_.emplace_back(it->second.per_session_state); + std::cout << fmt::format("---> ERASED {}\n", it->first); it = our_current_state_.state_by_relay_parent.erase(it); } } @@ -701,9 +703,11 @@ namespace kagome::parachain { } for (const auto &maybe_new : fresh_relay_parents) { + //std::cout << fmt::format("---> MAYBE_NEW {}\n", maybe_new); if (our_current_state_.state_by_relay_parent.contains(maybe_new)) { continue; } + //std::cout << fmt::format("---> NEW {}\n", maybe_new); ProspectiveParachainsModeOpt mode_; if (auto l = utils::get(our_current_state_.per_leaf, maybe_new)) { @@ -715,12 +719,12 @@ namespace kagome::parachain { auto rps_result = construct_per_relay_parent_state(maybe_new, mode_); if (rps_result.has_value()) { our_current_state_.state_by_relay_parent.insert_or_assign( - relay_parent, std::move(rps_result.value())); + maybe_new, std::move(rps_result.value())); } else if (rps_result.error() != Error::KEY_NOT_PRESENT) { SL_TRACE( logger_, "Relay parent state was not created. (relay parent={}, error={})", - relay_parent, + maybe_new, rps_result.error()); } } diff --git a/test/core/parachain/backing.cpp b/test/core/parachain/backing.cpp index 18657991ad..fe2f5a699d 100644 --- a/test/core/parachain/backing.cpp +++ b/test/core/parachain/backing.cpp @@ -377,12 +377,22 @@ class BackingTest : public ProspectiveParachainsTestHarness { .allowed_ancestry_len = 3, })); + EXPECT_CALL(*signer_, validatorIndex()).WillRepeatedly(Return(0)); + + EXPECT_CALL(*bitfield_store_, printStoragesLoad()).WillRepeatedly(Return()); + EXPECT_CALL(*backing_store_, printStoragesLoad()).WillRepeatedly(Return()); + EXPECT_CALL(*av_store_, printStoragesLoad()).WillRepeatedly(Return()); + EXPECT_CALL(*prospective_parachains_, printStoragesLoad()).WillRepeatedly(Return()); + + // network::ExViewRef ev_ref{ // .new_head = {update.new_head}, // .lost = update.lost, // }; + EXPECT_CALL(*backing_store_, onActivateLeaf(testing::_)).WillRepeatedly(Return()); EXPECT_CALL(*prospective_parachains_, onActiveLeavesUpdate(testing::_)) .WillRepeatedly(Return(outcome::success())); + EXPECT_CALL(*peer_manager_, enumeratePeerState(testing::_)).WillRepeatedly(Return()); const BlockNumber min_min = [&]() -> BlockNumber { std::optional min_min; @@ -528,7 +538,9 @@ class BackingTest : public ProspectiveParachainsTestHarness { } void assert_validation_requests( - const runtime::ValidationCode &validation_code) {} + const runtime::ValidationCode &validation_code) { + + } void assert_validate_seconded_candidate( const Hash &relay_parent, @@ -661,4 +673,6 @@ TEST_F(BackingTest, seconding_sanity_check_allowed_on_all) { validation_code, expected_head_data, false); + + std::this_thread::sleep_for(std::chrono::seconds(10)); } From 74e5285857a831d97162626383820f7433a123ea Mon Sep 17 00:00:00 2001 From: iceseer Date: Fri, 6 Dec 2024 19:41:17 +0300 Subject: [PATCH 27/27] trace 2 Signed-off-by: iceseer trace Signed-off-by: iceseer Revert "[fix] prune" This reverts commit 3ad4f479c70bc31e98c9049b24eccf032593db12. [fix] prune Signed-off-by: iceseer --- .../validator/backing_implicit_view.cpp | 39 ++++++++++++++++++- .../validator/backing_implicit_view.hpp | 3 +- .../validator/impl/parachain_processor.cpp | 33 +++++++++++++++- .../validator/parachain_processor.hpp | 1 + .../prospective_parachains.cpp | 7 +++- 5 files changed, 77 insertions(+), 6 deletions(-) diff --git a/core/parachain/validator/backing_implicit_view.cpp b/core/parachain/validator/backing_implicit_view.cpp index 231b7567fa..cc472ca5cf 100644 --- a/core/parachain/validator/backing_implicit_view.cpp +++ b/core/parachain/validator/backing_implicit_view.cpp @@ -33,11 +33,11 @@ namespace kagome::parachain { std::weak_ptr prospective_parachains, std::shared_ptr parachain_host_, std::shared_ptr block_tree, - std::optional collating_for_) + std::optional collating_for_, bool trace_insertions) : parachain_host(std::move(parachain_host_)), collating_for{collating_for_}, prospective_parachains_{std::move(prospective_parachains)}, - block_tree_{std::move(block_tree)} { + block_tree_{std::move(block_tree)}, trace_insertions_(trace_insertions) { BOOST_ASSERT(!prospective_parachains_.expired()); BOOST_ASSERT(parachain_host); BOOST_ASSERT(block_tree_); @@ -87,6 +87,9 @@ namespace kagome::parachain { ancestors.size()); for (const auto &ancestor : ancestors) { + if (trace_insertions_) { + SL_TRACE(logger, "activate_leaf_from_prospective_parachains(ancestors): {}", ancestor.hash); + } block_info_storage.insert_or_assign( ancestor.hash, BlockInfo{ @@ -98,6 +101,9 @@ namespace kagome::parachain { ancestor.hash); } + if (trace_insertions_) { + SL_TRACE(logger, "activate_leaf_from_prospective_parachains: {} {}", leaf.hash, retain_minimum); + } block_info_storage.insert_or_assign( leaf.hash, BlockInfo{ @@ -123,6 +129,9 @@ namespace kagome::parachain { std::vector ImplicitView::deactivate_leaf(const Hash &leaf_hash) { std::vector removed; + if (trace_insertions_) { + SL_TRACE(logger, "deactivate_leaf(leaves): {}", leaf_hash); + } if (leaves.erase(leaf_hash) == 0ull) { return removed; } @@ -133,14 +142,28 @@ namespace kagome::parachain { minimum ? std::min(*minimum, l.retain_minimum) : l.retain_minimum; } + if (trace_insertions_) { + if (minimum) { + SL_TRACE(logger, "deactivate_leaf(--): {}", *minimum); + } else { + SL_TRACE(logger, "deactivate_leaf(--): no"); + } + } + for (auto it = block_info_storage.begin(); it != block_info_storage.end();) { const auto &[hash, i] = *it; const bool keep = minimum && i.block_number >= *minimum; if (keep) { + if (trace_insertions_) { + SL_TRACE(logger, "deactivate_leaf(-): {}", i.block_number); + } ++it; } else { removed.emplace_back(hash); + if (trace_insertions_) { + SL_TRACE(logger, "deactivate_leaf: {}", hash); + } it = block_info_storage.erase(it); } } @@ -157,6 +180,9 @@ namespace kagome::parachain { fetched.minimum_ancestor_number, math::sat_sub_unsigned(fetched.leaf_number, MINIMUM_RETAIN_LENGTH)); + if (trace_insertions_) { + SL_TRACE(logger, "activate_leaf(-): {} {}", fetched.minimum_ancestor_number, math::sat_sub_unsigned(fetched.leaf_number, MINIMUM_RETAIN_LENGTH)); + } leaves.insert_or_assign( leaf_hash, ActiveLeafPruningInfo{.retain_minimum = retain_minimum}); return outcome::success(); @@ -232,6 +258,9 @@ namespace kagome::parachain { min_min = std::min(x, min_min); } } + if (trace_insertions_) { + SL_TRACE(logger, "fetch_fresh_leaf_and_insert_ancestry(-): {} {}", min_min, leaf_header.number); + } const size_t expected_ancestry_len = math::sat_sub_unsigned(leaf_header.number, min_min) + 1ull; @@ -250,6 +279,9 @@ namespace kagome::parachain { parent_hash = it->second.parent_hash; } else { OUTCOME_TRY(header, block_tree->getBlockHeader(next_ancestor_hash)); + if (trace_insertions_) { + SL_TRACE(logger, "activate_leaf(next_ancestor_number): {}", next_ancestor_hash); + } block_info_storage.emplace( next_ancestor_hash, BlockInfo{ @@ -272,6 +304,9 @@ namespace kagome::parachain { ancestry.emplace_back(leaf_hash); } + if (trace_insertions_) { + SL_TRACE(logger, "activate_leaf: {}", leaf_hash); + } block_info_storage.emplace( leaf_hash, BlockInfo{ diff --git a/core/parachain/validator/backing_implicit_view.hpp b/core/parachain/validator/backing_implicit_view.hpp index b559a1cdbb..d45bf4f3e4 100644 --- a/core/parachain/validator/backing_implicit_view.hpp +++ b/core/parachain/validator/backing_implicit_view.hpp @@ -127,7 +127,7 @@ namespace kagome::parachain { ImplicitView(std::weak_ptr prospective_parachains, std::shared_ptr parachain_host_, std::shared_ptr block_tree, - std::optional collating_for_); + std::optional collating_for_, bool trace_insertions = false); private: struct ActiveLeafPruningInfo { @@ -163,6 +163,7 @@ namespace kagome::parachain { std::weak_ptr prospective_parachains_; std::shared_ptr block_tree_; + bool trace_insertions_; log::Logger logger = log::createLogger("BackingImplicitView", "parachain"); }; diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index 3977bddfa7..61d75969b2 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -274,6 +274,9 @@ namespace kagome::parachain { network::IPeerView::EventType::kViewUpdated, [WEAK_SELF](const network::ExView &event) { WEAK_LOCK(self); + for (const auto &l : self->block_tree_->getLeaves()) { + SL_TRACE(self->logger_, "EXISTED LEAF IN TRIE {}", l); + } self->onViewUpdated(event); }); @@ -282,9 +285,35 @@ namespace kagome::parachain { void ParachainProcessorImpl::onViewUpdated(const network::ExView &event) { REINVOKE(*main_pool_handler_, onViewUpdated, event); - CHECK_OR_RET(canProcessParachains().has_value()); + const auto &relay_parent = event.new_head.hash(); - std::cout << fmt::format("===> ACTIVE LEAF {}\n", relay_parent); + existed_leaves_[relay_parent] = event.new_head.number; + for (const auto &l : event.lost) { + existed_leaves_.erase(l); + } + + if ((event.new_head.number % 10) == 0) { + std::map> tmp; + for (const auto &[h, i] : existed_leaves_) { + tmp[i].emplace_back(h); + } + + size_t counter = 0; + for (const auto &[n, hs] : tmp) { + for (const auto &h : hs) { + SL_TRACE(logger_, "[PARACHAIN PROC]: ACTIVE LEAF {} - {}", n, h); + if (++counter >= 10) { + break; + } + } + if (counter >= 10) { + break; + } + } + } + + CHECK_OR_RET(canProcessParachains().has_value()); + SL_TRACE(logger_, "===> ACTIVE LEAF {}", relay_parent); /// init `prospective_parachains` subsystem if (const auto r = diff --git a/core/parachain/validator/parachain_processor.hpp b/core/parachain/validator/parachain_processor.hpp index 1eec064f6e..bd830fa029 100644 --- a/core/parachain/validator/parachain_processor.hpp +++ b/core/parachain/validator/parachain_processor.hpp @@ -821,6 +821,7 @@ namespace kagome::parachain { metrics::RegistryPtr metrics_registry_ = metrics::createRegistry(); metrics::Gauge *metric_is_parachain_validator_; + std::unordered_map existed_leaves_; public: void handle_second_message(const network::CandidateReceipt &candidate, diff --git a/core/parachain/validator/prospective_parachains/prospective_parachains.cpp b/core/parachain/validator/prospective_parachains/prospective_parachains.cpp index 3b5c44b50f..634b1d8ab7 100644 --- a/core/parachain/validator/prospective_parachains/prospective_parachains.cpp +++ b/core/parachain/validator/prospective_parachains/prospective_parachains.cpp @@ -324,6 +324,7 @@ namespace kagome::parachain { size_t ancestors) { std::vector block_info; if (ancestors == 0) { + SL_TRACE(logger, "`ancestors` is 0. Return. (relay parent={})", relay_hash); return block_info; } @@ -464,6 +465,9 @@ namespace kagome::parachain { }; OUTCOME_TRY(ancestry, fetchAncestry(hash, mode->allowed_ancestry_len)); + if (ancestry.empty()) { + SL_TRACE(logger, "Failed to get inclusion backing state. (relay parent={})", hash); + } std::optional>> @@ -600,6 +604,7 @@ namespace kagome::parachain { auto r = view().implicit_view.all_allowed_relay_parents(); std::unordered_set remaining{r.begin(), r.end()}; + SL_TRACE(logger, "Remains RP in PP: {}", remaining.size()); for (auto it = view().per_relay_parent.begin(); it != view().per_relay_parent.end();) { @@ -619,7 +624,7 @@ namespace kagome::parachain { .per_relay_parent = {}, .active_leaves = {}, .implicit_view = ImplicitView( - weak_from_this(), parachain_host_, block_tree_, std::nullopt), + weak_from_this(), parachain_host_, block_tree_, std::nullopt, true), }); } return *view_;