Skip to content

Commit

Permalink
refactor(libp2p): typed data crate + client server feature set (#2877)
Browse files Browse the repository at this point in the history
* refactor(typed-data): parse number string macro

* fmt

* refactor(libp2p): modularize & client server features

* fmt

* lint

* fmt

* fix tests

* fmt

* fix clippy

* move mocks to td

* fix deps

* log targets

* fmt
  • Loading branch information
Larkooo authored Jan 9, 2025
1 parent 5d1d308 commit 7036164
Show file tree
Hide file tree
Showing 38 changed files with 943 additions and 894 deletions.
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 @@ -125,6 +125,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 @@ -40,7 +40,7 @@ torii-indexer.workspace = true
torii-sqlite.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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing::debug;

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc20_legacy_transfer";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::erc20_legacy_transfer";

#[derive(Default, Debug)]
pub struct Erc20LegacyTransferProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/erc20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing::debug;

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc20_transfer";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::erc20_transfer";

#[derive(Default, Debug)]
pub struct Erc20TransferProcessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing::debug;

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc721_legacy_transfer";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::erc721_legacy_transfer";

#[derive(Default, Debug)]
pub struct Erc721LegacyTransferProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/erc721_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing::debug;

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc721_transfer";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::erc721_transfer";

#[derive(Default, Debug)]
pub struct Erc721TransferProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/event_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::info;

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::event_message";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::event_message";

#[derive(Default, Debug)]
pub struct EventMessageProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/metadata_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tracing::{error, info};

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::metadata_update";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::metadata_update";

#[derive(Default, Debug)]
pub struct MetadataUpdateProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/register_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::{debug, info};

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::register_event";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::register_event";

#[derive(Default, Debug)]
pub struct RegisterEventProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/register_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::{debug, info};

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::register_model";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::register_model";

#[derive(Default, Debug)]
pub struct RegisterModelProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/store_del_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing::{debug, info};

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::store_del_record";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::store_del_record";

#[derive(Default, Debug)]
pub struct StoreDelRecordProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/store_set_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::{debug, info};

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::store_set_record";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::store_set_record";

#[derive(Default, Debug)]
pub struct StoreSetRecordProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/store_update_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tracing::info;

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::store_update_member";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::store_update_member";

#[derive(Default, Debug)]
pub struct StoreUpdateMemberProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/store_update_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::{debug, info};

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::store_update_record";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::store_update_record";

#[derive(Default, Debug)]
pub struct StoreUpdateRecordProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/upgrade_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::{debug, info};

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::upgrade_event";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::upgrade_event";

#[derive(Default, Debug)]
pub struct UpgradeEventProcessor;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/upgrade_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::{debug, info};

use super::{EventProcessor, EventProcessorConfig};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::upgrade_model";
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::upgrade_model";

#[derive(Default, Debug)]
pub struct UpgradeModelProcessor;
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-sqlite", "dep:dojo-types", "dep:dojo-world", "dep:starknet-crypto", "dep:chrono", "dep:libp2p-webrtc", "dep:rand" ]

# 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-sqlite = { 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-sqlite.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

0 comments on commit 7036164

Please sign in to comment.