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(sozo): add back -vvv and add label to class declaration #2675

Merged
merged 1 commit into from
Nov 10, 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
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 @@
}
}

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;

Check warning on line 58 in bin/sozo/src/args.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/args.rs#L54-L58

Added lines #L54 - L58 were not covered by tests

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"

Check warning on line 62 in bin/sozo/src/args.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/args.rs#L60-L62

Added lines #L60 - L62 were not covered by tests
} else {
"info,hyper=off,scarb=off,salsa=off,sozo=info,dojo_world=info"

Check warning on line 64 in bin/sozo/src/args.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/args.rs#L64

Added line #L64 was not covered by tests
};

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)),

Check warning on line 72 in bin/sozo/src/args.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/args.rs#L72

Added line #L72 was not covered by tests
)
.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::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);

Check warning on line 20 in bin/sozo/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/main.rs#L20

Added line #L20 was not covered by tests
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 @@
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 @@
/// 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 @@
}

/// 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);

Check warning on line 63 in crates/dojo/utils/src/tx/declarer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/utils/src/tx/declarer.rs#L61-L63

Added lines #L61 - L63 were not covered by tests
}
}

Expand All @@ -64,28 +74,26 @@
) -> 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 @@
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."

Check warning on line 111 in crates/dojo/utils/src/tx/declarer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/utils/src/tx/declarer.rs#L109-L111

Added lines #L109 - L111 were not covered by tests
);

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)

Check warning on line 124 in crates/dojo/utils/src/tx/declarer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/utils/src/tx/declarer.rs#L124

Added line #L124 was not covered by tests
.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),

Check warning on line 134 in crates/dojo/utils/src/tx/declarer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/utils/src/tx/declarer.rs#L134

Added line #L134 was not covered by tests
"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::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 @@
// 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 @@
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());

Check warning on line 363 in crates/sozo/ops/src/migrate/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migrate/mod.rs#L363

Added line #L363 was not covered by tests

let ui_text = format!("Declaring {} classes...", n_classes);
ui.update_text_boxed(ui_text);
Expand All @@ -373,9 +373,9 @@
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 @@
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 @@
"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 @@
"Upgrading contract."
);

let casm_class_hash = contract_local.common.casm_class_hash;
let class = contract_local.common.class.clone().flatten()?;

Check warning on line 497 in crates/sozo/ops/src/migrate/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migrate/mod.rs#L496-L497

Added lines #L496 - L497 were not covered by tests

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 },

Check warning on line 501 in crates/sozo/ops/src/migrate/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migrate/mod.rs#L500-L501

Added lines #L500 - L501 were not covered by tests
);

calls.push(self.world.upgrade_contract_getcall(
Expand All @@ -508,13 +516,13 @@
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 @@
"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 @@
"Upgrading model."
);

let casm_class_hash = model_local.common.casm_class_hash;
let class = model_local.common.class.clone().flatten()?;

Check warning on line 562 in crates/sozo/ops/src/migrate/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migrate/mod.rs#L561-L562

Added lines #L561 - L562 were not covered by tests

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 },

Check warning on line 566 in crates/sozo/ops/src/migrate/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migrate/mod.rs#L565-L566

Added lines #L565 - L566 were not covered by tests
);

calls.push(
Expand All @@ -566,13 +583,13 @@
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 @@
"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 @@
"Upgrading event."
);

let casm_class_hash = event_local.common.casm_class_hash;
let class = event_local.common.class.clone().flatten()?;

Check warning on line 629 in crates/sozo/ops/src/migrate/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migrate/mod.rs#L628-L629

Added lines #L628 - L629 were not covered by tests

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 },

Check warning on line 633 in crates/sozo/ops/src/migrate/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migrate/mod.rs#L632-L633

Added lines #L632 - L633 were not covered by tests
);

calls.push(
Expand All @@ -631,13 +657,13 @@
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 @@
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()?,

Check warning on line 720 in crates/sozo/ops/src/migrate/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migrate/mod.rs#L717-L720

Added lines #L717 - L720 were not covered by tests
};

Declarer::declare(labeled_class, &self.world.account, &self.txn_config).await?;

Check warning on line 723 in crates/sozo/ops/src/migrate/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migrate/mod.rs#L723

Added line #L723 was not covered by tests

let mut invoker = Invoker::new(&self.world.account, self.txn_config);

Expand Down
Loading
Loading