Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage layout #1772

Closed
wants to merge 11 commits into from
59 changes: 39 additions & 20 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in 'dojo-world'",
"cargo": {
"args": ["test", "--no-run", "--package=dojo-world", "--lib"],
"filter": {
"name": "dojo-world",
"kind": "lib"
}
},
"args": ["migration::compile_moves"],
"cwd": "${workspaceFolder}/crates/dojo-world"
}
]
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Dojo Core test",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/target/debug/sozo",
"args": ["--manifest-path", "crates/dojo-core/Scarb.toml", "test"]
},
{
"name": "Sozo Migrate",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/target/debug/sozo",
"args": [
"--manifest-path",
"examples/spawn-and-move/Scarb.toml",
"migrate",
"apply"
]
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in 'dojo-world'",
"cargo": {
"args": ["test", "--no-run", "--package=dojo-world", "--lib"],
"filter": {
"name": "dojo-world",
"kind": "lib"
}
},
"args": ["migration::compile_moves"],
"cwd": "${workspaceFolder}/crates/dojo-world"
}
]
}
2 changes: 2 additions & 0 deletions bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ mod tests {

use super::{create_stats_table, BuildArgs};

// Uncomment once bindings support arrays.
#[ignore]
#[test]
fn build_example_with_typescript_and_unity_bindings() {
let config = build_test_config("../../examples/spawn-and-move/Scarb.toml").unwrap();
Expand Down
9 changes: 6 additions & 3 deletions crates/benches/contracts/src/models/character.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use starknet::{ContractAddress, get_caller_address};

// TODO import all this when complex benchmarks are merged
#[derive(Model, Copy, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::model]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my original PR I removed auto deriving Introspect for models but I think we should probably add it back. Since there I don't think there is a case where we want model without introspect. Not necessary to land in this PR though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree 👍

struct Character {
#[key]
caller: ContractAddress,
Expand Down Expand Up @@ -47,15 +48,17 @@ struct Sword {
damage: u32,
}

#[derive(Model, Copy, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::model]
struct Case {
#[key]
owner: ContractAddress,
sword: Sword,
material: felt252,
}

#[derive(Model, Copy, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::model]
struct Alias {
#[key]
player: ContractAddress,
Expand Down
3 changes: 2 additions & 1 deletion crates/benches/contracts/src/models/moves.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use starknet::ContractAddress;

#[derive(Model, Drop, Serde)]
#[derive(Introspect, Drop, Serde)]
#[dojo::model]
struct Moves {
#[key]
player: ContractAddress,
Expand Down
3 changes: 2 additions & 1 deletion crates/benches/contracts/src/models/position.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use starknet::ContractAddress;

#[derive(Model, Copy, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::model]
struct Position {
#[key]
player: ContractAddress,
Expand Down
6 changes: 1 addition & 5 deletions crates/dojo-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ mod tests {
)
.unwrap();

assert_eq!(data.models.len(), 4);
assert_eq!(data.models.len(), 3);

assert_eq!(data.world.name, "dojo_example");

Expand All @@ -273,10 +273,6 @@ mod tests {
assert_eq!(moves.name, "Moves");
assert_eq!(moves.qualified_path, "dojo_examples::models::Moves");

let moved = data.models.get("Moved").unwrap();
assert_eq!(moved.name, "Moved");
assert_eq!(moved.qualified_path, "dojo_examples::actions::actions::Moved");

let moved = data.models.get("EmoteMessage").unwrap();
assert_eq!(moved.name, "EmoteMessage");
assert_eq!(moved.qualified_path, "dojo_examples::models::EmoteMessage");
Expand Down
2 changes: 2 additions & 0 deletions crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,8 @@ mod tests {
use super::*;
use crate::gather_dojo_data;

// Uncomment once bindings support arrays.
#[ignore]
#[test]
fn test_output() {
let mut expected_output = String::new();
Expand Down
68 changes: 57 additions & 11 deletions crates/dojo-core/src/base_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,46 @@ fn test_upgrade_direct() {
}

#[starknet::interface]
trait INameOnly<T> {
fn name(self: @T) -> felt252;
trait IMetadataOnly<T> {
fn selector(self: @T) -> felt252;
fn name(self: @T) -> ByteArray;
}

#[starknet::contract]
mod invalid_legacy_model {
#[storage]
struct Storage {}

#[abi(embed_v0)]
impl InvalidModelMetadata of super::IMetadataOnly<ContractState> {
fn selector(self: @ContractState) -> felt252 {
// Pre-computed address of a contract deployed through the world.
0x742c3d09472a40914dedcbd609788fd547bde613d6c4d4c2f15d41f4e241f25
}

fn name(self: @ContractState) -> ByteArray {
"invalid_legacy_model"
}
}
}

#[starknet::contract]
mod invalid_legacy_model_world {
#[storage]
struct Storage {}

#[abi(embed_v0)]
impl InvalidModelName of super::IMetadataOnly<ContractState> {
fn selector(self: @ContractState) -> felt252 {
// World address is 0, and not registered as deployed through the world
// as it's itself.
0
}

fn name(self: @ContractState) -> ByteArray {
"invalid_legacy_model"
}
}
}

#[starknet::contract]
Expand All @@ -104,12 +142,14 @@ mod invalid_model {
struct Storage {}

#[abi(embed_v0)]
impl InvalidModelName of super::INameOnly<ContractState> {
fn name(self: @ContractState) -> felt252 {
impl InvalidModelSelector of super::IMetadataOnly<ContractState> {
fn selector(self: @ContractState) -> felt252 {
// Pre-computed address of a contract deployed through the world.
// To print this addres, run:
// sozo test --manifest-path crates/dojo-core/Scarb.toml -f test_deploy_from_world_invalid_model
0x647d90f9663c37478a5fba689fc7166d957f782ea4a8316e0042929d48cf8be
0x7dd4efcdb43134973afac995519471062092df24e91107a2f8faef75daa2cd0
}

fn name(self: @ContractState) -> ByteArray {
"invalid_model"
}
}
}
Expand All @@ -120,12 +160,16 @@ mod invalid_model_world {
struct Storage {}

#[abi(embed_v0)]
impl InvalidModelName of super::INameOnly<ContractState> {
fn name(self: @ContractState) -> felt252 {
impl InvalidModelSelector of super::IMetadataOnly<ContractState> {
fn selector(self: @ContractState) -> felt252 {
// World address is 0, and not registered as deployed through the world
// as it's itself.
0
}

fn name(self: @ContractState) -> ByteArray {
"invalid_model_world"
}
}
}

Expand All @@ -136,8 +180,10 @@ fn test_deploy_from_world_invalid_model() {
let world = deploy_world();

let base_address = world.deploy_contract(0, base::TEST_CLASS_HASH.try_into().unwrap());
// The print is required for invalid_model name to be a valid address as the
// register_model will use the gas consumed as salt.

// This print allows to know the address of the deployed contract which must be returned
// by the selector() function of invalid model, to simulate a ACL issue
// (see register_model function)
base_address.print();

world.register_model(invalid_model::TEST_CLASS_HASH.try_into().unwrap());
Expand Down
Loading