-
Notifications
You must be signed in to change notification settings - Fork 74
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
Add support for "multi-stream connections" #2352
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automatically approving tomaka's pull requests. This auto-approval will be removed once more maintainers are active.
twiggy diff reportDifference in .wasm size before and after this pull request.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great work @tomaka 💯 did a first pass and everything looks good. will do another one later today or maybe tomorrow
/// Asynchronous task managing a specific multi-stream connection after it's been open. | ||
// TODO: a lot of logging disappeared | ||
async fn multi_stream_connection_task<TPlat: Platform>( | ||
mut websocket: TPlat::Connection, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's no longer a websocket, right? rename to connection
or conn
or platform_conn
whichever makes more sense
} | ||
|
||
if has_message { | ||
continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are so many loops in this function. might be good to assign a label to the main one
continue; | |
continue 'connection_task_loop |
src/libp2p/collection.rs
Outdated
|
||
/// Reverse mapping. | ||
// TODO: could be user datas in established? | ||
outbound_substreams_mapping2: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
outbound_substreams_mapping2: | |
outbound_substreams_reverse: |
// TODO: check conflicts between protocol names? | ||
|
||
let num_expected_substreams = | ||
config.request_protocols.len() + config.notifications_protocols.len() * 2 + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can u maybe add a comment explaining the logic behind this?
src/libp2p/collection.rs
Outdated
ping_timeout: Duration::from_secs(10), // TODO: hardcoded | ||
first_out_ping: now + Duration::from_secs(2), // TODO: hardcoded | ||
}), | ||
outbound_substreams_mapping: hashbrown::HashMap::with_capacity_and_hasher( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
outbound_substreams_mapping: hashbrown::HashMap::with_capacity_and_hasher( | |
outbound_substreams_map: hashbrown::HashMap::with_capacity_and_hasher( |
to be consistent with the rest of the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc #1712
Smoldot contains bindings with the JS code. Some functions in these bindings ask the JS code to open a connection to a multiaddress. On success, the JS code reports that the connection has been opened, and smoldot and the JS communicate in order to send and receive data on that connection.
Internally, smoldot performs a protocol negotiation of Noise + Yamux on these connections, and then applies the encryption and multiplexing internally.
The
connect
function that the JS must implement accepts as parameter a string multiaddress that the JS decodes in order to know the "actual" type of connection to open. At the moment, this is either a plain TCP/IP connection or a WebSocket connection.We want to add support for WebRTC. The problem with WebRTC is that it is the browser that performs the encryption and multiplexing, and thus it doesn't fit the model of one stream subdivided internally by smoldot.
For this reason, we have to add in smoldot a second type of connection named "multi-stream connections".
In order to not introduce any ambiguity, the current connections have been renamed to "single-stream connections".
This PR adds support for that other type of connection as a new module in
src/libp2p/connection/established
.In
collection.rs
,peers.rs
, andservice.rs
(which are collections of connections), multi-stream connections are handled the same way as single-stream connections except that the function to add a new connection is different.Inserting a connection previously returned a
ConnectionTask
that allows reading/writing on that connection. TheConnectionTask
struct has been renamed toSingleStreamConnectionTask
and there is now aMultiStreamConnectionTask
that has a different API.I have correspondingly updated the
Platform
trait of thelight-base
crate andbin/light-base/network_service
so that this type of connection is accessible to the Wasm node. I have also updated the JS <-> Wasm bindings, and the JS code can add support for WebRTC right now after this PR. However I've leftbin/wasm-node/rust/platform
(i.e. the code that "connects" the low-level bindings that pass u32s around and the higher-level objects) full oftodo!
s because I would like to refactor this module entirely.As always, this code hasn't been tested and to be honest it would be a bit miraculous if everything worked first try. I'm opening this PR now because I've been working on it for 2 weeks now, and I would like to land it and iterate on top of it. I believe that my API design is good, and only the implementation details are lacking.
Can be reviewed commit by commit.