From 69d83d092f2076c25fa2fa982ee1779f7ea619b5 Mon Sep 17 00:00:00 2001 From: Aroso Emmanuel Date: Mon, 6 Jan 2025 09:48:44 +0100 Subject: [PATCH] fix(sozo): model get not using prefixes --- bin/sozo/src/commands/auth.rs | 36 +++++++++++++++++--------- bin/sozo/src/commands/mod.rs | 6 +++-- bin/sozo/src/commands/model.rs | 46 +++++++++++++++++++++++++++++++--- 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/bin/sozo/src/commands/auth.rs b/bin/sozo/src/commands/auth.rs index 10e02841ce..4f405b9d8d 100644 --- a/bin/sozo/src/commands/auth.rs +++ b/bin/sozo/src/commands/auth.rs @@ -242,7 +242,11 @@ async fn clone_permissions( .external_writers .iter() .filter_map(|(resource_selector, writers)| { - if writers.contains(&from_address) { Some(*resource_selector) } else { None } + if writers.contains(&from_address) { + Some(*resource_selector) + } else { + None + } }) .collect(); @@ -251,7 +255,11 @@ async fn clone_permissions( .external_owners .iter() .filter_map(|(resource_selector, owners)| { - if owners.contains(&from_address) { Some(*resource_selector) } else { None } + if owners.contains(&from_address) { + Some(*resource_selector) + } else { + None + } }) .collect(); @@ -303,16 +311,20 @@ async fn clone_permissions( writers_resource_selectors.extend(external_writer_of.iter().copied()); owners_resource_selectors.extend(external_owner_of.iter().copied()); - writer_of.extend( - external_writer_of - .iter() - .map(|r| if r != &WORLD { format!("{:#066x}", r) } else { "World".to_string() }), - ); - owner_of.extend( - external_owner_of - .iter() - .map(|r| if r != &WORLD { format!("{:#066x}", r) } else { "World".to_string() }), - ); + writer_of.extend(external_writer_of.iter().map(|r| { + if r != &WORLD { + format!("{:#066x}", r) + } else { + "World".to_string() + } + })); + owner_of.extend(external_owner_of.iter().map(|r| { + if r != &WORLD { + format!("{:#066x}", r) + } else { + "World".to_string() + } + })); // Sort the tags to have a deterministic output. let mut writer_of = writer_of.into_iter().collect::>(); diff --git a/bin/sozo/src/commands/mod.rs b/bin/sozo/src/commands/mod.rs index 9ca42277f3..d5ee653c98 100644 --- a/bin/sozo/src/commands/mod.rs +++ b/bin/sozo/src/commands/mod.rs @@ -44,8 +44,10 @@ pub enum Commands { Build(Box), #[command(about = "Build and migrate the world every time a file changes")] Dev(Box), - #[command(about = "Run a migration, declaring and deploying contracts as necessary to update \ - the world")] + #[command( + about = "Run a migration, declaring and deploying contracts as necessary to update \ + the world" + )] Migrate(Box), #[command(about = "Execute a system with the given calldata.")] Execute(Box), diff --git a/bin/sozo/src/commands/model.rs b/bin/sozo/src/commands/model.rs index 4458f940cc..599c7fd697 100644 --- a/bin/sozo/src/commands/model.rs +++ b/bin/sozo/src/commands/model.rs @@ -1,5 +1,6 @@ use anyhow::Result; use clap::{Args, Subcommand}; +use dojo_world::config::calldata_decoder; use scarb::core::Config; use sozo_ops::model; use sozo_ops::resource_descriptor::ResourceDescriptor; @@ -109,8 +110,11 @@ hashes, called 'hash' in the following documentation. #[arg(value_name = "KEYS")] #[arg(value_delimiter = ',')] - #[arg(help = "Comma seperated values e.g., 0x12345,0x69420,...")] - keys: Vec, + #[arg(help = "Comma separated values e.g., \ + 0x12345,0x69420,sstr:\"hello\",sstr:\"misty\". Supported prefixes:\n \ + - sstr: A cairo short string\n \ + - no prefix: A cairo felt")] + keys: String, #[command(flatten)] world: WorldOptions, @@ -119,7 +123,9 @@ hashes, called 'hash' in the following documentation. starknet: StarknetOptions, #[arg(short, long)] - #[arg(help = "Block number at which to retrieve the model data (pending block by default)")] + #[arg( + help = "Block number at which to retrieve the model data (pending block by default)" + )] block: Option, }, } @@ -205,6 +211,8 @@ impl ModelArgs { let (world_diff, provider, _) = utils::get_world_diff_and_provider(starknet, world, &ws).await?; + let keys = calldata_decoder::decode_calldata(&keys)?; + let (record, _, _) = model::model_get( tag.to_string(), keys, @@ -222,3 +230,35 @@ impl ModelArgs { }) } } + +#[cfg(test)] +mod tests { + use super::*; + use starknet::core::utils::cairo_short_string_to_felt; + + #[test] + fn test_short_string_equals_felt() { + // Test that sstr:"misty" equals 0x6d69737479 + let with_prefix = "sstr:\"misty\""; + let with_hex = "0x6d69737479"; + + let felt_from_string = calldata_decoder::decode_calldata(with_prefix).unwrap(); + let felt_from_hex = calldata_decoder::decode_calldata(with_hex).unwrap(); + + assert_eq!(felt_from_string, felt_from_hex); + assert_eq!(felt_from_string[0], Felt::from_hex_str("0x6d69737479").unwrap()); + } + + #[test] + fn test_hex_equals_decimal() { + // Test that 0x6d69737479 equals 469920609401 + let with_hex = "0x6d69737479"; + let with_decimal = "469920609401"; + + let felt_from_hex = calldata_decoder::decode_calldata(with_hex).unwrap(); + let felt_from_decimal = calldata_decoder::decode_calldata(with_decimal).unwrap(); + + assert_eq!(felt_from_hex, felt_from_decimal); + assert_eq!(felt_from_hex[0], Felt::from(469920609401u128)); + } +}