pub struct NetworkNode<T: NodeType, D: DhtPersistentStorage> {
peer_id: PeerId,
swarm: Swarm<NetworkDef<T::SignatureKey, D>>,
kademlia_record_ttl: Duration,
consensus_key_to_pid_map: Arc<Mutex<BiMap<T::SignatureKey, PeerId>>>,
listener_id: Option<ListenerId>,
direct_message_state: DMBehaviour,
dht_handler: DHTBehaviour<T::SignatureKey, D>,
resend_tx: Option<UnboundedSender<ClientRequest>>,
saw_inbound_connection: bool,
expected_kad_protocol: StreamProtocol,
same_network_peers: HashSet<PeerId>,
dht_put_quorum: Option<NonZeroUsize>,
}Expand description
Network definition
Fields§
§peer_id: PeerIdpeer id of network node
swarm: Swarm<NetworkDef<T::SignatureKey, D>>the swarm of networkbehaviours
kademlia_record_ttl: DurationThe Kademlia record TTL
consensus_key_to_pid_map: Arc<Mutex<BiMap<T::SignatureKey, PeerId>>>The map from consensus keys to peer IDs
listener_id: Option<ListenerId>the listener id we are listening on, if it exists
direct_message_state: DMBehaviourHandler for direct messages
dht_handler: DHTBehaviour<T::SignatureKey, D>Handler for DHT Events
resend_tx: Option<UnboundedSender<ClientRequest>>Channel to resend requests, set to Some when we call spawn_listeners
saw_inbound_connection: boolWhether a peer ever connected to us.
Used to warn the operator when the advertised address is likely not publicly reachable.
expected_kad_protocol: StreamProtocolKademlia StreamProtocol expected from same-network peers.
same_network_peers: HashSet<PeerId>Peers confirmed via identify to share our network discriminator. Gates
NewExternalAddrOfPeer, whose event carries no protocols to check directly.
dht_put_quorum: Option<NonZeroUsize>Implementations§
Source§impl<T: NodeType, D: DhtPersistentStorage> NetworkNode<T, D>
impl<T: NodeType, D: DhtPersistentStorage> NetworkNode<T, D>
Sourcepub fn num_connected(&self) -> usize
pub fn num_connected(&self) -> usize
Returns number of peers this node is connected to
Sourcepub fn connected_pids(&self) -> HashSet<PeerId>
pub fn connected_pids(&self) -> HashSet<PeerId>
return hashset of PIDs this node is connected to
Sourcepub async fn start_listen(
&mut self,
listen_addr: Multiaddr,
) -> Result<Multiaddr, NetworkError>
pub async fn start_listen( &mut self, listen_addr: Multiaddr, ) -> Result<Multiaddr, NetworkError>
starts the swarm listening on listen_addr
and optionally dials into peer known_peer
returns the address the swarm is listening upon
Sourcepub fn add_known_peers(&mut self, known_peers: &[(PeerId, Multiaddr)])
pub fn add_known_peers(&mut self, known_peers: &[(PeerId, Multiaddr)])
initialize the DHT with known peers
add the peers to kademlia and then
the spawn_listeners function
will start connecting to peers
Sourcepub async fn new(
config: NetworkNodeConfig,
dht_persistent_storage: D,
consensus_key_to_pid_map: Arc<Mutex<BiMap<T::SignatureKey, PeerId>>>,
) -> Result<Self, NetworkError>
pub async fn new( config: NetworkNodeConfig, dht_persistent_storage: D, consensus_key_to_pid_map: Arc<Mutex<BiMap<T::SignatureKey, PeerId>>>, ) -> Result<Self, NetworkError>
Creates a new Network with the given settings.
Currently:
- Generates a random key pair and associated
PeerId - Launches a hopefully production ready transport: QUIC v1 (RFC 9000) + DNS
- Generates a connection to the “broadcast” topic
- Creates a swarm to manage peers and events
§Errors
- If we fail to generate the transport or any of the behaviours
§Panics
If 5 < 0
Sourcefn on_identify_received(&mut self, peer_id: PeerId, info: IdentifyInfo)
fn on_identify_received(&mut self, peer_id: PeerId, info: IdentifyInfo)
Identify is the first point a peer’s network is detectable; drop other networks.
Sourcepub fn put_record(&mut self, query: KadPutQuery)
pub fn put_record(&mut self, query: KadPutQuery)
Sourceasync fn handle_client_requests(
&mut self,
msg: Option<ClientRequest>,
) -> Result<bool, NetworkError>
async fn handle_client_requests( &mut self, msg: Option<ClientRequest>, ) -> Result<bool, NetworkError>
event handler for client events currently supported actions include
- shutting down the swarm
- gossipping a message to known peers on the
globaltopic - returning the id of the current peer
- subscribing to a topic
- unsubscribing from a toipc
- direct messaging a peer
Sourceasync fn handle_swarm_events(
&mut self,
event: SwarmEvent<NetworkEventInternal>,
send_to_client: &UnboundedSender<NetworkEvent>,
) -> Result<(), NetworkError>
async fn handle_swarm_events( &mut self, event: SwarmEvent<NetworkEventInternal>, send_to_client: &UnboundedSender<NetworkEvent>, ) -> Result<(), NetworkError>
event handler for events emitted from the swarm
Sourcepub fn spawn_listeners(
self,
) -> Result<(UnboundedSender<ClientRequest>, UnboundedReceiver<NetworkEvent>, SwarmTaskHandle), NetworkError>
pub fn spawn_listeners( self, ) -> Result<(UnboundedSender<ClientRequest>, UnboundedReceiver<NetworkEvent>, SwarmTaskHandle), NetworkError>
Spawn a task to listen for requests on the returned channel as well as any events produced by libp2p
§Errors
- If we fail to create the channels or the bootstrap channel
Sourcefn warn_if_unreachable(&self)
fn warn_if_unreachable(&self)
One-shot reachability check.
Run NO_INBOUND_CONNECTION_GRACE_PERIOD after the swarm loop starts.
A healthy, publicly reachable node receives inbound connections shortly
after joining. If we are listening but no peer has connected to us by
now, the advertised address is likely wrong or blocked, so point the
operator at it. If we have no connections at all, not even outbound,
the problem is more fundamental than the advertised address.
Trait Implementations§
Source§impl<T: NodeType, D: DhtPersistentStorage> Debug for NetworkNode<T, D>
impl<T: NodeType, D: DhtPersistentStorage> Debug for NetworkNode<T, D>
Auto Trait Implementations§
impl<T, D> !Freeze for NetworkNode<T, D>
impl<T, D> !RefUnwindSafe for NetworkNode<T, D>
impl<T, D> Send for NetworkNode<T, D>
impl<T, D> !Sync for NetworkNode<T, D>
impl<T, D> Unpin for NetworkNode<T, D>
impl<T, D> UnsafeUnpin for NetworkNode<T, D>where
D: UnsafeUnpin,
impl<T, D> !UnwindSafe for NetworkNode<T, D>
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.