From 4eff5735fb4dc40c9effc7c3354b13eadb7bcd78 Mon Sep 17 00:00:00 2001 From: notV4l Date: Mon, 10 Jun 2024 14:34:27 +0200 Subject: [PATCH 1/4] trick: to unlock toriiClient with safari/firefox --- crates/torii/grpc/src/client.rs | 26 ++++++++++++++++--- .../src/server/subscriptions/model_diff.rs | 8 ++++++ crates/torii/server/src/proxy.rs | 3 +-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/crates/torii/grpc/src/client.rs b/crates/torii/grpc/src/client.rs index 4a11e04777..29c2e52b16 100644 --- a/crates/torii/grpc/src/client.rs +++ b/crates/torii/grpc/src/client.rs @@ -3,7 +3,7 @@ use std::num::ParseIntError; use futures_util::stream::MapOk; use futures_util::{Stream, StreamExt, TryStreamExt}; -use starknet::core::types::{FromStrError, StateUpdate}; +use starknet::core::types::{FromStrError, StateDiff, StateUpdate}; use starknet_crypto::FieldElement; use crate::proto::world::{ @@ -144,9 +144,11 @@ impl WorldClient { .map_err(Error::Grpc) .map(|res| res.into_inner())?; - Ok(ModelDiffsStreaming(stream.map_ok(Box::new(|res| { - let update = res.model_update.expect("qed; state update must exist"); - TryInto::::try_into(update).expect("must able to serialize") + Ok(ModelDiffsStreaming(stream.map_ok(Box::new(|res| match res.model_update { + Some(update) => { + TryInto::::try_into(update).expect("must able to serialize") + } + None => empty_state_update(), })))) } } @@ -184,3 +186,19 @@ impl Stream for EntityUpdateStreaming { self.0.poll_next_unpin(cx) } } + +fn empty_state_update() -> StateUpdate { + StateUpdate { + block_hash: FieldElement::ZERO, + new_root: FieldElement::ZERO, + old_root: FieldElement::ZERO, + state_diff: StateDiff { + declared_classes: vec![], + deployed_contracts: vec![], + deprecated_declared_classes: vec![], + nonces: vec![], + replaced_classes: vec![], + storage_diffs: vec![], + }, + } +} diff --git a/crates/torii/grpc/src/server/subscriptions/model_diff.rs b/crates/torii/grpc/src/server/subscriptions/model_diff.rs index ad257c719c..b74b5098c4 100644 --- a/crates/torii/grpc/src/server/subscriptions/model_diff.rs +++ b/crates/torii/grpc/src/server/subscriptions/model_diff.rs @@ -20,6 +20,7 @@ use tracing::{debug, error, trace}; use super::error::SubscriptionError; use crate::proto; +use crate::proto::world::SubscribeModelsResponse; use crate::types::KeysClause; pub(crate) const LOG_TARGET: &str = "torii::grpc::server::subscriptions::model_diff"; @@ -87,6 +88,13 @@ impl StateDiffManager { .await .insert(id, ModelDiffSubscriber { storage_addresses, sender }); + // unlock issue with firefox/safari + // send empty model update to unlock browsers ... + let subscribers = self.subscribers.write().await; + let _ = subscribers.get(&id).unwrap().sender.send(Ok(SubscribeModelsResponse { + model_update: None + })).await; + Ok(receiver) } diff --git a/crates/torii/server/src/proxy.rs b/crates/torii/server/src/proxy.rs index df9f4e26f5..23539b5d49 100644 --- a/crates/torii/server/src/proxy.rs +++ b/crates/torii/server/src/proxy.rs @@ -17,7 +17,7 @@ use tower::ServiceBuilder; use tower_http::cors::{AllowOrigin, CorsLayer}; use tracing::error; -const DEFAULT_ALLOW_HEADERS: [&str; 12] = [ +const DEFAULT_ALLOW_HEADERS: [&str; 11] = [ "accept", "origin", "content-type", @@ -27,7 +27,6 @@ const DEFAULT_ALLOW_HEADERS: [&str; 12] = [ "x-grpc-timeout", "x-user-agent", "connection", - "upgrade", "sec-websocket-key", "sec-websocket-version", ]; From 6722c0d19dcc0c8246e65228b534fe3652d228b4 Mon Sep 17 00:00:00 2001 From: notV4l Date: Mon, 10 Jun 2024 17:41:17 +0200 Subject: [PATCH 2/4] same trick for other subscribers --- crates/torii/grpc/src/client.rs | 6 +++--- crates/torii/grpc/src/server/subscriptions/entity.rs | 11 +++++++++++ .../grpc/src/server/subscriptions/event_message.rs | 11 +++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/crates/torii/grpc/src/client.rs b/crates/torii/grpc/src/client.rs index 29c2e52b16..7f34a60209 100644 --- a/crates/torii/grpc/src/client.rs +++ b/crates/torii/grpc/src/client.rs @@ -105,9 +105,9 @@ impl WorldClient { .map_err(Error::Grpc) .map(|res| res.into_inner())?; - Ok(EntityUpdateStreaming(stream.map_ok(Box::new(|res| { - let entity = res.entity.expect("entity must exist"); - entity.try_into().expect("must able to serialize") + Ok(EntityUpdateStreaming(stream.map_ok(Box::new(|res| match res.entity { + Some(entity) => entity.try_into().expect("must able to serialize"), + None => Entity { hashed_keys: FieldElement::ZERO, models: vec![] }, })))) } diff --git a/crates/torii/grpc/src/server/subscriptions/entity.rs b/crates/torii/grpc/src/server/subscriptions/entity.rs index 1573b5c61f..c0e453c25d 100644 --- a/crates/torii/grpc/src/server/subscriptions/entity.rs +++ b/crates/torii/grpc/src/server/subscriptions/entity.rs @@ -20,6 +20,7 @@ use torii_core::types::Entity; use tracing::{error, trace}; use crate::proto; +use crate::proto::world::SubscribeEntityResponse; pub(crate) const LOG_TARGET: &str = "torii::grpc::server::subscriptions::entity"; @@ -48,6 +49,16 @@ impl EntityManager { EntitiesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender }, ); + // unlock issue with firefox/safari + // send empty model update to unlock browsers ... + let subscribers = self.subscribers.write().await; + let _ = subscribers + .get(&id) + .unwrap() + .sender + .send(Ok(SubscribeEntityResponse { entity: None })) + .await; + Ok(receiver) } diff --git a/crates/torii/grpc/src/server/subscriptions/event_message.rs b/crates/torii/grpc/src/server/subscriptions/event_message.rs index 736f88c0f9..68c7f6ffd3 100644 --- a/crates/torii/grpc/src/server/subscriptions/event_message.rs +++ b/crates/torii/grpc/src/server/subscriptions/event_message.rs @@ -20,6 +20,7 @@ use torii_core::types::EventMessage; use tracing::{error, trace}; use crate::proto; +use crate::proto::world::SubscribeEntityResponse; pub(crate) const LOG_TARGET: &str = "torii::grpc::server::subscriptions::event_message"; pub struct EventMessagesSubscriber { @@ -47,6 +48,16 @@ impl EventMessageManager { EventMessagesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender }, ); + // unlock issue with firefox/safari + // send empty model update to unlock browsers ... + let subscribers = self.subscribers.write().await; + let _ = subscribers + .get(&id) + .unwrap() + .sender + .send(Ok(SubscribeEntityResponse { entity: None })) + .await; + Ok(receiver) } From 4b1f8c187f8912f8009f49bec4a4cedf39b171b1 Mon Sep 17 00:00:00 2001 From: Nasr Date: Thu, 13 Jun 2024 09:30:44 -0400 Subject: [PATCH 3/4] refactor: send initial stream message directly --- .../torii/grpc/src/server/subscriptions/entity.rs | 14 ++++---------- .../grpc/src/server/subscriptions/event_message.rs | 14 ++++---------- .../grpc/src/server/subscriptions/model_diff.rs | 13 +++++-------- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/crates/torii/grpc/src/server/subscriptions/entity.rs b/crates/torii/grpc/src/server/subscriptions/entity.rs index c0e453c25d..254dcbc4b2 100644 --- a/crates/torii/grpc/src/server/subscriptions/entity.rs +++ b/crates/torii/grpc/src/server/subscriptions/entity.rs @@ -46,18 +46,12 @@ impl EntityManager { self.subscribers.write().await.insert( id, - EntitiesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender }, + EntitiesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender: sender.clone() }, ); - // unlock issue with firefox/safari - // send empty model update to unlock browsers ... - let subscribers = self.subscribers.write().await; - let _ = subscribers - .get(&id) - .unwrap() - .sender - .send(Ok(SubscribeEntityResponse { entity: None })) - .await; + // NOTE: unlock issue with firefox/safari + // initially send empty stream message to call + let _ = sender.send(Ok(SubscribeEntityResponse { entity: None })).await; Ok(receiver) } diff --git a/crates/torii/grpc/src/server/subscriptions/event_message.rs b/crates/torii/grpc/src/server/subscriptions/event_message.rs index 68c7f6ffd3..744aaa8cd6 100644 --- a/crates/torii/grpc/src/server/subscriptions/event_message.rs +++ b/crates/torii/grpc/src/server/subscriptions/event_message.rs @@ -45,18 +45,12 @@ impl EventMessageManager { self.subscribers.write().await.insert( id, - EventMessagesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender }, + EventMessagesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender: sender.clone() }, ); - // unlock issue with firefox/safari - // send empty model update to unlock browsers ... - let subscribers = self.subscribers.write().await; - let _ = subscribers - .get(&id) - .unwrap() - .sender - .send(Ok(SubscribeEntityResponse { entity: None })) - .await; + // NOTE: unlock issue with firefox/safari + // initially send empty stream message to call + let _ = sender.send(Ok(SubscribeEntityResponse { entity: None })).await; Ok(receiver) } diff --git a/crates/torii/grpc/src/server/subscriptions/model_diff.rs b/crates/torii/grpc/src/server/subscriptions/model_diff.rs index b74b5098c4..0767c62be9 100644 --- a/crates/torii/grpc/src/server/subscriptions/model_diff.rs +++ b/crates/torii/grpc/src/server/subscriptions/model_diff.rs @@ -86,14 +86,11 @@ impl StateDiffManager { self.subscribers .write() .await - .insert(id, ModelDiffSubscriber { storage_addresses, sender }); - - // unlock issue with firefox/safari - // send empty model update to unlock browsers ... - let subscribers = self.subscribers.write().await; - let _ = subscribers.get(&id).unwrap().sender.send(Ok(SubscribeModelsResponse { - model_update: None - })).await; + .insert(id, ModelDiffSubscriber { storage_addresses, sender: sender.clone() }); + + // NOTE: unlock issue with firefox/safari + // initially send empty stream message to call + let _ = sender.send(Ok(SubscribeModelsResponse { model_update: None })).await; Ok(receiver) } From a5b0c31499fc4fa9e8184ed2bafc59c039385ea3 Mon Sep 17 00:00:00 2001 From: Nasr Date: Thu, 13 Jun 2024 09:43:47 -0400 Subject: [PATCH 4/4] chore: avoid sender clone --- crates/torii/grpc/src/server/subscriptions/entity.rs | 11 ++++++----- .../grpc/src/server/subscriptions/event_message.rs | 11 ++++++----- .../torii/grpc/src/server/subscriptions/model_diff.rs | 11 ++++++----- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/crates/torii/grpc/src/server/subscriptions/entity.rs b/crates/torii/grpc/src/server/subscriptions/entity.rs index 254dcbc4b2..f9d4ae0d96 100644 --- a/crates/torii/grpc/src/server/subscriptions/entity.rs +++ b/crates/torii/grpc/src/server/subscriptions/entity.rs @@ -44,15 +44,16 @@ impl EntityManager { let id = rand::thread_rng().gen::(); let (sender, receiver) = channel(1); + // NOTE: unlock issue with firefox/safari + // initially send empty stream message to return from + // initial subscribe call + let _ = sender.send(Ok(SubscribeEntityResponse { entity: None })).await; + self.subscribers.write().await.insert( id, - EntitiesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender: sender.clone() }, + EntitiesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender }, ); - // NOTE: unlock issue with firefox/safari - // initially send empty stream message to call - let _ = sender.send(Ok(SubscribeEntityResponse { entity: None })).await; - Ok(receiver) } diff --git a/crates/torii/grpc/src/server/subscriptions/event_message.rs b/crates/torii/grpc/src/server/subscriptions/event_message.rs index 744aaa8cd6..67cf1cf172 100644 --- a/crates/torii/grpc/src/server/subscriptions/event_message.rs +++ b/crates/torii/grpc/src/server/subscriptions/event_message.rs @@ -43,15 +43,16 @@ impl EventMessageManager { let id = rand::thread_rng().gen::(); let (sender, receiver) = channel(1); + // NOTE: unlock issue with firefox/safari + // initially send empty stream message to return from + // initial subscribe call + let _ = sender.send(Ok(SubscribeEntityResponse { entity: None })).await; + self.subscribers.write().await.insert( id, - EventMessagesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender: sender.clone() }, + EventMessagesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender }, ); - // NOTE: unlock issue with firefox/safari - // initially send empty stream message to call - let _ = sender.send(Ok(SubscribeEntityResponse { entity: None })).await; - Ok(receiver) } diff --git a/crates/torii/grpc/src/server/subscriptions/model_diff.rs b/crates/torii/grpc/src/server/subscriptions/model_diff.rs index 0767c62be9..8e1f4e80cf 100644 --- a/crates/torii/grpc/src/server/subscriptions/model_diff.rs +++ b/crates/torii/grpc/src/server/subscriptions/model_diff.rs @@ -83,14 +83,15 @@ impl StateDiffManager { .flatten() .collect::>(); + // NOTE: unlock issue with firefox/safari + // initially send empty stream message to return from + // initial subscribe call + let _ = sender.send(Ok(SubscribeModelsResponse { model_update: None })).await; + self.subscribers .write() .await - .insert(id, ModelDiffSubscriber { storage_addresses, sender: sender.clone() }); - - // NOTE: unlock issue with firefox/safari - // initially send empty stream message to call - let _ = sender.send(Ok(SubscribeModelsResponse { model_update: None })).await; + .insert(id, ModelDiffSubscriber { storage_addresses, sender }); Ok(receiver) }