Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Fix transaction resources calculations #849

Merged
merged 16 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/definitions/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ pub(crate) const N_DEFAULT_TOPICS: usize = 1; // Events have one default topic.
pub(crate) const CONSUMED_MSG_TO_L2_ENCODED_DATA_SIZE: usize =
(L1_TO_L2_MSG_HEADER_SIZE + 1) - CONSUMED_MSG_TO_L2_N_TOPICS;

/// Sender and sequencer balance updates.
pub(crate) const FEE_TRANSFER_N_STORAGE_CHANGES: usize = 2;

/// Exclude the sequencer balance update, since it's charged once throught the batch.
pub(crate) const FEE_TRANSFER_N_STORAGE_CHANGES_TO_CHARGE: usize =
FEE_TRANSFER_N_STORAGE_CHANGES - 1;

lazy_static! {
pub(crate) static ref QUERY_VERSION_BASE: Felt252 =
felt_str!("340282366920938463463374607431768211456");
Expand Down
237 changes: 226 additions & 11 deletions src/execution/os_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{definitions::transaction_type::TransactionType, transaction::error::

#[derive(Debug, Clone)]
pub struct OsResources {
_execute_syscalls: HashMap<String, ExecutionResources>,
execute_syscalls: HashMap<String, ExecutionResources>,
execute_txs_inner: HashMap<TransactionType, ExecutionResources>,
}

Expand All @@ -16,22 +16,22 @@ impl Default for OsResources {
(
TransactionType::InvokeFunction,
ExecutionResources {
n_steps: 2839,
n_steps: 3363,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([
("pedersen_builtin".to_string(), 16),
("range_check_builtin".to_string(), 70),
("range_check_builtin".to_string(), 80),
]),
},
),
(
TransactionType::Declare,
ExecutionResources {
n_steps: 2336,
n_steps: 2703,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([
("pedersen_builtin".to_string(), 15),
("range_check_builtin".to_string(), 57),
("range_check_builtin".to_string(), 63),
]),
},
),
Expand All @@ -46,11 +46,11 @@ impl Default for OsResources {
(
TransactionType::DeployAccount,
ExecutionResources {
n_steps: 3098,
n_steps: 3612,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([
("pedersen_builtin".to_string(), 23),
("range_check_builtin".to_string(), 74),
("range_check_builtin".to_string(), 83),
]),
},
),
Expand All @@ -67,22 +67,237 @@ impl Default for OsResources {
),
]);

let execute_syscalls = HashMap::from([
(
"call_contract".to_string(),
ExecutionResources {
n_steps: 690,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(
"range_check_builtin".to_string(),
19,
)]),
},
),
(
"delegate_call".to_string(),
ExecutionResources {
n_steps: 712,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(
"range_check_builtin".to_string(),
19,
)]),
},
),
(
"delegate_l1_handler".to_string(),
ExecutionResources {
n_steps: 691,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(
"range_check_builtin".to_string(),
15,
)]),
},
),
(
"deploy".to_string(),
ExecutionResources {
n_steps: 936,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([
("range_check_builtin".to_string(), 18),
("pedersen_builtin".to_string(), 7),
]),
},
),
(
"library_call".to_string(),
ExecutionResources {
n_steps: 679,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(
"range_check_builtin".to_string(),
19,
)]),
},
),
(
"emit_event".to_string(),
ExecutionResources {
n_steps: 19,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"get_block_hash".to_string(),
ExecutionResources {
n_steps: 44,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"get_block_number".to_string(),
ExecutionResources {
n_steps: 40,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"get_block_timestamp".to_string(),
ExecutionResources {
n_steps: 38,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"get_caller_address".to_string(),
ExecutionResources {
n_steps: 32,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"get_contract_address".to_string(),
ExecutionResources {
n_steps: 36,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"get_execution_info".to_string(),
ExecutionResources {
n_steps: 29,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"get_sequencer_address".to_string(),
ExecutionResources {
n_steps: 34,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"get_tx_info".to_string(),
ExecutionResources {
n_steps: 29,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"get_tx_signature".to_string(),
ExecutionResources {
n_steps: 44,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"library_call_l1_handler".to_string(),
ExecutionResources {
n_steps: 658,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(
"range_check_builtin".to_string(),
15,
)]),
},
),
(
"replace_class".to_string(),
ExecutionResources {
n_steps: 73,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"send_message_to_l1".to_string(),
ExecutionResources {
n_steps: 84,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"storage_read".to_string(),
ExecutionResources {
n_steps: 44,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
(
"storage_write".to_string(),
ExecutionResources {
n_steps: 46,
n_memory_holes: 0,
builtin_instance_counter: HashMap::new(),
},
),
]);

OsResources {
_execute_syscalls: HashMap::new(),
execute_syscalls,
execute_txs_inner,
}
}
}

pub fn get_additional_os_resources(
_syscall_counter: HashMap<String, u64>,
syscall_counter: HashMap<String, u64>,
tx_type: &TransactionType,
) -> Result<ExecutionResources, TransactionError> {
let os_resources = OsResources::default();

Ok(os_resources
let mut additional_os_resources = ExecutionResources::default();

for (syscall, count) in syscall_counter {
let syscall_resources = &os_resources
.execute_syscalls
.get(&syscall)
.ok_or_else(|| TransactionError::ResourcesError)?
.clone()
* count as usize;

additional_os_resources += &syscall_resources;
}

additional_os_resources += &os_resources
.execute_txs_inner
.get(tx_type)
.ok_or_else(|| TransactionError::NoneTransactionType(*tx_type, os_resources.clone()))?
.clone())
.clone();

Ok(additional_os_resources)
}

#[test]
fn get_additional_os_resources_test() {
let syscall_counter = HashMap::from([("storage_read".into(), 2), ("storage_write".into(), 3)]);

let tx_type = TransactionType::InvokeFunction;

let additional_os_resources = get_additional_os_resources(syscall_counter, &tx_type).unwrap();
let expected_additional_os_resources = ExecutionResources {
n_steps: 3589,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([
("range_check_builtin".to_string(), 80),
("pedersen_builtin".to_string(), 16),
]),
};

assert_eq!(additional_os_resources, expected_additional_os_resources);
}
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ mod test {
let transaction = Transaction::InvokeFunction(invoke_function);

let estimated_fee = estimate_fee(&[transaction], state, &block_context).unwrap();
assert_eq!(estimated_fee[0], (30, 0));
assert_eq!(estimated_fee[0], (2483, 2448));
}

#[test]
Expand Down Expand Up @@ -378,7 +378,7 @@ mod test {
block_context.starknet_os_config.gas_price = 1;

let estimated_fee = estimate_message_fee(&l1_handler, state, &block_context).unwrap();
assert_eq!(estimated_fee, (18484, 18471));
assert_eq!(estimated_fee, (19708, 19695));
}

#[test]
Expand Down Expand Up @@ -986,7 +986,7 @@ mod test {

assert_eq!(
estimate_fee(&[deploy, invoke_tx], state, block_context,).unwrap(),
[(0, 1224), (0, 0)]
[(0, 3672), (0, 2448)]
);
}

Expand Down
Loading