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

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiagoPittella committed Jul 17, 2023
1 parent 5227390 commit 5e4a64f
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,13 +1004,21 @@ mod test {
fn test_declare_v2_with_invalid_compiled_class_hash() {
let (block_context, mut state) = create_account_tx_test_state().unwrap();
let mut declare_v2 = declarev2_tx();
declare_v2.compiled_class_hash = Felt252::from(1);
let real_casm_class_hash = declare_v2.compiled_class_hash;
let wrong_casm_class_hash = Felt252::from(1);
declare_v2.compiled_class_hash = wrong_casm_class_hash.clone();
let declare_tx = Transaction::DeclareV2(Box::new(declare_v2));

let err = declare_tx
.execute(&mut state, &block_context, INITIAL_GAS_COST)
.unwrap_err();

assert_eq!(err.to_string(), "Invalid compiled class, expected class hash: \"1948962768849191111780391610229754715773924969841143100991524171924131413970\", but received: \"1\"".to_string());
assert_eq!(
err.to_string(),
format!(
"Invalid compiled class, expected class hash: {}, but received: {}",
real_casm_class_hash, wrong_casm_class_hash
)
);
}
}
129 changes: 129 additions & 0 deletions src/transaction/declare_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,133 @@ mod tests {

assert_eq!(expected_casm_class, casm_class);
}

#[test]
fn create_declare_v2_with_casm_contract_class_none_test() {
// read file to create sierra contract class
let version;
let path;
#[cfg(not(feature = "cairo_1_tests"))]
{
version = Felt252::from(2);
path = PathBuf::from("starknet_programs/cairo2/fibonacci.sierra");
}

#[cfg(feature = "cairo_1_tests")]
{
version = Felt252::from(1);
path = PathBuf::from("starknet_programs/cairo1/fibonacci.sierra");
}

let file = File::open(path).unwrap();
let reader = BufReader::new(file);
let sierra_contract_class: cairo_lang_starknet::contract_class::ContractClass =
serde_json::from_reader(reader).unwrap();
let sender_address = Address(1.into());
let casm_class =
CasmContractClass::from_contract_class(sierra_contract_class.clone(), true).unwrap();
let casm_class_hash = compute_casm_class_hash(&casm_class).unwrap();

// create internal declare v2

let internal_declare = DeclareV2::new_with_tx_hash(
&sierra_contract_class,
None,
casm_class_hash,
sender_address,
0,
version,
[1.into()].to_vec(),
Felt252::zero(),
Felt252::one(),
)
.unwrap();

// crate state to store casm contract class
let casm_contract_class_cache = HashMap::new();
let state_reader = InMemoryStateReader::default();
let mut state = CachedState::new(state_reader, None, Some(casm_contract_class_cache));

// call compile and store
assert!(internal_declare
.compile_and_store_casm_class(&mut state)
.is_ok());

// test we can retreive the data
let expected_casm_class = CasmContractClass::from_contract_class(
internal_declare.sierra_contract_class.clone(),
true,
)
.unwrap();

let casm_class = match state
.get_contract_class(&internal_declare.compiled_class_hash.to_be_bytes())
.unwrap()
{
CompiledClass::Casm(casm) => *casm,
_ => unreachable!(),
};

assert_eq!(expected_casm_class, casm_class);
}

#[test]
fn create_declare_v2_wrong_casm_class_hash_test() {
// read file to create sierra contract class
let version;
let path;
#[cfg(not(feature = "cairo_1_tests"))]
{
version = Felt252::from(2);
path = PathBuf::from("starknet_programs/cairo2/fibonacci.sierra");
}

#[cfg(feature = "cairo_1_tests")]
{
version = Felt252::from(1);
path = PathBuf::from("starknet_programs/cairo1/fibonacci.sierra");
}

let file = File::open(path).unwrap();
let reader = BufReader::new(file);
let sierra_contract_class: cairo_lang_starknet::contract_class::ContractClass =
serde_json::from_reader(reader).unwrap();
let sender_address = Address(1.into());
let casm_class =
CasmContractClass::from_contract_class(sierra_contract_class.clone(), true).unwrap();
let casm_class_hash = compute_casm_class_hash(&casm_class).unwrap();

let sended_class_hash = Felt252::from(5);
// create internal declare v2

let internal_declare = DeclareV2::new_with_tx_hash(
&sierra_contract_class,
None,
sended_class_hash.clone(),
sender_address,
0,
version,
[1.into()].to_vec(),
Felt252::zero(),
Felt252::one(),
)
.unwrap();

// crate state to store casm contract class
let casm_contract_class_cache = HashMap::new();
let state_reader = InMemoryStateReader::default();
let mut state = CachedState::new(state_reader, None, Some(casm_contract_class_cache));

let expected_err = format!(
"Invalid compiled class, expected class hash: {}, but received: {}",
casm_class_hash, sended_class_hash
);
assert_eq!(
internal_declare
.compile_and_store_casm_class(&mut state)
.unwrap_err()
.to_string(),
expected_err
);
}
}
2 changes: 1 addition & 1 deletion src/transaction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,6 @@ pub enum TransactionError {
CallInfoIsNone,
#[error("Unsupported version {0:?}")]
UnsupportedVersion(String),
#[error("Invalid compiled class, expected class hash: {0:?}, but received: {1:?}")]
#[error("Invalid compiled class, expected class hash: {0}, but received: {1}")]
InvalidCompiledClassHash(String, String),
}

0 comments on commit 5e4a64f

Please # to comment.