DEV Community

Youngjoon Lee
Youngjoon Lee

Posted on

Exploring rust-libp2p

Connectivity Tests

In, the current state of Transport implementations in each programming language is already written. But, I’ve seen there have been so many changes in rust-libp2p, as described in the rust-libp2p in 2022 blog post.

After reading the libp2p Connectivity document, I've tested if 'dialing' works for each of the following scenarios using rust-libp2p v0.51.3.

Scenario Dialing Transports tested
Standalone -> Standalone Successful TCP, WebSocket, WebRTC1
WASM browser -> Standalone Successful WebSocket2
WASM browser <- Standalone Failed3 WebSocket
WASM browser -> WASM browser Failed3 WebSocket
JS browser4 -> Standalone Successful WebRTC
Private -> (Relay5) -> Private Successful TCP
Private6 -> (Relay5) -> Private with Hole-punching Failed7 TCP

WASM Limitations

I've found that the following features of rust-libp2p cannot be enabled for WASM.

gossipsub, mdns, dns, tokio
Enter fullscreen mode Exit fullscreen mode

If WASM codes import those features, the following error occurs:

error[E0432]: unresolved import `libp2p::gossipsub`
  --> core/src/
20 |     gossipsub, identity,
   |     ^^^^^^^^^ no `gossipsub` in the root
Enter fullscreen mode Exit fullscreen mode

In regard to gossipsub especially, the gossipsub has been disabled for the wasm32-unknown-unknown target by rust-libp2p/pull/2506#issuecomment-1036448620
because of the following reasons:

  • The custom Interval implementation in rust-libp2p wasn't performant: rust-libp2p/issues/2497. So, they decided to revert back to wasm-timer by rust-libp2p/pull/2506.
  • However, the rust-libp2p with wasm-timer cannot be compiled for wasm32-unknown-unknown with the following error:

    $ wasm-pack build
    [INFO]: 🎯  Checking for the Wasm target...
    [INFO]: 🌀  Compiling to Wasm...
       Compiling libp2p-gossipsub v0.45.0 (
    error[E0599]: no method named `checked_add` found for struct `wasm_timer::Instant` in the current scope
       --> /Users/yjlee/.cargo/git/checkouts/rust-libp2p-98135dbcf5b63918/1493804/protocols/gossipsub/src/
    871 |   ...                   let window_time = validated_time
        |  _________________________________________-
    872 | | ...                       .checked_add(topic_params.mesh_message_deliveries_window)
        | |                           -^^^^^^^^^^^ method not found in `Instant`
        | |___________________________|


  • Fortunately, it seems that we can use the instant crate instead of wasm_timer::Instant. This try had been done by rust-libp2p/pull/2320, but has been reverted by rust-libp2p/pull/2506 as above.

  • But the one remaining problem was that the gossipsub needs the Interval struct which is also dependant on the Instant. To resolve this, the libp2p team is thinking of contributing an Interval implementation to the futures-timer crate: rust-libp2p/issues/2497#issuecomment-1038923700

  • But, I think the easier solution would be just implementing the missing wasm_imter::Instant.checked_add() function above into the wasm_timer because the wasm_timer already provides the Interval struct which is being used by rust-libp2p in production. However, it seems that the libp2p the doesn't want to keep maintaining the wasm-timer crate.

  1. WebRTC example 

  2. vincev/wasm-p2p-chat based on vincev/libp2p-websys-transport that will be included in the rust-libp2p offically 

  3. Browser nodes cannot listen for incoming conns. 

  4. js-libp2p-webrtc browser-to-server example 

  5. Relay-server example 

  6. dCUtR example 

  7. It seems that the hole-punching is not always successful. Used AWS for relayer and listener (in private subnet with NAT), and my laptop for dialer. 

Top comments (0)