Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gossipsub): allow compiling for WASM #3973

Merged
merged 10 commits into from May 25, 2023

Conversation

onur-ozkan
Copy link
Contributor

@onur-ozkan onur-ozkan commented May 22, 2023

Description

This modification removes deprecated dependency wasm_timer and enables wasm compatibility on the gossibsup protocol by simply substituting the wasm_timer::Instant with instant::Instant(which supports fn checked_add) and wasm_timer::Interval with futures_ticker::Ticker.

Notes

In my opinion, wasm-timer should be removed completely from the dependency tree since it's quite bad that relying on the dependency which is deprecated and no longer maintained. instant is also not so up-to-date dependency, but at very least it works today. But it might be broken someday as well if we make a change on standard library on rustc. Writing simple wrapper in the libp2p tree would be the most stable solution for this project since you can have complete control over it and update it whenever standard library adds or changes something.

@onur-ozkan onur-ozkan changed the title provide wasm compatibility for gossipsub protocol fix(gossipsub): wasm compatibility May 22, 2023
@thomaseizinger
Copy link
Contributor

In my opinion, wasm-timer should be removed completely from the dependency tree since it's quite bad that relying on the dependency which is deprecated and no longer maintained.

Agreed! Are you happy to send a PR for the remaining usages? (I haven't checked whether there actually are any.)

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! One question.

protocols/gossipsub/Cargo.toml Outdated Show resolved Hide resolved
@onur-ozkan
Copy link
Contributor Author

onur-ozkan commented May 22, 2023

Are you happy to send a PR for the remaining usages?

Certainly, I will check/work on that matter during the upcoming weekend.

@onur-ozkan onur-ozkan force-pushed the gossipsub-wasm-compatibility branch 2 times, most recently from 1ab42f6 to 9d1a36f Compare May 22, 2023 15:39
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two more comments, otherwise LGTM :)

protocols/gossipsub/Cargo.toml Show resolved Hide resolved
protocols/gossipsub/src/peer_score.rs Show resolved Hide resolved
Signed-off-by: ozkanonur <work@onurozkan.dev>
@onur-ozkan onur-ozkan force-pushed the gossipsub-wasm-compatibility branch from 9d1a36f to d72d9a7 Compare May 22, 2023 16:33
@thomaseizinger thomaseizinger changed the title fix(gossipsub): wasm compatibility feat(gossipsub): allow compiling for WASM May 22, 2023
@thomaseizinger
Copy link
Contributor

Thanks! I've pushed a few follow-up commits. Most notable, the crate is now included in our WASM build on CI!

If we are already working on WASM compatibility, I'd be great if we could also get rid of wasm_timer in this PR. Let me know if you are up for that. I think replacing Interval with futures_timer::Delay shouldn't be too difficult.

@onur-ozkan
Copy link
Contributor Author

Most notable, the crate is now included in our WASM build on CI!

Awesome :)

I believed that shipping this would greatly benefit many people, so I directly opened the pull request for it. But I am also completely okay with replacing wasm_timer entirely in this PR. Since this PR is a blocker for one of our project, I will probably do it in couple days.

@thomaseizinger
Copy link
Contributor

Most notable, the crate is now included in our WASM build on CI!

Awesome :)

I believed that shipping this would greatly benefit many people, so I directly opened the pull request for it. But I am also completely okay with replacing wasm_timer entirely in this PR. Since this PR is a blocker for one of our project, I will probably do it in couple days.

We will make a new release soon but it is likely a week or so away. This PR should easily make it in even if it takes a few more days before we can land it :)

@onur-ozkan onur-ozkan force-pushed the gossipsub-wasm-compatibility branch from f8990b3 to f11f9e4 Compare May 22, 2023 20:25
@onur-ozkan
Copy link
Contributor Author

I decided to use futures_ticker::Ticker as a replacement of wasm_timer::Interval. The reason I did that is that crate already using the dependencies we have in libp2p dependency tree. It implements the ticker functionality on top of futures_timer.

futures_timer has an open issue about merging futures_ticker. See async-rs/futures-timer#60

Also, it's worth to see this comment about futures_timer and futures_ticker #2497 (comment)

If futures_ticker will be merged into futures_timer, we can use futures_timer easily in libp2p by simply replacing a few keywords in the codebase.

Let me know if this is acceptable or not :)

thomaseizinger
thomaseizinger previously approved these changes May 23, 2023
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

I guess this means we can close #2497 with this PR then?

@thomaseizinger
Copy link
Contributor

cc @mxinden @AgeManning In #2497, there was talk that the only remaining work would be to (re?) enable WASM support for gossipsub. This PR achieves this by means of https://github.com/antifuchs/futures-ticker.

@onur-ozkan
Copy link
Contributor Author

I guess this means we can close #2497 with this PR then?

I think so.

Another way of solving this would be porting futures_ticker implementation in gossipsub. The implementation is super simple(~60 lines of code), and you can update it's dependencies(futures_timer, futures and instant) whenever you want. When you have those 3 dependencies updated in libp2p dependency tree, you will have multiple versions of the same dependency just because of futures_ticker.

@thomaseizinger
Copy link
Contributor

I guess this means we can close #2497 with this PR then?

I think so.

Another way of solving this would be porting futures_ticker implementation in gossipsub. The implementation is super simple(~60 lines of code), and you can update it's dependencies(futures_timer, futures and instant) whenever you want. When you have those 3 dependencies updated in libp2p dependency tree, you will have multiple versions of the same dependency just because of futures_ticker.

If the dependencies aren't pinned, cargo should resolve them to the same version if they are semver compatible.#2497 was created because we initially had our own interval implementation and it was buggy. I'd like to not go back to that (even without a buggy implemention 😀 )

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work here. No objections from my end.

Note that futures-tick uses a "skip" style strategy, i.e. when one misses e.g. 2 ticks, only one is delivered. See also good documentation in tokio's Interval implementation.

Looking at futures-tick, I think there is room for improvement. See antifuchs/futures-ticker#10.

protocols/gossipsub/src/behaviour.rs Outdated Show resolved Hide resolved
@thomaseizinger
Copy link
Contributor

Note that futures-tick uses a "skip" style strategy, i.e. when one misses e.g. 2 ticks, only one is delivered. See also good documentation in tokio's Interval implementation.

I think that makes sense for us. This is used for heartbeats so I don't see a need for a burst strategy. Rather, performing the next heartbeat as soon as possible is important.

Do we agree?

@onur-ozkan
Copy link
Contributor Author

I think that makes sense for us. This is used for heartbeats so I don't see a need for a burst strategy.

++

Signed-off-by: ozkanonur <work@onurozkan.dev>
@mxinden
Copy link
Member

mxinden commented May 25, 2023

Note that futures-tick uses a "skip" style strategy, i.e. when one misses e.g. 2 ticks, only one is delivered. See also good documentation in tokio's Interval implementation.

I think that makes sense for us. This is used for heartbeats so I don't see a need for a burst strategy. Rather, performing the next heartbeat as soon as possible is important.

Do we agree?

yes

mxinden
mxinden previously approved these changes May 25, 2023
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the work here!

@mxinden
Copy link
Member

mxinden commented May 25, 2023

and enables wasm compatibility on the gossibsup protocol

If you don't mind sharing, I would like to know what you are building with rust-libp2p and WASM @ozkanonur.

@AgeManning
Copy link
Contributor

Sorry for the late input here.

As you've all pointed out, we had significant issues because in the past because there was a buggy implementation of interval. It's very important that the heartbeat stays consistent. It appears the ticker approach behaves like what we would need, in that if the software is over-loaded heartbeats are skipped, but should still occur at regular intervals.

We (Lighthouse) have no affinity to wasm-timer. So changes look good, provided the heartbeat works as expected :)

@mergify mergify bot dismissed stale reviews from thomaseizinger and mxinden May 25, 2023 05:41

Approvals have been dismissed because the PR was updated after the send-it label was applied.

@mergify mergify bot merged commit 801023f into libp2p:master May 25, 2023
63 checks passed
@onur-ozkan
Copy link
Contributor Author

and enables wasm compatibility on the gossibsup protocol

If you don't mind sharing, I would like to know what you are building with rust-libp2p and WASM @ozkanonur.

We have a dex/swap protocol called https://github.com/KomodoPlatform/atomicDEX-API that utilizes libp2p for the network stack. Initially, it was written in C, but around 2019, we made the decision to rewrite it in Rust. As part of this rewrite, the network stack was migrated from nanomsg to libp2p, which involved creating custom patches for the libp2p library.

Today, I am rewriting the network layer of the project(KomodoPlatform/komodo-defi-framework#1756). This involves removing the forks and instead utilizing the original version of libp2p (the upstream). Throughout this process, I will likely come up with several more ideas and proposals to improve the libp2p upstream if I believe they would be beneficial.

@onur-ozkan onur-ozkan deleted the gossipsub-wasm-compatibility branch May 25, 2023 06:40
@thomaseizinger
Copy link
Contributor

As you've all pointed out, we had significant issues because in the past because there was a buggy implementation of interval. It's very important that the heartbeat stays consistent. It appears the ticker approach behaves like what we would need, in that if the software is over-loaded heartbeats are skipped, but should still occur at regular intervals.

This sounds like we should have a test for that :)

I'll create an issue.

mergify bot pushed a commit that referenced this pull request Jul 20, 2023
Since #3973, gossipsub now fully supports wasm targets. It functions properly when added as a dependency on its own. However, when trying to use it under libp2p by activating a feature, it's not possible and the compiler will raise an error like `unresolved import libp2p::gossipsub`. This pull request enables the use of gossipsub for wasm targets when it's activated as a feature of the libp2p dependency.

Pull-Request: #4217.
@thomaseizinger thomaseizinger mentioned this pull request Aug 19, 2023
thomaseizinger pushed a commit that referenced this pull request Aug 20, 2023
Since #3973, gossipsub now fully supports wasm targets. It functions properly when added as a dependency on its own. However, when trying to use it under libp2p by activating a feature, it's not possible and the compiler will raise an error like `unresolved import libp2p::gossipsub`. This pull request enables the use of gossipsub for wasm targets when it's activated as a feature of the libp2p dependency.

Pull-Request: #4217.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants