Skip to content

Commit

Permalink
feat: update torii server
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Aug 28, 2024
1 parent d633026 commit 779aba4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 46 deletions.
38 changes: 9 additions & 29 deletions crates/torii/libp2p/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use std::{fs, io};

use chrono::Utc;
use dojo_types::schema::Ty;
use dojo_world::contracts::naming::compute_selector_from_names;
use dojo_world::contracts::naming::compute_selector_from_tag;
use futures::StreamExt;
use indexmap::IndexMap;
use libp2p::core::multiaddr::Protocol;
use libp2p::core::muxing::StreamMuxerBox;
use libp2p::core::upgrade::Version;
Expand All @@ -37,7 +36,7 @@ use crate::errors::Error;
mod events;

use crate::server::events::ServerEvent;
use crate::typed_data::{parse_value_to_ty, PrimitiveType};
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 @@ -211,7 +210,7 @@ impl<P: Provider + Sync> Relay<P> {
}
};

let ty = match validate_message(&self.db, &data.message.message).await {
let ty = match validate_message(&self.db, &data.message).await {
Ok(parsed_message) => parsed_message,
Err(e) => {
info!(
Expand Down Expand Up @@ -464,37 +463,18 @@ fn ty_keys(ty: &Ty) -> Result<Vec<Felt>, Error> {

// Validates the message model
// and returns the identity and signature
async fn validate_message(
db: &Sql,
message: &IndexMap<String, PrimitiveType>,
) -> Result<Ty, Error> {
let (selector, model) = if let Some(model_name) = message.get("model") {
if let PrimitiveType::String(model_name) = model_name {
let (namespace, name) = model_name.split_once('-').ok_or_else(|| {
Error::InvalidMessageError(
"Model name is not in the format namespace-model".to_string(),
)
})?;

(compute_selector_from_names(namespace, name), model_name)
} else {
return Err(Error::InvalidMessageError("Model name is not a string".to_string()));
}
} else {
return Err(Error::InvalidMessageError("Model name is missing".to_string()));
};
async fn validate_message(db: &Sql, message: &TypedData) -> Result<Ty, Error> {
let selector = compute_selector_from_tag(&message.primary_type);

let mut ty = db
.model(selector)
.await
.map_err(|e| Error::InvalidMessageError(format!("Model {} not found: {}", model, e)))?
.map_err(|e| {
Error::InvalidMessageError(format!("Model {} not found: {}", message.primary_type, e))
})?
.schema;

if let Some(object) = message.get(model) {
parse_value_to_ty(object, &mut ty)?;
} else {
return Err(Error::InvalidMessageError("Model is missing".to_string()));
};
parse_value_to_ty(&PrimitiveType::Object(message.message.clone()), &mut ty)?;

Ok(ty)
}
Expand Down
19 changes: 2 additions & 17 deletions crates/torii/libp2p/src/typed_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ pub fn encode_type(name: &str, types: &IndexMap<String, Vec<Field>>) -> Result<S
&simple_field.r#type[1..simple_field.r#type.len() - 1]
.split(',')
.map(|t| {
if !t.is_empty() {
format!("\"{}\"", t)
} else {
t.to_string()
}
if !t.is_empty() { format!("\"{}\"", t) } else { t.to_string() }
})
.collect::<Vec<String>>()
.join(",");
Expand Down Expand Up @@ -870,26 +866,15 @@ impl TypedData {
);

let mut values = IndexMap::new();
let mut model_values = IndexMap::new();

let mut fields = Vec::new();
for member in model.children.iter() {
let field = map_ty_type(&mut types, &member.name, member.ty.clone());
fields.push(field);

model_values.insert(member.name.clone(), map_ty_to_primitive(&member.ty)?);
values.insert(member.name.clone(), map_ty_to_primitive(&member.ty)?);
}

values.insert("model".to_string(), PrimitiveType::String(model.name.clone()));
values.insert(model.name.clone(), PrimitiveType::Object(model_values));

types.insert(
"model".to_string(),
vec![Field::SimpleType(SimpleField {
name: "model".to_string(),
r#type: "shortstring".to_string(),
})],
);
types.insert(model.name.clone(), fields);

Ok(Self::new(types, model.name.as_str(), domain, values))
Expand Down

0 comments on commit 779aba4

Please sign in to comment.