Move block announcement protocol config out of Protocol (#12441)

* Move Role(s) to `sc-network-common`

* Introduce `NotificationHandshake` type

* Move block announce protocol config creation to `ChainSync`

* Include block announcement into `notification_protocols`

* Apply review comments

* Remove unneeded include

* Add missing include

* Apply review comments
This commit is contained in:
Aaro Altonen
2022-10-10 10:10:53 +03:00
committed by GitHub
parent 0b77060986
commit ce9ce49bc6
23 changed files with 439 additions and 263 deletions
@@ -20,6 +20,7 @@
use crate::protocol;
use codec::Encode;
use libp2p::{multiaddr, Multiaddr, PeerId};
use std::{fmt, str, str::FromStr};
@@ -199,6 +200,30 @@ impl Default for SetConfig {
}
}
/// Custom handshake for the notification protocol
#[derive(Debug, Clone)]
pub struct NotificationHandshake(Vec<u8>);
impl NotificationHandshake {
/// Create new `NotificationHandshake` from an object that implements `Encode`
pub fn new<H: Encode>(handshake: H) -> Self {
Self(handshake.encode())
}
/// Create new `NotificationHandshake` from raw bytes
pub fn from_bytes(bytes: Vec<u8>) -> Self {
Self(bytes)
}
}
impl std::ops::Deref for NotificationHandshake {
type Target = Vec<u8>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
/// Extension to [`SetConfig`] for sets that aren't the default set.
///
/// > **Note**: As new fields might be added in the future, please consider using the `new` method
@@ -218,6 +243,8 @@ pub struct NonDefaultSetConfig {
/// If a fallback is used, it will be reported in
/// `sc_network::protocol::event::Event::NotificationStreamOpened::negotiated_fallback`
pub fallback_names: Vec<protocol::ProtocolName>,
/// Handshake of the protocol
pub handshake: Option<NotificationHandshake>,
/// Maximum allowed size of single notifications.
pub max_notification_size: u64,
/// Base configuration.
@@ -231,6 +258,7 @@ impl NonDefaultSetConfig {
notifications_protocol,
max_notification_size,
fallback_names: Vec::new(),
handshake: None,
set_config: SetConfig {
in_peers: 0,
out_peers: 0,