Skip to content

Commit

Permalink
use full name for artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Dec 11, 2023
1 parent 194a1ec commit 1222da6
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 52 deletions.
57 changes: 32 additions & 25 deletions crates/dojo-lang/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,17 @@ impl Compiler for DojoCompiler {
HashMap::new();

for (decl, class) in zip(contracts, classes) {
let target_name = &unit.target().name;
let contract_name = decl.submodule_id.name(db.upcast_mut());
let file_name = format!("{target_name}-{contract_name}.json");
let contract_full_path = decl.module_id().full_path(db.upcast_mut());
let file_name = format!("{contract_full_path}.json");

let mut file = target_dir.open_rw(file_name.clone(), "output file", ws.config())?;
serde_json::to_writer_pretty(file.deref_mut(), &class)
.with_context(|| format!("failed to serialize contract: {contract_name}"))?;
.with_context(|| format!("failed to serialize contract: {contract_full_path}"))?;

let class_hash = compute_class_hash_of_contract_class(&class).with_context(|| {
format!("problem computing class hash for contract `{contract_name}`")
format!("problem computing class hash for contract `{contract_full_path}`")
})?;
compiled_classes.insert(contract_name, (class_hash, class.abi));
compiled_classes.insert(contract_full_path.into(), (class_hash, class.abi));
}

let mut manifest = target_dir
Expand Down Expand Up @@ -181,9 +180,9 @@ fn find_project_contracts(

pub fn collect_core_crate_ids(db: &RootDatabase) -> Vec<CrateId> {
[
ContractSelector("dojo::base::base".to_string()),
ContractSelector("dojo::executor::executor".to_string()),
ContractSelector("dojo::world::world".to_string()),
ContractSelector(BASE_CONTRACT_NAME.to_string()),
ContractSelector(EXECUTOR_CONTRACT_NAME.to_string()),
ContractSelector(WORLD_CONTRACT_NAME.to_string()),
]
.iter()
.map(|selector| selector.package().into())
Expand Down Expand Up @@ -289,7 +288,7 @@ fn update_manifest(
});

for model in &models {
contracts.remove(model.0.to_case(Case::Snake).as_str());
contracts.remove(model.0.as_str());
}

do_update_manifest(manifest, world, executor, base, models, contracts)?;
Expand All @@ -300,30 +299,34 @@ fn update_manifest(
/// Finds the inline modules annotated as models in the given crate_ids and
/// returns the corresponding Models.
fn get_dojo_model_artifacts(
db: &dyn SemanticGroup,
db: &RootDatabase,
aux_data: &DojoAuxData,
module_id: ModuleId,
compiled_classes: &HashMap<SmolStr, (FieldElement, Option<abi::Contract>)>,
) -> anyhow::Result<HashMap<String, dojo_world::manifest::Model>> {
let mut models = HashMap::with_capacity(aux_data.models.len());

let module_name = module_id.full_path(db);
let module_name = module_name.as_str();

for model in &aux_data.models {
if let Ok(Some(ModuleItemId::Struct(_))) =
db.module_item_by_name(module_id, model.name.clone().into())
{
let model_contract_name = model.name.to_case(Case::Snake);
let model_full_name = format!("{module_name}::{}", &model_contract_name);

let (class_hash, abi) = compiled_classes
.get(model_contract_name.as_str())
.get(model_full_name.as_str())
.cloned()
.ok_or(anyhow!("Model {} not found in target.", model.name))?;

models.insert(
model.name.clone(),
model_full_name.clone(),
dojo_world::manifest::Model {
abi,
class_hash,
name: model.name.clone(),
name: model_full_name.clone(),
members: model.members.clone(),
},
);
Expand All @@ -340,13 +343,17 @@ fn get_dojo_computed_values(
computed_values: &mut BTreeMap<SmolStr, Vec<ComputedValueEntrypoint>>,
) {
if let ModuleId::Submodule(submod_id) = module_id {
let contract = submod_id.name(db);
if !computed_values.contains_key(&contract) {
computed_values.insert(contract.clone(), vec![]);
let _contract = submod_id.name(db);

let module_name = module_id.full_path(db);
let module_name = SmolStr::from(module_name);

if !computed_values.contains_key(&module_name) {
computed_values.insert(module_name.clone(), vec![]);
}
let computed_vals = computed_values.get_mut(&contract).unwrap();
let computed_vals = computed_values.get_mut(&module_name).unwrap();
computed_vals.push(ComputedValueEntrypoint {
contract,
contract: module_name,
entrypoint: aux_data.entrypoint.clone(),
model: aux_data.model.clone(),
})
Expand All @@ -365,30 +372,30 @@ fn get_dojo_contract_artifacts(
.filter(|name| !matches!(name.as_ref(), "world" | "executor" | "base"))
.map(|name| {
let module_name = module_id.full_path(db);
let module_last_name = module_name.split("::").last().unwrap();
let module_name = module_name.as_str();

let reads = match SYSTEM_READS.lock().unwrap().get(module_last_name) {
let reads = match SYSTEM_READS.lock().unwrap().get(module_name) {
Some(models) => {
models.clone().into_iter().collect::<BTreeSet<_>>().into_iter().collect()
}
None => vec![],
};

let write_entries = SYSTEM_WRITES.lock().unwrap();
let writes = match write_entries.get(module_last_name) {
let writes = match write_entries.get(module_name) {
Some(write_ops) => find_module_rw(db, module_id, write_ops),
None => vec![],
};

let (class_hash, abi) = compiled_classes
.get(name)
.get(module_name)
.cloned()
.ok_or(anyhow!("Contract {name} not found in target."))?;

Ok((
name.clone(),
SmolStr::from(module_name),
Contract {
name: name.clone(),
name: module_name.into(),
class_hash,
abi,
writes,
Expand Down
8 changes: 4 additions & 4 deletions crates/dojo-lang/src/manifest_test_data/cairo_v240
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_compiler_cairo_v240
//! > expected_manifest_file
{
"world": {
"name": "world",
"name": "dojo::world::world",
"address": null,
"class_hash": "0x5ac623f0c96059936bd2d0904bdd31799e430fe08a0caff7a5f497260b16497",
"abi": [
Expand Down Expand Up @@ -789,7 +789,7 @@ test_compiler_cairo_v240
"computed": []
},
"executor": {
"name": "executor",
"name": "dojo::executor::executor",
"address": null,
"class_hash": "0x585507fa2818fe78e66da6ea4c5915376739f4abf509d41153f60a16cb1f68d",
"abi": [
Expand Down Expand Up @@ -850,7 +850,7 @@ test_compiler_cairo_v240
"computed": []
},
"base": {
"name": "base",
"name": "dojo::base::base",
"class_hash": "0x6c458453d35753703ad25632deec20a29faf8531942ec109e6eb0650316a2bc",
"abi": [
{
Expand Down Expand Up @@ -953,7 +953,7 @@ test_compiler_cairo_v240
},
"contracts": [
{
"name": "cairo_v240",
"name": "cairo_v240::cairo_v240",
"address": null,
"class_hash": "0x714353d2a54cfd74820f89c19d8911a379214d5d4ccf369d74b5f5844dc565f",
"abi": [
Expand Down
12 changes: 6 additions & 6 deletions crates/dojo-lang/src/manifest_test_data/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_manifest_file
//! > expected_manifest_file
{
"world": {
"name": "world",
"name": "dojo::world::world",
"address": null,
"class_hash": "0x5ac623f0c96059936bd2d0904bdd31799e430fe08a0caff7a5f497260b16497",
"abi": [
Expand Down Expand Up @@ -789,7 +789,7 @@ test_manifest_file
"computed": []
},
"executor": {
"name": "executor",
"name": "dojo::executor::executor",
"address": null,
"class_hash": "0x585507fa2818fe78e66da6ea4c5915376739f4abf509d41153f60a16cb1f68d",
"abi": [
Expand Down Expand Up @@ -850,7 +850,7 @@ test_manifest_file
"computed": []
},
"base": {
"name": "base",
"name": "dojo::base::base",
"class_hash": "0x6c458453d35753703ad25632deec20a29faf8531942ec109e6eb0650316a2bc",
"abi": [
{
Expand Down Expand Up @@ -953,7 +953,7 @@ test_manifest_file
},
"contracts": [
{
"name": "actions",
"name": "dojo_examples::actions::actions",
"address": null,
"class_hash": "0x69c6bec7de74fc2404fe6b68ad8ece7be81ad6d861b38a8ba8fa583bfc3666b",
"abi": [
Expand Down Expand Up @@ -1203,7 +1203,7 @@ test_manifest_file
"computed": []
},
{
"name": "moves",
"name": "dojo_examples::models::moves",
"address": null,
"class_hash": "0x64495ca6dc1dc328972697b30468cea364bcb7452bbb6e4aaad3e4b3f190147",
"abi": [
Expand Down Expand Up @@ -1372,7 +1372,7 @@ test_manifest_file
"computed": []
},
{
"name": "position",
"name": "dojo_examples::models::position",
"address": null,
"class_hash": "0x2b233bba9a232a5e891c85eca9f67beedca7a12f9768729ff017bcb62d25c9d",
"abi": [
Expand Down
6 changes: 3 additions & 3 deletions crates/dojo-world/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ use crate::contracts::WorldContractReader;
#[path = "manifest_test.rs"]
mod test;

pub const WORLD_CONTRACT_NAME: &str = "world";
pub const EXECUTOR_CONTRACT_NAME: &str = "executor";
pub const BASE_CONTRACT_NAME: &str = "base";
pub const WORLD_CONTRACT_NAME: &str = "dojo::world::world";
pub const EXECUTOR_CONTRACT_NAME: &str = "dojo::executor::executor";
pub const BASE_CONTRACT_NAME: &str = "dojo::base::base";

#[derive(Error, Debug)]
pub enum ManifestError {
Expand Down
9 changes: 3 additions & 6 deletions crates/dojo-world/src/migration/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Context, Result};
use convert_case::{Case, Casing};
use starknet::core::types::FieldElement;
use starknet::core::utils::{cairo_short_string_to_felt, get_contract_address};
use starknet_crypto::{poseidon_hash_many, poseidon_hash_single};
Expand Down Expand Up @@ -99,7 +98,7 @@ where
continue;
}

let name = file_name_str.split('-').last().unwrap().trim_end_matches(".json").to_string();
let name = file_name_str.trim_end_matches(".json").to_string();

artifact_paths.insert(name, entry.path());
}
Expand Down Expand Up @@ -165,8 +164,7 @@ fn evaluate_class_to_migrate(
match class.remote {
Some(remote) if remote == class.local && !world_contract_will_migrate => Ok(None),
_ => {
let path =
find_artifact_path(class.name.to_case(Case::Snake).as_str(), artifact_paths)?;
let path = find_artifact_path(class.name.as_str(), artifact_paths)?;
Ok(Some(ClassMigration { diff: class.clone(), artifact_path: path.clone() }))
}
}
Expand All @@ -183,8 +181,7 @@ fn evaluate_contracts_to_migrate(
match c.remote {
Some(remote) if remote == c.local && !world_contract_will_migrate => continue,
_ => {
let path =
find_artifact_path(c.name.to_case(Case::Snake).as_str(), artifact_paths)?;
let path = find_artifact_path(c.name.as_str(), artifact_paths)?;
comps_to_migrate.push(ContractMigration {
diff: c.clone(),
artifact_path: path.clone(),
Expand Down
14 changes: 7 additions & 7 deletions crates/dojo-world/src/migration/world_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn no_diff_when_local_and_remote_are_equal() {

let models = vec![Model {
members: vec![],
name: "Model".into(),
name: "dojo_mock::models::model".into(),
class_hash: 11_u32.into(),
..Default::default()
}];
Expand Down Expand Up @@ -56,27 +56,27 @@ fn diff_when_local_and_remote_are_different() {
let models = vec![
Model {
members: vec![],
name: "Model".into(),
name: "dojo_mock::models::model".into(),
class_hash: felt!("0x11"),
..Default::default()
},
Model {
members: vec![],
name: "Model2".into(),
name: "dojo_mock::models::model_2".into(),
class_hash: felt!("0x22"),
..Default::default()
},
];

let contracts = vec![
Contract {
name: "my_contract".into(),
name: "dojo_mock::contracts::my_contract".into(),
class_hash: felt!("0x1111"),
address: Some(felt!("0x2222")),
..Contract::default()
},
Contract {
name: "my_contract_2".into(),
name: "dojo_mock::contracts::my_contract_2".into(),
class_hash: felt!("0x3333"),
address: Some(felt!("4444")),
..Contract::default()
Expand All @@ -100,6 +100,6 @@ fn diff_when_local_and_remote_are_different() {
let diff = WorldDiff::compute(local, Some(remote));

assert_eq!(diff.count_diffs(), 4);
assert!(diff.models.iter().any(|m| m.name == "Model2"));
assert!(diff.contracts.iter().any(|c| c.name == "my_contract"));
assert!(diff.models.iter().any(|m| m.name == "dojo_mock::models::model_2"));
assert!(diff.contracts.iter().any(|c| c.name == "dojo_mock::contracts::my_contract"));
}
3 changes: 2 additions & 1 deletion crates/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::ops::execute;
#[derive(Debug, Args)]
#[command(about = "Execute a system with the given calldata.")]
pub struct ExecuteArgs {
#[arg(help = "The address of the contract to be executed.")]
#[arg(help = "The address of the contract to be executed. Or fully qualified contract name \
(ex: dojo_example::actions::actions")]
pub contract: String,

#[arg(help = "The name of the entrypoint to be executed.")]
Expand Down
5 changes: 5 additions & 0 deletions crates/sozo/src/ops/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ where
None => local_manifest.base.class_hash,
};

// executor.address be updated
// if let Some(manifest) = remote_manifest {
// local_manifest.executor.address = manifest.executor.address;
// }

local_manifest.contracts.iter_mut().for_each(|c| {
let salt = generate_salt(&c.name);
c.address = Some(get_contract_address(salt, base_class_hash, &[], world_address));
Expand Down
1 change: 1 addition & 0 deletions crates/torii/graphql/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ pub async fn spinup_types_test() -> Result<SqlitePool> {
execute_strategy(&ws, &migration, &account, None).await.unwrap();

// Execute `create` and insert 10 records into storage

let records_contract = "0x414e9776a1626963ba21c4f45ddd7b4d2ac907d20c230e9c6a2e6119c2e5d";
let InvokeTransactionResult { transaction_hash } = account
.execute(vec![Call {
Expand Down

0 comments on commit 1222da6

Please sign in to comment.