Skip to content

Commit

Permalink
map selector resource type to other resource type where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda-0x committed Jul 31, 2024
1 parent 25358dc commit ccd9a59
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 4 deletions.
43 changes: 41 additions & 2 deletions crates/sozo/ops/src/migration/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use starknet::providers::{Provider, ProviderError};
use tokio::fs;

use super::ui::{bold_message, italic_message, MigrationUi};
use super::utils::generate_resource_map;
use super::{
ContractDeploymentOutput, ContractMigrationOutput, ContractUpgradeOutput, MigrationOutput,
};
Expand Down Expand Up @@ -900,6 +901,12 @@ where
.collect::<HashSet<_>>()
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L902 was not covered by tests

// Generate a map of `Felt` (resource selector) -> `ResourceType` that are available locally
// so we can check if the resource being revoked is known locally.
//
// if the selector is not found in the map we just print its selector
let resource_map = generate_resource_map(ui, world, diff).await?;

for c in &diff.contracts {
// remote is none meants it was not previously deployed.
// but if it didn't get deployed even during this run we should skip migration for it
Expand Down Expand Up @@ -945,7 +952,22 @@ where
ui.print_sub(format!(
"Granting write access to {} for resources: {:?}",
c.tag,
contract_grants.iter().map(|rw| rw.resource.clone()).collect::<Vec<_>>()
contract_grants
.iter()
.map(|rw| {
let resource = &rw.resource;
match resource {
ResourceType::Selector(s) => {
if let Some(r) = resource_map.get(&s.to_hex_string()) {
r.clone()

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

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L960-L962

Added lines #L960 - L962 were not covered by tests
} else {
resource.clone()

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L964 was not covered by tests
}
}
_ => resource.clone(),
}
})
.collect::<Vec<_>>()
));
}

Expand All @@ -955,7 +977,24 @@ where
ui.print_sub(format!(
"Revoking write access to {} for resources: {:?}",
c.tag,
contract_revokes.iter().map(|rw| rw.resource.clone()).collect::<Vec<_>>()
contract_revokes
.iter()
.map(|rw| {
let resource = &rw.resource;
match resource {

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

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L977-L984

Added lines #L977 - L984 were not covered by tests
// Replace selector with appropriate resource type if present in
// resource_map
ResourceType::Selector(s) => {
if let Some(r) = resource_map.get(&s.to_hex_string()) {
r.clone()

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

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L987-L989

Added lines #L987 - L989 were not covered by tests
} else {
resource.clone()

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L991 was not covered by tests
}
}
_ => resource.clone(),

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L994 was not covered by tests
}
})
.collect::<Vec<_>>()
));

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

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L996-L998

Added lines #L996 - L998 were not covered by tests
}

Expand Down
71 changes: 69 additions & 2 deletions crates/sozo/ops/src/migration/utils.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use anyhow::{anyhow, Result};
use std::collections::HashMap;

use anyhow::{anyhow, Context, Result};
use camino::Utf8PathBuf;
use dojo_world::contracts::naming::get_namespace_from_tag;
use dojo_world::contracts::WorldContract;
use dojo_world::manifest::{
AbstractManifestError, BaseManifest, DeploymentManifest, OverlayManifest,
};
use dojo_world::migration::world::WorldDiff;
use itertools::Itertools;
use scarb_ui::Ui;
use starknet::accounts::ConnectedAccount;
use starknet::accounts::{Account, ConnectedAccount};
use starknet::core::types::Felt;

use super::ui::MigrationUi;
use crate::auth::{get_resource_selector, ResourceType};

/// Loads:
/// - `BaseManifest` from filesystem
Expand Down Expand Up @@ -63,3 +70,63 @@ where

Ok((local_manifest, remote_manifest))
}

pub async fn generate_resource_map<A>(
ui: &Ui,
world: &WorldContract<A>,
diff: &WorldDiff,
) -> Result<HashMap<String, ResourceType>>
where
A: ConnectedAccount + Sync + Send,
<A as Account>::SignError: 'static,
{
let mut resource_map = HashMap::new();

for contract in diff.contracts.iter() {
let resource = ResourceType::Contract(contract.tag.clone());
// we know the tag already contains the namespace
let default_namespace = get_namespace_from_tag(&contract.tag);
let selector =
get_resource_selector(ui, world, &resource, &default_namespace).await.with_context(
|| format!("Failed to get resource selector for contract: {}", contract.tag),
)?;

resource_map.insert(selector.to_hex_string(), resource);
}

for model in diff.models.iter() {
let resource = ResourceType::Model(model.tag.clone());
// we know the tag already contains the namespace
let default_namespace = get_namespace_from_tag(&model.tag);
let selector = get_resource_selector(ui, world, &resource, &default_namespace)
.await
.with_context(|| format!("Failed to get resource selector for model: {}", model.tag))?;

resource_map.insert(selector.to_hex_string(), resource);
}

// Collect all the namespaces from the contracts and models
let namespaces = {
let mut namespaces =
diff.models.iter().map(|m| get_namespace_from_tag(&m.tag)).collect::<Vec<_>>();

namespaces.extend(
diff.contracts.iter().map(|c| get_namespace_from_tag(&c.tag)).collect::<Vec<_>>(),
);

// remove duplicates
namespaces.into_iter().unique().collect::<Vec<_>>()
};

for namespace in &namespaces {
let resource = ResourceType::Namespace(namespace.clone());
let selector =
get_resource_selector(ui, world, &resource, "").await.with_context(|| {
format!("Failed to get resource selector for namespace: {}", namespace)

Check warning on line 125 in crates/sozo/ops/src/migration/utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/utils.rs#L125

Added line #L125 was not covered by tests
})?;

resource_map.insert(selector.to_hex_string(), resource);
}

Ok(resource_map)
}

0 comments on commit ccd9a59

Please sign in to comment.