From 646365e1e5e997ae42bfcbee27294249581d1f4b Mon Sep 17 00:00:00 2001 From: Ruslan Tushov Date: Wed, 29 Jan 2025 16:31:53 +0500 Subject: [PATCH] fix initial ViewUpdate (#2354) Signed-off-by: turuslan Co-authored-by: kamilsa --- core/network/impl/peer_view.cpp | 34 ++++++++++++----------- core/network/impl/protocols/parachain.cpp | 5 ++-- core/network/peer_view.hpp | 5 ++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/core/network/impl/peer_view.cpp b/core/network/impl/peer_view.cpp index 91147ce511..58ee637abf 100644 --- a/core/network/impl/peer_view.cpp +++ b/core/network/impl/peer_view.cpp @@ -12,15 +12,24 @@ namespace kagome::network { static constexpr size_t MAX_VIEW_HEADS = 5; - inline std::pair> makeView( + /** + * @returns `View` with all leaves, `View` with `MAX_VIEW_HEAD` leaves + */ + inline std::pair makeViews( const LazySPtr &block_tree) { + std::pair result; + auto &[view, stripped_view] = result; + auto &heads_ = stripped_view.heads_; + auto last_finalized = block_tree.get()->getLastFinalized().number; + view.finalized_number_ = last_finalized; + stripped_view.finalized_number_ = last_finalized; + auto heads = block_tree.get()->getLeavesInfo(); std::ranges::sort(heads, [](const auto &l, const auto &r) { return l < r; }); - std::vector heads_; heads_.reserve(std::min(MAX_VIEW_HEADS, heads.size())); for (const auto &head : heads | std::views::reverse | std::views::take(MAX_VIEW_HEADS)) { @@ -29,16 +38,13 @@ namespace kagome::network { std::ranges::sort(heads_); assert(heads_.size() <= MAX_VIEW_HEADS); - View view{ - .heads_ = {}, - .finalized_number_ = last_finalized, - }; + view.heads_.reserve(heads.size()); std::ranges::transform(heads, std::back_inserter(view.heads_), [](const auto &data) { return data.hash; }); std::ranges::sort(view.heads_); - return {view, heads_}; + return result; } PeerView::PeerView( @@ -50,8 +56,8 @@ namespace kagome::network { my_view_update_observable_{ std::make_shared()}, remote_view_update_observable_{ - std::make_shared()}, - my_view_{makeView(block_tree_).first} { + std::make_shared()} { + std::tie(my_view_, my_view_stripped_) = makeViews(block_tree_); app_state_manager->takeControl(*this); } @@ -79,21 +85,17 @@ namespace kagome::network { void PeerView::updateMyView(const primitives::BlockHeader &header) { BOOST_ASSERT(my_view_update_observable_); - auto [view, stripped_view] = makeView(block_tree_); - const auto last_finalized = view.finalized_number_; + auto [view, stripped_view] = makeViews(block_tree_); ExView event{ .view = std::move(view), - .stripped_view = - View{ - .heads_ = std::move(stripped_view), - .finalized_number_ = last_finalized, - }, + .stripped_view = stripped_view, .new_head = header, .lost = {}, }; if (event.view == my_view_) { return; } + my_view_stripped_ = std::move(stripped_view); for (const auto &head : my_view_.heads_) { if (not event.view.contains(head)) { event.lost.emplace_back(head); diff --git a/core/network/impl/protocols/parachain.cpp b/core/network/impl/protocols/parachain.cpp index 6010d72fde..72fc8f7816 100644 --- a/core/network/impl/protocols/parachain.cpp +++ b/core/network/impl/protocols/parachain.cpp @@ -19,8 +19,7 @@ // TODO(turuslan): https://github.com/qdrvm/kagome/issues/1989 #define PROTOCOL_V1(protocol) \ - { \ - } + {} namespace kagome::network { // https://github.com/paritytech/polkadot-sdk/blob/edf79aa972bcf2e043e18065a9bb860ecdbd1a6e/polkadot/node/network/protocol/src/peer_set.rs#L118-L119 @@ -88,7 +87,7 @@ namespace kagome::network { collation_versions_.at(protocol_group); if (out) { notifications_->write( - peer_id, protocol_group, encodeView(peer_view_->getMyView())); + peer_id, protocol_group, encodeView(peer_view_->getMyViewStripped())); } return true; } diff --git a/core/network/peer_view.hpp b/core/network/peer_view.hpp index b91fb410fa..4bd26367aa 100644 --- a/core/network/peer_view.hpp +++ b/core/network/peer_view.hpp @@ -84,8 +84,8 @@ namespace kagome::network { void removePeer(const PeerId &peer_id); void updateRemoteView(const PeerId &peer_id, network::View &&view); - auto &getMyView() const { - return my_view_; + auto &getMyViewStripped() const { + return my_view_stripped_; } private: @@ -98,6 +98,7 @@ namespace kagome::network { PeerViewSubscriptionEnginePtr remote_view_update_observable_; View my_view_; + View my_view_stripped_; SafeObject> remote_view_; };