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

feat(lang): add the ability to get a WorldStorage from a namespace hash #2686

Open
wants to merge 148 commits into
base: feat/poseidon-macro
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
148 commits
Select commit Hold shift + click to select a range
b1f3532
added poseidon_hash_string macro
bengineer42 Nov 14, 2024
5a9fa53
working
bengineer42 Nov 14, 2024
adc5566
added tests and world function
bengineer42 Nov 14, 2024
a4c9109
fmt
bengineer42 Nov 14, 2024
a897f4c
ran tests gen
bengineer42 Nov 14, 2024
21a19b7
updated format to macth convention
bengineer42 Nov 14, 2024
3b772ce
Added function for dns from hash
bengineer42 Nov 21, 2024
ef21c50
Merge branch 'main' into storage-from-hash
bengineer42 Nov 21, 2024
8a1f9d9
added resource from selector
bengineer42 Nov 21, 2024
f44886d
feat: add i128 + use BigNumberish type (#2705)
MartianGreed Nov 21, 2024
374461d
fix(torii): graphql playground URL (#2707)
glihm Nov 21, 2024
b55f1fc
refactor(torii-server): server proxy handlers (#2708)
Larkooo Nov 22, 2024
c5a4eaf
refactor(katana): rpc class types improvements (#2712)
kariy Nov 22, 2024
da52a06
feat(torii): sql playground (#2714)
Larkooo Nov 22, 2024
d7930e6
fix: ensure the manifest existence is correctly tested (#2713)
glihm Nov 25, 2024
8eb8c48
Prepare release: v1.0.2 (#2722)
tarrencev Nov 26, 2024
cc82661
Update devcontainer image: v1.0.2 (#2723)
tarrencev Nov 26, 2024
1aabd5f
fix: all bindgen functionns external instead of view (#2725)
MartianGreed Nov 26, 2024
3283ecb
[Fix] Couldn't connect to Torii on IOS/Android (#2726)
caillef Nov 26, 2024
339e9c1
fix(chore): update Cargo.lock
glihm Nov 26, 2024
ebcc23d
ci: update rust toolchain and conventional commit format (#2727)
glihm Nov 26, 2024
edaa6a2
feat(torii-core): json value for ty for efficient json ser (#2730)
Larkooo Nov 28, 2024
fe5c48e
feat(sozo): upload world/models/contracts metadata only if changed (#…
remybar Nov 28, 2024
c65c257
Prepare release: v1.0.3 (#2735)
tarrencev Nov 29, 2024
cd80507
chore(devcontainer): update image: v1.0.3 (#2736)
tarrencev Nov 29, 2024
0fe329a
fix: fix graphql playground URL (#2737)
glihm Nov 29, 2024
08a52c7
fix: remove unused file
glihm Nov 29, 2024
1aab149
refactor(torii-core): add provider health check (#2729)
847850277 Nov 29, 2024
3774731
feat(katana): sequencer feeder gateway client (#2732)
kariy Nov 30, 2024
e191569
refactor(katana): dont run sequencing task inside of pipeline (#2740)
kariy Nov 30, 2024
0f72d24
feat(katana): pipeline execution loop with checkpointing (#2741)
kariy Nov 30, 2024
3ba53c8
refactor(katana): include config in the node struct (#2742)
kariy Nov 30, 2024
a0a173e
feat(katana): implement more feeder gateway types (#2744)
kariy Dec 2, 2024
6644564
fix(torii): avoid panic on fetch token URI fail (#2750)
glihm Dec 2, 2024
cd17676
fix(torii): handle case where token_id no longer exists and few graph…
lambda-0x Nov 28, 2024
938d50f
fix(torii): add erc20 patch for eth mainnet token
lambda-0x Nov 28, 2024
859c9d9
fix(torii/graphql): wait to query db after getting broker update
lambda-0x Dec 3, 2024
56c0bf7
refactor(torii-graphql): move away from external url (#2753)
Larkooo Dec 3, 2024
d0722c4
opt-refactor(torii-core): complete sql schema refactor (#2718)
Larkooo Dec 3, 2024
ef343bb
fix(torii): handle failed metadata fetch and empty metadata in db
lambda-0x Dec 3, 2024
2026f58
chore: remove redundant words in comment (#2752)
rusttech Dec 3, 2024
2dd561a
release: v1.0.4 (#2756)
tarrencev Dec 3, 2024
e9f286c
fix: remove dbg (#2757)
glihm Dec 3, 2024
75c43a8
chore(devcontainer): update image: v1.0.4 (#2758)
tarrencev Dec 3, 2024
9d57b4b
refactor(katana-rpc): remove some components dependency from starknet…
kariy Dec 3, 2024
9aa5b10
refactor(katana): fix feeder gateway types (#2760)
kariy Dec 4, 2024
68999b2
docs: fix discord invite link (#2761)
Ocheretovich Dec 4, 2024
327e653
feat(katana): experimental full node (#2724)
kariy Dec 4, 2024
ffc9259
opt(torii/core): add explicit limit to sql query
lambda-0x Dec 4, 2024
0cb5923
ci(workflow): handle missing cache directories gracefully
steebchen Dec 5, 2024
7179919
refactor(torii-args): bool values no need for explicit true (#2767)
Larkooo Dec 5, 2024
06bc91e
fix(torii-tokens-graphql): returns tokens balances with no metadata (…
glihm Dec 5, 2024
90e2edd
fix(torii-core): store v3 transactions (#2768)
Larkooo Dec 5, 2024
ea0973c
fix(torii/graphql): filter out tokens with zero balance (#2769)
lambda-0x Dec 5, 2024
9be17f3
feat(katana): l1 gas prices sampling (#2766)
augustin-v Dec 5, 2024
b5d7ded
test(katana-rpc): get starknet pending block (#2772)
kariy Dec 5, 2024
575040c
feat: Improve bindgen types (#2773)
MartianGreed Dec 6, 2024
b821845
feat(bindgen): add `AccountInterface` to function (#2770)
starknetdev Dec 6, 2024
c5e7d6b
Prepare release: v1.0.5 (#2774)
tarrencev Dec 6, 2024
a023fc6
fix(torii-sql): sql playground for slot (#2779)
Larkooo Dec 6, 2024
484b7da
chore(devcontainer): update image: v1.0.5 (#2775)
tarrencev Dec 6, 2024
d44522f
feat(torii-grpc): ordering and pagination (#2765)
Larkooo Dec 6, 2024
dccd06e
refactor(torii-libp2p): always use is_valid_signature (#2776)
Larkooo Dec 6, 2024
36dd785
fix(torii-grpc): retrieve balances (#2777)
Larkooo Dec 9, 2024
bd04c6b
fix(sozo): remove skipped contracts from manifest (#2787)
glihm Dec 9, 2024
0d29ae3
feat(sozo): provider health check (#2745)
847850277 Dec 9, 2024
3e062fd
feat(torii-server): add MCP service to torii ✨ (#2778)
Larkooo Dec 10, 2024
2738f67
chore(katana): move syncing specific component to `/sync` dir (#2789)
kariy Dec 10, 2024
71db0b4
feat(katana-rpc): rpc server builder (#2788)
kariy Dec 10, 2024
cc4c800
refactor(katana-node): flatten rpc server building logic (#2792)
kariy Dec 10, 2024
51545fa
fix(sozo-bindgen): unity less strict on composites (#2791)
Larkooo Dec 10, 2024
bcd581e
refactor(torii-server): mcp and sql cleanup with instructions static …
Larkooo Dec 10, 2024
35a58b3
feat(torii): add world block and instrument queries (#2796)
tarrencev Dec 12, 2024
91c76d9
fix: keys composite clause (#2798)
Larkooo Dec 12, 2024
133feec
fix(torii-grpc): address sql precedence in composite (#2800)
Larkooo Dec 12, 2024
ba8ff05
Prepare release: v1.0.6 (#2801)
tarrencev Dec 12, 2024
09dfaaf
chore(devcontainer): update image: v1.0.6 (#2802)
tarrencev Dec 12, 2024
8f72aea
fix(torii-grpc-server): adjust regex to support variable len correctl…
glihm Dec 13, 2024
7817319
docs: add latest OpenZeppelin audit link
glihm Dec 13, 2024
c730914
feat(grpc): add entity models for returned models (#2805)
Larkooo Dec 13, 2024
180c6d2
Prepare release: v1.0.7 (#2804)
tarrencev Dec 13, 2024
4e8f254
chore(devcontainer): update image: v1.0.7 (#2806)
tarrencev Dec 13, 2024
8111b6d
feat: introduce updated at field in top level torii query (#2807)
edisontim Dec 16, 2024
b0e9367
opt(torii-grpc): entity updated after on entities table (#2810)
Larkooo Dec 16, 2024
3f9c309
Prepare release: v1.0.8 (#2813)
tarrencev Dec 16, 2024
28ec8e8
chore(devcontainer): update image: v1.0.8 (#2815)
tarrencev Dec 17, 2024
fcf9e26
feat(torii-grpc): IN operator for comparison (#2812)
Larkooo Dec 17, 2024
a2f00ed
Feat/bindgen improve types (#2816)
MartianGreed Dec 17, 2024
d67a5fd
feat(katana): dedup fork request (#2001)
cwkang1998 Dec 19, 2024
c0b8033
feat(torii-core): add indices to speed up queries (#2824)
Larkooo Dec 20, 2024
93168a1
Fix/bindgen types improvements (#2832)
MartianGreed Dec 20, 2024
7ff762c
fix(katana): update default erc20 class hash (#2836)
kariy Dec 21, 2024
12c96ae
refactor(dojo): change usage of trace! to avoid path prefix (#2830)
847850277 Dec 21, 2024
3c32891
feat(torii-grpc): start rework to use 1 single query (#2817)
Larkooo Dec 21, 2024
845c5b1
feat(torii-grpc): add list to member value enum for usage in sdk (#2828)
Larkooo Dec 23, 2024
548783b
refactor(torii-gql): get rid of world indexing error (#2827)
Larkooo Dec 23, 2024
7b37e22
feat(torii): token balances subscription (#2831)
Larkooo Dec 23, 2024
6be2d0c
refactor(torii-grpc): chunk schema joins to avoid sqlite join limit (…
Larkooo Dec 23, 2024
02557ae
fix(torii-core): client negative i128 conversion (#2840)
Larkooo Dec 23, 2024
04b5f02
Prepare release: v1.0.9 (#2841)
tarrencev Dec 23, 2024
1c3ff08
chore(devcontainer): update image: v1.0.9 (#2842)
tarrencev Dec 27, 2024
d21b1a1
feat: split calldata and provider call (#2852)
MartianGreed Dec 31, 2024
bd8afc8
feat: add events in typescript bindgen + sort functions by name (#2853)
MartianGreed Dec 31, 2024
6816de7
feat(katana): `getStorageProof` endpoint (#2809)
kariy Dec 16, 2024
bae15c6
feat(katana): limit number of proof keys (#2814)
kariy Dec 16, 2024
6fee398
feat(katana-rpc-types): untagged enum (#2820)
kariy Dec 18, 2024
cfbe7f7
feat(katana): settlement layer initialization command (#2821)
kariy Dec 19, 2024
5524d6f
refactor(katana-trie): use pedersen hash in all tries (#2822)
kariy Dec 19, 2024
247a4ca
refactor(katana): bump `bonsai-trie` to use `IndexMap` in multiproof …
kariy Dec 19, 2024
8087e0a
fix(katana-rpc): return the correct block hash from `getStorageProof`…
kariy Dec 19, 2024
13c0c24
fix(katana): invalid trie path conversion (#2844)
kariy Dec 25, 2024
4aecc53
test(katana): trie snapshot db (#2845)
kariy Dec 25, 2024
c45f70d
feat(katana): return contract storage root in RPC proof response (#2846)
kariy Dec 25, 2024
19b0486
feat(katana): include genesis states in states trie (#2847)
kariy Dec 27, 2024
38b3c2a
feat(katana): rpc modules selection (#2848)
kariy Dec 28, 2024
3ccea4b
hide init subcommand for now
kariy Jan 2, 2025
38f68db
refactor(katana): make gas oracle a critical task (#2859)
kariy Jan 2, 2025
07bb880
fix(torii-core): correct parallelized task hash (#2860)
Larkooo Jan 3, 2025
d9e93c4
fix(torii-core): remove old check for StoreUpdateMember processor (#2…
glihm Jan 6, 2025
3daa543
docs: readme (#2861)
Olexandr88 Jan 6, 2025
1ddee9e
ci: add constraints on jobs to start based on linters (#2866)
glihm Jan 6, 2025
e6d82d9
feat(sozo): introduce walnut verify command + execute "--walnut" flag…
mobycrypt Jan 6, 2025
23614ea
fix(katana-rpc-types): proofs types serde (#2872)
kariy Jan 6, 2025
eb65d81
feat(katana): classes trie hash (#2876)
kariy Jan 8, 2025
baf0ce9
refactor(torii): fragment into different modules (#2856)
Larkooo Jan 8, 2025
5d1d308
chore: fix some typos in comment (#2880)
longxiangqiao Jan 8, 2025
7036164
refactor(libp2p): typed data crate + client server feature set (#2877)
Larkooo Jan 9, 2025
07d7595
fix(torii-graphql): nullable array is empty string (#2882)
Larkooo Jan 9, 2025
7718099
refactor(torii): torii-runner (#2881)
Larkooo Jan 9, 2025
1829ce5
refactor(katana-messaging): remove message execution and fix hashing …
glihm Jan 9, 2025
4371331
Prepare release: v1.0.10 (#2886)
tarrencev Jan 9, 2025
80ac6d1
fix(torii-grpc): nullable enum array deser (#2887)
Larkooo Jan 10, 2025
6a7f065
chore(devcontainer): update image: v1.0.10 (#2889)
tarrencev Jan 10, 2025
dd1ccce
chore: fix wrong keywords in comment (#2885)
dashangcun Jan 10, 2025
b5e45c3
fix(katana): genesis deprecated declared classes in state updates (#2…
kariy Jan 11, 2025
3e517d1
chore(katana): use workspace dep (#2895)
kariy Jan 11, 2025
c93a058
feat(katana): special block hash registry contract (#2894)
kariy Jan 11, 2025
a5377ac
fix(katana-cli): ensure compilation without server feature (#2896)
glihm Jan 11, 2025
56e0bf3
fix: ensure dev flags enables dev api (#2901)
glihm Jan 13, 2025
fde72d7
feat(sozo): split hash command into 'hash compute' and 'hash find' (#…
remybar Jan 13, 2025
a295c22
feat: change enum to CairoCustomEnum (#2907)
MartianGreed Jan 14, 2025
2caf241
refactor: log targets not snakecase (#2904)
Larkooo Jan 14, 2025
1ca1257
fix(katana): separate deprecated declared class (#2903)
kariy Jan 14, 2025
c54c427
feat(torii-graphql): filter on nested values (#2905)
Larkooo Jan 14, 2025
34e56d6
feat(sozo): apply semver to tag versions (#2909)
glihm Jan 14, 2025
2c77db4
Prepare release: v1.0.11 (#2908)
tarrencev Jan 14, 2025
47349d1
fix(sozo): remove dbg
glihm Jan 15, 2025
3e5bf2b
merge main
glihm Jan 15, 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
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.0.0-rc.0"
version = "1.0.0"
dependencies = [
"dojo_plugin",
]
Expand Down
1 change: 1 addition & 0 deletions crates/dojo/core-cairo-test/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mod tests {

mod expanded {
pub(crate) mod selector_attack;
mod poseidon_hash_string;
}

mod helpers {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use core::poseidon::poseidon_hash_span;


glihm marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn test_poseidon_hash_string() {
let bytes: ByteArray = "foo";
let hash = poseidon_hash_string!("foo");
let mut array = array![];
bytes.serialize(ref array);
let computed = poseidon_hash_span(array.span());
assert_eq!(computed, hash);
}
#[test]
fn test_poseidon_hash_string_empty() {
let bytes: ByteArray = "";
let hash = poseidon_hash_string!("");
let mut array = array![];
bytes.serialize(ref array);
let computed = poseidon_hash_span(array.span());
assert_eq!(computed, hash);
}

#[test]
fn test_poseidon_hash_string_31() {
let bytes: ByteArray = "0123456789012345678901234567890";
let hash = poseidon_hash_string!("0123456789012345678901234567890");
let mut array = array![];
bytes.serialize(ref array);
let computed = poseidon_hash_span(array.span());
assert_eq!(computed, hash);
}

#[test]
fn test_poseidon_hash_string_long() {
let bytes: ByteArray = "0123456789012345678901234567890foo";
let hash = poseidon_hash_string!("0123456789012345678901234567890foo");
let mut array = array![];
bytes.serialize(ref array);
let computed = poseidon_hash_span(array.span());
assert_eq!(computed, hash);
}

fn test_poseidon_hash_string_var() {
let bytes: ByteArray = "foo";
let bytes2: ByteArray = "foo";
let hash = poseidon_hash_string!(bytes);
let mut array = array![];
bytes2.serialize(ref array);
let computed = poseidon_hash_span(array.span());
assert_eq!(computed, hash);
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical: Missing #[test] attribute!

The test won't be executed without the test attribute.

Apply this diff to fix:

+#[test]
 fn test_poseidon_hash_string_var() {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fn test_poseidon_hash_string_var() {
let bytes: ByteArray = "foo";
let bytes2: ByteArray = "foo";
let hash = poseidon_hash_string!(bytes);
let mut array = array![];
bytes2.serialize(ref array);
let computed = poseidon_hash_span(array.span());
assert_eq!(computed, hash);
}
#[test]
fn test_poseidon_hash_string_var() {
let bytes: ByteArray = "foo";
let bytes2: ByteArray = "foo";
let hash = poseidon_hash_string!(bytes);
let mut array = array![];
bytes2.serialize(ref array);
let computed = poseidon_hash_span(array.span());
assert_eq!(computed, hash);
}


fn test_poseidon_hash_string_ne() {
let bytes: ByteArray = "foo";
let hash = poseidon_hash_string!("bar");
let mut array = array![];
bytes.serialize(ref array);
let computed = poseidon_hash_span(array.span());
assert_ne!(computed, hash);
}
Comment on lines +44 to +51
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical: Missing #[test] attribute and consider additional negative cases!

  1. The test won't be executed without the test attribute.
  2. Consider adding more negative tests:
    • Similar strings (e.g., "foo" vs "fooo")
    • Case variations (e.g., "foo" vs "FOO")
    • Different string lengths with same content

Apply this diff to fix the attribute:

+#[test]
 fn test_poseidon_hash_string_ne() {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fn test_poseidon_hash_string_ne() {
let bytes: ByteArray = "foo";
let hash = poseidon_hash_string!("bar");
let mut array = array![];
bytes.serialize(ref array);
let computed = poseidon_hash_span(array.span());
assert_ne!(computed, hash);
}
#[test]
fn test_poseidon_hash_string_ne() {
let bytes: ByteArray = "foo";
let hash = poseidon_hash_string!("bar");
let mut array = array![];
bytes.serialize(ref array);
let computed = poseidon_hash_span(array.span());
assert_ne!(computed, hash);
}


4 changes: 4 additions & 0 deletions crates/dojo/core/src/world/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub impl WorldStorageInternalImpl of WorldStorageTrait {
WorldStorage { dispatcher: world, namespace_hash }
}

fn new_from_hash(self: @IWorldDispatcher, namespace_hash: felt252) -> WorldStorage {
WorldStorage { dispatcher: *self, namespace_hash }
}

fn set_namespace(ref self: WorldStorage, namespace: @ByteArray) {
self.namespace_hash = dojo::utils::bytearray_hash(namespace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub mod $name$ {
fn world(self: @ContractState, namespace: @ByteArray) -> dojo::world::storage::WorldStorage {
dojo::world::WorldStorageTrait::new(self.world_provider.world_dispatcher(), namespace)
}

fn world_from_hash(self: @ContractState, namespace_hash: felt252) -> dojo::world::storage::WorldStorage {
dojo::world::WorldStorageTrait::new_from_hash(@(self.world_provider.world_dispatcher()), namespace_hash)
}
}

$body$
Expand Down
3 changes: 2 additions & 1 deletion crates/dojo/lang/src/cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::attribute_macros::{
DojoContract, DojoEvent, DojoModel, DOJO_CONTRACT_ATTR, DOJO_EVENT_ATTR, DOJO_MODEL_ATTR,
};
use super::derive_macros::{dojo_derive_all, DOJO_INTROSPECT_DERIVE, DOJO_PACKED_DERIVE};
use super::inline_macros::SelectorFromTagMacro;
use super::inline_macros::{PoseidonHashStringMacro, SelectorFromTagMacro};

// #[cfg(test)]
// #[path = "plugin_test.rs"]
Expand All @@ -26,6 +26,7 @@ pub fn dojo_plugin_suite() -> PluginSuite {
let mut suite = PluginSuite::default();

suite.add_plugin::<BuiltinDojoPlugin>().add_inline_macro_plugin::<SelectorFromTagMacro>();
suite.add_plugin::<BuiltinDojoPlugin>().add_inline_macro_plugin::<PoseidonHashStringMacro>();

suite
}
Expand Down
2 changes: 2 additions & 0 deletions crates/dojo/lang/src/inline_macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod delete;
pub mod emit;
pub mod get;
pub mod get_models_test_class_hashes;
pub mod poseidon_hash_string;
pub mod selector_from_tag;
pub mod set;
pub mod spawn_test_world;
Expand All @@ -21,6 +22,7 @@ pub use delete::DeleteMacro;
pub use emit::EmitMacro;
pub use get::GetMacro;
pub use get_models_test_class_hashes::GetModelsTestClassHashes;
pub use poseidon_hash_string::PoseidonHashStringMacro;
pub use selector_from_tag::SelectorFromTagMacro;
pub use set::SetMacro;
pub use spawn_test_world::SpawnTestWorld;
Expand Down
62 changes: 62 additions & 0 deletions crates/dojo/lang/src/inline_macros/poseidon_hash_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use cairo_lang_defs::patcher::PatchBuilder;
use cairo_lang_defs::plugin::{
InlineMacroExprPlugin, InlinePluginResult, MacroPluginMetadata, NamedPlugin, PluginDiagnostic,
PluginGeneratedFile,
};
use cairo_lang_defs::plugin_utils::unsupported_bracket_diagnostic;
use cairo_lang_diagnostics::Severity;
use cairo_lang_syntax::node::{ast, TypedStablePtr, TypedSyntaxNode};
use dojo_types::naming;

#[derive(Debug, Default)]
pub struct PoseidonHashStringMacro;

impl NamedPlugin for PoseidonHashStringMacro {
const NAME: &'static str = "poseidon_hash_string";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
const NAME: &'static str = "poseidon_hash_string";
const NAME: &'static str = "poseidon_hash_string";

Wondering if poseidon_hash_bytearray would not be more consistent with the dojo utils functions already using bytearray instead of string.

}

impl InlineMacroExprPlugin for PoseidonHashStringMacro {
fn generate_code(
&self,
db: &dyn cairo_lang_syntax::node::db::SyntaxGroup,
syntax: &ast::ExprInlineMacro,
_metadata: &MacroPluginMetadata<'_>,
) -> InlinePluginResult {
let ast::WrappedArgList::ParenthesizedArgList(arg_list) = syntax.arguments(db) else {
return unsupported_bracket_diagnostic(db, syntax);
};

let args = arg_list.arguments(db).elements(db);

if args.len() != 1 {
return InlinePluginResult {
code: None,
diagnostics: vec![PluginDiagnostic {
stable_ptr: syntax.stable_ptr().untyped(),
message: "Invalid arguments. Expected \"poseidon_hash_string!(\"tag\")\""
.to_string(),
severity: Severity::Error,
}],
};
}

let tag = &args[0].as_syntax_node().get_text(db).replace('\"', "");
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Ohayo, sensei! Properly handle string literals for the tag.

Using replace('\"', "") may not correctly handle all cases, especially if the tag contains escaped quotes or special characters.

Consider parsing the string literal to accurately extract the tag value, handling any escaped characters appropriately.


let selector = naming::compute_bytearray_hash(tag);

let mut builder = PatchBuilder::new(db, syntax);
builder.add_str(&format!("{:#64x}", selector));

let (code, code_mappings) = builder.build();

InlinePluginResult {
code: Some(PluginGeneratedFile {
name: "poseidon_hash_string_macro".into(),
content: code,
code_mappings,
aux_data: None,
}),
diagnostics: vec![],
}
}
}
2 changes: 2 additions & 0 deletions crates/dojo/lang/src/semantics/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ test_file_test!(
get_models_test_class_hashes: "get_models_test_class_hashes",

spawn_test_world: "spawn_test_world",

poseidon_hash_string: "poseidon_hash_string",
Comment on lines +25 to +26
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Ohayo sensei! Missing test file for poseidon_hash_string

The test case poseidon_hash_string is added to the test macro but the corresponding test file is missing in crates/dojo/lang/src/semantics/test_data/. All other test files listed in the macro have their corresponding files present in the test directory.

  • Missing test file: crates/dojo/lang/src/semantics/test_data/poseidon_hash_string
  • Other test files like get, set, selector_from_tag etc. are properly present
🔗 Analysis chain

Ohayo sensei! Verify the test file existence and structure.

The new test case poseidon_hash_string has been added to the test suite, but we should verify that the corresponding test file exists at the expected location.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence and structure of the poseidon_hash_string test file

# Check if the test file exists
fd "poseidon_hash_string" "src/plugin/semantics/test_data"

# If found, examine its content for proper test structure
if [ $? -eq 0 ]; then
  rg -A 10 "test|assert" "src/plugin/semantics/test_data/poseidon_hash_string.cairo"
fi

Length of output: 192


Script:

#!/bin/bash
# Let's first find the correct test data directory structure
fd -t d "test_data"

# Then search for the poseidon test file in the correct location
fd "poseidon_hash_string" .

# Let's also check the test file structure in semantics/tests.rs to understand the pattern
rg -A 5 "test_file_test!" "crates/dojo/lang/src/semantics/tests.rs"

Length of output: 586


Script:

#!/bin/bash
# Let's check if the test file exists in the correct location
fd "poseidon_hash_string" "crates/dojo/lang/src/semantics/test_data"

# Let's also check the content of the test macro to understand the full context
cat "crates/dojo/lang/src/semantics/tests.rs"

# And examine the actual test files that exist
ls -la "crates/dojo/lang/src/semantics/test_data" 2>/dev/null || echo "Directory not found"

Length of output: 2474

},
test_semantics
);
Expand Down
18 changes: 17 additions & 1 deletion examples/spawn-and-move/src/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,17 @@ pub mod actions {
fn world_default(self: @ContractState) -> dojo::world::WorldStorage {
self.world(@"ns")
}

fn world_default_from_hash(self: @ContractState) -> dojo::world::WorldStorage {
self.world_from_hash(poseidon_hash_string!("ns"))
}
}
}

#[cfg(test)]
mod tests {
use dojo::model::{ModelStorage, ModelValueStorage, ModelStorageTest};
use dojo::world::WorldStorageTrait;
use dojo::world::{WorldStorageTrait};
use dojo_cairo_test::{
spawn_test_world, NamespaceDef, TestResource, ContractDefTrait, ContractDef,
WorldStorageTestTrait
Expand Down Expand Up @@ -306,4 +310,16 @@ mod tests {
assert(new_position.vec.x == initial_position.vec.x + 1, 'position x is wrong');
assert(new_position.vec.y == initial_position.vec.y, 'position y is wrong');
}

#[test]
#[available_gas(30000000)]
fn test_world_from_hash() {
let ndef = namespace_def();
let mut world = spawn_test_world([ndef].span());
world.sync_perms_and_inits(contract_defs());
Copy link
Collaborator

Choose a reason for hiding this comment

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

May be removed as not used.

Suggested change
world.sync_perms_and_inits(contract_defs());

let hash: felt252 = poseidon_hash_string!("ns");
let storage = world.dispatcher.new_from_hash(hash);
assert_eq!(storage.namespace_hash, world.namespace_hash);
assert_eq!(storage.dispatcher.contract_address, world.dispatcher.contract_address);
}
}
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.
Loading