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

Commit b69e951

Browse files
matias-gonzxqftSantiagoPittella
authored
Fix transaction resources calculations (#849)
* Update os usage * Print gas_price * Remove print * Add FEE_TRANSFER_N_STORAGE_CHANGES_TO_CHARGE * Update count_actual_storage_changes * Add update_initial_values * Added additional_os_res test * Added count storage changes test * Fix tests * Fix tests * Add test for updating init values for cached state --------- Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy> Co-authored-by: Santiago Pittella <87827390+SantiagoPittella@users.noreply.github.com>
1 parent cdf9880 commit b69e951

File tree

11 files changed

+377
-55
lines changed

11 files changed

+377
-55
lines changed

src/definitions/constants.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ pub(crate) const N_DEFAULT_TOPICS: usize = 1; // Events have one default topic.
1414
pub(crate) const CONSUMED_MSG_TO_L2_ENCODED_DATA_SIZE: usize =
1515
(L1_TO_L2_MSG_HEADER_SIZE + 1) - CONSUMED_MSG_TO_L2_N_TOPICS;
1616

17+
/// Sender and sequencer balance updates.
18+
pub(crate) const FEE_TRANSFER_N_STORAGE_CHANGES: usize = 2;
19+
20+
/// Exclude the sequencer balance update, since it's charged once throught the batch.
21+
pub(crate) const FEE_TRANSFER_N_STORAGE_CHANGES_TO_CHARGE: usize =
22+
FEE_TRANSFER_N_STORAGE_CHANGES - 1;
23+
1724
lazy_static! {
1825
pub(crate) static ref QUERY_VERSION_BASE: Felt252 =
1926
felt_str!("340282366920938463463374607431768211456");

src/execution/os_usage.rs

Lines changed: 226 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{definitions::transaction_type::TransactionType, transaction::error::
66

77
#[derive(Debug, Clone)]
88
pub struct OsResources {
9-
_execute_syscalls: HashMap<String, ExecutionResources>,
9+
execute_syscalls: HashMap<String, ExecutionResources>,
1010
execute_txs_inner: HashMap<TransactionType, ExecutionResources>,
1111
}
1212

@@ -16,22 +16,22 @@ impl Default for OsResources {
1616
(
1717
TransactionType::InvokeFunction,
1818
ExecutionResources {
19-
n_steps: 2839,
19+
n_steps: 3363,
2020
n_memory_holes: 0,
2121
builtin_instance_counter: HashMap::from([
2222
("pedersen_builtin".to_string(), 16),
23-
("range_check_builtin".to_string(), 70),
23+
("range_check_builtin".to_string(), 80),
2424
]),
2525
},
2626
),
2727
(
2828
TransactionType::Declare,
2929
ExecutionResources {
30-
n_steps: 2336,
30+
n_steps: 2703,
3131
n_memory_holes: 0,
3232
builtin_instance_counter: HashMap::from([
3333
("pedersen_builtin".to_string(), 15),
34-
("range_check_builtin".to_string(), 57),
34+
("range_check_builtin".to_string(), 63),
3535
]),
3636
},
3737
),
@@ -46,11 +46,11 @@ impl Default for OsResources {
4646
(
4747
TransactionType::DeployAccount,
4848
ExecutionResources {
49-
n_steps: 3098,
49+
n_steps: 3612,
5050
n_memory_holes: 0,
5151
builtin_instance_counter: HashMap::from([
5252
("pedersen_builtin".to_string(), 23),
53-
("range_check_builtin".to_string(), 74),
53+
("range_check_builtin".to_string(), 83),
5454
]),
5555
},
5656
),
@@ -67,22 +67,237 @@ impl Default for OsResources {
6767
),
6868
]);
6969

70+
let execute_syscalls = HashMap::from([
71+
(
72+
"call_contract".to_string(),
73+
ExecutionResources {
74+
n_steps: 690,
75+
n_memory_holes: 0,
76+
builtin_instance_counter: HashMap::from([(
77+
"range_check_builtin".to_string(),
78+
19,
79+
)]),
80+
},
81+
),
82+
(
83+
"delegate_call".to_string(),
84+
ExecutionResources {
85+
n_steps: 712,
86+
n_memory_holes: 0,
87+
builtin_instance_counter: HashMap::from([(
88+
"range_check_builtin".to_string(),
89+
19,
90+
)]),
91+
},
92+
),
93+
(
94+
"delegate_l1_handler".to_string(),
95+
ExecutionResources {
96+
n_steps: 691,
97+
n_memory_holes: 0,
98+
builtin_instance_counter: HashMap::from([(
99+
"range_check_builtin".to_string(),
100+
15,
101+
)]),
102+
},
103+
),
104+
(
105+
"deploy".to_string(),
106+
ExecutionResources {
107+
n_steps: 936,
108+
n_memory_holes: 0,
109+
builtin_instance_counter: HashMap::from([
110+
("range_check_builtin".to_string(), 18),
111+
("pedersen_builtin".to_string(), 7),
112+
]),
113+
},
114+
),
115+
(
116+
"library_call".to_string(),
117+
ExecutionResources {
118+
n_steps: 679,
119+
n_memory_holes: 0,
120+
builtin_instance_counter: HashMap::from([(
121+
"range_check_builtin".to_string(),
122+
19,
123+
)]),
124+
},
125+
),
126+
(
127+
"emit_event".to_string(),
128+
ExecutionResources {
129+
n_steps: 19,
130+
n_memory_holes: 0,
131+
builtin_instance_counter: HashMap::new(),
132+
},
133+
),
134+
(
135+
"get_block_hash".to_string(),
136+
ExecutionResources {
137+
n_steps: 44,
138+
n_memory_holes: 0,
139+
builtin_instance_counter: HashMap::new(),
140+
},
141+
),
142+
(
143+
"get_block_number".to_string(),
144+
ExecutionResources {
145+
n_steps: 40,
146+
n_memory_holes: 0,
147+
builtin_instance_counter: HashMap::new(),
148+
},
149+
),
150+
(
151+
"get_block_timestamp".to_string(),
152+
ExecutionResources {
153+
n_steps: 38,
154+
n_memory_holes: 0,
155+
builtin_instance_counter: HashMap::new(),
156+
},
157+
),
158+
(
159+
"get_caller_address".to_string(),
160+
ExecutionResources {
161+
n_steps: 32,
162+
n_memory_holes: 0,
163+
builtin_instance_counter: HashMap::new(),
164+
},
165+
),
166+
(
167+
"get_contract_address".to_string(),
168+
ExecutionResources {
169+
n_steps: 36,
170+
n_memory_holes: 0,
171+
builtin_instance_counter: HashMap::new(),
172+
},
173+
),
174+
(
175+
"get_execution_info".to_string(),
176+
ExecutionResources {
177+
n_steps: 29,
178+
n_memory_holes: 0,
179+
builtin_instance_counter: HashMap::new(),
180+
},
181+
),
182+
(
183+
"get_sequencer_address".to_string(),
184+
ExecutionResources {
185+
n_steps: 34,
186+
n_memory_holes: 0,
187+
builtin_instance_counter: HashMap::new(),
188+
},
189+
),
190+
(
191+
"get_tx_info".to_string(),
192+
ExecutionResources {
193+
n_steps: 29,
194+
n_memory_holes: 0,
195+
builtin_instance_counter: HashMap::new(),
196+
},
197+
),
198+
(
199+
"get_tx_signature".to_string(),
200+
ExecutionResources {
201+
n_steps: 44,
202+
n_memory_holes: 0,
203+
builtin_instance_counter: HashMap::new(),
204+
},
205+
),
206+
(
207+
"library_call_l1_handler".to_string(),
208+
ExecutionResources {
209+
n_steps: 658,
210+
n_memory_holes: 0,
211+
builtin_instance_counter: HashMap::from([(
212+
"range_check_builtin".to_string(),
213+
15,
214+
)]),
215+
},
216+
),
217+
(
218+
"replace_class".to_string(),
219+
ExecutionResources {
220+
n_steps: 73,
221+
n_memory_holes: 0,
222+
builtin_instance_counter: HashMap::new(),
223+
},
224+
),
225+
(
226+
"send_message_to_l1".to_string(),
227+
ExecutionResources {
228+
n_steps: 84,
229+
n_memory_holes: 0,
230+
builtin_instance_counter: HashMap::new(),
231+
},
232+
),
233+
(
234+
"storage_read".to_string(),
235+
ExecutionResources {
236+
n_steps: 44,
237+
n_memory_holes: 0,
238+
builtin_instance_counter: HashMap::new(),
239+
},
240+
),
241+
(
242+
"storage_write".to_string(),
243+
ExecutionResources {
244+
n_steps: 46,
245+
n_memory_holes: 0,
246+
builtin_instance_counter: HashMap::new(),
247+
},
248+
),
249+
]);
250+
70251
OsResources {
71-
_execute_syscalls: HashMap::new(),
252+
execute_syscalls,
72253
execute_txs_inner,
73254
}
74255
}
75256
}
76257

77258
pub fn get_additional_os_resources(
78-
_syscall_counter: HashMap<String, u64>,
259+
syscall_counter: HashMap<String, u64>,
79260
tx_type: &TransactionType,
80261
) -> Result<ExecutionResources, TransactionError> {
81262
let os_resources = OsResources::default();
82263

83-
Ok(os_resources
264+
let mut additional_os_resources = ExecutionResources::default();
265+
266+
for (syscall, count) in syscall_counter {
267+
let syscall_resources = &os_resources
268+
.execute_syscalls
269+
.get(&syscall)
270+
.ok_or_else(|| TransactionError::ResourcesError)?
271+
.clone()
272+
* count as usize;
273+
274+
additional_os_resources += &syscall_resources;
275+
}
276+
277+
additional_os_resources += &os_resources
84278
.execute_txs_inner
85279
.get(tx_type)
86280
.ok_or_else(|| TransactionError::NoneTransactionType(*tx_type, os_resources.clone()))?
87-
.clone())
281+
.clone();
282+
283+
Ok(additional_os_resources)
284+
}
285+
286+
#[test]
287+
fn get_additional_os_resources_test() {
288+
let syscall_counter = HashMap::from([("storage_read".into(), 2), ("storage_write".into(), 3)]);
289+
290+
let tx_type = TransactionType::InvokeFunction;
291+
292+
let additional_os_resources = get_additional_os_resources(syscall_counter, &tx_type).unwrap();
293+
let expected_additional_os_resources = ExecutionResources {
294+
n_steps: 3589,
295+
n_memory_holes: 0,
296+
builtin_instance_counter: HashMap::from([
297+
("range_check_builtin".to_string(), 80),
298+
("pedersen_builtin".to_string(), 16),
299+
]),
300+
};
301+
302+
assert_eq!(additional_os_resources, expected_additional_os_resources);
88303
}

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ mod test {
287287
let transaction = Transaction::InvokeFunction(invoke_function);
288288

289289
let estimated_fee = estimate_fee(&[transaction], state, &block_context).unwrap();
290-
assert_eq!(estimated_fee[0], (30, 0));
290+
assert_eq!(estimated_fee[0], (2483, 2448));
291291
}
292292

293293
#[test]
@@ -378,7 +378,7 @@ mod test {
378378
block_context.starknet_os_config.gas_price = 1;
379379

380380
let estimated_fee = estimate_message_fee(&l1_handler, state, &block_context).unwrap();
381-
assert_eq!(estimated_fee, (18484, 18471));
381+
assert_eq!(estimated_fee, (19708, 19695));
382382
}
383383

384384
#[test]
@@ -986,7 +986,7 @@ mod test {
986986

987987
assert_eq!(
988988
estimate_fee(&[deploy, invoke_tx], state, block_context,).unwrap(),
989-
[(0, 1224), (0, 0)]
989+
[(0, 3672), (0, 2448)]
990990
);
991991
}
992992

0 commit comments

Comments
 (0)