Skip to content

Commit

Permalink
[Dojo] add dojo-core interfaces into their file + snake_case contracts (
Browse files Browse the repository at this point in the history
#588)

* add dojo-core interfaces into their file + snake_case

The snake_casing of the world and executor contracts
has an impact on the dojo-lang and dojo-world.
Modifications were made accordingly + refacto into
constants to allow future changes

* apply cargo fmt

* cargo fmt with nightly

* fix missing world use path
  • Loading branch information
glihm authored Jul 1, 2023
1 parent 5715d09 commit 7e5e358
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 128 deletions.
19 changes: 14 additions & 5 deletions crates/dojo-core/src/executor.cairo
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
use dojo::world::Context;

#[starknet::interface]
trait IExecutor<T> {
fn execute(self: @T, ctx: Context, calldata: Span<felt252>) -> Span<felt252>;
}

#[starknet::contract]
mod Executor {
mod executor {
use array::{ArrayTrait, ArrayTCloneImpl, SpanTrait};
use serde::Serde;
use clone::Clone;
use box::BoxTrait;
use traits::Into;
use traits::{TryInto, Into};
use option::OptionTrait;
use starknet::{get_caller_address, get_tx_info};

use dojo::interfaces::{
IWorldDispatcher, ISystemLibraryDispatcher, ISystemDispatcherTrait, IExecutor
};
use dojo::world::IWorldDispatcher;
use dojo::interfaces::{ISystemLibraryDispatcher, ISystemDispatcherTrait};
use dojo::world::Context;

use super::IExecutor;

const EXECUTE_ENTRYPOINT: felt252 =
0x0240060cdb34fcc260f41eac7474ee1d7c80b7e3607daff9ac67c7ea2ebb1c44;

Expand Down
56 changes: 1 addition & 55 deletions crates/dojo-core/src/interfaces.cairo
Original file line number Diff line number Diff line change
@@ -1,47 +1,4 @@
use serde::Serde;
use array::{ArrayTrait, SpanTrait};
use traits::{TryInto, Into};
use option::OptionTrait;

use starknet::{ClassHash, ContractAddress};
use starknet::contract_address::Felt252TryIntoContractAddress;

use dojo::database::query::Query;
use dojo::world::Context;

#[starknet::interface]
trait IWorld<T> {
fn component(self: @T, name: felt252) -> ClassHash;
fn register_component(ref self: T, class_hash: ClassHash);
fn system(self: @T, name: felt252) -> ClassHash;
fn register_system(ref self: T, class_hash: ClassHash);
fn uuid(ref self: T) -> usize;
fn emit(self: @T, keys: Span<felt252>, values: Span<felt252>);
fn execute(ref self: T, system: felt252, calldata: Span<felt252>) -> Span<felt252>;
fn entity(
self: @T, component: felt252, query: Query, offset: u8, length: usize
) -> Span<felt252>;
fn set_entity(ref self: T, component: felt252, query: Query, offset: u8, value: Span<felt252>);
fn entities(
self: @T, component: felt252, partition: felt252
) -> (Span<felt252>, Span<Span<felt252>>);
fn set_executor(ref self: T, contract_address: ContractAddress);
fn executor(self: @T) -> ContractAddress;
fn delete_entity(ref self: T, component: felt252, query: Query);

fn is_owner(self: @T, account: ContractAddress, target: felt252) -> bool;
fn grant_owner(ref self: T, account: ContractAddress, target: felt252);
fn revoke_owner(ref self: T, account: ContractAddress, target: felt252);

fn is_writer(self: @T, component: felt252, system: felt252) -> bool;
fn grant_writer(ref self: T, component: felt252, system: felt252);
fn revoke_writer(ref self: T, component: felt252, system: felt252);
}

#[starknet::interface]
trait IExecutor<T> {
fn execute(self: @T, ctx: Context, calldata: Span<felt252>) -> Span<felt252>;
}
use array::ArrayTrait;

#[starknet::interface]
trait IComponent<T> {
Expand All @@ -54,14 +11,3 @@ trait ISystem<T> {
fn name(self: @T) -> felt252;
fn dependencies(self: @T) -> Array<(felt252, bool)>;
}

#[starknet::interface]
trait IWorldFactory<T> {
fn set_world(ref self: T, class_hash: ClassHash);
fn set_executor(ref self: T, executor_address: ContractAddress);
fn spawn(
ref self: T, components: Array<ClassHash>, systems: Array<ClassHash>,
) -> ContractAddress;
fn world(self: @T) -> ClassHash;
fn executor(self: @T) -> ContractAddress;
}
7 changes: 4 additions & 3 deletions crates/dojo-core/src/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ use traits::TryInto;
use option::OptionTrait;
use core::{result::ResultTrait, traits::Into};

use dojo::{executor::Executor, world::World, interfaces::{IWorldDispatcher, IWorldDispatcherTrait}};
use dojo::executor::executor;
use dojo::world::{world, IWorldDispatcher, IWorldDispatcherTrait};

fn spawn_test_world(components: Array<felt252>, systems: Array<felt252>) -> IWorldDispatcher {
// deploy executor
let constructor_calldata = array::ArrayTrait::new();
let (executor_address, _) = deploy_syscall(
Executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false
executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false
)
.unwrap();

// deploy world
let mut world_constructor_calldata = array::ArrayTrait::new();
world_constructor_calldata.append(executor_address.into());
let (world_address, _) = deploy_syscall(
World::TEST_CLASS_HASH.try_into().unwrap(), 0, world_constructor_calldata.span(), false
world::TEST_CLASS_HASH.try_into().unwrap(), 0, world_constructor_calldata.span(), false
)
.unwrap();
let world = IWorldDispatcher { contract_address: world_address };
Expand Down
40 changes: 35 additions & 5 deletions crates/dojo-core/src/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use starknet::{ContractAddress, ClassHash, StorageAccess, StorageBaseAddress, Sy
use traits::{Into, TryInto};
use option::OptionTrait;

use dojo::interfaces::{IWorldDispatcher, IWorldDispatcherTrait};
use dojo::database::query::{Query, QueryTrait};

#[derive(Copy, Drop, Serde)]
struct Context {
Expand All @@ -12,8 +12,37 @@ struct Context {
system_class_hash: ClassHash, // Class hash of the calling system
}

#[starknet::interface]
trait IWorld<T> {
fn component(self: @T, name: felt252) -> ClassHash;
fn register_component(ref self: T, class_hash: ClassHash);
fn system(self: @T, name: felt252) -> ClassHash;
fn register_system(ref self: T, class_hash: ClassHash);
fn uuid(ref self: T) -> usize;
fn emit(self: @T, keys: Span<felt252>, values: Span<felt252>);
fn execute(ref self: T, system: felt252, calldata: Span<felt252>) -> Span<felt252>;
fn entity(
self: @T, component: felt252, query: Query, offset: u8, length: usize
) -> Span<felt252>;
fn set_entity(ref self: T, component: felt252, query: Query, offset: u8, value: Span<felt252>);
fn entities(
self: @T, component: felt252, partition: felt252
) -> (Span<felt252>, Span<Span<felt252>>);
fn set_executor(ref self: T, contract_address: ContractAddress);
fn executor(self: @T) -> ContractAddress;
fn delete_entity(ref self: T, component: felt252, query: Query);

fn is_owner(self: @T, account: ContractAddress, target: felt252) -> bool;
fn grant_owner(ref self: T, account: ContractAddress, target: felt252);
fn revoke_owner(ref self: T, account: ContractAddress, target: felt252);

fn is_writer(self: @T, component: felt252, system: felt252) -> bool;
fn grant_writer(ref self: T, component: felt252, system: felt252);
fn revoke_writer(ref self: T, component: felt252, system: felt252);
}

#[starknet::contract]
mod World {
mod world {
use array::{ArrayTrait, SpanTrait};
use traits::Into;
use option::OptionTrait;
Expand All @@ -27,11 +56,12 @@ mod World {

use dojo::database;
use dojo::database::query::{Query, QueryTrait};
use dojo::executor::{IExecutorDispatcher, IExecutorDispatcherTrait};
use dojo::interfaces::{
IComponentLibraryDispatcher, IComponentDispatcherTrait, IExecutorDispatcher,
IExecutorDispatcherTrait, ISystemLibraryDispatcher, ISystemDispatcherTrait,
IWorldDispatcher, IWorld
IComponentLibraryDispatcher, IComponentDispatcherTrait, ISystemLibraryDispatcher,
ISystemDispatcherTrait
};
use dojo::world::{IWorldDispatcher, IWorld};

use super::Context;

Expand Down
31 changes: 23 additions & 8 deletions crates/dojo-core/src/world_factory.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
use array::ArrayTrait;

use starknet::{ClassHash, ContractAddress};

#[starknet::interface]
trait IWorldFactory<T> {
fn set_world(ref self: T, class_hash: ClassHash);
fn set_executor(ref self: T, executor_address: ContractAddress);
fn spawn(
ref self: T, components: Array<ClassHash>, systems: Array<ClassHash>,
) -> ContractAddress;
fn world(self: @T) -> ClassHash;
fn executor(self: @T) -> ContractAddress;
}

#[starknet::contract]
mod WorldFactory {
mod world_factory {
use array::ArrayTrait;
use option::OptionTrait;
use traits::Into;
Expand All @@ -11,7 +24,9 @@ mod WorldFactory {
syscalls::deploy_syscall, get_caller_address
};

use dojo::interfaces::{IWorldDispatcher, IWorldDispatcherTrait, IWorldFactory};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use super::IWorldFactory;

#[storage]
struct Storage {
Expand All @@ -32,7 +47,7 @@ mod WorldFactory {

#[constructor]
fn constructor(
ref self: ContractState, world_class_hash_: ClassHash, executor_address_: ContractAddress,
ref self: ContractState, world_class_hash_: ClassHash, executor_address_: ContractAddress,
) {
self.world_class_hash.write(world_class_hash_);
self.executor_address.write(executor_address_);
Expand All @@ -44,14 +59,14 @@ mod WorldFactory {
///
/// # Arguments
///
/// * `components` - The components to be registered.
/// * `systems` - The systems to be registered.
/// * `components` - The components to be registered.
/// * `systems` - The systems to be registered.
///
/// # Returns
///
/// The address of the deployed world.
fn spawn(
ref self: ContractState, components: Array<ClassHash>, systems: Array<ClassHash>,
ref self: ContractState, components: Array<ClassHash>, systems: Array<ClassHash>,
) -> ContractAddress {
// deploy world
let mut world_constructor_calldata: Array<felt252> = ArrayTrait::new();
Expand Down Expand Up @@ -81,7 +96,7 @@ mod WorldFactory {
///
/// # Arguments
///
/// * `executor_address` - The address of the executor.
/// * `executor_address` - The address of the executor.
fn set_executor(ref self: ContractState, executor_address: ContractAddress) {
self.executor_address.write(executor_address);
}
Expand All @@ -90,7 +105,7 @@ mod WorldFactory {
///
/// # Arguments
///
/// * `class_hash` - The class hash of world.
/// * `class_hash` - The class hash of world.
fn set_world(ref self: ContractState, class_hash: ClassHash) {
self.world_class_hash.write(class_hash);
}
Expand Down
9 changes: 3 additions & 6 deletions crates/dojo-core/tests/src/executor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ use traits::TryInto;

use starknet::syscalls::deploy_syscall;
use starknet::class_hash::Felt252TryIntoClassHash;
use dojo::interfaces::IExecutorDispatcher;
use dojo::interfaces::IExecutorDispatcherTrait;
use dojo::interfaces::IWorldDispatcher;
use dojo::executor::Executor;
use dojo::world::Context;
use dojo::executor::{executor, IExecutorDispatcher, IExecutorDispatcherTrait};
use dojo::world::{Context, IWorldDispatcher};

#[derive(Component, Copy, Drop, Serde)]
struct Foo {
Expand All @@ -32,7 +29,7 @@ mod Bar {
fn test_executor() {
let constructor_calldata = array::ArrayTrait::new();
let (executor_address, _) = deploy_syscall(
Executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false
executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false
)
.unwrap();

Expand Down
13 changes: 5 additions & 8 deletions crates/dojo-core/tests/src/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ use starknet::get_caller_address;
use starknet::syscalls::deploy_syscall;

use dojo::database::query::QueryTrait;
use dojo::interfaces::IWorldDispatcher;
use dojo::interfaces::IWorldDispatcherTrait;
use dojo::executor::Executor;
use dojo::world::World;
use dojo::world::library_call;
use dojo::executor::executor;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait, library_call, world};

// Components and Systems

Expand Down Expand Up @@ -61,7 +58,7 @@ fn deploy_world() -> IWorldDispatcher {
let mut calldata: Array<felt252> = array::ArrayTrait::new();
calldata.append(starknet::contract_address_const::<0x0>().into());
let (world_address, _) = deploy_syscall(
World::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
world::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
)
.unwrap();

Expand Down Expand Up @@ -193,7 +190,7 @@ fn spawn_empty_world() -> IWorldDispatcher {
// Deploy executor contract
let executor_constructor_calldata = array::ArrayTrait::new();
let (executor_address, _) = deploy_syscall(
Executor::TEST_CLASS_HASH.try_into().unwrap(),
executor::TEST_CLASS_HASH.try_into().unwrap(),
0,
executor_constructor_calldata.span(),
false
Expand All @@ -204,7 +201,7 @@ fn spawn_empty_world() -> IWorldDispatcher {
let mut constructor_calldata = array::ArrayTrait::new();
constructor_calldata.append(executor_address.into());
let (world_address, _) = deploy_syscall(
World::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false
world::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false
)
.unwrap();
let world = IWorldDispatcher { contract_address: world_address };
Expand Down
26 changes: 11 additions & 15 deletions crates/dojo-core/tests/src/world_factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ use starknet::syscalls::deploy_syscall;
use starknet::get_caller_address;
use starknet::class_hash::ClassHash;
use starknet::class_hash::Felt252TryIntoClassHash;
use dojo::interfaces::IWorldFactoryDispatcher;
use dojo::interfaces::IWorldFactoryDispatcherTrait;
use dojo::interfaces::IWorldDispatcher;
use dojo::interfaces::IWorldDispatcherTrait;
use dojo::executor::Executor;
use dojo::world::World;
use dojo::world_factory::WorldFactory;

use dojo::executor::executor;
use dojo::world_factory::{IWorldFactoryDispatcher, IWorldFactoryDispatcherTrait, world_factory};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait, world};


#[derive(Component, Copy, Drop, Serde)]
struct Foo {
Expand All @@ -41,7 +39,7 @@ fn test_constructor() {
calldata.append(starknet::contract_address_const::<0x69>().into());

let (factory_address, _) = deploy_syscall(
WorldFactory::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
world_factory::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
)
.unwrap();

Expand All @@ -59,24 +57,24 @@ fn test_spawn_world() {
// Deploy Executor
let constructor_calldata = array::ArrayTrait::new();
let (executor_address, _) = deploy_syscall(
Executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false
executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false
)
.unwrap();

// WorldFactory constructor
let mut calldata: Array<felt252> = array::ArrayTrait::new();
calldata.append(World::TEST_CLASS_HASH);
calldata.append(world::TEST_CLASS_HASH);
calldata.append(executor_address.into());

let (factory_address, _) = deploy_syscall(
WorldFactory::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
world_factory::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
)
.unwrap();

let factory = IWorldFactoryDispatcher { contract_address: factory_address };

assert(factory.executor() == executor_address, 'wrong executor address');
assert(factory.world() == World::TEST_CLASS_HASH.try_into().unwrap(), 'wrong world class hash');
assert(factory.world() == world::TEST_CLASS_HASH.try_into().unwrap(), 'wrong world class hash');

// Prepare components and systems
let mut systems: Array<ClassHash> = array::ArrayTrait::new();
Expand All @@ -91,9 +89,7 @@ fn test_spawn_world() {

// Check Foo component and Bar system are registered
let foo_hash = world.component('Foo'.into());
assert(
foo_hash == foo::TEST_CLASS_HASH.try_into().unwrap(), 'component not registered'
);
assert(foo_hash == foo::TEST_CLASS_HASH.try_into().unwrap(), 'component not registered');

let bar_hash = world.system('bar'.into());
assert(bar_hash == bar::TEST_CLASS_HASH.try_into().unwrap(), 'system not registered');
Expand Down
Loading

0 comments on commit 7e5e358

Please sign in to comment.