How to combine QUIC and relay client transports? #3653
-
I'm attempting to combine a QUIC transport and relay client transport before passing it into a Here's an example that mentions QUIC and relay: #2883 (comment) Here is my incorrect attempt: // Generate a relay transport and client behaviour if relay client mode is selected
let (relay_transport, relay_client) = if network_config.relay_client {
let (relay_transport, relay_client) = relay::client::new(key_pair.public().to_peer_id());
(Some(relay_transport), Some(relay_client))
} else {
(None, None)
};
// Configure the QUIC transport and define the listening address
let quic_config = quic::Config::new(&key_pair);
let quic_multiaddr =
format!("/ip4/0.0.0.0/udp/{}/quic-v1", network_config.quic_port).parse()?;
// Create QUIC transport (provides transport, security and multiplexing in a single protocol)
let quic_transport = quic::tokio::Transport::new(quic_config)
.map(|(p, c), _| (p, StreamMuxerBox::new(c)))
.boxed();
/* This next code block is definitely incorrect... */
// Create a combined transport incorporating QUIC and relay
let transport = if let Some(relay_transport) = relay_transport {
OrTransport::new(relay_transport, quic_transport)
.map(|either_output, _| match either_output {
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
})
.boxed()
} else {
quic_transport
};
// Instantiate the custom network behaviour with default configuration and the libp2p peer ID
let behaviour = Behaviour::new(&network_config, peer_id, key_pair, relay_client)?;
// Initialise a swarm with QUIC transports, our composed network behaviour and the default configuration parameters
let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, peer_id)
.connection_limits(network_config.connection_limits())
.dial_concurrency_factor(network_config.dial_concurrency_factor.try_into()?)
.per_connection_event_buffer_size(network_config.per_connection_event_buffer_size)
.notify_handler_buffer_size(network_config.notify_handler_buffer_size.try_into()?)
.build(); Compiler output:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
You've picked a tricky one :) The problem is that Try wrapping your let relay_transport = Toggle::from(relay_transport); cc @mxinden I remember we talked about |
Beta Was this translation helpful? Give feedback.
-
Another idea would be to not make the relay-transport optional. Unless you hand it a relayed address to listen on, it doesn't do anything and thus doesn't hurt having it in there unconditionally. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the swift assistance, @thomaseizinger. Trying your first suggestion (converting the let (relay_transport, relay_client) = if network_config.relay_client {
let (relay_transport, relay_client) = relay::client::new(key_pair.public().to_peer_id());
(Some(relay_transport), Some(relay_client))
} else {
(None, None)
};
let quic_transport = quic::tokio::Transport::new(quic_config)
.map(|(p, c), _| (p, StreamMuxerBox::new(c)))
.boxed();
let relay_transport = Toggle::from(relay_transport);
let transport = OrTransport::new(relay_transport, quic_transport)
.map(|either_output, _| match either_output {
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
})
.boxed();
And the second suggestion (do not make the relay transport optional) results in a "mismatched types" error: let (relay_transport, relay_client) = relay::client::new(key_pair.public().to_peer_id());
let relay_client = Some(relay_client);
let quic_transport = quic::tokio::Transport::new(quic_config)
.map(|(p, c), _| (p, StreamMuxerBox::new(c)))
.boxed();
let transport = OrTransport::new(relay_transport, quic_transport)
.map(|either_output, _| match either_output {
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
})
.boxed();
|
Beta Was this translation helpful? Give feedback.
-
Related: #3143. |
Beta Was this translation helpful? Give feedback.
Thanks for the swift assistance, @thomaseizinger.
Trying your first suggestion (converting the
Option<relay_transport>
to aToggle
) results in a "trait bounds not satisfied" error: