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:
Aaro Altonen
2022-09-26 15:10:09 +03:00
committed by GitHub
parent ea377d0b17
commit 4c19c13d05
32 changed files with 466 additions and 471 deletions
+16 -61
View File
@@ -29,9 +29,8 @@
use crate::{
behaviour::{self, Behaviour, BehaviourOut},
config::{Params, TransportConfig},
config::Params,
discovery::DiscoveryConfig,
error::Error,
network_state::{
NetworkState, NotConnectedPeer as NetworkStateNotConnectedPeer, Peer as NetworkStatePeer,
},
@@ -39,7 +38,7 @@ use crate::{
self, message::generic::Roles, NotificationsSink, NotifsHandlerError, PeerInfo, Protocol,
Ready,
},
transactions, transport, ExHashT, ReputationChange,
transport, ReputationChange,
};
use codec::Encode as _;
@@ -60,7 +59,8 @@ use metrics::{Histogram, HistogramVec, MetricSources, Metrics};
use parking_lot::Mutex;
use sc_consensus::{BlockImportError, BlockImportStatus, ImportQueue, Link};
use sc_network_common::{
config::MultiaddrWithPeerId,
config::{MultiaddrWithPeerId, TransportConfig},
error::Error,
protocol::{
event::{DhtEvent, Event},
ProtocolName,
@@ -73,6 +73,7 @@ use sc_network_common::{
NotificationSenderReady as NotificationSenderReadyT, Signature, SigningError,
},
sync::{SyncState, SyncStatus},
ExHashT,
};
use sc_peerset::PeersetHandle;
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
@@ -101,7 +102,7 @@ mod out_events;
mod tests;
pub use libp2p::identity::{error::DecodingError, Keypair, PublicKey};
use sc_network_common::service::{NetworkBlock, NetworkRequest, NetworkTransaction};
use sc_network_common::service::{NetworkBlock, NetworkRequest};
/// Substrate network service. Handles network IO and manages connectivity.
pub struct NetworkService<B: BlockT + 'static, H: ExHashT> {
@@ -121,7 +122,7 @@ pub struct NetworkService<B: BlockT + 'static, H: ExHashT> {
/// nodes it should be connected to or not.
peerset: PeersetHandle,
/// Channel that sends messages to the actual worker.
to_worker: TracingUnboundedSender<ServiceToWorkerMsg<B, H>>,
to_worker: TracingUnboundedSender<ServiceToWorkerMsg<B>>,
/// For each peer and protocol combination, an object that allows sending notifications to
/// that peer. Updated by the [`NetworkWorker`].
peers_notifications_sinks: Arc<Mutex<HashMap<(PeerId, ProtocolName), NotificationsSink>>>,
@@ -144,7 +145,7 @@ where
/// Returns a `NetworkWorker` that implements `Future` and must be regularly polled in order
/// for the network processing to advance. From it, you can extract a `NetworkService` using
/// `worker.service()`. The `NetworkService` can be shared through the codebase.
pub fn new(mut params: Params<B, H, Client>) -> Result<Self, Error> {
pub fn new(mut params: Params<B, Client>) -> Result<Self, Error> {
// Private and public keys configuration.
let local_identity = params.network_config.node_key.clone().into_keypair()?;
let local_public = local_identity.public();
@@ -215,21 +216,6 @@ where
fs::create_dir_all(path)?;
}
let transactions_handler_proto = transactions::TransactionsHandlerPrototype::new(
params.protocol_id.clone(),
params
.chain
.hash(0u32.into())
.ok()
.flatten()
.expect("Genesis block exists; qed"),
params.fork_id.clone(),
);
params
.network_config
.extra_sets
.insert(0, transactions_handler_proto.set_config());
info!(
target: "sub-libp2p",
"🏷 Local node identity is: {}",
@@ -244,11 +230,8 @@ where
params.protocol_id.clone(),
&params.fork_id,
&params.network_config,
iter::once(Vec::new())
.chain(
(0..params.network_config.extra_sets.len() - 1)
.map(|_| default_notif_handshake_message.clone()),
)
(0..params.network_config.extra_sets.len())
.map(|_| default_notif_handshake_message.clone())
.collect(),
params.metrics_registry.as_ref(),
params.chain_sync,
@@ -465,13 +448,6 @@ where
_marker: PhantomData,
});
let (tx_handler, tx_handler_controller) = transactions_handler_proto.build(
service.clone(),
params.transaction_pool,
params.metrics_registry.as_ref(),
)?;
(params.transactions_handler_executor)(tx_handler.run().boxed());
Ok(NetworkWorker {
external_addresses,
num_connected,
@@ -482,9 +458,9 @@ where
from_service,
event_streams: out_events::OutChannels::new(params.metrics_registry.as_ref())?,
peers_notifications_sinks,
tx_handler_controller,
metrics,
boot_node_ids,
_marker: Default::default(),
})
}
@@ -1149,20 +1125,6 @@ where
}
}
impl<B, H> NetworkTransaction<H> for NetworkService<B, H>
where
B: BlockT + 'static,
H: ExHashT,
{
fn trigger_repropagate(&self) {
let _ = self.to_worker.unbounded_send(ServiceToWorkerMsg::PropagateTransactions);
}
fn propagate_transaction(&self, hash: H) {
let _ = self.to_worker.unbounded_send(ServiceToWorkerMsg::PropagateTransaction(hash));
}
}
impl<B, H> NetworkBlock<B::Hash, NumberFor<B>> for NetworkService<B, H>
where
B: BlockT + 'static,
@@ -1249,9 +1211,7 @@ impl<'a> NotificationSenderReadyT for NotificationSenderReady<'a> {
/// Messages sent from the `NetworkService` to the `NetworkWorker`.
///
/// Each entry corresponds to a method of `NetworkService`.
enum ServiceToWorkerMsg<B: BlockT, H: ExHashT> {
PropagateTransaction(H),
PropagateTransactions,
enum ServiceToWorkerMsg<B: BlockT> {
RequestJustification(B::Hash, NumberFor<B>),
ClearJustificationRequests,
AnnounceBlock(B::Hash, Option<Vec<u8>>),
@@ -1309,7 +1269,7 @@ where
/// The import queue that was passed at initialization.
import_queue: Box<dyn ImportQueue<B>>,
/// Messages from the [`NetworkService`] that must be processed.
from_service: TracingUnboundedReceiver<ServiceToWorkerMsg<B, H>>,
from_service: TracingUnboundedReceiver<ServiceToWorkerMsg<B>>,
/// Senders for events that happen on the network.
event_streams: out_events::OutChannels,
/// Prometheus network metrics.
@@ -1319,8 +1279,9 @@ where
/// For each peer and protocol combination, an object that allows sending notifications to
/// that peer. Shared with the [`NetworkService`].
peers_notifications_sinks: Arc<Mutex<HashMap<(PeerId, ProtocolName), NotificationsSink>>>,
/// Controller for the handler of incoming and outgoing transactions.
tx_handler_controller: transactions::TransactionsHandlerController<H>,
/// Marker to pin the `H` generic. Serves no purpose except to not break backwards
/// compatibility.
_marker: PhantomData<H>,
}
impl<B, H, Client> Future for NetworkWorker<B, H, Client>
@@ -1376,10 +1337,6 @@ where
.behaviour_mut()
.user_protocol_mut()
.clear_justification_requests(),
ServiceToWorkerMsg::PropagateTransaction(hash) =>
this.tx_handler_controller.propagate_transaction(hash),
ServiceToWorkerMsg::PropagateTransactions =>
this.tx_handler_controller.propagate_transactions(),
ServiceToWorkerMsg::GetValue(key) =>
this.network_service.behaviour_mut().get_value(key),
ServiceToWorkerMsg::PutValue(key, value) =>
@@ -1922,8 +1879,6 @@ where
SyncState::Downloading => true,
};
this.tx_handler_controller.set_gossip_enabled(!is_major_syncing);
this.is_major_syncing.store(is_major_syncing, Ordering::Relaxed);
if let Some(metrics) = this.metrics.as_ref() {