Skip to content

Commit

Permalink
fix(sozo): return error if inspect doesn't find resource (#2920)
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm authored Jan 18, 2025
1 parent 30e574c commit 74ccf13
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ jobs:
/tmp/bins/sozo --manifest-path crates/dojo/core/Scarb.toml test
/tmp/bins/sozo --manifest-path crates/dojo/core-cairo-test/Scarb.toml test
dojo-spawn-and-move-example-test:
dojo-examples-test:
needs: build
runs-on: ubuntu-latest
steps:
Expand All @@ -154,6 +154,9 @@ jobs:
- uses: actions/checkout@v3
- run: |
chmod +x /tmp/bins/sozo
/tmp/bins/sozo --manifest-path examples/spawn-and-move/Scarb.toml build
/tmp/bins/sozo --manifest-path examples/spawn-and-move/Scarb.toml inspect ns-Flatbow
/tmp/bins/sozo --manifest-path examples/spawn-and-move/Scarb.toml inspect ns-RiverSkale
/tmp/bins/sozo --manifest-path examples/spawn-and-move/Scarb.toml test
/tmp/bins/sozo --manifest-path examples/simple/Scarb.toml test
Expand Down
8 changes: 4 additions & 4 deletions bin/sozo/src/commands/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl InspectArgs {
utils::get_world_diff_and_provider(starknet.clone(), world, &ws).await?;

if let Some(resource) = resource {
inspect_resource(&resource, &world_diff);
inspect_resource(&resource, &world_diff)?;
} else {
inspect_world(&world_diff);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ struct GranteeDisplay {
}

/// Inspects a resource.
fn inspect_resource(resource_name_or_tag: &str, world_diff: &WorldDiff) {
fn inspect_resource(resource_name_or_tag: &str, world_diff: &WorldDiff) -> Result<()> {
let selector = if naming::is_valid_tag(resource_name_or_tag) {
naming::compute_selector_from_tag(resource_name_or_tag)
} else {
Expand All @@ -175,8 +175,7 @@ fn inspect_resource(resource_name_or_tag: &str, world_diff: &WorldDiff) {
let resource_diff = world_diff.resources.get(&selector);

if resource_diff.is_none() {
println!("Resource not found locally.");
return;
return Err(anyhow::anyhow!("Resource not found locally."));
}

let resource_diff = resource_diff.unwrap();
Expand Down Expand Up @@ -243,6 +242,7 @@ fn inspect_resource(resource_name_or_tag: &str, world_diff: &WorldDiff) {

print_table(&writers_disp, Some(Color::FG_BRIGHT_CYAN), Some("\n> Writers"));
print_table(&owners_disp, Some(Color::FG_BRIGHT_MAGENTA), Some("\n> Owners"));
Ok(())
}

/// Inspects the whole world.
Expand Down
13 changes: 9 additions & 4 deletions bin/sozo/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use starknet::core::types::Felt;
use starknet::core::utils as snutils;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::{JsonRpcClient, Provider};
use tracing::{error, trace};
use tracing::{trace, warn};

use crate::commands::options::account::{AccountOptions, SozoAccount};
use crate::commands::options::starknet::StarknetOptions;
Expand Down Expand Up @@ -133,10 +133,15 @@ pub async fn get_world_diff_and_provider(

let (provider, rpc_url) = starknet.provider(env)?;
let provider = Arc::new(provider);
if let Err(e) = provider_utils::health_check_provider(provider.clone()).await {
error!(target: LOG_TARGET,"Provider health check failed during sozo inspect.");
return Err(e);
if (provider_utils::health_check_provider(provider.clone()).await).is_err() {
warn!(target: LOG_TARGET, "Provider health check failed during sozo inspect, inspecting locally and all resources will appeared as `Created`. Remote resources will not be fetched.");
return Ok((
WorldDiff::from_local(world_local)?,
Arc::try_unwrap(provider).map_err(|_| anyhow!("Failed to unwrap Arc"))?,
rpc_url,
));
}

let provider = Arc::try_unwrap(provider).map_err(|_| anyhow!("Failed to unwrap Arc"))?;
trace!(?provider, "Provider initialized.");

Expand Down
2 changes: 1 addition & 1 deletion crates/dojo/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ mod tests {
gather_dojo_data(&config.manifest_path().to_path_buf(), "ns", "dev", skip_migrations)
.expect("Failed to gather dojo data");

assert_eq!(data.models.len(), 6);
assert_eq!(data.models.len(), 8);

assert_eq!(data.world.name, "ns");

Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async fn test_load_from_remote(sequencer: &RunnerCtx) {

let _block_timestamp = 1710754478_u64;
let models = sqlx::query("SELECT * FROM models").fetch_all(&pool).await.unwrap();
assert_eq!(models.len(), 8);
assert_eq!(models.len(), 10);

let (id, name, namespace, packed_size, unpacked_size): (String, String, String, u8, u8) =
sqlx::query_as(
Expand Down
4 changes: 2 additions & 2 deletions examples/game-lib/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ dependencies = [

[[package]]
name = "dojo"
version = "1.0.0-rc.0"
version = "1.0.12"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "2.9.1"
version = "2.9.2"
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2024_07"
sierra-replace-ids = true

[[target.starknet-contract]]
build-external-contracts = [ "dojo::world::world_contract::world" ]
build-external-contracts = [ "dojo::world::world_contract::world", "armory::m_Flatbow", "bestiary::m_RiverSkale" ]

[dependencies]
armory = { path = "../game-lib/armory" }
Expand Down
38 changes: 38 additions & 0 deletions examples/spawn-and-move/src/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ mod tests {
};

use super::{actions, IActionsDispatcher, IActionsDispatcherTrait};
use crate::dungeon::dungeon;
use dojo_examples::models::{Position, PositionValue, m_Position, Moves, m_Moves, Direction};

fn namespace_def() -> NamespaceDef {
Expand Down Expand Up @@ -321,4 +322,41 @@ mod tests {
assert(new_position.vec.x == initial_position.vec.x + 1, 'position x is wrong');
assert(new_position.vec.y == initial_position.vec.y, 'position y is wrong');
}

#[test]
#[available_gas(30000000)]
#[cfg(feature: 'dungeon')]
fn test_feature_dungeon() {
let ndef = NamespaceDef {
namespace: "ns",
resources: [
TestResource::Model(armory::m_Flatbow::TEST_CLASS_HASH),
TestResource::Model(bestiary::m_RiverSkale::TEST_CLASS_HASH),
TestResource::Contract(actions::TEST_CLASS_HASH),
TestResource::Contract(dungeon::TEST_CLASS_HASH),
]
.span(),
};

let contract_defs = [
ContractDefTrait::new(@"ns", @"actions")
.with_writer_of([dojo::utils::bytearray_hash(@"ns")].span()),
ContractDefTrait::new(@"ns", @"dungeon")
.with_writer_of([dojo::utils::bytearray_hash(@"ns")].span()),
]
.span();

let mut world = spawn_test_world([ndef].span());
world.sync_perms_and_inits(contract_defs);

let other = starknet::contract_address_const::<0x1234>();
starknet::testing::set_contract_address(other);

let (dungeon_addr, _) = world.dns(@"dungeon").unwrap();

let (actions_system_addr, _) = world.dns(@"actions").unwrap();
let actions_system = IActionsDispatcher { contract_address: actions_system_addr };

actions_system.enter_dungeon(dungeon_addr);
}
}
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 74ccf13

Please sign in to comment.