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: metadata update use bytearray #2031

Merged
merged 5 commits into from
Jun 6, 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
59 changes: 54 additions & 5 deletions crates/dojo-world/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,31 @@
if let Ok(manifest) = BaseManifest::load_from_path(&manifest_dir.join(BASE_DIR)) {
for model in manifest.models {
let name = model.name.to_string();
dojo_metadata.artifacts.insert(
dojo_metadata.resources_artifacts.insert(
name.clone(),
build_artifact_from_name(&sources_dir, &abis_dir.join("models"), &name),
ResourceMetadata {
name: name.clone(),
artifacts: build_artifact_from_name(
&sources_dir,
&abis_dir.join("models"),
&name,
),
},
);
}

for contract in manifest.contracts {
let name = contract.name.to_string();
dojo_metadata.artifacts.insert(
dojo_metadata.resources_artifacts.insert(
name.clone(),
build_artifact_from_name(&sources_dir, &abis_dir.join("contracts"), &name),
ResourceMetadata {
name: name.clone(),
artifacts: build_artifact_from_name(
&sources_dir,
&abis_dir.join("contracts"),
&name,
),
},
);
}
}
Expand All @@ -135,12 +149,19 @@
pub env: Option<Environment>,
}

/// Metadata for a user defined resource (models, contracts).
#[derive(Default, Serialize, Deserialize, Debug, Clone)]

Check warning on line 153 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L153

Added line #L153 was not covered by tests
pub struct ResourceMetadata {
pub name: String,
pub artifacts: ArtifactMetadata,
}

/// Metadata collected from the project configuration and the Dojo workspace
#[derive(Default, Deserialize, Debug, Clone)]
pub struct DojoMetadata {
pub world: WorldMetadata,
pub env: Option<Environment>,
pub artifacts: HashMap<String, ArtifactMetadata>,
pub resources_artifacts: HashMap<String, ResourceMetadata>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -342,6 +363,34 @@
}
}

impl ResourceMetadata {
pub async fn upload(&self) -> Result<String> {
let mut meta = self.clone();
let client =
IpfsClient::from_str(IPFS_CLIENT_URL)?.with_credentials(IPFS_USERNAME, IPFS_PASSWORD);

Check warning on line 370 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L367-L370

Added lines #L367 - L370 were not covered by tests

if let Some(Uri::File(abi)) = &self.artifacts.abi {
let abi_data = std::fs::read(abi)?;
let reader = Cursor::new(abi_data);
let response = client.add(reader).await?;
meta.artifacts.abi = Some(Uri::Ipfs(format!("ipfs://{}", response.hash)))
};

Check warning on line 377 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L372-L377

Added lines #L372 - L377 were not covered by tests

if let Some(Uri::File(source)) = &self.artifacts.source {
let source_data = std::fs::read(source)?;
let reader = Cursor::new(source_data);
let response = client.add(reader).await?;
meta.artifacts.source = Some(Uri::Ipfs(format!("ipfs://{}", response.hash)))
};

Check warning on line 384 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L379-L384

Added lines #L379 - L384 were not covered by tests

let serialized = json!(meta).to_string();
let reader = Cursor::new(serialized);
let response = client.add(reader).await?;

Check warning on line 388 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L386-L388

Added lines #L386 - L388 were not covered by tests

Ok(response.hash)
}

Check warning on line 391 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L390-L391

Added lines #L390 - L391 were not covered by tests
}

impl DojoMetadata {
pub fn env(&self) -> Option<&Environment> {
self.env.as_ref()
Expand Down
13 changes: 9 additions & 4 deletions crates/dojo-world/src/metadata_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,18 @@ async fn get_full_dojo_metadata_from_workspace() {

dbg!(&artifacts);
for (abi_subdir, name) in artifacts {
let artifact = dojo_metadata.artifacts.get(&name);
assert!(artifact.is_some(), "bad artifact for {}", name);
let artifact = artifact.unwrap();
let resource = dojo_metadata.resources_artifacts.get(&name);
assert!(resource.is_some(), "bad resource metadata for {}", name);
let resource = resource.unwrap();

let sanitized_name = name.replace("::", "_");

check_artifact(artifact.clone(), sanitized_name, &abis_dir.join(abi_subdir), &sources_dir);
check_artifact(
resource.artifacts.clone(),
sanitized_name,
&abis_dir.join(abi_subdir),
&sources_dir,
);
}
}

Expand Down
15 changes: 6 additions & 9 deletions crates/sozo/ops/src/migration/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
AbiFormat, BaseManifest, DeploymentManifest, DojoContract, DojoModel, Manifest,
ManifestMethods, WorldContract as ManifestWorldContract, WorldMetadata,
};
use dojo_world::metadata::{dojo_metadata_from_workspace, ArtifactMetadata};
use dojo_world::metadata::{dojo_metadata_from_workspace, ResourceMetadata};
use dojo_world::migration::class::ClassMigration;
use dojo_world::migration::contract::ContractMigration;
use dojo_world::migration::strategy::{generate_salt, prepare_for_migration, MigrationStrategy};
Expand Down Expand Up @@ -255,13 +255,12 @@
/// on success.
async fn upload_on_ipfs_and_create_resource(
ui: &Ui,
element_name: String,
resource_id: FieldElement,
artifact: ArtifactMetadata,
metadata: ResourceMetadata,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L259 was not covered by tests
) -> Result<world::ResourceMetadata> {
match artifact.upload().await {
match metadata.upload().await {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L261 was not covered by tests
Ok(hash) => {
ui.print_sub(format!("{}: ipfs://{}", element_name, hash));
ui.print_sub(format!("{}: ipfs://{}", metadata.name, hash));

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L263 was not covered by tests
create_resource_metadata(resource_id, hash)
}
Err(_) => Err(anyhow!("Failed to upload IPFS resource.")),
Expand Down Expand Up @@ -330,10 +329,9 @@
// models
if !migration_output.models.is_empty() {
for model_name in migration_output.models {
if let Some(m) = dojo_metadata.artifacts.get(&model_name) {
if let Some(m) = dojo_metadata.resources_artifacts.get(&model_name) {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L332 was not covered by tests
ipfs.push(upload_on_ipfs_and_create_resource(
&ui,
model_name.clone(),
get_selector_from_name(&model_name).expect("ASCII model name"),
m.clone(),
));
Expand All @@ -346,10 +344,9 @@

if !migrated_contracts.is_empty() {
for contract in migrated_contracts {
if let Some(m) = dojo_metadata.artifacts.get(&contract.name) {
if let Some(m) = dojo_metadata.resources_artifacts.get(&contract.name) {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L347 was not covered by tests
ipfs.push(upload_on_ipfs_and_create_resource(
&ui,
contract.name.clone(),
contract.contract_address,
m.clone(),
));
Expand Down
8 changes: 4 additions & 4 deletions crates/sozo/ops/src/tests/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,19 +484,19 @@ async fn check_artifact_metadata<P: starknet::providers::Provider + Sync>(
) {
let resource = world_reader.metadata(&resource_id).call().await.unwrap();

let expected_artifact = dojo_metadata.artifacts.get(element_name);
let expected_resource = dojo_metadata.resources_artifacts.get(element_name);
assert!(
expected_artifact.is_some(),
expected_resource.is_some(),
"Unable to find local artifact metadata for {}",
element_name
);
let expected_artifact = expected_artifact.unwrap();
let expected_resource = expected_resource.unwrap();

check_ipfs_metadata(
client,
element_name,
&resource.metadata_uri.to_string().unwrap(),
expected_artifact,
&expected_resource.artifacts,
)
.await;
}
15 changes: 2 additions & 13 deletions crates/torii/core/src/processors/metadata_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
use async_trait::async_trait;
use base64::engine::general_purpose;
use base64::Engine as _;
use cainome::cairo_serde::{ByteArray, CairoSerde};
use dojo_world::contracts::world::WorldContractReader;
use dojo_world::metadata::{Uri, WorldMetadata};
use reqwest::Client;
use starknet::core::types::{Event, MaybePendingTransactionReceipt};
use starknet::core::utils::parse_cairo_short_string;
use starknet::providers::Provider;
use starknet_crypto::FieldElement;
use tokio_util::bytes::Bytes;
Expand Down Expand Up @@ -58,18 +58,7 @@
event: &Event,
) -> Result<(), Error> {
let resource = &event.data[0];
let uri_len: u8 = event.data[1].try_into().unwrap();

let uri_str = if uri_len > 0 {
event.data[2..=uri_len as usize + 1]
.iter()
.map(parse_cairo_short_string)
.collect::<Result<Vec<_>, _>>()?
.concat()
} else {
"".to_string()
};

let uri_str = ByteArray::cairo_deserialize(&event.data, 1)?.to_string()?;

Check warning on line 61 in crates/torii/core/src/processors/metadata_update.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/metadata_update.rs#L61

Added line #L61 was not covered by tests
info!(
target: LOG_TARGET,
resource = %format!("{:#x}", resource),
Expand Down
Loading