BuildP2P NetworkingTesting

Testing Multi-node P2P Blueprints

SDK source (GitHub): https://github.com/tangle-network/blueprint/tree/v2/crates/networking

When you build multi-operator protocols, the networking test utilities let you spin up multiple libp2p nodes and verify handshakes and message flow without running a full chain.

Example: Multi-node handshake + message

use blueprint_sdk::crypto::k256::K256Ecdsa;
use blueprint_sdk::networking::service_handle::NetworkServiceHandle;
use blueprint_sdk::networking::test_utils::{create_whitelisted_nodes, setup_log, wait_for_all_handshakes};
use blueprint_sdk::networking::types::MessageRouting;
use std::time::Duration;
 
#[tokio::test]
async fn test_p2p_protocol() {
    setup_log();
 
    // Create 3 nodes that trust the same whitelist.
    let mut nodes = create_whitelisted_nodes::<K256Ecdsa>(3, "test-net", "sum-test", false);
 
    let mut handles = Vec::new();
    for node in nodes.iter_mut() {
        handles.push(node.start().await.expect("start node"));
    }
 
    let mut handle_refs: Vec<&mut NetworkServiceHandle<K256Ecdsa>> = handles.iter_mut().collect();
    wait_for_all_handshakes(handle_refs.as_mut_slice(), Duration::from_secs(10)).await;
 
    let routing = MessageRouting {
        message_id: 1,
        round_id: 0,
        sender: handles[0].local_peer_id,
        recipient: None,
    };
    handles[0]
        .send(routing, b"hello world".to_vec())
        .expect("send message");
}

Tips

  • Use create_whitelisted_nodes to keep key management consistent across your test network.
  • wait_for_all_handshakes gives deterministic readiness checks before you start sending messages.
  • If you need to inspect traffic, read from handle.next_protocol_message() in each node loop.