rust-libp2p
rust-libp2p copied to clipboard
swarm/: NetworkBehaviour cross-communication
Description
Today there is no way to send generic local and remote node information from one NetworkBehaviour to another NetworkBehaviour, apart from NetworkBehaviour::inject_new_external_addr.
Examples of such generic information could be:
- Supported protocols by the local peer.
- Supported protocols of a remote peer.
- Addresses of a remote peer.
Motivation
One example use-case is the exchange of information betweenlibp2p-identify and libp2p-kad to support Kademlia Client Mode (see https://github.com/libp2p/rust-libp2p/issues/2032).
libp2p-kadneeds to know whether a remote peer advertises/ipfs/kad/1.0.0via the identify protocol to decide whether to include the remote peer in its routing table or not.libp2p-identifyneeds to know whether the local node runs in client or in server mode, i.e. supports the/ipfs/kad/1.0.0or not.
See https://github.com/libp2p/rust-libp2p/pull/2521 which is currently blocked on this.
Proposal
Below is an initial action plan proposal based on a meeting with @MarcoPolo.
- Proof of concept 1 (communicating remote metadata across behaviours)
- Extend
NetworkBehaviourActionwithNetworkBehaviourAction::ReportRemoteMetaData, containing a delta update. - Add
NetworkBehaviourAction::inject_remote_metadata. - Write a
SelectNetworkBehaviourwhich takes twoNetworkBehaviourimplementations and passesReportRemoteMetaDataemitted by one behaviour to the other viainject_remote_metadata. - Showcase how
libp2p-kadcan learn supported protocols of remote peers fromlibp2p-identify.
- Extend
- Proof of concept 2 (communicating local metadata across behaviours)
- Extend
NetworkBehaviourActionwithNetworkBehaviourAction::ReportSupportedProtocolwhich eachNetworkBehaviouremits when it supports a protocol as a listener and which is passed to otherNetworkBehaviours via theSelectNetworkBehaviour. - Showcase how remote peers can learn that
libp2p-kadmoved from client mode into server mode vialibp2p-identify.
- Extend
- In case proof of concepts looks good, don't merge but consolidate
inject_*methods into singleinject_eventmethod with a singleenum. Motivation here is to not have yet anotherinject_***method onNetworkBehaviour. - Adjust proof of concepts, implement
Selectbehaviour logic inlibp2p-swarm-deriveand merge. - Add method on
Swarm(e.g.add_remote_metadata) which would callNetworkBehaviour::inject_event(Event::RemoteMetaData)on the rootNetworkBehaviour. - Design
AddressBooktype which eachNetworkBehaviourcan instantiate and pass a reference frominject_eventinto to track peer metadata.
Open questions
Are you planning to do it yourself in a pull request?
No, or at least not any time soon.