From 626818b39f48b90cc59b1901413faef3ba49277a Mon Sep 17 00:00:00 2001 From: ftheirs Date: Wed, 18 Dec 2024 10:22:49 -0300 Subject: [PATCH] Revert "retrieve extra keys for 0x1 contract" and clean up This reverts commit 8fcae6c27d2f164d4b645fb0b3f1627e1e05f656. --- crates/bin/prove_block/src/rpc_utils.rs | 51 ++++++------------------- crates/bin/prove_block/src/utils.rs | 33 +++++----------- 2 files changed, 21 insertions(+), 63 deletions(-) diff --git a/crates/bin/prove_block/src/rpc_utils.rs b/crates/bin/prove_block/src/rpc_utils.rs index 8ad34efb0..5c9dc9aec 100644 --- a/crates/bin/prove_block/src/rpc_utils.rs +++ b/crates/bin/prove_block/src/rpc_utils.rs @@ -117,7 +117,6 @@ fn verify_storage_proof(contract_data: &ContractData, keys: &[Felt]) -> Vec>, old_block_number: Felt252, keys: &mut HashMap>, ) { @@ -125,21 +124,19 @@ fn insert_extra_storage_reads_keys( let special_addresses: Vec = vec![ contract_address!("0x01246c3031c5d0d1cf60a9370aac03a4717538f659e4a2bfb0f692e970e0c4b5"), contract_address!("0x00656ca4889a405ec5222e4b0997e5a043902a98cb1f85a039f76f50c000479d"), + contract_address!("0x022207b425a6c0239bbf5d58fbf0272fbb059ee4bb89f48255321d6e7c1606ef"), // Ekubo:core contract address. Source code is not available but `key_not_in_preimage` error is triggered every time it's called contract_address!("0x5dd3d2f4429af886cd1a3b08289dbcea99a294197e9eb43b0e0325b4b"), ]; - let extra_storage_reads = if special_addresses.iter().any(|address| storage_reads.contains_key(address)) { - 100 * STORED_BLOCK_HASH_BUFFER - } else { - STORED_BLOCK_HASH_BUFFER - }; - - if old_block_number >= Felt252::from(extra_storage_reads) { - for i in 1..=extra_storage_reads { - keys.entry(contract_address!("0x1")) - .or_default() - .insert((old_block_number - i).try_into().expect("Felt to StorageKey conversion failed")); + if special_addresses.iter().any(|address| keys.contains_key(address)) { + let extra_storage_reads = 200 * STORED_BLOCK_HASH_BUFFER; + if old_block_number >= Felt252::from(extra_storage_reads) { + for i in 1..=extra_storage_reads { + keys.entry(contract_address!("0x1")) + .or_default() + .insert((old_block_number - i).try_into().expect("Felt to StorageKey conversion failed")); + } } } } @@ -151,35 +148,11 @@ pub(crate) async fn get_storage_proofs( old_block_number: Felt, ) -> Result, ClientError> { let accessed_keys_by_address = { - let (mut keys, storage_reads) = get_all_accessed_keys(tx_execution_infos); - + let mut keys = get_all_accessed_keys(tx_execution_infos); // We need to fetch the storage proof for the block hash contract - // Include not only old_block_hash but also retrieve the previous 10 values - let block_hash_contract_address = contract_address!("0x1"); - keys.entry(block_hash_contract_address) - .or_default() - .insert((old_block_number).try_into().expect("Felt to StorageKey conversion failed")); - + keys.entry(contract_address!("0x1")).or_default().insert(old_block_number.try_into().unwrap()); // Include extra keys for contracts that trigger get_block_hash_syscall - insert_extra_storage_reads_keys(&storage_reads, old_block_number, &mut keys); - - // Within the Starknet architecture, the address 0x1 is a special address that maps block numbers to their corresponding block hashes. As some contracts might access storage reads using `get_block_hash_syscall`, it is necessary to add some extra keys here. - // By leveraging the structure of this special contract, we filter out storage_read_values that are greater than old_block_number and add these extra values to the necessary keys from the 0x1 contract address. - // It is worth noting that this approach incurs some overhead due to the retrieval of additional data. - // let additional_storage_reads: Vec = storage_reads.values().flat_map(|vec| vec.iter()).cloned().collect(); - let additional_storage_reads: Vec = storage_reads - .values() - .flat_map(|vec| vec.iter().cloned().filter(|x| x <= &Felt252::from(block_number))) - .collect::>() - .into_iter() - .collect(); - - keys.entry(block_hash_contract_address).or_default().extend( - additional_storage_reads - .into_iter() - .map(|key| StorageKey::try_from(key).expect("Felt to StorageKey conversion failed")), - ); - + insert_extra_storage_reads_keys(old_block_number, &mut keys); keys }; diff --git a/crates/bin/prove_block/src/utils.rs b/crates/bin/prove_block/src/utils.rs index 5fef3e045..10be9cd03 100644 --- a/crates/bin/prove_block/src/utils.rs +++ b/crates/bin/prove_block/src/utils.rs @@ -84,26 +84,23 @@ fn process_function_invocations( /// We need this as the OS require proofs for all the accessed values pub(crate) fn get_all_accessed_keys( tx_execution_infos: &[TransactionExecutionInfo], -) -> (HashMap>, HashMap>) { +) -> HashMap> { let mut accessed_keys_by_address: HashMap> = HashMap::new(); - let mut call_storage_reads: HashMap> = HashMap::new(); for tx_execution_info in tx_execution_infos { - let (accessed_keys_in_tx, accessed_storage_reads_in_tx) = get_accessed_keys_in_tx(tx_execution_info); + let accessed_keys_in_tx = get_accessed_keys_in_tx(tx_execution_info); for (contract_address, storage_keys) in accessed_keys_in_tx { accessed_keys_by_address.entry(contract_address).or_default().extend(storage_keys); } - call_storage_reads.extend(accessed_storage_reads_in_tx); } - (accessed_keys_by_address, call_storage_reads) + accessed_keys_by_address } fn get_accessed_keys_in_tx( tx_execution_info: &TransactionExecutionInfo, -) -> (HashMap>, HashMap>) { +) -> HashMap> { let mut accessed_keys_by_address: HashMap> = HashMap::new(); - let mut accessed_storage_reads: HashMap> = HashMap::new(); for call_info in [ &tx_execution_info.validate_call_info, @@ -113,21 +110,17 @@ fn get_accessed_keys_in_tx( .into_iter() .flatten() { - let (call_storage_keys, call_storage_reads) = get_accessed_storage_keys(call_info); + let call_storage_keys = get_accessed_storage_keys(call_info); for (contract_address, storage_keys) in call_storage_keys { accessed_keys_by_address.entry(contract_address).or_default().extend(storage_keys); } - accessed_storage_reads.extend(call_storage_reads); } - (accessed_keys_by_address, accessed_storage_reads) + accessed_keys_by_address } -fn get_accessed_storage_keys( - call_info: &CallInfo, -) -> (HashMap>, HashMap>) { +fn get_accessed_storage_keys(call_info: &CallInfo) -> HashMap> { let mut accessed_keys_by_address: HashMap> = HashMap::new(); - let mut accessed_storage_read_values: HashMap> = HashMap::new(); let contract_address = &call_info.call.storage_address; accessed_keys_by_address @@ -135,20 +128,12 @@ fn get_accessed_storage_keys( .or_default() .extend(call_info.accessed_storage_keys.iter().copied()); - accessed_storage_read_values - .entry(*contract_address) - .or_default() - .extend(call_info.storage_read_values.iter().copied()); - for inner_call in &call_info.inner_calls { - let (inner_call_storage_keys, inner_call_storage_read_values) = get_accessed_storage_keys(inner_call); + let inner_call_storage_keys = get_accessed_storage_keys(inner_call); for (contract_address, storage_keys) in inner_call_storage_keys { accessed_keys_by_address.entry(contract_address).or_default().extend(storage_keys); } - for (contract_address, storage_read_values) in inner_call_storage_read_values { - accessed_storage_read_values.entry(contract_address).or_default().extend(storage_read_values); - } } - (accessed_keys_by_address, accessed_storage_read_values) + accessed_keys_by_address }