Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Compiled class hash not found in CachedState after executing Declare tx #1246

Open
@kariy

Description

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:

fn set_compiled_class_hash(
&mut self,
class_hash: &Felt252,
compiled_class_hash: &Felt252,
) -> Result<(), StateError> {
let class_hash = ClassHash::from(*class_hash);
let compiled_class_hash = ClassHash::from(*compiled_class_hash);
self.cache
.compiled_class_hash_writes
.insert(class_hash, compiled_class_hash);
Ok(())
}

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

fn get_compiled_class_hash(&self, class_hash: &ClassHash) -> Result<ClassHash, StateError> {
if let Some(compiled_class_hash) =
self.cache.class_hash_to_compiled_class_hash.get(class_hash)
{
Ok(*compiled_class_hash)
} else {
self.state_reader.get_compiled_class_hash(class_hash)
}
}

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions