Skip to content

Commit 2c52cd0

Browse files
Merge branch 'master' into discv5.2
2 parents 2948146 + ea4711d commit 2c52cd0

24 files changed

+1004
-360
lines changed

.github/pull_request_template.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Description
2+
3+
<!--
4+
A summary of what this pull request achieves and a rough list of changes.
5+
-->
6+
7+
## Notes & open questions
8+
9+
<!--
10+
Any notes, remarks or open questions you have to make about the PR. Feel free to remove this section if it's unnecessary
11+
-->
12+
13+
## Change checklist
14+
15+
- [ ] Self-review
16+
- [ ] Documentation updates if relevant
17+
- [ ] Tests if relevant

.github/workflows/build.yml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
cargo-fmt:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v2
9+
- uses: actions/checkout@v3
1010
- name: Get latest version of stable rust
1111
run: rustup update stable
1212
- name: Check formatting with cargofmt
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
needs: cargo-fmt
1717
steps:
18-
- uses: actions/checkout@v2
18+
- uses: actions/checkout@v3
1919
- name: Get latest version of stable rust
2020
run: rustup update stable
2121
- name: Install protobuf compiler for the libp2p-core dependency
@@ -26,7 +26,7 @@ jobs:
2626
runs-on: ubuntu-latest
2727
needs: cargo-fmt
2828
steps:
29-
- uses: actions/checkout@v2
29+
- uses: actions/checkout@v3
3030
- name: Get latest version of stable rust
3131
run: rustup update stable
3232
- name: Run tests in release
@@ -37,7 +37,7 @@ jobs:
3737
image: rust
3838
needs: cargo-fmt
3939
steps:
40-
- uses: actions/checkout@v2
40+
- uses: actions/checkout@v3
4141
- name: Get latest version of stable rust
4242
run: rustup update stable
4343
- name: Install protobuf compiler for the libp2p-core dependency
@@ -50,8 +50,20 @@ jobs:
5050
container:
5151
image: rust
5252
steps:
53-
- uses: actions/checkout@v2
53+
- uses: actions/checkout@v3
5454
- name: Get latest version of stable rust
5555
run: rustup update stable
5656
- name: Check rustdoc links
5757
run: RUSTDOCFLAGS="--deny broken_intra_doc_links" cargo doc --verbose --workspace --no-deps --document-private-items
58+
cargo-udeps:
59+
name: cargo-udeps
60+
runs-on: ubuntu-latest
61+
needs: cargo-fmt
62+
steps:
63+
- uses: actions/checkout@v3
64+
- name: Install Rust nightly
65+
run: rustup toolchain install nightly
66+
- name: Install cargo-udeps
67+
run: cargo install cargo-udeps --locked --force
68+
- name: Run cargo udeps to identify unused crates in the dependency graph
69+
run: cargo +nightly udeps --tests --all-targets --release

Cargo.toml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ categories = ["network-programming", "asynchronous"]
1212
exclude = [".gitignore", ".github/*"]
1313

1414
[dependencies]
15-
enr = { version = "0.7.0", features = ["k256", "ed25519"] }
15+
enr = { version = "0.8.1", features = ["k256", "ed25519"] }
1616
tokio = { version = "1.15.0", features = ["net", "sync", "macros", "rt"] }
17-
tokio-stream = "0.1.8"
18-
tokio-util = { version = "0.6.9", features = ["time"] }
1917
libp2p-core = { version = "0.36.0", optional = true }
2018
zeroize = { version = "1.4.3", features = ["zeroize_derive"] }
2119
futures = "0.3.19"
@@ -43,14 +41,11 @@ more-asserts = "0.2.2"
4341
[dev-dependencies]
4442
rand_07 = { package = "rand", version = "0.7" }
4543
quickcheck = "0.9.2"
46-
env_logger = "0.9.0"
47-
hex-literal = "0.3.4"
48-
simple_logger = "1.16.0"
49-
tokio-util = { version = "0.6.9", features = ["time"] }
5044
tokio = { version = "1.15.0", features = ["full"] }
5145
rand_xorshift = "0.3.0"
5246
rand_core = "0.6.3"
5347
clap = { version = "3.1", features = ["derive"] }
48+
if-addrs = "0.10.1"
5449

5550
[features]
5651
libp2p = ["libp2p-core"]

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ A simple example of creating this service is as follows:
3838

3939
```rust
4040
use discv5::{enr, enr::{CombinedKey, NodeId}, TokioExecutor, Discv5, Discv5ConfigBuilder};
41+
use discv5::socket::ListenConfig;
4142
use std::net::SocketAddr;
4243

43-
// listening address and port
44-
let listen_addr = "0.0.0.0:9000".parse::<SocketAddr>().unwrap();
45-
4644
// construct a local ENR
4745
let enr_key = CombinedKey::generate_secp256k1();
4846
let enr = enr::EnrBuilder::new("v4").build(&enr_key).unwrap();
@@ -54,8 +52,14 @@ A simple example of creating this service is as follows:
5452
.build()
5553
.unwrap();
5654

55+
// configuration for the sockets to listen on
56+
let listen_config = ListenConfig::Ipv4 {
57+
ip: Ipv4Addr::UNSPECIFIED,
58+
port: 9000,
59+
};
60+
5761
// default configuration
58-
let config = Discv5ConfigBuilder::new().build();
62+
let config = Discv5ConfigBuilder::new(listen_config).build();
5963

6064
// construct the discv5 server
6165
let mut discv5: Discv5 = Discv5::new(enr, enr_key, config).unwrap();
@@ -65,7 +69,7 @@ A simple example of creating this service is as follows:
6569
// discv5.add_enr(<ENR>)
6670

6771
// start the discv5 server
68-
runtime.block_on(discv5.start(listen_addr));
72+
runtime.block_on(discv5.start());
6973

7074
// run a find_node query
7175
runtime.block_on(async {

examples/custom_executor.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
//! $ cargo run --example custom_executor <BASE64ENR>
1010
//! ```
1111
12-
use discv5::{enr, enr::CombinedKey, Discv5, Discv5ConfigBuilder, Discv5Event};
13-
use std::net::SocketAddr;
12+
use discv5::{enr, enr::CombinedKey, Discv5, Discv5ConfigBuilder, Discv5Event, ListenConfig};
13+
use std::net::Ipv4Addr;
1414

1515
fn main() {
1616
// allows detailed logging with the RUST_LOG env variable
@@ -22,7 +22,10 @@ fn main() {
2222
.try_init();
2323

2424
// listening address and port
25-
let listen_addr = "0.0.0.0:9000".parse::<SocketAddr>().unwrap();
25+
let listen_config = ListenConfig::Ipv4 {
26+
ip: Ipv4Addr::UNSPECIFIED,
27+
port: 9000,
28+
};
2629

2730
let enr_key = CombinedKey::generate_secp256k1();
2831
// construct a local ENR
@@ -36,7 +39,7 @@ fn main() {
3639
.unwrap();
3740

3841
// default configuration - uses the current executor
39-
let config = Discv5ConfigBuilder::new().build();
42+
let config = Discv5ConfigBuilder::new(listen_config).build();
4043

4144
// construct the discv5 server
4245
let mut discv5: Discv5 = Discv5::new(enr, enr_key, config).unwrap();
@@ -61,7 +64,7 @@ fn main() {
6164

6265
runtime.block_on(async {
6366
// start the discv5 service
64-
discv5.start(listen_addr).await.unwrap();
67+
discv5.start().await.unwrap();
6568
println!("Server started");
6669

6770
// get an event stream

examples/find_nodes.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@ use clap::Parser;
1919
use discv5::{
2020
enr,
2121
enr::{k256, CombinedKey},
22-
Discv5, Discv5ConfigBuilder, Discv5Event,
22+
Discv5, Discv5ConfigBuilder, Discv5Event, ListenConfig,
2323
};
2424
use std::{
25-
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
25+
net::{Ipv4Addr, Ipv6Addr},
2626
time::Duration,
2727
};
2828
use tracing::{info, warn};
2929

3030
#[derive(Parser)]
3131
struct FindNodesArgs {
32-
/// Type of socket to bind ['ds', 'ip4', 'ip6']. The dual stack option will enable mapped
33-
/// addresses over an IpV6 socket.
32+
/// Type of socket to bind ['ds', 'ip4', 'ip6'].
3433
#[clap(long, default_value_t = SocketKind::Ds)]
3534
socket_kind: SocketKind,
3635
/// IpV4 to advertise in the ENR. This is needed so that other IpV4 nodes can connect to us.
@@ -43,6 +42,10 @@ struct FindNodesArgs {
4342
/// randomly.
4443
#[clap(long)]
4544
port: Option<u16>,
45+
/// Port to bind for ipv6. If none is provided, a random one in the 9000 - 9999 range will be picked
46+
/// randomly.
47+
#[clap(long)]
48+
port6: Option<u16>,
4649
/// Use a default test key.
4750
#[clap(long)]
4851
use_test_key: bool,
@@ -67,13 +70,19 @@ async fn main() {
6770
let port = args
6871
.port
6972
.unwrap_or_else(|| (rand::random::<u16>() % 1000) + 9000);
73+
let port6 = args.port.unwrap_or_else(|| loop {
74+
let port6 = (rand::random::<u16>() % 1000) + 9000;
75+
if port6 != port {
76+
return port6;
77+
}
78+
});
7079

7180
let enr_key = if args.use_test_key {
7281
// A fixed key for testing
7382
let raw_key =
7483
hex::decode("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
7584
.unwrap();
76-
let secret_key = k256::ecdsa::SigningKey::from_bytes(&raw_key).unwrap();
85+
let secret_key = k256::ecdsa::SigningKey::from_slice(&raw_key).unwrap();
7786
CombinedKey::from(secret_key)
7887
} else {
7988
// use a new key if specified
@@ -93,29 +102,28 @@ async fn main() {
93102
if let Some(ip6) = args.enr_ip6 {
94103
// if the given address is the UNSPECIFIED address we want to advertise localhost
95104
if ip6.is_unspecified() {
96-
builder.ip6(Ipv6Addr::LOCALHOST).udp6(port);
105+
builder.ip6(Ipv6Addr::LOCALHOST).udp6(port6);
97106
} else {
98-
builder.ip6(ip6).udp6(port);
107+
builder.ip6(ip6).udp6(port6);
99108
}
100109
}
101110
builder.build(&enr_key).unwrap()
102111
};
103112

113+
// the address to listen on.
114+
let listen_config = match args.socket_kind {
115+
SocketKind::Ip4 => ListenConfig::new_ipv4(Ipv4Addr::UNSPECIFIED, port),
116+
SocketKind::Ip6 => ListenConfig::new_ipv6(Ipv6Addr::UNSPECIFIED, port6),
117+
SocketKind::Ds => ListenConfig::default()
118+
.with_ipv4(Ipv4Addr::UNSPECIFIED, port)
119+
.with_ipv6(Ipv6Addr::UNSPECIFIED, port6),
120+
};
121+
104122
// default configuration with packet filtering
105-
// let config = Discv5ConfigBuilder::new().enable_packet_filter().build();
123+
// let config = Discv5ConfigBuilder::new(listen_config).enable_packet_filter().build();
106124

107125
// default configuration without packet filtering
108-
let config = Discv5ConfigBuilder::new()
109-
.ip_mode(match args.socket_kind {
110-
SocketKind::Ip4 => discv5::IpMode::Ip4,
111-
SocketKind::Ip6 => discv5::IpMode::Ip6 {
112-
enable_mapped_addresses: false,
113-
},
114-
SocketKind::Ds => discv5::IpMode::Ip6 {
115-
enable_mapped_addresses: true,
116-
},
117-
})
118-
.build();
126+
let config = Discv5ConfigBuilder::new(listen_config).build();
119127

120128
info!("Node Id: {}", enr.node_id());
121129
if args.enr_ip6.is_some() || args.enr_ip4.is_some() {
@@ -127,12 +135,6 @@ async fn main() {
127135
enr.udp4_socket()
128136
);
129137
}
130-
// the address to listen on.
131-
let bind_addr = match args.socket_kind {
132-
SocketKind::Ip4 => Ipv4Addr::UNSPECIFIED.into(),
133-
SocketKind::Ip6 | SocketKind::Ds => Ipv6Addr::UNSPECIFIED.into(),
134-
};
135-
let socket_addr = SocketAddr::new(bind_addr, port);
136138

137139
// construct the discv5 server
138140
let mut discv5: Discv5 = Discv5::new(enr, enr_key, config).unwrap();
@@ -154,7 +156,7 @@ async fn main() {
154156
}
155157

156158
// start the discv5 service
157-
discv5.start(socket_addr).await.unwrap();
159+
discv5.start().await.unwrap();
158160
let mut event_stream = discv5.event_stream().await.unwrap();
159161
let check_evs = args.events;
160162

examples/request_enr.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
//!
1414
//! This requires the "libp2p" feature.
1515
#[cfg(feature = "libp2p")]
16-
use discv5::{enr, enr::CombinedKey, Discv5, Discv5Config};
16+
use discv5::Discv5ConfigBuilder;
1717
#[cfg(feature = "libp2p")]
18-
use std::net::SocketAddr;
18+
use discv5::ListenConfig;
19+
#[cfg(feature = "libp2p")]
20+
use discv5::{enr, enr::CombinedKey, Discv5};
21+
#[cfg(feature = "libp2p")]
22+
use std::net::Ipv4Addr;
1923

2024
#[cfg(not(feature = "libp2p"))]
2125
fn main() {}
@@ -31,15 +35,18 @@ async fn main() {
3135
.try_init();
3236

3337
// listening address and port
34-
let listen_addr = "0.0.0.0:9000".parse::<SocketAddr>().unwrap();
38+
let listen_config = ListenConfig::Ipv4 {
39+
ip: Ipv4Addr::UNSPECIFIED,
40+
port: 9000,
41+
};
3542

3643
// generate a new enr key
3744
let enr_key = CombinedKey::generate_secp256k1();
3845
// construct a local ENR
3946
let enr = enr::EnrBuilder::new("v4").build(&enr_key).unwrap();
4047

4148
// default discv5 configuration
42-
let config = Discv5Config::default();
49+
let config = Discv5ConfigBuilder::new(listen_config).build();
4350

4451
let multiaddr = std::env::args()
4552
.nth(1)
@@ -49,7 +56,7 @@ async fn main() {
4956
let mut discv5: Discv5 = Discv5::new(enr, enr_key, config).unwrap();
5057

5158
// start the discv5 service
52-
discv5.start(listen_addr).await.unwrap();
59+
discv5.start().await.unwrap();
5360

5461
// search for the ENR
5562
match discv5.request_enr(multiaddr).await {

examples/simple_server.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//! $ cargo run --example simple_server -- <ENR-IP> <ENR-PORT> <BASE64ENR>
1111
//! ```
1212
13-
use discv5::{enr, enr::CombinedKey, Discv5, Discv5Config, Discv5Event};
14-
use std::net::{Ipv4Addr, SocketAddr};
13+
use discv5::{enr, enr::CombinedKey, Discv5, Discv5ConfigBuilder, Discv5Event, ListenConfig};
14+
use std::net::Ipv4Addr;
1515

1616
#[tokio::main]
1717
async fn main() {
@@ -37,7 +37,10 @@ async fn main() {
3737
};
3838

3939
// listening address and port
40-
let listen_addr = "0.0.0.0:9000".parse::<SocketAddr>().unwrap();
40+
let listen_config = ListenConfig::Ipv4 {
41+
ip: Ipv4Addr::UNSPECIFIED,
42+
port: 9000,
43+
};
4144

4245
let enr_key = CombinedKey::generate_secp256k1();
4346

@@ -69,7 +72,7 @@ async fn main() {
6972
}
7073

7174
// default configuration
72-
let config = Discv5Config::default();
75+
let config = Discv5ConfigBuilder::new(listen_config).build();
7376

7477
// construct the discv5 server
7578
let mut discv5: Discv5 = Discv5::new(enr, enr_key, config).unwrap();
@@ -93,7 +96,7 @@ async fn main() {
9396
}
9497

9598
// start the discv5 service
96-
discv5.start(listen_addr).await.unwrap();
99+
discv5.start().await.unwrap();
97100
println!("Server started");
98101

99102
// get an event stream

0 commit comments

Comments
 (0)