Skip to content

Commit

Permalink
chore(p2p_bootstrap): use latest apis in place of deprecated ones
Browse files Browse the repository at this point in the history
  • Loading branch information
CHr15F0x committed Nov 2, 2023
1 parent 2f61213 commit a63047d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
16 changes: 8 additions & 8 deletions crates/p2p_bootstrap/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ use libp2p::autonat;
use libp2p::dcutr;
use libp2p::identify;
use libp2p::identity;
use libp2p::kad::{record::store::MemoryStore, Kademlia, KademliaConfig, KademliaEvent};
use libp2p::kad::{self, record::store::MemoryStore};
use libp2p::ping;
use libp2p::relay;
use libp2p::swarm::NetworkBehaviour;
use libp2p::StreamProtocol;

#[derive(NetworkBehaviour)]
#[behaviour(out_event = "BootstrapEvent", event_process = false)]
#[behaviour(to_swarm = "BootstrapEvent", event_process = false)]
pub struct BootstrapBehaviour {
relay: relay::Behaviour,
autonat: autonat::Behaviour,
dcutr: dcutr::Behaviour,
ping: ping::Behaviour,
identify: identify::Behaviour,
pub kademlia: Kademlia<MemoryStore>,
pub kademlia: kad::Behaviour<MemoryStore>,
}

pub const KADEMLIA_PROTOCOL_NAME: &str = "/pathfinder/kad/1.0.0";
Expand All @@ -30,15 +30,15 @@ impl BootstrapBehaviour {
// FIXME: we're also missing the starting '/'
const PROTOCOL_VERSION: &str = "starknet/0.9.1";

let mut kademlia_config = KademliaConfig::default();
let mut kademlia_config = kad::Config::default();
kademlia_config.set_record_ttl(Some(Duration::from_secs(0)));
kademlia_config.set_provider_record_ttl(Some(PROVIDER_PUBLICATION_INTERVAL * 3));
kademlia_config.set_provider_publication_interval(Some(PROVIDER_PUBLICATION_INTERVAL));
// FIXME: this make sure that the DHT we're implementing is incompatible with the "default" IPFS
// DHT from libp2p.
kademlia_config.set_protocol_names(vec![StreamProtocol::new(KADEMLIA_PROTOCOL_NAME)]);

let kademlia = Kademlia::with_config(
let kademlia = kad::Behaviour::with_config(
pub_key.to_peer_id(),
MemoryStore::new(pub_key.to_peer_id()),
kademlia_config,
Expand Down Expand Up @@ -67,7 +67,7 @@ pub enum BootstrapEvent {
Dcutr(dcutr::Event),
Ping(ping::Event),
Identify(Box<identify::Event>),
Kademlia(KademliaEvent),
Kademlia(kad::Event),
}

impl From<relay::Event> for BootstrapEvent {
Expand Down Expand Up @@ -100,8 +100,8 @@ impl From<identify::Event> for BootstrapEvent {
}
}

impl From<KademliaEvent> for BootstrapEvent {
fn from(event: KademliaEvent) -> Self {
impl From<kad::Event> for BootstrapEvent {
fn from(event: kad::Event) -> Self {
BootstrapEvent::Kademlia(event)
}
}
34 changes: 10 additions & 24 deletions crates/p2p_bootstrap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ use std::time::Duration;
use clap::Parser;
use futures::StreamExt;
use libp2p::core::upgrade;
use libp2p::dns;
use libp2p::identify::{Event as IdentifyEvent, Info as IdentifyInfo};
use libp2p::identify;
use libp2p::identity::Keypair;
use libp2p::noise;
use libp2p::swarm::{SwarmBuilder, SwarmEvent};
use libp2p::Multiaddr;
use libp2p::Transport;
use libp2p::swarm::{Config, SwarmEvent};
use libp2p::{dns, noise};
use libp2p::{Multiaddr, Swarm, Transport};
use serde::Deserialize;
use zeroize::Zeroizing;

Expand Down Expand Up @@ -47,17 +45,6 @@ impl zeroize::Zeroize for IdentityConfig {
}
}

pub struct TokioExecutor();

impl libp2p::swarm::Executor for TokioExecutor {
fn exec(
&self,
future: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + 'static + Send>>,
) {
tokio::task::spawn(future);
}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
Expand All @@ -84,7 +71,7 @@ async fn main() -> anyhow::Result<()> {
tracing::info!(%peer_id, "Starting up");

let transport = libp2p::tcp::tokio::Transport::new(libp2p::tcp::Config::new());
let transport = dns::TokioDnsConfig::system(transport).unwrap();
let transport = dns::tokio::Transport::system(transport).unwrap();
let noise_config =
noise::Config::new(&keypair).expect("Signing libp2p-noise static DH keypair failed.");
let transport = transport
Expand All @@ -93,13 +80,12 @@ async fn main() -> anyhow::Result<()> {
.multiplex(libp2p::yamux::Config::default())
.boxed();

let mut swarm = SwarmBuilder::with_executor(
let mut swarm = Swarm::new(
transport,
behaviour::BootstrapBehaviour::new(keypair.public()),
keypair.public().to_peer_id(),
TokioExecutor(),
)
.build();
Config::with_tokio_executor(),
);

swarm.listen_on(args.listen_on)?;

Expand Down Expand Up @@ -131,10 +117,10 @@ async fn main() -> anyhow::Result<()> {
Some(event) = swarm.next() => {
match event {
SwarmEvent::Behaviour(behaviour::BootstrapEvent::Identify(e)) => {
if let IdentifyEvent::Received {
if let identify::Event::Received {
peer_id,
info:
IdentifyInfo {
identify::Info {
listen_addrs,
protocols,
observed_addr,
Expand Down

0 comments on commit a63047d

Please sign in to comment.