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

feat(torii-core): store update member #2182

Merged
merged 38 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
98801d3
feat(torii-core): store update member
Larkooo Jul 17, 2024
3f6908e
feat; add set_model_member for updating specific member
Larkooo Jul 17, 2024
8aba41c
chore: format
Larkooo Jul 17, 2024
65b2c39
chore: update log to include member nname
Larkooo Jul 17, 2024
b54901c
fmt
Larkooo Jul 17, 2024
349b327
refactor: clean code & add is event message
Larkooo Jul 17, 2024
9dd48d8
fmt
Larkooo Jul 17, 2024
b0d9fd5
chore
Larkooo Jul 17, 2024
71af001
feat: emit the store member update from the world
glihm Jul 18, 2024
9343249
fix: ensure event messages are emitted with the correct selector
glihm Jul 18, 2024
a900617
merge main
glihm Jul 18, 2024
acf7c60
fix: cairo fmt
glihm Jul 18, 2024
8bcaf36
fix: fix test typo
glihm Jul 18, 2024
91949d5
wip:
glihm Jul 18, 2024
7fa5f5a
fix: simple types model memebr update
Larkooo Jul 18, 2024
8d14285
chore; pass memmber type to func
Larkooo Jul 18, 2024
7e4c56c
chore: clean code
Larkooo Jul 18, 2024
6f23b9a
fmt
Larkooo Jul 18, 2024
59736ee
wip
Larkooo Jul 19, 2024
98b7d48
fix: upsert to avoid constraint CHECK failing
Larkooo Jul 19, 2024
5d8e141
fmt
Larkooo Jul 19, 2024
ba34cd3
Merge remote-tracking branch 'upstream/main' into store-update-member
Larkooo Jul 29, 2024
2e87cde
fix: figure out way to fix upsert issue to reuse func
Larkooo Jul 29, 2024
ddb37c3
refactor: refactor recursive set to handle store update member
Larkooo Jul 29, 2024
ac41ee0
fix: fix world typo and merge
glihm Jul 30, 2024
58eef8d
merge main
glihm Jul 30, 2024
8805fa0
fix: struct from ty as mutable
Larkooo Jul 30, 2024
c6bfb58
fix: compilable torii
Larkooo Jul 30, 2024
e4a734d
feat: wrap up store update member
Larkooo Jul 30, 2024
1a585b5
fmt
Larkooo Jul 30, 2024
d81a2d7
fix fmt
Larkooo Jul 30, 2024
ac8b09d
fmt
Larkooo Jul 30, 2024
14861ad
cairo fmt
Larkooo Jul 30, 2024
a87133d
fix: update example to set array with new model API
glihm Jul 30, 2024
92ec2fe
fix: handle arrays
Larkooo Jul 31, 2024
d6c939e
fix: correctly handle arrays
Larkooo Jul 31, 2024
652a1f4
chore
Larkooo Jul 31, 2024
72cc8df
fix: test
Larkooo Jul 31, 2024
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
41 changes: 36 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bin/torii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use torii_core::processors::store_del_record::StoreDelRecordProcessor;
use torii_core::processors::store_set_record::StoreSetRecordProcessor;
use torii_core::processors::store_transaction::StoreTransactionProcessor;
use torii_core::processors::store_update_member::StoreUpdateMemberProcessor;
use torii_core::processors::store_update_record::StoreUpdateRecordProcessor;
use torii_core::simple_broker::SimpleBroker;
use torii_core::sql::Sql;
Expand Down Expand Up @@ -172,6 +173,7 @@
Box::new(StoreDelRecordProcessor),
Box::new(EventMessageProcessor),
Box::new(StoreUpdateRecordProcessor),
Box::new(StoreUpdateMemberProcessor),

Check warning on line 176 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L176

Added line #L176 was not covered by tests
glihm marked this conversation as resolved.
Show resolved Hide resolved
],
transaction: vec![Box::new(StoreTransactionProcessor)],
..Processors::default()
Expand Down
23 changes: 20 additions & 3 deletions crates/dojo-core/src/world/world_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub mod world {
ModelRegistered: ModelRegistered,
StoreSetRecord: StoreSetRecord,
StoreUpdateRecord: StoreUpdateRecord,
StoreUpdateMember: StoreUpdateMember,
StoreDelRecord: StoreDelRecord,
WriterUpdated: WriterUpdated,
OwnerUpdated: OwnerUpdated,
Expand Down Expand Up @@ -219,6 +220,14 @@ pub mod world {
pub values: Span<felt252>,
}

#[derive(Drop, starknet::Event)]
pub struct StoreUpdateMember {
pub table: felt252,
pub entity_id: felt252,
pub member_selector: felt252,
pub values: Span<felt252>,
}

#[derive(Drop, starknet::Event)]
pub struct StoreDelRecord {
pub table: felt252,
Expand Down Expand Up @@ -821,10 +830,18 @@ pub mod world {
);
},
ModelIndex::MemberId((
entity_id, member_id
entity_id, member_selector
)) => {
self.write_model_member(model_selector, entity_id, member_id, values, layout);
// TODO: here we need a new event update and see how Torii can process that.
self
.write_model_member(
model_selector, entity_id, member_selector, values, layout
);
EventEmitter::emit(
ref self,
StoreUpdateMember {
table: model_selector, entity_id, member_selector, values
}
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,33 @@
}
]
},
{
"type": "event",
"name": "dojo::world::world_contract::world::StoreUpdateMember",
"kind": "struct",
"members": [
{
"name": "table",
"type": "core::felt252",
"kind": "data"
},
{
"name": "entity_id",
"type": "core::felt252",
"kind": "data"
},
{
"name": "member_selector",
"type": "core::felt252",
"kind": "data"
},
{
"name": "values",
"type": "core::array::Span::<core::felt252>",
"kind": "data"
}
]
},
{
"type": "event",
"name": "dojo::world::world_contract::world::StoreDelRecord",
Expand Down Expand Up @@ -1156,6 +1183,11 @@
"type": "dojo::world::world_contract::world::StoreUpdateRecord",
"kind": "nested"
},
{
"name": "StoreUpdateMember",
"type": "dojo::world::world_contract::world::StoreUpdateMember",
"kind": "nested"
},
{
"name": "StoreDelRecord",
"type": "dojo::world::world_contract::world::StoreDelRecord",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind = "Class"
class_hash = "0x4c90da98d2bad157dec1d7b9b9c8c5861828a7ec1b323425d84fa6c3071303f"
original_class_hash = "0x4c90da98d2bad157dec1d7b9b9c8c5861828a7ec1b323425d84fa6c3071303f"
class_hash = "0x2178527e9556d1aa21d3c2961d28f9114fcfed81b4c3674ed591c50ce46cc9d"
original_class_hash = "0x2178527e9556d1aa21d3c2961d28f9114fcfed81b4c3674ed591c50ce46cc9d"
abi = "manifests/dev/base/abis/dojo-world.json"
tag = "dojo-world"
manifest_name = "dojo-world"
32 changes: 32 additions & 0 deletions crates/dojo-world/src/contracts/abi/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,33 @@ abigen!(
}
]
},
{
"type": "event",
"name": "dojo::world::world_contract::world::StoreUpdateMember",
"kind": "struct",
"members": [
{
"name": "table",
"type": "core::felt252",
"kind": "data"
},
{
"name": "entity_id",
"type": "core::felt252",
"kind": "data"
},
{
"name": "member_selector",
"type": "core::felt252",
"kind": "data"
},
{
"name": "values",
"type": "core::array::Span::<core::felt252>",
"kind": "data"
}
]
},
{
"type": "event",
"name": "dojo::world::world_contract::world::StoreDelRecord",
Expand Down Expand Up @@ -1162,6 +1189,11 @@ abigen!(
"type": "dojo::world::world_contract::world::StoreUpdateRecord",
"kind": "nested"
},
{
"name": "StoreUpdateMember",
"type": "dojo::world::world_contract::world::StoreUpdateMember",
"kind": "nested"
},
{
"name": "StoreDelRecord",
"type": "dojo::world::world_contract::world::StoreDelRecord",
Expand Down
2 changes: 1 addition & 1 deletion crates/sozo/ops/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ mod tests {
let result = extract_events(&manifest, &project_dir, &target_dir).unwrap();

// we are just collecting all events from manifest file so just verifying count should work
assert_eq!(result.len(), 17);
assert_eq!(result.len(), 18);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/sozo/ops/src/tests/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn test_model_ops() {
)
.await
.unwrap(),
Felt::from_hex("0x2b14bbb22e6a21cc949e06a187436c96aeab0e0290b3a8d91fb357ed2e6d973")
Felt::from_hex("0x4eddd8563a17c7d256b35e3cb0decdfcdfe122dd72593ebc572cfc535941ac2")
.unwrap()
);

Expand Down
1 change: 1 addition & 0 deletions crates/torii/core/src/processors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod register_model;
pub mod store_del_record;
pub mod store_set_record;
pub mod store_transaction;
pub mod store_update_member;
pub mod store_update_record;

const MODEL_INDEX: usize = 0;
Expand Down
108 changes: 108 additions & 0 deletions crates/torii/core/src/processors/store_update_member.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use anyhow::{Context, Error, Result};
use async_trait::async_trait;
use dojo_world::contracts::model::ModelReader;
use dojo_world::contracts::naming;
use dojo_world::contracts::world::WorldContractReader;
use num_traits::ToPrimitive;
use starknet::core::types::{Event, TransactionReceiptWithBlockInfo};
use starknet::core::utils::get_selector_from_name;
use starknet::providers::Provider;
use tracing::{info, warn};

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

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

const MEMBER_INDEX: usize = 2;

#[derive(Default, Debug)]
pub struct StoreUpdateMemberProcessor;

#[async_trait]
impl<P> EventProcessor<P> for StoreUpdateMemberProcessor
where
P: Provider + Send + Sync + std::fmt::Debug,
{
fn event_key(&self) -> String {
"StoreUpdateMember".to_string()
}

Check warning on line 30 in crates/torii/core/src/processors/store_update_member.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/store_update_member.rs#L28-L30

Added lines #L28 - L30 were not covered by tests

fn validate(&self, event: &Event) -> bool {
if event.keys.len() > 1 {
info!(

Check warning on line 34 in crates/torii/core/src/processors/store_update_member.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/store_update_member.rs#L32-L34

Added lines #L32 - L34 were not covered by tests
target: LOG_TARGET,
event_key = %<StoreUpdateMemberProcessor as EventProcessor<P>>::event_key(self),
invalid_keys = %<StoreUpdateMemberProcessor as EventProcessor<P>>::event_keys_as_string(self, event),
"Invalid event keys."

Check warning on line 38 in crates/torii/core/src/processors/store_update_member.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/store_update_member.rs#L36-L38

Added lines #L36 - L38 were not covered by tests
);
return false;
}
true
}

Check warning on line 43 in crates/torii/core/src/processors/store_update_member.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/store_update_member.rs#L40-L43

Added lines #L40 - L43 were not covered by tests

async fn process(
&self,
_world: &WorldContractReader<P>,
db: &mut Sql,
_block_number: u64,
block_timestamp: u64,
_transaction_receipt: &TransactionReceiptWithBlockInfo,
event_id: &str,
event: &Event,
) -> Result<(), Error> {
let selector = event.data[MODEL_INDEX];
let entity_id = event.data[ENTITY_ID_INDEX];
let member_selector = event.data[MEMBER_INDEX];

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

let mut member = schema
.as_struct()
.expect("model schema must be a struct")
.children
.iter()
.find(|c| {
get_selector_from_name(&c.name).expect("invalid selector for member name")
== member_selector
})
.context("member not found")?
.clone();

info!(
target: LOG_TARGET,
name = %model.name(),
entity_id = format!("{:#x}", entity_id),
member = %member.name,
"Store update member.",
);

let values_start = MEMBER_INDEX + 1;
let values_end: usize =
values_start + event.data[values_start].to_usize().context("invalid usize")?;

// Skip the length to only get the values as they will be deserialized.
let mut values = event.data[values_start + 1..=values_end].to_vec();

let tag = naming::get_tag(model.namespace(), model.name());

if !db.does_entity_exist(tag.clone(), entity_id).await? {
warn!(
target: LOG_TARGET,
tag,
entity_id = format!("{:#x}", entity_id),
"Entity not found, must be set before updating a member.",
);

return Ok(());
}

member.ty.deserialize(&mut values)?;

db.set_model_member(&schema.name(), entity_id, false, &member, event_id, block_timestamp)
.await?;
Ok(())
}

Check warning on line 107 in crates/torii/core/src/processors/store_update_member.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/store_update_member.rs#L54-L107

Added lines #L54 - L107 were not covered by tests
}
Loading
Loading