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 committed Aug 12, 2024
1 parent 0bf090c commit 3ee44ce
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 247 deletions.
19 changes: 14 additions & 5 deletions crates/dojo-core/src/tests/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use dojo::world::{
IWorldDispatcher, IWorldDispatcherTrait, world, IUpgradeableWorld, IUpgradeableWorldDispatcher,
IUpgradeableWorldDispatcherTrait, Resource
};
use dojo::world::world::NamespaceRegistered;
use dojo::world::world::{Event, NamespaceRegistered};

use super::benchmarks;
use super::benchmarks::Character;
Expand Down Expand Up @@ -491,13 +491,14 @@ fn test_register_namespace() {
let namespace = "namespace";
let hash = bytearray_hash(@namespace);

world.register_namespace(namespace);
world.register_namespace(namespace.clone());

assert(world.is_owner(hash, caller), '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(expected_event))
);
}

Expand Down Expand Up @@ -949,7 +950,11 @@ fn test_differ_program_hash_event_emit() {

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

Expand All @@ -964,7 +969,11 @@ fn test_facts_registry_event_emit() {

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

Expand Down
131 changes: 81 additions & 50 deletions crates/dojo-core/src/tests/world/resources.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 }
)
)
);
}

Expand Down Expand Up @@ -132,17 +136,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);
let event = starknet::testing::pop_log::<Event>(world.contract_address);
assert(event.is_some(), 'no event)');

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'
);
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');
}
Expand All @@ -160,17 +169,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);
let event = starknet::testing::pop_log::<Event>(world.contract_address);
assert(event.is_some(), 'no event)');

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'
);
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');
}
Expand All @@ -190,17 +204,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);
let event = starknet::testing::pop_log::<Event>(world.contract_address);
assert(event.is_some(), 'no event)');

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'
);
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');
}
Expand Down Expand Up @@ -290,7 +309,9 @@ fn test_register_namespace() {

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

Expand Down Expand Up @@ -366,16 +387,21 @@ fn test_deploy_contract_for_namespace_owner() {
let contract_address = world.deploy_contract('salt1', class_hash, array![].span());
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]
Expand Down Expand Up @@ -467,13 +493,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]
Expand Down
42 changes: 32 additions & 10 deletions crates/dojo-core/src/world/world_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,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,
Expand All @@ -147,52 +147,61 @@ 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,
}

#[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,
Expand All @@ -208,44 +217,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,
}
Expand Down
Loading

0 comments on commit 3ee44ce

Please sign in to comment.