Skip to content

Commit

Permalink
Merge remote-tracking branch 'dojo/main' into katana/piltover-update-…
Browse files Browse the repository at this point in the history
…deser
  • Loading branch information
glihm committed Feb 7, 2025
2 parents b6621b6 + 022d71a commit 12b849b
Show file tree
Hide file tree
Showing 53 changed files with 1,398 additions and 237 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ alloy-primitives = { version = "0.8.3", default-features = false }
alloy-sol-types = { version = "0.8.3", default-features = false }

criterion = "0.5.1"
pprof = { version = "0.13.0", features = [ "criterion", "flamegraph" ] }

# Slot integration. Dojo don't need to manually include `account_sdk` as dependency as `slot` already re-exports it.
slot = { git = "https://github.com/cartridge-gg/slot", rev = "1298a30" }
Expand Down
164 changes: 159 additions & 5 deletions bin/sozo/src/commands/model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use anyhow::{Context, 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;
Expand All @@ -10,6 +11,7 @@ use tracing::trace;
use super::options::starknet::StarknetOptions;
use super::options::world::WorldOptions;
use crate::utils;
use crate::utils::CALLDATA_DOC;

#[derive(Debug, Args)]
pub struct ModelArgs {
Expand Down Expand Up @@ -108,9 +110,12 @@ hashes, called 'hash' in the following documentation.
tag_or_name: ResourceDescriptor,

#[arg(value_name = "KEYS")]
#[arg(value_delimiter = ',')]
#[arg(help = "Comma seperated values e.g., 0x12345,0x69420,...")]
keys: Vec<Felt>,
#[arg(num_args = 1..)]
#[arg(required = true)]
#[arg(
help = format!("List of values representing the serialized keys of the model.\n{CALLDATA_DOC}")
)]
keys: Vec<String>,

#[command(flatten)]
world: WorldOptions,
Expand Down Expand Up @@ -207,7 +212,7 @@ impl ModelArgs {

let (record, _, _) = model::model_get(
tag.to_string(),
keys,
parse_keys(&keys)?,
world_diff.world_info.address,
&provider,
block_id,
Expand All @@ -222,3 +227,152 @@ impl ModelArgs {
})
}
}

/// Parses the keys from the command line into a vector of Felt representing the serialized keys of
/// the model.
fn parse_keys(keys: &[String]) -> Result<Vec<Felt>> {
let mut keys_serde = vec![];

for key in keys {
let key_felt = calldata_decoder::decode_single_calldata(key)
.with_context(|| format!("Failed to decode key: {}", key))?;
keys_serde.extend(key_felt);
}

Ok(keys_serde)
}

#[cfg(test)]
mod tests {
// To do: Add more tests for the flattening of keys
// let flattened_keys: Vec<Felt> = keys.into_iter().flatten().collect();

use clap::Parser;
use starknet::core::utils::cairo_short_string_to_felt;
use starknet::macros::felt;

use super::*;

#[derive(Parser, Debug)]
struct TestCommand {
#[command(subcommand)]
command: ModelCommand,
}

#[test]
fn test_model_get_argument_parsing() {
// Test parsing with hex
let args = TestCommand::parse_from([
"model",
"get",
"Account",
"0x054cb935d86d80b5a0a6e756edf448ab33876d01dd2b07a2a4e63a41e06d0ef5",
"0x6d69737479",
]);

if let ModelCommand::Get { keys, .. } = args.command {
let expected = vec![
felt!("0x054cb935d86d80b5a0a6e756edf448ab33876d01dd2b07a2a4e63a41e06d0ef5"),
felt!("0x6d69737479"),
];

assert_eq!(parse_keys(&keys).unwrap(), expected);
} else {
panic!("Expected Get command");
}

// Test parsing with short string prefix
let args = TestCommand::parse_from([
"model",
"get",
"Account",
"0x054cb935d86d80b5a0a6e756edf448ab33876d01dd2b07a2a4e63a41e06d0ef5",
"sstr:\"misty\"",
]);

if let ModelCommand::Get { keys, .. } = args.command {
let expected = vec![
felt!("0x054cb935d86d80b5a0a6e756edf448ab33876d01dd2b07a2a4e63a41e06d0ef5"),
cairo_short_string_to_felt("misty").unwrap(),
];

assert_eq!(parse_keys(&keys).unwrap(), expected);
} else {
panic!("Expected Get command");
}

// Test parsing with u256 prefix
let args = TestCommand::parse_from([
"model",
"get",
"Account",
"0x054cb935d86d80b5a0a6e756edf448ab33876d01dd2b07a2a4e63a41e06d0ef5",
"u256:0x1",
]);

if let ModelCommand::Get { keys, .. } = args.command {
let expected = vec![
felt!("0x054cb935d86d80b5a0a6e756edf448ab33876d01dd2b07a2a4e63a41e06d0ef5"),
Felt::ONE,
Felt::ZERO,
];

assert_eq!(parse_keys(&keys).unwrap(), expected);
} else {
panic!("Expected Get command");
}

// Test parsing with int prefix
let args = TestCommand::parse_from(["model", "get", "Account", "int:-123456789"]);

if let ModelCommand::Get { keys, .. } = args.command {
let expected = vec![(-123456789_i64).into()];

assert_eq!(parse_keys(&keys).unwrap(), expected);
} else {
panic!("Expected Get command");
}

// Test parsing with str prefix
let args = TestCommand::parse_from(["model", "get", "Account", "str:hello"]);

if let ModelCommand::Get { keys, .. } = args.command {
let expected = vec![
Felt::ZERO,
cairo_short_string_to_felt("hello").unwrap(),
Felt::from_dec_str("5").unwrap(),
];

assert_eq!(parse_keys(&keys).unwrap(), expected);
} else {
panic!("Expected Get command");
}

// Test parsing with all prefixes
let args = TestCommand::parse_from([
"model",
"get",
"Account",
"0x054cb935d86d80b5a0a6e756edf448ab33876d01dd2b07a2a4e63a41e06d0ef5",
"u256:0x1",
"int:-123456789",
"str:hello",
]);

if let ModelCommand::Get { keys, .. } = args.command {
let expected = vec![
felt!("0x054cb935d86d80b5a0a6e756edf448ab33876d01dd2b07a2a4e63a41e06d0ef5"),
Felt::ONE,
Felt::ZERO,
(-123456789_i64).into(),
Felt::ZERO,
cairo_short_string_to_felt("hello").unwrap(),
Felt::from_dec_str("5").unwrap(),
];

assert_eq!(parse_keys(&keys).unwrap(), expected);
} else {
panic!("Expected Get command");
}
}
}
Loading

0 comments on commit 12b849b

Please sign in to comment.