|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use ethnum::U256; |
| 4 | +use pretty_assertions::assert_eq; |
| 5 | +use rstest::rstest; |
| 6 | +use starknet_api::core::{ClassHash, ContractAddress, Nonce}; |
| 7 | +use starknet_api::hash::HashOutput; |
| 8 | +use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{ |
| 9 | + BinaryData, |
| 10 | + EdgeData, |
| 11 | + EdgePath, |
| 12 | + EdgePathLength, |
| 13 | + PathToBottom, |
| 14 | + Preimage, |
| 15 | + PreimageMap, |
| 16 | +}; |
| 17 | +use starknet_types_core::felt::Felt; |
| 18 | + |
| 19 | +use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; |
| 20 | +use crate::patricia_merkle_tree::types::{ContractsTrieProof, StarknetForestProofs}; |
| 21 | + |
| 22 | +fn binary_preimage(node_hash: u128, left: u128, right: u128) -> (HashOutput, Preimage) { |
| 23 | + ( |
| 24 | + HashOutput(Felt::from(node_hash)), |
| 25 | + Preimage::Binary(BinaryData { |
| 26 | + left_data: HashOutput(Felt::from(left)), |
| 27 | + right_data: HashOutput(Felt::from(right)), |
| 28 | + }), |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +fn edge_preimage(node_hash: u128, bottom: u128, path: u128, length: u8) -> (HashOutput, Preimage) { |
| 33 | + ( |
| 34 | + HashOutput(Felt::from(node_hash)), |
| 35 | + Preimage::Edge(EdgeData { |
| 36 | + bottom_data: HashOutput(Felt::from(bottom)), |
| 37 | + path_to_bottom: PathToBottom::new( |
| 38 | + EdgePath(U256::from(path)), |
| 39 | + EdgePathLength::new(length).unwrap(), |
| 40 | + ) |
| 41 | + .unwrap(), |
| 42 | + }), |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +fn contract_state(nonce: u128, storage_root: u128, class_hash: u128) -> ContractState { |
| 47 | + ContractState { |
| 48 | + nonce: Nonce(Felt::from(nonce)), |
| 49 | + storage_root_hash: HashOutput(Felt::from(storage_root)), |
| 50 | + class_hash: ClassHash(Felt::from(class_hash)), |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fn empty_contracts_trie_proof() -> ContractsTrieProof { |
| 55 | + ContractsTrieProof { nodes: PreimageMap::new(), leaves: HashMap::new() } |
| 56 | +} |
| 57 | + |
| 58 | +fn only_classes() -> StarknetForestProofs { |
| 59 | + StarknetForestProofs { |
| 60 | + classes_trie_proof: PreimageMap::from([ |
| 61 | + binary_preimage(100, 101, 102), |
| 62 | + edge_preimage(200, 201, 0, 1), |
| 63 | + ]), |
| 64 | + contracts_trie_proof: empty_contracts_trie_proof(), |
| 65 | + contracts_trie_storage_proofs: HashMap::new(), |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn only_contracts() -> StarknetForestProofs { |
| 70 | + let address = ContractAddress::try_from(Felt::from(7)).unwrap(); |
| 71 | + StarknetForestProofs { |
| 72 | + classes_trie_proof: PreimageMap::new(), |
| 73 | + contracts_trie_proof: ContractsTrieProof { |
| 74 | + nodes: PreimageMap::from([binary_preimage(300, 301, 302)]), |
| 75 | + leaves: HashMap::from([(address, contract_state(1, 400, 500))]), |
| 76 | + }, |
| 77 | + contracts_trie_storage_proofs: HashMap::new(), |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +fn classes_and_contracts() -> StarknetForestProofs { |
| 82 | + let address = ContractAddress::try_from(Felt::from(9)).unwrap(); |
| 83 | + StarknetForestProofs { |
| 84 | + classes_trie_proof: PreimageMap::from([binary_preimage(110, 111, 112)]), |
| 85 | + contracts_trie_proof: ContractsTrieProof { |
| 86 | + nodes: PreimageMap::from([edge_preimage(310, 311, 1, 1)]), |
| 87 | + leaves: HashMap::from([(address, contract_state(2, 401, 501))]), |
| 88 | + }, |
| 89 | + contracts_trie_storage_proofs: HashMap::new(), |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +fn all_leaf_types_single_storage() -> StarknetForestProofs { |
| 94 | + let address = ContractAddress::try_from(Felt::from(11)).unwrap(); |
| 95 | + StarknetForestProofs { |
| 96 | + classes_trie_proof: PreimageMap::from([binary_preimage(120, 121, 122)]), |
| 97 | + contracts_trie_proof: ContractsTrieProof { |
| 98 | + nodes: PreimageMap::from([binary_preimage(320, 321, 322)]), |
| 99 | + leaves: HashMap::from([(address, contract_state(3, 402, 502))]), |
| 100 | + }, |
| 101 | + contracts_trie_storage_proofs: HashMap::from([( |
| 102 | + address, |
| 103 | + PreimageMap::from([edge_preimage(410, 411, 0, 1)]), |
| 104 | + )]), |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +fn all_leaf_types_multiple_storages() -> StarknetForestProofs { |
| 109 | + let address_a = ContractAddress::try_from(Felt::from(13)).unwrap(); |
| 110 | + let address_b = ContractAddress::try_from(Felt::from(14)).unwrap(); |
| 111 | + StarknetForestProofs { |
| 112 | + classes_trie_proof: PreimageMap::from([edge_preimage(130, 131, 1, 1)]), |
| 113 | + contracts_trie_proof: ContractsTrieProof { |
| 114 | + nodes: PreimageMap::from([binary_preimage(330, 331, 332)]), |
| 115 | + leaves: HashMap::from([ |
| 116 | + (address_a, contract_state(4, 403, 503)), |
| 117 | + (address_b, contract_state(5, 404, 504)), |
| 118 | + ]), |
| 119 | + }, |
| 120 | + contracts_trie_storage_proofs: HashMap::from([ |
| 121 | + (address_a, PreimageMap::from([binary_preimage(420, 421, 422)])), |
| 122 | + (address_b, PreimageMap::from([edge_preimage(430, 431, 1, 1)])), |
| 123 | + ]), |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +#[rstest] |
| 128 | +#[case::only_classes(only_classes())] |
| 129 | +#[case::only_contracts(only_contracts())] |
| 130 | +#[case::classes_and_contracts(classes_and_contracts())] |
| 131 | +#[case::all_leaf_types_single_storage(all_leaf_types_single_storage())] |
| 132 | +#[case::all_leaf_types_multiple_storages(all_leaf_types_multiple_storages())] |
| 133 | +fn test_starknet_forest_proofs_serialization_round_trip(#[case] proofs: StarknetForestProofs) { |
| 134 | + let encoded = proofs.serialize().unwrap(); |
| 135 | + let decoded = StarknetForestProofs::deserialize(&encoded).unwrap(); |
| 136 | + assert_eq!(proofs, decoded); |
| 137 | +} |
0 commit comments