rust-libp2p

repository·master·Indexed 26 days ago

https://github.com/libp2p/rust-libp2p

The central implementation of the libp2p specification in Rust. It provides core networking primitives, transport protocols, and application-level protocols for building decentralized peer-to-peer applications. The project includes libp2p-core for fundamental traits and structs (such as Transport, StreamMuxer, PeerId, and Multiaddr), libp2p-stream for inbound and outbound stream interaction, and support for various transports including QUIC, WebTransport, and WebRTC.

Tokens
23.2K
Snippets
37
Records
168
Agent score
90%

What's inside rust-libp2p

  1. Automate port-forwarding via UPnP

    master
    To increase the probability of a node being publicly reachable when behind a NAT or firewall, rust-libp2p supports automating port-forwarding using protocols like UPnP. This serves as a complementary technique to hole punching.
  2. Manually run WebTransport tests

    master

    To run the WebTransport tests manually, you must first set up an echo server using Docker, then execute the tests using wasm-pack in a browser environment.

    1. Build and start the echo-server

    Run the following commands to build and start the server on your host network:

    docker build -t webtransport-echo-server echo-server
    docker run -it --rm --network=host webtransport-echo-server

    2. Run the tests

    In a separate terminal, use wasm-pack to run the tests in Chrome.

    For interactive testing (visible browser):

    wasm-pack test --chrome

    After running this, navigate to http://127.0.0.1:8000 in your browser.

    For headless testing (no visible browser):

    wasm-pack test --chrome --headless

    Troubleshooting Headless Mode

    • Version Compatibility: Ensure your Chrome browser and chromedriver have the same major version.
    • Custom Chromedriver Path: If chromedriver is not in your PATH, specify it using the --chromedriver flag:
    wasm-pack test --chrome --headless --chromedriver=/path/to/chromedriver
    docker build -t webtransport-echo-server echo-server
    docker run -it --rm --network=host webtransport-echo-server
    
    wasm-pack test --chrome
  3. Correlate asynchronous responses to requests using unique IDs

    master

    When designing asynchronous systems where a command eventually results in a response via an event, ensure that the command includes a unique identifier. This identifier must be included in the subsequent asynchronous response event so that users can reliably match responses to their original requests (e.g., matching a specific connection request to a specific new connection).

    struct Command {
      id: Id,
      // ...
    }
    
    struct Response {
      command_id: Id,
      // ...
    }
  4. Get support and report issues

    master

    Depending on your needs, use the following channels for communication:

    • Bugs, improvements, or feature requests: Open a GitHub issue on the rust-libp2p repository.
    • rust-libp2p specific questions: Use the GitHub Discussions forum.
    • General libp2p implementation questions: Use the libp2p Discourse forum.
    • Synchronous discussions: Join the open rust-libp2p maintainer calls or the biweekly libp2p community calls.
  5. Use `libp2p-webrtc-websys` for Browser Transport

    master
    The libp2p-webrtc-websys crate provides WebRTC transport capabilities for Rust libp2p when running in a browser environment via web-sys bindings. To use this transport, you must initialize your Swarm using Swarm::with_wasm_executor to enable the wasm-bindgen executor required for browser compatibility.
  6. Open outbound streams with libp2p-stream

    master

    To initiate a new outbound stream to a specific peer for a particular protocol, use the Control::open_stream method. This method is asynchronous and returns the stream once the connection is established.

    # fn main() {
    # use libp2p_swarm::{Swarm, StreamProtocol};
    # use libp2p_stream as stream;
    # use libp2p_identity::PeerId;
    let mut swarm: Swarm<stream::Behaviour> = todo!();
    let peer_id: PeerId = todo!();
    
    let mut control = swarm.behaviour().new_control();
    
    let protocol_future = async move {
        let stream = control.open_stream(peer_id, StreamProtocol::new("/my-protocol")).await.unwrap();
    
        // Execute your protocol here using `stream`.
    };
    # }
  7. Run all interop tests locally with Compose

    master

    To run interop tests against all released libp2p versions, you must have the libp2p/test-plans repository checked out. Perform these steps from the root directory of the rust-libp2p repository:

    1. Build the head image: docker build -t rust-libp2p-head . -f interop-tests/Dockerfile.
    2. Build images for all released versions in the test-plans directory: (cd <path to >/libp2p/test-plans/multidim-interop/ && make).
    3. Execute the test suite using npm run test from the multidim-interop directory, passing the path to the rust-libp2p root and a name filter.