Skip to content

Commit

Permalink
tests: added tests for fact_registry + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed May 17, 2024
1 parent ba7a106 commit ae041b8
Show file tree
Hide file tree
Showing 8 changed files with 2,538 additions and 66 deletions.
2 changes: 1 addition & 1 deletion starknet/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ pub mod testing {
pub mod blocks;
pub mod storage;
}
}
}
6 changes: 4 additions & 2 deletions starknet/src/library/trie_proof.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub fn verify_proof(
root_hash.values == split_u256_to_u64_array(EMPTY_TRIE_ROOT_HASH),
"Empty proof must have empty trie root hash"
);
return Option::Some(Words64Sequence { values: array![].span(), len_bytes: 0 });
// return Option::Some(Words64Sequence { values: array![].span(), len_bytes: 0 });
return Option::None;
}

let mut next_hash = Words64Sequence { values: array![].span(), len_bytes: 0 };
Expand Down Expand Up @@ -88,7 +89,8 @@ pub fn verify_proof(
let node_children = extract_nibble(path, path_offset).try_into().unwrap();
let children = *node.at(node_children);
assert!(children.length == 0, "Invalid children length");
out = Option::Some(Words64Sequence { values: array![].span(), len_bytes: 0 });
// out = Option::Some(Words64Sequence { values: array![].span(), len_bytes: 0 });
out = Option::None;
break;
}
} else {
Expand Down
11 changes: 9 additions & 2 deletions starknet/src/library/words64_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,21 @@ pub fn words64_to_int(input: Words64Sequence) -> u256 {
let mut i = 0;

while i < num_full_words {
result = BitOr::bitor(result, BitShift::shl((*input.values.at(i)).into(), ((num_full_words - i - 1) * 64).into()));
result =
BitOr::bitor(
result,
BitShift::shl((*input.values.at(i)).into(), ((num_full_words - i - 1) * 64).into())
);
i += 1;
};

if remaining_bytes > 0 {
let last_word = *input.values.at(num_full_words);
let mask = (BitShift::shl(1_u64, (remaining_bytes * 8).into()) - 1).into();
result = BitOr::bitor(result, BitShift::shl((last_word & mask).into(), (num_full_words * 64).into()));
result =
BitOr::bitor(
result, BitShift::shl((last_word & mask).into(), (num_full_words * 64).into())
);
}

result
Expand Down
1,347 changes: 1,344 additions & 3 deletions starknet/src/testing/proofs/account.cairo

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion starknet/src/testing/proofs/blocks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ pub fn BLOCK_3() -> Block {
number: 13843670,
state_root: 0x2045bf4ea5561e88a4d0d9afbc316354e49fe892ac7e961a5e68f1f4b9561152,
}
}
}
1,003 changes: 999 additions & 4 deletions starknet/src/testing/proofs/storage.cairo

Large diffs are not rendered by default.

232 changes: 180 additions & 52 deletions starknet/tests/test_fact_registry.cairo
Original file line number Diff line number Diff line change
@@ -1,82 +1,106 @@
use fossil::library::words64_utils::Words64Trait;
use fossil::testing::proofs;
use fossil::types::OptionsSet;
use fossil::{
L1_headers_store::interface::IL1HeadersStoreDispatcherTrait,
fact_registry::interface::IFactRegistryDispatcherTrait
};
use super::test_utils::{setup, testdata};
use super::test_utils::setup;

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

let block = testdata::blocks::BLOCK_3();
let block = proofs::blocks::BLOCK_3();
dsp.store.set_state_root(block.number, block.state_root);

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

dsp
.registry
.prove_account(
OptionsSet::CodeHash, proof.account, block.number, proof.len_bytes, proof.data
);
.prove_account(OptionsSet::CodeHash, proof.address, block.number, proof.bytes, proof.data);

let code_hash = dsp.registry.get_verified_account_code_hash(proof.account, block.number);
assert_eq!(code_hash, 0x4e36f96ee1667a663dfaac57c4d185a0e369a3a217e0079d49620f34f85d1ac7);
let code_hash = dsp.registry.get_verified_account_code_hash(proof.address, block.number);
assert_eq!(code_hash, proof.code_hash);
}

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

let block = testdata::blocks::BLOCK_3();
let block = proofs::blocks::BLOCK_3();
dsp.store.set_state_root(block.number, block.state_root);

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

dsp
.registry
.prove_account(
OptionsSet::Balance, proof.account, block.number, proof.len_bytes, proof.data
);
.prove_account(OptionsSet::Balance, proof.address, block.number, proof.bytes, proof.data);

let balance = dsp.registry.get_verified_account_balance(proof.account, block.number);
assert_eq!(balance, 0x0);
let balance = dsp.registry.get_verified_account_balance(proof.address, block.number);
assert_eq!(balance, proof.balance);
}

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

let block = testdata::blocks::BLOCK_3();
let block = proofs::blocks::BLOCK_3();
dsp.store.set_state_root(block.number, block.state_root);

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

dsp
.registry
.prove_account(OptionsSet::Nonce, proof.account, block.number, proof.len_bytes, proof.data);
.prove_account(OptionsSet::Nonce, proof.address, block.number, proof.bytes, proof.data);

let nonce = dsp.registry.get_verified_account_nonce(proof.account, block.number);
assert_eq!(nonce, 0x1);
let nonce = dsp.registry.get_verified_account_nonce(proof.address, block.number);
assert_eq!(nonce, proof.nonce);
}

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

let block = testdata::blocks::BLOCK_3();
let block = proofs::blocks::BLOCK_3();
dsp.store.set_state_root(block.number, block.state_root);

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

dsp
.registry
.prove_account(
OptionsSet::StorageHash, proof.account, block.number, proof.len_bytes, proof.data
OptionsSet::StorageHash, proof.address, block.number, proof.bytes, proof.data
);

let storage_hash = dsp.registry.get_verified_account_storage_hash(proof.account, block.number);
assert_eq!(storage_hash, 0x199c2e6b850bcc9beaea25bf1bacc5741a7aad954d28af9b23f4b53f5404937b);
let storage_hash = dsp.registry.get_verified_account_storage_hash(proof.address, block.number);
assert_eq!(storage_hash, proof.storage_hash);
}

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

let block = proofs::blocks::BLOCK_3();
dsp.store.set_state_root(block.number, block.state_root);

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

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

let storage_hash = dsp.registry.get_verified_account_storage_hash(proof.address, block.number);
assert_eq!(storage_hash, proof.storage_hash);

let code_hash = dsp.registry.get_verified_account_code_hash(proof.address, block.number);
assert_eq!(code_hash, proof.code_hash);

let balance = dsp.registry.get_verified_account_balance(proof.address, block.number);
assert_eq!(balance, proof.balance);

let nonce = dsp.registry.get_verified_account_nonce(proof.address, block.number);
assert_eq!(nonce, proof.nonce);
}

#[test]
Expand All @@ -93,51 +117,155 @@ fn prove_account_test_fail_account_not_found() {

#[test]
fn get_storage_test_success_with_some_data() {
assert!(true)
let dsp = setup();

let block = proofs::blocks::BLOCK_3();
dsp.store.set_state_root(block.number, block.state_root);

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

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

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

let result = dsp
.registry
.get_storage(
block.number,
account_proof.address,
storage_proof.key,
storage_proof.bytes,
storage_proof.data
);
assert_eq!(result.values, array![551956115156281927, 37889].span());
}

#[test]
fn get_storage_test_success_with_no_data() {
assert!(true)
let dsp = setup();

let block = proofs::blocks::BLOCK_3();
dsp.store.set_state_root(block.number, block.state_root);

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

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

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

let result = dsp
.registry
.get_storage(
block.number,
account_proof.address,
storage_proof.key,
storage_proof.bytes,
storage_proof.data
);
assert_eq!(result.values, array![].span());
}

#[test]
#[should_panic]
fn get_storage_test_fail_state_root_is_zero() {
assert!(false)
fn get_storage_test_fail_account_not_found() {
let dsp = setup();

let block = proofs::blocks::BLOCK_3();
let account_proof = proofs::account::PROOF_1();
let storage_proof = proofs::storage::PROOF_1();

let result = dsp
.registry
.get_storage(
block.number,
account_proof.address,
storage_proof.key,
storage_proof.bytes,
storage_proof.data
);
assert_eq!(result.values, array![].span());
}


#[test]
fn get_storage_uint_test_success() {
assert!(true)
}
let dsp = setup();

#[test]
fn get_initialized_test() {
assert!(true)
}
let block = proofs::blocks::BLOCK_3();
dsp.store.set_state_root(block.number, block.state_root);

#[test]
fn get_l1_headers_store_addr_test() {
assert!(true)
}
let account_proof = proofs::account::PROOF_1();

#[test]
fn get_verified_account_storage_hash_test() {
assert!(true)
}
dsp
.registry
.prove_account(
OptionsSet::All,
account_proof.address,
block.number,
account_proof.bytes,
account_proof.data
);

#[test]
fn get_verified_account_code_hash_test() {
assert!(true)
}
let storage_proof = proofs::storage::PROOF_2();

#[test]
fn get_verified_account_balance_test() {
assert!(true)
let result = dsp
.registry
.get_storage_uint(
block.number,
account_proof.address,
storage_proof.key,
storage_proof.bytes,
storage_proof.data
);
assert_eq!(result, 698929238164896357460551);
}

#[test]
fn get_verified_account_nonce_test() {
assert!(true)
fn get_storage_uint_test_success_no_data() {
let dsp = setup();

let block = proofs::blocks::BLOCK_3();
dsp.store.set_state_root(block.number, block.state_root);

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

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

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

let result = dsp
.registry
.get_storage_uint(
block.number,
account_proof.address,
storage_proof.key,
storage_proof.bytes,
storage_proof.data
);
assert_eq!(result, 0);
}

1 change: 0 additions & 1 deletion starknet/tests/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use fossil::fact_registry::{
use snforge_std::{declare, ContractClassTrait};



fn L1_ORIGIN() -> starknet::EthAddress {
'L1_messege_origin'.try_into().unwrap()
}
Expand Down

0 comments on commit ae041b8

Please sign in to comment.