pub struct Node {
config: Arc<Config>,
key_pair: KeyPair,
peer_id: PeerID,
monitor: Arc<Monitor>,
discovery: Arc<dyn Discovery>,
peer_pool: Arc<PeerPool>,
connector: Arc<Connector<PeerNetMsgCodec>>,
listener: Arc<Listener<PeerNetMsgCodec>>,
task_group: TaskGroup,
bloom: BloomRef,
}Expand description
Central entry point for the p2p network.
Manages peer connections, discovery, and protocol registration.
§Example
use karyon_core::async_runtime::global_executor;
use karyon_p2p::{Node, Config, keypair::{KeyPair, KeyPairType}};
let key_pair = KeyPair::generate(&KeyPairType::Ed25519);
let config = Config {
listen_endpoints: vec![
"tcp://0.0.0.0:8000".parse().unwrap(),
],
..Config::default()
};
let node = Node::new(&key_pair, config, global_executor());
// node.run().await.unwrap();
// node.attach_protocol::<MyProto>(|peer| MyProto::new(peer)).await;
// node.shutdown().await;Fields§
§config: Arc<Config>The Configuration for the P2P network.
key_pair: KeyPairIdentity Key pair
peer_id: PeerIDPeer ID
monitor: Arc<Monitor>Responsible for network and system monitoring.
discovery: Arc<dyn Discovery>Discovery instance.
peer_pool: Arc<PeerPool>PeerPool instance.
connector: Arc<Connector<PeerNetMsgCodec>>Connector for outbound connections.
listener: Arc<Listener<PeerNetMsgCodec>>Listener for inbound connections.
task_group: TaskGroupManaging spawned tasks.
bloom: BloomRefLocal bloom advertising what items (protocols, swarm keys, …)
this node supports. The mandatory side is filtered with covers,
the optional side with intersects. Updated by attach_protocol
(via Protocol::kind()) and by application layers like Swarm.
Implementations§
Source§impl Node
impl Node
Sourcepub fn new(key_pair: &KeyPair, config: Config, ex: Executor) -> Arc<Node>
pub fn new(key_pair: &KeyPair, config: Config, ex: Executor) -> Arc<Node>
Creates a new Node with the default Kademlia discovery.
Sourcepub fn with_discovery(
key_pair: &KeyPair,
config: Config,
discovery: Arc<dyn Discovery>,
ex: Executor,
) -> Arc<Node>
pub fn with_discovery( key_pair: &KeyPair, config: Config, discovery: Arc<dyn Discovery>, ex: Executor, ) -> Arc<Node>
Creates a new Node with a custom discovery implementation.
The caller is responsible for wiring its own bloom_provider into
the discovery; Node’s bloom_add_* methods will not affect
it unless the discovery reads from the same source.
Sourcepub async fn run(self: &Arc<Self>) -> Result<()>
pub async fn run(self: &Arc<Self>) -> Result<()>
Run the Node, starting listeners, PeerPool, and Discovery.
Sourceasync fn forward_peer_events(self: Arc<Self>)
async fn forward_peer_events(self: Arc<Self>)
Forward peer-pool lifecycle events to discovery so it can update its routing state. Each registered listener (Node’s here, plus any external Swarm subscriber) gets every event independently - no MPMC stealing.
Sourceasync fn connect_discovered_peers(self: Arc<Self>)
async fn connect_discovered_peers(self: Arc<Self>)
Consume discovered peers from the discovery service and connect to them. Runs forever; the task_group cancels it on Node::shutdown.
Sourcepub async fn attach_protocol<P: ProtocolTrait>(
&self,
c: impl Fn(PeerConn) -> Result<Arc<dyn ProtocolTrait>> + Send + Sync + 'static,
) -> Result<()>
pub async fn attach_protocol<P: ProtocolTrait>( &self, c: impl Fn(PeerConn) -> Result<Arc<dyn ProtocolTrait>> + Send + Sync + 'static, ) -> Result<()>
Attach a custom protocol. karyon runs the constructor closure
once per connected peer with a typed PeerConn scoped to this
protocol. Bloom advertises the protocol id according to
P::kind().
Sourceasync fn attach_core_protocols(self: &Arc<Self>) -> Result<()>
async fn attach_core_protocols(self: &Arc<Self>) -> Result<()>
Attach the core protocols (PING). Called once during run.
Sourcepub fn bloom_add_mandatory(&self, item: impl AsRef<[u8]>)
pub fn bloom_add_mandatory(&self, item: impl AsRef<[u8]>)
Add an item the local node REQUIRES peers to also support. Reflected in the next bloom snapshot advertised in PeerMsg.
Sourcepub fn bloom_add_optional(&self, item: impl AsRef<[u8]>)
pub fn bloom_add_optional(&self, item: impl AsRef<[u8]>)
Add an item the local node would LIKE peers to also support but doesn’t require. Used by Swarm and other layers for fuzzy protocol-aware discovery without rejecting non-matches.
Sourcepub fn bloom_snapshot(&self) -> Bloom
pub fn bloom_snapshot(&self) -> Bloom
Snapshot of the local bloom (mandatory + optional sides).
Sourcepub fn find_peers_with(&self, item: impl AsRef<[u8]>) -> Vec<DiscoveredPeer>
pub fn find_peers_with(&self, item: impl AsRef<[u8]>) -> Vec<DiscoveredPeer>
Find peers in the routing table whose advertised bloom may
contain item. Useful for swarm-targeted lookups (e.g.
“peers in this room”) without changing handshake semantics.
Sourcepub async fn inbound_peers(&self) -> HashMap<PeerID, Endpoint>
pub async fn inbound_peers(&self) -> HashMap<PeerID, Endpoint>
Returns a map of inbound connected peers with their endpoints.
Sourcepub async fn outbound_peers(&self) -> HashMap<PeerID, Endpoint>
pub async fn outbound_peers(&self) -> HashMap<PeerID, Endpoint>
Returns a map of outbound connected peers with their endpoints.
Sourcepub fn register_peer_events(&self) -> EventListener<PeerEventTopic, PeerEvent>
pub fn register_peer_events(&self) -> EventListener<PeerEventTopic, PeerEvent>
Register a listener for peer lifecycle events. Each call returns a fresh listener that receives every event (true broadcast).
Sourcepub async fn broadcast_to(
&self,
proto_id: &ProtocolID,
msg: Vec<u8>,
targets: &HashSet<PeerID>,
)
pub async fn broadcast_to( &self, proto_id: &ProtocolID, msg: Vec<u8>, targets: &HashSet<PeerID>, )
Broadcast a message to a specific set of peers on a given protocol. Used by Swarm and other layers to scope broadcasts.
Sourcepub async fn send_to(
&self,
peer_id: &PeerID,
proto_id: &ProtocolID,
msg: Vec<u8>,
) -> Result<()>
pub async fn send_to( &self, peer_id: &PeerID, proto_id: &ProtocolID, msg: Vec<u8>, ) -> Result<()>
Send a message to a specific peer on the given protocol.
Returns PeerNotFound if the peer is not currently connected.
Sourcepub async fn peer_protocol_set(
&self,
pid: &PeerID,
) -> Option<HashSet<ProtocolID>>
pub async fn peer_protocol_set( &self, pid: &PeerID, ) -> Option<HashSet<ProtocolID>>
Returns the negotiated protocol set for a connected peer, or
None if no peer with that id is currently in the pool.