mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 07:01:05 +00:00
Throw an error if a bootnode is registered with two different peer ids (#3891)
* Throw an error if a bootnode is registered with two different peer ids * Rename error * Fix compilation :( * Review feedback
This commit is contained in:
@@ -18,16 +18,42 @@
|
||||
|
||||
use client;
|
||||
|
||||
use libp2p::{PeerId, Multiaddr};
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// Result type alias for the network.
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Error type for the network.
|
||||
#[derive(Debug, derive_more::Display, derive_more::From)]
|
||||
#[derive(derive_more::Display, derive_more::From)]
|
||||
pub enum Error {
|
||||
/// Io error
|
||||
Io(std::io::Error),
|
||||
/// Client error
|
||||
Client(client::error::Error),
|
||||
/// The same bootnode (based on address) is registered with two different peer ids.
|
||||
#[display(
|
||||
fmt = "The same bootnode (`{}`) is registered with two different peer ids: `{}` and `{}`",
|
||||
address,
|
||||
first_id,
|
||||
second_id,
|
||||
)]
|
||||
DuplicateBootnode {
|
||||
/// The address of the bootnode.
|
||||
address: Multiaddr,
|
||||
/// The first peer id that was found for the bootnode.
|
||||
first_id: PeerId,
|
||||
/// The second peer id that was found for the bootnode.
|
||||
second_id: PeerId,
|
||||
},
|
||||
}
|
||||
|
||||
// Make `Debug` use the `Display` implementation.
|
||||
impl fmt::Debug for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
@@ -35,6 +61,7 @@ impl std::error::Error for Error {
|
||||
match self {
|
||||
Error::Io(ref err) => Some(err),
|
||||
Error::Client(ref err) => Some(err),
|
||||
Error::DuplicateBootnode { .. } => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,14 +52,11 @@ use crate::protocol::specialization::NetworkSpecialization;
|
||||
use crate::protocol::sync::SyncState;
|
||||
|
||||
/// Minimum Requirements for a Hash within Networking
|
||||
pub trait ExHashT:
|
||||
::std::hash::Hash + Eq + ::std::fmt::Debug + Clone + Send + Sync + 'static
|
||||
{
|
||||
}
|
||||
pub trait ExHashT: std::hash::Hash + Eq + std::fmt::Debug + Clone + Send + Sync + 'static {}
|
||||
|
||||
impl<T> ExHashT for T where
|
||||
T: ::std::hash::Hash + Eq + ::std::fmt::Debug + Clone + Send + Sync + 'static
|
||||
{
|
||||
}
|
||||
T: std::hash::Hash + Eq + std::fmt::Debug + Clone + Send + Sync + 'static
|
||||
{}
|
||||
|
||||
/// Transaction pool interface
|
||||
pub trait TransactionPool<H: ExHashT, B: BlockT>: Send + Sync {
|
||||
@@ -152,6 +149,23 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkWorker
|
||||
}
|
||||
}
|
||||
|
||||
// Check for duplicate bootnodes.
|
||||
known_addresses.iter()
|
||||
.try_for_each(|(peer_id, addr)|
|
||||
if let Some(other) = known_addresses
|
||||
.iter()
|
||||
.find(|o| o.1 == *addr && o.0 != *peer_id)
|
||||
{
|
||||
Err(Error::DuplicateBootnode {
|
||||
address: addr.clone(),
|
||||
first_id: peer_id.clone(),
|
||||
second_id: other.0.clone(),
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
)?;
|
||||
|
||||
// Initialize the reserved peers.
|
||||
for reserved in params.network_config.reserved_nodes.iter() {
|
||||
if let Ok((peer_id, addr)) = parse_str_addr(reserved) {
|
||||
@@ -553,8 +567,9 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkServic
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT>
|
||||
::consensus::SyncOracle for NetworkService<B, S, H> {
|
||||
impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> consensus::SyncOracle
|
||||
for NetworkService<B, S, H>
|
||||
{
|
||||
fn is_major_syncing(&mut self) -> bool {
|
||||
NetworkService::is_major_syncing(self)
|
||||
}
|
||||
@@ -564,8 +579,9 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT>
|
||||
::consensus::SyncOracle for &'a NetworkService<B, S, H> {
|
||||
impl<'a, B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> consensus::SyncOracle
|
||||
for &'a NetworkService<B, S, H>
|
||||
{
|
||||
fn is_major_syncing(&mut self) -> bool {
|
||||
NetworkService::is_major_syncing(self)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user