Skip to content

Commit

Permalink
chore: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Jan 22, 2024
1 parent 2486c30 commit e7536fc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion crates/torii/libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tokio.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
tracing-subscriber = "0.3.18"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing.workspace = true
async-trait = "0.1.77"
regex = "1.10.3"
4 changes: 2 additions & 2 deletions crates/torii/libp2p/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Libp2pClient {
let local_key = identity::Keypair::generate_ed25519();
let peer_id = PeerId::from(local_key.public());

info!(target: "libp2p", "Local peer id: {:?}", peer_id);
info!("Local peer id: {:?}", peer_id);

let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key)
.with_tokio()
Expand All @@ -58,7 +58,7 @@ impl Libp2pClient {
})?
.build();

info!(target: "libp2p", "Dialing relay: {:?}", relay_addr);
info!("Dialing relay: {:?}", relay_addr);
swarm.dial(relay_addr.parse::<Multiaddr>()?)?;

Ok(Self { swarm, topics: HashMap::new() })
Expand Down
25 changes: 13 additions & 12 deletions crates/torii/libp2p/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ use std::io;
use std::net::Ipv4Addr;
use std::time::Duration;

use futures::future::{select, Either};
use futures::StreamExt;
use libp2p::core::multiaddr::Protocol;
use libp2p::core::Multiaddr;
use libp2p::gossipsub::{self, IdentTopic};
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
use libp2p::{
core::muxing::StreamMuxerBox, identify, identity, noise, ping, relay,
tcp, yamux, StreamProtocol, Swarm, Transport,
core::muxing::StreamMuxerBox, identify, identity, noise, ping, relay, tcp, yamux,
Swarm, Transport,
};
use libp2p_webrtc as webrtc;
use libp2p_webrtc::tokio::Certificate;
use rand::thread_rng;
use tracing::info;

Expand Down Expand Up @@ -53,7 +51,7 @@ impl Libp2pRelay {
webrtc::tokio::Certificate::generate(&mut thread_rng())?,
)
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn))))
}).unwrap()
}).expect("Failed to create WebRTC transport")
.with_behaviour(|key| {
let message_id_fn = |message: &gossipsub::Message| {
let mut s = DefaultHasher::new();
Expand Down Expand Up @@ -85,18 +83,18 @@ impl Libp2pRelay {

// TCP
let listen_addr_tcp = Multiaddr::from(Ipv4Addr::UNSPECIFIED).with(Protocol::Tcp(port));
swarm.listen_on(listen_addr_tcp)?;
swarm.listen_on(listen_addr_tcp.clone())?;

// UDP QUIC
let listen_addr_quic =
Multiaddr::from(Ipv4Addr::UNSPECIFIED).with(Protocol::Udp(port)).with(Protocol::QuicV1);
swarm.listen_on(listen_addr_quic)?;
swarm.listen_on(listen_addr_quic.clone())?;

// WebRTC
let listen_addr_webrtc = Multiaddr::from(Ipv4Addr::UNSPECIFIED)
.with(Protocol::Udp(port_webrtc))
.with(Protocol::WebRTCDirect);
swarm.listen_on(listen_addr_webrtc)?;
swarm.listen_on(listen_addr_webrtc.clone())?;

// Clients will send their messages to the "message" topic
// with a room name as the message data.
Expand All @@ -121,7 +119,7 @@ impl Libp2pRelay {
let message: ClientMessage = serde_json::from_slice(&message.data)
.expect("Failed to deserialize message");

info!(target: "libp2p", "Received message {:?} from peer {:?} with topic {:?} and data {:?}", message_id, peer_id, message.topic, message.data);
info!("Received message {:?} from peer {:?} with topic {:?} and data {:?}", message_id, peer_id, message.topic, message.data);

// forward message to room
let server_message =
Expand All @@ -141,17 +139,20 @@ impl Libp2pRelay {
info: identify::Info { observed_addr, .. },
peer_id,
}) => {
info!(target: "libp2p", "Received identify event from peer {:?} with observed address {:?}", peer_id, observed_addr);
info!(
"Received identify event from peer {:?} with observed address {:?}",
peer_id, observed_addr
);
self.swarm.add_external_address(observed_addr.clone());
}
ServerEvent::Ping(ping::Event { peer, result, .. }) => {
info!(target: "libp2p", "Ping success from peer {:?} with result {:?}", peer, result);
info!("Ping success from peer {:?} with result {:?}", peer, result);
}
_ => {}
}
}
SwarmEvent::NewListenAddr { address, .. } => {
info!(target: "libp2p", "Listening on {:?}", address);
info!("Listening on {:?}", address);
}
_ => {}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/torii/libp2p/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ mod test {
// This tests subscribing to a topic and receiving a message
#[tokio::test]
async fn test_client_messaging() -> Result<(), Box<dyn Error>> {
let _ = tracing_subscriber::fmt()
.with_env_filter("torii_libp2p=debug")
.try_init();
// Initialize the relay server
let mut relay_server: Libp2pRelay = Libp2pRelay::new(1010, 2020)?;

Expand Down

0 comments on commit e7536fc

Please sign in to comment.