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

refactor: write manifest file even if migration failed #1652

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions bin/sozo/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use clap::Args;
use dojo_lang::compiler::{BASE_DIR, MANIFESTS_DIR};
use dojo_lang::scarb_internal::build_scarb_root_database;
use dojo_world::manifest::{BaseManifest, DeployedManifest};
use dojo_world::manifest::{BaseManifest, DeploymentManifest};
use dojo_world::metadata::dojo_metadata_from_workspace;
use dojo_world::migration::world::WorldDiff;
use notify_debouncer_mini::notify::RecursiveMode;
Expand All @@ -26,7 +26,7 @@
use super::options::account::AccountOptions;
use super::options::starknet::StarknetOptions;
use super::options::world::WorldOptions;
use crate::ops::migration;
use crate::ops::migration::{self, prepare_migration};

#[derive(Args)]
pub struct DevArgs {
Expand Down Expand Up @@ -113,8 +113,8 @@
account: &SingleOwnerAccount<P, S>,
name: Option<String>,
ws: &Workspace<'_>,
previous_manifest: Option<DeployedManifest>,
) -> Result<(DeployedManifest, Option<FieldElement>)>
previous_manifest: Option<DeploymentManifest>,
) -> Result<(DeploymentManifest, Option<FieldElement>)>

Check warning on line 117 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L116-L117

Added lines #L116 - L117 were not covered by tests
where
P: Provider + Sync + Send + 'static,
S: Signer + Sync + Send + 'static,
Expand All @@ -140,9 +140,10 @@
return Ok((new_manifest.into(), world_address));
}

match migration::apply_diff(ws, &target_dir, diff, name.clone(), world_address, account, None)
.await
{
let ui = ws.config().ui();
let strategy = prepare_migration(&target_dir, diff, name, world_address, &ui)?;

Check warning on line 144 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L143-L144

Added lines #L143 - L144 were not covered by tests

match migration::apply_diff(ws, account, None, &strategy).await {

Check warning on line 146 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L146

Added line #L146 was not covered by tests
Ok(migration_output) => {
config.ui().print(format!(
"🎉 World at address {} updated!",
Expand Down Expand Up @@ -209,7 +210,7 @@
RecursiveMode::Recursive,
)?;
let name = self.name.clone();
let mut previous_manifest: Option<DeployedManifest> = Option::None;
let mut previous_manifest: Option<DeploymentManifest> = Option::None;

Check warning on line 213 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L213

Added line #L213 was not covered by tests
let result = build(&mut context);

let Some((mut world_address, account, _)) = context
Expand Down
9 changes: 6 additions & 3 deletions bin/sozo/src/ops/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use cainome::parser::AbiParser;
use camino::Utf8PathBuf;
use dojo_lang::compiler::{DEPLOYMENTS_DIR, MANIFESTS_DIR};
use dojo_world::manifest::{DeployedManifest, ManifestMethods};
use dojo_world::manifest::{DeploymentManifest, ManifestMethods};
use dojo_world::metadata::Environment;
use starknet::core::types::{BlockId, EventFilter, FieldElement};
use starknet::core::utils::{parse_cairo_short_string, starknet_keccak};
Expand Down Expand Up @@ -48,7 +48,10 @@
return Err(anyhow!("Run scarb migrate before running this command"));
}

Some(extract_events(&DeployedManifest::load_from_path(&deployed_manifest)?, manifest_dir)?)
Some(extract_events(
&DeploymentManifest::load_from_path(&deployed_manifest)?,
manifest_dir,
)?)

Check warning on line 54 in bin/sozo/src/ops/events.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/events.rs#L51-L54

Added lines #L51 - L54 were not covered by tests
} else {
None
};
Expand Down Expand Up @@ -80,7 +83,7 @@
}

fn extract_events(
manifest: &DeployedManifest,
manifest: &DeploymentManifest,
manifest_dir: &Utf8PathBuf,
) -> Result<HashMap<String, Vec<Token>>> {
fn process_abi(
Expand Down
4 changes: 2 additions & 2 deletions bin/sozo/src/ops/migration/migration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use dojo_test_utils::sequencer::{
get_default_test_starknet_config, SequencerConfig, StarknetConfig, TestSequencer,
};
use dojo_world::manifest::{BaseManifest, DeployedManifest};
use dojo_world::manifest::{BaseManifest, DeploymentManifest};
use dojo_world::migration::strategy::prepare_for_migration;
use dojo_world::migration::world::WorldDiff;
use scarb::ops;
Expand Down Expand Up @@ -162,7 +162,7 @@
&Utf8Path::new(base).to_path_buf().join(MANIFESTS_DIR).join(BASE_DIR),
)
.unwrap();
let remote_manifest = DeployedManifest::load_from_remote(
let remote_manifest = DeploymentManifest::load_from_remote(

Check warning on line 165 in bin/sozo/src/ops/migration/migration_test.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/migration_test.rs#L165

Added line #L165 was not covered by tests
JsonRpcClient::new(HttpTransport::new(sequencer.url())),
migration.world_address().unwrap(),
)
Expand Down
147 changes: 91 additions & 56 deletions bin/sozo/src/ops/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use dojo_world::contracts::cairo_utils;
use dojo_world::contracts::world::WorldContract;
use dojo_world::manifest::{
AbstractManifestError, BaseManifest, DeployedManifest, DojoContract, Manifest, ManifestMethods,
OverlayManifest,
AbstractManifestError, BaseManifest, DeploymentManifest, DojoContract, Manifest,
ManifestMethods, OverlayManifest,
};
use dojo_world::metadata::{dojo_metadata_from_workspace, Environment};
use dojo_world::migration::contract::ContractMigration;
Expand Down Expand Up @@ -44,10 +44,14 @@
use crate::commands::options::transaction::TransactionOptions;
use crate::commands::options::world::WorldOptions;

#[derive(Debug, Default, Clone)]

Check warning on line 47 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L47

Added line #L47 was not covered by tests
pub struct MigrationOutput {
pub world_address: FieldElement,
pub world_tx_hash: Option<FieldElement>,
pub world_block_number: Option<u64>,
// Represents if full migration got completeled.
// If false that means migration got partially completed.
pub full: bool,
}

pub async fn execute(
Expand Down Expand Up @@ -83,38 +87,47 @@
ui.print_sub(format!("Total diffs found: {total_diffs}"));

if total_diffs == 0 {
ui.print("\n✨ No changes to be made. Remote World is already up to date!")
} else {
// Migrate according to the diff.
let migration_output = apply_diff(
ws,
&target_dir,
diff,
name,
world_address,
&account,
Some(args.transaction),
)
.await?;

update_manifests_and_abis(
ws,
local_manifest,
remote_manifest,
&manifest_dir,
migration_output,
&chain_id,
)
.await?;
ui.print("\n✨ No changes to be made. Remote World is already up to date!");
return Ok(());

Check warning on line 91 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L90-L91

Added lines #L90 - L91 were not covered by tests
}

let strategy = prepare_migration(&target_dir, diff, name, world_address, &ui)?;
let world_address = strategy.world_address().expect("world address must exist");

// Migrate according to the diff.
match apply_diff(ws, &account, None, &strategy).await {
Ok(migration_output) => {
update_manifests_and_abis(
ws,
local_manifest,
remote_manifest,
&manifest_dir,
migration_output,
&chain_id,
)
.await?;

Check warning on line 108 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L94-L108

Added lines #L94 - L108 were not covered by tests
}
Err(e) => {
update_manifests_and_abis(
ws,
local_manifest,
remote_manifest,
&manifest_dir,
MigrationOutput { world_address, ..Default::default() },
&chain_id,
)
.await?;
return Err(e)?;

Check warning on line 120 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L110-L120

Added lines #L110 - L120 were not covered by tests
}
};

Ok(())
}

async fn update_manifests_and_abis(
ws: &Workspace<'_>,
local_manifest: BaseManifest,
remote_manifest: Option<DeployedManifest>,
remote_manifest: Option<DeploymentManifest>,

Check warning on line 130 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L130

Added line #L130 was not covered by tests
manifest_dir: &Utf8PathBuf,
migration_output: MigrationOutput,
chain_id: &str,
Expand All @@ -128,10 +141,10 @@
.join(chain_id)
.with_extension("toml");

let mut local_manifest: DeployedManifest = local_manifest.into();
let mut local_manifest: DeploymentManifest = local_manifest.into();

Check warning on line 144 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L144

Added line #L144 was not covered by tests

if deployed_path.exists() {
let previous_manifest = DeployedManifest::load_from_path(&deployed_path)?;
let previous_manifest = DeploymentManifest::load_from_path(&deployed_path)?;

Check warning on line 147 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L147

Added line #L147 was not covered by tests
local_manifest.merge_from_previous(previous_manifest);
};

Expand Down Expand Up @@ -166,7 +179,7 @@
}

async fn update_manifest_abis(
local_manifest: &mut DeployedManifest,
local_manifest: &mut DeploymentManifest,

Check warning on line 182 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L182

Added line #L182 was not covered by tests
manifest_dir: &Utf8PathBuf,
chain_id: &str,
) {
Expand Down Expand Up @@ -202,42 +215,47 @@
}
}

#[allow(clippy::too_many_arguments)]
pub(crate) async fn apply_diff<P, S>(
ws: &Workspace<'_>,
target_dir: &Utf8PathBuf,
diff: WorldDiff,
name: Option<String>,
world_address: Option<FieldElement>,
account: &SingleOwnerAccount<P, S>,
txn_config: Option<TransactionOptions>,
strategy: &MigrationStrategy,

Check warning on line 222 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L222

Added line #L222 was not covered by tests
) -> Result<MigrationOutput>
where
P: Provider + Sync + Send + 'static,
S: Signer + Sync + Send + 'static,
{
let ui = ws.config().ui();
let strategy = prepare_migration(target_dir, diff, name, world_address, &ui)?;

println!(" ");

let migration_output = execute_strategy(ws, &strategy, account, txn_config)
let migration_output = execute_strategy(ws, strategy, account, txn_config)

Check warning on line 232 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L232

Added line #L232 was not covered by tests
.await
.map_err(|e| anyhow!(e))
.with_context(|| "Problem trying to migrate.")?;

if let Some(block_number) = migration_output.world_block_number {
ui.print(format!(
"\n🎉 Successfully migrated World on block #{} at address {}",
block_number,
bold_message(format!(
"{:#x}",
strategy.world_address().expect("world address must exist")
))
));
if migration_output.full {
if let Some(block_number) = migration_output.world_block_number {
ui.print(format!(
"\n🎉 Successfully migrated World on block #{} at address {}",
block_number,
bold_message(format!(
"{:#x}",
strategy.world_address().expect("world address must exist")
))
));
} else {
ui.print(format!(
"\n🎉 Successfully migrated World at address {}",
bold_message(format!(
"{:#x}",
strategy.world_address().expect("world address must exist")
))
));
}

Check warning on line 255 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L237-L255

Added lines #L237 - L255 were not covered by tests
} else {
ui.print(format!(
"\n🎉 Successfully migrated World at address {}",
"\n🚨 Partially migrated World at address {}",

Check warning on line 258 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L258

Added line #L258 was not covered by tests
bold_message(format!(
"{:#x}",
strategy.world_address().expect("world address must exist")
Expand Down Expand Up @@ -298,7 +316,7 @@
account: &SingleOwnerAccount<P, S>,
world_address: Option<FieldElement>,
ui: &Ui,
) -> Result<(BaseManifest, Option<DeployedManifest>)>
) -> Result<(BaseManifest, Option<DeploymentManifest>)>

Check warning on line 319 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L319

Added line #L319 was not covered by tests
where
P: Provider + Sync + Send + 'static,
S: Signer + Sync + Send + 'static,
Expand All @@ -318,7 +336,7 @@
}

let remote_manifest = if let Some(address) = world_address {
match DeployedManifest::load_from_remote(account.provider(), address).await {
match DeploymentManifest::load_from_remote(account.provider(), address).await {

Check warning on line 339 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L339

Added line #L339 was not covered by tests
Ok(manifest) => {
ui.print_sub(format!("Found remote World: {address:#x}"));
Some(manifest)
Expand All @@ -340,7 +358,7 @@
Ok((local_manifest, remote_manifest))
}

fn prepare_migration(
pub fn prepare_migration(

Check warning on line 361 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L361

Added line #L361 was not covered by tests
target_dir: &Utf8PathBuf,
diff: WorldDiff,
name: Option<String>,
Expand Down Expand Up @@ -476,17 +494,34 @@
None => {}
};

let mut migration_output = MigrationOutput {
world_address: strategy.world_address()?,
world_tx_hash,
world_block_number,
full: false,
};

// Once Torii supports indexing arrays, we should declare and register the
// ResourceMetadata model.

register_models(strategy, migrator, &ui, txn_config.clone()).await?;
deploy_contracts(strategy, migrator, &ui, txn_config).await?;
match register_models(strategy, migrator, &ui, txn_config.clone()).await {
Ok(_) => (),
Err(e) => {
ui.anyhow(&e);
return Ok(migration_output);

Check warning on line 511 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L509-L511

Added lines #L509 - L511 were not covered by tests
}
}
match deploy_contracts(strategy, migrator, &ui, txn_config).await {
Ok(_) => (),
Err(e) => {
ui.anyhow(&e);
return Ok(migration_output);

Check warning on line 518 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L516-L518

Added lines #L516 - L518 were not covered by tests
}
};

Ok(MigrationOutput {
world_address: strategy.world_address()?,
world_tx_hash,
world_block_number,
})
migration_output.full = true;

Ok(migration_output)
}

enum ContractDeploymentOutput {
Expand Down
4 changes: 2 additions & 2 deletions bin/sozo/src/ops/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use anyhow::{Context, Result};
use dojo_world::contracts::model::ModelReader;
use dojo_world::contracts::{WorldContract, WorldContractReader};
use dojo_world::manifest::DeployedManifest;
use dojo_world::manifest::DeploymentManifest;
use dojo_world::metadata::Environment;
use scarb::core::Config;
use starknet::accounts::Account;
Expand All @@ -25,7 +25,7 @@ pub async fn execute(
let world_reader = WorldContractReader::new(world_address, &provider)
.with_block(BlockId::Tag(BlockTag::Pending));
let manifest = {
match DeployedManifest::load_from_remote(&provider, world_address).await {
match DeploymentManifest::load_from_remote(&provider, world_address).await {
Ok(manifest) => manifest,
Err(e) => {
return Err(anyhow::anyhow!("Failed to build remote World state: {e}"));
Expand Down
4 changes: 2 additions & 2 deletions crates/benches/src/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::Parser;
use dojo_lang::compiler::{DojoCompiler, DEPLOYMENTS_DIR, MANIFESTS_DIR};
use dojo_lang::plugin::CairoPluginRepository;
use dojo_lang::scarb_internal::compile_workspace;
use dojo_world::manifest::DeployedManifest;
use dojo_world::manifest::DeploymentManifest;
use futures::executor::block_on;
use katana_runner::KatanaRunner;
use scarb::compiler::CompilerRepository;
Expand Down Expand Up @@ -117,7 +117,7 @@ async fn prepare_migration_args(args: SozoArgs) -> Result<FieldElement> {

migration::execute(&ws, migrate, None).await?;

let manifest = DeployedManifest::load_from_path(
let manifest = DeploymentManifest::load_from_path(
&manifest_dir
.join(MANIFESTS_DIR)
.join(DEPLOYMENTS_DIR)
Expand Down
Loading
Loading