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

fix: ensure we check registered model and not declared models #2368

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Changes from all commits
Commits
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
53 changes: 30 additions & 23 deletions crates/sozo/ops/src/migration/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,25 @@

ui.print_header(format!("# Models ({})", models.len()));

let world = WorldContract::new(world_address, &migrator);

Check warning on line 509 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L508-L509

Added lines #L508 - L509 were not covered by tests
let mut declare_output = vec![];
let mut declared_models_tags = vec![];
let mut models_to_register = vec![];

Check warning on line 511 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L511

Added line #L511 was not covered by tests

for (i, m) in models.iter().enumerate() {
let tag = &m.diff.tag;

ui.print(italic_message(tag).to_string());

if let Resource::Unregistered =
world.resource(&compute_selector_from_tag(tag)).call().await?
{
models_to_register.push(tag.clone());
} else {
ui.print_sub("Already registered");
continue;

Check warning on line 524 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L518-L524

Added lines #L518 - L524 were not covered by tests
}

match m.declare(&migrator, txn_config).await {
Ok(output) => {
ui.print_sub(format!("Selector: {:#066x}", compute_selector_from_tag(tag)));
Expand All @@ -525,7 +536,6 @@
}
Err(MigrationError::ClassAlreadyDeclared) => {
ui.print_sub("Already declared");
declared_models_tags.push(m.diff.tag.clone());
}
Err(MigrationError::ArtifactError(e)) => {
return Err(handle_artifact_error(ui, models[i].artifact_path(), e));
Expand All @@ -537,17 +547,10 @@
}
}

let world = WorldContract::new(world_address, &migrator);

let mut registered_models = vec![];

let calls = models
.iter()
.filter(|m| !declared_models_tags.contains(&m.diff.tag))
.map(|c| {
registered_models.push(c.diff.tag.clone());
world.register_model_getcall(&c.diff.local_class_hash.into())
})
.filter(|m| models_to_register.contains(&m.diff.tag))
.map(|c| world.register_model_getcall(&c.diff.local_class_hash.into()))

Check warning on line 553 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L552-L553

Added lines #L552 - L553 were not covered by tests
.collect::<Vec<_>>();

let InvokeTransactionResult { transaction_hash } =
Expand All @@ -560,7 +563,7 @@

ui.print(format!("All models are registered at: {transaction_hash:#x}\n"));

Ok(RegisterOutput { transaction_hash, declare_output, registered_models })
Ok(RegisterOutput { transaction_hash, declare_output, registered_models: models_to_register })

Check warning on line 566 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L566

Added line #L566 was not covered by tests
}

// For now duplicated because the migrator account is different from the declarers account type.
Expand All @@ -587,7 +590,7 @@
ui.print_header(format!("# Models ({})", models.len()));

let mut declare_output = vec![];
let mut registered_models = vec![];
let mut models_to_register = vec![];

let mut declarers_tasks = HashMap::new();
for (i, m) in models.iter().enumerate() {
Expand Down Expand Up @@ -615,11 +618,21 @@

let all_results = futures::future::join_all(futures).await;

let mut declared_models_tags = vec![];
let world = WorldContract::new(world_address, &migrator);

for results in all_results {
for (index, tag, result) in results {
ui.print(italic_message(&tag).to_string());

if let Resource::Unregistered =
world.resource(&compute_selector_from_tag(&tag)).call().await?
{
models_to_register.push(tag.clone());
} else {
ui.print_sub("Already registered");
continue;

Check warning on line 633 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L632-L633

Added lines #L632 - L633 were not covered by tests
}

match result {
Ok(output) => {
ui.print_sub(format!("Selector: {:#066x}", compute_selector_from_tag(&tag)));
Expand All @@ -632,7 +645,6 @@
}
Err(MigrationError::ClassAlreadyDeclared) => {
ui.print_sub("Already declared");
declared_models_tags.push(tag.clone());
}
Err(MigrationError::ArtifactError(e)) => {
return Err(handle_artifact_error(ui, models[index].artifact_path(), e));
Expand All @@ -645,15 +657,10 @@
}
}

let world = WorldContract::new(world_address, &migrator);

let calls = models
.iter()
.filter(|m| !declared_models_tags.contains(&m.diff.tag))
.map(|c| {
registered_models.push(c.diff.tag.clone());
world.register_model_getcall(&c.diff.local_class_hash.into())
})
.filter(|m| models_to_register.contains(&m.diff.tag))
.map(|c| world.register_model_getcall(&c.diff.local_class_hash.into()))
.collect::<Vec<_>>();

let InvokeTransactionResult { transaction_hash } =
Expand All @@ -666,7 +673,7 @@

ui.print(format!("All models are registered at: {transaction_hash:#x}\n"));

Ok(RegisterOutput { transaction_hash, declare_output, registered_models })
Ok(RegisterOutput { transaction_hash, declare_output, registered_models: models_to_register })
}

async fn register_dojo_contracts<A>(
Expand Down
Loading