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

refactor(libp2p): typed data crate + client server feature set #2877

Merged
merged 14 commits into from
Jan 9, 2025
28 changes: 21 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ torii-graphql = { path = "crates/torii/graphql" }
torii-grpc = { path = "crates/torii/grpc" }
torii-relay = { path = "crates/torii/libp2p" }
torii-server = { path = "crates/torii/server" }
torii-typed-data = { path = "crates/torii/typed-data" }

# saya
saya-core = { path = "crates/saya/core" }
Expand Down
2 changes: 1 addition & 1 deletion bin/torii/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ torii-cli.workspace = true
torii-core.workspace = true
torii-graphql.workspace = true
torii-grpc = { workspace = true, features = [ "server" ] }
torii-relay.workspace = true
torii-relay = { workspace = true, features = [ "client", "server" ] }
torii-server.workspace = true
tower.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion crates/torii/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ starknet-crypto.workspace = true
thiserror.workspace = true
tokio = { version = "1.32.0", features = [ "sync" ], default-features = false }
torii-grpc = { workspace = true, features = [ "client" ] }
torii-relay = { workspace = true }
torii-relay = { workspace = true, features = [ "client" ] }
url.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/client/src/client/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Error {
#[error(transparent)]
GrpcClient(#[from] torii_grpc::client::Error),
#[error(transparent)]
RelayClient(#[from] torii_relay::errors::Error),
RelayClient(#[from] torii_relay::error::Error),
#[error(transparent)]
Model(#[from] ModelError),
#[error("Unsupported query")]
Expand Down
38 changes: 22 additions & 16 deletions crates/torii/libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,49 @@ name = "torii-relay"
repository.workspace = true
version.workspace = true

[features]
client = [ ]
server = [ "dep:sqlx", "dep:torii-core", "dep:dojo-types", "dep:dojo-world", "dep:starknet-crypto", "dep:chrono", "dep:libp2p-webrtc", "dep:rand" ]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Ohayo! Your client feature needs its dependencies, sensei!

The client module uses several libp2p-related dependencies that should be gated behind the client feature:

  • Add "dep:libp2p", "dep:futures" to the client feature
  • For WASM support: "dep:libp2p-webrtc-websys", "dep:libp2p-websocket-websys"
  • Other essential deps: "dep:tracing", "dep:serde_json"
🔗 Analysis chain

Ohayo! The empty client feature needs attention, sensei!

The client feature is currently empty. If there are client-specific dependencies or functionality, they should be included in this feature flag.

Let's check if there are any client-specific dependencies that should be gated behind this feature:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for #[cfg(feature = "client")] usage
rg --type rust 'cfg\(feature = "client"\)' crates/torii/libp2p/src/

Length of output: 127


Script:

#!/bin/bash
# Check the context around client feature flag
rg --type rust -B 3 -A 10 'cfg\(feature = "client"\)' crates/torii/libp2p/src/lib.rs

# List all dependencies in use
ast-grep --pattern 'use $_::$$$'

Length of output: 66114


Script:

#!/bin/bash
# Check the contents of the client module
cat crates/torii/libp2p/src/client/mod.rs

# Look for any use statements in client-related files
find crates/torii/libp2p/src/client -type f -name "*.rs" -exec rg '^use' {} \;

Length of output: 10184


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
futures.workspace = true
rand.workspace = true
serde.workspace = true
# preserve order
anyhow.workspace = true
cainome.workspace = true
chrono.workspace = true
crypto-bigint.workspace = true
dojo-types.workspace = true
dojo-world.workspace = true
indexmap.workspace = true
serde_json.workspace = true
starknet-crypto.workspace = true
starknet.workspace = true
thiserror.workspace = true
torii-typed-data.workspace = true
tracing.workspace = true
sqlx = { workspace = true, optional = true }
torii-core = { workspace = true, optional = true }
dojo-types = { workspace = true, optional = true }
dojo-world = { workspace = true, optional = true }
rand = { workspace = true, optional = true }
starknet-crypto = { workspace = true, optional = true }
chrono = { workspace = true, optional = true }
libp2p-webrtc = { git = "https://github.com/libp2p/rust-libp2p", features = [ "pem", "tokio" ], rev = "cdc9638", optional = true }

[dev-dependencies]
indexmap.workspace = true
katana-runner.workspace = true
tempfile.workspace = true
tokio.workspace = true
tracing-subscriber.workspace = true

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
tracing-wasm = "0.2.1"
wasm-bindgen-futures = "0.4.40"
wasm-bindgen-test = "0.3.40"
wasm-timer = "0.2.5"


[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
libp2p = { git = "https://github.com/libp2p/rust-libp2p", features = [ "dns", "ed25519", "gossipsub", "identify", "macros", "noise", "ping", "quic", "relay", "tcp", "tokio", "websocket", "yamux" ], rev = "cdc9638" }
libp2p-webrtc = { git = "https://github.com/libp2p/rust-libp2p", features = [ "pem", "tokio" ], rev = "cdc9638" }
sqlx.workspace = true
torii-core.workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
libp2p = { git = "https://github.com/libp2p/rust-libp2p", features = [ "ed25519", "gossipsub", "identify", "macros", "noise", "ping", "tcp", "wasm-bindgen", "yamux" ], rev = "cdc9638" }
libp2p-webrtc-websys = { git = "https://github.com/libp2p/rust-libp2p", rev = "cdc9638" }
libp2p-websocket-websys = { git = "https://github.com/libp2p/rust-libp2p", rev = "cdc9638" }
tracing-wasm = "0.2.1"
wasm-bindgen-futures = "0.4.40"
wasm-bindgen-test = "0.3.40"
wasm-timer = "0.2.5"
libp2p-websocket-websys = { git = "https://github.com/libp2p/rust-libp2p", rev = "cdc9638" }
2 changes: 1 addition & 1 deletion crates/torii/libp2p/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tracing::info;
pub mod events;
use crate::client::events::ClientEvent;
use crate::constants;
use crate::errors::Error;
use crate::error::Error;
use crate::types::Message;

pub(crate) const LOG_TARGET: &str = "torii::relay::client";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use libp2p::gossipsub::{PublishError, SubscriptionError};
use libp2p::noise;
use starknet::providers::ProviderError;
use thiserror::Error;
use torii_typed_data::error::Error as TypedDataError;

#[derive(Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -41,12 +42,15 @@ pub enum Error {
#[error("Failed to read certificate: {0}")]
ReadCertificateError(anyhow::Error),

#[error("Invalid message provided: {0}")]
InvalidMessageError(String),

#[error("Invalid type provided: {0}")]
InvalidTypeError(String),

#[error(transparent)]
ProviderError(#[from] ProviderError),

#[error(transparent)]
TypedDataError(#[from] TypedDataError),

#[error("Invalid message provided: {0}")]
InvalidMessageError(String),
}
14 changes: 9 additions & 5 deletions crates/torii/libp2p/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#![warn(unused_crate_dependencies)]

pub mod client;
mod constants;
pub mod errors;
#[cfg(not(target_arch = "wasm32"))]
pub mod error;

#[cfg(feature = "client")]
pub mod client;
#[cfg(feature = "server")]
pub mod server;
mod tests;
pub mod typed_data;

#[cfg(test)]
mod test;

pub mod types;
7 changes: 4 additions & 3 deletions crates/torii/libp2p/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ use tracing::{info, warn};
use webrtc::tokio::Certificate;

use crate::constants;
use crate::errors::Error;
use crate::error::Error;

mod events;

use torii_typed_data::typed_data::{parse_value_to_ty, PrimitiveType, TypedData};

use crate::server::events::ServerEvent;
use crate::typed_data::{parse_value_to_ty, PrimitiveType, TypedData};
use crate::types::Message;

pub(crate) const LOG_TARGET: &str = "torii::relay::server";
Expand Down Expand Up @@ -128,7 +129,7 @@ impl<P: Provider + Sync> Relay<P> {
relay: relay::Behaviour::new(key.public().to_peer_id(), Default::default()),
ping: ping::Behaviour::new(ping::Config::new()),
identify: identify::Behaviour::new(identify::Config::new(
format!("/torii-relay/{}", env!("CARGO_PKG_VERSION")),
format!("/{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
key.public(),
)),
gossipsub: gossipsub::Behaviour::new(
Expand Down
Loading
Loading