Skip to content

Commit

Permalink
set some world event fields as event keys
Browse files Browse the repository at this point in the history
  • Loading branch information
remybar authored and Larkooo committed Aug 27, 2024
1 parent e1a349b commit 2aa07db
Showing 17 changed files with 344 additions and 279 deletions.
138 changes: 85 additions & 53 deletions crates/dojo-core/src/tests/world/resources.cairo
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use dojo::model::{Model, ResourceMetadata};
use dojo::utils::{bytearray_hash, entity_id_from_keys};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait, world};
use dojo::world::world::{
NamespaceRegistered, ModelRegistered, ModelUpgraded, MetadataUpdate, ContractDeployed,
Event, NamespaceRegistered, ModelRegistered, ModelUpgraded, MetadataUpdate, ContractDeployed,
ContractUpgraded
};
use dojo::contract::{IContractDispatcher, IContractDispatcherTrait};
@@ -51,7 +51,11 @@ fn test_set_metadata_resource_owner() {

assert_eq!(
starknet::testing::pop_log(world.contract_address),
Option::Some(MetadataUpdate { resource: metadata.resource_id, uri: metadata.metadata_uri })
Option::Some(
Event::MetadataUpdate(
MetadataUpdate { resource: metadata.resource_id, uri: metadata.metadata_uri }
)
)
);
}

@@ -137,17 +141,22 @@ fn test_register_model_for_namespace_owner() {
starknet::testing::set_contract_address(bob);
world.register_model(foo::TEST_CLASS_HASH.try_into().unwrap());

let event = starknet::testing::pop_log::<ModelRegistered>(world.contract_address);

assert(event.is_some(), 'no ModelRegistered event');
let event = event.unwrap();
assert(event.name == Model::<Foo>::name(), 'bad model name');
assert(event.namespace == Model::<Foo>::namespace(), 'bad model namespace');
assert(event.class_hash == foo::TEST_CLASS_HASH.try_into().unwrap(), 'bad model class_hash');
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(),
'bad model prev address'
);
let event = starknet::testing::pop_log::<Event>(world.contract_address);
assert(event.is_some(), 'no event)');

if let Event::ModelRegistered(event) = event.unwrap() {
assert(event.name == Model::<Foo>::name(), 'bad model name');
assert(event.namespace == Model::<Foo>::namespace(), 'bad model namespace');
assert(
event.class_hash == foo::TEST_CLASS_HASH.try_into().unwrap(), 'bad model class_hash'
);
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(),
'bad model prev address'
);
} else {
core::panic_with_felt252('no ModelRegistered event');
}

assert(world.is_owner(Model::<Foo>::selector(), bob), 'bob is not the owner');
}
@@ -165,17 +174,22 @@ fn test_register_model_for_namespace_writer() {
starknet::testing::set_contract_address(bob);
world.register_model(foo::TEST_CLASS_HASH.try_into().unwrap());

let event = starknet::testing::pop_log::<ModelRegistered>(world.contract_address);

assert(event.is_some(), 'no ModelRegistered event');
let event = event.unwrap();
assert(event.name == Model::<Foo>::name(), 'bad model name');
assert(event.namespace == Model::<Foo>::namespace(), 'bad model namespace');
assert(event.class_hash == foo::TEST_CLASS_HASH.try_into().unwrap(), 'bad model class_hash');
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(),
'bad model prev address'
);
let event = starknet::testing::pop_log::<Event>(world.contract_address);
assert(event.is_some(), 'no event)');

if let Event::ModelRegistered(event) = event.unwrap() {
assert(event.name == Model::<Foo>::name(), 'bad model name');
assert(event.namespace == Model::<Foo>::namespace(), 'bad model namespace');
assert(
event.class_hash == foo::TEST_CLASS_HASH.try_into().unwrap(), 'bad model class_hash'
);
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(),
'bad model prev address'
);
} else {
core::panic_with_felt252('no ModelRegistered event');
}

assert(world.is_owner(Model::<Foo>::selector(), bob), 'bob is not the owner');
}
@@ -195,17 +209,22 @@ fn test_upgrade_model_from_model_owner() {

world.upgrade_model(foo::TEST_CLASS_HASH.try_into().unwrap());

let event = starknet::testing::pop_log::<ModelUpgraded>(world.contract_address);

assert(event.is_some(), 'no ModelRegistered event');
let event = event.unwrap();
assert(event.name == Model::<Foo>::name(), 'bad model name');
assert(event.namespace == Model::<Foo>::namespace(), 'bad model namespace');
assert(event.class_hash == foo::TEST_CLASS_HASH.try_into().unwrap(), 'bad model class_hash');
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(),
'bad model prev address'
);
let event = starknet::testing::pop_log::<Event>(world.contract_address);
assert(event.is_some(), 'no event)');

if let Event::ModelUpgraded(event) = event.unwrap() {
assert(event.name == Model::<Foo>::name(), 'bad model name');
assert(event.namespace == Model::<Foo>::namespace(), 'bad model namespace');
assert(
event.class_hash == foo::TEST_CLASS_HASH.try_into().unwrap(), 'bad model class_hash'
);
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(),
'bad model prev address'
);
} else {
core::panic_with_felt252('no ModelRegistered event');
}

assert(world.is_owner(Model::<Foo>::selector(), bob), 'bob is not the owner');
}
@@ -295,9 +314,12 @@ fn test_register_namespace() {

assert(world.is_owner(hash, bob), 'namespace not registered');

let expected_event = NamespaceRegistered { namespace, hash };
assert_eq!(
starknet::testing::pop_log(world.contract_address),
Option::Some(NamespaceRegistered { namespace: "namespace", hash })
Option::Some(
Event::NamespaceRegistered(Event::NamespaceRegistered(NamespaceRegistered))
)
);
}

@@ -349,16 +371,21 @@ fn test_deploy_contract_for_namespace_owner() {
let contract_address = world.deploy_contract('salt1', class_hash);
let dispatcher = IContractDispatcher { contract_address };

let event = starknet::testing::pop_log::<ContractDeployed>(world.contract_address);
assert(event.is_some(), 'no ContractDeployed event');
let event = event.unwrap();
assert(event.salt == 'salt1', 'bad event salt');
assert(event.class_hash == class_hash, 'bad class_hash');
assert(event.name == dispatcher.contract_name(), 'bad contract name');
assert(event.namespace == dispatcher.namespace(), 'bad namespace');
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(), 'bad contract address'
);
let event = starknet::testing::pop_log::<Event>(world.contract_address);
assert(event.is_some(), 'no event)');

if let Event::ContractDeployed(event) = event.unwrap() {
assert(event.salt == 'salt1', 'bad event salt');
assert(event.class_hash == class_hash, 'bad class_hash');
assert(event.name == dispatcher.contract_name(), 'bad contract name');
assert(event.namespace == dispatcher.namespace(), 'bad namespace');
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(),
'bad contract address'
);
} else {
core::panic_with_felt252('no ContractDeployed event');
};
}

#[test]
@@ -437,13 +464,18 @@ fn test_upgrade_contract_from_resource_owner() {

world.upgrade_contract(dispatcher.selector(), class_hash);

let event = starknet::testing::pop_log::<ContractUpgraded>(world.contract_address);
assert(event.is_some(), 'no ContractUpgraded event');
let event = event.unwrap();
assert(event.class_hash == class_hash, 'bad class_hash');
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(), 'bad contract address'
);
let event = starknet::testing::pop_log::<Event>(world.contract_address);
assert(event.is_some(), 'no event)');

if let Event::ContractUpgraded(event) = event.unwrap() {
assert(event.class_hash == class_hash, 'bad class_hash');
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(),
'bad contract address'
);
} else {
core::panic_with_felt252('no ContractUpgraded event');
};
}

#[test]
9 changes: 7 additions & 2 deletions crates/dojo-core/src/tests/world/world.cairo
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ use dojo::world::{
IWorldDispatcher, IWorldDispatcherTrait, world, IUpgradeableWorld, IUpgradeableWorldDispatcher,
IUpgradeableWorldDispatcherTrait
};
use dojo::world::world::Event;
use dojo::tests::helpers::{
IbarDispatcher, IbarDispatcherTrait, drop_all_events, deploy_world_and_bar, Foo, foo, bar,
Character, character, test_contract, test_contract_with_dojo_init_args
@@ -310,9 +311,12 @@ fn test_differ_program_hash_event_emit() {

config.set_differ_program_hash(program_hash: 98758347158781475198374598718743);

let expected_event = Event::DifferProgramHashUpdate(
DifferProgramHashUpdate { program_hash: 98758347158781475198374598718743 }
);
assert_eq!(
starknet::testing::pop_log(world.contract_address),
Option::Some(DifferProgramHashUpdate { program_hash: 98758347158781475198374598718743 })
Option::Some(Event::DifferProgramHashUpdate(expected_event))
);
}

@@ -325,9 +329,10 @@ fn test_facts_registry_event_emit() {

config.set_facts_registry(contract_address_const::<0x12>());

let expected_event = FactsRegistryUpdate { address: contract_address_const::<0x12>() };
assert_eq!(
starknet::testing::pop_log(world.contract_address),
Option::Some(FactsRegistryUpdate { address: contract_address_const::<0x12>() })
Option::Some(Event::FactsRegistryUpdate(expected_event))
);
}

42 changes: 32 additions & 10 deletions crates/dojo-core/src/world/world_contract.cairo
Original file line number Diff line number Diff line change
@@ -140,7 +140,7 @@ pub mod world {
impl ConfigInternalImpl = Config::InternalImpl<ContractState>;

#[event]
#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub enum Event {
WorldSpawned: WorldSpawned,
ContractDeployed: ContractDeployed,
@@ -161,33 +161,38 @@ pub mod world {
StateUpdated: StateUpdated
}

#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct StateUpdated {
pub da_hash: felt252,
}

#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct WorldSpawned {
pub address: ContractAddress,
#[key]
pub creator: ContractAddress
}

#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct WorldUpgraded {
pub class_hash: ClassHash,
}

#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct ContractDeployed {
pub salt: felt252,
#[key]
pub class_hash: ClassHash,
pub address: ContractAddress,
#[key]
pub namespace: ByteArray,
#[key]
pub name: ByteArray
}

#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct ContractUpgraded {
#[key]
pub class_hash: ClassHash,
pub address: ContractAddress,
}
@@ -200,19 +205,23 @@ pub mod world {

#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct MetadataUpdate {
#[key]
pub resource: felt252,
pub uri: ByteArray
}

#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct NamespaceRegistered {
#[key]
pub namespace: ByteArray,
pub hash: felt252
}

#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct ModelRegistered {
#[key]
pub name: ByteArray,
#[key]
pub namespace: ByteArray,
pub class_hash: ClassHash,
pub address: ContractAddress,
@@ -228,44 +237,57 @@ pub mod world {
pub prev_address: ContractAddress,
}

#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct StoreSetRecord {
#[key]
pub table: felt252,
#[key]
pub keys: Span<felt252>,
pub values: Span<felt252>,
}

#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct StoreUpdateRecord {
#[key]
pub table: felt252,
#[key]
pub entity_id: felt252,
pub values: Span<felt252>,
}

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

#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct StoreDelRecord {
#[key]
pub table: felt252,
#[key]
pub entity_id: felt252,
}

#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct WriterUpdated {
#[key]
pub resource: felt252,
#[key]
pub contract: ContractAddress,
pub value: bool
}

#[derive(Drop, starknet::Event)]
#[derive(Drop, starknet::Event, Debug, PartialEq)]
pub struct OwnerUpdated {
#[key]
pub address: ContractAddress,
#[key]
pub resource: felt252,
pub value: bool,
}
Loading

0 comments on commit 2aa07db

Please sign in to comment.