diff --git a/Cargo.lock b/Cargo.lock index 8708b1d1..47eae8e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -144,11 +144,11 @@ dependencies = [ [[package]] name = "ahash" -version = "0.7.6" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" dependencies = [ - "getrandom 0.2.8", + "cfg-if", "once_cell", "version_check", ] @@ -1743,6 +1743,12 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" dependencies = [ "ahash", ] @@ -2001,7 +2007,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.3", ] [[package]] @@ -2135,9 +2141,9 @@ checksum = "348108ab3fba42ec82ff6e9564fc4ca0247bdccdc68dd8af9764bbc79c3c8ffb" [[package]] name = "libp2p" -version = "0.50.1" +version = "0.51.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c7b0104790be871edcf97db9bd2356604984e623a08d825c3f27852290266b8" +checksum = "53e068bb83ef4e0bed45de5ca4a4118018ac1f70ea3ecb1f4878742d08a97473" dependencies = [ "bytes", "futures", @@ -2148,6 +2154,7 @@ dependencies = [ "libp2p-dns", "libp2p-gossipsub", "libp2p-identify", + "libp2p-identity", "libp2p-kad", "libp2p-mdns", "libp2p-metrics", @@ -2161,25 +2168,21 @@ dependencies = [ "libp2p-webrtc", "libp2p-yamux", "multiaddr", - "parking_lot 0.12.1", "pin-project", - "smallvec", ] [[package]] name = "libp2p-core" -version = "0.38.0" +version = "0.39.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6a8fcd392ff67af6cc3f03b1426c41f7f26b6b9aff2dc632c1c56dd649e571f" +checksum = "9b7f8b7d65c070a5a1b5f8f0510648189da08f787b8963f8e21219e0710733af" dependencies = [ - "asn1_der", - "bs58", - "ed25519-dalek", "either", "fnv", "futures", "futures-timer", "instant", + "libp2p-identity", "log", "multiaddr", "multihash", @@ -2187,26 +2190,21 @@ dependencies = [ "once_cell", "parking_lot 0.12.1", "pin-project", - "prost", - "prost-build", + "quick-protobuf", "rand 0.8.5", - "ring", "rw-stream-sink", - "sec1", "serde", - "sha2 0.10.6", "smallvec", "thiserror", "unsigned-varint", "void", - "zeroize", ] [[package]] name = "libp2p-dns" -version = "0.38.0" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e42a271c1b49f789b92f7fc87749fa79ce5c7bdc88cbdfacb818a4bca47fec5" +checksum = "146ff7034daae62077c415c2376b8057368042df6ab95f5432ad5e88568b1554" dependencies = [ "futures", "libp2p-core", @@ -2218,12 +2216,12 @@ dependencies = [ [[package]] name = "libp2p-gossipsub" -version = "0.43.0" +version = "0.44.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a173171c71c29bb156f98886c7c4824596de3903dadf01e2e79d2ccdcf38cd9f" +checksum = "708235886ca7c8f3792a8683ef81f9b7fb0a952bbd15fe5038b7610689a88390" dependencies = [ "asynchronous-codec", - "base64 0.13.1", + "base64 0.21.0", "byteorder", "bytes", "fnv", @@ -2231,12 +2229,12 @@ dependencies = [ "hex_fmt", "instant", "libp2p-core", + "libp2p-identity", "libp2p-swarm", "log", "prometheus-client", - "prost", - "prost-build", - "prost-codec", + "quick-protobuf", + "quick-protobuf-codec", "rand 0.8.5", "regex", "serde", @@ -2249,30 +2247,53 @@ dependencies = [ [[package]] name = "libp2p-identify" -version = "0.41.1" +version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c052d0026f4817b44869bfb6810f4e1112f43aec8553f2cb38881c524b563abf" +checksum = "40d1da1f75baf824cfdc80f6aced51f7cbf8dc14e32363e9443570a80d4ee337" dependencies = [ "asynchronous-codec", + "either", "futures", "futures-timer", "libp2p-core", + "libp2p-identity", "libp2p-swarm", "log", "lru", - "prost", - "prost-build", - "prost-codec", + "quick-protobuf", + "quick-protobuf-codec", "smallvec", "thiserror", "void", ] +[[package]] +name = "libp2p-identity" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff6c9cb71e2333d31f18e7556b9a5f1d0a2e013effc9325e36f436be65fe7bd2" +dependencies = [ + "asn1_der", + "bs58", + "ed25519-dalek", + "log", + "multiaddr", + "multihash", + "prost", + "prost-build", + "quick-protobuf", + "rand 0.8.5", + "ring", + "serde", + "thiserror", + "zeroize", +] + [[package]] name = "libp2p-kad" -version = "0.42.1" +version = "0.43.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2766dcd2be8c87d5e1f35487deb22d765f49c6ae1251b3633efe3b25698bd3d2" +checksum = "5bc57e02d7ad49a63792370f24b829af38f34982ff56556e59e4cb325a4dbf6b" dependencies = [ "arrayvec", "asynchronous-codec", @@ -2283,10 +2304,10 @@ dependencies = [ "futures-timer", "instant", "libp2p-core", + "libp2p-identity", "libp2p-swarm", "log", - "prost", - "prost-build", + "quick-protobuf", "rand 0.8.5", "serde", "sha2 0.10.6", @@ -2299,9 +2320,9 @@ dependencies = [ [[package]] name = "libp2p-mdns" -version = "0.42.0" +version = "0.43.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04f378264aade9872d6ccd315c0accc18be3a35d15fc1b9c36e5b6f983b62b5b" +checksum = "687a0b54ee8f7106be36f012b32bd30faceeb4cd857ebad96e512566886ffea5" dependencies = [ "data-encoding", "futures", @@ -2319,9 +2340,9 @@ dependencies = [ [[package]] name = "libp2p-metrics" -version = "0.11.0" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ad8a64f29da86005c86a4d2728b8a0719e9b192f4092b609fd8790acb9dec55" +checksum = "a42ec91e227d7d0dafa4ce88b333cdf5f277253873ab087555c92798db2ddd46" dependencies = [ "libp2p-core", "libp2p-identify", @@ -2332,9 +2353,9 @@ dependencies = [ [[package]] name = "libp2p-mplex" -version = "0.38.0" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03805b44107aa013e7cbbfa5627b31c36cbedfdfb00603c0311998882bc4bace" +checksum = "4d34780b514b159e6f3fd70ba3e72664ec89da28dca2d1e7856ee55e2c7031ba" dependencies = [ "asynchronous-codec", "bytes", @@ -2350,18 +2371,18 @@ dependencies = [ [[package]] name = "libp2p-noise" -version = "0.41.0" +version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a978cb57efe82e892ec6f348a536bfbd9fee677adbe5689d7a93ad3a9bffbf2e" +checksum = "9c87c2803deffeae94108072a0387f8c9ff494af68a4908454c11c811e27b5e5" dependencies = [ "bytes", "curve25519-dalek 3.2.0", "futures", "libp2p-core", + "libp2p-identity", "log", "once_cell", - "prost", - "prost-build", + "quick-protobuf", "rand 0.8.5", "sha2 0.10.6", "snow", @@ -2373,10 +2394,11 @@ dependencies = [ [[package]] name = "libp2p-ping" -version = "0.41.0" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "929fcace45a112536e22b3dcfd4db538723ef9c3cb79f672b98be2cc8e25f37f" +checksum = "3e57759c19c28a73ef1eb3585ca410cefb72c1a709fcf6de1612a378e4219202" dependencies = [ + "either", "futures", "futures-timer", "instant", @@ -2389,15 +2411,16 @@ dependencies = [ [[package]] name = "libp2p-quic" -version = "0.7.0-alpha" +version = "0.7.0-alpha.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01e7c867e95c8130667b24409d236d37598270e6da69b3baf54213ba31ffca59" +checksum = "c6b26abd81cd2398382a1edfe739b539775be8a90fa6914f39b2ab49571ec735" dependencies = [ "bytes", "futures", "futures-timer", "if-watch", "libp2p-core", + "libp2p-identity", "libp2p-tls", "log", "parking_lot 0.12.1", @@ -2410,9 +2433,9 @@ dependencies = [ [[package]] name = "libp2p-request-response" -version = "0.23.0" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3236168796727bfcf4927f766393415361e2c644b08bedb6a6b13d957c9a4884" +checksum = "872b9d63fed44d9f81110c30be6c7ca5593a093576d2a95c5d018051e294d2e9" dependencies = [ "async-trait", "bytes", @@ -2428,9 +2451,9 @@ dependencies = [ [[package]] name = "libp2p-swarm" -version = "0.41.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2a35472fe3276b3855c00f1c032ea8413615e030256429ad5349cdf67c6e1a0" +checksum = "92c22a83d70703d140092c969c1ca06ecdffff8ca7ce8ac2fd3b7eb2c1f0da86" dependencies = [ "either", "fnv", @@ -2450,9 +2473,9 @@ dependencies = [ [[package]] name = "libp2p-swarm-derive" -version = "0.31.0" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d527d5827582abd44a6d80c07ff8b50b4ee238a8979e05998474179e79dc400" +checksum = "0fba456131824ab6acd4c7bf61e9c0f0a3014b5fc9868ccb8e10d344594cdc4f" dependencies = [ "heck", "quote", @@ -2461,9 +2484,9 @@ dependencies = [ [[package]] name = "libp2p-tcp" -version = "0.38.0" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4b257baf6df8f2df39678b86c578961d48cc8b68642a12f0f763f56c8e5858d" +checksum = "33d33698596d7722d85d3ab0c86c2c322254fce1241e91208e3679b4eb3026cf" dependencies = [ "futures", "futures-timer", @@ -2477,13 +2500,14 @@ dependencies = [ [[package]] name = "libp2p-tls" -version = "0.1.0-alpha" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7905ce0d040576634e8a3229a7587cc8beab83f79db6023800f1792895defa8" +checksum = "ff08d13d0dc66e5e9ba6279c1de417b84fa0d0adc3b03e5732928c180ec02781" dependencies = [ "futures", "futures-rustls", "libp2p-core", + "libp2p-identity", "rcgen 0.10.0", "ring", "rustls 0.20.8", @@ -2495,9 +2519,9 @@ dependencies = [ [[package]] name = "libp2p-webrtc" -version = "0.4.0-alpha" +version = "0.4.0-alpha.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb6cd86dd68cba72308ea05de1cebf3ba0ae6e187c40548167955d4e3970f6a" +checksum = "ffd0c3af5921e3bdd5fecdf3eef3be5a197def64c1d23cd3ea79abcadbd461f7" dependencies = [ "async-trait", "asynchronous-codec", @@ -2507,12 +2531,12 @@ dependencies = [ "hex", "if-watch", "libp2p-core", + "libp2p-identity", "libp2p-noise", "log", "multihash", - "prost", - "prost-build", - "prost-codec", + "quick-protobuf", + "quick-protobuf-codec", "rand 0.8.5", "rcgen 0.9.3", "serde", @@ -2526,9 +2550,9 @@ dependencies = [ [[package]] name = "libp2p-yamux" -version = "0.42.0" +version = "0.43.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f63594a0aa818642d9d4915c791945053877253f08a3626f13416b5cd928a29" +checksum = "d048cd82f72c8800655aa1ee9b808ea8c9d523a649d3579b3ef0cfe23952d7fd" dependencies = [ "futures", "libp2p-core", @@ -2571,11 +2595,11 @@ dependencies = [ [[package]] name = "lru" -version = "0.8.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6e8aaa3f231bb4bd57b84b2d5dc3ae7f350265df8aa96492e0bc394a1571909" +checksum = "71e7d46de488603ffdd5f30afbc64fbba2378214a2c3a2fb83abf3d33126df17" dependencies = [ - "hashbrown", + "hashbrown 0.13.2", ] [[package]] @@ -2658,9 +2682,9 @@ dependencies = [ [[package]] name = "multiaddr" -version = "0.16.0" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4aebdb21e90f81d13ed01dc84123320838e53963c2ca94b60b305d3fa64f31e" +checksum = "3b53e0cc5907a5c216ba6584bf74be8ab47d6d6289f72793b2dddbf15dc3bf8c" dependencies = [ "arrayref", "byteorder", @@ -2687,9 +2711,9 @@ dependencies = [ [[package]] name = "multihash" -version = "0.16.3" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c346cf9999c631f002d8f977c4eaeaa0e6386f16007202308d0b3757522c2cc" +checksum = "835d6ff01d610179fbce3de1694d007e500bf33a7f29689838941d6bf783ae40" dependencies = [ "core2", "digest 0.10.6", @@ -3321,21 +3345,21 @@ dependencies = [ [[package]] name = "prometheus-client" -version = "0.18.1" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83cd1b99916654a69008fd66b4f9397fbe08e6e51dfe23d4417acf5d3b8cb87c" +checksum = "5d6fa99d535dd930d1249e6c79cb3c2915f9172a540fe2b02a4c8f9ca954721e" dependencies = [ "dtoa", "itoa", "parking_lot 0.12.1", - "prometheus-client-derive-text-encode", + "prometheus-client-derive-encode", ] [[package]] -name = "prometheus-client-derive-text-encode" -version = "0.3.0" +name = "prometheus-client-derive-encode" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66a455fbcb954c1a7decf3c586e860fd7889cddf4b8e164be736dbac95a953cd" +checksum = "72b6a5217beb0ad503ee7fa752d451c905113d70721b937126158f3106a48cc1" dependencies = [ "proc-macro2", "quote", @@ -3374,19 +3398,6 @@ dependencies = [ "which", ] -[[package]] -name = "prost-codec" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dc34979ff898b6e141106178981ce2596c387ea6e62533facfc61a37fc879c0" -dependencies = [ - "asynchronous-codec", - "bytes", - "prost", - "thiserror", - "unsigned-varint", -] - [[package]] name = "prost-derive" version = "0.11.6" @@ -3416,6 +3427,28 @@ version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" +[[package]] +name = "quick-protobuf" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d6da84cc204722a989e01ba2f6e1e276e190f22263d0cb6ce8526fcdb0d2e1f" +dependencies = [ + "byteorder", +] + +[[package]] +name = "quick-protobuf-codec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1693116345026436eb2f10b677806169c1a1260c1c60eaaffe3fb5a29ae23d8b" +dependencies = [ + "asynchronous-codec", + "bytes", + "quick-protobuf", + "thiserror", + "unsigned-varint", +] + [[package]] name = "quinn-proto" version = "0.9.2" @@ -4303,18 +4336,18 @@ checksum = "95059e91184749cb66be6dc994f67f182b6d897cb3df74a5bf66b5e709295fd8" [[package]] name = "thiserror" -version = "1.0.38" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" +checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.38" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" +checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" dependencies = [ "proc-macro2", "quote", @@ -5504,6 +5537,7 @@ version = "0.3.0" dependencies = [ "async-trait", "deno_core", + "either", "env_logger", "libp2p", "log", diff --git a/ext/libp2p/Cargo.toml b/ext/libp2p/Cargo.toml index ee968ba0..976419e8 100644 --- a/ext/libp2p/Cargo.toml +++ b/ext/libp2p/Cargo.toml @@ -18,9 +18,10 @@ log.workspace = true smallvec = "1.10.0" tokio = { workspace = true, features = ["rt", "macros", "sync", "time"] } tokio-util = "0.7.7" +either = "1.8.1" [dependencies.libp2p] -version = "0.50.1" +version = "0.51.1" features = [ # "async-std", # "autonat", @@ -28,6 +29,7 @@ features = [ # "deflate", "dns", # "ecdsa", + "ed25519", # "floodsub", # "gossipsub", "identify", diff --git a/ext/libp2p/lib.rs b/ext/libp2p/lib.rs index 2a4eab11..4545f4e9 100644 --- a/ext/libp2p/lib.rs +++ b/ext/libp2p/lib.rs @@ -4,8 +4,9 @@ use std::rc::Rc; use deno_core::anyhow::{anyhow, Context, Result}; use deno_core::error::AnyError; use deno_core::{include_js_files, op, Extension, OpState, ZeroCopyBuf}; +use libp2p::identity::PeerId; use libp2p::multiaddr::Protocol; -use libp2p::{Multiaddr, PeerId}; +use libp2p::Multiaddr; use peer::PeerNode; pub use peer::PeerNodeConfig; diff --git a/ext/libp2p/peer.rs b/ext/libp2p/peer.rs index 9e32aa09..a821386d 100644 --- a/ext/libp2p/peer.rs +++ b/ext/libp2p/peer.rs @@ -39,21 +39,22 @@ pub use config::PeerNodeConfig; use deno_core::anyhow::Result; use deno_core::{AsyncResult, Resource}; -use libp2p::core::either::EitherError; use std::collections::{hash_map, HashMap}; use std::error::Error; use std::rc::Rc; +use either::Either; use tokio::sync::{mpsc, oneshot}; use tokio::task::JoinHandle; use libp2p::core::muxing::StreamMuxerBox; -use libp2p::core::{transport, upgrade, Multiaddr, PeerId}; +use libp2p::core::{transport, upgrade, Multiaddr}; use libp2p::futures::StreamExt; +use libp2p::identity::{Keypair, PeerId}; use libp2p::multiaddr::Protocol; use libp2p::swarm::{ConnectionHandlerUpgrErr, NetworkBehaviour, Swarm, SwarmEvent}; -use libp2p::{identify, identity, noise, ping, yamux, Transport}; +use libp2p::{identify, noise, ping, yamux, Transport}; /// A Zinnia peer node wrapping rust-libp2p and providing higher-level APIs /// for consumption by Deno ops. @@ -71,7 +72,7 @@ impl PeerNode { pub fn spawn(config: PeerNodeConfig) -> Result> { // Create a new random public/private key pair // Zinnia will always generate a new key pair on (re)start - let id_keys = identity::Keypair::generate_ed25519(); + let id_keys = Keypair::generate_ed25519(); let peer_id = id_keys.public().to_peer_id(); let tcp_transport = create_transport(&id_keys)?; @@ -175,7 +176,7 @@ impl Resource for PeerNode { } pub fn create_transport( - id_keys: &identity::Keypair, + id_keys: &Keypair, ) -> Result, noise::NoiseError> { // Setup the transport + multiplex + auth // Zinnia will hard-code this configuration initially. @@ -256,10 +257,7 @@ impl EventLoop { &mut self, event: SwarmEvent< NodeBehaviourEvent, - EitherError< - EitherError>, - std::io::Error, - >, + Either>, std::io::Error>, >, ) { match event { @@ -468,7 +466,7 @@ mod tests { init(); let cancellation_token = CancellationToken::new(); - let listener_id_keys = identity::Keypair::generate_ed25519(); + let listener_id_keys = Keypair::generate_ed25519(); let listener_peer_id = listener_id_keys.public().to_peer_id(); let listener_transport = create_transport(&listener_id_keys).unwrap(); diff --git a/ext/libp2p/peer/behaviour.rs b/ext/libp2p/peer/behaviour.rs index 2c99564f..d13ec4b7 100644 --- a/ext/libp2p/peer/behaviour.rs +++ b/ext/libp2p/peer/behaviour.rs @@ -37,12 +37,14 @@ use std::{ time::Duration, }; -use libp2p::core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId}; +use libp2p::core::{ConnectedPoint, Endpoint, Multiaddr}; +use libp2p::identity::PeerId; use libp2p::swarm::{ behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}, dial_opts::DialOpts, - IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, + NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, }; +use libp2p::swarm::{ConnectionDenied, ConnectionId, THandler, THandlerInEvent, THandlerOutEvent}; use smallvec::SmallVec; @@ -198,7 +200,7 @@ pub struct RequestResponse { /// The protocol configuration. config: RequestResponseConfig, /// Pending events to return from `poll`. - pending_events: VecDeque>, + pending_events: VecDeque>, /// The currently connected peers, their pending outbound and inbound responses and their known, /// reachable addresses, if any. connected: HashMap>, @@ -249,10 +251,8 @@ impl RequestResponse { }; if let Some(request) = self.try_send_request(peer, request) { - let handler = self.new_handler(); self.pending_events.push_back(NetworkBehaviourAction::Dial { opts: DialOpts::peer_id(*peer).build(), - handler, }); self.pending_outbound_requests .entry(*peer) @@ -472,10 +472,7 @@ impl RequestResponse { } } - fn on_dial_failure( - &mut self, - DialFailure { peer_id, .. }: DialFailure<::ConnectionHandler>, - ) { + fn on_dial_failure(&mut self, DialFailure { peer_id, .. }: DialFailure) { if let Some(peer) = peer_id { // If there are pending outgoing requests when a dial failure occurs, // it is implied that we are not connected to the peer, since pending @@ -503,22 +500,39 @@ impl NetworkBehaviour for RequestResponse { type ConnectionHandler = RequestResponseHandler; type OutEvent = RequestResponseEvent; - fn new_handler(&mut self) -> Self::ConnectionHandler { - RequestResponseHandler::new( - self.config.connection_keep_alive, - self.config.request_timeout, - ) - } + fn handle_pending_outbound_connection( + &mut self, + _connection_id: ConnectionId, + maybe_peer: Option, + _addresses: &[Multiaddr], + _effective_role: Endpoint, + ) -> Result, ConnectionDenied> { + let peer = match maybe_peer { + None => return Ok(vec![]), + Some(peer) => peer, + }; - fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec { let mut addresses = Vec::new(); - if let Some(connections) = self.connected.get(peer) { + if let Some(connections) = self.connected.get(&peer) { addresses.extend(connections.iter().filter_map(|c| c.address.clone())) } - if let Some(more) = self.addresses.get(peer) { + if let Some(more) = self.addresses.get(&peer) { addresses.extend(more.into_iter().cloned()); } - addresses + Ok(addresses) + } + + fn handle_established_outbound_connection( + &mut self, + _: ConnectionId, + _: PeerId, + _: &Multiaddr, + _: Endpoint, + ) -> Result, ConnectionDenied> { + Ok(RequestResponseHandler::new( + self.config.connection_keep_alive, + self.config.request_timeout, + )) } fn on_swarm_event(&mut self, event: FromSwarm) { @@ -546,8 +560,7 @@ impl NetworkBehaviour for RequestResponse { &mut self, peer: PeerId, connection: ConnectionId, - event: <::Handler as - libp2p::swarm::ConnectionHandler>::OutEvent, + event: THandlerOutEvent, ) { match event { RequestResponseHandlerEvent::Response { @@ -635,7 +648,7 @@ impl NetworkBehaviour for RequestResponse { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll> { + ) -> Poll>> { if let Some(ev) = self.pending_events.pop_front() { return Poll::Ready(ev); } else if self.pending_events.capacity() > EMPTY_QUEUE_SHRINK_THRESHOLD {