Skip to content

Commit

Permalink
fix(sozo): add back -vvv and add label to class declaration (#2675)
Browse files Browse the repository at this point in the history
fix: add back -vvv and add label to class declaration
  • Loading branch information
glihm authored Nov 10, 2024
1 parent e27a356 commit 95afbe0
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 58 deletions.
17 changes: 13 additions & 4 deletions bin/sozo/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,25 @@ impl SozoArgs {
}
}

pub fn init_logging(&self) -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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();

Expand Down
3 changes: 1 addition & 2 deletions bin/sozo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
50 changes: 34 additions & 16 deletions crates/dojo/utils/src/tx/declarer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<A>
Expand All @@ -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<Felt, FlattenedSierraClass>,
pub classes: HashMap<Felt, LabeledClass>,
}

impl<A> Declarer<A>
Expand All @@ -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<LabeledClass>) {
for labeled_class in classes {
self.classes.entry(labeled_class.casm_class_hash).or_insert(labeled_class);
}
}

Expand All @@ -64,28 +74,26 @@ where
) -> Result<Vec<TransactionResult>, TransactionError<A::SignError>> {
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)
}

/// Declares a class.
pub async fn declare(
casm_class_hash: Felt,
class: FlattenedSierraClass,
labeled_class: LabeledClass,
account: &A,
txn_config: &TxnConfig,
) -> Result<TransactionResult, TransactionError<A::SignError>> {
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."
);
Expand All @@ -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."
);

Expand Down
98 changes: 62 additions & 36 deletions crates/sozo/ops/src/migrate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Felt, FlattenedSierraClass> = HashMap::new();
let mut classes: HashMap<Felt, LabeledClass> = HashMap::new();
let mut n_resources = 0;

// Collects the calls and classes to be declared to sync the resources.
Expand Down Expand Up @@ -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);
Expand All @@ -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 =
Expand Down Expand Up @@ -450,13 +450,13 @@ where
async fn contracts_calls_classes(
&self,
resource: &ResourceDiff,
) -> Result<(Vec<Call>, HashMap<Felt, FlattenedSierraClass>), MigrationError<A::SignError>>
{
) -> Result<(Vec<Call>, HashMap<Felt, LabeledClass>), MigrationError<A::SignError>> {
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!(
Expand All @@ -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(),
Expand All @@ -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(
Expand All @@ -508,13 +516,13 @@ where
async fn models_calls_classes(
&self,
resource: &ResourceDiff,
) -> Result<(Vec<Call>, HashMap<Felt, FlattenedSierraClass>), MigrationError<A::SignError>>
{
) -> Result<(Vec<Call>, HashMap<Felt, LabeledClass>), MigrationError<A::SignError>> {
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!(
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -566,13 +583,13 @@ where
async fn events_calls_classes(
&self,
resource: &ResourceDiff,
) -> Result<(Vec<Call>, HashMap<Felt, FlattenedSierraClass>), MigrationError<A::SignError>>
{
) -> Result<(Vec<Call>, HashMap<Felt, LabeledClass>), MigrationError<A::SignError>> {
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!(
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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.
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 95afbe0

Please sign in to comment.