Skip to content

Commit f360c87

Browse files
altonendmitry-markinbkchr
authored
Move transactions protocol to its own crate (paritytech#12264)
* Move transaction protocol to its own crate * Update Cargo.lock * Fix binaries * Update client/network/transactions/src/lib.rs Co-authored-by: Dmitry Markin <[email protected]> * Update client/service/src/builder.rs Co-authored-by: Bastian Köcher <[email protected]> * Apply review comments * Revert one change and apply cargo-fmt * Remove Transaction from Message * Add array-bytes * trigger CI * Add comment about codec index Co-authored-by: Dmitry Markin <[email protected]> Co-authored-by: Bastian Köcher <[email protected]>
1 parent 0dbeaa0 commit f360c87

File tree

32 files changed

+466
-471
lines changed

32 files changed

+466
-471
lines changed

Diff for: Cargo.lock

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

Diff for: bin/node-template/node/src/service.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
191191
Vec::default(),
192192
));
193193

194-
let (network, system_rpc_tx, network_starter) =
194+
let (network, system_rpc_tx, tx_handler_controller, network_starter) =
195195
sc_service::build_network(sc_service::BuildNetworkParams {
196196
config: &config,
197197
client: client.clone(),
@@ -238,6 +238,7 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
238238
rpc_builder: rpc_extensions_builder,
239239
backend,
240240
system_rpc_tx,
241+
tx_handler_controller,
241242
config,
242243
telemetry: telemetry.as_mut(),
243244
})?;

Diff for: bin/node/cli/src/service.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ pub fn new_full_base(
354354
Vec::default(),
355355
));
356356

357-
let (network, system_rpc_tx, network_starter) =
357+
let (network, system_rpc_tx, tx_handler_controller, network_starter) =
358358
sc_service::build_network(sc_service::BuildNetworkParams {
359359
config: &config,
360360
client: client.clone(),
@@ -392,6 +392,7 @@ pub fn new_full_base(
392392
transaction_pool: transaction_pool.clone(),
393393
task_manager: &mut task_manager,
394394
system_rpc_tx,
395+
tx_handler_controller,
395396
telemetry: telemetry.as_mut(),
396397
})?;
397398

Diff for: client/beefy/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ sc-consensus = { version = "0.10.0-dev", path = "../consensus/common" }
2727
sc-finality-grandpa = { version = "0.10.0-dev", path = "../../client/finality-grandpa" }
2828
sc-keystore = { version = "4.0.0-dev", path = "../keystore" }
2929
sc-network = { version = "0.10.0-dev", path = "../network" }
30+
sc-network-common = { version = "0.10.0-dev", path = "../network/common" }
3031
sc-network-gossip = { version = "0.10.0-dev", path = "../network-gossip" }
3132
sc-utils = { version = "4.0.0-dev", path = "../utils" }
3233
sp-api = { version = "4.0.0-dev", path = "../../primitives/api" }

Diff for: client/beefy/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ pub(crate) mod beefy_protocol_name {
8383
/// For standard protocol name see [`beefy_protocol_name::standard_name`].
8484
pub fn beefy_peers_set_config(
8585
protocol_name: ProtocolName,
86-
) -> sc_network::config::NonDefaultSetConfig {
87-
let mut cfg = sc_network::config::NonDefaultSetConfig::new(protocol_name, 1024 * 1024);
86+
) -> sc_network_common::config::NonDefaultSetConfig {
87+
let mut cfg = sc_network_common::config::NonDefaultSetConfig::new(protocol_name, 1024 * 1024);
8888

8989
cfg.allow_non_reserved(25, 25);
9090
cfg.add_fallback_names(beefy_protocol_name::LEGACY_NAMES.iter().map(|&n| n.into()).collect());

Diff for: client/cli/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ sc-client-api = { version = "4.0.0-dev", path = "../api" }
3434
sc-client-db = { version = "0.10.0-dev", default-features = false, path = "../db" }
3535
sc-keystore = { version = "4.0.0-dev", path = "../keystore" }
3636
sc-network = { version = "0.10.0-dev", path = "../network" }
37+
sc-network-common = { version = "0.10.0-dev", path = "../network/common" }
3738
sc-service = { version = "0.10.0-dev", default-features = false, path = "../service" }
3839
sc-telemetry = { version = "4.0.0-dev", path = "../telemetry" }
3940
sc-tracing = { version = "4.0.0-dev", path = "../tracing" }

Diff for: client/cli/src/params/network_params.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
use crate::{arg_enums::SyncMode, params::node_key_params::NodeKeyParams};
2020
use clap::Args;
2121
use sc_network::{
22-
config::{
23-
NetworkConfiguration, NodeKeyConfig, NonReservedPeerMode, SetConfig, TransportConfig,
24-
},
22+
config::{NetworkConfiguration, NodeKeyConfig},
2523
multiaddr::Protocol,
2624
};
25+
use sc_network_common::config::{NonReservedPeerMode, SetConfig, TransportConfig};
2726
use sc_service::{
2827
config::{Multiaddr, MultiaddrWithPeerId},
2928
ChainSpec, ChainType,

Diff for: client/finality-grandpa/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -688,18 +688,18 @@ pub struct GrandpaParams<Block: BlockT, C, N, SC, VR> {
688688
/// For standard protocol name see [`crate::protocol_standard_name`].
689689
pub fn grandpa_peers_set_config(
690690
protocol_name: ProtocolName,
691-
) -> sc_network::config::NonDefaultSetConfig {
691+
) -> sc_network_common::config::NonDefaultSetConfig {
692692
use communication::grandpa_protocol_name;
693-
sc_network::config::NonDefaultSetConfig {
693+
sc_network_common::config::NonDefaultSetConfig {
694694
notifications_protocol: protocol_name,
695695
fallback_names: grandpa_protocol_name::LEGACY_NAMES.iter().map(|&n| n.into()).collect(),
696696
// Notifications reach ~256kiB in size at the time of writing on Kusama and Polkadot.
697697
max_notification_size: 1024 * 1024,
698-
set_config: sc_network::config::SetConfig {
698+
set_config: sc_network_common::config::SetConfig {
699699
in_peers: 0,
700700
out_peers: 0,
701701
reserved_nodes: Vec::new(),
702-
non_reserved_mode: sc_network::config::NonReservedPeerMode::Deny,
702+
non_reserved_mode: sc_network_common::config::NonReservedPeerMode::Deny,
703703
},
704704
}
705705
}

Diff for: client/network/common/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ codec = { package = "parity-scale-codec", version = "3.0.0", features = [
2424
"derive",
2525
] }
2626
futures = "0.3.21"
27+
futures-timer = "3.0.2"
2728
libp2p = "0.46.1"
29+
linked_hash_set = "0.1.3"
30+
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.10.0-dev", path = "../../../utils/prometheus" }
2831
smallvec = "1.8.0"
2932
sc-consensus = { version = "0.10.0-dev", path = "../../consensus/common" }
3033
sc-peerset = { version = "4.0.0-dev", path = "../../peerset" }

Diff for: client/network/common/src/config.rs

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

1919
//! Configuration of the networking layer.
2020
21+
use crate::protocol;
22+
2123
use libp2p::{multiaddr, Multiaddr, PeerId};
2224
use std::{fmt, str, str::FromStr};
2325

@@ -171,3 +173,129 @@ impl From<multiaddr::Error> for ParseErr {
171173
Self::MultiaddrParse(err)
172174
}
173175
}
176+
177+
/// Configuration for a set of nodes.
178+
#[derive(Clone, Debug)]
179+
pub struct SetConfig {
180+
/// Maximum allowed number of incoming substreams related to this set.
181+
pub in_peers: u32,
182+
/// Number of outgoing substreams related to this set that we're trying to maintain.
183+
pub out_peers: u32,
184+
/// List of reserved node addresses.
185+
pub reserved_nodes: Vec<MultiaddrWithPeerId>,
186+
/// Whether nodes that aren't in [`SetConfig::reserved_nodes`] are accepted or automatically
187+
/// refused.
188+
pub non_reserved_mode: NonReservedPeerMode,
189+
}
190+
191+
impl Default for SetConfig {
192+
fn default() -> Self {
193+
Self {
194+
in_peers: 25,
195+
out_peers: 75,
196+
reserved_nodes: Vec::new(),
197+
non_reserved_mode: NonReservedPeerMode::Accept,
198+
}
199+
}
200+
}
201+
202+
/// Extension to [`SetConfig`] for sets that aren't the default set.
203+
///
204+
/// > **Note**: As new fields might be added in the future, please consider using the `new` method
205+
/// > and modifiers instead of creating this struct manually.
206+
#[derive(Clone, Debug)]
207+
pub struct NonDefaultSetConfig {
208+
/// Name of the notifications protocols of this set. A substream on this set will be
209+
/// considered established once this protocol is open.
210+
///
211+
/// > **Note**: This field isn't present for the default set, as this is handled internally
212+
/// > by the networking code.
213+
pub notifications_protocol: protocol::ProtocolName,
214+
/// If the remote reports that it doesn't support the protocol indicated in the
215+
/// `notifications_protocol` field, then each of these fallback names will be tried one by
216+
/// one.
217+
///
218+
/// If a fallback is used, it will be reported in
219+
/// `sc_network::protocol::event::Event::NotificationStreamOpened::negotiated_fallback`
220+
pub fallback_names: Vec<protocol::ProtocolName>,
221+
/// Maximum allowed size of single notifications.
222+
pub max_notification_size: u64,
223+
/// Base configuration.
224+
pub set_config: SetConfig,
225+
}
226+
227+
impl NonDefaultSetConfig {
228+
/// Creates a new [`NonDefaultSetConfig`]. Zero slots and accepts only reserved nodes.
229+
pub fn new(notifications_protocol: protocol::ProtocolName, max_notification_size: u64) -> Self {
230+
Self {
231+
notifications_protocol,
232+
max_notification_size,
233+
fallback_names: Vec::new(),
234+
set_config: SetConfig {
235+
in_peers: 0,
236+
out_peers: 0,
237+
reserved_nodes: Vec::new(),
238+
non_reserved_mode: NonReservedPeerMode::Deny,
239+
},
240+
}
241+
}
242+
243+
/// Modifies the configuration to allow non-reserved nodes.
244+
pub fn allow_non_reserved(&mut self, in_peers: u32, out_peers: u32) {
245+
self.set_config.in_peers = in_peers;
246+
self.set_config.out_peers = out_peers;
247+
self.set_config.non_reserved_mode = NonReservedPeerMode::Accept;
248+
}
249+
250+
/// Add a node to the list of reserved nodes.
251+
pub fn add_reserved(&mut self, peer: MultiaddrWithPeerId) {
252+
self.set_config.reserved_nodes.push(peer);
253+
}
254+
255+
/// Add a list of protocol names used for backward compatibility.
256+
///
257+
/// See the explanations in [`NonDefaultSetConfig::fallback_names`].
258+
pub fn add_fallback_names(&mut self, fallback_names: Vec<protocol::ProtocolName>) {
259+
self.fallback_names.extend(fallback_names);
260+
}
261+
}
262+
263+
/// Configuration for the transport layer.
264+
#[derive(Clone, Debug)]
265+
pub enum TransportConfig {
266+
/// Normal transport mode.
267+
Normal {
268+
/// If true, the network will use mDNS to discover other libp2p nodes on the local network
269+
/// and connect to them if they support the same chain.
270+
enable_mdns: bool,
271+
272+
/// If true, allow connecting to private IPv4 addresses (as defined in
273+
/// [RFC1918](https://tools.ietf.org/html/rfc1918)). Irrelevant for addresses that have
274+
/// been passed in `::sc_network::config::NetworkConfiguration::boot_nodes`.
275+
allow_private_ipv4: bool,
276+
},
277+
278+
/// Only allow connections within the same process.
279+
/// Only addresses of the form `/memory/...` will be supported.
280+
MemoryOnly,
281+
}
282+
283+
/// The policy for connections to non-reserved peers.
284+
#[derive(Clone, Debug, PartialEq, Eq)]
285+
pub enum NonReservedPeerMode {
286+
/// Accept them. This is the default.
287+
Accept,
288+
/// Deny them.
289+
Deny,
290+
}
291+
292+
impl NonReservedPeerMode {
293+
/// Attempt to parse the peer mode from a string.
294+
pub fn parse(s: &str) -> Option<Self> {
295+
match s {
296+
"accept" => Some(Self::Accept),
297+
"deny" => Some(Self::Deny),
298+
_ => None,
299+
}
300+
}
301+
}

Diff for: client/network/src/error.rs renamed to client/network/common/src/error.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919
//! Substrate network possible errors.
2020
21-
use crate::config::TransportConfig;
21+
use crate::{config::TransportConfig, protocol::ProtocolName};
2222
use libp2p::{Multiaddr, PeerId};
23-
use sc_network_common::protocol::ProtocolName;
2423

2524
use std::fmt;
2625

Diff for: client/network/common/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@
1919
//! Common data structures of the networking layer.
2020
2121
pub mod config;
22+
pub mod error;
2223
pub mod message;
2324
pub mod protocol;
2425
pub mod request_responses;
2526
pub mod service;
2627
pub mod sync;
28+
pub mod utils;
29+
30+
/// Minimum Requirements for a Hash within Networking
31+
pub trait ExHashT: std::hash::Hash + Eq + std::fmt::Debug + Clone + Send + Sync + 'static {}
32+
33+
impl<T> ExHashT for T where T: std::hash::Hash + Eq + std::fmt::Debug + Clone + Send + Sync + 'static
34+
{}

Diff for: client/network/common/src/service.rs

-29
Original file line numberDiff line numberDiff line change
@@ -604,35 +604,6 @@ where
604604
}
605605
}
606606

607-
/// Provides ability to propagate transactions over the network.
608-
pub trait NetworkTransaction<H> {
609-
/// You may call this when new transactions are imported by the transaction pool.
610-
///
611-
/// All transactions will be fetched from the `TransactionPool` that was passed at
612-
/// initialization as part of the configuration and propagated to peers.
613-
fn trigger_repropagate(&self);
614-
615-
/// You must call when new transaction is imported by the transaction pool.
616-
///
617-
/// This transaction will be fetched from the `TransactionPool` that was passed at
618-
/// initialization as part of the configuration and propagated to peers.
619-
fn propagate_transaction(&self, hash: H);
620-
}
621-
622-
impl<T, H> NetworkTransaction<H> for Arc<T>
623-
where
624-
T: ?Sized,
625-
T: NetworkTransaction<H>,
626-
{
627-
fn trigger_repropagate(&self) {
628-
T::trigger_repropagate(self)
629-
}
630-
631-
fn propagate_transaction(&self, hash: H) {
632-
T::propagate_transaction(self, hash)
633-
}
634-
}
635-
636607
/// Provides ability to announce blocks to the network.
637608
pub trait NetworkBlock<BlockHash, BlockNumber> {
638609
/// Make sure an important block is propagated to peers.
File renamed without changes.

0 commit comments

Comments
 (0)