starknet_in_rust icon indicating copy to clipboard operation
starknet_in_rust copied to clipboard

Compiled class hash not found in `CachedState` after executing `Declare` tx

Open kariy opened this issue 1 year ago • 2 comments

If you try to get the compiled class hash after executing a declare tx, you will get a NoneCompiledHash error. This is what i try to do in the contract_execution example:

   //* --------------------------------------------
    //*        Declare new contract class
    //* --------------------------------------------
    let declare_tx = Declare::new_with_tx_hash(
        &sierra_contract_class,
        Some(casm_class),
        compiled_class_hash,
        account_contract_address.clone(),
        Default::default(), // max fee
        2.into(),
        signature.clone(),
        1.into(), // nonce
        // Value hardcoded to pass signature validation
        2718.into(),
    )
    .expect("couldn't create declare transaction");

    let class_hash = declare_tx.sierra_class_hash;

    declare_tx
        .execute(
            &mut state,
            &block_context,
            #[cfg(feature = "cairo-native")]
            None,
        )
        .expect("could not declare the contract class");

    // This will return `NoneCompiledHash` error
    let actual_compiled_class_hash = state
        .get_compiled_class_hash(&(class_hash.into()))
        .expect("should have compiled class hash");

this probably what caused it, here it sets the compiled hash in cache.compiled_hash_writes:

https://github.com/lambdaclass/starknet_in_rust/blob/370d406787626d660f6437463c67be7a6b09d2e5/src/state/cached_state.rs#L306-L318

but the get_compiled_class_hash() implementation doesn't read from it, but instead from cache.class_hash_to_compiled_class_hash:

https://github.com/lambdaclass/starknet_in_rust/blob/370d406787626d660f6437463c67be7a6b09d2e5/src/state/cached_state.rs#L177-L185

if i replace line 179 above with self.cache.get_compiled_class_hash(class_hash), which reads from the compiled_class_hash_writes field, the issue is fixed.

kariy avatar Mar 07 '24 15:03 kariy

Thanks @kariy !!! I will take a look a it

pefontana avatar Mar 07 '24 20:03 pefontana

@pefontana any updates on this? I don't mind opening a PR for this.

kariy avatar Mar 15 '24 14:03 kariy