Skip to content

Commit

Permalink
Add executor tests (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev authored Apr 25, 2023
1 parent 1f61719 commit 30832c6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
49 changes: 46 additions & 3 deletions crates/dojo-core/src/executor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ mod Executor {
use dojo_core::serde::SpanSerde;

const EXECUTE_ENTRYPOINT: felt252 =
0x240060cdb34fcc260f41eac7474ee1d7c80b7e3607daff9ac67c7ea2ebb1c44;
0x0240060cdb34fcc260f41eac7474ee1d7c80b7e3607daff9ac67c7ea2ebb1c44;

#[external]
#[raw_output]
fn execute(class_hash: starknet::ClassHash, mut calldata: Span<felt252>, ) -> Span<felt252> {
fn execute(class_hash: starknet::ClassHash, calldata: Span<felt252>) -> Span<felt252> {
let world_address = starknet::get_caller_address();

let mut calldata_arr = calldata.snapshot.clone();
Expand All @@ -25,3 +24,47 @@ mod Executor {
res
}
}

mod tests {
use core::traits::Into;
use core::result::ResultTrait;
use array::ArrayTrait;
use option::OptionTrait;
use traits::TryInto;

use starknet::syscalls::deploy_syscall;
use starknet::class_hash::Felt252TryIntoClassHash;
use dojo_core::interfaces::IExecutorDispatcher;
use dojo_core::interfaces::IExecutorDispatcherTrait;

#[derive(Component)]
struct Foo {
a: felt252,
b: u128,
}

#[system]
mod Bar {
use super::Foo;

fn execute(foo: Foo) -> Foo {
foo
}
}

#[test]
#[available_gas(30000000)]
fn test_executor() {
let constructor_calldata = array::ArrayTrait::<felt252>::new();
let (executor_address, _) = deploy_syscall(
super::Executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false
).unwrap();

let executor = IExecutorDispatcher { contract_address: executor_address };

let mut system_calldata = ArrayTrait::new();
system_calldata.append(42);
system_calldata.append(53);
let res = executor.execute(BarSystem::TEST_CLASS_HASH.try_into().unwrap(), system_calldata.span());
}
}
39 changes: 27 additions & 12 deletions crates/dojo-core/src/serde.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,34 @@ use serde::Serde;

impl SpanSerde of Serde::<Span<felt252>> {
fn serialize(ref output: Array<felt252>, mut input: Span<felt252>) {
loop {
gas::withdraw_gas().expect('Out of gas');
match input.pop_front() {
Option::Some(v) => {
output.append(*v);
},
Option::None(_) => {
break ();
},
};
};
Serde::<usize>::serialize(ref output, input.len());
serialize_span_helper(ref output, input);
}
fn deserialize(ref serialized: Span<felt252>) -> Option<Span<felt252>> {
Option::Some(serialized)
let length = *serialized.pop_front()?;
let mut arr = ArrayTrait::new();
deserialize_array_helper(ref serialized, arr, length)
}
}

fn serialize_span_helper(
ref output: Array<felt252>, mut input: Span<felt252>
) {
match input.pop_front() {
Option::Some(v) => {
output.append(*v);
serialize_span_helper(ref output, input);
},
Option::None(_) => {},
}
}

fn deserialize_array_helper(
ref serialized: Span<felt252>, mut curr_output: Array<felt252>, remaining: felt252
) -> Option<Span<felt252>> {
if remaining == 0 {
return Option::Some(curr_output.span());
}
curr_output.append(*serialized.pop_front()?);
deserialize_array_helper(ref serialized, curr_output, remaining - 1)
}
1 change: 0 additions & 1 deletion crates/dojo-core/src/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod World {
use starknet::ClassHash;
use starknet::ContractAddress;

use dojo_core::serde::SpanSerde;
use dojo_core::storage::query::Query;
use dojo_core::storage::query::QueryTrait;
use dojo_core::storage::db::Database;
Expand Down

0 comments on commit 30832c6

Please sign in to comment.