Skip to content

Commit ef327fb

Browse files
blockifier: update patricia resource calc according to new OS impl
1 parent e882511 commit ef327fb

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

crates/blockifier/src/bouncer.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,11 @@ pub fn get_tx_weights<S: StateReader>(
846846
map_class_hash_to_casm_hash_computation_resources(state_reader, executed_class_hashes)?;
847847

848848
// Patricia update + transaction resources.
849-
let patrticia_update_resources = get_particia_update_resources(n_visited_storage_entries);
849+
let patrticia_update_resources = get_patricia_update_resources(
850+
n_visited_storage_entries,
851+
// TODO(Yoni): consider counting here the global contract tree and the aliases as well.
852+
state_changes_keys.storage_keys.len(),
853+
);
850854
let vm_resources = &patrticia_update_resources + &tx_resources.computation.total_vm_resources();
851855

852856
// Builtin gas costs for stone and for stwo.
@@ -939,12 +943,23 @@ pub fn map_class_hash_to_casm_hash_computation_resources<S: StateReader>(
939943
.collect()
940944
}
941945

942-
/// Returns the estimated Cairo resources for Patricia tree updates, or hash invocations
943-
/// (done by the OS), required for accessing (read/write) the given storage entries.
944-
// For each tree: n_visited_leaves * log(n_initialized_leaves)
945-
// as the height of a Patricia tree with N uniformly distributed leaves is ~log(N),
946-
// and number of visited leaves includes reads and writes.
947-
pub fn get_particia_update_resources(n_visited_storage_entries: usize) -> ExecutionResources {
946+
/// Returns the estimated Cairo resources for Patricia tree updates given the accessed and
947+
/// modified storage entries.
948+
///
949+
/// Each access (read or write) requires a traversal of the previous tree, and a write access
950+
/// requires an additional traversal of the new tree.
951+
///
952+
/// Note:
953+
/// 1. n_visited_storage_entries includes both read and write accesses, and may overlap with
954+
/// n_modified_storage_entries (if the first access to a cell was write) and my not (if a
955+
/// cell was read by a previous transaction and is now modified).
956+
/// 2. In practice, the OS performs a multi-update, which is more efficient than performing
957+
/// separate updates. However, we use this conservative estimate for simplicity.
958+
pub fn get_patricia_update_resources(
959+
n_visited_storage_entries: usize,
960+
n_modified_storage_entries: usize,
961+
) -> ExecutionResources {
962+
// The height of a Patricia tree with N uniformly distributed leaves is ~log(N).
948963
const TREE_HEIGHT_UPPER_BOUND: usize = 24;
949964
// TODO(Yoni, 1/5/2024): re-estimate this.
950965
const STEPS_IN_TREE_PER_HEIGHT: usize = 16;
@@ -959,8 +974,8 @@ pub fn get_particia_update_resources(n_visited_storage_entries: usize) -> Execut
959974
n_memory_holes: 0,
960975
};
961976

962-
// Multiply by 2 since each storage entry is accessed in both the old and new tree.
963-
&resources_per_tree_access * (n_visited_storage_entries * 2)
977+
// One traversal per access (read or write), and an additional one per write access.
978+
&resources_per_tree_access * (n_visited_storage_entries + n_modified_storage_entries)
964979
}
965980

966981
pub fn verify_tx_weights_within_max_capacity<S: StateReader>(

crates/blockifier/src/bouncer_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::BouncerConfig;
1717
use crate::blockifier::transaction_executor::TransactionExecutorError;
1818
use crate::bouncer::{
1919
builtins_to_gas,
20-
get_particia_update_resources,
20+
get_patricia_update_resources,
2121
get_tx_weights,
2222
map_class_hash_to_casm_hash_computation_resources,
2323
verify_tx_weights_within_max_capacity,
@@ -448,11 +448,11 @@ fn test_bouncer_try_update_n_txs(
448448
n_events: 10,
449449
state_diff_size: 14,
450450
sierra_gas: GasAmount(
451-
542410,
451+
406810,
452452
),
453453
n_txs: 20,
454454
proving_gas: GasAmount(
455-
702922,
455+
527194,
456456
),
457457
}
458458
"#
@@ -651,7 +651,7 @@ fn test_proving_gas_minus_sierra_gas_equals_builtin_gas(
651651
let n_visited_storage_entries = if casm_hash_computation_builtins.is_empty() { 0 } else { 1 };
652652

653653
let mut additional_os_resources =
654-
get_particia_update_resources(n_visited_storage_entries).prover_builtins();
654+
get_patricia_update_resources(n_visited_storage_entries, 0).prover_builtins();
655655
add_maps(&mut additional_os_resources, &casm_hash_computation_builtins);
656656

657657
let result = get_tx_weights(

0 commit comments

Comments
 (0)