Skip to content

Commit

Permalink
Merge branch 'behaviour-on-event' into behaviour-on-event-2
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs committed Nov 14, 2022
2 parents bb15c1f + 73e0dcf commit 10a5a62
Show file tree
Hide file tree
Showing 54 changed files with 442 additions and 245 deletions.
2 changes: 1 addition & 1 deletion .github/mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defaults:
commit_message_template: |
{{ title }} (#{{ number }})
{{ body | get_section("## Commit Message body", "") }}
{{ body | get_section("## Description", "") }}
pull_request_rules:
- name: Ask to resolve conflict
Expand Down
11 changes: 6 additions & 5 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Description
## Description

<!-- Please write a summary of your changes and why you made them.-->
<!-- This section will appear as the commit message after merging. Please craft it accordingly. -->

## Notes

<!-- Any notes or remarks you'd like to make about the PR. -->

## Links to any relevant issues

<!-- Reference any related issues.-->


## Open Questions

<!-- Unresolved questions, if any. -->
Expand All @@ -19,6 +23,3 @@
- [ ] I have made corresponding changes to the documentation
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] A changelog entry has been made in the appropriate crates

<!-- The below text will appear as the commit message body once we squash-merge the PR. -->
## Commit message body
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
- Remove deprecated features: `tcp-tokio`, `mdns-tokio`, `dns-tokio`, `tcp-async-io`, `mdns-async-io`, `dns-async-std`.
See [PR 3001].
- Introduce [`libp2p-tls` `v0.1.0`](transports/tls/CHANGELOG.md#010). See [PR 2945].
- Remove `NetworkBehaviour` macro export from root crate in favor of re-exported macro from `libp2p::swarm`.
Change your import from `libp2p::NetworkBehaviour` to `libp2p::swarm::NetworkBehaviour`. See [PR 3055].
- Feature-gate `NetworkBehaviour` macro behind `macros` feature flag. See [PR 3055].
- Update individual crates.
- Update to [`libp2p-autonat` `v0.89.0`](protocols/autonat/CHANGELOG.md#090).
- Update to [`libp2p-core` `v0.38.0`](core/CHANGELOG.md#0380).
Expand Down Expand Up @@ -79,6 +82,7 @@
[PR 2945]: https://github.com/libp2p/rust-libp2p/pull/2945
[PR 3001]: https://github.com/libp2p/rust-libp2p/pull/3001
[PR 2945]: https://github.com/libp2p/rust-libp2p/pull/2945
[PR 3055]: https://github.com/libp2p/rust-libp2p/pull/3055

# 0.49.0

Expand Down
24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ full = [
"ping",
"plaintext",
"pnet",
"macros",
"relay",
"rendezvous",
"request-response",
Expand All @@ -45,46 +46,46 @@ full = [
"websocket",
"yamux",
]
async-std = ["libp2p-mdns?/async-io", "libp2p-tcp?/async-io", "libp2p-dns?/async-std"]
autonat = ["dep:libp2p-autonat"]
dcutr = ["dep:libp2p-dcutr", "libp2p-metrics?/dcutr"]
deflate = ["dep:libp2p-deflate"]
dns = ["dep:libp2p-dns"]
ecdsa = ["libp2p-core/ecdsa"]
floodsub = ["dep:libp2p-floodsub"]
gossipsub = ["dep:libp2p-gossipsub", "libp2p-metrics?/gossipsub"]
identify = ["dep:libp2p-identify", "libp2p-metrics?/identify"]
kad = ["dep:libp2p-kad", "libp2p-metrics?/kad"]
gossipsub = ["dep:libp2p-gossipsub", "libp2p-metrics?/gossipsub"]
macros = ["libp2p-swarm/macros"]
mdns = ["dep:libp2p-mdns"]
tls = ["dep:libp2p-tls"]
metrics = ["dep:libp2p-metrics"]
mdns = ["dep:libp2p-mdns"]
mplex = ["dep:libp2p-mplex"]
noise = ["dep:libp2p-noise"]
ping = ["dep:libp2p-ping", "libp2p-metrics?/ping"]
plaintext = ["dep:libp2p-plaintext"]
pnet = ["dep:libp2p-pnet"]
relay = ["dep:libp2p-relay", "libp2p-metrics?/relay"]
request-response = ["dep:libp2p-request-response"]
rendezvous = ["dep:libp2p-rendezvous"]
request-response = ["dep:libp2p-request-response"]
rsa = ["libp2p-core/rsa"]
secp256k1 = ["libp2p-core/secp256k1"]
serde = ["libp2p-core/serde", "libp2p-kad?/serde", "libp2p-gossipsub?/serde"]
tcp = ["dep:libp2p-tcp"]
tokio = ["libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio"]
uds = ["dep:libp2p-uds"]
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js"]
wasm-ext = ["dep:libp2p-wasm-ext"]
wasm-ext-websocket = ["wasm-ext", "libp2p-wasm-ext?/websocket"]
websocket = ["dep:libp2p-websocket"]
yamux = ["dep:libp2p-yamux"]
secp256k1 = ["libp2p-core/secp256k1"]
rsa = ["libp2p-core/rsa"]
ecdsa = ["libp2p-core/ecdsa"]
serde = ["libp2p-core/serde", "libp2p-kad?/serde", "libp2p-gossipsub?/serde"]
tokio = ["libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio"]
async-std = ["libp2p-mdns?/async-io", "libp2p-tcp?/async-io", "libp2p-dns?/async-std"]

[dependencies]
bytes = "1"
futures = "0.3.1"
futures-timer = "3.0.2" # Explicit dependency to be used in `wasm-bindgen` feature
getrandom = "0.2.3" # Explicit dependency to be used in `wasm-bindgen` feature
instant = "0.1.11" # Explicit dependency to be used in `wasm-bindgen` feature
lazy_static = "1.2"

libp2p-autonat = { version = "0.9.0", path = "protocols/autonat", optional = true }
libp2p-core = { version = "0.38.0", path = "core" }
Expand All @@ -102,11 +103,10 @@ libp2p-relay = { version = "0.14.0", path = "protocols/relay", optional = true }
libp2p-rendezvous = { version = "0.11.0", path = "protocols/rendezvous", optional = true }
libp2p-request-response = { version = "0.23.0", path = "protocols/request-response", optional = true }
libp2p-swarm = { version = "0.41.0", path = "swarm" }
libp2p-swarm-derive = { version = "0.30.1", path = "swarm-derive" }
libp2p-uds = { version = "0.37.0", path = "transports/uds", optional = true }
libp2p-wasm-ext = { version = "0.38.0", path = "transports/wasm-ext", optional = true }
libp2p-yamux = { version = "0.42.0", path = "muxers/yamux", optional = true }
multiaddr = { version = "0.15.0" }
multiaddr = { version = "0.16.0" }
parking_lot = "0.12.0"
pin-project = "1.0.0"
smallvec = "1.6.1"
Expand Down
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ fnv = "1.0"
futures = { version = "0.3.1", features = ["executor", "thread-pool"] }
futures-timer = "3"
instant = "0.1.11"
lazy_static = "1.2"
libsecp256k1 = { version = "0.7.0", optional = true }
log = "0.4"
multiaddr = { version = "0.15.0" }
multiaddr = { version = "0.16.0" }
multihash = { version = "0.16", default-features = false, features = ["std", "multihash-impl", "identity", "sha2"] }
multistream-select = { version = "0.12", path = "../misc/multistream-select" }
p256 = { version = "0.11.1", default-features = false, features = ["ecdsa"], optional = true }
parking_lot = "0.12.0"
pin-project = "1.0.0"
prost = "0.11"
once_cell = "1.16.0"
rand = "0.8"
rw-stream-sink = { version = "0.3.0", path = "../misc/rw-stream-sink" }
sha2 = "0.10.0"
Expand Down
2 changes: 1 addition & 1 deletion core/src/identity/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl PublicKey {
}

let key_len = bitstr_head[1].checked_sub(1)? as usize;
let key_buf = asn1_buf.get(4 + oids_len + 3..4 + oids_len + 3 + key_len as usize)?;
let key_buf = asn1_buf.get(4 + oids_len + 3..4 + oids_len + 3 + key_len)?;
Some(key_buf)
}
}
Expand Down
6 changes: 2 additions & 4 deletions core/src/transport/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use futures::{
task::Context,
task::Poll,
};
use lazy_static::lazy_static;
use multiaddr::{Multiaddr, Protocol};
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use rw_stream_sink::RwStreamSink;
use std::{
Expand All @@ -38,9 +38,7 @@ use std::{
pin::Pin,
};

lazy_static! {
static ref HUB: Hub = Hub(Mutex::new(FnvHashMap::default()));
}
static HUB: Lazy<Hub> = Lazy::new(|| Hub(Mutex::new(FnvHashMap::default())));

struct Hub(Mutex<FnvHashMap<NonZeroU64, ChannelSender>>);

Expand Down
10 changes: 5 additions & 5 deletions examples/chat-tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use libp2p::{
TokioMdns,
},
mplex, noise,
swarm::{SwarmBuilder, SwarmEvent},
tcp, Multiaddr, NetworkBehaviour, PeerId, Transport,
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, Multiaddr, PeerId, Transport,
};
use std::error::Error;
use tokio::io::{self, AsyncBufReadExt};
Expand All @@ -53,7 +53,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Create a random PeerId
let id_keys = identity::Keypair::generate_ed25519();
let peer_id = PeerId::from(id_keys.public());
println!("Local peer id: {:?}", peer_id);
println!("Local peer id: {peer_id:?}");

// Create a tokio-based TCP transport use noise for authenticated
// encryption and Mplex for multiplexing of substreams on a TCP stream.
Expand Down Expand Up @@ -119,7 +119,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
if let Some(to_dial) = std::env::args().nth(1) {
let addr: Multiaddr = to_dial.parse()?;
swarm.dial(addr)?;
println!("Dialed {:?}", to_dial);
println!("Dialed {to_dial:?}");
}

// Read full lines from stdin
Expand All @@ -138,7 +138,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
event = swarm.select_next_some() => {
match event {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening on {:?}", address);
println!("Listening on {address:?}");
}
SwarmEvent::Behaviour(MyBehaviourEvent::Floodsub(FloodsubEvent::Message(message))) => {
println!(
Expand Down
10 changes: 5 additions & 5 deletions examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ use libp2p::{
floodsub::{self, Floodsub, FloodsubEvent},
identity,
mdns::{Mdns, MdnsConfig, MdnsEvent},
swarm::SwarmEvent,
Multiaddr, NetworkBehaviour, PeerId, Swarm,
swarm::{NetworkBehaviour, SwarmEvent},
Multiaddr, PeerId, Swarm,
};
use std::error::Error;

Expand All @@ -70,7 +70,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Create a random PeerId
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
println!("Local peer id: {:?}", local_peer_id);
println!("Local peer id: {local_peer_id:?}");

// Set up an encrypted DNS-enabled TCP Transport over the Mplex and Yamux protocols
let transport = libp2p::development_transport(local_key).await?;
Expand Down Expand Up @@ -122,7 +122,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
if let Some(to_dial) = std::env::args().nth(1) {
let addr: Multiaddr = to_dial.parse()?;
swarm.dial(addr)?;
println!("Dialed {:?}", to_dial)
println!("Dialed {to_dial:?}")
}

// Read full lines from stdin
Expand All @@ -140,7 +140,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.publish(floodsub_topic.clone(), line.expect("Stdin not to close").as_bytes()),
event = swarm.select_next_some() => match event {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening on {:?}", address);
println!("Listening on {address:?}");
}
SwarmEvent::Behaviour(OutEvent::Floodsub(
FloodsubEvent::Message(message)
Expand Down
14 changes: 7 additions & 7 deletions examples/distributed-key-value-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ use libp2p::kad::{
use libp2p::{
development_transport, identity,
mdns::{Mdns, MdnsConfig, MdnsEvent},
swarm::SwarmEvent,
NetworkBehaviour, PeerId, Swarm,
swarm::{NetworkBehaviour, SwarmEvent},
PeerId, Swarm,
};
use std::error::Error;

Expand Down Expand Up @@ -114,7 +114,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
line = stdin.select_next_some() => handle_input_line(&mut swarm.behaviour_mut().kademlia, line.expect("Stdin not to close")),
event = swarm.select_next_some() => match event {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening in {:?}", address);
println!("Listening in {address:?}");
},
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(MdnsEvent::Discovered(list))) => {
for (peer_id, multiaddr) in list {
Expand All @@ -133,7 +133,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
QueryResult::GetProviders(Err(err)) => {
eprintln!("Failed to get providers: {:?}", err);
eprintln!("Failed to get providers: {err:?}");
}
QueryResult::GetRecord(Ok(ok)) => {
for PeerRecord {
Expand All @@ -149,7 +149,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
QueryResult::GetRecord(Err(err)) => {
eprintln!("Failed to get record: {:?}", err);
eprintln!("Failed to get record: {err:?}");
}
QueryResult::PutRecord(Ok(PutRecordOk { key })) => {
println!(
Expand All @@ -158,7 +158,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
);
}
QueryResult::PutRecord(Err(err)) => {
eprintln!("Failed to put record: {:?}", err);
eprintln!("Failed to put record: {err:?}");
}
QueryResult::StartProviding(Ok(AddProviderOk { key })) => {
println!(
Expand All @@ -167,7 +167,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
);
}
QueryResult::StartProviding(Err(err)) => {
eprintln!("Failed to put provider record: {:?}", err);
eprintln!("Failed to put provider record: {err:?}");
}
_ => {}
}
Expand Down
11 changes: 6 additions & 5 deletions examples/file-sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Locate all nodes providing the file.
let providers = network_client.get_providers(name.clone()).await;
if providers.is_empty() {
return Err(format!("Could not find provider for file {}.", name).into());
return Err(format!("Could not find provider for file {name}.").into());
}

// Request the content of the file from each node.
Expand Down Expand Up @@ -219,8 +219,9 @@ mod network {
ProtocolSupport, RequestId, RequestResponse, RequestResponseCodec, RequestResponseEvent,
RequestResponseMessage, ResponseChannel,
};
use libp2p::swarm::{ConnectionHandlerUpgrErr, SwarmBuilder, SwarmEvent};
use libp2p::{NetworkBehaviour, Swarm};
use libp2p::swarm::{
ConnectionHandlerUpgrErr, NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent,
};
use std::collections::{hash_map, HashMap, HashSet};
use std::iter;

Expand Down Expand Up @@ -506,8 +507,8 @@ mod network {
}
}
SwarmEvent::IncomingConnectionError { .. } => {}
SwarmEvent::Dialing(peer_id) => eprintln!("Dialing {}", peer_id),
e => panic!("{:?}", e),
SwarmEvent::Dialing(peer_id) => eprintln!("Dialing {peer_id}"),
e => panic!("{e:?}"),
}
}

Expand Down
11 changes: 6 additions & 5 deletions examples/gossipsub-chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ use libp2p::gossipsub::{
use libp2p::{
gossipsub, identity,
mdns::{Mdns, MdnsConfig, MdnsEvent},
swarm::NetworkBehaviour,
swarm::SwarmEvent,
NetworkBehaviour, PeerId, Swarm,
PeerId, Swarm,
};
use std::collections::hash_map::DefaultHasher;
use std::error::Error;
Expand All @@ -68,7 +69,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Create a random PeerId
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
println!("Local peer id: {}", local_peer_id);
println!("Local peer id: {local_peer_id}");

// Set up an encrypted DNS-enabled TCP Transport over the Mplex protocol.
let transport = libp2p::development_transport(local_key.clone()).await?;
Expand Down Expand Up @@ -127,19 +128,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
if let Err(e) = swarm
.behaviour_mut().gossipsub
.publish(topic.clone(), line.expect("Stdin not to close").as_bytes()) {
println!("Publish error: {:?}", e);
println!("Publish error: {e:?}");
}
},
event = swarm.select_next_some() => match event {
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(MdnsEvent::Discovered(list))) => {
for (peer_id, _multiaddr) in list {
println!("mDNS discovered a new peer: {}", peer_id);
println!("mDNS discovered a new peer: {peer_id}");
swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id);
}
},
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(MdnsEvent::Expired(list))) => {
for (peer_id, _multiaddr) in list {
println!("mDNS discover peer has expired: {}", peer_id);
println!("mDNS discover peer has expired: {peer_id}");
swarm.behaviour_mut().gossipsub.remove_explicit_peer(&peer_id);
}
},
Expand Down
Loading

0 comments on commit 10a5a62

Please sign in to comment.