Skip to content

Commit

Permalink
fix: ensure contract initialization works as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Nov 1, 2024
1 parent 6cc7c33 commit 28a4f10
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/dojo/world/src/remote/events_to_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl WorldRemote {
resource.push_class_hash(e.class_hash.into());
}
WorldEvent::ContractInitialized(e) => {
// Unwrap is safe bcause the contract must exist in the world.
// Unwrap is safe because the contract must exist in the world.
let resource = self.resources.get_mut(&e.selector).unwrap();
let contract = resource.as_contract_mut()?;
contract.is_initialized = true;
Expand Down
10 changes: 4 additions & 6 deletions crates/sozo/ops/src/migrate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,15 @@ where

let has_changed = !invoker.calls.is_empty();

if !ordered_init_calls.is_empty() {
if !invoker.calls.is_empty() {
if self.do_multicall() {
let ui_text = format!("Initializing {} contracts...", ordered_init_calls.len());
let ui_text = format!("Initializing {} contracts...", invoker.calls.len());
ui.update_text_boxed(ui_text);

invoker.multicall().await?;
} else {
let ui_text = format!(
"Initializing {} contracts (sequentially)...",
ordered_init_calls.len()
);
let ui_text =
format!("Initializing {} contracts (sequentially)...", invoker.calls.len());
ui.update_text_boxed(ui_text);

invoker.invoke_all_sequentially().await?;
Expand Down
4 changes: 2 additions & 2 deletions crates/sozo/ops/src/migration_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ impl MigrationUi {
if let Some(text) = text {
let frames = spinner!(["⛩️ ", "🎃", "👻", "🧟", "💀"], 500);
let spinner = Spinner::new(frames.clone(), text, None);
return Self { spinner, default_frames: frames, silent: false };
Self { spinner, default_frames: frames, silent: false }
} else {
let frames = spinner!([""], 5000);
let spinner = Spinner::new(frames.clone(), "", None);
return Self { spinner, default_frames: frames, silent: false };
Self { spinner, default_frames: frames, silent: false }
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/sozo/ops/src/tests/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ async fn migrate_from_local(sequencer: &RunnerCtx) {
#[katana_runner::test(accounts = 10, db_dir = copy_spawn_and_move_db().as_str())]
async fn migrate_no_change(sequencer: &RunnerCtx) {
let MigrationResult { manifest, has_changes } = migrate_spawn_and_move(sequencer).await;
println!("has changes: {:?}", has_changes);
assert!(!has_changes);
assert_eq!(manifest.contracts.len(), 4);
}
12 changes: 6 additions & 6 deletions examples/spawn-and-move/manifest_dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -1786,8 +1786,8 @@
"systems": []
},
{
"address": "0x79e0653fbebdbdb864ca69d1470b263f2efdfce9cf355cfe9c7719627eff792",
"class_hash": "0x3356ff99134a997be6f9366c47ad18993aac3e29803819cc80309a57fa23f88",
"address": "0x200cd8070a7dbe0894c74426d2f4c9d778b13042ce955203cb189d85cfb43d1",
"class_hash": "0x4f290c42a0a2e342bf8da74103a31c6e3f2ebca2c72054da9d6d24754c3cc7e",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -1991,13 +1991,13 @@
"events": [
{
"members": [],
"class_hash": "0x37920709310be24ce9be9993d3f17b632a080eef9d4383b03fe251189696cc2",
"tag": "ContractInitialized"
"class_hash": "0x22c33f3c7e525a21cd3651326f74c61deff6d75e26e06725f53ac2dfd4646fa",
"tag": "Moved"
},
{
"members": [],
"class_hash": "0x22c33f3c7e525a21cd3651326f74c61deff6d75e26e06725f53ac2dfd4646fa",
"tag": "Moved"
"class_hash": "0x2c78ad5ec05d05be24a1cbace308ad15bff9375b56be8a8fa892ff5fc79eb23",
"tag": "MyInit"
}
]
}
4 changes: 2 additions & 2 deletions examples/spawn-and-move/src/others.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod others {

#[derive(Copy, Drop, Serde)]
#[dojo::event]
struct ContractInitialized {
struct MyInit {
#[key]
caller: ContractAddress,
value: u8,
Expand All @@ -14,6 +14,6 @@ pub mod others {
fn dojo_init(self: @ContractState, value: u8) {
let mut world = self.world(@"ns");

world.emit_event(@ContractInitialized { caller: get_caller_address(), value });
world.emit_event(@MyInit { caller: get_caller_address(), value });
}
}
2 changes: 1 addition & 1 deletion scripts/rebuild_test_artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ cargo +nightly-2024-08-28 fmt --all -- "$@"
./target/release/sozo build --manifest-path crates/torii/types-test/Scarb.toml

# Generates the database for testing by migrating the spawn and move example.
cargo generate-test-db
KATANA_RUNNER_BIN=./target/release/katana cargo generate-test-db
# Ensure the user has locally the db dir in /tmp.
rm -rf /tmp/spawn-and-move-db
rm -rf /tmp/types-test-db
Expand Down
Binary file modified spawn-and-move-db.tar.gz
Binary file not shown.
Binary file modified types-test-db.tar.gz
Binary file not shown.

0 comments on commit 28a4f10

Please sign in to comment.