dojoengine / dojo

Dojo is a toolchain for building provable games and autonomous worlds with Cairo
https://dojoengine.org
Apache License 2.0
407 stars 165 forks source link

Panic on running sozo test when codes embed if else with loop #1747

Open zsluedem opened 5 months ago

zsluedem commented 5 months ago

Describe the bug A clear and concise description of what the bug is. panic with

Difference in FunctionId { id: 1692, debug_name: Some("dojo_starter::test_mod::Private::get_building_area_info[expr215]") }: Some(OrderedHashMap({Const: 27610})) != Some(OrderedHashMap({Const: 2060})).
Difference in FunctionId { id: 1692, debug_name: Some("dojo_starter::test_mod::Private::get_building_area_info[expr215]") }: Some(OrderedHashMap({Const: 27610})) != Some(OrderedHashMap({Const: 2060})).
thread 'main' panicked at /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cairo-lang-sierra-gas-2.5.4/src/gas_info.rs:97:9:
Comparison failed.
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: cairo_lang_sierra_gas::gas_info::GasInfo::assert_eq_functions
   3: cairo_lang_sierra_to_casm::metadata::calc_metadata
   4: cairo_lang_runner::SierraCasmRunner::new
   5: cairo_lang_test_runner::run_tests
   6: cairo_lang_test_runner::CompiledTestRunner::run
   7: sozo::commands::test::TestArgs::run
   8: sozo::commands::run
   9: sozo::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

To Reproduce Steps to reproduce the behavior:

use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use test_mod::IKingdomLordAdminDispatcherImpl;

#[dojo::contract]
mod test_mod {
    #[derive(Model, Copy, Drop, Serde)]
    struct BuildingAreaInfo {
        #[key]
        building_id: u64,
        building_kind: u64,
        a: bool
    }

    #[derive(Model, Copy, Drop, Serde)]
    struct Item {
        #[key]
        building_id: u64,
        building_kind: u64,
        go: bool
    }

    #[starknet::interface]
    trait IKingdomLordAdmin<TState> {
        fn get_building_area_info(self: @TState, building_id: u64);
    }

    #[abi(embed_v0)]
    impl Private of IKingdomLordAdmin<ContractState> {
        fn get_building_area_info(self: @ContractState, building_id: u64) {
            let world = self.world_dispatcher.read();
            let mut b = get!(world, (1_u64), (BuildingAreaInfo));

            if b.a{
                b.building_kind = 1;
                set!(world, (b));
            } else{
                let mut index = 1;
                loop{
                    if index == 10{
                        break;
                    }
                    let mut c = get!(world, (index), (Item));
                    if c.go{
                        c.building_kind = b.building_kind;
                        break;
                    }
                    index += 1;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests{
    use dojo::test_utils::{spawn_test_world, deploy_contract};
    use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
    use super::test_mod;
    use super::IKingdomLordAdminDispatcherImpl;
    #[test]
    #[available_gas(300000000000)]
    fn test_default(){
        let mut models = array![
            test_mod::building_area_info::TEST_CLASS_HASH,
            test_mod::item::TEST_CLASS_HASH,
        ];
        // deploy world with models
        let world = spawn_test_world(models);
        // deploy systems contract
        let contract_address = world
            .deploy_contract('salt1', test_mod::TEST_CLASS_HASH.try_into().unwrap());

        let contract = test_mod::IKingdomLordAdminDispatcher{contract_address};
        contract.get_building_area_info(1);
    }
}

Expected behavior A clear and concise description of what you expected to happen.

Test should run normally

Screenshots If applicable, add screenshots to help explain your problem.

None

Additional context Add any other context about the problem here.

glihm commented 5 months ago

@zsluedem thank you for the report!

  1. Which version of sozo is this?
  2. Do you have this error at test time only?
zsluedem commented 5 months ago

@zsluedem thank you for the report!

  1. Which version of sozo is this?
  2. Do you have this error at test time only?
  1. got the error in sozo v0.6.0-alpha.10 and I met this kind of errors in sozo 0.5.4,too.
  2. Yes. In tests only. sozo build is fine
glihm commented 5 months ago

Thanks @zsluedem for the report. Have a solution on that, due to the Sierra code generation around the loop and if statement. Will check some test suite around that and open a PR to push the fix.