mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 19:17:58 +00:00
Move transactions protocol to its own crate (#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 <dmitry@markin.tech> * Update client/service/src/builder.rs Co-authored-by: Bastian Köcher <info@kchr.de> * 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 <dmitry@markin.tech> Co-authored-by: Bastian Köcher <info@kchr.de>
This commit is contained in:
@@ -27,24 +27,24 @@ pub use sc_network_common::{
|
||||
IncomingRequest, OutgoingResponse, ProtocolConfig as RequestResponseConfig,
|
||||
},
|
||||
sync::warp::WarpSyncProvider,
|
||||
ExHashT,
|
||||
};
|
||||
|
||||
pub use libp2p::{build_multiaddr, core::PublicKey, identity};
|
||||
|
||||
use crate::ExHashT;
|
||||
|
||||
use core::{fmt, iter};
|
||||
use futures::future;
|
||||
use libp2p::{
|
||||
identity::{ed25519, Keypair},
|
||||
multiaddr, Multiaddr,
|
||||
};
|
||||
use prometheus_endpoint::Registry;
|
||||
use sc_consensus::ImportQueue;
|
||||
use sc_network_common::{config::MultiaddrWithPeerId, protocol::ProtocolName, sync::ChainSync};
|
||||
use sc_network_common::{
|
||||
config::{MultiaddrWithPeerId, NonDefaultSetConfig, SetConfig, TransportConfig},
|
||||
sync::ChainSync,
|
||||
};
|
||||
use sp_runtime::traits::Block as BlockT;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
error::Error,
|
||||
fs,
|
||||
future::Future,
|
||||
@@ -52,16 +52,14 @@ use std::{
|
||||
net::Ipv4Addr,
|
||||
path::{Path, PathBuf},
|
||||
pin::Pin,
|
||||
str,
|
||||
sync::Arc,
|
||||
};
|
||||
use zeroize::Zeroize;
|
||||
|
||||
/// Network initialization parameters.
|
||||
pub struct Params<B, H, Client>
|
||||
pub struct Params<B, Client>
|
||||
where
|
||||
B: BlockT + 'static,
|
||||
H: ExHashT,
|
||||
{
|
||||
/// Assigned role for our node (full, light, ...).
|
||||
pub role: Role,
|
||||
@@ -70,21 +68,12 @@ where
|
||||
/// default.
|
||||
pub executor: Option<Box<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send>>,
|
||||
|
||||
/// How to spawn the background task dedicated to the transactions handler.
|
||||
pub transactions_handler_executor: Box<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send>,
|
||||
|
||||
/// Network layer configuration.
|
||||
pub network_config: NetworkConfiguration,
|
||||
|
||||
/// Client that contains the blockchain.
|
||||
pub chain: Arc<Client>,
|
||||
|
||||
/// Pool of transactions.
|
||||
///
|
||||
/// The network worker will fetch transactions from this object in order to propagate them on
|
||||
/// the network.
|
||||
pub transaction_pool: Arc<dyn TransactionPool<H, B>>,
|
||||
|
||||
/// Legacy name of the protocol to use on the wire. Should be different for each chain.
|
||||
pub protocol_id: ProtocolId,
|
||||
|
||||
@@ -166,66 +155,6 @@ impl fmt::Display for Role {
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of the transaction import.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum TransactionImport {
|
||||
/// Transaction is good but already known by the transaction pool.
|
||||
KnownGood,
|
||||
/// Transaction is good and not yet known.
|
||||
NewGood,
|
||||
/// Transaction is invalid.
|
||||
Bad,
|
||||
/// Transaction import was not performed.
|
||||
None,
|
||||
}
|
||||
|
||||
/// Future resolving to transaction import result.
|
||||
pub type TransactionImportFuture = Pin<Box<dyn Future<Output = TransactionImport> + Send>>;
|
||||
|
||||
/// Transaction pool interface
|
||||
pub trait TransactionPool<H: ExHashT, B: BlockT>: Send + Sync {
|
||||
/// Get transactions from the pool that are ready to be propagated.
|
||||
fn transactions(&self) -> Vec<(H, B::Extrinsic)>;
|
||||
/// Get hash of transaction.
|
||||
fn hash_of(&self, transaction: &B::Extrinsic) -> H;
|
||||
/// Import a transaction into the pool.
|
||||
///
|
||||
/// This will return future.
|
||||
fn import(&self, transaction: B::Extrinsic) -> TransactionImportFuture;
|
||||
/// Notify the pool about transactions broadcast.
|
||||
fn on_broadcasted(&self, propagations: HashMap<H, Vec<String>>);
|
||||
/// Get transaction by hash.
|
||||
fn transaction(&self, hash: &H) -> Option<B::Extrinsic>;
|
||||
}
|
||||
|
||||
/// Dummy implementation of the [`TransactionPool`] trait for a transaction pool that is always
|
||||
/// empty and discards all incoming transactions.
|
||||
///
|
||||
/// Requires the "hash" type to implement the `Default` trait.
|
||||
///
|
||||
/// Useful for testing purposes.
|
||||
pub struct EmptyTransactionPool;
|
||||
|
||||
impl<H: ExHashT + Default, B: BlockT> TransactionPool<H, B> for EmptyTransactionPool {
|
||||
fn transactions(&self) -> Vec<(H, B::Extrinsic)> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn hash_of(&self, _transaction: &B::Extrinsic) -> H {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
fn import(&self, _transaction: B::Extrinsic) -> TransactionImportFuture {
|
||||
Box::pin(future::ready(TransactionImport::KnownGood))
|
||||
}
|
||||
|
||||
fn on_broadcasted(&self, _: HashMap<H, Vec<String>>) {}
|
||||
|
||||
fn transaction(&self, _h: &H) -> Option<B::Extrinsic> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Sync operation mode.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub enum SyncMode {
|
||||
@@ -394,132 +323,6 @@ impl NetworkConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
/// Configuration for a set of nodes.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SetConfig {
|
||||
/// Maximum allowed number of incoming substreams related to this set.
|
||||
pub in_peers: u32,
|
||||
/// Number of outgoing substreams related to this set that we're trying to maintain.
|
||||
pub out_peers: u32,
|
||||
/// List of reserved node addresses.
|
||||
pub reserved_nodes: Vec<MultiaddrWithPeerId>,
|
||||
/// Whether nodes that aren't in [`SetConfig::reserved_nodes`] are accepted or automatically
|
||||
/// refused.
|
||||
pub non_reserved_mode: NonReservedPeerMode,
|
||||
}
|
||||
|
||||
impl Default for SetConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
in_peers: 25,
|
||||
out_peers: 75,
|
||||
reserved_nodes: Vec::new(),
|
||||
non_reserved_mode: NonReservedPeerMode::Accept,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// > and modifiers instead of creating this struct manually.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct NonDefaultSetConfig {
|
||||
/// Name of the notifications protocols of this set. A substream on this set will be
|
||||
/// considered established once this protocol is open.
|
||||
///
|
||||
/// > **Note**: This field isn't present for the default set, as this is handled internally
|
||||
/// > by the networking code.
|
||||
pub notifications_protocol: ProtocolName,
|
||||
/// If the remote reports that it doesn't support the protocol indicated in the
|
||||
/// `notifications_protocol` field, then each of these fallback names will be tried one by
|
||||
/// one.
|
||||
///
|
||||
/// If a fallback is used, it will be reported in
|
||||
/// [`crate::Event::NotificationStreamOpened::negotiated_fallback`].
|
||||
pub fallback_names: Vec<ProtocolName>,
|
||||
/// Maximum allowed size of single notifications.
|
||||
pub max_notification_size: u64,
|
||||
/// Base configuration.
|
||||
pub set_config: SetConfig,
|
||||
}
|
||||
|
||||
impl NonDefaultSetConfig {
|
||||
/// Creates a new [`NonDefaultSetConfig`]. Zero slots and accepts only reserved nodes.
|
||||
pub fn new(notifications_protocol: ProtocolName, max_notification_size: u64) -> Self {
|
||||
Self {
|
||||
notifications_protocol,
|
||||
max_notification_size,
|
||||
fallback_names: Vec::new(),
|
||||
set_config: SetConfig {
|
||||
in_peers: 0,
|
||||
out_peers: 0,
|
||||
reserved_nodes: Vec::new(),
|
||||
non_reserved_mode: NonReservedPeerMode::Deny,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Modifies the configuration to allow non-reserved nodes.
|
||||
pub fn allow_non_reserved(&mut self, in_peers: u32, out_peers: u32) {
|
||||
self.set_config.in_peers = in_peers;
|
||||
self.set_config.out_peers = out_peers;
|
||||
self.set_config.non_reserved_mode = NonReservedPeerMode::Accept;
|
||||
}
|
||||
|
||||
/// Add a node to the list of reserved nodes.
|
||||
pub fn add_reserved(&mut self, peer: MultiaddrWithPeerId) {
|
||||
self.set_config.reserved_nodes.push(peer);
|
||||
}
|
||||
|
||||
/// Add a list of protocol names used for backward compatibility.
|
||||
///
|
||||
/// See the explanations in [`NonDefaultSetConfig::fallback_names`].
|
||||
pub fn add_fallback_names(&mut self, fallback_names: Vec<ProtocolName>) {
|
||||
self.fallback_names.extend(fallback_names);
|
||||
}
|
||||
}
|
||||
|
||||
/// Configuration for the transport layer.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TransportConfig {
|
||||
/// Normal transport mode.
|
||||
Normal {
|
||||
/// If true, the network will use mDNS to discover other libp2p nodes on the local network
|
||||
/// and connect to them if they support the same chain.
|
||||
enable_mdns: bool,
|
||||
|
||||
/// If true, allow connecting to private IPv4 addresses (as defined in
|
||||
/// [RFC1918](https://tools.ietf.org/html/rfc1918)). Irrelevant for addresses that have
|
||||
/// been passed in [`NetworkConfiguration::boot_nodes`].
|
||||
allow_private_ipv4: bool,
|
||||
},
|
||||
|
||||
/// Only allow connections within the same process.
|
||||
/// Only addresses of the form `/memory/...` will be supported.
|
||||
MemoryOnly,
|
||||
}
|
||||
|
||||
/// The policy for connections to non-reserved peers.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum NonReservedPeerMode {
|
||||
/// Accept them. This is the default.
|
||||
Accept,
|
||||
/// Deny them.
|
||||
Deny,
|
||||
}
|
||||
|
||||
impl NonReservedPeerMode {
|
||||
/// Attempt to parse the peer mode from a string.
|
||||
pub fn parse(s: &str) -> Option<Self> {
|
||||
match s {
|
||||
"accept" => Some(Self::Accept),
|
||||
"deny" => Some(Self::Deny),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The configuration of a node's secret key, describing the type of key
|
||||
/// and how it is obtained. A node's identity keypair is the result of
|
||||
/// the evaluation of the node key configuration.
|
||||
|
||||
Reference in New Issue
Block a user