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(torii-client): cleanup & remove rpc #3019

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Changes from all 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
52 changes: 11 additions & 41 deletions crates/torii/client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
use std::sync::Arc;

use dojo_types::WorldMetadata;
use dojo_world::contracts::WorldContractReader;
use futures::lock::Mutex;
use parking_lot::{RwLock, RwLockReadGuard};
use starknet::core::types::Felt;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::JsonRpcClient;
use tokio::sync::RwLock as AsyncRwLock;
use tokio::sync::RwLock;
use torii_grpc::client::{
EntityUpdateStreaming, EventUpdateStreaming, IndexerUpdateStreaming, TokenBalanceStreaming,
TokenUpdateStreaming,
Expand All @@ -25,50 +21,24 @@
use torii_relay::client::EventLoop;
use torii_relay::types::Message;

use crate::client::error::{Error, ParseError};
use crate::client::error::Error;

// TODO: remove reliance on RPC
#[allow(unused)]
#[derive(Debug)]
pub struct Client {
/// Metadata of the World that the client is connected to.
metadata: Arc<RwLock<WorldMetadata>>,
/// The grpc client.
inner: AsyncRwLock<torii_grpc::client::WorldClient>,
inner: RwLock<torii_grpc::client::WorldClient>,
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ohayo sensei! Ensure references to the removed world_reader field are cleaned up.
Excellent approach to storing WorldClient in a RwLock. However, referencing self.world_reader.address around line 216 looks like a leftover. This will cause a compile error now that world_reader is gone.

As a follow-up fix outside the changed lines, consider updating the code to reference the new client’s address field (assuming one exists). For instance:

- subscribe_indexer(contract_address.unwrap_or(self.world_reader.address))
+ subscribe_indexer(contract_address.unwrap_or(grpc_client.world_address))

Committable suggestion skipped: line range outside the PR's diff.

/// Relay client.
relay_client: torii_relay::client::RelayClient,
/// The subscription client handle.
/// World contract reader.
world_reader: WorldContractReader<JsonRpcClient<HttpTransport>>,
}

impl Client {
/// Returns a initialized [Client].
pub async fn new(
torii_url: String,
rpc_url: String,
relay_url: String,
world: Felt,
) -> Result<Self, Error> {
let mut grpc_client = torii_grpc::client::WorldClient::new(torii_url, world).await?;

pub async fn new(torii_url: String, relay_url: String, world: Felt) -> Result<Self, Error> {
let grpc_client = torii_grpc::client::WorldClient::new(torii_url, world).await?;

Check warning on line 38 in crates/torii/client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/client/src/client/mod.rs#L37-L38

Added lines #L37 - L38 were not covered by tests
let relay_client = torii_relay::client::RelayClient::new(relay_url)?;

let metadata = grpc_client.metadata().await?;

let shared_metadata: Arc<_> = RwLock::new(metadata).into();

// initialize the entities to be synced with the latest values
let rpc_url = url::Url::parse(&rpc_url).map_err(ParseError::Url)?;
let provider = JsonRpcClient::new(HttpTransport::new(rpc_url));
let world_reader = WorldContractReader::new(world, provider);

Ok(Self {
world_reader,
metadata: shared_metadata,
inner: AsyncRwLock::new(grpc_client),
relay_client,
})
Ok(Self { inner: RwLock::new(grpc_client), relay_client })

Check warning on line 41 in crates/torii/client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/client/src/client/mod.rs#L41

Added line #L41 was not covered by tests
}

/// Starts the relay client event loop.
Expand All @@ -89,8 +59,10 @@
}

/// Returns a read lock on the World metadata that the client is connected to.
pub fn metadata(&self) -> RwLockReadGuard<'_, WorldMetadata> {
self.metadata.read()
pub async fn metadata(&self) -> Result<WorldMetadata, Error> {
let mut grpc_client = self.inner.write().await;
let metadata = grpc_client.metadata().await?;
Ok(metadata)

Check warning on line 65 in crates/torii/client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/client/src/client/mod.rs#L62-L65

Added lines #L62 - L65 were not covered by tests
}

/// Retrieves controllers matching contract addresses.
Expand Down Expand Up @@ -233,9 +205,7 @@
contract_address: Option<Felt>,
) -> Result<IndexerUpdateStreaming, Error> {
let mut grpc_client = self.inner.write().await;
let stream = grpc_client
.subscribe_indexer(contract_address.unwrap_or(self.world_reader.address))
.await?;
let stream = grpc_client.subscribe_indexer(contract_address.unwrap_or_default()).await?;

Check warning on line 208 in crates/torii/client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/client/src/client/mod.rs#L208

Added line #L208 was not covered by tests
Ok(stream)
}

Expand Down
Loading