Skip to content

Commit

Permalink
fix(dojo-core): disable use of capacity derive on model
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Feb 14, 2024
1 parent 6619d6c commit 589c9a4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 303 deletions.
7 changes: 2 additions & 5 deletions bin/sozo/src/ops/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,8 @@ where
None => {}
};

// We don't declare and register the model for Resource Metadata for two reasons:
// 1. Having the ModelRegistered event is occupying space for data that are redundant with
// `MetadataUpdate` event.
// 2. This model is never used from outside world queries, only to set metadata. It should then
// remain internal.
// Once Torii supports indexing arrays, we should declare and register the
// ResourceMetadata model.

register_models(strategy, migrator, &ui, txn_config.clone()).await?;
deploy_contracts(strategy, migrator, &ui, txn_config).await?;
Expand Down
6 changes: 0 additions & 6 deletions crates/dojo-core/src/database/introspect_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ struct Generic<T> {
value: T,
}

#[derive(Drop, Introspect)]
struct FeltsArray {
#[capacity(10)]
felts: Array<felt252>,
}

#[test]
#[available_gas(2000000)]
fn test_generic_introspect() {
Expand Down
24 changes: 0 additions & 24 deletions crates/dojo-core/src/world_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -585,27 +585,3 @@ fn test_upgradeable_world_from_non_owner() {
};
upgradeable_world_dispatcher.upgrade(worldupgrade::TEST_CLASS_HASH.try_into().unwrap());
}

#[derive(Model, Drop, Serde)]
struct ArrayModel {
#[key]
id: felt252,
#[capacity(3)]
values: Array<felt252>,
}

#[test]
#[available_gas(6000000)]
fn test_array_model() {
let world = deploy_world();

set!(world, ArrayModel { id: 0xaa, values: array_cap!(3, (0x1, 0xf2)) });
let stored: ArrayModel = get!(world, 0xaa, ArrayModel);

assert(stored.id == 0xaa, 'invalid id');
assert(stored.values.len() == 3, 'invalid capacity');
assert((*stored.values[0]) == 1, 'invalid 0');
assert((*stored.values[1]) == 0xf2, 'invalid 1');
assert((*stored.values[2]) == 0, 'invalid 2');
}

5 changes: 4 additions & 1 deletion crates/dojo-lang/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ impl MacroPlugin for BuiltinDojoPlugin {
"dojo::contract".to_string(),
"key".to_string(),
"computed".to_string(),
"capacity".to_string(),

// Not adding capacity for now, this will automatically
// makes Scarb emitting a diagnostic saying this attribute is not supported.
// "capacity".to_string(),
]
}
}
Expand Down
15 changes: 15 additions & 0 deletions crates/dojo-lang/src/plugin_test_data/introspect
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,27 @@ impl FeltsArrayBadCapacityIntrospect<T, impl TIntrospect: dojo::database::intros
}

//! > expected_diagnostics
error: Unsupported attribute.
--> test_src/lib.cairo:49:5
#[capacity(10)]
^*************^

error: Capacity is only supported for Array<felt252> or Span<felt252>.
--> test_src/lib.cairo:55:5
#[capacity(10)]
^*************^

error: Unsupported attribute.
--> test_src/lib.cairo:55:5
#[capacity(10)]
^*************^

error: Capacity must be greater than 0.
--> test_src/lib.cairo:61:5
#[capacity(0)]
^************^

error: Unsupported attribute.
--> test_src/lib.cairo:61:5
#[capacity(0)]
^************^
172 changes: 0 additions & 172 deletions crates/dojo-lang/src/plugin_test_data/model
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ struct Player {
name: felt252,
}

#[derive(Model, Serde)]
struct ModelWithArray {
#[key]
id: felt252,
#[capacity(10)]
values: Array<felt252>,
}

//! > generated_cairo_code
use core::serde::Serde;

Expand Down Expand Up @@ -564,11 +556,6 @@ error: Unsupported attribute.
#[starknet::contract]
^*******************^

error: Unsupported attribute.
--> test_src/lib.cairo[ModelWithArray]:83:13
#[starknet::contract]
^*******************^

error: Unsupported attribute.
--> test_src/lib.cairo[Position]:78:17
#[storage]
Expand Down Expand Up @@ -629,21 +616,6 @@ error: Unsupported attribute.
#[abi(embed_v0)]
^**************^

error: Unsupported attribute.
--> test_src/lib.cairo[ModelWithArray]:88:17
#[storage]
^********^

error: Unsupported attribute.
--> test_src/lib.cairo[ModelWithArray]:91:17
#[abi(embed_v0)]
^**************^

error: Unsupported attribute.
--> test_src/lib.cairo[ModelWithArray]:119:17
#[abi(embed_v0)]
^**************^

//! > expanded_cairo_code
use core::serde::Serde;

Expand Down Expand Up @@ -701,14 +673,6 @@ struct Player {

name: felt252,
}

#[derive(Model, Serde)]
struct ModelWithArray {
#[key]
id: felt252,
#[capacity(10)]
values: Array<felt252>,
}
impl Vec3Copy of core::traits::Copy::<Vec3>;
impl Vec3Drop of core::traits::Drop::<Vec3>;
impl Vec3Serde of core::serde::Serde::<Vec3> {
Expand Down Expand Up @@ -1262,139 +1226,3 @@ impl PlayerIntrospect<> of dojo::database::introspect::Introspect<Player<>> {
}
}
}
impl ModelWithArraySerde of core::serde::Serde::<ModelWithArray> {
fn serialize(self: @ModelWithArray, ref output: core::array::Array<felt252>) {
core::serde::Serde::serialize(self.id, ref output);
core::serde::Serde::serialize(self.values, ref output)
}
fn deserialize(ref serialized: core::array::Span<felt252>) -> core::option::Option<ModelWithArray> {
core::option::Option::Some(ModelWithArray {
id: core::serde::Serde::deserialize(ref serialized)?,
values: core::serde::Serde::deserialize(ref serialized)?,
})
}
}

impl ModelWithArrayModel of dojo::model::Model<ModelWithArray> {
#[inline(always)]
fn name(self: @ModelWithArray) -> felt252 {
'ModelWithArray'
}

#[inline(always)]
fn keys(self: @ModelWithArray) -> Span<felt252> {
let mut serialized = core::array::ArrayTrait::new();
core::array::ArrayTrait::append(ref serialized, *self.id);
core::array::ArrayTrait::span(@serialized)
}

#[inline(always)]
fn values(self: @ModelWithArray) -> Span<felt252> {
let mut serialized = core::array::ArrayTrait::new();
core::serde::Serde::serialize(self.values, ref serialized);
core::array::ArrayTrait::span(@serialized)
}

#[inline(always)]
fn layout(self: @ModelWithArray) -> Span<u8> {
let mut layout = core::array::ArrayTrait::new();
dojo::database::introspect::Introspect::<ModelWithArray>::layout(ref layout);
core::array::ArrayTrait::span(@layout)
}

#[inline(always)]
fn packed_size(self: @ModelWithArray) -> usize {
let mut layout = self.layout();
dojo::packing::calculate_packed_size(ref layout)
}
}


impl ModelWithArrayIntrospect<> of dojo::database::introspect::Introspect<ModelWithArray<>> {
#[inline(always)]
fn size() -> usize {
1 + 10 + 0
}

#[inline(always)]
fn layout(ref layout: Array<u8>) {
layout.append(251);
layout.append(251);
layout.append(251);
layout.append(251);
layout.append(251);
layout.append(251);
layout.append(251);
layout.append(251);
layout.append(251);
layout.append(251);
layout.append(251);

}

#[inline(always)]
fn ty() -> dojo::database::introspect::Ty {
dojo::database::introspect::Ty::Struct(dojo::database::introspect::Struct {
name: 'ModelWithArray',
attrs: array![].span(),
children: array![dojo::database::introspect::serialize_member(@dojo::database::introspect::Member {
name: 'id',
ty: dojo::database::introspect::Ty::Primitive('felt252'),
attrs: array!['key'].span()
}), dojo::database::introspect::serialize_member(@dojo::database::introspect::Member {
name: 'values',
ty: dojo::database::introspect::Ty::Array(10),
attrs: array![].span()
})].span()
})
}
}


#[starknet::interface]
trait Imodel_with_array<T> {
fn ensure_abi(self: @T, model: ModelWithArray);
}

#[starknet::contract]
mod model_with_array {
use super::ModelWithArray;
use super::Imodel_with_array;

#[storage]
struct Storage {}

#[abi(embed_v0)]
impl DojoModelImpl of dojo::model::IDojoModel<ContractState>{
fn name(self: @ContractState) -> felt252 {
'ModelWithArray'
}

fn unpacked_size(self: @ContractState) -> usize {
dojo::database::introspect::Introspect::<ModelWithArray>::size()
}

fn packed_size(self: @ContractState) -> usize {
let mut layout = core::array::ArrayTrait::new();
dojo::database::introspect::Introspect::<ModelWithArray>::layout(ref layout);
let mut layout_span = layout.span();
dojo::packing::calculate_packed_size(ref layout_span)
}

fn layout(self: @ContractState) -> Span<u8> {
let mut layout = core::array::ArrayTrait::new();
dojo::database::introspect::Introspect::<ModelWithArray>::layout(ref layout);
core::array::ArrayTrait::span(@layout)
}

fn schema(self: @ContractState) -> dojo::database::introspect::Ty {
dojo::database::introspect::Introspect::<ModelWithArray>::ty()
}
}

#[abi(embed_v0)]
impl model_with_arrayImpl of Imodel_with_array<ContractState>{
fn ensure_abi(self: @ContractState, model: ModelWithArray) {
}
}
}
Loading

0 comments on commit 589c9a4

Please sign in to comment.