Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 57e3486

Browse files
authored
Network sync refactoring (part 7) (#12006)
* Move `MultiaddrWithPeerId` and related parsing functions into `sc-network-common`, remove dependency on `sc-network` from `sc-chain-spec` * Remove dependency on `sc-network` from `sc-offchain` * Remove dependency on `sc-network` from `sc-network-gossip`
1 parent 1498d86 commit 57e3486

File tree

23 files changed

+173
-165
lines changed

23 files changed

+173
-165
lines changed

Cargo.lock

+6-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/authority-discovery/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ thiserror = "1.0"
2929
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.10.0-dev", path = "../../utils/prometheus" }
3030
sc-client-api = { version = "4.0.0-dev", path = "../api" }
3131
sc-network-common = { version = "0.10.0-dev", path = "../network/common" }
32-
sc-network = { version = "0.10.0-dev", path = "../network" }
3332
sp-api = { version = "4.0.0-dev", path = "../../primitives/api" }
3433
sp-authority-discovery = { version = "4.0.0-dev", path = "../../primitives/authority-discovery" }
3534
sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" }

client/authority-discovery/src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub enum Error {
5757
ParsingMultiaddress(#[from] libp2p::core::multiaddr::Error),
5858

5959
#[error("Failed to parse a libp2p key.")]
60-
ParsingLibp2pIdentity(#[from] sc_network::DecodingError),
60+
ParsingLibp2pIdentity(#[from] libp2p::identity::error::DecodingError),
6161

6262
#[error("Failed to sign using a specific public key.")]
6363
MissingSignature(CryptoTypePublicPair),

client/chain-spec/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ memmap2 = "0.5.0"
1919
serde = { version = "1.0.136", features = ["derive"] }
2020
serde_json = "1.0.79"
2121
sc-chain-spec-derive = { version = "4.0.0-dev", path = "./derive" }
22-
sc-network = { version = "0.10.0-dev", path = "../network" }
22+
sc-network-common = { version = "0.10.0-dev", path = "../network/common" }
2323
sc-telemetry = { version = "4.0.0-dev", path = "../telemetry" }
2424
sp-core = { version = "6.0.0", path = "../../primitives/core" }
2525
sp-runtime = { version = "6.0.0", path = "../../primitives/runtime" }

client/chain-spec/src/chain_spec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#![warn(missing_docs)]
2121

2222
use crate::{extension::GetExtension, ChainType, Properties, RuntimeGenesis};
23-
use sc_network::config::MultiaddrWithPeerId;
23+
use sc_network_common::config::MultiaddrWithPeerId;
2424
use sc_telemetry::TelemetryEndpoints;
2525
use serde::{Deserialize, Serialize};
2626
use serde_json as json;

client/chain-spec/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub use extension::{
184184
};
185185
pub use sc_chain_spec_derive::{ChainSpecExtension, ChainSpecGroup};
186186

187-
use sc_network::config::MultiaddrWithPeerId;
187+
use sc_network_common::config::MultiaddrWithPeerId;
188188
use sc_telemetry::TelemetryEndpoints;
189189
use serde::{de::DeserializeOwned, Serialize};
190190
use sp_core::storage::Storage;

client/network-gossip/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ log = "0.4.17"
2222
lru = "0.7.5"
2323
tracing = "0.1.29"
2424
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.10.0-dev", path = "../../utils/prometheus" }
25-
sc-network = { version = "0.10.0-dev", path = "../network" }
2625
sc-network-common = { version = "0.10.0-dev", path = "../network/common" }
26+
sc-peerset = { version = "4.0.0-dev", path = "../peerset" }
2727
sp-runtime = { version = "6.0.0", path = "../../primitives/runtime" }
2828

2929
[dev-dependencies]

client/network-gossip/src/bridge.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::{
2121
Network, Validator,
2222
};
2323

24-
use sc_network::ReputationChange;
2524
use sc_network_common::protocol::event::Event;
25+
use sc_peerset::ReputationChange;
2626

2727
use futures::{
2828
channel::mpsc::{channel, Receiver, Sender},
@@ -152,7 +152,7 @@ impl<B: BlockT> GossipEngine<B> {
152152

153153
/// Send addressed message to the given peers. The message is not kept or multicast
154154
/// later on.
155-
pub fn send_message(&mut self, who: Vec<sc_network::PeerId>, data: Vec<u8>) {
155+
pub fn send_message(&mut self, who: Vec<PeerId>, data: Vec<u8>) {
156156
for who in &who {
157157
self.state_machine.send_message(&mut *self.network, who, data.clone());
158158
}

client/network-gossip/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub use self::{
6767
validator::{DiscardAll, MessageIntent, ValidationResult, Validator, ValidatorContext},
6868
};
6969

70-
use sc_network::{multiaddr, PeerId};
70+
use libp2p::{multiaddr, PeerId};
7171
use sc_network_common::service::{
7272
NetworkBlock, NetworkEventStream, NetworkNotification, NetworkPeers,
7373
};

client/network-gossip/src/state_machine.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ const REBROADCAST_INTERVAL: time::Duration = time::Duration::from_millis(750);
4242
pub(crate) const PERIODIC_MAINTENANCE_INTERVAL: time::Duration = time::Duration::from_millis(1100);
4343

4444
mod rep {
45-
use sc_network::ReputationChange as Rep;
45+
use sc_peerset::ReputationChange as Rep;
4646
/// Reputation change when a peer sends us a gossip message that we didn't know about.
47-
pub const GOSSIP_SUCCESS: Rep = Rep::new(1 << 4, "Successfull gossip");
47+
pub const GOSSIP_SUCCESS: Rep = Rep::new(1 << 4, "Successful gossip");
4848
/// Reputation change when a peer sends us a gossip message that we already knew about.
4949
pub const DUPLICATE_GOSSIP: Rep = Rep::new(-(1 << 2), "Duplicate gossip");
5050
}
@@ -513,14 +513,14 @@ mod tests {
513513
use super::*;
514514
use crate::multiaddr::Multiaddr;
515515
use futures::prelude::*;
516-
use sc_network::ReputationChange;
517516
use sc_network_common::{
518517
protocol::event::Event,
519518
service::{
520519
NetworkBlock, NetworkEventStream, NetworkNotification, NetworkPeers,
521520
NotificationSender, NotificationSenderError,
522521
},
523522
};
523+
use sc_peerset::ReputationChange;
524524
use sp_runtime::{
525525
testing::{Block as RawBlock, ExtrinsicWrapper, H256},
526526
traits::NumberFor,

client/network-gossip/src/validator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// You should have received a copy of the GNU General Public License
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

19-
use sc_network::PeerId;
19+
use libp2p::PeerId;
2020
use sc_network_common::protocol::event::ObservedRole;
2121
use sp_runtime::traits::Block as BlockT;
2222

client/network/common/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ libp2p = "0.46.1"
2828
smallvec = "1.8.0"
2929
sc-consensus = { version = "0.10.0-dev", path = "../../consensus/common" }
3030
sc-peerset = { version = "4.0.0-dev", path = "../../peerset" }
31+
serde = { version = "1.0.136", features = ["derive"] }
3132
sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/common" }
3233
sp-finality-grandpa = { version = "4.0.0-dev", path = "../../../primitives/finality-grandpa" }
3334
sp-runtime = { version = "6.0.0", path = "../../../primitives/runtime" }

client/network/common/src/config.rs

+128-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
//! Configuration of the networking layer.
2020
21-
use std::{fmt, str};
21+
use libp2p::{multiaddr, Multiaddr, PeerId};
22+
use std::{fmt, str, str::FromStr};
2223

2324
/// Name of a protocol, transmitted on the wire. Should be unique for each chain. Always UTF-8.
2425
#[derive(Clone, PartialEq, Eq, Hash)]
@@ -42,3 +43,129 @@ impl fmt::Debug for ProtocolId {
4243
fmt::Debug::fmt(self.as_ref(), f)
4344
}
4445
}
46+
47+
/// Parses a string address and splits it into Multiaddress and PeerId, if
48+
/// valid.
49+
///
50+
/// # Example
51+
///
52+
/// ```
53+
/// # use libp2p::{Multiaddr, PeerId};
54+
/// # use sc_network_common::config::parse_str_addr;
55+
/// let (peer_id, addr) = parse_str_addr(
56+
/// "/ip4/198.51.100.19/tcp/30333/p2p/QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV"
57+
/// ).unwrap();
58+
/// assert_eq!(peer_id, "QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV".parse::<PeerId>().unwrap());
59+
/// assert_eq!(addr, "/ip4/198.51.100.19/tcp/30333".parse::<Multiaddr>().unwrap());
60+
/// ```
61+
pub fn parse_str_addr(addr_str: &str) -> Result<(PeerId, Multiaddr), ParseErr> {
62+
let addr: Multiaddr = addr_str.parse()?;
63+
parse_addr(addr)
64+
}
65+
66+
/// Splits a Multiaddress into a Multiaddress and PeerId.
67+
pub fn parse_addr(mut addr: Multiaddr) -> Result<(PeerId, Multiaddr), ParseErr> {
68+
let who = match addr.pop() {
69+
Some(multiaddr::Protocol::P2p(key)) =>
70+
PeerId::from_multihash(key).map_err(|_| ParseErr::InvalidPeerId)?,
71+
_ => return Err(ParseErr::PeerIdMissing),
72+
};
73+
74+
Ok((who, addr))
75+
}
76+
77+
/// Address of a node, including its identity.
78+
///
79+
/// This struct represents a decoded version of a multiaddress that ends with `/p2p/<peerid>`.
80+
///
81+
/// # Example
82+
///
83+
/// ```
84+
/// # use libp2p::{Multiaddr, PeerId};
85+
/// # use sc_network_common::config::MultiaddrWithPeerId;
86+
/// let addr: MultiaddrWithPeerId =
87+
/// "/ip4/198.51.100.19/tcp/30333/p2p/QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV".parse().unwrap();
88+
/// assert_eq!(addr.peer_id.to_base58(), "QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV");
89+
/// assert_eq!(addr.multiaddr.to_string(), "/ip4/198.51.100.19/tcp/30333");
90+
/// ```
91+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
92+
#[serde(try_from = "String", into = "String")]
93+
pub struct MultiaddrWithPeerId {
94+
/// Address of the node.
95+
pub multiaddr: Multiaddr,
96+
/// Its identity.
97+
pub peer_id: PeerId,
98+
}
99+
100+
impl MultiaddrWithPeerId {
101+
/// Concatenates the multiaddress and peer ID into one multiaddress containing both.
102+
pub fn concat(&self) -> Multiaddr {
103+
let proto = multiaddr::Protocol::P2p(From::from(self.peer_id));
104+
self.multiaddr.clone().with(proto)
105+
}
106+
}
107+
108+
impl fmt::Display for MultiaddrWithPeerId {
109+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110+
fmt::Display::fmt(&self.concat(), f)
111+
}
112+
}
113+
114+
impl FromStr for MultiaddrWithPeerId {
115+
type Err = ParseErr;
116+
117+
fn from_str(s: &str) -> Result<Self, Self::Err> {
118+
let (peer_id, multiaddr) = parse_str_addr(s)?;
119+
Ok(Self { peer_id, multiaddr })
120+
}
121+
}
122+
123+
impl From<MultiaddrWithPeerId> for String {
124+
fn from(ma: MultiaddrWithPeerId) -> String {
125+
format!("{}", ma)
126+
}
127+
}
128+
129+
impl TryFrom<String> for MultiaddrWithPeerId {
130+
type Error = ParseErr;
131+
fn try_from(string: String) -> Result<Self, Self::Error> {
132+
string.parse()
133+
}
134+
}
135+
136+
/// Error that can be generated by `parse_str_addr`.
137+
#[derive(Debug)]
138+
pub enum ParseErr {
139+
/// Error while parsing the multiaddress.
140+
MultiaddrParse(multiaddr::Error),
141+
/// Multihash of the peer ID is invalid.
142+
InvalidPeerId,
143+
/// The peer ID is missing from the address.
144+
PeerIdMissing,
145+
}
146+
147+
impl fmt::Display for ParseErr {
148+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149+
match self {
150+
Self::MultiaddrParse(err) => write!(f, "{}", err),
151+
Self::InvalidPeerId => write!(f, "Peer id at the end of the address is invalid"),
152+
Self::PeerIdMissing => write!(f, "Peer id is missing from the address"),
153+
}
154+
}
155+
}
156+
157+
impl std::error::Error for ParseErr {
158+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
159+
match self {
160+
Self::MultiaddrParse(err) => Some(err),
161+
Self::InvalidPeerId => None,
162+
Self::PeerIdMissing => None,
163+
}
164+
}
165+
}
166+
167+
impl From<multiaddr::Error> for ParseErr {
168+
fn from(err: multiaddr::Error) -> ParseErr {
169+
Self::MultiaddrParse(err)
170+
}
171+
}

0 commit comments

Comments
 (0)