Skip to content

Commit

Permalink
- Add namespace in ModelRegistered event
Browse files Browse the repository at this point in the history
- Fix `sozo event` command to print byte arrays
- Review comments
  • Loading branch information
remybar committed Jun 1, 2024
1 parent ec736fa commit 277c07b
Show file tree
Hide file tree
Showing 32 changed files with 319 additions and 63 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
"apply"
]
},
{
"name": "Sozo",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/target/debug/sozo",
"args": [
"--manifest-path",
"examples/spawn-and-move/Scarb.toml",
"events",
"--world",
"0x04c972a756d796d716f665a8079dbf9aff05bbba41a2b8194553073f31d7d393",
"--chunk-size",
"100"
]
},
{
"type": "lldb",
"request": "launch",
Expand Down
5 changes: 3 additions & 2 deletions crates/dojo-core/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ trait IModel<T> {
/// * `class_hash` - Class Hash of the model.
fn deploy_and_get_metadata(
salt: felt252, class_hash: starknet::ClassHash
) -> SyscallResult<(starknet::ContractAddress, ByteArray, felt252)> {
) -> SyscallResult<(starknet::ContractAddress, ByteArray, felt252, ByteArray)> {
let (contract_address, _) = starknet::deploy_syscall(
class_hash, salt, array![].span(), false,
)?;
let model = IModelDispatcher { contract_address };
let name = model.name();
let selector = model.selector();
Result::Ok((contract_address, name, selector))
let namespace = model.namespace();
Result::Ok((contract_address, name, selector, namespace))
}
5 changes: 3 additions & 2 deletions crates/dojo-core/src/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ mod world {
#[derive(Drop, starknet::Event)]
struct ModelRegistered {
name: ByteArray,
namespace: ByteArray,
class_hash: ClassHash,
prev_class_hash: ClassHash,
address: ContractAddress,
Expand Down Expand Up @@ -382,7 +383,7 @@ mod world {
let caller = get_caller_address();

let salt = self.models_count.read();
let (address, name, selector) = dojo::model::deploy_and_get_metadata(
let (address, name, selector, namespace) = dojo::model::deploy_and_get_metadata(
salt.into(), class_hash
)
.unwrap_syscall();
Expand Down Expand Up @@ -414,7 +415,7 @@ mod world {
self.models.write(selector, (class_hash, address));
EventEmitter::emit(
ref self,
ModelRegistered { name, prev_address, address, class_hash, prev_class_hash }
ModelRegistered { name, namespace, prev_address, address, class_hash, prev_class_hash }
);
}

Expand Down
3 changes: 2 additions & 1 deletion crates/dojo-lang/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ fn get_additional_derive_attrs_for_model(derive_attr_names: &[String]) -> Vec<St

// parse the configuration file of the first crate to extract
// the main package Id (so the name field of the package section of the Scarb.toml file)
// TODO: Ask to Scarb team to expose this package Id information with the new macro system.
fn get_package_id(db: &dyn SyntaxGroup) -> Option<String> {
let crates = db.crates();

Expand Down Expand Up @@ -376,7 +377,7 @@ impl MacroPlugin for BuiltinDojoPlugin {
code: Option::None,
diagnostics: vec![PluginDiagnostic {
stable_ptr: item_ast.stable_ptr().0,
message: "Unable to find the package ID. Be sure to have a 'package:name' field in your Scarb.toml file.".into(),
message: "Unable to find the package ID. Be sure to have a 'package.name' field in your Scarb.toml file.".into(),
severity: Severity::Error,
}],
remove_original_item: false,
Expand Down
5 changes: 5 additions & 0 deletions crates/dojo-world/src/contracts/abi/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,11 @@ abigen!(
"type": "core::byte_array::ByteArray",
"kind": "data"
},
{
"name": "namespace",
"type": "core::byte_array::ByteArray",
"kind": "data"
},
{
"name": "class_hash",
"type": "core::starknet::class_hash::ClassHash",
Expand Down
9 changes: 9 additions & 0 deletions crates/dojo-world/src/manifest/manifest_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ fn parse_registered_model_events() {
data: {
let mut data =
ByteArray::cairo_serialize(&ByteArray::from_string("Model1").unwrap());
data.extend(ByteArray::cairo_serialize(
&ByteArray::from_string("Namespace1").unwrap(),
));
data.extend(vec![felt!("0x5555"), felt!("0xbeef"), felt!("0xa1"), felt!("0")]);
data
},
Expand All @@ -79,6 +82,9 @@ fn parse_registered_model_events() {
data: {
let mut data =
ByteArray::cairo_serialize(&ByteArray::from_string("Model1").unwrap());
data.extend(ByteArray::cairo_serialize(
&ByteArray::from_string("Namespace1").unwrap(),
));
data.extend(vec![felt!("0xbeef"), felt!("0"), felt!("0xa1"), felt!("0xa1")]);
data
},
Expand All @@ -92,6 +98,9 @@ fn parse_registered_model_events() {
data: {
let mut data =
ByteArray::cairo_serialize(&ByteArray::from_string("Model2").unwrap());
data.extend(ByteArray::cairo_serialize(
&ByteArray::from_string("Namespace2").unwrap(),
));
data.extend(vec![felt!("0x6666"), felt!("0"), felt!("0xa3"), felt!("0")]);
data
},
Expand Down
11 changes: 11 additions & 0 deletions crates/sozo/ops/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::{HashMap, VecDeque};
use std::fs;

use anyhow::{anyhow, Result};
use cainome::cairo_serde::{ByteArray, CairoSerde};
use cainome::parser::tokens::{CompositeInner, CompositeInnerKind, CoreBasic, Token};
use cainome::parser::AbiParser;
use camino::Utf8PathBuf;
Expand Down Expand Up @@ -213,6 +214,16 @@ fn process_inners(

let formatted_value = match &inner.token {
Token::CoreBasic(ref cb) => parse_core_basic(cb, &value, true)?,
Token::Composite(c) => {
if c.type_path.eq("core::byte_array::ByteArray") {
data.push_front(value);
let bytearray = ByteArray::cairo_deserialize(data.as_mut_slices().0, 0)?;
data.drain(0..ByteArray::cairo_serialized_size(&bytearray));
ByteArray::to_string(&bytearray)?
} else {
return Err(anyhow!("Unhandled Composite token"));
}
}
Token::Array(ref array) => {
let length = value
.to_string()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kind = "Class"
class_hash = "0xf6f44afb3cacbcc01a371aff62c86ca9a45feba065424c99f7cd8637514d8f"
original_class_hash = "0xf6f44afb3cacbcc01a371aff62c86ca9a45feba065424c99f7cd8637514d8f"
class_hash = "0x610b8797d5ca3d551eb33a4086b13893f10d8d60b8ff23da40d4bd8d907dccb"
original_class_hash = "0x610b8797d5ca3d551eb33a4086b13893f10d8d60b8ff23da40d4bd8d907dccb"
abi = "manifests/dev/abis/base/dojo_world_world.json"
name = "dojo::world::world"
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,11 @@
"type": "core::byte_array::ByteArray",
"kind": "data"
},
{
"name": "namespace",
"type": "core::byte_array::ByteArray",
"kind": "data"
},
{
"name": "class_hash",
"type": "core::starknet::class_hash::ClassHash",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,11 @@
"type": "core::byte_array::ByteArray",
"kind": "data"
},
{
"name": "namespace",
"type": "core::byte_array::ByteArray",
"kind": "data"
},
{
"name": "class_hash",
"type": "core::starknet::class_hash::ClassHash",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@
],
"state_mutability": "view"
},
{
"type": "function",
"name": "namespace",
"inputs": [],
"outputs": [
{
"type": "core::byte_array::ByteArray"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "unpacked_size",
Expand Down
Loading

0 comments on commit 277c07b

Please sign in to comment.