Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(torii-libp2p): inter torii messaging for offchain messages #3082

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions crates/torii/cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ pub struct RelayOptions {
)]
#[serde(default)]
pub cert_path: Option<String>,

/// A list of other torii relays to connect to and sync with.
/// Right now, only offchain messages broadcasted by the relay will be synced.
#[arg(
long = "relay.peers",
value_delimiter = ',',
help = "A list of other torii relays to connect to and sync with."
)]
#[serde(default)]
pub peers: Vec<String>,
}

impl Default for RelayOptions {
Expand All @@ -85,6 +95,7 @@ impl Default for RelayOptions {
websocket_port: DEFAULT_RELAY_WEBSOCKET_PORT,
local_key_path: None,
cert_path: None,
peers: vec![],
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions crates/torii/libp2p/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent};
#[cfg(not(target_arch = "wasm32"))]
use libp2p::tcp;
use libp2p::{identify, identity, noise, ping, yamux, Multiaddr, PeerId};
use libp2p_webrtc as webrtc;
use tracing::info;

pub mod events;
Expand Down Expand Up @@ -50,15 +51,40 @@ enum Command {
impl RelayClient {
#[cfg(not(target_arch = "wasm32"))]
pub fn new(relay_addr: String) -> Result<Self, Error> {
use libp2p::{core::{muxing::StreamMuxerBox, upgrade::Version}, dns, websocket};
use libp2p_webrtc::tokio::Certificate;
use rand::thread_rng;
use libp2p::Transport;

let local_key = identity::Keypair::generate_ed25519();
let peer_id = PeerId::from(local_key.public());

info!(target: LOG_TARGET, peer_id = %peer_id, "Local peer id.");

let cert = Certificate::generate(&mut thread_rng()).unwrap();
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key)
.with_tokio()
.with_tcp(tcp::Config::default(), noise::Config::new, yamux::Config::default)?
.with_quic()
.with_other_transport(|key| {
webrtc::tokio::Transport::new(key.clone(), cert)
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn)))
})
.expect("Failed to create WebRTC transport")
.with_other_transport(|key| {
let transport = websocket::WsConfig::new(
dns::tokio::Transport::system(tcp::tokio::Transport::new(
tcp::Config::default(),
))
.unwrap(),
);

transport
.upgrade(Version::V1)
.authenticate(noise::Config::new(key).unwrap())
.multiplex(yamux::Config::default())
})
.expect("Failed to create WebSocket transport")
.with_behaviour(|key| {
let gossipsub_config: gossipsub::Config = gossipsub::ConfigBuilder::default()
.heartbeat_interval(Duration::from_secs(
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/libp2p/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl<P: Provider + Sync> Relay<P> {
continue;
}
},
None => match get_identity_from_ty(&ty) {
_ => match get_identity_from_ty(&ty) {
Ok(identity) => identity,
Err(e) => {
warn!(
Expand Down
Loading