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

fix(core): introduce DojoStore trait to handle some specific storage cases #3051

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3362741
fix: introduce DojoStore trait to handle some specific storage cases
remybar Feb 19, 2025
03c7524
fix fmt
remybar Feb 19, 2025
1944a5c
update spawn-and-moves example
remybar Feb 19, 2025
68a5ab8
add tuple management
remybar Feb 20, 2025
d37686f
point to remybar/scarb branch
remybar Feb 20, 2025
6bf0872
fix fmt
remybar Feb 20, 2025
c6eb661
update tuple management
remybar Feb 20, 2025
7ff6aa6
update tuple + generic management
remybar Feb 23, 2025
58e1df2
update core-cairo-test
remybar Feb 23, 2025
101dc58
manage legacy storage layout
remybar Feb 23, 2025
4b034a8
restore Cargo.toml to point to remybar/scarb branch
remybar Feb 23, 2025
81be5d0
add tests to improve coverage of layout write/read/delete operations
remybar Feb 23, 2025
5f20ea3
update torii test with mandatory trait for enums
remybar Feb 23, 2025
8afde13
add DojoStore tests
remybar Feb 23, 2025
fad069f
test LegacyModel/DojoStoreModel + some fixes
remybar Feb 24, 2025
ee548c5
update comment
remybar Feb 24, 2025
0777956
small improvement of model layout code generation
remybar Feb 26, 2025
f3c88ae
update test artifacts
remybar Feb 26, 2025
861909f
update scarb rev
remybar Feb 26, 2025
3ebd12b
add new use_legacy_storage() model function + update sozo model commands
remybar Mar 2, 2025
d0a3f2a
fixed layout models can be upgraded as soon as their layout remain th…
remybar Mar 2, 2025
77b91b7
update sozo model commands
remybar Mar 3, 2025
f6f6136
fail at compiled time if a Option<T> is used in a packed struct/enum
remybar Mar 3, 2025
88ad9c8
add use_legacy_model_storage() function into Torii to temporary make …
remybar Mar 3, 2025
35a26b8
fix fmt
remybar Mar 3, 2025
7026e34
rename use_legacy_model_storage() to use_legacy_storage() as it's onl…
remybar Mar 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 94 additions & 164 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,19 @@ rpassword = "7.2.0"
rstest = "0.18.2"
rstest_reuse = "0.6.0"
salsa = "0.16.1"
scarb = { git = "https://github.com/dojoengine/scarb", rev = "22aa38f53de205af25bf73b3ffc2c5334999d480" }
scarb-metadata = { git = "https://github.com/dojoengine/scarb", rev = "22aa38f53de205af25bf73b3ffc2c5334999d480" }
scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "22aa38f53de205af25bf73b3ffc2c5334999d480" }

#scarb = { git = "https://github.com/dojoengine/scarb", rev = "22aa38f53de205af25bf73b3ffc2c5334999d480" }
#scarb-metadata = { git = "https://github.com/dojoengine/scarb", rev = "22aa38f53de205af25bf73b3ffc2c5334999d480" }
#scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "22aa38f53de205af25bf73b3ffc2c5334999d480" }

scarb = { git = "https://github.com/remybar/scarb", rev = "fb905dd49264dcf5a38d09470529ef143f4a8762" }
scarb-metadata = { git = "https://github.com/remybar/scarb", rev = "fb905dd49264dcf5a38d09470529ef143f4a8762" }
scarb-ui = { git = "https://github.com/remybar/scarb", rev = "fb905dd49264dcf5a38d09470529ef143f4a8762" }

#scarb = { path = "/Users/remybaranx/pro/projets/contribs/dojo/scarb/scarb" }
#scarb-metadata = { path = "/Users/remybaranx/pro/projets/contribs/dojo/scarb/scarb-metadata" }
#scarb-ui = { path = "/Users/remybaranx/pro/projets/contribs/dojo/scarb/utils/scarb-ui" }

semver = "1.0.5"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = { version = "1.0", features = [ "arbitrary_precision" ] }
Expand Down
5 changes: 3 additions & 2 deletions crates/benches/contracts/src/models/character.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ struct Stats {
romances: u16,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde, Default)]
enum Weapon {
#[default]
DualWield: (Sword, Sword),
Fists: (Sword, Sword),
}

#[derive(Introspect, Copy, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde, Default)]
struct Sword {
swordsmith: ContractAddress,
damage: u32,
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo/core-cairo-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = 1

[[package]]
name = "dojo"
version = "1.2.0"
version = "1.2.2"
dependencies = [
"dojo_plugin",
]
Expand Down
3 changes: 3 additions & 0 deletions crates/dojo/core-cairo-test/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub use world::{
mod tests {
mod meta {
mod introspect;
mod layout;
}

mod event {
Expand All @@ -29,8 +30,10 @@ mod tests {

mod storage {
mod database;
mod dojo_store;
mod packing;
mod storage;
mod layout;
}

mod contract;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,9 @@ pub mod attacker_model {
unpacked_size: Self::unpacked_size(self),
}
}

fn use_legacy_storage(self: @ContractState) -> bool {
false
}
}
}
8 changes: 5 additions & 3 deletions crates/dojo/core-cairo-test/src/tests/helpers/event.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::world::{spawn_test_world, NamespaceDef, TestResource};
/// These event contracts are used to test event upgrades in tests/event.cairo.

// This event is used as a base to create the "previous" version of an event to be upgraded.
#[derive(Introspect)]
#[derive(Introspect, Serde)]
struct FooBaseEvent {
#[key]
pub caller: ContractAddress,
Expand Down Expand Up @@ -63,8 +63,9 @@ struct FooEventMemberAdded {
pub b: u128,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde, Default)]
enum MyEnum {
#[default]
X: u8,
}

Expand All @@ -77,8 +78,9 @@ struct FooEventMemberChanged {
pub b: u128,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde, Default)]
enum AnotherEnum {
#[default]
X: u8,
}

Expand Down
13 changes: 10 additions & 3 deletions crates/dojo/core-cairo-test/src/tests/helpers/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use starknet::{ContractAddress};

use dojo::world::{IWorldDispatcher, WorldStorage, WorldStorageTrait};
use dojo::model::Model;
use dojo::storage::ContractAddressDefault;

use crate::world::{
spawn_test_world, NamespaceDef, TestResource, ContractDefTrait, WorldStorageTestTrait,
Expand Down Expand Up @@ -35,9 +36,10 @@ pub struct NotCopiable {
pub b: ByteArray,
}

#[derive(Drop, Serde, Debug, PartialEq, Introspect)]
#[derive(Drop, Serde, Debug, PartialEq, Introspect, Default)]
pub enum EnumOne {
One,
#[default]
Two: u32,
Three: (felt252, u32),
}
Expand Down Expand Up @@ -95,6 +97,10 @@ pub mod foo_invalid_name {
unpacked_size: Self::unpacked_size(self),
}
}

fn use_legacy_storage(self: @ContractState) -> bool {
false
}
}
}

Expand Down Expand Up @@ -127,7 +133,7 @@ pub mod test_contract_with_dojo_init_args {
}
}

#[derive(IntrospectPacked, Copy, Drop, Serde)]
#[derive(IntrospectPacked, Copy, Drop, Serde, Default)]
pub struct Sword {
pub swordsmith: ContractAddress,
pub damage: u32,
Expand Down Expand Up @@ -177,8 +183,9 @@ pub struct Stats {
pub romances: u16,
}

#[derive(IntrospectPacked, Copy, Drop, Serde)]
#[derive(IntrospectPacked, Copy, Drop, Serde, Default)]
pub enum Weapon {
#[default]
DualWield: (Sword, Sword),
Fists: (Sword, Sword),
}
Expand Down
6 changes: 4 additions & 2 deletions crates/dojo/core-cairo-test/src/tests/helpers/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ struct FooModelMemberAdded {
pub b: u128,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde, Default)]
enum MyEnum {
#[default]
X: u8,
}

Expand All @@ -69,8 +70,9 @@ struct FooModelMemberChanged {
pub b: u128,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde, Default)]
enum AnotherEnum {
#[default]
X: u8,
}

Expand Down
57 changes: 37 additions & 20 deletions crates/dojo/core-cairo-test/src/tests/meta/introspect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,116 +2,129 @@ use dojo::meta::introspect::{Introspect, Struct, Enum, Member, Ty, TyCompareTrai
use dojo::meta::{Layout, FieldLayout};
use crate::utils::GasCounterTrait;

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde, Default)]
struct Base {
value: u32,
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde)]
struct WithArray {
value: u32,
arr: Array<u8>,
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde)]
struct WithByteArray {
value: u32,
arr: ByteArray,
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde)]
struct WithTuple {
value: u32,
arr: (u8, u16, u32),
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde)]
struct WithNestedTuple {
value: u32,
arr: (u8, (u16, u128, u256), u32),
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde)]
struct WithNestedArrayInTuple {
value: u32,
arr: (u8, (u16, Array<u128>, u256), u32),
}

#[derive(Drop, IntrospectPacked)]
#[derive(Drop, IntrospectPacked, Serde, Default)]
struct Vec3 {
x: u32,
y: u32,
z: u32,
}

#[derive(IntrospectPacked)]
#[derive(IntrospectPacked, Serde)]
struct Translation {
from: Vec3,
to: Vec3,
}

#[derive(Drop, IntrospectPacked)]
#[derive(Drop, IntrospectPacked, Serde)]
struct StructInnerNotPacked {
x: Base,
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde, Default)]
enum EnumNoData {
#[default]
One,
Two,
Three,
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde, Default)]
enum EnumWithSameData {
#[default]
One: u256,
Two: u256,
Three: u256,
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde, Default)]
enum EnumWithSameTupleData {
#[default]
One: (u256, u32),
Two: (u256, u32),
Three: (u256, u32),
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde, Default)]
enum EnumWithVariousData {
#[default]
One: u32,
Two: (u8, u16),
Three: Array<u128>,
}


#[derive(Drop, IntrospectPacked)]
#[derive(Drop, IntrospectPacked, Serde, Default)]
enum EnumPacked {
#[default]
A: u32,
B: u32,
}

#[derive(Drop, IntrospectPacked)]
#[derive(Drop, IntrospectPacked, Serde, Default)]
enum EnumInnerPacked {
#[default]
A: (EnumPacked, Vec3),
B: (EnumPacked, Vec3),
}

#[derive(Drop, IntrospectPacked)]
#[derive(Drop, IntrospectPacked, Serde, Default)]
enum EnumInnerNotPacked {
#[default]
A: (EnumPacked, Base),
B: (EnumPacked, Base),
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde)]
struct StructWithOption {
x: Option<u16>,
}

#[derive(Drop, Introspect)]
#[derive(Drop, Introspect, Serde)]
struct Generic<T> {
value: T,
}

#[derive(Drop, Introspect, Serde, Default)]
enum GenericEnum<T> {
#[default]
A: T,
}

fn field(selector: felt252, layout: Layout) -> FieldLayout {
FieldLayout { selector, layout }
}
Expand All @@ -134,9 +147,13 @@ fn _enum(values: Array<Option<Layout>>) -> Layout {
}

let v = *values.at(i);

// in the new Dojo storage layout, variants start from 1.
let variant = i + 1;

match v {
Option::Some(v) => { items.append(field(i.into(), v)); },
Option::None => { items.append(field(i.into(), fixed(array![]))) },
Option::Some(v) => { items.append(field(variant.into(), v)); },
Option::None => { items.append(field(variant.into(), fixed(array![]))) },
}

i += 1;
Expand Down
Loading
Loading