-
Notifications
You must be signed in to change notification settings - Fork 960
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
misc/multistream-select: Treat protocol as String
and not [u8]
#2831
Comments
Should we remove the |
For the sake of type safety, what do you think of a new type wrapping a |
I'd like to work on this.
Are we thinking something along these line? I am using But doing so, rippled its effect into places where dialers and listeners are used, as in |
It is a fairly invasive change yes. Happy to mentor you along if you want to pick it up! See some of the communication in #2798. I think we should store the protocol as String or &str everywhere to begin with and not convert it. Some places also use Cow so the |
Many abstractions that require |
@thomaseizinger yes, I would like to work on this.
So currently it is
I will try to look for places where Cow is used. But off the top of your head, lmk if you know places where this is the case. Just out of curiosity: was there any particular reason for choosing Bytes over String in the first place? |
In order to not have to decide between the two, we can have We will either need to use I think using
|
@thomaseizinger yeah, not-compiling draft PRs exploring the usage of |
@mxinden do you know? |
👍 another option which I have seen used more would be
I don't unfortunately. One day I would like to see rust-libp2p support both multistream-select and Protocol Select. With that in mind, I would suggest Thanks @efarg for the work and thanks @thomaseizinger for the guidance! |
|
@efarg Are you still working on this? I'd otherwise pick it up soon :) |
Yes, I am very much working on it. But I am a bit lost. So here is the diff of changes made for this issue: https://paste.debian.net/hidden/524606d1/ Upon compilation, E0759 will be emitted. And I don't know how to solve it. I am thinking, the way we use Some queries: (1) Why do we even need String? Are we ever modifying the data in Protocol struct? Can't we just use '&str' instead of Cow?
(2) This implies that right now, libp2p-kad depends on multistream-select. But there is no mention of multistream-select in libp2p-kad's toml. @mxinden
(3) With @mxinden 's last comment, should the idea of using |
Thanks for sharing! I'd tackle this from a different angle. Try and remove the rust-libp2p/core/src/upgrade.rs Lines 123 to 129 in 2025de3
And replace rust-libp2p/core/src/upgrade.rs Lines 137 to 147 in 2025de3
The reason we need a newtype is because some protocols construct their identifiers dynamically and thus need to allocate whereas others provide just a Once that phase is done, you can adopt Hope this helps! :) |
@thomaseizinger what is meant by replacing I have been reading about newtype from ch. 19 in The Book, and can't find anything which would allow such replacement. |
You are right, it can't be replaced 1 to 1! Essentially, removing Basically what we are doing is removing one layer of "genericness". Instead of allowing many different types that all have to implement Does that make sense? :) |
@thomaseizinger sharing some progress. This patch gives 29 errors. I am thinking about how to solve them. https://paste.debian.net/hidden/3c3659e9/ Major problem is with
I am thinking instead of replacing |
Can you open it as a pull-request please? I'd be able to make inline comments then :)
Yes, that is exactly the signature change we want to see!
I am not sure this makes sense? The point of this change is to remove a layer of abstraction. By virtue, this means we will get more concrete in some places, e.g. the type in the iterator is always fixed! |
Great! I will open a pull request. |
I am still in favor of this change. @efarg let me know in case you are still interested in contributing this change. (Sorry in case I am missing a related discussion.) |
They are working on it! We do have a conversation going on Element about this :) |
Previously, a protocol could be any sequence of bytes as long as it started with `/`. Now, we directly parse a protocol as `String` which enforces it to be valid UTF8. To notify users of this change, we delete the `ProtocolName` trait. The new requirement is that users need to provide a type that implements `AsRef<str>`. We also add a `StreamProtocol` newtype in `libp2p-swarm` which provides an easy way for users to ensure their protocol strings are compliant. The newtype enforces that protocol strings start with `/`. `StreamProtocol` also implements `AsRef<str>`, meaning users can directly use it in their upgrades. `multistream-select` by itself only changes marginally with this patch. The only thing we enforce in the type-system is that protocols must implement `AsRef<str>`. Resolves: #2831. Pull-Request: #3746.
Previously, a protocol could be any sequence of bytes as long as it started with `/`. Now, we directly parse a protocol as `String` which enforces it to be valid UTF8. To notify users of this change, we delete the `ProtocolName` trait. The new requirement is that users need to provide a type that implements `AsRef<str>`. We also add a `StreamProtocol` newtype in `libp2p-swarm` which provides an easy way for users to ensure their protocol strings are compliant. The newtype enforces that protocol strings start with `/`. `StreamProtocol` also implements `AsRef<str>`, meaning users can directly use it in their upgrades. `multistream-select` by itself only changes marginally with this patch. The only thing we enforce in the type-system is that protocols must implement `AsRef<str>`. Resolves: libp2p#2831. Pull-Request: libp2p#3746.
Description
Today we treat protocols negotiated via
multistream-select
as a[u8]
. Instead we should treat it as aString
.Motivation
libp2p-identify
treats protocols as strings. See spec.EitherName
in favor ofEither
#2798 (comment)Current Implementation
rust-libp2p/misc/multistream-select/src/protocol.rs
Lines 69 to 77 in 3d3666e
Are you planning to do it yourself in a pull request?
No
The text was updated successfully, but these errors were encountered: