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

opt(torii): avoid calculating poseidon hash where possible #2394

Merged
merged 4 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions bin/sozo/tests/test_data/policies.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"target": "0x2d24481107b55ecd73c4d1b62f6bfe8c42a224447b71db7dcec2eab484d53cd",
"method": "set_player_server_profile"
},
{
"target": "0x2d24481107b55ecd73c4d1b62f6bfe8c42a224447b71db7dcec2eab484d53cd",
"method": "set_models"
},
{
"target": "0x2d24481107b55ecd73c4d1b62f6bfe8c42a224447b71db7dcec2eab484d53cd",
"method": "enter_dungeon"
Expand Down
3 changes: 2 additions & 1 deletion crates/dojo-core/src/world/world_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ pub mod world {
#[derive(Drop, starknet::Event)]
pub struct StoreSetRecord {
pub table: felt252,
pub entity_id: felt252,
pub keys: Span<felt252>,
pub values: Span<felt252>,
}
Expand Down Expand Up @@ -1091,7 +1092,7 @@ pub mod world {
let entity_id = entity_id_from_keys(keys);
self.write_model_entity(model_selector, entity_id, values, layout);
EventEmitter::emit(
ref self, StoreSetRecord { table: model_selector, keys, values }
ref self, StoreSetRecord { table: model_selector, keys, values, entity_id }
);
},
ModelIndex::Id(entity_id) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,11 @@
"type": "core::felt252",
"kind": "data"
},
{
"name": "entity_id",
"type": "core::felt252",
"kind": "data"
},
{
"name": "keys",
"type": "core::array::Span::<core::felt252>",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind = "Class"
class_hash = "0xa349b743d361ce4567361475a89b84a386bb383448c6926954e5fe0b525597"
original_class_hash = "0xa349b743d361ce4567361475a89b84a386bb383448c6926954e5fe0b525597"
class_hash = "0x6f4515274ee23404789c3351a77107d0ec07508530119822046600ca6948d6e"
original_class_hash = "0x6f4515274ee23404789c3351a77107d0ec07508530119822046600ca6948d6e"
abi = "manifests/dev/base/abis/dojo-world.json"
tag = "dojo-world"
manifest_name = "dojo-world"
5 changes: 5 additions & 0 deletions crates/dojo-world/src/contracts/abi/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,11 @@ abigen!(
"type": "core::felt252",
"kind": "data"
},
{
"name": "entity_id",
"type": "core::felt252",
"kind": "data"
},
{
"name": "keys",
"type": "core::array::Span::<core::felt252>",
Expand Down
3 changes: 2 additions & 1 deletion crates/sozo/ops/src/tests/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ async fn test_model_ops() {
)
.await
.unwrap(),
Felt::from_hex("0x5581faa4aa9c7cf1903a7e07f28707a0a599b602ca13f3d2f88d68322e240d").unwrap()
Felt::from_hex("0x68e3a53988f20d84c6652f25d6add070633a5d05f8c4ac68285cacb228afa14")
.unwrap()
);

let layout =
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/core/src/processors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub mod store_update_member;
pub mod store_update_record;

const MODEL_INDEX: usize = 0;
const NUM_KEYS_INDEX: usize = 1;
const ENTITY_ID_INDEX: usize = 1;
const NUM_KEYS_INDEX: usize = 2;

#[async_trait]
pub trait EventProcessor<P>
Expand Down
13 changes: 8 additions & 5 deletions crates/torii/core/src/processors/store_set_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use starknet::providers::Provider;
use tracing::info;

use super::EventProcessor;
use crate::processors::{MODEL_INDEX, NUM_KEYS_INDEX};
use crate::sql::Sql;
use crate::processors::{ENTITY_ID_INDEX, MODEL_INDEX, NUM_KEYS_INDEX};
use crate::sql::{felts_sql_string, Sql};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::store_set_record";

Expand Down Expand Up @@ -46,9 +46,9 @@ where
event_id: &str,
event: &Event,
) -> Result<(), Error> {
let selector = event.data[MODEL_INDEX];
let model_id = event.data[MODEL_INDEX];

let model = db.model(selector).await?;
let model = db.model(model_id).await?;

info!(
target: LOG_TARGET,
Expand All @@ -60,6 +60,7 @@ where
let keys_end: usize =
keys_start + event.data[NUM_KEYS_INDEX].to_usize().context("invalid usize")?;
let keys = event.data[keys_start..keys_end].to_vec();
let keys_str = felts_sql_string(&keys);

// keys_end is already the length of the values array.

Expand All @@ -68,12 +69,14 @@ where
values_start + event.data[keys_end].to_usize().context("invalid usize")?;

let values = event.data[values_start..values_end].to_vec();
let entity_id = event.data[ENTITY_ID_INDEX];

let mut keys_and_unpacked = [keys, values].concat();

let mut entity = model.schema;
entity.deserialize(&mut keys_and_unpacked)?;

db.set_entity(entity, event_id, block_timestamp).await?;
db.set_entity(entity, event_id, block_timestamp, entity_id, model_id, &keys_str).await?;
Ok(())
}
}
10 changes: 6 additions & 4 deletions crates/torii/core/src/processors/store_update_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use super::EventProcessor;
use crate::processors::{ENTITY_ID_INDEX, MODEL_INDEX};
use crate::sql::Sql;
use crate::sql::{felts_sql_string, Sql};

pub(crate) const LOG_TARGET: &str = "torii_core::processors::store_update_record";

Expand Down Expand Up @@ -47,10 +47,10 @@
event_id: &str,
event: &Event,
) -> Result<(), Error> {
let selector = event.data[MODEL_INDEX];
let model_id = event.data[MODEL_INDEX];

Check warning on line 50 in crates/torii/core/src/processors/store_update_record.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/store_update_record.rs#L50

Added line #L50 was not covered by tests
let entity_id = event.data[ENTITY_ID_INDEX];

let model = db.model(selector).await?;
let model = db.model(model_id).await?;

Check warning on line 53 in crates/torii/core/src/processors/store_update_record.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/store_update_record.rs#L53

Added line #L53 was not covered by tests

info!(
target: LOG_TARGET,
Expand All @@ -71,12 +71,14 @@
// Keys are read from the db, since we don't have access to them when only
// the entity id is passed.
let keys = db.get_entity_keys(entity_id, &tag).await?;

let keys_str = felts_sql_string(&keys);

Check warning on line 75 in crates/torii/core/src/processors/store_update_record.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/store_update_record.rs#L74-L75

Added lines #L74 - L75 were not covered by tests
let mut keys_and_unpacked = [keys, values].concat();

let mut entity = model.schema;
entity.deserialize(&mut keys_and_unpacked)?;

db.set_entity(entity, event_id, block_timestamp).await?;
db.set_entity(entity, event_id, block_timestamp, entity_id, model_id, &keys_str).await?;

Check warning on line 81 in crates/torii/core/src/processors/store_update_record.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/store_update_record.rs#L81

Added line #L81 was not covered by tests
Ok(())
}
}
23 changes: 7 additions & 16 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,37 +165,28 @@ impl Sql {
entity: Ty,
event_id: &str,
block_timestamp: u64,
entity_id: Felt,
model_id: Felt,
keys_str: &str,
) -> Result<()> {
let keys = if let Ty::Struct(s) = &entity {
let mut keys = Vec::new();
for m in s.keys() {
keys.extend(m.serialize()?);
}
keys
} else {
return Err(anyhow!("Entity is not a struct"));
};

let namespaced_name = entity.name();
let (model_namespace, model_name) = namespaced_name.split_once('-').unwrap();

let entity_id = format!("{:#x}", poseidon_hash_many(&keys));
let model_id = format!("{:#x}", compute_selector_from_names(model_namespace, model_name));
let entity_id = format!("{:#x}", entity_id);
let model_id = format!("{:#x}", model_id);

self.query_queue.enqueue(
"INSERT INTO entity_model (entity_id, model_id) VALUES (?, ?) ON CONFLICT(entity_id, \
model_id) DO NOTHING",
vec![Argument::String(entity_id.clone()), Argument::String(model_id.clone())],
);

let keys_str = felts_sql_string(&keys);
let insert_entities = "INSERT INTO entities (id, keys, event_id, executed_at) VALUES (?, \
?, ?, ?) ON CONFLICT(id) DO UPDATE SET \
updated_at=CURRENT_TIMESTAMP, executed_at=EXCLUDED.executed_at, \
event_id=EXCLUDED.event_id RETURNING *";
let mut entity_updated: EntityUpdated = sqlx::query_as(insert_entities)
.bind(&entity_id)
.bind(&keys_str)
.bind(keys_str)
.bind(event_id)
.bind(utc_dt_string_from_timestamp(block_timestamp))
.fetch_one(&self.pool)
Expand Down Expand Up @@ -1184,7 +1175,7 @@ impl Sql {
}
}

fn felts_sql_string(felts: &[Felt]) -> String {
pub fn felts_sql_string(felts: &[Felt]) -> String {
felts.iter().map(|k| format!("{:#x}", k)).collect::<Vec<String>>().join(FELT_DELIMITER)
+ FELT_DELIMITER
}
Loading
Loading