Skip to content

Commit

Permalink
broadcast neighbor message (#1735)
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <turuslan.devbox@gmail.com>
Co-authored-by: kamilsa <kamilsa16@gmail.com>
  • Loading branch information
turuslan and kamilsa authored Aug 18, 2023
1 parent 55ecf8a commit 8534ed5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
52 changes: 19 additions & 33 deletions core/network/impl/protocols/grandpa_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,26 +168,16 @@ namespace kagome::network {
stream->remotePeerId().value());

// Send neighbor message first
auto own_peer_state =
self->peer_manager_->getPeerState(self->own_info_.id);
if (own_peer_state.has_value()) {
GrandpaNeighborMessage msg{
.round_number =
own_peer_state->get().round_number.value_or(1),
.voter_set_id = own_peer_state->get().set_id.value_or(0),
.last_finalized = own_peer_state->get().last_finalized};

SL_DEBUG(self->base_.logger(),
"Send initial neighbor message: grandpa round number {}",
msg.round_number);

auto shared_msg =
KAGOME_EXTRACT_SHARED_CACHE(GrandpaProtocol, GrandpaMessage);
(*shared_msg) = GrandpaMessage(std::move(msg));

self->stream_engine_->send(
stream->remotePeerId().value(), self, std::move(shared_msg));
}
SL_DEBUG(self->base_.logger(),
"Send initial neighbor message: grandpa round number {}",
self->last_neighbor_.round_number);

auto shared_msg =
KAGOME_EXTRACT_SHARED_CACHE(GrandpaProtocol, GrandpaMessage);
(*shared_msg) = self->last_neighbor_;

self->stream_engine_->send(
stream->remotePeerId().value(), self, std::move(shared_msg));

cb(std::move(stream));
};
Expand Down Expand Up @@ -449,13 +439,20 @@ namespace kagome::network {
}

void GrandpaProtocol::neighbor(GrandpaNeighborMessage &&msg) {
if (msg == last_neighbor_) {
return;
}
auto set_changed = msg.voter_set_id != last_neighbor_.voter_set_id;
last_neighbor_ = msg;

SL_DEBUG(base_.logger(),
"Send neighbor message: grandpa round number {}",
msg.round_number);

peer_manager_->updatePeerState(own_info_.id, msg);

auto filter = [this,
set_changed,
set_id = msg.voter_set_id,
round_number = msg.round_number](const PeerId &peer_id) {
auto info_opt = peer_manager_->getPeerState(peer_id);
Expand All @@ -470,19 +467,8 @@ namespace kagome::network {
}
const auto &info = info_opt.value().get();

if (info.roles.flags.light and info.set_id.has_value()) {
// Neighbor message will be sent for light clients only when they lag by
// voter-set
if (info.set_id >= set_id) {
SL_DEBUG(
base_.logger(),
"Neighbor message with set_id={} in round={} has not been sent "
"to {}: peer is light-client and his voter-set id same or bigger",
set_id,
round_number,
peer_id);
return false;
}
if (not set_changed and info.roles.flags.light) {
return false;
}

return true;
Expand Down
2 changes: 2 additions & 0 deletions core/network/impl/protocols/grandpa_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ namespace kagome::network {
consensus::grandpa::VoterSetId>>
recent_catchup_requests_by_round_;

GrandpaNeighborMessage last_neighbor_{};

std::set<libp2p::peer::PeerId> recent_catchup_requests_by_peer_;
};

Expand Down

0 comments on commit 8534ed5

Please sign in to comment.