2323//! secret_key,
2424//! NodeType::QUIC,
2525//! false, // filter_private_addrs
26- //! vec![], // known_peers
27- //! |builder, _p2p_ctx, _keypair, relay_client| {
26+ //! P2PContext::default(),
27+ //! |builder, _keypair, relay_client| {
2828//! builder
2929//! .with_user_agent("my-app/1.0.0")
3030//! .with_inner(relay_client)
4242//! secret_key,
4343//! NodeType::QUIC,
4444//! false, // filter_private_addrs
45- //! vec![], // known_peers
46- //! |builder, _p2p_ctx, keypair, relay_client| {
45+ //! P2PContext::default(),
46+ //! |builder, keypair, relay_client| {
4747//! builder
4848//! .with_user_agent("my-app/1.0.0")
4949//! .with_inner(MyBehaviour {
6767//! secret_key,
6868//! NodeType::TCP,
6969//! false, // filter_private_addrs
70- //! vec![], // known_peers
71- //! |builder, _p2p_ctx, keypair| {
70+ //! P2PContext::default(),
71+ //! |builder, keypair| {
7272//! builder.with_inner(
7373//! relay::Behaviour::new(keypair.public().to_peer_id(), relay_config)
7474//! )
@@ -148,6 +148,15 @@ pub enum P2PError {
148148 /// Failed to parse IP address.
149149 #[ error( "Failed to parse IP address: {0}" ) ]
150150 FailedToParseIpAddress ( #[ from] std:: net:: AddrParseError ) ,
151+
152+ /// The provided P2P context is already bound to a different local peer ID.
153+ #[ error( "P2P context local peer ID mismatch: expected {expected}, got {actual}" ) ]
154+ LocalPeerIdMismatch {
155+ /// Local peer ID derived from the node keypair.
156+ expected : Box < PeerId > ,
157+ /// Local peer ID already bound in the shared P2P context.
158+ actual : Box < PeerId > ,
159+ } ,
151160}
152161
153162impl P2PError {
@@ -183,18 +192,18 @@ pub struct Node<B: NetworkBehaviour> {
183192impl < B : NetworkBehaviour > Node < B > {
184193 /// Creates a new client node with relay client support.
185194 ///
186- /// The `behaviour_fn` receives a default `PlutoBehaviourBuilder`, the P2P
187- /// context, keypair, and relay client. It should configure the builder
188- /// (e.g., set user agent, inner behaviour) and return it. The builder
189- /// will then be finalized internally.
195+ /// The `behaviour_fn` receives a `PlutoBehaviourBuilder`, keypair, and
196+ /// relay client. It should configure the builder (e.g., set user agent,
197+ /// inner behaviour) and return it. The builder will then be finalized
198+ /// internally.
190199 ///
191200 /// # Arguments
192201 ///
193202 /// * `cfg` - P2P configuration for addresses and networking
194203 /// * `key` - Secret key for node identity
195204 /// * `node_type` - Transport type (TCP or QUIC)
196205 /// * `filter_private_addrs` - Whether to filter private addresses
197- /// * `known_peers ` - List of known cluster peer IDs for metrics tracking
206+ /// * `p2p_context ` - Shared P2P runtime context for this node
198207 /// * `behaviour_fn` - Closure that configures and returns the behaviour
199208 /// builder
200209 ///
@@ -206,8 +215,8 @@ impl<B: NetworkBehaviour> Node<B> {
206215 /// secret_key,
207216 /// NodeType::QUIC,
208217 /// false,
209- /// vec![peer1, peer2], // known cluster peers
210- /// |builder, _p2p_ctx, _keypair, relay_client| {
218+ /// P2PContext::new( vec![peer1, peer2]),
219+ /// |builder, _keypair, relay_client| {
211220 /// builder
212221 /// .with_user_agent("my-app/1.0.0")
213222 /// .with_inner(MyBehaviour { relay_client, peerinfo: ... })
@@ -219,7 +228,7 @@ impl<B: NetworkBehaviour> Node<B> {
219228 key : k256:: SecretKey ,
220229 node_type : NodeType ,
221230 filter_private_addrs : bool ,
222- known_peers : impl IntoIterator < Item = PeerId > ,
231+ p2p_context : P2PContext ,
223232 behaviour_fn : F ,
224233 ) -> Result < Self >
225234 where
@@ -230,7 +239,7 @@ impl<B: NetworkBehaviour> Node<B> {
230239 ) -> PlutoBehaviourBuilder < B > ,
231240 {
232241 let keypair = utils:: keypair_from_secret_key ( key) ?;
233- let p2p_context = P2PContext :: new ( known_peers ) ;
242+ Self :: bind_local_peer_id ( & p2p_context , keypair . public ( ) . to_peer_id ( ) ) ? ;
234243
235244 let mut node = match node_type {
236245 NodeType :: TCP => Self :: build_tcp_client ( keypair, p2p_context, behaviour_fn) ,
@@ -247,22 +256,22 @@ impl<B: NetworkBehaviour> Node<B> {
247256 /// Server nodes (like relay servers) don't include relay client support
248257 /// since they are expected to be publicly reachable.
249258 ///
250- /// The `behaviour_fn` receives a default `PlutoBehaviourBuilder`, the P2P
251- /// context, and keypair. It should configure the builder (e.g., set user
252- /// agent, inner behaviour) and return it.
259+ /// The `behaviour_fn` receives a `PlutoBehaviourBuilder` and keypair. It
260+ /// should configure the builder (e.g., set user agent, inner behaviour)
261+ /// and return it.
253262 pub fn new_server < F > (
254263 cfg : P2PConfig ,
255264 key : k256:: SecretKey ,
256265 node_type : NodeType ,
257266 filter_private_addrs : bool ,
258- known_peers : impl IntoIterator < Item = PeerId > ,
267+ p2p_context : P2PContext ,
259268 behaviour_fn : F ,
260269 ) -> Result < Self >
261270 where
262271 F : FnOnce ( PlutoBehaviourBuilder < B > , & Keypair ) -> PlutoBehaviourBuilder < B > ,
263272 {
264273 let keypair = utils:: keypair_from_secret_key ( key) ?;
265- let p2p_context = P2PContext :: new ( known_peers ) ;
274+ Self :: bind_local_peer_id ( & p2p_context , keypair . public ( ) . to_peer_id ( ) ) ? ;
266275
267276 let mut node = match node_type {
268277 NodeType :: TCP => Self :: build_tcp_server ( keypair, p2p_context, behaviour_fn) ,
@@ -317,6 +326,22 @@ impl<B: NetworkBehaviour> Node<B> {
317326 Ok ( ( ) )
318327 }
319328
329+ fn bind_local_peer_id ( p2p_context : & P2PContext , local_peer_id : PeerId ) -> Result < ( ) > {
330+ match p2p_context. local_peer_id ( ) {
331+ Some ( existing_peer_id) if existing_peer_id != local_peer_id => {
332+ Err ( P2PError :: LocalPeerIdMismatch {
333+ expected : Box :: new ( local_peer_id) ,
334+ actual : Box :: new ( existing_peer_id) ,
335+ } )
336+ }
337+ Some ( _) => Ok ( ( ) ) ,
338+ None => {
339+ p2p_context. set_local_peer_id ( local_peer_id) ;
340+ Ok ( ( ) )
341+ }
342+ }
343+ }
344+
320345 fn build_quic_client < F > (
321346 keypair : Keypair ,
322347 p2p_context : P2PContext ,
@@ -339,9 +364,8 @@ impl<B: NetworkBehaviour> Node<B> {
339364 . with_relay_client ( noise:: Config :: new, yamux_config)
340365 . map_err ( P2PError :: failed_to_build_swarm) ?
341366 . with_behaviour ( |key, relay_client| {
342- let builder = PlutoBehaviourBuilder :: default ( )
343- . with_p2p_context ( p2p_context. clone ( ) )
344- . with_quic_enabled ( true ) ;
367+ let builder =
368+ PlutoBehaviourBuilder :: new ( p2p_context. clone ( ) ) . with_quic_enabled ( true ) ;
345369 behaviour_fn ( builder, key, relay_client) . build ( key)
346370 } )
347371 . map_err ( P2PError :: failed_to_build_swarm) ?
@@ -376,8 +400,7 @@ impl<B: NetworkBehaviour> Node<B> {
376400 . with_relay_client ( noise:: Config :: new, yamux_config)
377401 . map_err ( P2PError :: failed_to_build_swarm) ?
378402 . with_behaviour ( |key, relay_client| {
379- let builder =
380- PlutoBehaviourBuilder :: default ( ) . with_p2p_context ( p2p_context. clone ( ) ) ;
403+ let builder = PlutoBehaviourBuilder :: new ( p2p_context. clone ( ) ) ;
381404 behaviour_fn ( builder, key, relay_client) . build ( key)
382405 } )
383406 . map_err ( P2PError :: failed_to_build_swarm) ?
@@ -407,8 +430,7 @@ impl<B: NetworkBehaviour> Node<B> {
407430 . with_dns ( )
408431 . map_err ( P2PError :: failed_to_build_swarm) ?
409432 . with_behaviour ( |key| {
410- let builder =
411- PlutoBehaviourBuilder :: default ( ) . with_p2p_context ( p2p_context. clone ( ) ) ;
433+ let builder = PlutoBehaviourBuilder :: new ( p2p_context. clone ( ) ) ;
412434 behaviour_fn ( builder, key) . build ( key)
413435 } )
414436 . map_err ( P2PError :: failed_to_build_swarm) ?
@@ -438,8 +460,7 @@ impl<B: NetworkBehaviour> Node<B> {
438460 . with_dns ( )
439461 . map_err ( P2PError :: failed_to_build_swarm) ?
440462 . with_behaviour ( |key| {
441- let builder =
442- PlutoBehaviourBuilder :: default ( ) . with_p2p_context ( p2p_context. clone ( ) ) ;
463+ let builder = PlutoBehaviourBuilder :: new ( p2p_context. clone ( ) ) ;
443464 behaviour_fn ( builder, key) . build ( key)
444465 } )
445466 . map_err ( P2PError :: failed_to_build_swarm) ?
0 commit comments