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

🚧✨ Store contract abi and source #1124

Closed
Closed
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/dojo-core/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ version = 1

[[package]]
name = "dojo"
version = "0.3.1"
version = "0.3.2"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "0.3.1"
version = "0.3.2"
41 changes: 27 additions & 14 deletions crates/dojo-lang/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use cairo_lang_starknet::abi;
use cairo_lang_starknet::contract::{find_contracts, ContractDeclaration};
use cairo_lang_starknet::contract_class::{compile_prepared_db, ContractClass};
use cairo_lang_starknet::plugin::aux_data::StarkNetContractAuxData;
use cairo_lang_utils::bigint::BigUintAsHex;
use cairo_lang_utils::UpcastMut;
use convert_case::{Case, Casing};
use dojo_world::manifest::{
Expand Down Expand Up @@ -99,8 +100,10 @@ impl Compiler for DojoCompiler {
};

// (contract name, class hash)
let mut compiled_classes: HashMap<SmolStr, (FieldElement, Option<abi::Contract>)> =
HashMap::new();
let mut compiled_classes: HashMap<
SmolStr,
(FieldElement, Option<abi::Contract>, Vec<BigUintAsHex>),
> = HashMap::new();

for (decl, class) in zip(contracts, classes) {
let target_name = &unit.target().name;
Expand All @@ -114,7 +117,7 @@ impl Compiler for DojoCompiler {
let class_hash = compute_class_hash_of_contract_class(&class).with_context(|| {
format!("problem computing class hash for contract `{contract_name}`")
})?;
compiled_classes.insert(contract_name, (class_hash, class.abi));
compiled_classes.insert(contract_name, (class_hash, class.abi, class.sierra_program));
}

let mut manifest = target_dir
Expand Down Expand Up @@ -207,41 +210,50 @@ fn update_manifest(
manifest: &mut dojo_world::manifest::Manifest,
db: &RootDatabase,
crate_ids: &[CrateId],
compiled_artifacts: HashMap<SmolStr, (FieldElement, Option<abi::Contract>)>,
compiled_artifacts: HashMap<SmolStr, (FieldElement, Option<abi::Contract>, Vec<BigUintAsHex>)>,
) -> anyhow::Result<()> {
fn get_compiled_artifact_from_map<'a>(
artifacts: &'a HashMap<SmolStr, (FieldElement, Option<abi::Contract>)>,
artifacts: &'a HashMap<SmolStr, (FieldElement, Option<abi::Contract>, Vec<BigUintAsHex>)>,
artifact_name: &str,
) -> anyhow::Result<&'a (FieldElement, Option<abi::Contract>)> {
) -> anyhow::Result<&'a (FieldElement, Option<abi::Contract>, Vec<BigUintAsHex>)> {
artifacts.get(artifact_name).context(format!(
"Contract `{artifact_name}` not found. Did you include `dojo` as a dependency?",
))
}

let world = {
let (hash, abi) = get_compiled_artifact_from_map(&compiled_artifacts, WORLD_CONTRACT_NAME)?;
let (hash, abi, source) =
get_compiled_artifact_from_map(&compiled_artifacts, WORLD_CONTRACT_NAME)?;
Contract {
name: WORLD_CONTRACT_NAME.into(),
abi: abi.clone(),
source: source.clone(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the sierra? Is it enough to verify a contract + abi with just sierra? I think we want to store the cairo code itself, so we can compile it down to sierra -> casm to verify?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, sierra code must be compiled by the verifier

Copy link
Contributor Author

@bal7hazar bal7hazar Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tarrencev I am a bit confused, about what we expect:

  • Everything in the manifest
  • Everything in a separate file abi + source per contract
  • Everything in separate files (1 file for abi + 1 file for the source per contract)
  • All the cairo code into a single file (generated by the compiler? I am not aware of that, also it should includes cairo code dependancies to be verifiable) and 1 abi file per contract

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bal7hazar I think the 3rd option would be best, with the flattened source code. I'm not sure exactly how to extract that from the compiler but it should be possible. Ideally we could support the file structure as well, but it might be tricky. Perhaps we handle source in a separate commit and start with abi?

Copy link
Contributor Author

@bal7hazar bal7hazar Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we handle source in a separate commit and start with abi?

@tarrencev, actually I am not sure the abi is required for a verification purpose. If we can have the cairo sources, we should be able to generate abi from it, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bal7hazar yes that is true but abi can still be useful for interacting with the contracts without verification

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have overspent time on it, and it is still confused to me.
I think it will be more efficient if someone else knowing better dojo internal flows and rust could work on it
I can prefer to pause this PR, leaving it for anyone wanting to continue or restart from the beginning.

class_hash: *hash,
..Default::default()
}
};

let executor = {
let (hash, abi) =
let (hash, abi, source) =
get_compiled_artifact_from_map(&compiled_artifacts, EXECUTOR_CONTRACT_NAME)?;
Contract {
name: EXECUTOR_CONTRACT_NAME.into(),
abi: abi.clone(),
source: source.clone(),
class_hash: *hash,
..Default::default()
}
};

let base = {
let (hash, abi) = get_compiled_artifact_from_map(&compiled_artifacts, BASE_CONTRACT_NAME)?;
Class { name: BASE_CONTRACT_NAME.into(), abi: abi.clone(), class_hash: *hash }
let (hash, abi, source) =
get_compiled_artifact_from_map(&compiled_artifacts, BASE_CONTRACT_NAME)?;
Class {
name: BASE_CONTRACT_NAME.into(),
abi: abi.clone(),
source: source.clone(),
class_hash: *hash,
}
};

let mut models = BTreeMap::new();
Expand Down Expand Up @@ -292,7 +304,7 @@ fn get_dojo_model_artifacts(
db: &dyn SemanticGroup,
aux_data: &DojoAuxData,
module_id: ModuleId,
compiled_classes: &HashMap<SmolStr, (FieldElement, Option<abi::Contract>)>,
compiled_classes: &HashMap<SmolStr, (FieldElement, Option<abi::Contract>, Vec<BigUintAsHex>)>,
) -> anyhow::Result<HashMap<String, dojo_world::manifest::Model>> {
let mut models = HashMap::with_capacity(aux_data.models.len());

Expand All @@ -302,7 +314,7 @@ fn get_dojo_model_artifacts(
{
let model_contract_name = model.name.to_case(Case::Snake);

let (class_hash, abi) = compiled_classes
let (class_hash, abi, _) = compiled_classes
.get(model_contract_name.as_str())
.cloned()
.ok_or(anyhow!("Model {} not found in target.", model.name))?;
Expand All @@ -326,7 +338,7 @@ fn get_dojo_contract_artifacts(
db: &RootDatabase,
module_id: &ModuleId,
aux_data: &StarkNetContractAuxData,
compiled_classes: &HashMap<SmolStr, (FieldElement, Option<abi::Contract>)>,
compiled_classes: &HashMap<SmolStr, (FieldElement, Option<abi::Contract>, Vec<BigUintAsHex>)>,
) -> anyhow::Result<HashMap<SmolStr, Contract>> {
aux_data
.contracts
Expand All @@ -349,7 +361,7 @@ fn get_dojo_contract_artifacts(
None => vec![],
};

let (class_hash, abi) = compiled_classes
let (class_hash, abi, source) = compiled_classes
.get(name)
.cloned()
.ok_or(anyhow!("Contract {name} not found in target."))?;
Expand All @@ -360,6 +372,7 @@ fn get_dojo_contract_artifacts(
name: name.clone(),
class_hash,
abi,
source,
writes,
reads,
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions crates/dojo-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async-trait.workspace = true
cairo-lang-filesystem.workspace = true
cairo-lang-project.workspace = true
cairo-lang-starknet.workspace = true
cairo-lang-utils.workspace = true
camino.workspace = true
convert_case.workspace = true
futures.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/dojo-world/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::Path;

use ::serde::{Deserialize, Serialize};
use cairo_lang_starknet::abi;
use cairo_lang_utils::bigint::BigUintAsHex;
use serde_with::serde_as;
use smol_str::SmolStr;
use starknet::core::serde::unsigned_field_element::UfeHex;
Expand Down Expand Up @@ -104,6 +105,7 @@ pub struct Contract {
#[serde_as(as = "UfeHex")]
pub class_hash: FieldElement,
pub abi: Option<abi::Contract>,
pub source: Vec<BigUintAsHex>,
pub reads: Vec<String>,
pub writes: Vec<String>,
}
Expand All @@ -115,6 +117,7 @@ pub struct Class {
#[serde_as(as = "UfeHex")]
pub class_hash: FieldElement,
pub abi: Option<abi::Contract>,
pub source: Vec<BigUintAsHex>,
}

#[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq)]
Expand Down
16 changes: 16 additions & 0 deletions crates/dojo-world/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub struct WorldMetadata {
pub icon_uri: Option<Uri>,
pub website: Option<Url>,
pub socials: Option<HashMap<String, String>>,
pub abi_uri: Option<Uri>,
pub source_uri: Option<Uri>,
}

#[derive(Default, Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -144,6 +146,20 @@ impl WorldMetadata {
meta.cover_uri = Some(Uri::Ipfs(format!("ipfs://{}", response.hash)))
};

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

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

let serialized = json!(meta).to_string();
let reader = Cursor::new(serialized);
let response = client.add(reader).await?;
Expand Down
6 changes: 6 additions & 0 deletions crates/dojo-world/src/metadata_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ cover_uri = "file://example_cover.png"
icon_uri = "file://example_icon.png"
website = "https://dojoengine.org"
socials.x = "https://x.com/dojostarknet"
abi_uri = "file://path/abi.json"
source_uri = "file://path/source.json"
"#,
)
.unwrap();
Expand Down Expand Up @@ -56,6 +58,8 @@ socials.x = "https://x.com/dojostarknet"
assert_eq!(world.icon_uri, Some(Uri::File("example_icon.png".into())));
assert_eq!(world.website, Some(Url::parse("https://dojoengine.org").unwrap()));
assert_eq!(world.socials.unwrap().get("x"), Some(&"https://x.com/dojostarknet".to_string()));
assert_eq!(world.abi_uri, Some(Uri::File("path/abi.json".into())));
assert_eq!(world.source_uri, Some(Uri::File("path/source.json".into())));
}

#[tokio::test]
Expand All @@ -67,6 +71,8 @@ async fn world_metadata_hash_and_upload() {
icon_uri: None,
website: Some(Url::parse("https://dojoengine.org").unwrap()),
socials: Some(HashMap::from([("x".to_string(), "https://x.com/dojostarknet".to_string())])),
abi_uri: None,
source_uri: None,
};

let _ = meta.upload().await.unwrap();
Expand Down
38 changes: 27 additions & 11 deletions crates/sozo/src/ops/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::Path;
use anyhow::{anyhow, bail, Context, Result};
use dojo_world::contracts::world::WorldContract;
use dojo_world::manifest::{Manifest, ManifestError};
use dojo_world::metadata::dojo_metadata_from_workspace;
use dojo_world::metadata::{dojo_metadata_from_workspace, Metadata, WorldMetadata};
use dojo_world::migration::contract::ContractMigration;
use dojo_world::migration::strategy::{generate_salt, prepare_for_migration, MigrationStrategy};
use dojo_world::migration::world::WorldDiff;
Expand Down Expand Up @@ -271,6 +271,23 @@ fn prepare_migration(
Ok(migration)
}

fn merge_with_default_metadata(metadata: Option<Metadata>) -> WorldMetadata {
// TODO: get default abi and source from compiler
if let Some(meta) = metadata.as_ref().and_then(|inner| inner.world()) {
WorldMetadata {
// abi_uri: Some(Uri::File()),
// source_uri: Some(Uri::File()),
..meta.clone()
}
} else {
WorldMetadata {
// abi_uri: Some(Uri::File()),
// source_uri: Some(Uri::File()),
..Default::default()
}
}
}

// returns the Some(block number) at which migration world is deployed, returns none if world was
// not redeployed
pub async fn execute_strategy<P, S>(
Expand Down Expand Up @@ -341,18 +358,17 @@ where
ui.print_sub(format!("Contract address: {:#x}", world.contract_address));

let metadata = dojo_metadata_from_workspace(ws);
if let Some(meta) = metadata.as_ref().and_then(|inner| inner.world()) {
let hash = meta.upload().await?;
let world_metadata = merge_with_default_metadata(metadata);
let hash = world_metadata.upload().await?;

let InvokeTransactionResult { transaction_hash } =
WorldContract::new(world.contract_address, migrator)
.set_metadata_uri(FieldElement::ZERO, format!("ipfs://{hash}"))
.await
.map_err(|e| anyhow!("Failed to set World metadata: {e}"))?;
let InvokeTransactionResult { transaction_hash } =
WorldContract::new(world.contract_address, migrator)
.set_metadata_uri(FieldElement::ZERO, format!("ipfs://{hash}"))
.await
.map_err(|e| anyhow!("Failed to set World metadata: {e}"))?;

ui.print_sub(format!("Set Metadata transaction: {:#x}", transaction_hash));
ui.print_sub(format!("Metadata uri: ipfs://{hash}"));
}
ui.print_sub(format!("Set Metadata transaction: {:#x}", transaction_hash));
ui.print_sub(format!("Metadata uri: ipfs://{hash}"));
}
None => {}
};
Expand Down