From 2fb740e7295f7fdee5229c53b5959d1bf37eda29 Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Wed, 1 Nov 2023 13:55:43 -0400 Subject: [PATCH] Use sql for model metadata in set record (#1132) --- .../core/src/processors/store_set_record.rs | 29 +++++++++++-------- crates/torii/core/src/sql.rs | 6 ++++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/crates/torii/core/src/processors/store_set_record.rs b/crates/torii/core/src/processors/store_set_record.rs index 6c655eb37a..f629872608 100644 --- a/crates/torii/core/src/processors/store_set_record.rs +++ b/crates/torii/core/src/processors/store_set_record.rs @@ -1,10 +1,10 @@ use anyhow::{Error, Ok, Result}; use async_trait::async_trait; +use dojo_world::contracts::model::ModelReader; use dojo_world::contracts::world::WorldContractReader; use starknet::core::types::{BlockWithTxs, Event, InvokeTransactionReceipt}; use starknet::core::utils::parse_cairo_short_string; use starknet::providers::Provider; -use starknet_crypto::FieldElement; use tracing::info; use super::EventProcessor; @@ -40,7 +40,7 @@ where async fn process( &self, - world: &WorldContractReader

, + _world: &WorldContractReader

, db: &mut Sql, _block: &BlockWithTxs, _transaction_receipt: &InvokeTransactionReceipt, @@ -50,17 +50,22 @@ where let name = parse_cairo_short_string(&event.data[MODEL_INDEX])?; info!("store set record: {}", name); - let model = world.model(&name).await?; - let keys = values_at(&event.data, NUM_KEYS_INDEX)?; - let entity = model.entity(&keys).await?; + let model = db.model(&name).await?; + + let keys_start = NUM_KEYS_INDEX + 1; + let keys_end: usize = keys_start + usize::from(u8::try_from(event.data[NUM_KEYS_INDEX])?); + let keys = event.data[keys_start..keys_end].to_vec(); + + let values_start = keys_end + 2; + let values_end: usize = values_start + usize::from(u8::try_from(event.data[keys_end + 1])?); + + let values = event.data[values_start..values_end].to_vec(); + let mut keys_and_unpacked = [keys, values].concat(); + + let mut entity = model.schema().await?; + entity.deserialize(&mut keys_and_unpacked)?; + db.set_entity(entity, event_id).await?; Ok(()) } } - -fn values_at(data: &[FieldElement], len_index: usize) -> Result, Error> { - let len: usize = u8::try_from(data[len_index])?.into(); - let start = len_index + 1_usize; - let end = start + len; - Ok(data[start..end].to_vec()) -} diff --git a/crates/torii/core/src/sql.rs b/crates/torii/core/src/sql.rs index 02d8028b39..7ce346c4af 100644 --- a/crates/torii/core/src/sql.rs +++ b/crates/torii/core/src/sql.rs @@ -12,6 +12,7 @@ use starknet::core::types::{Event, FieldElement, InvokeTransactionV1}; use starknet_crypto::poseidon_hash_many; use super::World; +use crate::model::ModelSQLReader; use crate::simple_broker::SimpleBroker; use crate::types::{Entity, Model as ModelType}; @@ -222,6 +223,11 @@ impl Sql { Ok(()) } + pub async fn model(&self, model: &str) -> Result { + let reader = ModelSQLReader::new(model, self.pool.clone()).await?; + Ok(reader) + } + pub async fn entity(&self, model: String, key: FieldElement) -> Result> { let query = format!("SELECT * FROM {model} WHERE id = {key}"); let mut conn: PoolConnection = self.pool.acquire().await?;