Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Feature/grid view #2007

Merged
merged 46 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
aac758a
gv
iceseer Mar 19, 2024
bd3e992
ref
iceseer Mar 20, 2024
b8589b1
format
iceseer Mar 20, 2024
142cf0a
1
iceseer Mar 21, 2024
c134062
1
iceseer Mar 21, 2024
8f5b6f0
1
iceseer Mar 22, 2024
d934872
1
iceseer Mar 22, 2024
c58a68d
handle_incoming_manifest_common
iceseer Mar 24, 2024
20f4e25
2
iceseer Mar 25, 2024
c3fb1b8
2
iceseer Mar 25, 2024
cb47aec
2
iceseer Mar 25, 2024
26fbe2d
3
iceseer Mar 26, 2024
379e515
2
iceseer Mar 27, 2024
29b8e48
2
iceseer Mar 28, 2024
a6367ea
2
iceseer Mar 29, 2024
bd3653b
2
iceseer Mar 29, 2024
16e3706
2
iceseer Mar 29, 2024
a962179
formatting
iceseer Mar 29, 2024
7621937
send_cluster_candidate_statements
iceseer Mar 31, 2024
66d0004
circulate_statement
iceseer Apr 1, 2024
1206a0c
fixup!
iceseer Apr 4, 2024
e7051d9
1
iceseer Apr 5, 2024
a05e752
1
iceseer Apr 5, 2024
7cb5f36
fixup
iceseer Apr 5, 2024
8f139bc
fixup
iceseer Apr 5, 2024
1cc5541
Merge remote-tracking branch 'origin/master' into feature/grid_view
iceseer Apr 7, 2024
872a08f
fixup
iceseer Apr 8, 2024
e9b48a0
fixup
iceseer Apr 8, 2024
c4a637a
test no filter
iceseer Apr 9, 2024
56a3e1c
cores fixup
iceseer Apr 9, 2024
2c25137
Merge branch 'master' into feature/grid_view
iceseer Apr 10, 2024
dc026a1
clean
iceseer Apr 12, 2024
02e8789
Merge remote-tracking branch 'origin/master' into feature/grid_view
iceseer Apr 12, 2024
d226d8b
clean
iceseer Apr 12, 2024
32c4420
Merge branch 'master' into feature/grid_view
iceseer Apr 16, 2024
2f9dc2f
Merge branch 'master' into feature/grid_view
iceseer Apr 17, 2024
903597c
mac os build fixup
iceseer Apr 17, 2024
93984ca
issues
iceseer Apr 24, 2024
aa60981
issues
iceseer Apr 24, 2024
e1b45e2
Merge branch 'master' into feature/grid_view
iceseer Apr 24, 2024
400adde
issues
iceseer Apr 24, 2024
a7e5a4e
Merge branch 'master' into feature/grid_view
iceseer Apr 25, 2024
7cf14da
Issues fixes
iceseer Apr 30, 2024
788c572
Merge branch 'master' into feature/grid_view
iceseer Apr 30, 2024
4c3224c
clang fixes
iceseer Apr 30, 2024
f8d5286
clang fixes
iceseer Apr 30, 2024
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
1 change: 1 addition & 0 deletions core/authority_discovery/query/query_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ namespace kagome::authority_discovery {
signed_record_pb.size())) {
return Error::DECODE_ERROR;
}

libp2p::crypto::ProtobufKey protobuf_key{
common::Buffer{str2byte(signed_record.peer_signature().public_key())}};
OUTCOME_TRY(peer_key, key_marshaller_->unmarshalPublicKey(protobuf_key));
Expand Down
10 changes: 10 additions & 0 deletions core/network/impl/peer_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,16 @@ namespace kagome::network {
return it->second;
}

void PeerManagerImpl::enumeratePeerState(const PeersCallback &callback) {
if (nullptr != callback) {
for (auto &[peer, state] : peer_states_) {
if (!callback(peer, state)) {
break;
}
}
}
}

std::optional<std::reference_wrapper<const PeerState>>
PeerManagerImpl::getPeerState(const PeerId &peer_id) const {
auto it = peer_states_.find(peer_id);
Expand Down
2 changes: 2 additions & 0 deletions core/network/impl/peer_manager_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ namespace kagome::network {
std::optional<std::reference_wrapper<const PeerState>> getPeerState(
const PeerId &peer_id) const override;

void enumeratePeerState(const PeersCallback &callback) override;

private:
/// Right way to check self peer as it takes into account dev mode
bool isSelfPeer(const PeerId &peer_id) const;
Expand Down
85 changes: 8 additions & 77 deletions core/network/peer_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,93 +12,18 @@
#include <libp2p/peer/peer_id.hpp>
#include <libp2p/peer/peer_info.hpp>

#include "network/peer_state.hpp"
#include "network/types/block_announce.hpp"
#include "network/types/block_announce_handshake.hpp"
#include "network/types/collator_messages_vstaging.hpp"
#include "network/types/grandpa_message.hpp"
#include "outcome/outcome.hpp"
#include "parachain/validator/backing_implicit_view.hpp"
#include "primitives/common.hpp"
#include "utils/lru.hpp"
#include "utils/non_copyable.hpp"

namespace kagome::network {
constexpr size_t kPeerStateMaxKnownBlocks = 1024;
constexpr size_t kPeerStateMaxKnownGrandpaMessages = 8192;

struct CollatingPeerState {
network::ParachainId para_id;
network::CollatorPublicKey collator_id;
std::unordered_map<Hash, std::unordered_set<CandidateHash>> advertisements;
std::chrono::system_clock::time_point last_active;
};

struct CollationEvent {
CollatorId collator_id;
struct {
RelayHash relay_parent;
network::ParachainId para_id;
libp2p::peer::PeerId peer_id;
std::optional<Hash> commitments_hash;
} pending_collation;
};

using OurView = network::View;

struct PeerStateCompact {
std::optional<RoundNumber> round_number;
std::optional<VoterSetId> set_id;
BlockNumber last_finalized;
};

struct PeerState {
clock::SteadyClock::TimePoint time;
Roles roles = 0;
BlockInfo best_block = {0, {}};
std::optional<RoundNumber> round_number = std::nullopt;
std::optional<VoterSetId> set_id = std::nullopt;
BlockNumber last_finalized = 0;
std::optional<CollatingPeerState> collator_state = std::nullopt;
std::optional<View> view;
CollationVersion version;
LruSet<primitives::BlockHash> known_blocks{kPeerStateMaxKnownBlocks};
LruSet<common::Hash256> known_grandpa_messages{
kPeerStateMaxKnownGrandpaMessages,
};

bool hasAdvertised(
const RelayHash &relay_parent,
const std::optional<CandidateHash> &maybe_candidate_hash) const {
if (!collator_state) {
return false;
}

const auto &collating_state = *collator_state;
if (maybe_candidate_hash) {
if (auto it = collating_state.advertisements.find(relay_parent);
it != collating_state.advertisements.end()) {
return it->second.contains(*maybe_candidate_hash);
}
return false;
}
return collating_state.advertisements.contains(relay_parent);
}

PeerStateCompact compact() const {
return PeerStateCompact{
.round_number = round_number,
.set_id = set_id,
.last_finalized = last_finalized,
};
}
};

inline std::optional<PeerStateCompact> compactFromRefToOwn(
const std::optional<std::reference_wrapper<PeerState>> &opt_ref) {
if (opt_ref) {
return opt_ref->get().compact();
}
return std::nullopt;
}

struct StreamEngine;

Expand All @@ -116,6 +41,7 @@ namespace kagome::network {
using BlockInfo = primitives::BlockInfo;
using AdvResult = outcome::result<
std::pair<const network::CollatorPublicKey &, network::ParachainId>>;
using PeersCallback = std::function<bool(const PeerId &, PeerState &)>;

virtual ~PeerManager() = default;

Expand Down Expand Up @@ -196,6 +122,11 @@ namespace kagome::network {
virtual std::optional<std::reference_wrapper<const PeerState>> getPeerState(
const PeerId &peer_id) const = 0;

/**
* Apply callback to each PeerState.
*/
virtual void enumeratePeerState(const PeersCallback &callback) = 0;
iceseer marked this conversation as resolved.
Show resolved Hide resolved

/**
* @returns number of active peers
*/
Expand Down
164 changes: 164 additions & 0 deletions core/network/peer_state.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <chrono>
#include <optional>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <libp2p/peer/peer_id.hpp>
#include <libp2p/peer/peer_info.hpp>

#include "consensus/grandpa/common.hpp"
#include "network/types/collator_messages_vstaging.hpp"
#include "outcome/outcome.hpp"
#include "parachain/validator/backing_implicit_view.hpp"
#include "primitives/common.hpp"
#include "utils/lru.hpp"

namespace kagome::network {

constexpr size_t kPeerStateMaxKnownBlocks = 1024;
constexpr size_t kPeerStateMaxKnownGrandpaMessages = 8192;
using RoundNumber = consensus::grandpa::RoundNumber;
using VoterSetId = consensus::grandpa::VoterSetId;
using BlockInfo = primitives::BlockInfo;

struct CollatingPeerState {
network::ParachainId para_id;
network::CollatorPublicKey collator_id;
std::unordered_map<Hash, std::unordered_set<CandidateHash>> advertisements;
std::chrono::system_clock::time_point last_active;
};

struct CollationEvent {
CollatorId collator_id;
struct {
RelayHash relay_parent;
network::ParachainId para_id;
libp2p::peer::PeerId peer_id;
std::optional<Hash> commitments_hash;
} pending_collation;
};

using OurView = network::View;

struct PeerStateCompact {
std::optional<RoundNumber> round_number;
std::optional<VoterSetId> set_id;
BlockNumber last_finalized;
};

struct PeerState {
clock::SteadyClock::TimePoint time;
Roles roles = 0;
BlockInfo best_block = {0, {}};
std::optional<RoundNumber> round_number = std::nullopt;
std::optional<VoterSetId> set_id = std::nullopt;
BlockNumber last_finalized = 0;
LruSet<primitives::BlockHash> known_blocks{kPeerStateMaxKnownBlocks};
LruSet<common::Hash256> known_grandpa_messages{
kPeerStateMaxKnownGrandpaMessages,
};

/// @brief parachain peer state
std::optional<CollatingPeerState> collator_state = std::nullopt;
View view;
std::unordered_set<common::Hash256> implicit_view;
std::optional<CollationVersion> version;

/// Update the view, returning a vector of implicit relay-parents which
/// weren't previously part of the view.
std::vector<common::Hash256> update_view(
const View &new_view, const parachain::ImplicitView &local_implicit) {
std::unordered_set<common::Hash256> next_implicit;
for (const auto &x : new_view.heads_) {
auto t = local_implicit.knownAllowedRelayParentsUnder(x, std::nullopt);
next_implicit.insert(t.begin(), t.end());
}

std::vector<common::Hash256> fresh_implicit;
for (const auto &x : next_implicit) {
if (implicit_view.find(x) == implicit_view.end()) {
fresh_implicit.emplace_back(x);
}
}

view = new_view;
implicit_view = next_implicit;
return fresh_implicit;
}

/// Whether we know that a peer knows a relay-parent. The peer knows the
/// relay-parent if it is either implicit or explicit in their view.
/// However, if it is implicit via an active-leaf we don't recognize, we
/// will not accurately be able to recognize them as 'knowing' the
/// relay-parent.
bool knows_relay_parent(const common::Hash256 &relay_parent) {
return implicit_view.contains(relay_parent)
|| view.contains(relay_parent);
}

/// Attempt to reconcile the view with new information about the implicit
/// relay parents under an active leaf.
std::vector<common::Hash256> reconcile_active_leaf(
const common::Hash256 &leaf_hash,
std::span<const common::Hash256> implicit) {
if (!view.contains(leaf_hash)) {
return {};
}

std::vector<common::Hash256> v;
v.reserve(implicit.size());
for (const auto &i : implicit) {
auto [_, inserted] = implicit_view.insert(i);
if (inserted) {
v.emplace_back(i);
}
}
return v;
}

/// Whether the peer has advertised the given collation.
bool hasAdvertised(
const RelayHash &relay_parent,
const std::optional<CandidateHash> &maybe_candidate_hash) const {
if (!collator_state) {
return false;
}

const auto &collating_state = *collator_state;
if (maybe_candidate_hash) {
if (auto it = collating_state.advertisements.find(relay_parent);
it != collating_state.advertisements.end()) {
return it->second.contains(*maybe_candidate_hash);
}
return false;
}
return collating_state.advertisements.contains(relay_parent);
}

PeerStateCompact compact() const {
return PeerStateCompact{
.round_number = round_number,
.set_id = set_id,
.last_finalized = last_finalized,
};
}
};

inline std::optional<PeerStateCompact> compactFromRefToOwn(
const std::optional<std::reference_wrapper<PeerState>> &opt_ref) {
if (opt_ref) {
return opt_ref->get().compact();
}
return std::nullopt;
}

} // namespace kagome::network
Loading
Loading