From 4e911d47f3389a0d41b16be646f8ba1c659c4375 Mon Sep 17 00:00:00 2001 From: glihm Date: Sun, 10 Nov 2024 17:09:46 -0600 Subject: [PATCH] fix: add back -vvv and add label to class declaration --- bin/sozo/src/args.rs | 17 +++-- bin/sozo/src/main.rs | 3 +- crates/dojo/utils/src/tx/declarer.rs | 50 +++++++++----- crates/sozo/ops/src/migrate/mod.rs | 98 ++++++++++++++++++---------- examples/simple/manifest_dev.json | 20 +++--- 5 files changed, 120 insertions(+), 68 deletions(-) diff --git a/bin/sozo/src/args.rs b/bin/sozo/src/args.rs index 8ad205b603..226d12cd63 100644 --- a/bin/sozo/src/args.rs +++ b/bin/sozo/src/args.rs @@ -51,16 +51,25 @@ impl SozoArgs { } } - pub fn init_logging(&self) -> Result<(), Box> { - const DEFAULT_LOG_FILTER: &str = - "info,hyper=off,scarb=off,salsa=off,sozo=info,dojo_world=info"; + pub fn init_logging( + &self, + clap_verbosity: &clap_verbosity_flag::Verbosity, + ) -> Result<(), Box> { + let verbose = clap_verbosity.log_level_filter().as_trace() >= LevelFilter::DEBUG; + + let default_log_filter: &str = if verbose { + "info,hyper=off,scarb=off,salsa=off,sozo=trace,dojo_world=trace,dojo_utils=trace,\ + sozo_ops=trace" + } else { + "info,hyper=off,scarb=off,salsa=off,sozo=info,dojo_world=info" + }; LogTracer::init()?; let subscriber = FmtSubscriber::builder() .with_env_filter( tracing_subscriber::EnvFilter::try_from_default_env() - .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(DEFAULT_LOG_FILTER)), + .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(default_log_filter)), ) .finish(); diff --git a/bin/sozo/src/main.rs b/bin/sozo/src/main.rs index 26697bcf63..9a327bed10 100644 --- a/bin/sozo/src/main.rs +++ b/bin/sozo/src/main.rs @@ -11,14 +11,13 @@ use scarb::compiler::CompilerRepository; use scarb::core::Config; use scarb_ui::{OutputFormat, Ui}; use tracing::trace; - mod args; mod commands; mod utils; fn main() { let args = SozoArgs::parse(); - let _ = args.init_logging(); + let _ = args.init_logging(&args.verbose); let ui = Ui::new(args.ui_verbosity(), OutputFormat::Text); if let Err(err) = cli_main(args) { diff --git a/crates/dojo/utils/src/tx/declarer.rs b/crates/dojo/utils/src/tx/declarer.rs index cd08d8f1ca..e1b5b0f48c 100644 --- a/crates/dojo/utils/src/tx/declarer.rs +++ b/crates/dojo/utils/src/tx/declarer.rs @@ -19,6 +19,16 @@ use crate::{ FeeConfig, TransactionError, TransactionExt, TransactionResult, TransactionWaiter, TxnConfig, }; +#[derive(Debug, Clone)] +pub struct LabeledClass { + /// The label of the class. + pub label: String, + /// The casm class hash of the class. + pub casm_class_hash: Felt, + /// The class itself. + pub class: FlattenedSierraClass, +} + /// A declarer is in charge of declaring contracts. #[derive(Debug)] pub struct Declarer @@ -30,7 +40,7 @@ where /// The transaction configuration. pub txn_config: TxnConfig, /// The classes to declare, identified by their casm class hash. - pub classes: HashMap, + pub classes: HashMap, } impl Declarer @@ -43,14 +53,14 @@ where } /// Adds a class to the declarer, do nothing if the class is already known. - pub fn add_class(&mut self, casm_class_hash: Felt, class: FlattenedSierraClass) { - self.classes.entry(casm_class_hash).or_insert(class); + pub fn add_class(&mut self, labeled_class: LabeledClass) { + self.classes.entry(labeled_class.casm_class_hash).or_insert(labeled_class); } /// Extends the classes to the declarer. - pub fn extend_classes(&mut self, classes: Vec<(Felt, FlattenedSierraClass)>) { - for (casm_class_hash, class) in classes { - self.classes.entry(casm_class_hash).or_insert(class); + pub fn extend_classes(&mut self, classes: Vec) { + for labeled_class in classes { + self.classes.entry(labeled_class.casm_class_hash).or_insert(labeled_class); } } @@ -64,10 +74,8 @@ where ) -> Result, TransactionError> { let mut results = vec![]; - for (casm_class_hash, class) in self.classes { - results.push( - Self::declare(casm_class_hash, class, &self.account, &self.txn_config).await?, - ); + for (_, labeled_class) in self.classes { + results.push(Self::declare(labeled_class, &self.account, &self.txn_config).await?); } Ok(results) @@ -75,17 +83,17 @@ where /// Declares a class. pub async fn declare( - casm_class_hash: Felt, - class: FlattenedSierraClass, + labeled_class: LabeledClass, account: &A, txn_config: &TxnConfig, ) -> Result> { - let class_hash = class.class_hash(); + let class_hash = &labeled_class.class.class_hash(); match account.provider().get_class(BlockId::Tag(BlockTag::Pending), class_hash).await { Err(ProviderError::StarknetError(StarknetError::ClassHashNotFound)) => {} Ok(_) => { tracing::trace!( + label = labeled_class.label, class_hash = format!("{:#066x}", class_hash), "Class already declared." ); @@ -94,26 +102,36 @@ where Err(e) => return Err(TransactionError::Provider(e)), } + let casm_class_hash = labeled_class.casm_class_hash; + + tracing::trace!( + label = labeled_class.label, + class_hash = format!("{:#066x}", class_hash), + casm_class_hash = format!("{:#066x}", casm_class_hash), + "Declaring class." + ); + let DeclareTransactionResult { transaction_hash, class_hash } = match txn_config.fee_config { FeeConfig::Strk(_) => { account - .declare_v3(Arc::new(class), casm_class_hash) + .declare_v3(Arc::new(labeled_class.class), casm_class_hash) .send_with_cfg(txn_config) .await? } FeeConfig::Eth(_) => { account - .declare_v2(Arc::new(class), casm_class_hash) + .declare_v2(Arc::new(labeled_class.class), casm_class_hash) .send_with_cfg(txn_config) .await? } }; tracing::trace!( + label = labeled_class.label, transaction_hash = format!("{:#066x}", transaction_hash), class_hash = format!("{:#066x}", class_hash), - casm_class_hash = format!("{:#066x}", casm_class_hash), + casm_class_hash = format!("{:#066x}", labeled_class.casm_class_hash), "Declared class." ); diff --git a/crates/sozo/ops/src/migrate/mod.rs b/crates/sozo/ops/src/migrate/mod.rs index cb93576943..a5b1ddb52b 100644 --- a/crates/sozo/ops/src/migrate/mod.rs +++ b/crates/sozo/ops/src/migrate/mod.rs @@ -21,7 +21,7 @@ use std::collections::HashMap; use cainome::cairo_serde::{ByteArray, ClassHash, ContractAddress}; -use dojo_utils::{Declarer, Deployer, Invoker, TransactionResult, TxnConfig}; +use dojo_utils::{Declarer, Deployer, Invoker, LabeledClass, TransactionResult, TxnConfig}; use dojo_world::config::calldata_decoder::decode_calldata; use dojo_world::config::ProfileConfig; use dojo_world::contracts::WorldContract; @@ -30,7 +30,7 @@ use dojo_world::local::ResourceLocal; use dojo_world::remote::ResourceRemote; use dojo_world::{utils, ResourceType}; use starknet::accounts::{ConnectedAccount, SingleOwnerAccount}; -use starknet::core::types::{Call, FlattenedSierraClass}; +use starknet::core::types::Call; use starknet::providers::{AnyProvider, Provider}; use starknet::signers::LocalWallet; use starknet_crypto::Felt; @@ -302,7 +302,7 @@ where // Namespaces must be synced first, since contracts, models and events are namespaced. self.namespaces_getcalls(&mut invoker).await?; - let mut classes: HashMap = HashMap::new(); + let mut classes: HashMap = HashMap::new(); let mut n_resources = 0; // Collects the calls and classes to be declared to sync the resources. @@ -360,7 +360,7 @@ where if accounts.is_empty() { trace!("Declaring classes with migrator account."); let mut declarer = Declarer::new(&self.world.account, self.txn_config); - declarer.extend_classes(classes.into_iter().collect()); + declarer.extend_classes(classes.into_values().collect()); let ui_text = format!("Declaring {} classes...", n_classes); ui.update_text_boxed(ui_text); @@ -373,9 +373,9 @@ where declarers.push(Declarer::new(account, self.txn_config)); } - for (idx, (casm_class_hash, class)) in classes.into_iter().enumerate() { + for (idx, (_, labeled_class)) in classes.into_iter().enumerate() { let declarer_idx = idx % declarers.len(); - declarers[declarer_idx].add_class(casm_class_hash, class); + declarers[declarer_idx].add_class(labeled_class); } let ui_text = @@ -450,13 +450,13 @@ where async fn contracts_calls_classes( &self, resource: &ResourceDiff, - ) -> Result<(Vec, HashMap), MigrationError> - { + ) -> Result<(Vec, HashMap), MigrationError> { let mut calls = vec![]; let mut classes = HashMap::new(); let namespace = resource.namespace(); let ns_bytearray = ByteArray::from_string(&namespace)?; + let tag = resource.tag(); if let ResourceDiff::Created(ResourceLocal::Contract(contract)) = resource { trace!( @@ -466,8 +466,13 @@ where "Registering contract." ); - classes - .insert(contract.common.casm_class_hash, contract.common.class.clone().flatten()?); + let casm_class_hash = contract.common.casm_class_hash; + let class = contract.common.class.clone().flatten()?; + + classes.insert( + casm_class_hash, + LabeledClass { label: tag.clone(), casm_class_hash, class }, + ); calls.push(self.world.register_contract_getcall( &contract.dojo_selector(), @@ -488,9 +493,12 @@ where "Upgrading contract." ); + let casm_class_hash = contract_local.common.casm_class_hash; + let class = contract_local.common.class.clone().flatten()?; + classes.insert( - contract_local.common.casm_class_hash, - contract_local.common.class.clone().flatten()?, + casm_class_hash, + LabeledClass { label: tag.clone(), casm_class_hash, class }, ); calls.push(self.world.upgrade_contract_getcall( @@ -508,13 +516,13 @@ where async fn models_calls_classes( &self, resource: &ResourceDiff, - ) -> Result<(Vec, HashMap), MigrationError> - { + ) -> Result<(Vec, HashMap), MigrationError> { let mut calls = vec![]; let mut classes = HashMap::new(); let namespace = resource.namespace(); let ns_bytearray = ByteArray::from_string(&namespace)?; + let tag = resource.tag(); if let ResourceDiff::Created(ResourceLocal::Model(model)) = resource { trace!( @@ -524,7 +532,13 @@ where "Registering model." ); - classes.insert(model.common.casm_class_hash, model.common.class.clone().flatten()?); + let casm_class_hash = model.common.casm_class_hash; + let class = model.common.class.clone().flatten()?; + + classes.insert( + casm_class_hash, + LabeledClass { label: tag.clone(), casm_class_hash, class }, + ); calls.push( self.world @@ -544,9 +558,12 @@ where "Upgrading model." ); + let casm_class_hash = model_local.common.casm_class_hash; + let class = model_local.common.class.clone().flatten()?; + classes.insert( - model_local.common.casm_class_hash, - model_local.common.class.clone().flatten()?, + casm_class_hash, + LabeledClass { label: tag.clone(), casm_class_hash, class }, ); calls.push( @@ -566,13 +583,13 @@ where async fn events_calls_classes( &self, resource: &ResourceDiff, - ) -> Result<(Vec, HashMap), MigrationError> - { + ) -> Result<(Vec, HashMap), MigrationError> { let mut calls = vec![]; let mut classes = HashMap::new(); let namespace = resource.namespace(); let ns_bytearray = ByteArray::from_string(&namespace)?; + let tag = resource.tag(); if let ResourceDiff::Created(ResourceLocal::Event(event)) = resource { trace!( @@ -582,7 +599,13 @@ where "Registering event." ); - classes.insert(event.common.casm_class_hash, event.common.class.clone().flatten()?); + let casm_class_hash = event.common.casm_class_hash; + let class = event.common.class.clone().flatten()?; + + classes.insert( + casm_class_hash, + LabeledClass { label: tag.clone(), casm_class_hash, class }, + ); calls.push( self.world @@ -602,9 +625,12 @@ where "Upgrading event." ); + let casm_class_hash = event_local.common.casm_class_hash; + let class = event_local.common.class.clone().flatten()?; + classes.insert( - event_local.common.casm_class_hash, - event_local.common.class.clone().flatten()?, + casm_class_hash, + LabeledClass { label: tag.clone(), casm_class_hash, class }, ); calls.push( @@ -631,13 +657,13 @@ where ui.update_text("Deploying the world..."); trace!("Deploying the first world."); - Declarer::declare( - self.diff.world_info.casm_class_hash, - self.diff.world_info.class.clone().flatten()?, - &self.world.account, - &self.txn_config, - ) - .await?; + let labeled_class = LabeledClass { + label: "world".to_string(), + casm_class_hash: self.diff.world_info.casm_class_hash, + class: self.diff.world_info.class.clone().flatten()?, + }; + + Declarer::declare(labeled_class, &self.world.account, &self.txn_config).await?; // We want to wait for the receipt to be be able to print the // world block number. @@ -688,13 +714,13 @@ where trace!("Upgrading the world."); ui.update_text("Upgrading the world..."); - Declarer::declare( - self.diff.world_info.casm_class_hash, - self.diff.world_info.class.clone().flatten()?, - &self.world.account, - &self.txn_config, - ) - .await?; + let labeled_class = LabeledClass { + label: "world".to_string(), + casm_class_hash: self.diff.world_info.casm_class_hash, + class: self.diff.world_info.class.clone().flatten()?, + }; + + Declarer::declare(labeled_class, &self.world.account, &self.txn_config).await?; let mut invoker = Invoker::new(&self.world.account, self.txn_config); diff --git a/examples/simple/manifest_dev.json b/examples/simple/manifest_dev.json index dcf12f67a9..ec085d7fce 100644 --- a/examples/simple/manifest_dev.json +++ b/examples/simple/manifest_dev.json @@ -1,7 +1,7 @@ { "world": { - "class_hash": "0x2f92b70bd2b5a40ddef12c55257f245176870b25c7eb0bd7a60cf1f1f2fbf0e", - "address": "0x30e907536f553a4b90bbd008df0785061073e432f20779e0c3549923dd67576", + "class_hash": "0x79d9ce84b97bcc2a631996c3100d57966fc2f5b061fb1ec4dfd0040976bcac6", + "address": "0x1b2e50266b9b673eb82e68249a1babde860f6414737b4a36ff7b29411a64666", "seed": "simple", "name": "simple", "entrypoints": [ @@ -1243,8 +1243,8 @@ }, "contracts": [ { - "address": "0xb03e5934197257ff6e03833bf34989930cd0fa49acd441995e5f40b08e96bc", - "class_hash": "0x758181d4a28c2a580c7ef9e963d4bcd5b0712320dc68a174c4f3ff4ad3eaae0", + "address": "0x366a86098f39b0c3e026067c846367e83111c727b88fa036597120d154d44f5", + "class_hash": "0x340e197b0fac61961591acdd872a89b0cb862893272ab72455356f5534baa7e", "abi": [ { "type": "impl", @@ -1501,7 +1501,7 @@ ] }, { - "address": "0x4664cdf8f8b36a99f441440a92252e40b4885a9973e0d0b38ca204ef2b3c4a1", + "address": "0x53fbb8640694994275d8a1b31ce290ec13e0dd433077775e185a9c31f054008", "class_hash": "0x2a400df88b0add6c980d281dc96354a5cfc2b886547e9ed621b097e21842ee6", "abi": [ { @@ -1677,7 +1677,7 @@ ] }, { - "address": "0x151981aac48998f52e5d3189cf0fb0819ba642158425275a939d915e66bd2a3", + "address": "0x40c69a07b5a9b64f581176511c8f8eac9008b48cb3e3c543eac2bf7466c57e3", "class_hash": "0x7cc8d15e576873d544640f7fd124bd430bd19c0f31e203fb069b4fc2f5c0ab9", "abi": [ { @@ -1853,8 +1853,8 @@ ] }, { - "address": "0x7b320d6a08aa96dd4bdcae3e031d6313e0628239c332e37584ed3ebaea133dd", - "class_hash": "0x758181d4a28c2a580c7ef9e963d4bcd5b0712320dc68a174c4f3ff4ad3eaae0", + "address": "0x1302b12ead2cdce8cb0a47f461ecd9d53629e62e8d38327f6452066298381b5", + "class_hash": "0x340e197b0fac61961591acdd872a89b0cb862893272ab72455356f5534baa7e", "abi": [ { "type": "impl", @@ -2128,13 +2128,13 @@ "events": [ { "members": [], - "class_hash": "0x5c72f78327d896e16f3c9fe7ee5e136ad9fc4cda89fba1ce7e665877d477cd5", + "class_hash": "0x943620824729c411797e6be26c3078924893be417ab08789489532d9c6aebb", "tag": "ns-E", "selector": "0x260e0511a6fa454a7d4ed8bea5fa52fc80fc588e33ba4cb58c65bbeeadf7565" }, { "members": [], - "class_hash": "0x408fc887363634ee0d261cc26606574ce2bc433bedbcef4bb88f8fbc69a1e43", + "class_hash": "0x5ed10e08c25eb6a1cb7e221209eac3baab14be36a3ea0b55f789d8302712310", "tag": "ns-EH", "selector": "0x4c6c7772b19b700cf97d078d02a419670d11d2b689a7a3647eac311b2817ced" }