Background

In rust-libp2p v0.51.3, the gossipsub module cannot be compiled with the target wasm32-unknown-unknown because the wasm-timer crate required by gossipsub throws the following error, as mentioned in my previous post.

$ wasm-pack build

[INFO]: 🎯  Checking for the Wasm target...
[INFO]: 🌀  Compiling to Wasm...
   Compiling libp2p-gossipsub v0.45.0 (https://github.com/libp2p/rust-libp2p.git?branch=master#14938043)
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/peer_score.rs:872:34
    |
871 |   ...                   let window_time = validated_time
    |  _________________________________________-
872 | | ...                       .checked_add(topic_params.mesh_message_deliveries_window)
    | |                           -^^^^^^^^^^^ method not found in `Instant`
    | |___________________________|
    |

There might be several options to resolve the issue, as discussed by the libp2p team. Among them, I chose to fork the wasm-timer crate with minimum changes because rust-libp2p gossipsub deeply depends on the wasm-timer::Interval which is very critical for the performance of gossipsub.

What I’ve changed

In my fork, I’ve made only single commit. that replaces std::time::Instant with instant::Instant that works in both non-WASM and WASM. Thanks to instant::Instant, I was able to remove all Instant definition and implementations from my fork. On the other hand, I left all wasm-timer::Interval-related codes as they are, because I didn’t want to violate the current gossipsub performance that the libp2p team has measured.

After pushing my wasm-timer fork to the repository, I forked rust-libp2p as well in order to update the wasm-timer dependence to my fork: d81554. In that commit, I also had to enable the gossipsub module for the wasm32-unknown-unknown target, which has been disabled so far becauase of the reason that I mentioned above.

Does it work?

Yes! I tested it in my personal project. I made a simple Rust WASM library that requires gossipsub, and ran wasm-pack build (youngjoon-lee/jiri@7a9aa17). Everything was complied successfully and the gossipsub worked in browser.