Skip to content

Commit

Permalink
Merge pull request #9 from sqrlfirst/feat/add-invariant-tests
Browse files Browse the repository at this point in the history
Add missing test for project invariants
  • Loading branch information
ametel01 authored Jun 11, 2024
2 parents 9ddb131 + 9c83c57 commit 58d3ac1
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 2 deletions.
39 changes: 39 additions & 0 deletions starknet/tests/test_fact_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ fn prove_account_test_success_save_all() {
assert_eq!(nonce, proof.nonce);
}

#[test]
#[should_panic(expected: "FactRegistry: block state root not found")]
fn prove_account_test_fail_state_root_not_found() {
let dsp = setup();

let block = proofs::blocks::BLOCK_3();

let proof = proofs::account::PROOF_1();

dsp
.registry
.prove_account(OptionsSet::All, proof.address, block.number, proof.bytes, proof.data);
}

#[test]
fn prove_storage_test_success_with_some_data() {
let dsp = setup();
Expand Down Expand Up @@ -151,6 +165,31 @@ fn prove_storage_test_success_with_some_data() {
);
}

#[test]
#[should_panic(expected: "FactRegistry: block state root not found")]
fn prove_storage_test_fail_state_root_not_found() {
let dsp = setup();

let block = proofs::blocks::BLOCK_3();

start_cheat_caller_address(dsp.store.contract_address, ADMIN());
dsp.store.store_state_root(block.number, block.state_root);

let account_proof = proofs::account::PROOF_1();

let storage_proof = proofs::storage::PROOF_2();

let _ = dsp
.registry
.prove_storage(
block.number,
account_proof.address,
storage_proof.key,
storage_proof.bytes,
storage_proof.data
);
}

#[test]
fn test_get_storage_not_verified() {
let dsp = setup();
Expand Down
99 changes: 97 additions & 2 deletions starknet/tests/test_l1_header_store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,40 @@ fn receive_from_l1_success_test() {
}

#[test]
fn test_store_state_root() {
#[should_panic(expected: "L1HeaderStore: unauthorized caller")]
fn receive_from_l1_fail_unauthorized_sender() {
let dsp = setup();

let block = proofs::blocks::BLOCK_0();
let parent_hash: u256 = 0xfbacb363819451babc6e7596aa48af6c223e40e8b0ad975e372347df5d60ba0f;

dsp.store.receive_from_l1(parent_hash, block.number);

assert_eq!(dsp.store.get_parent_hash(block.number), parent_hash);
}

#[test]
fn change_l1_messages_origin_success() {
let dsp = setup();

let new_origin: starknet::ContractAddress = 'NEW_L1_MESSAGES'.try_into().unwrap();
start_cheat_caller_address(dsp.store.contract_address, ADMIN());
dsp.store.change_l1_messages_origin(new_origin);
}

#[test]
#[should_panic(expected: 'Caller is not the owner')]
fn change_l1_messages_origin_fail_unauthorized_sender() {
let dsp = setup();

let new_origin: starknet::ContractAddress = 'NEW_L1_MESSAGES'.try_into().unwrap();

dsp.store.change_l1_messages_origin(new_origin);
}


#[test]
fn test_store_state_root_success() {
let dsp = setup();

let state_root: u256 = 0x1e7f7;
Expand All @@ -34,7 +67,33 @@ fn test_store_state_root() {
}

#[test]
fn test_store_many_state_root() {
#[should_panic(expected: 'Caller is not the owner')]
fn test_store_state_root_fail_call_not_by_owner() {
let dsp = setup();

let state_root: u256 = 0x1e7f7;
let block = 19882;

dsp.store.store_state_root(block, state_root);
}

#[test]
#[should_panic(expected: "L1HeaderStore: state root already exists")]
fn test_store_state_root_fail_block_number_is_not_zero() {
let dsp = setup();

let state_root: u256 = 0x1e7f7;
let block = 19882;

start_cheat_caller_address(dsp.store.contract_address, ADMIN());
dsp.store.store_state_root(block, state_root);

start_cheat_caller_address(dsp.store.contract_address, ADMIN());
dsp.store.store_state_root(block, state_root);
}

#[test]
fn test_store_many_state_root_success() {
let dsp = setup();

let state_roots: Array<u256> = array![
Expand All @@ -51,3 +110,39 @@ fn test_store_many_state_root() {
assert_eq!(dsp.store.get_state_root(10), 0x11e7f7);
}

#[test]
#[should_panic(expected: 'Caller is not the owner')]
fn test_store_many_state_root_fail_called_not_by_owner() {
let dsp = setup();

let state_roots: Array<u256> = array![
0x1e7f7, 0x3e497, 0x4e7f7, 0x5e7f7, 0x6e7f7, 0x7e7f7, 0x8e7f7, 0x9e7f7, 0x10e7f7, 0x11e7f7
];
let start_block = 1;
let end_block = 10;

dsp.store.store_many_state_roots(start_block, end_block, state_roots);

assert_eq!(dsp.store.get_state_root(1), 0x1e7f7);
assert_eq!(dsp.store.get_state_root(5), 0x6e7f7);
assert_eq!(dsp.store.get_state_root(10), 0x11e7f7);
}

#[test]
#[should_panic(expected: "L1HeaderStore: invalid state roots length")]
fn test_store_many_state_root_fail_invailed_state_root_length() {
let dsp = setup();

let state_roots: Array<u256> = array![
0x1e7f7, 0x3e497, 0x4e7f7, 0x5e7f7, 0x6e7f7, 0x7e7f7, 0x8e7f7, 0x9e7f7, 0x10e7f7, 0x11e7f7
];
let start_block = 1;
let end_block = 9;

start_cheat_caller_address(dsp.store.contract_address, ADMIN());
dsp.store.store_many_state_roots(start_block, end_block, state_roots);

assert_eq!(dsp.store.get_state_root(1), 0x1e7f7);
assert_eq!(dsp.store.get_state_root(5), 0x6e7f7);
assert_eq!(dsp.store.get_state_root(10), 0x11e7f7);
}
25 changes: 25 additions & 0 deletions starknet/tests/test_l1_messages_proxy.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,28 @@ fn get_l1_headers_store_address_test() {

assert_eq!(dsp.proxy.get_l1_headers_store_address(), dsp.store.contract_address);
}

#[test]
fn change_contract_addresses_success() {
let dsp = setup();

let new_l1_headers_store_address: starknet::ContractAddress = 'NEW_L1_HEADER_STORE_ADDRESS'
.try_into()
.unwrap();

start_cheat_caller_address(dsp.proxy.contract_address, OWNER());
dsp.proxy.change_contract_addresses(L1_ORIGIN(), new_l1_headers_store_address);
}

#[test]
#[should_panic(expected: 'Caller is not the owner')]
fn change_contract_addresses_fail_calle_not_by_owner() {
let dsp = setup();

let new_l1_headers_store_address: starknet::ContractAddress = 'NEW_L1_HEADER_STORE_ADDRESS'
.try_into()
.unwrap();

dsp.proxy.change_contract_addresses(L1_ORIGIN(), new_l1_headers_store_address);
}

0 comments on commit 58d3ac1

Please sign in to comment.