Skip to content

Commit

Permalink
refactor(torii): model reader without call to world (#3052)
Browse files Browse the repository at this point in the history
* refactor(torii): model reader without call to world

* refactor: model ocntract reader from event address

* fmt

* remove result
  • Loading branch information
Larkooo authored Feb 21, 2025
1 parent f244197 commit 7facdb7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 53 deletions.
39 changes: 22 additions & 17 deletions crates/dojo/world/src/contracts/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,35 @@ where
pub async fn new(
namespace: &str,
name: &str,
address: Felt,
class_hash: Felt,
world: &'a WorldContractReader<P>,
) -> Result<ModelRPCReader<'a, P>, ModelError> {
Self::new_with_block(namespace, name, world, world.block_id).await
) -> ModelRPCReader<'a, P> {
let selector = naming::compute_selector_from_names(namespace, name);
let contract_reader = ModelContractReader::new(address, world.provider());

Self {
namespace: namespace.into(),
name: name.into(),
selector,
class_hash,
contract_address: address,
world_reader: world,
model_reader: contract_reader,
}
}

pub async fn new_with_block(
pub async fn new_from_world(
namespace: &str,
name: &str,
world: &'a WorldContractReader<P>,
block_id: BlockId,
) -> Result<ModelRPCReader<'a, P>, ModelError> {
let model_selector = naming::compute_selector_from_names(namespace, name);

// Events are also considered like models from a off-chain perspective. They both have
// introspection and convey type information.
let (contract_address, class_hash) =
match world.resource(&model_selector).block_id(block_id).call().await? {
match world.resource(&model_selector).block_id(world.block_id).call().await? {
abigen::world::Resource::Model((address, hash)) => (address, hash),
abigen::world::Resource::Event((address, hash)) => (address, hash),
_ => return Err(ModelError::ModelNotFound),
Expand All @@ -113,18 +125,7 @@ where
return Err(ModelError::ModelNotFound);
}

let mut model_reader = ModelContractReader::new(contract_address.into(), world.provider());
model_reader.set_block(block_id);

Ok(Self {
namespace: namespace.into(),
name: name.into(),
world_reader: world,
class_hash,
contract_address: contract_address.into(),
selector: model_selector,
model_reader,
})
Ok(Self::new(namespace, name, contract_address.0, class_hash, world).await)
}

pub async fn entity_storage(&self, keys: &[Felt]) -> Result<Vec<Felt>, ModelError> {
Expand Down Expand Up @@ -153,6 +154,10 @@ where

Ok(schema)
}

pub async fn set_block(&mut self, block_id: BlockId) {
self.model_reader.set_block(block_id);
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
Expand Down
14 changes: 2 additions & 12 deletions crates/dojo/world/src/contracts/world.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::result::Result;

use starknet::core::types::BlockId;
use starknet::providers::Provider;

pub use super::abigen::world::{
Expand All @@ -24,23 +23,14 @@ where
) -> Result<ModelRPCReader<'_, P>, ModelError> {
let (namespace, name) =
naming::split_tag(tag).map_err(|e| ModelError::TagError(e.to_string()))?;
ModelRPCReader::new(&namespace, &name, self).await
ModelRPCReader::new_from_world(&namespace, &name, self).await
}

pub async fn model_reader(
&self,
namespace: &str,
name: &str,
) -> Result<ModelRPCReader<'_, P>, ModelError> {
ModelRPCReader::new(namespace, name, self).await
}

pub async fn model_reader_with_block(
&self,
namespace: &str,
name: &str,
block_id: BlockId,
) -> Result<ModelRPCReader<'_, P>, ModelError> {
ModelRPCReader::new_with_block(namespace, name, self, block_id).await
ModelRPCReader::new_from_world(namespace, name, self).await
}
}
13 changes: 7 additions & 6 deletions crates/torii/indexer/src/processors/register_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::{DefaultHasher, Hash, Hasher};
use anyhow::{Error, Ok, Result};
use async_trait::async_trait;
use dojo_world::contracts::abigen::world::Event as WorldEvent;
use dojo_world::contracts::model::ModelReader;
use dojo_world::contracts::model::{ModelRPCReader, ModelReader};
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::{BlockId, Event};
use starknet::providers::Provider;
Expand Down Expand Up @@ -79,11 +79,12 @@ where

// Called model here by language, but it's an event. Torii rework will make clear
// distinction.
let model = if config.strict_model_reader {
world.model_reader_with_block(&namespace, &name, BlockId::Number(block_number)).await?
} else {
world.model_reader(&namespace, &name).await?
};
let mut model =
ModelRPCReader::new(&namespace, &name, event.address.0, event.class_hash.0, world)
.await;
if config.strict_model_reader {
model.set_block(BlockId::Number(block_number)).await;
}
let schema = model.schema().await?;
let layout = model.layout().await?;

Expand Down
13 changes: 7 additions & 6 deletions crates/torii/indexer/src/processors/register_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::{DefaultHasher, Hash, Hasher};
use anyhow::{Error, Ok, Result};
use async_trait::async_trait;
use dojo_world::contracts::abigen::world::Event as WorldEvent;
use dojo_world::contracts::model::ModelReader;
use dojo_world::contracts::model::{ModelRPCReader, ModelReader};
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::{BlockId, Event};
use starknet::providers::Provider;
Expand Down Expand Up @@ -77,11 +77,12 @@ where
return Ok(());
}

let model = if config.strict_model_reader {
world.model_reader_with_block(&namespace, &name, BlockId::Number(block_number)).await?
} else {
world.model_reader(&namespace, &name).await?
};
let mut model =
ModelRPCReader::new(&namespace, &name, event.address.0, event.class_hash.0, world)
.await;
if config.strict_model_reader {
model.set_block(BlockId::Number(block_number)).await;
}
let schema = model.schema().await?;
let layout = model.layout().await?;

Expand Down
13 changes: 7 additions & 6 deletions crates/torii/indexer/src/processors/upgrade_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::{DefaultHasher, Hash, Hasher};
use anyhow::{Error, Result};
use async_trait::async_trait;
use dojo_world::contracts::abigen::world::Event as WorldEvent;
use dojo_world::contracts::model::ModelReader;
use dojo_world::contracts::model::{ModelRPCReader, ModelReader};
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::{BlockId, Event};
use starknet::providers::Provider;
Expand Down Expand Up @@ -88,11 +88,12 @@ where
let namespace = model.namespace;
let prev_schema = model.schema;

let model = if config.strict_model_reader {
world.model_reader_with_block(&namespace, &name, BlockId::Number(block_number)).await?
} else {
world.model_reader(&namespace, &name).await?
};
let mut model =
ModelRPCReader::new(&namespace, &name, event.address.0, event.class_hash.0, world)
.await;
if config.strict_model_reader {
model.set_block(BlockId::Number(block_number)).await;
}
let new_schema = model.schema().await?;
let schema_diff = prev_schema.diff(&new_schema);
// No changes to the schema. This can happen if torii is re-run with a fresh database.
Expand Down
13 changes: 7 additions & 6 deletions crates/torii/indexer/src/processors/upgrade_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::{DefaultHasher, Hash, Hasher};
use anyhow::{Error, Result};
use async_trait::async_trait;
use dojo_world::contracts::abigen::world::Event as WorldEvent;
use dojo_world::contracts::model::ModelReader;
use dojo_world::contracts::model::{ModelRPCReader, ModelReader};
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::{BlockId, Event};
use starknet::providers::Provider;
Expand Down Expand Up @@ -86,11 +86,12 @@ where
let namespace = model.namespace;
let prev_schema = model.schema;

let model = if config.strict_model_reader {
world.model_reader_with_block(&namespace, &name, BlockId::Number(block_number)).await?
} else {
world.model_reader(&namespace, &name).await?
};
let mut model =
ModelRPCReader::new(&namespace, &name, event.address.0, event.class_hash.0, world)
.await;
if config.strict_model_reader {
model.set_block(BlockId::Number(block_number)).await;
}
let new_schema = model.schema().await?;
let schema_diff = prev_schema.diff(&new_schema);
// No changes to the schema. This can happen if torii is re-run with a fresh database.
Expand Down

0 comments on commit 7facdb7

Please sign in to comment.