Skip to content

Commit

Permalink
Merge branch 'master' of github.com:libp2p/rust-libp2p into rename-relay
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs committed Dec 14, 2022
2 parents 9805109 + be3ec6c commit 96420cb
Show file tree
Hide file tree
Showing 34 changed files with 665 additions and 350 deletions.
78 changes: 78 additions & 0 deletions .github/workflows/cache-factory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# This workflow _produces_ caches which are used to speed up pull request builds.
# The caches are split by Rust version (stable vs MSRV per crate) because those caches cannot share any artifacts.

name: Cache factory

on:
push:
branches:
- master # Caches are only created on master branch.

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
gather_msrv_versions:
runs-on: ubuntu-latest
outputs:
versions: ${{ steps.find-rust-versions.outputs.versions }}
steps:
- uses: actions/checkout@v3

- id: find-rust-versions
run: |
RUST_VERSIONS=$(cargo metadata --format-version=1 --no-deps | jq -c '.packages | map(.rust_version) | unique | del(..|nulls)')
echo "versions=${RUST_VERSIONS}" >> $GITHUB_OUTPUT
make_msrv_cache:
runs-on: ubuntu-latest
needs: gather_msrv_versions
strategy:
fail-fast: false
matrix:
rust: ${{ fromJSON(needs.gather_msrv_versions.outputs.versions) }}
steps:
- name: Install Protoc
uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # v1.1.2

- uses: actions/checkout@v3

- uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
with:
toolchain: ${{ matrix.rust }}

- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
with:
shared-key: msrv-cache

- name: Compile all crates which have MSRV ${{ matrix.rust }}
run: |
cargo metadata --format-version=1 --no-deps | \
jq -r '.packages[] | select(.rust_version == "${{ matrix.rust }}") | "+\(.rust_version) build --all-features --package \(.name)"' |
xargs --verbose -L 1 cargo
make_stable_rust_cache:
runs-on: ubuntu-latest
steps:
- name: Install Protoc
uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # v1.1.2

- uses: actions/checkout@v3

- uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
with:
toolchain: stable

- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
with:
shared-key: stable-cache

- name: Compile workspace with stable Rust
run: cargo test --all-features --all-targets --workspace --no-run

- name: Render docs
run: cargo doc --all-features --workspace

- name: Install tools
run: cargo install cargo-semver-checks --locked
29 changes: 15 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,33 @@ jobs:
with:
profile: minimal
toolchain: ${{ steps.parse-msrv.outputs.version }}
override: true

- name: Update to latest stable Rust
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
with:
shared-key: msrv-cache
save-if: false

- name: Check if ${{ matrix.crate }} compiles on MSRV (Rust ${{ steps.parse-msrv.outputs.version }})
run: cargo +${{ steps.parse-msrv.outputs.version }} build --package ${{ matrix.crate }} --all-features

- uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
with:
profile: minimal
toolchain: stable
override: true

# By default, this action already includes the active Rust toolchain in the cache key.
# We also install a separate toolchain for the MSRV check so all we need to do is add that to the key to make sure it invalidates when we update the MSRV.
# cargo separates build artifacts by Rust compiler version, meaning we can compile with different versions but cache all artifacts.
- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
with:
key: ${{ matrix.crate }}-msrv-${{ steps.parse-msrv.outputs.version }}

- name: Check if ${{ matrix.crate }} compiles on MSRV (Rust ${{ steps.parse-msrv.outputs.version }})
run: cargo +${{ steps.parse-msrv.outputs.version }} check --package ${{ matrix.crate }} --all-features

- name: Check if we compile without any features activated
run: cargo check --package ${{ matrix.crate }} --no-default-features
shared-key: stable-cache
save-if: false

- name: Run all tests
run: cargo test --package ${{ matrix.crate }} --all-features

- name: Check if we compile without any features activated
run: cargo build --package ${{ matrix.crate }} --no-default-features

- name: Check if crate has been released
id: check-released
run: |
Expand Down Expand Up @@ -138,8 +141,6 @@ jobs:
override: true

- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
with:
key: ${{ matrix.runtime }}

- run: cargo check --package libp2p --features="${{ matrix.features }}"

Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ First draft is in https://github.com/libp2p/rust-libp2p/pull/2828

| Category | Status | Target Completion | Tracking | Dependencies | Dependents |
|--------------|-------------|-------------------|-------------------------------------------------|--------------|------------|
| Optimization | in progress | Q1/2023 | https://github.com/libp2p/rust-libp2p/pull/2712 | | |
| Optimization | done | Q1/2023 | https://github.com/libp2p/rust-libp2p/pull/2712 | | |

Users of rust-libp2p like [iroh](https://github.com/n0-computer/iroh) need this for low latency
usage of `libp2p-kad`. The rust-libp2p maintainers can pick this up unless iroh folks finish the
Expand Down
2 changes: 1 addition & 1 deletion examples/chat-tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

// Create a Swarm to manage peers and events.
let mdns_behaviour = mdns::Behaviour::new(Default::default())?;
let mdns_behaviour = mdns::Behaviour::new(Default::default(), peer_id)?;
let behaviour = MyBehaviour {
floodsub: Floodsub::new(peer_id),
mdns: mdns_behaviour,
Expand Down
2 changes: 1 addition & 1 deletion examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Create a Swarm to manage peers and events
let mut swarm = {
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default())?;
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default(), local_peer_id)?;
let mut behaviour = MyBehaviour {
floodsub: Floodsub::new(local_peer_id),
mdns,
Expand Down
2 changes: 1 addition & 1 deletion examples/distributed-key-value-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Create a Kademlia behaviour.
let store = MemoryStore::new(local_peer_id);
let kademlia = Kademlia::new(local_peer_id, store);
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default())?;
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default(), local_peer_id)?;
let behaviour = MyBehaviour { kademlia, mdns };
Swarm::with_async_std_executor(transport, behaviour, local_peer_id)
};
Expand Down
2 changes: 1 addition & 1 deletion examples/gossipsub-chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Create a Swarm to manage peers and events
let mut swarm = {
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default())?;
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default(), local_peer_id)?;
let behaviour = MyBehaviour { gossipsub, mdns };
Swarm::with_async_std_executor(transport, behaviour, local_peer_id)
};
Expand Down
2 changes: 1 addition & 1 deletion examples/mdns-passive-discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let transport = libp2p::development_transport(id_keys).await?;

// Create an MDNS network behaviour.
let behaviour = mdns::async_io::Behaviour::new(mdns::Config::default())?;
let behaviour = mdns::async_io::Behaviour::new(mdns::Config::default(), peer_id)?;

// Create a Swarm that establishes connections through the given transport.
// Note that the MDNS behaviour itself will not actually inititiate any connections,
Expand Down
4 changes: 4 additions & 0 deletions protocols/autonat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# 0.10.0 [unreleased]

- Require the node's local `PeerId` to be passed into the constructor of `libp2p_autonat::Behaviour`. See [PR 3153].

- Update to `libp2p-request-response` `v0.24.0`.

- Update to `libp2p-swarm` `v0.42.0`.

[PR 3153]: https://github.com/libp2p/rust-libp2p/pull/3153

# 0.9.0

- Update to `libp2p-core` `v0.38.0`.
Expand Down
16 changes: 13 additions & 3 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use libp2p_swarm::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr,
ExpiredListenAddr, FromSwarm,
},
ConnectionHandler, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction,
PollParameters,
ConnectionHandler, ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour,
NetworkBehaviourAction, PollParameters,
};
use std::{
collections::{HashMap, VecDeque},
Expand Down Expand Up @@ -212,6 +212,9 @@ pub struct Behaviour {
pending_out_events: VecDeque<<Self as NetworkBehaviour>::OutEvent>,

probe_id: ProbeId,

listen_addresses: ListenAddresses,
external_addresses: ExternalAddresses,
}

impl Behaviour {
Expand All @@ -236,6 +239,8 @@ impl Behaviour {
last_probe: None,
pending_out_events: VecDeque::new(),
probe_id: ProbeId(0),
listen_addresses: Default::default(),
external_addresses: Default::default(),
}
}

Expand Down Expand Up @@ -288,6 +293,8 @@ impl Behaviour {
ongoing_outbound: &mut self.ongoing_outbound,
last_probe: &mut self.last_probe,
schedule_probe: &mut self.schedule_probe,
listen_addresses: &self.listen_addresses,
external_addresses: &self.external_addresses,
}
}

Expand Down Expand Up @@ -457,7 +464,7 @@ impl NetworkBehaviour for Behaviour {
Poll::Pending => is_inner_pending = true,
}

match self.as_client().poll_auto_probe(params, cx) {
match self.as_client().poll_auto_probe(cx) {
Poll::Ready(event) => self
.pending_out_events
.push_back(Event::OutboundProbe(event)),
Expand All @@ -476,6 +483,9 @@ impl NetworkBehaviour for Behaviour {
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
self.listen_addresses.on_swarm_event(&event);
self.external_addresses.on_swarn_event(&event);

match event {
FromSwarm::ConnectionEstablished(connection_established) => {
self.inner
Expand Down
23 changes: 15 additions & 8 deletions protocols/autonat/src/behaviour/as_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use futures_timer::Delay;
use instant::Instant;
use libp2p_core::{connection::ConnectionId, Multiaddr, PeerId};
use libp2p_request_response::{self as request_response, OutboundFailure, RequestId};
use libp2p_swarm::{AddressScore, NetworkBehaviourAction, PollParameters};
use libp2p_swarm::{
AddressScore, ExternalAddresses, ListenAddresses, NetworkBehaviourAction, PollParameters,
};
use rand::{seq::SliceRandom, thread_rng};
use std::{
collections::{HashMap, VecDeque},
Expand Down Expand Up @@ -97,6 +99,9 @@ pub struct AsClient<'a> {

pub last_probe: &'a mut Option<Instant>,
pub schedule_probe: &'a mut Delay,

pub listen_addresses: &'a ListenAddresses,
pub external_addresses: &'a ExternalAddresses,
}

impl<'a> HandleInnerEvent for AsClient<'a> {
Expand Down Expand Up @@ -146,6 +151,8 @@ impl<'a> HandleInnerEvent for AsClient<'a> {

if let Ok(address) = response.result {
// Update observed address score if it is finite.
#[allow(deprecated)]
// TODO: Fix once we report `AddressScore` through `FromSwarm` event.
let score = params
.external_addresses()
.find_map(|r| (r.addr == address).then_some(r.score))
Expand Down Expand Up @@ -188,17 +195,17 @@ impl<'a> HandleInnerEvent for AsClient<'a> {
}

impl<'a> AsClient<'a> {
pub fn poll_auto_probe(
&mut self,
params: &mut impl PollParameters,
cx: &mut Context<'_>,
) -> Poll<OutboundProbeEvent> {
pub fn poll_auto_probe(&mut self, cx: &mut Context<'_>) -> Poll<OutboundProbeEvent> {
match self.schedule_probe.poll_unpin(cx) {
Poll::Ready(()) => {
self.schedule_probe.reset(self.config.retry_interval);

let mut addresses: Vec<_> = params.external_addresses().map(|r| r.addr).collect();
addresses.extend(params.listened_addresses());
let addresses = self
.external_addresses
.iter()
.chain(self.listen_addresses.iter())
.cloned()
.collect();

let probe_id = self.probe_id.next();
let event = match self.do_probe(probe_id, addresses) {
Expand Down
3 changes: 3 additions & 0 deletions protocols/dcutr/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
- Declare `InboundUpgradeError` and `OutboundUpgradeError` as type aliases instead of renames.
This is a workaround for a missing feature in `cargo semver-checks`. See [PR 3213].

- Require the node's local `PeerId` to be passed into the constructor of `libp2p_dcutr::Behaviour`. See [PR 3153].

[PR 3213]: https://github.com/libp2p/rust-libp2p/pull/3213
[PR 3153]: https://github.com/libp2p/rust-libp2p/pull/3153

# 0.8.0

Expand Down
2 changes: 1 addition & 1 deletion protocols/dcutr/examples/dcutr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn main() -> Result<(), Box<dyn Error>> {
"/TODO/0.0.1".to_string(),
local_key.public(),
)),
dcutr: dcutr::behaviour::Behaviour::new(),
dcutr: dcutr::behaviour::Behaviour::new(local_peer_id),
};

let mut swarm = match ThreadPool::new() {
Expand Down
Loading

0 comments on commit 96420cb

Please sign in to comment.