Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix ViewUpdate #2354

Merged
merged 5 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions core/network/impl/peer_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@
namespace kagome::network {
static constexpr size_t MAX_VIEW_HEADS = 5;

inline std::pair<View, std::vector<primitives::BlockHash>> makeView(
/**
* @returns `View` with all leaves, `View` with `MAX_VIEW_HEAD` leaves
*/
inline std::pair<View, View> makeViews(
const LazySPtr<blockchain::BlockTree> &block_tree) {
std::pair<View, View> 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<primitives::BlockHash> heads_;
heads_.reserve(std::min(MAX_VIEW_HEADS, heads.size()));
for (const auto &head :
heads | std::views::reverse | std::views::take(MAX_VIEW_HEADS)) {
Expand All @@ -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(
Expand All @@ -50,8 +56,8 @@ namespace kagome::network {
my_view_update_observable_{
std::make_shared<MyViewSubscriptionEngine>()},
remote_view_update_observable_{
std::make_shared<PeerViewSubscriptionEngine>()},
my_view_{makeView(block_tree_).first} {
std::make_shared<PeerViewSubscriptionEngine>()} {
std::tie(my_view_, my_view_stripped_) = makeViews(block_tree_);
app_state_manager->takeControl(*this);
}

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions core/network/impl/protocols/parachain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions core/network/peer_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -98,6 +98,7 @@ namespace kagome::network {
PeerViewSubscriptionEnginePtr remote_view_update_observable_;

View my_view_;
View my_view_stripped_;
SafeObject<std::unordered_map<PeerId, View>> remote_view_;
};

Expand Down
Loading