Skip to content

Commit

Permalink
refacto return values for fact registry
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Jul 25, 2024
1 parent ecb56f5 commit 842538d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
30 changes: 20 additions & 10 deletions starknet/src/fact_registry/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub mod FactRegistry {
// Upgradeable
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

const SUCCESS: felt252 = 'Proof verified successfully';
const ALREADY_VERIFIED: felt252 = 'Proof already verified';
const EMPTY_SLOT: felt252 = 'Empty slot';

// *************************************************************************
// STORAGE
// *************************************************************************
Expand All @@ -44,6 +48,7 @@ pub mod FactRegistry {
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
l1_headers_store: IL1HeadersStoreDispatcher,
verified_account: LegacyMap::<(EthAddress, u64), bool>,
verified_account_storage_hash: LegacyMap::<(EthAddress, u64), u256>,
verified_account_code_hash: LegacyMap::<(EthAddress, u64), u256>,
verified_account_balance: LegacyMap::<(EthAddress, u64), u256>,
Expand Down Expand Up @@ -111,14 +116,14 @@ pub mod FactRegistry {
block: u64,
proof_sizes_bytes: Array<usize>,
proofs_concat: Array<u64>,
) -> felt252 {
) -> (bool, felt252) {
let state_root = self.l1_headers_store.read().get_state_root(block);
assert!(state_root != 0, "FactRegistry: block state root not found");
let proof = self
.reconstruct_ints_sequence_list(proofs_concat.span(), proof_sizes_bytes.span());
let result = verify_proof(account.to_words64(), state_root.to_words64(), proof.span());
match result {
Result::Err(e) => { e },
Result::Err(e) => { (false, e) },
Result::Ok(result) => {
let result = result.unwrap();
let result_items = to_rlp_array(result);
Expand Down Expand Up @@ -163,7 +168,7 @@ pub mod FactRegistry {
.write((account, block), code_hash.from_words64());
},
};
return 'Result Found';
return (true, SUCCESS);
}
}
}
Expand Down Expand Up @@ -192,10 +197,15 @@ pub mod FactRegistry {
slot: u256,
proof_sizes_bytes: Array<usize>,
proofs_concat: Array<u64>,
) -> Result<u256, felt252> {
) -> (bool, u256, felt252) {
let account_state_root = self.verified_account_storage_hash.read((account, block));
assert!(account_state_root != 0, "FactRegistry: block state root not found");

let (already_proved, value) = self.verified_storage.read((account, block, slot));
if already_proved {
return (true, value, ALREADY_VERIFIED);
}

let result = verify_proof(
keccak_words64(slot.to_words64()),
account_state_root.to_words64(),
Expand All @@ -205,14 +215,14 @@ pub mod FactRegistry {
);

match result {
Result::Err(e) => { Result::Err(e) },
Result::Err(e) => { return (false, 0, e); },
Result::Ok(result) => {
if !result.is_some() {
Result::Err('Result is None')
return (true, 0, EMPTY_SLOT);
} else {
let result_value = words64_to_int(result.unwrap());
self.verified_storage.write((account, block, slot), (true, result_value));
Result::Ok(result_value)
return (true, result_value, SUCCESS);
}
}
}
Expand All @@ -234,12 +244,12 @@ pub mod FactRegistry {
/// * `u256` - The storage value at the given slot.
fn get_storage(
ref self: ContractState, block: u64, account: starknet::EthAddress, slot: u256,
) -> Option<u256> {
) -> (bool, u256) {
let (verified, value) = self.verified_storage.read((account, block, slot));
if verified {
return Option::Some(value);
return (true, value);
} else {
return Option::None;
return (false, 0);
}
}

Expand Down
6 changes: 3 additions & 3 deletions starknet/src/fact_registry/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ pub trait IFactRegistry<TState> {
block: u64,
proof_sizes_bytes: Array<usize>,
proofs_concat: Array<u64>,
) -> felt252;
) -> (bool, felt252);
fn prove_storage(
ref self: TState,
block: u64,
account: starknet::EthAddress,
slot: u256,
proof_sizes_bytes: Array<usize>,
proofs_concat: Array<u64>,
) -> Result<u256, felt252>;
) -> (bool, u256, felt252);
fn get_storage(
ref self: TState, block: u64, account: starknet::EthAddress, slot: u256,
) -> Option<u256>;
) -> (bool, u256);
fn get_l1_headers_store_addr(self: @TState) -> starknet::ContractAddress;
fn get_verified_account_storage_hash(
self: @TState, account: starknet::EthAddress, block: u64
Expand Down
16 changes: 8 additions & 8 deletions starknet/tests/test_fact_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn prove_all_test_success_mainnet_weth() {

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

let result = dsp
let (proved, value, result) = dsp
.registry
.prove_storage(
block.number,
Expand All @@ -62,7 +62,7 @@ fn prove_all_test_success_mainnet_weth() {
let storage_result = dsp
.registry
.get_storage(block.number, account_proof.address, storage_proof.key);
assert_eq!(result.unwrap(), storage_result.unwrap());
assert_eq!((proved, value), storage_result);
}

#[test]
Expand Down Expand Up @@ -208,7 +208,7 @@ fn prove_storage_test_success_with_some_data() {

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

let result = dsp
let (proved, value, _) = dsp
.registry
.prove_storage(
block.number,
Expand All @@ -220,7 +220,7 @@ fn prove_storage_test_success_with_some_data() {
let storage_result = dsp
.registry
.get_storage(block.number, account_proof.address, storage_proof.key);
assert_eq!(result.unwrap(), storage_result.unwrap());
assert_eq!((proved, value), storage_result);
}

#[test]
Expand Down Expand Up @@ -254,7 +254,7 @@ fn test_get_storage_not_verified() {

let result = dsp.registry.get_storage(0, 0_u256.into(), 0);

assert!(result == Option::None);
assert!(result == (false, 0));
}

#[test]
Expand Down Expand Up @@ -289,7 +289,7 @@ fn get_storage_test_success_with_no_data() {
storage_proof.bytes,
storage_proof.data
);
assert!(result == Result::Err('Result is None'));
assert!(result == (true, 0, 'Empty slot'));
}

#[test]
Expand Down Expand Up @@ -338,7 +338,7 @@ fn prove_account_test_error_invalid_children_length() {
.registry
.prove_account(OptionsSet::CodeHash, proof.address, block.number, proof.bytes, proof.data);

assert_eq!(output, 'invalid children length');
assert_eq!(output, (false, 'invalid children length'));
}

#[test]
Expand All @@ -355,5 +355,5 @@ fn prove_account_test_error_root_hash_mismatch() {
let output = dsp
.registry
.prove_account(OptionsSet::CodeHash, proof.address, block.number, proof.bytes, proof.data);
assert_eq!(output, 'Root hash mismatch');
assert_eq!(output, (false, 'Root hash mismatch'));
}

0 comments on commit 842538d

Please sign in to comment.