Skip to content

Commit

Permalink
feat: Improve fn delete_entity(), add event StoreDelRecordProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
gianalarcon committed Jan 15, 2024
1 parent f78f0de commit 0e935fb
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 24 deletions.
23 changes: 12 additions & 11 deletions crates/dojo-core/src/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ trait IWorld<T> {

#[starknet::interface]
trait IUpgradeableWorld<T> {
fn upgrade(ref self: T, new_class_hash : ClassHash);
fn upgrade(ref self: T, new_class_hash: ClassHash);
}

#[starknet::interface]
Expand All @@ -70,15 +70,15 @@ mod world {
use starknet::{
get_caller_address, get_contract_address, get_tx_info,
contract_address::ContractAddressIntoFelt252, ClassHash, Zeroable, ContractAddress,
syscalls::{deploy_syscall, emit_event_syscall, replace_class_syscall}, SyscallResult, SyscallResultTrait,
SyscallResultTraitImpl
syscalls::{deploy_syscall, emit_event_syscall, replace_class_syscall}, SyscallResult,
SyscallResultTrait, SyscallResultTraitImpl
};

use dojo::database;
use dojo::database::index::WhereCondition;
use dojo::executor::{IExecutorDispatcher, IExecutorDispatcherTrait};
use dojo::world::{IWorldDispatcher, IWorld, IUpgradeableWorld};

use dojo::components::upgradeable::{IUpgradeableDispatcher, IUpgradeableDispatcherTrait};

const NAME_ENTRYPOINT: felt252 =
Expand Down Expand Up @@ -112,7 +112,7 @@ mod world {
struct WorldUpgraded {
class_hash: ClassHash,
}

#[derive(Drop, starknet::Event)]
struct ContractDeployed {
salt: felt252,
Expand Down Expand Up @@ -525,7 +525,7 @@ mod world {
};

let key = poseidon::poseidon_hash_span(keys);
database::set(model, key, 0, empty_values.span(), layout);
//database::set(model, key, 0, empty_values.span(), layout);
// this deletes the index
database::del(model, key);

Expand Down Expand Up @@ -641,17 +641,18 @@ mod world {
/// # Arguments
///
/// * `new_class_hash` - The new world class hash.
fn upgrade(ref self: ContractState, new_class_hash : ClassHash){
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
assert(new_class_hash.is_non_zero(), 'invalid class_hash');
assert(IWorld::is_owner(@self, get_tx_info().unbox().account_contract_address, WORLD), 'only owner can upgrade');
assert(
IWorld::is_owner(@self, get_tx_info().unbox().account_contract_address, WORLD),
'only owner can upgrade'
);

// upgrade to new_class_hash
replace_class_syscall(new_class_hash).unwrap();

// emit Upgrade Event
EventEmitter::emit(
ref self, WorldUpgraded {class_hash: new_class_hash }
);
EventEmitter::emit(ref self, WorldUpgraded { class_hash: new_class_hash });
}
}

Expand Down
13 changes: 13 additions & 0 deletions crates/torii/core/src/processors/store_del_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,22 @@ where

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?;
info!("get entity");
entity.deserialize(&mut keys_and_unpacked)?;

db.delete_entity(entity, event_id).await?;
info!("Deleted entity");
Ok(())
}
}
27 changes: 20 additions & 7 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use chrono::Utc;
use dojo_types::primitive::Primitive;
use dojo_types::schema::Ty;
use dojo_world::metadata::WorldMetadata;
use log::info;
use sqlx::pool::PoolConnection;
use sqlx::{Pool, Sqlite};
use starknet::core::types::{Event, FieldElement, InvokeTransactionV1};
Expand Down Expand Up @@ -149,13 +150,25 @@ impl Sql {
Ok(())
}

pub fn delete_entity(&mut self, model: String, key: FieldElement) {
println!("delete entity model: {}", model);
let model = Argument::String(model);
println!("delete entity key: {:#x}", key);
let id = Argument::FieldElement(key);

self.query_queue.enqueue("DELETE FROM ? WHERE id = ?", vec![model, id]);
pub async fn delete_entity(&mut self, entity: Ty, event_id: &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 entity_id = format!("{:#x}", poseidon_hash_many(&keys));
let table = format!("{}", entity.name());
info!("Deleting entity: {}, table: {}", entity_id, table);
self.query_queue.enqueue(
"DELETE FROM ? WHERE entity_id = ?",
vec![Argument::String(table), Argument::String(entity_id)],
);
self.query_queue.execute_all().await.unwrap();
Ok(())
}

pub fn set_metadata(&mut self, resource: &FieldElement, uri: &str) {
Expand Down
2 changes: 2 additions & 0 deletions crates/torii/server/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use tokio_stream::StreamExt;
use torii_core::engine::{Engine, EngineConfig, Processors};
use torii_core::processors::metadata_update::MetadataUpdateProcessor;
use torii_core::processors::register_model::RegisterModelProcessor;
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::simple_broker::SimpleBroker;
Expand Down Expand Up @@ -132,6 +133,7 @@ async fn main() -> anyhow::Result<()> {
Box::new(RegisterModelProcessor),
Box::new(StoreSetRecordProcessor),
Box::new(MetadataUpdateProcessor),
Box::new(StoreDelRecordProcessor),
],
transaction: vec![Box::new(StoreTransactionProcessor)],
..Processors::default()
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/types-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7

[[package]]
name = "types_test"
version = "0.4.4"
version = "0.5.0"
dependencies = [
"dojo",
]
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies = [

[[package]]
name = "dojo_examples"
version = "0.4.4"
version = "0.5.0"
dependencies = [
"dojo",
]
Expand Down
13 changes: 9 additions & 4 deletions examples/spawn-and-move/src/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use starknet::{ContractAddress, ClassHash};
trait IActions<TContractState> {
fn spawn(self: @TContractState);
fn move(self: @TContractState, direction: Direction);
fn delete(self: @TContractState);
fn delete_move(self: @TContractState);
}

#[dojo::contract]
Expand Down Expand Up @@ -88,18 +88,18 @@ mod actions {
return ();
}

fn delete(self: @ContractState) {
fn delete_move(self: @ContractState) {
// Access the world dispatcher for reading.
let world = self.world_dispatcher.read();

// Get the address of the current caller, possibly the player's address.
let player = get_caller_address();

// Retrieve the player's current position and moves data from the world.
let (position, moves) = get!(world, player, (Position, Moves));
let moves = get!(world, player, Moves);

// Delete the player's data from the world.
delete!(world, (position, moves));
delete!(world, (moves));
}
}
}
Expand Down Expand Up @@ -144,5 +144,10 @@ mod tests {
let new_position = get!(world, caller, Position);
assert(new_position.vec.x == 11, 'position x is wrong');
assert(new_position.vec.y == 10, 'position y is wrong');

actions_system.delete_move();
// check world state
let moves = get!(world, caller, Moves);
assert(moves.remaining == 0, 'moves is wrong');
}
}

0 comments on commit 0e935fb

Please sign in to comment.