Struct sc_network::NetworkService
source · [−]Expand description
Substrate network service. Handles network IO and manages connectivity.
Implementations
sourceimpl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H>
impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H>
sourcepub fn local_peer_id(&self) -> &PeerId
pub fn local_peer_id(&self) -> &PeerId
Returns the local PeerId
.
Set authorized peers.
Need a better solution to manage authorized peers, but now just use reserved peers for prototyping.
Set authorized_only flag.
Need a better solution to decide authorized_only, but now just use reserved_only flag for prototyping.
sourcepub fn write_notification(
&self,
target: PeerId,
protocol: Cow<'static, str>,
message: Vec<u8>
)
pub fn write_notification(
&self,
target: PeerId,
protocol: Cow<'static, str>,
message: Vec<u8>
)
Appends a notification to the buffer of pending outgoing notifications with the given peer. Has no effect if the notifications channel with this protocol name is not open.
If the buffer of pending outgoing notifications with that peer is full, the notification is silently dropped and the connection to the remote will start being shut down. This happens if you call this method at a higher rate than the rate at which the peer processes these notifications, or if the available network bandwidth is too low.
For this reason, this method is considered soft-deprecated. You are encouraged to use
NetworkService::notification_sender
instead.
Note: The reason why this is a no-op in the situation where we have no channel is that we don’t guarantee message delivery anyway. Networking issues can cause connections to drop at any time, and higher-level logic shouldn’t differentiate between the remote voluntarily closing a substream or a network error preventing the message from being delivered.
The protocol must have been registered with
NetworkConfiguration::notifications_protocols
.
sourcepub fn notification_sender(
&self,
target: PeerId,
protocol: Cow<'static, str>
) -> Result<NotificationSender, NotificationSenderError>
pub fn notification_sender(
&self,
target: PeerId,
protocol: Cow<'static, str>
) -> Result<NotificationSender, NotificationSenderError>
Obtains a NotificationSender
for a connected peer, if it exists.
A NotificationSender
is scoped to a particular connection to the peer that holds
a receiver. With a NotificationSender
at hand, sending a notification is done in two steps:
NotificationSender::ready
is used to wait for the sender to become ready for another notification, yielding aNotificationSenderReady
token.NotificationSenderReady::send
enqueues the notification for sending. This operation can only fail if the underlying notification substream or connection has suddenly closed.
An error is returned by NotificationSenderReady::send
if there exists no open
notifications substream with that combination of peer and protocol, or if the remote
has asked to close the notifications substream. If that happens, it is guaranteed that an
Event::NotificationStreamClosed
has been generated on the stream returned by
NetworkService::event_stream
.
If the remote requests to close the notifications substream, all notifications successfully
enqueued using NotificationSenderReady::send
will finish being sent out before the
substream actually gets closed, but attempting to enqueue more notifications will now
return an error. It is however possible for the entire connection to be abruptly closed,
in which case enqueued notifications will be lost.
The protocol must have been registered with
NetworkConfiguration::notifications_protocols
.
Usage
This method returns a struct that allows waiting until there is space available in the buffer of messages towards the given peer. If the peer processes notifications at a slower rate than we send them, this buffer will quickly fill up.
As such, you should never do something like this:
// Do NOT do this
for peer in peers {
if let Ok(n) = network.notification_sender(peer, ...) {
if let Ok(s) = n.ready().await {
let _ = s.send(...);
}
}
}
Doing so would slow down all peers to the rate of the slowest one. A malicious or malfunctioning peer could intentionally process notifications at a very slow rate.
Instead, you are encouraged to maintain your own buffer of notifications on top of the one
maintained by sc-network
, and use notification_sender
to progressively send out
elements from your buffer. If this additional buffer is full (which will happen at some
point if the peer is too slow to process notifications), appropriate measures can be taken,
such as removing non-critical notifications from the buffer or disconnecting the peer
using NetworkService::disconnect_peer
.
Notifications Per-peer buffer
broadcast +—––> of notifications +–> notification_sender
+–> Internet
^ (not covered by
| sc-network)
+
Notifications should be dropped
if buffer is full
See also the gossip
module for a higher-level way to send
notifications.
sourcepub fn event_stream(&self, name: &'static str) -> impl Stream<Item = Event>
pub fn event_stream(&self, name: &'static str) -> impl Stream<Item = Event>
Returns a stream containing the events that happen on the network.
If this method is called multiple times, the events are duplicated.
The stream never ends (unless the NetworkWorker
gets shut down).
The name passed is used to identify the channel in the Prometheus metrics. Note that the
parameter is a &'static str
, and not a String
, in order to avoid accidentally having
an unbounded set of Prometheus metrics, which would be quite bad in terms of memory
sourcepub async fn request(
&self,
target: PeerId,
protocol: impl Into<Cow<'static, str>>,
request: Vec<u8>,
connect: IfDisconnected
) -> Result<Vec<u8>, RequestFailure>
pub async fn request(
&self,
target: PeerId,
protocol: impl Into<Cow<'static, str>>,
request: Vec<u8>,
connect: IfDisconnected
) -> Result<Vec<u8>, RequestFailure>
Sends a single targeted request to a specific peer. On success, returns the response of the peer.
Request-response protocols are a way to complement notifications protocols, but
notifications should remain the default ways of communicating information. For example, a
peer can announce something through a notification, after which the recipient can obtain
more information by performing a request.
As such, call this function with IfDisconnected::ImmediateError
for connect
. This way you
will get an error immediately for disconnected peers, instead of waiting for a potentially very
long connection attempt, which would suggest that something is wrong anyway, as you are
supposed to be connected because of the notification protocol.
No limit or throttling of concurrent outbound requests per peer and protocol are enforced. Such restrictions, if desired, need to be enforced at the call site(s).
The protocol must have been registered through
NetworkConfiguration::request_response_protocols
.
sourcepub fn start_request(
&self,
target: PeerId,
protocol: impl Into<Cow<'static, str>>,
request: Vec<u8>,
tx: Sender<Result<Vec<u8>, RequestFailure>>,
connect: IfDisconnected
)
pub fn start_request(
&self,
target: PeerId,
protocol: impl Into<Cow<'static, str>>,
request: Vec<u8>,
tx: Sender<Result<Vec<u8>, RequestFailure>>,
connect: IfDisconnected
)
Variation of request
which starts a request whose response is delivered on a provided channel.
Instead of blocking and waiting for a reply, this function returns immediately, sending responses via the passed in sender. This alternative API exists to make it easier to integrate with message passing APIs.
Keep in mind that the connected receiver might receive a Canceled
event in case of a
closing connection. This is expected behaviour. With request
you would get a
RequestFailure::Network(OutboundFailure::ConnectionClosed)
in that case.
sourcepub fn trigger_repropagate(&self)
pub fn trigger_repropagate(&self)
You may call this when new transactons are imported by the transaction pool.
All transactions will be fetched from the TransactionPool
that was passed at
initialization as part of the configuration and propagated to peers.
sourcepub fn propagate_transaction(&self, hash: H)
pub fn propagate_transaction(&self, hash: H)
You must call when new transaction is imported by the transaction pool.
This transaction will be fetched from the TransactionPool
that was passed at
initialization as part of the configuration and propagated to peers.
sourcepub fn announce_block(&self, hash: B::Hash, data: Option<Vec<u8>>)
pub fn announce_block(&self, hash: B::Hash, data: Option<Vec<u8>>)
Make sure an important block is propagated to peers.
In chain-based consensus, we often need to make sure non-best forks are at least temporarily synced. This function forces such an announcement.
sourcepub fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange)
pub fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange)
Report a given peer as either beneficial (+) or costly (-) according to the given scalar.
sourcepub fn disconnect_peer(
&self,
who: PeerId,
protocol: impl Into<Cow<'static, str>>
)
pub fn disconnect_peer(
&self,
who: PeerId,
protocol: impl Into<Cow<'static, str>>
)
Disconnect from a node as soon as possible.
This triggers the same effects as if the connection had closed itself spontaneously.
See also NetworkService::remove_from_peers_set
, which has the same effect but also
prevents the local node from re-establishing an outgoing substream to this peer until it
is added again.
sourcepub fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>)
pub fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>)
Request a justification for the given block from the network.
On success, the justification will be passed to the import queue that was part at initialization as part of the configuration.
sourcepub fn is_major_syncing(&self) -> bool
pub fn is_major_syncing(&self) -> bool
Are we in the process of downloading the chain?
sourcepub fn get_value(&self, key: &Key)
pub fn get_value(&self, key: &Key)
Start getting a value from the DHT.
This will generate either a ValueFound
or a ValueNotFound
event and pass it as an
item on the NetworkWorker
stream.
sourcepub fn put_value(&self, key: Key, value: Vec<u8>)
pub fn put_value(&self, key: Key, value: Vec<u8>)
Start putting a value in the DHT.
This will generate either a ValuePut
or a ValuePutFailed
event and pass it as an
item on the NetworkWorker
stream.
sourcepub fn accept_unreserved_peers(&self)
pub fn accept_unreserved_peers(&self)
Connect to unreserved peers and allow unreserved peers to connect for syncing purposes.
sourcepub fn deny_unreserved_peers(&self)
pub fn deny_unreserved_peers(&self)
Disconnect from unreserved peers and deny new unreserved peers to connect for syncing purposes.
sourcepub fn add_reserved_peer(&self, peer: String) -> Result<(), String>
pub fn add_reserved_peer(&self, peer: String) -> Result<(), String>
Adds a PeerId
and its address as reserved. The string should encode the address
and peer ID of the remote node.
Returns an Err
if the given string is not a valid multiaddress
or contains an invalid peer ID (which includes the local peer ID).
sourcepub fn remove_reserved_peer(&self, peer_id: PeerId)
pub fn remove_reserved_peer(&self, peer_id: PeerId)
Removes a PeerId
from the list of reserved peers.
sourcepub fn add_peers_to_reserved_set(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>
) -> Result<(), String>
pub fn add_peers_to_reserved_set(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>
) -> Result<(), String>
Add peers to a peer set.
Each Multiaddr
must end with a /p2p/
component containing the PeerId
. It can also
consist of only /p2p/<peerid>
.
Returns an Err
if one of the given addresses is invalid or contains an
invalid peer ID (which includes the local peer ID).
sourcepub fn remove_peers_from_reserved_set(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>
) -> Result<(), String>
pub fn remove_peers_from_reserved_set(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>
) -> Result<(), String>
Remove peers from a peer set.
Each Multiaddr
must end with a /p2p/
component containing the PeerId
.
Returns an Err
if one of the given addresses is invalid or contains an
invalid peer ID (which includes the local peer ID).
sourcepub fn set_sync_fork_request(
&self,
peers: Vec<PeerId>,
hash: B::Hash,
number: NumberFor<B>
)
pub fn set_sync_fork_request(
&self,
peers: Vec<PeerId>,
hash: B::Hash,
number: NumberFor<B>
)
Configure an explicit fork sync request.
Note that this function should not be used for recent blocks.
Sync should be able to download all the recent forks normally.
set_sync_fork_request
should only be used if external code detects that there’s
a stale fork missing.
Passing empty peers
set effectively removes the sync request.
sourcepub fn add_to_peers_set(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>
) -> Result<(), String>
pub fn add_to_peers_set(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>
) -> Result<(), String>
Add a peer to a set of peers.
If the set has slots available, it will try to open a substream with this peer.
Each Multiaddr
must end with a /p2p/
component containing the PeerId
. It can also
consist of only /p2p/<peerid>
.
Returns an Err
if one of the given addresses is invalid or contains an
invalid peer ID (which includes the local peer ID).
sourcepub fn remove_from_peers_set(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>
) -> Result<(), String>
pub fn remove_from_peers_set(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>
) -> Result<(), String>
Remove peers from a peer set.
If we currently have an open substream with this peer, it will soon be closed.
Each Multiaddr
must end with a /p2p/
component containing the PeerId
.
Returns an Err
if one of the given addresses is invalid or contains an
invalid peer ID (which includes the local peer ID).
sourcepub fn num_connected(&self) -> usize
pub fn num_connected(&self) -> usize
Returns the number of peers we’re connected to.
sourcepub fn new_best_block_imported(&self, hash: B::Hash, number: NumberFor<B>)
pub fn new_best_block_imported(&self, hash: B::Hash, number: NumberFor<B>)
Inform the network service about new best imported block.
Trait Implementations
sourceimpl<B, H> NetworkStateInfo for NetworkService<B, H> where
B: Block,
H: ExHashT,
impl<B, H> NetworkStateInfo for NetworkService<B, H> where
B: Block,
H: ExHashT,
sourceimpl<B: BlockT + 'static, H: ExHashT> SyncOracle for NetworkService<B, H>
impl<B: BlockT + 'static, H: ExHashT> SyncOracle for NetworkService<B, H>
sourcefn is_major_syncing(&mut self) -> bool
fn is_major_syncing(&mut self) -> bool
Whether the synchronization service is undergoing major sync. Returns true if so. Read more
sourcefn is_offline(&mut self) -> bool
fn is_offline(&mut self) -> bool
Whether the synchronization service is offline. Returns true if so. Read more
sourceimpl<'a, B: BlockT + 'static, H: ExHashT> SyncOracle for &'a NetworkService<B, H>
impl<'a, B: BlockT + 'static, H: ExHashT> SyncOracle for &'a NetworkService<B, H>
sourcefn is_major_syncing(&mut self) -> bool
fn is_major_syncing(&mut self) -> bool
Whether the synchronization service is undergoing major sync. Returns true if so. Read more
sourcefn is_offline(&mut self) -> bool
fn is_offline(&mut self) -> bool
Whether the synchronization service is offline. Returns true if so. Read more
Auto Trait Implementations
impl<B, H> !RefUnwindSafe for NetworkService<B, H>
impl<B, H> Send for NetworkService<B, H>
impl<B, H> Sync for NetworkService<B, H>
impl<B, H> Unpin for NetworkService<B, H> where
H: Unpin,
impl<B, H> !UnwindSafe for NetworkService<B, H>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> CheckedConversion for T
impl<T> CheckedConversion for T
sourcefn checked_from<T>(t: T) -> Option<Self> where
Self: TryFrom<T>,
fn checked_from<T>(t: T) -> Option<Self> where
Self: TryFrom<T>,
Convert from a value of T
into an equivalent instance of Option<Self>
. Read more
sourcefn checked_into<T>(self) -> Option<T> where
Self: TryInto<T>,
fn checked_into<T>(self) -> Option<T> where
Self: TryInto<T>,
Consume self to return Some
equivalent value of Option<T>
. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T, Outer> IsWrappedBy<Outer> for T where
Outer: AsRef<T> + AsMut<T> + From<T>,
T: From<Outer>,
impl<T, Outer> IsWrappedBy<Outer> for T where
Outer: AsRef<T> + AsMut<T> + From<T>,
T: From<Outer>,
sourceimpl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
sourcefn saturated_from<T>(t: T) -> Self where
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Self where
Self: UniqueSaturatedFrom<T>,
Convert from a value of T
into an equivalent instance of Self
. Read more
sourcefn saturated_into<T>(self) -> T where
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> T where
Self: UniqueSaturatedInto<T>,
Consume self to return an equivalent value of T
. Read more
sourceimpl<S, T> UncheckedInto<T> for S where
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for S where
T: UncheckedFrom<S>,
sourcepub fn unchecked_into(self) -> T
pub fn unchecked_into(self) -> T
The counterpart to unchecked_from
.
sourceimpl<T, S> UniqueSaturatedInto<T> for S where
T: Bounded,
S: TryInto<T>,
impl<T, S> UniqueSaturatedInto<T> for S where
T: Bounded,
S: TryInto<T>,
sourcepub fn unique_saturated_into(self) -> T
pub fn unique_saturated_into(self) -> T
Consume self to return an equivalent value of T
.
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more