diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/transport/libp2p.rs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/src/transport/libp2p.rs b/src/transport/libp2p.rs index 077b9ac..54bfc76 100644 --- a/src/transport/libp2p.rs +++ b/src/transport/libp2p.rs @@ -6,7 +6,9 @@ //! without the peer ID, which a connection finds out. Messages go one per stream of the //! `/continuum/1` protocol: their length (4 bytes, big endian), then the message as //! `wire` encodes it. A send is done once the stream is written and closed; a connection -//! that can't be made, or a stream that can't be written, refuses it. +//! that can't be made, or a stream that can't be written, refuses it. A stream's protocol +//! goes with its first data, without waiting for the other side to agree +//! (`swarm_config`): opening a stream costs no round trip. //! //! Connected peers tell each other where they listen (libp2p's identify protocol), so a //! node learns the addresses of peers that dialled it. A node publishes where it can be @@ -722,10 +724,25 @@ fn network_swarm(key: Keypair, features: Features) -> Result<Swarm<Behaviour>> { Behaviour::new(key, relay_client, features) .map_err(|error| Box::new(error) as Box<dyn std::error::Error + Send + Sync>) })? - .with_swarm_config(|config| config.with_idle_connection_timeout(IDLE_TIMEOUT)) + .with_swarm_config(|config| swarm_config(config, features)) .build()) } +/// The swarm's settings. Streams are opened without waiting for the other side to agree +/// on the protocol (`V1Lazy`): the protocol and the first data go together, saving a +/// round trip per message (one stream each, module docs). That is safe only towards +/// peers sure to speak the protocol, so not with public libp2p infrastructure +/// (`Libp2pConfig::public`), whose nodes speak only some: there a protocol they don't +/// would fail late, and unclearly. +fn swarm_config(config: ::libp2p::swarm::Config, features: Features) -> ::libp2p::swarm::Config { + let config = config.with_idle_connection_timeout(IDLE_TIMEOUT); + if features.public { + config + } else { + config.with_substream_upgrade_protocol_override(::libp2p::core::upgrade::Version::V1Lazy) + } +} + fn memory_swarm(key: Keypair, features: Features) -> Result<Swarm<Behaviour>> { use ::libp2p::Transport as _; use ::libp2p::core::transport::{MemoryTransport, OrTransport}; @@ -744,7 +761,7 @@ fn memory_swarm(key: Keypair, features: Features) -> Result<Swarm<Behaviour>> { transport, Behaviour::new(&key, relay_client, features)?, peer_id, - ::libp2p::swarm::Config::with_tokio_executor().with_idle_connection_timeout(IDLE_TIMEOUT), + swarm_config(::libp2p::swarm::Config::with_tokio_executor(), features), )) } |
