Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

chore: upgrade to latest libp2p master #292

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 139 additions & 62 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ cidr = "0.2.1"
csv = "1.2.1"
env_logger = "0.10.0"
futures = "0.3.28"
libp2p = { git = "https://github.com/libp2p/rust-libp2p.git", branch = "bump-yamux", version = "0.51", default-features = false, features = ["dns", "async-std", "noise", "tcp", "yamux", "identify", "kad", "ping", "mplex", "metrics", "quic", "rsa", "macros"] }
libp2p = { git = "https://github.com/libp2p/rust-libp2p.git", branch = "master", version = "0.52", default-features = false, features = ["dns", "async-std", "noise", "tcp", "yamux", "identify", "kad", "ping", "mplex", "metrics", "quic", "rsa", "macros"] }
log = "0.4.17"
prometheus-client = "0.19.0"
prometheus-client = "0.20.0"
parking_lot = "0.12"
regex = "1"
void = "1.0.2"
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rustlang/rust:nightly-bullseye as builder
FROM rust:1.69 as builder
WORKDIR /usr/src/kademlia-exporter

# Cache dependencies between test runs,
Expand All @@ -8,14 +8,14 @@ WORKDIR /usr/src/kademlia-exporter
RUN mkdir -p ./src/
RUN echo "fn main() {}" > ./src/main.rs
COPY ./Cargo.* ./
RUN cargo +nightly build --release
RUN cargo build --release

COPY . .
# This is in order to make sure `main.rs`s mtime timestamp is updated to avoid the dummy `main`
# remaining in the binary.
# https://github.com/rust-lang/cargo/issues/9598
RUN touch ./src/main.rs
RUN cargo +nightly build --release
RUN cargo build --release

FROM debian:bullseye-slim
COPY --from=builder /usr/src/kademlia-exporter/target/release/kademlia-exporter /usr/local/bin/kademlia-exporter
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Exporter exposing [Prometheus](https://prometheus.io/) metrics for
### Quickstart

```bash
cargo +nightly run -- --config-file config.toml
cargo run -- --config-file config.toml

curl localhost:8080/metrics
```
Expand Down
2 changes: 0 additions & 2 deletions rust-toolchain.toml

This file was deleted.

58 changes: 20 additions & 38 deletions src/exporter/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ use futures::future::Either;
use futures::prelude::*;
use futures::ready;
use libp2p::bandwidth::BandwidthSinks;
use libp2p::StreamProtocol;
use libp2p::TransportExt;
use libp2p::{
core::{
self, multiaddr::Protocol, muxing::StreamMuxerBox, transport::Boxed, transport::Transport,
multiaddr::Protocol, muxing::StreamMuxerBox, transport::Boxed, transport::Transport,
upgrade, Multiaddr,
},
dns, identify,
identity::Keypair,
kad::{record::store::MemoryStore, Kademlia, KademliaConfig},
metrics::{Metrics, Recorder},
mplex, noise, ping, swarm,
noise, ping, swarm,
swarm::NetworkBehaviour,
swarm::{DialError, NetworkInfo, SwarmBuilder, SwarmEvent},
tcp, yamux, InboundUpgradeExt, OutboundUpgradeExt, PeerId, Swarm,
tcp, yamux, PeerId, Swarm,
};
use prometheus_client::registry::Registry;
use std::sync::Arc;
Expand All @@ -27,11 +28,8 @@ use std::{
pin::Pin,
task::{Context, Poll},
time::Duration,
usize,
};

mod global_only;

pub struct Client {
swarm: Swarm<MyBehaviour>,
bandwidth_sinks: Arc<BandwidthSinks>,
Expand Down Expand Up @@ -210,7 +208,8 @@ impl MyBehaviour {
kademlia_config.set_max_packet_size(8000);

if let Some(protocol_name) = protocol_name {
kademlia_config.set_protocol_names(vec![protocol_name.into_bytes().into()]);
kademlia_config.set_protocol_names(vec![StreamProtocol::try_from_owned(protocol_name)
.expect("configuration to contain valid stream protocol name")]);
}

if disjoint_query_paths {
Expand Down Expand Up @@ -245,31 +244,12 @@ impl MyBehaviour {
fn build_transport(keypair: Keypair) -> (Boxed<(PeerId, StreamMuxerBox)>, Arc<BandwidthSinks>) {
let tcp = tcp::async_io::Transport::new(tcp::Config::default().nodelay(true));

let authentication_config = {
let noise_keypair_spec = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&keypair)
.expect("can only fail in case of a hardware bug; since this signing is performed only \
once and at initialization, we're taking the bet that the inconvenience of a very \
rare panic here is basically zero");

let xx_config = noise::NoiseConfig::xx(noise_keypair_spec);

xx_config.into_authenticated()
};
let authentication_config = { noise::Config::new(&keypair).unwrap() };

let multiplexing_config = {
let mut mplex_config = mplex::MplexConfig::new();
mplex_config.set_max_buffer_behaviour(mplex::MaxBufferBehaviour::Block);
mplex_config.set_max_buffer_size(usize::MAX);

let mut yamux_config = yamux::YamuxConfig::default();
// Enable proper flow-control: window updates are only sent when
// buffered data has been consumed.
yamux_config.set_window_update_mode(yamux::WindowUpdateMode::on_read());

core::upgrade::SelectUpgrade::new(yamux_config, mplex_config)
.map_inbound(core::muxing::StreamMuxerBox::new)
.map_outbound(core::muxing::StreamMuxerBox::new)
};
let mut yamux_config = yamux::Config::default();
// Enable proper flow-control: window updates are only sent when
// buffered data has been consumed.
yamux_config.set_window_update_mode(yamux::WindowUpdateMode::on_read());

let quic_transport = {
let mut config = libp2p::quic::Config::new(&keypair);
Expand All @@ -281,13 +261,15 @@ fn build_transport(keypair: Keypair) -> (Boxed<(PeerId, StreamMuxerBox)>, Arc<Ba
// addresses in most Dhts dialing private IP addresses can easily be (and
// has been) interpreted as a port-scan by ones hosting provider.
let (transport, bandwidth_sinks) = block_on(dns::DnsConfig::system(
global_only::GlobalIpOnly::new(libp2p::core::transport::OrTransport::new(
quic_transport,
tcp.upgrade(upgrade::Version::V1Lazy)
.authenticate(authentication_config)
.multiplex(multiplexing_config)
.timeout(Duration::from_secs(20)),
)),
libp2p::core::transport::global_only::Transport::new(
libp2p::core::transport::OrTransport::new(
quic_transport,
tcp.upgrade(upgrade::Version::V1Lazy)
.authenticate(authentication_config)
.multiplex(yamux_config)
.timeout(Duration::from_secs(20)),
),
),
))
.unwrap()
.map(|either_output, _| match either_output {
Expand Down
82 changes: 0 additions & 82 deletions src/exporter/client/global_only.rs

This file was deleted.

5 changes: 2 additions & 3 deletions src/exporter/node_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ impl NodeStore {

// Remove old offline nodes.
let length = self.nodes.len();
self.nodes.drain_filter(|_, n| {
(Instant::now() - n.last_seen) > Duration::from_secs(60 * 60 * 12)
});
self.nodes
.retain(|_, n| (Instant::now() - n.last_seen) <= Duration::from_secs(60 * 60 * 12));
self.metrics
.meta_offline_nodes_removed
.inc_by((length - self.nodes.len()).try_into().unwrap());
Expand Down
3 changes: 0 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#![feature(ip)]
#![feature(hash_drain_filter)]

use async_std::task;
use prometheus_client::encoding::text::encode;
use prometheus_client::registry::Registry;
Expand Down