pub struct P2PNode {
pub security_dashboard: Option<Arc<SecurityDashboard>>,
/* private fields */
}Expand description
Main P2P node structure Main P2P network node that manages connections, routing, and communication
This struct represents a complete P2P network participant that can:
- Connect to other peers via QUIC transport
- Participate in distributed hash table (DHT) operations
- Send and receive messages through various protocols
- Handle network events and peer lifecycle
- Provide MCP (Model Context Protocol) services
Fields§
§security_dashboard: Option<Arc<SecurityDashboard>>Security dashboard for monitoring
Implementations§
Source§impl P2PNode
impl P2PNode
Sourcepub fn new_for_tests() -> Result<Self>
pub fn new_for_tests() -> Result<Self>
Minimal constructor for tests that avoids real networking
Sourcepub async fn new(config: NodeConfig) -> Result<Self>
pub async fn new(config: NodeConfig) -> Result<Self>
Create a new P2P node with the given configuration
Sourcepub fn builder() -> NodeBuilder
pub fn builder() -> NodeBuilder
Create a new node builder
Sourcepub fn transport_peer_id(&self) -> Option<String>
pub fn transport_peer_id(&self) -> Option<String>
Get the hex-encoded transport-level peer ID.
This is the ID used in P2PEvent::Message.source, connected_peers(),
and send_message(). It differs from peer_id() which is the app-level ID.
Returns the transport peer ID from whichever stack (v4 or v6) is active.
pub fn local_addr(&self) -> Option<String>
pub async fn subscribe(&self, topic: &str) -> Result<()>
pub async fn publish(&self, topic: &str, data: &[u8]) -> Result<()>
Sourcepub fn config(&self) -> &NodeConfig
pub fn config(&self) -> &NodeConfig
Get the node configuration
Sourcepub async fn is_running(&self) -> bool
pub async fn is_running(&self) -> bool
Check if the node is running
Sourcepub async fn listen_addrs(&self) -> Vec<SocketAddr>
pub async fn listen_addrs(&self) -> Vec<SocketAddr>
Get the current listen addresses
Sourcepub async fn connected_peers(&self) -> Vec<PeerId> ⓘ
pub async fn connected_peers(&self) -> Vec<PeerId> ⓘ
Get connected peers
Sourcepub async fn peer_count(&self) -> usize
pub async fn peer_count(&self) -> usize
Get peer count
Sourcepub async fn get_peer_id_by_address(&self, addr: &str) -> Option<PeerId>
pub async fn get_peer_id_by_address(&self, addr: &str) -> Option<PeerId>
Get the peer ID for a given socket address, if connected
This method searches through all connected peers to find one that has the specified address in its address list.
§Arguments
addr- The socket address to search for (e.g., “192.168.1.100:9000”)
§Returns
Some(PeerId)- The peer ID if a matching connected peer is foundNone- If no peer with this address is currently connected
Sourcepub async fn list_active_connections(&self) -> Vec<(PeerId, Vec<String>)>
pub async fn list_active_connections(&self) -> Vec<(PeerId, Vec<String>)>
List all active connections with their peer IDs and addresses
§Returns
A vector of tuples containing (PeerId, Vec
Sourcepub async fn remove_peer(&self, peer_id: &PeerId) -> bool
pub async fn remove_peer(&self, peer_id: &PeerId) -> bool
Remove a peer from the peers map
This method removes a peer from the internal peers map. It should be used when a connection is no longer valid (e.g., after detecting that the underlying ant-quic connection has closed).
§Arguments
peer_id- The ID of the peer to remove
§Returns
true if the peer was found and removed, false if the peer was not in the map
Sourcepub async fn is_peer_connected(&self, peer_id: &PeerId) -> bool
pub async fn is_peer_connected(&self, peer_id: &PeerId) -> bool
Check if a peer is connected
This method checks if the peer ID exists in the peers map. Note that this
only verifies the peer is registered - it does not guarantee the underlying
ant-quic connection is still active. For connection validation, use send_message
which will fail if the connection is closed.
§Arguments
peer_id- The ID of the peer to check
§Returns
true if the peer exists in the peers map, false otherwise
Sourcepub async fn connect_peer(&self, address: &str) -> Result<PeerId>
pub async fn connect_peer(&self, address: &str) -> Result<PeerId>
Connect to a peer
Sourcepub async fn disconnect_peer(&self, peer_id: &PeerId) -> Result<()>
pub async fn disconnect_peer(&self, peer_id: &PeerId) -> Result<()>
Disconnect from a peer
Actively closes the underlying QUIC connection via
P2pEndpoint::disconnect() and removes the peer from all
local tracking maps.
Sourcepub async fn is_connection_active(&self, peer_id: &str) -> bool
pub async fn is_connection_active(&self, peer_id: &str) -> bool
Check if a connection to a peer is active
Source§impl P2PNode
impl P2PNode
Sourcepub fn subscribe_events(&self) -> Receiver<P2PEvent>
pub fn subscribe_events(&self) -> Receiver<P2PEvent>
Subscribe to network events
Sourcepub fn binary_hash(&self) -> &[u8; 32]
pub fn binary_hash(&self) -> &[u8; 32]
Get this node’s binary hash used for attestation.
Sourcepub fn entangled_id(&self) -> Option<&EntangledId>
pub fn entangled_id(&self) -> Option<&EntangledId>
Get this node’s entangled identity, if set.
Sourcepub fn set_entangled_id(&mut self, entangled_id: EntangledId)
pub fn set_entangled_id(&mut self, entangled_id: EntangledId)
Set the entangled identity for this node.
This should be called after the node’s cryptographic identity is established, typically by deriving from the NodeIdentity’s public key.
Sourcepub fn verify_peer_attestation(
&self,
peer_id: &str,
peer_entangled_id: &EntangledId,
peer_public_key: &MlDsaPublicKey,
) -> EnforcementDecision
pub fn verify_peer_attestation( &self, peer_id: &str, peer_entangled_id: &EntangledId, peer_public_key: &MlDsaPublicKey, ) -> EnforcementDecision
Verify a peer’s attestation and return the enforcement decision.
This function implements the Entangled Attestation verification protocol (Phase 6: Hard Enforcement). Based on the configured enforcement mode:
- Off: Skips verification entirely
- Soft: Logs warnings but allows connections
- Hard: Rejects connections with invalid attestations
§Arguments
peer_id- The peer’s identifier for loggingpeer_entangled_id- The peer’s claimed entangled IDpeer_public_key- The peer’s ML-DSA public key
§Returns
An [EnforcementDecision] indicating whether to allow or reject the connection.
§Example
let decision = node.verify_peer_attestation(peer_id, &entangled_id, &public_key);
if decision.should_reject() {
// Send rejection message and close connection
if let Some(rejection) = decision.rejection() {
send_rejection(peer_id, rejection);
}
disconnect(peer_id);
}Sourcepub fn verify_peer_attestation_simple(
&self,
peer_id: &str,
peer_entangled_id: &EntangledId,
peer_public_key: &MlDsaPublicKey,
) -> bool
pub fn verify_peer_attestation_simple( &self, peer_id: &str, peer_entangled_id: &EntangledId, peer_public_key: &MlDsaPublicKey, ) -> bool
Verify a peer’s attestation and return a simple boolean result.
This is a convenience method that wraps [verify_peer_attestation] for cases
where only a pass/fail result is needed without the detailed decision.
§Returns
true if the connection should be allowed, false if it should be rejected.
Sourcepub async fn resource_metrics(&self) -> Result<ResourceMetrics>
pub async fn resource_metrics(&self) -> Result<ResourceMetrics>
Get production resource metrics
Sourcepub async fn health_check(&self) -> Result<()>
pub async fn health_check(&self) -> Result<()>
Check system health
Sourcepub fn production_config(&self) -> Option<&ProductionConfig>
pub fn production_config(&self) -> Option<&ProductionConfig>
Get production configuration (if enabled)
Sourcepub fn is_production_mode(&self) -> bool
pub fn is_production_mode(&self) -> bool
Check if production hardening is enabled
Sourcepub async fn add_discovered_peer(
&self,
peer_id: PeerId,
addresses: Vec<String>,
) -> Result<()>
pub async fn add_discovered_peer( &self, peer_id: PeerId, addresses: Vec<String>, ) -> Result<()>
Add a discovered peer to the bootstrap cache
Sourcepub async fn update_peer_metrics(
&self,
peer_id: &PeerId,
success: bool,
latency_ms: Option<u64>,
_error: Option<String>,
) -> Result<()>
pub async fn update_peer_metrics( &self, peer_id: &PeerId, success: bool, latency_ms: Option<u64>, _error: Option<String>, ) -> Result<()>
Update connection metrics for a peer in the bootstrap cache
Sourcepub async fn get_bootstrap_cache_stats(&self) -> Result<Option<CacheStats>>
pub async fn get_bootstrap_cache_stats(&self) -> Result<Option<CacheStats>>
Get bootstrap cache statistics
Sourcepub async fn cached_peer_count(&self) -> usize
pub async fn cached_peer_count(&self) -> usize
Get the number of cached bootstrap peers
Auto Trait Implementations§
impl !Freeze for P2PNode
impl !RefUnwindSafe for P2PNode
impl Send for P2PNode
impl Sync for P2PNode
impl Unpin for P2PNode
impl !UnwindSafe for P2PNode
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§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
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§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.Source§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.Source§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.Source§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.Source§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.Source§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.Source§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.Source§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.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§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 moreSource§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 moreSource§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
Source§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
Source§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.Source§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.Source§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.Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<T> Tap for T
impl<T> Tap for T
Source§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 moreSource§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 moreSource§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 moreSource§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 moreSource§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 moreSource§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 moreSource§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.Source§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.Source§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.Source§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.Source§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.Source§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.Source§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.