#![deny(unused_crate_dependencies)]
#![warn(missing_docs)]
use parity_scale_codec::{Decode, Encode};
use polkadot_primitives::v2::{BlockNumber, Hash};
use std::{collections::HashMap, fmt};
#[doc(hidden)]
pub use polkadot_node_jaeger as jaeger;
pub use sc_network::{IfDisconnected, PeerId};
#[doc(hidden)]
pub use std::sync::Arc;
mod reputation;
pub use self::reputation::{ReputationChange, UnifiedReputationChange};
pub mod peer_set;
pub mod request_response;
pub mod authority_discovery;
pub mod grid_topology;
pub const MIN_GOSSIP_PEERS: usize = 25;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WrongVariant;
impl fmt::Display for WrongVariant {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "Wrong message variant")
}
}
impl std::error::Error for WrongVariant {}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ObservedRole {
Light,
Full,
Authority,
}
impl From<sc_network::ObservedRole> for ObservedRole {
fn from(role: sc_network::ObservedRole) -> ObservedRole {
match role {
sc_network::ObservedRole::Light => ObservedRole::Light,
sc_network::ObservedRole::Authority => ObservedRole::Authority,
sc_network::ObservedRole::Full => ObservedRole::Full,
}
}
}
impl Into<sc_network::ObservedRole> for ObservedRole {
fn into(self) -> sc_network::ObservedRole {
match self {
ObservedRole::Light => sc_network::ObservedRole::Light,
ObservedRole::Full => sc_network::ObservedRole::Full,
ObservedRole::Authority => sc_network::ObservedRole::Authority,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct OurView {
view: View,
span_per_head: HashMap<Hash, Arc<jaeger::Span>>,
}
impl OurView {
pub fn new(
heads: impl IntoIterator<Item = (Hash, Arc<jaeger::Span>)>,
finalized_number: BlockNumber,
) -> Self {
let state_per_head = heads.into_iter().collect::<HashMap<_, _>>();
let view = View::new(state_per_head.keys().cloned(), finalized_number);
Self { view, span_per_head: state_per_head }
}
pub fn span_per_head(&self) -> &HashMap<Hash, Arc<jaeger::Span>> {
&self.span_per_head
}
}
impl PartialEq for OurView {
fn eq(&self, other: &Self) -> bool {
self.view == other.view
}
}
impl std::ops::Deref for OurView {
type Target = View;
fn deref(&self) -> &View {
&self.view
}
}
#[macro_export]
macro_rules! our_view {
( $( $hash:expr ),* $(,)? ) => {
$crate::OurView::new(
vec![ $( $hash.clone() ),* ].into_iter().map(|h| (h, $crate::Arc::new($crate::jaeger::Span::Disabled))),
0,
)
};
}
#[derive(Default, Debug, Clone, PartialEq, Eq, Encode, Decode)]
pub struct View {
heads: Vec<Hash>,
pub finalized_number: BlockNumber,
}
#[macro_export]
macro_rules! view {
( $( $hash:expr ),* $(,)? ) => {
$crate::View::new(vec![ $( $hash.clone() ),* ], 0)
};
}
impl View {
pub fn new(heads: impl IntoIterator<Item = Hash>, finalized_number: BlockNumber) -> Self {
let mut heads = heads.into_iter().collect::<Vec<Hash>>();
heads.sort();
Self { heads, finalized_number }
}
pub fn with_finalized(finalized_number: BlockNumber) -> Self {
Self { heads: Vec::new(), finalized_number }
}
pub fn len(&self) -> usize {
self.heads.len()
}
pub fn is_empty(&self) -> bool {
self.heads.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = &Hash> {
self.heads.iter()
}
pub fn into_iter(self) -> impl Iterator<Item = Hash> {
self.heads.into_iter()
}
pub fn replace_difference(&mut self, new: View) -> impl Iterator<Item = &Hash> {
let old = std::mem::replace(self, new);
self.heads.iter().filter(move |h| !old.contains(h))
}
pub fn difference<'a>(&'a self, other: &'a View) -> impl Iterator<Item = &'a Hash> + 'a {
self.heads.iter().filter(move |h| !other.contains(h))
}
pub fn intersection<'a>(&'a self, other: &'a View) -> impl Iterator<Item = &'a Hash> + 'a {
self.heads.iter().filter(move |h| other.contains(h))
}
pub fn contains(&self, hash: &Hash) -> bool {
self.heads.contains(hash)
}
pub fn check_heads_eq(&self, other: &Self) -> bool {
self.heads == other.heads
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Versioned<V1> {
V1(V1),
}
impl<V1: Clone> Versioned<&'_ V1> {
pub fn clone_inner(&self) -> Versioned<V1> {
match *self {
Versioned::V1(inner) => Versioned::V1(inner.clone()),
}
}
}
pub type VersionedValidationProtocol = Versioned<v1::ValidationProtocol>;
impl From<v1::ValidationProtocol> for VersionedValidationProtocol {
fn from(v1: v1::ValidationProtocol) -> Self {
VersionedValidationProtocol::V1(v1)
}
}
pub type VersionedCollationProtocol = Versioned<v1::CollationProtocol>;
impl From<v1::CollationProtocol> for VersionedCollationProtocol {
fn from(v1: v1::CollationProtocol) -> Self {
VersionedCollationProtocol::V1(v1)
}
}
macro_rules! impl_versioned_full_protocol_from {
($from:ty, $out:ty, $variant:ident) => {
impl From<$from> for $out {
fn from(versioned_from: $from) -> $out {
match versioned_from {
Versioned::V1(x) => Versioned::V1(x.into()),
}
}
}
};
}
macro_rules! impl_versioned_try_from {
($from:ty, $out:ty, $v1_pat:pat => $v1_out:expr) => {
impl TryFrom<$from> for $out {
type Error = crate::WrongVariant;
fn try_from(x: $from) -> Result<$out, Self::Error> {
#[allow(unreachable_patterns)] match x {
Versioned::V1($v1_pat) => Ok(Versioned::V1($v1_out)),
_ => Err(crate::WrongVariant),
}
}
}
impl<'a> TryFrom<&'a $from> for $out {
type Error = crate::WrongVariant;
fn try_from(x: &'a $from) -> Result<$out, Self::Error> {
#[allow(unreachable_patterns)] match x {
Versioned::V1($v1_pat) => Ok(Versioned::V1($v1_out.clone())),
_ => Err(crate::WrongVariant),
}
}
}
};
}
pub type BitfieldDistributionMessage = Versioned<v1::BitfieldDistributionMessage>;
impl_versioned_full_protocol_from!(
BitfieldDistributionMessage,
VersionedValidationProtocol,
BitfieldDistribution
);
impl_versioned_try_from!(
VersionedValidationProtocol,
BitfieldDistributionMessage,
v1::ValidationProtocol::BitfieldDistribution(x) => x
);
pub type StatementDistributionMessage = Versioned<v1::StatementDistributionMessage>;
impl_versioned_full_protocol_from!(
StatementDistributionMessage,
VersionedValidationProtocol,
StatementDistribution
);
impl_versioned_try_from!(
VersionedValidationProtocol,
StatementDistributionMessage,
v1::ValidationProtocol::StatementDistribution(x) => x
);
pub type ApprovalDistributionMessage = Versioned<v1::ApprovalDistributionMessage>;
impl_versioned_full_protocol_from!(
ApprovalDistributionMessage,
VersionedValidationProtocol,
ApprovalDistribution
);
impl_versioned_try_from!(
VersionedValidationProtocol,
ApprovalDistributionMessage,
v1::ValidationProtocol::ApprovalDistribution(x) => x
);
pub type GossipSupportNetworkMessage = Versioned<v1::GossipSupportNetworkMessage>;
impl TryFrom<VersionedValidationProtocol> for GossipSupportNetworkMessage {
type Error = WrongVariant;
fn try_from(_: VersionedValidationProtocol) -> Result<Self, Self::Error> {
Err(WrongVariant)
}
}
impl<'a> TryFrom<&'a VersionedValidationProtocol> for GossipSupportNetworkMessage {
type Error = WrongVariant;
fn try_from(_: &'a VersionedValidationProtocol) -> Result<Self, Self::Error> {
Err(WrongVariant)
}
}
pub type CollatorProtocolMessage = Versioned<v1::CollatorProtocolMessage>;
impl_versioned_full_protocol_from!(
CollatorProtocolMessage,
VersionedCollationProtocol,
CollatorProtocol
);
impl_versioned_try_from!(
VersionedCollationProtocol,
CollatorProtocolMessage,
v1::CollationProtocol::CollatorProtocol(x) => x
);
pub mod v1 {
use parity_scale_codec::{Decode, Encode};
use polkadot_primitives::v2::{
CandidateHash, CandidateIndex, CollatorId, CollatorSignature, CompactStatement, Hash,
Id as ParaId, UncheckedSignedAvailabilityBitfield, ValidatorIndex, ValidatorSignature,
};
use polkadot_node_primitives::{
approval::{IndirectAssignmentCert, IndirectSignedApprovalVote},
UncheckedSignedFullStatement,
};
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub enum BitfieldDistributionMessage {
#[codec(index = 0)]
Bitfield(Hash, UncheckedSignedAvailabilityBitfield),
}
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub enum StatementDistributionMessage {
#[codec(index = 0)]
Statement(Hash, UncheckedSignedFullStatement),
#[codec(index = 1)]
LargeStatement(StatementMetadata),
}
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq, Hash)]
pub struct StatementMetadata {
pub relay_parent: Hash,
pub candidate_hash: CandidateHash,
pub signed_by: ValidatorIndex,
pub signature: ValidatorSignature,
}
impl StatementDistributionMessage {
pub fn get_fingerprint(&self) -> (CompactStatement, ValidatorIndex) {
match self {
Self::Statement(_, statement) => (
statement.unchecked_payload().to_compact(),
statement.unchecked_validator_index(),
),
Self::LargeStatement(meta) =>
(CompactStatement::Seconded(meta.candidate_hash), meta.signed_by),
}
}
pub fn get_signature(&self) -> ValidatorSignature {
match self {
Self::Statement(_, statement) => statement.unchecked_signature().clone(),
Self::LargeStatement(metadata) => metadata.signature.clone(),
}
}
pub fn get_relay_parent(&self) -> Hash {
match self {
Self::Statement(r, _) => *r,
Self::LargeStatement(meta) => meta.relay_parent,
}
}
pub fn is_large_statement(&self) -> bool {
if let Self::LargeStatement(_) = self {
true
} else {
false
}
}
}
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub enum ApprovalDistributionMessage {
#[codec(index = 0)]
Assignments(Vec<(IndirectAssignmentCert, CandidateIndex)>),
#[codec(index = 1)]
Approvals(Vec<IndirectSignedApprovalVote>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GossipSupportNetworkMessage {}
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub enum CollatorProtocolMessage {
#[codec(index = 0)]
Declare(CollatorId, ParaId, CollatorSignature),
#[codec(index = 1)]
AdvertiseCollation(Hash),
#[codec(index = 4)]
CollationSeconded(Hash, UncheckedSignedFullStatement),
}
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq, derive_more::From)]
pub enum ValidationProtocol {
#[codec(index = 1)]
#[from]
BitfieldDistribution(BitfieldDistributionMessage),
#[codec(index = 3)]
#[from]
StatementDistribution(StatementDistributionMessage),
#[codec(index = 4)]
#[from]
ApprovalDistribution(ApprovalDistributionMessage),
}
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq, derive_more::From)]
pub enum CollationProtocol {
#[codec(index = 0)]
#[from]
CollatorProtocol(CollatorProtocolMessage),
}
pub fn declare_signature_payload(peer_id: &sc_network::PeerId) -> Vec<u8> {
let mut payload = peer_id.to_bytes();
payload.extend_from_slice(b"COLL");
payload
}
}