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: add transaction_hash and block_number in the manifest file #1651

Merged
merged 4 commits into from
Mar 14, 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
11 changes: 6 additions & 5 deletions bin/sozo/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,12 @@
match migration::apply_diff(ws, &target_dir, diff, name.clone(), world_address, account, None)
.await
{
Ok(address) => {
config
.ui()
.print(format!("🎉 World at address {} updated!", format_args!("{:#x}", address)));
world_address = Some(address);
Ok(migration_output) => {
config.ui().print(format!(
"🎉 World at address {} updated!",
format_args!("{:#x}", migration_output.world_address)
));
world_address = Some(migration_output.world_address);

Check warning on line 151 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L146-L151

Added lines #L146 - L151 were not covered by tests
}
Err(err) => {
config.ui().error(err.to_string());
Expand Down
91 changes: 60 additions & 31 deletions bin/sozo/src/ops/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
use crate::commands::options::transaction::TransactionOptions;
use crate::commands::options::world::WorldOptions;

pub struct MigrationOutput {
pub world_address: FieldElement,
pub world_tx_hash: Option<FieldElement>,
pub world_block_number: Option<u64>,
}

pub async fn execute(
ws: &Workspace<'_>,
args: MigrateArgs,
Expand Down Expand Up @@ -79,8 +85,8 @@
if total_diffs == 0 {
ui.print("\n✨ No changes to be made. Remote World is already up to date!")
} else {
// Mirate according to the diff.
let world_address = apply_diff(
// Migrate according to the diff.
let migration_output = apply_diff(

Check warning on line 89 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L89

Added line #L89 was not covered by tests
ws,
&target_dir,
diff,
Expand All @@ -96,7 +102,7 @@
local_manifest,
remote_manifest,
&manifest_dir,
world_address,
migration_output,

Check warning on line 105 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L105

Added line #L105 was not covered by tests
&chain_id,
)
.await?;
Expand All @@ -110,14 +116,33 @@
local_manifest: BaseManifest,
remote_manifest: Option<DeployedManifest>,
manifest_dir: &Utf8PathBuf,
world_address: FieldElement,
migration_output: MigrationOutput,

Check warning on line 119 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L119

Added line #L119 was not covered by tests
chain_id: &str,
) -> Result<()> {
let ui = ws.config().ui();
ui.print("\n✨ Updating manifests...");

let deployed_path = manifest_dir
.join(MANIFESTS_DIR)
.join(DEPLOYMENTS_DIR)
.join(chain_id)
.with_extension("toml");

Check warning on line 130 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L125-L130

Added lines #L125 - L130 were not covered by tests
let mut local_manifest: DeployedManifest = local_manifest.into();
local_manifest.world.inner.address = Some(world_address);

if deployed_path.exists() {
let previous_manifest = DeployedManifest::load_from_path(&deployed_path)?;
local_manifest.merge_from_previous(previous_manifest);
};

Check warning on line 136 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L132-L136

Added lines #L132 - L136 were not covered by tests

local_manifest.world.inner.address = Some(migration_output.world_address);

if migration_output.world_tx_hash.is_some() {
local_manifest.world.inner.transaction_hash = migration_output.world_tx_hash;
}
if migration_output.world_block_number.is_some() {
local_manifest.world.inner.block_number = migration_output.world_block_number;
}

Check warning on line 145 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L138-L145

Added lines #L138 - L145 were not covered by tests

let base_class_hash = match remote_manifest {
Some(manifest) => *manifest.base.inner.class_hash(),
Expand All @@ -126,20 +151,15 @@

local_manifest.contracts.iter_mut().for_each(|c| {
let salt = generate_salt(&c.name);
c.inner.address = Some(get_contract_address(salt, base_class_hash, &[], world_address));
c.inner.address =
Some(get_contract_address(salt, base_class_hash, &[], migration_output.world_address));

Check warning on line 155 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L154-L155

Added lines #L154 - L155 were not covered by tests
});

// copy abi files from `abi/base` to `abi/deployments/{chain_id}` and update abi path in
// local_manifest
update_manifest_abis(&mut local_manifest, manifest_dir, chain_id).await;

local_manifest.write_to_path(
&manifest_dir
.join(MANIFESTS_DIR)
.join(DEPLOYMENTS_DIR)
.join(chain_id)
.with_extension("toml"),
)?;
local_manifest.write_to_path(&deployed_path)?;

Check warning on line 162 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L162

Added line #L162 was not covered by tests
ui.print("\n✨ Done.");

Ok(())
Expand Down Expand Up @@ -191,7 +211,7 @@
world_address: Option<FieldElement>,
account: &SingleOwnerAccount<P, S>,
txn_config: Option<TransactionOptions>,
) -> Result<FieldElement>
) -> Result<MigrationOutput>

Check warning on line 214 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L214

Added line #L214 was not covered by tests
where
P: Provider + Sync + Send + 'static,
S: Signer + Sync + Send + 'static,
Expand All @@ -201,15 +221,15 @@

println!(" ");

let block_height = execute_strategy(ws, &strategy, account, txn_config)
let migration_output = execute_strategy(ws, &strategy, account, txn_config)

Check warning on line 224 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L224

Added line #L224 was not covered by tests
.await
.map_err(|e| anyhow!(e))
.with_context(|| "Problem trying to migrate.")?;

if let Some(block_height) = block_height {
if let Some(block_number) = migration_output.world_block_number {

Check warning on line 229 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L229

Added line #L229 was not covered by tests
ui.print(format!(
"\n🎉 Successfully migrated World on block #{} at address {}",
block_height,
block_number,

Check warning on line 232 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L232

Added line #L232 was not covered by tests
bold_message(format!(
"{:#x}",
strategy.world_address().expect("world address must exist")
Expand All @@ -225,7 +245,7 @@
));
}

strategy.world_address()
Ok(migration_output)

Check warning on line 248 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L248

Added line #L248 was not covered by tests
}

pub(crate) async fn setup_env(
Expand Down Expand Up @@ -357,19 +377,19 @@
Ok(migration)
}

// 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>(
ws: &Workspace<'_>,
strategy: &MigrationStrategy,
migrator: &SingleOwnerAccount<P, S>,
txn_config: Option<TransactionOptions>,
) -> Result<Option<u64>>
) -> Result<MigrationOutput>
where
P: Provider + Sync + Send + 'static,
S: Signer + Sync + Send + 'static,
{
let ui = ws.config().ui();
let mut world_tx_hash: Option<FieldElement> = None;
let mut world_block_number: Option<u64> = None;

match &strategy.base {
Some(base) => {
Expand Down Expand Up @@ -399,12 +419,20 @@
ui.print_header("# World");

let calldata = vec![strategy.base.as_ref().unwrap().diff.local];
deploy_contract(world, "world", calldata.clone(), migrator, &ui, &txn_config)
.await
.map_err(|e| {
ui.verbose(format!("{e:?}"));
anyhow!("Failed to deploy world: {e}")
})?;
let deploy_result =
deploy_contract(world, "world", calldata.clone(), migrator, &ui, &txn_config)
.await
.map_err(|e| {
ui.verbose(format!("{e:?}"));
anyhow!("Failed to deploy world: {e}")

Check warning on line 427 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L426-L427

Added lines #L426 - L427 were not covered by tests
})?;

(world_tx_hash, world_block_number) =
if let ContractDeploymentOutput::Output(deploy_result) = deploy_result {
(Some(deploy_result.transaction_hash), deploy_result.block_number)
} else {
(None, None)

Check warning on line 434 in bin/sozo/src/ops/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/migration/mod.rs#L434

Added line #L434 was not covered by tests
};

ui.print_sub(format!("Contract address: {:#x}", world.contract_address));

Expand Down Expand Up @@ -454,10 +482,11 @@
register_models(strategy, migrator, &ui, txn_config.clone()).await?;
deploy_contracts(strategy, migrator, &ui, txn_config).await?;

// This gets current block numder if helpful
// let block_height = migrator.provider().block_number().await.ok();

Ok(None)
Ok(MigrationOutput {
world_address: strategy.world_address()?,
world_tx_hash,
world_block_number,
})
}

enum ContractDeploymentOutput {
Expand Down
24 changes: 22 additions & 2 deletions crates/dojo-world/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@
pub abi: Option<Utf8PathBuf>,
#[serde_as(as = "Option<UfeHex>")]
pub address: Option<FieldElement>,
#[serde_as(as = "Option<UfeHex>")]
pub transaction_hash: Option<FieldElement>,
pub block_number: Option<u64>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
Expand All @@ -174,7 +177,13 @@
impl From<Manifest<Class>> for Manifest<Contract> {
fn from(value: Manifest<Class>) -> Self {
Manifest::new(
Contract { class_hash: value.inner.class_hash, abi: value.inner.abi, address: None },
Contract {
class_hash: value.inner.class_hash,
abi: value.inner.abi,
address: None,
transaction_hash: None,
block_number: None,
},
value.name,
)
}
Expand Down Expand Up @@ -286,6 +295,11 @@
Ok(manifest)
}

pub fn merge_from_previous(&mut self, previous: DeployedManifest) {
self.world.inner.transaction_hash = previous.world.inner.transaction_hash;
self.world.inner.block_number = previous.world.inner.block_number;
}

Check warning on line 301 in crates/dojo-world/src/manifest.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/manifest.rs#L298-L301

Added lines #L298 - L301 were not covered by tests

pub fn write_to_path(&self, path: &Utf8PathBuf) -> Result<()> {
fs::create_dir_all(path.parent().unwrap())?;

Expand Down Expand Up @@ -328,7 +342,13 @@
models,
contracts,
world: Manifest::new(
Contract { address: Some(world_address), class_hash: world_class_hash, abi: None },
Contract {
address: Some(world_address),
class_hash: world_class_hash,
abi: None,
transaction_hash: None,
block_number: None,
},
WORLD_CONTRACT_NAME.into(),
),
base: Manifest::new(
Expand Down
24 changes: 19 additions & 5 deletions crates/dojo-world/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use starknet::core::types::contract::{CompiledClass, SierraClass};
use starknet::core::types::{
BlockId, BlockTag, DeclareTransactionResult, FieldElement, FlattenedSierraClass, FunctionCall,
InvokeTransactionResult, StarknetError,
InvokeTransactionResult, MaybePendingTransactionReceipt, StarknetError, TransactionReceipt,
};
use starknet::core::utils::{
get_contract_address, get_selector_from_name, CairoShortStringToFeltError,
Expand All @@ -32,6 +32,7 @@
#[derive(Clone, Debug)]
pub struct DeployOutput {
pub transaction_hash: FieldElement,
pub block_number: Option<u64>,
pub contract_address: FieldElement,
pub declare: Option<DeclareOutput>,
}
Expand Down Expand Up @@ -200,9 +201,10 @@
let InvokeTransactionResult { transaction_hash } =
txn.send().await.map_err(MigrationError::Migrator)?;

TransactionWaiter::new(transaction_hash, account.provider()).await?;
let receipt = TransactionWaiter::new(transaction_hash, account.provider()).await?;
let block_number = get_block_number_from_receipt(receipt);

Ok(DeployOutput { transaction_hash, contract_address, declare })
Ok(DeployOutput { transaction_hash, block_number, contract_address, declare })
}

async fn deploy<P, S>(
Expand Down Expand Up @@ -264,9 +266,10 @@
let InvokeTransactionResult { transaction_hash } =
txn.send().await.map_err(MigrationError::Migrator)?;

TransactionWaiter::new(transaction_hash, account.provider()).await?;
let receipt = TransactionWaiter::new(transaction_hash, account.provider()).await?;
let block_number = get_block_number_from_receipt(receipt);

Ok(DeployOutput { transaction_hash, contract_address, declare })
Ok(DeployOutput { transaction_hash, block_number, contract_address, declare })
}

fn salt(&self) -> FieldElement;
Expand Down Expand Up @@ -298,3 +301,14 @@
let compiled_class: CompiledClass = serde_json::from_str(&res)?;
Ok(compiled_class.class_hash()?)
}

fn get_block_number_from_receipt(receipt: MaybePendingTransactionReceipt) -> Option<u64> {
match receipt {
MaybePendingTransactionReceipt::Receipt(receipt) => match receipt {
TransactionReceipt::Deploy(r) => Some(r.block_number),

Check warning on line 308 in crates/dojo-world/src/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/migration/mod.rs#L308

Added line #L308 was not covered by tests
TransactionReceipt::Invoke(r) => Some(r.block_number),
_ => None,

Check warning on line 310 in crates/dojo-world/src/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/migration/mod.rs#L310

Added line #L310 was not covered by tests
},
MaybePendingTransactionReceipt::PendingReceipt(_receipt) => None,

Check warning on line 312 in crates/dojo-world/src/migration/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/migration/mod.rs#L312

Added line #L312 was not covered by tests
}
}
9 changes: 1 addition & 8 deletions crates/dojo-world/src/migration/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ use starknet_crypto::{poseidon_hash_many, poseidon_hash_single};
use super::class::{ClassDiff, ClassMigration};
use super::contract::{ContractDiff, ContractMigration};
use super::world::WorldDiff;
use super::{DeployOutput, MigrationType, RegisterOutput};

#[derive(Debug)]
pub struct MigrationOutput {
pub world: Option<DeployOutput>,
pub contracts: Vec<DeployOutput>,
pub models: Option<RegisterOutput>,
}
use super::MigrationType;

#[derive(Debug)]
pub struct MigrationStrategy {
Expand Down
2 changes: 2 additions & 0 deletions examples/spawn-and-move/manifests/deployments/KATANA.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
kind = "Contract"
class_hash = "0x799bc4e9da10bfb3dd88e6f223c9cfbf7745435cd14f5d69675ea448e578cd"
address = "0x1385f25d20a724edc9c7b3bd9636c59af64cbaf9fcd12f33b3af96b2452f295"
transaction_hash = "0x6afefdcc49b3563a4f3657900ba71e9f9356861b15b942a73f2018f046a1048"
block_number = 3
name = "dojo::world::world"

[base]
Expand Down
Loading