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

feat(sozo): output block number after successful migration #717

Merged
merged 5 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 9 additions & 3 deletions crates/dojo-world/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use starknet::providers::{Provider, ProviderError};
use starknet::signers::Signer;
use thiserror::Error;

use crate::utils::{TransactionWaiter, TransactionWaitingError};
use crate::utils::{block_number_from_receipt, TransactionWaiter, TransactionWaitingError};

pub mod class;
pub mod contract;
Expand All @@ -33,6 +33,7 @@ pub struct DeployOutput {
pub transaction_hash: FieldElement,
pub contract_address: FieldElement,
pub declare: Option<DeclareOutput>,
pub block_number: u64,
}

#[derive(Debug)]
Expand Down Expand Up @@ -181,11 +182,16 @@ pub trait Deployable: Declarable + Sync {
.await
.map_err(MigrationError::Migrator)?;

let _ = TransactionWaiter::new(transaction_hash, account.provider())
let txn = TransactionWaiter::new(transaction_hash, account.provider())
.await
.map_err(MigrationError::WaitingError)?;

Ok(DeployOutput { transaction_hash, contract_address, declare })
Ok(DeployOutput {
transaction_hash,
contract_address,
declare,
block_number: block_number_from_receipt(&txn),
})
}

fn salt(&self) -> FieldElement;
Expand Down
12 changes: 11 additions & 1 deletion crates/dojo-world/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ where
}
}

fn transaction_status_from_receipt(receipt: &TransactionReceipt) -> TransactionStatus {
pub fn transaction_status_from_receipt(receipt: &TransactionReceipt) -> TransactionStatus {
match receipt {
TransactionReceipt::Invoke(receipt) => receipt.status,
TransactionReceipt::Deploy(receipt) => receipt.status,
Expand All @@ -163,6 +163,16 @@ fn transaction_status_from_receipt(receipt: &TransactionReceipt) -> TransactionS
}
}

pub fn block_number_from_receipt(tx: &TransactionReceipt) -> u64 {
match tx {
TransactionReceipt::Invoke(tx) => tx.block_number,
TransactionReceipt::L1Handler(tx) => tx.block_number,
TransactionReceipt::Declare(tx) => tx.block_number,
TransactionReceipt::Deploy(tx) => tx.block_number,
TransactionReceipt::DeployAccount(tx) => tx.block_number,
}
}

#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
Expand Down
39 changes: 26 additions & 13 deletions crates/sozo/src/ops/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use dojo_world::manifest::{Manifest, ManifestError};
use dojo_world::migration::strategy::{prepare_for_migration, MigrationStrategy};
use dojo_world::migration::world::WorldDiff;
use dojo_world::migration::{Declarable, Deployable, MigrationError, RegisterOutput, StateDiff};
use dojo_world::utils::TransactionWaiter;
use dojo_world::utils::{block_number_from_receipt, TransactionWaiter};
use scarb::core::Config;
use starknet::accounts::{Account, ConnectedAccount, SingleOwnerAccount};
use starknet::core::types::{
Expand Down Expand Up @@ -68,13 +68,14 @@ where

println!(" ");

execute_strategy(&strategy, &account, config)
let block_height = execute_strategy(&strategy, &account, config)
.await
.map_err(|e| anyhow!(e))
.with_context(|| "Problem trying to migrate.")?;

config.ui().print(format!(
"\n🎉 Successfully migrated World at address {}",
"\n🎉 Successfully migrated World on block #{} at address {}",
block_height.expect("Atleast one of the contract should be upgraded"),
bold_message(format!(
"{:#x}",
strategy.world_address().expect("world address must exist")
Expand Down Expand Up @@ -196,15 +197,17 @@ where
Ok(migration)
}

// returns the block number at which migration happened
async fn execute_strategy<P, S>(
strategy: &MigrationStrategy,
migrator: &SingleOwnerAccount<P, S>,
ws_config: &Config,
) -> Result<()>
) -> Result<Option<u64>>
where
P: Provider + Sync + Send + 'static,
S: Signer + Sync + Send + 'static,
{
let mut block_height = None;
match &strategy.executor {
Some(executor) => {
ws_config.ui().print_header("# Executor");
Expand Down Expand Up @@ -236,9 +239,11 @@ where
.set_executor(executor.contract_address)
.await?;

let _ = TransactionWaiter::new(transaction_hash, migrator.provider())
let txn = TransactionWaiter::new(transaction_hash, migrator.provider())
.await
.map_err(MigrationError::<S, <P as Provider>::Error>::WaitingError);
.map_err(|_| anyhow!("Transaction execution failed"))?;
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved

block_height = Some(block_number_from_receipt(&txn));

ws_config.ui().print_hidden_sub(format!("Updated at: {transaction_hash:#x}"));
}
Expand Down Expand Up @@ -273,6 +278,8 @@ where
val.transaction_hash
));

block_height = Some(val.block_number);

Ok(())
}
Err(MigrationError::ContractAlreadyDeployed) => Err(anyhow!(
Expand All @@ -288,16 +295,17 @@ where
None => {}
};

register_components(strategy, migrator, ws_config).await?;
register_systems(strategy, migrator, ws_config).await?;
register_components(strategy, migrator, ws_config, &mut block_height).await?;
register_systems(strategy, migrator, ws_config, &mut block_height).await?;

Ok(())
Ok(block_height)
}

async fn register_components<P, S>(
strategy: &MigrationStrategy,
migrator: &SingleOwnerAccount<P, S>,
ws_config: &Config,
block_height: &mut Option<u64>,
) -> Result<Option<RegisterOutput>>
where
P: Provider + Sync + Send + 'static,
Expand Down Expand Up @@ -345,9 +353,11 @@ where
.await
.map_err(|e| anyhow!("Failed to register components to World: {e}"))?;

let _ = TransactionWaiter::new(transaction_hash, migrator.provider())
let txn = TransactionWaiter::new(transaction_hash, migrator.provider())
.await
.map_err(MigrationError::<S, <P as Provider>::Error>::WaitingError);
.map_err(|_| anyhow!("Transaction execution failed"))?;
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved

*block_height = Some(block_number_from_receipt(&txn));

ws_config.ui().print_hidden_sub(format!("registered at: {transaction_hash:#x}"));

Expand All @@ -358,6 +368,7 @@ async fn register_systems<P, S>(
strategy: &MigrationStrategy,
migrator: &SingleOwnerAccount<P, S>,
ws_config: &Config,
block_height: &mut Option<u64>,
) -> Result<Option<RegisterOutput>>
where
P: Provider + Sync + Send + 'static,
Expand Down Expand Up @@ -405,9 +416,11 @@ where
.await
.map_err(|e| anyhow!("Failed to register systems to World: {e}"))?;

let _ = TransactionWaiter::new(transaction_hash, migrator.provider())
let txn = TransactionWaiter::new(transaction_hash, migrator.provider())
.await
.map_err(MigrationError::<S, <P as Provider>::Error>::WaitingError);
.map_err(|_| anyhow!("Transaction execution failed"))?;
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved

*block_height = Some(block_number_from_receipt(&txn));

ws_config.ui().print_hidden_sub(format!("registered at: {transaction_hash:#x}"));

Expand Down