Skip to content

Commit

Permalink
fix(dojo-lang): remove restriction for u256 as keys in models (#2890)
Browse files Browse the repository at this point in the history
* Added dojo support for u256 as keys in models

* fix: lint and update versions for test crates

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
bengineer42 and glihm authored Jan 16, 2025
1 parent 792b3c9 commit e57d820
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 44 deletions.
4 changes: 2 additions & 2 deletions crates/dojo/core-cairo-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ version = 1

[[package]]
name = "dojo"
version = "1.0.10"
version = "1.0.11"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_cairo_test"
version = "1.0.0-rc.0"
version = "1.0.11"
dependencies = [
"dojo",
]
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo/core-cairo-test/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cairo-version = "=2.8.4"
edition = "2024_07"
description = "Testing library for Dojo using Cairo test runner."
name = "dojo_cairo_test"
version = "1.0.0-rc.0"
version = "1.0.11"

[dependencies]
starknet = "=2.8.4"
Expand Down
11 changes: 11 additions & 0 deletions crates/dojo/core-cairo-test/src/tests/model/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ struct Foo2 {
v2: u32
}

#[derive(Copy, Drop, Serde, Debug)]
#[dojo::model]
struct Foo3 {
#[key]
k1: u256,
#[key]
k2: felt252,
v1: u128,
v2: u32
}

fn namespace_def() -> NamespaceDef {
NamespaceDef {
namespace: "dojo_cairo_test", resources: [
Expand Down
40 changes: 6 additions & 34 deletions crates/dojo/lang/src/attribute_macros/element.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use cairo_lang_defs::patcher::RewriteNode;
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_diagnostics::Severity;
use cairo_lang_syntax::node::ast::Member as MemberAst;
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::helpers::QueryAttrs;
use cairo_lang_syntax::node::{Terminal, TypedStablePtr, TypedSyntaxNode};
use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode};
use dojo_types::naming::compute_bytearray_hash;
use starknet_crypto::{poseidon_hash_many, Felt};

Expand Down Expand Up @@ -36,39 +34,13 @@ pub fn compute_unique_hash(
poseidon_hash_many(&hashes)
}

pub fn parse_members(
db: &dyn SyntaxGroup,
members: &[MemberAst],
diagnostics: &mut Vec<PluginDiagnostic>,
) -> Vec<Member> {
pub fn parse_members(db: &dyn SyntaxGroup, members: &[MemberAst]) -> Vec<Member> {
members
.iter()
.filter_map(|member_ast| {
let member = Member {
name: member_ast.name(db).text(db).to_string(),
ty: member_ast
.type_clause(db)
.ty(db)
.as_syntax_node()
.get_text(db)
.trim()
.to_string(),
key: member_ast.has_attr(db, "key"),
};

// validate key member
if member.key && member.ty == "u256" {
diagnostics.push(PluginDiagnostic {
message: "Key is only supported for core types that are 1 felt long once \
serialized. `u256` is a struct of 2 u128, hence not supported."
.into(),
stable_ptr: member_ast.name(db).stable_ptr().untyped(),
severity: Severity::Error,
});
None
} else {
Some(member)
}
.map(|member_ast| Member {
name: member_ast.name(db).text(db).to_string(),
ty: member_ast.type_clause(db).ty(db).as_syntax_node().get_text(db).trim().to_string(),
key: member_ast.has_attr(db, "key"),
})
.collect::<Vec<_>>()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo/lang/src/attribute_macros/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl DojoEvent {
}
}

let members = parse_members(db, &struct_ast.members(db).elements(db), &mut diagnostics);
let members = parse_members(db, &struct_ast.members(db).elements(db));

let mut serialized_keys: Vec<RewriteNode> = vec![];
let mut serialized_values: Vec<RewriteNode> = vec![];
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo/lang/src/attribute_macros/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl DojoModel {
let mut model_member_store_impls_processed: HashSet<String> = HashSet::new();
let mut model_member_store_impls: Vec<String> = vec![];

let members = parse_members(db, &struct_ast.members(db).elements(db), &mut diagnostics);
let members = parse_members(db, &struct_ast.members(db).elements(db));

members.iter().for_each(|member| {
if member.key {
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/types-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ version = "2.8.4"

[[package]]
name = "types_test"
version = "1.0.9"
version = "1.0.11"
dependencies = [
"dojo",
]
4 changes: 2 additions & 2 deletions examples/simple/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ version = 1

[[package]]
name = "dojo"
version = "1.0.9"
version = "1.0.11"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_cairo_test"
version = "1.0.0-rc.0"
version = "1.0.11"
dependencies = [
"dojo",
]
Expand Down
4 changes: 2 additions & 2 deletions examples/spawn-and-move/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ dependencies = [

[[package]]
name = "dojo_cairo_test"
version = "1.0.0-rc.0"
version = "1.0.11"
dependencies = [
"dojo",
]

[[package]]
name = "dojo_examples"
version = "1.0.9"
version = "1.0.11"
dependencies = [
"armory",
"bestiary",
Expand Down
1 change: 1 addition & 0 deletions scripts/rebuild_test_artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cargo +nightly-2024-08-28 fmt --all -- "$@"
./target/release/sozo build --manifest-path examples/spawn-and-move/Scarb.toml
./target/release/sozo build --manifest-path examples/spawn-and-move/Scarb.toml -P release
./target/release/sozo build --manifest-path crates/torii/types-test/Scarb.toml
./target/release/sozo test --manifest-path crates/dojo/core-cairo-test/Scarb.toml

# Generates the database for testing by migrating the spawn and move example.
KATANA_RUNNER_BIN=./target/release/katana cargo generate-test-db
Expand Down
Binary file modified spawn-and-move-db.tar.gz
Binary file not shown.
Binary file modified types-test-db.tar.gz
Binary file not shown.

0 comments on commit e57d820

Please sign in to comment.