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

Commit f7906c3

Browse files
Transactions support for simulation (#703)
* Add skip flags to transactions * implement functions in deploy * implement functions in deploy account * implement functions in l1handler * avoid execution * Add skip_fee_transfer flag * Fix broken test * remove simulate_Tx method, refactor create tx methods * remove cached state imports * add tests for invoke, declare, deploy and deploy_account * Add declare v2 simulation test * Add l1 handler tx test * re arrange imports * rmeove unwrap --------- Co-authored-by: SantiagoPittella <pittellasantiago@gmail.com>
1 parent 3af21ba commit f7906c3

File tree

10 files changed

+653
-101
lines changed

10 files changed

+653
-101
lines changed

src/lib.rs

Lines changed: 356 additions & 13 deletions
Large diffs are not rendered by default.

src/transaction/declare.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ use num_traits::Zero;
2828
use starknet_contract_class::EntryPointType;
2929
use std::collections::HashMap;
3030

31+
use super::Transaction;
32+
3133
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3234
/// Represents an internal transaction in the StarkNet network that is a declaration of a Cairo
3335
/// contract class.
34-
#[derive(Debug)]
36+
#[derive(Debug, Clone)]
3537
pub struct Declare {
3638
pub class_hash: ClassHash,
3739
pub sender_address: Address,
@@ -43,6 +45,9 @@ pub struct Declare {
4345
pub nonce: Felt252,
4446
pub hash_value: Felt252,
4547
pub contract_class: ContractClass,
48+
pub skip_validate: bool,
49+
pub skip_execute: bool,
50+
pub skip_fee_transfer: bool,
4651
}
4752

4853
// ------------------------------------------------------------
@@ -88,6 +93,9 @@ impl Declare {
8893
nonce,
8994
hash_value,
9095
contract_class,
96+
skip_execute: false,
97+
skip_validate: false,
98+
skip_fee_transfer: false,
9199
};
92100

93101
internal_declare.verify_version()?;
@@ -128,9 +136,11 @@ impl Declare {
128136

129137
// validate transaction
130138
let mut resources_manager = ExecutionResourcesManager::default();
131-
let validate_info =
132-
self.run_validate_entrypoint(state, &mut resources_manager, block_context)?;
133-
139+
let validate_info = if self.skip_validate {
140+
None
141+
} else {
142+
self.run_validate_entrypoint(state, &mut resources_manager, block_context)?
143+
};
134144
let changes = state.count_actual_storage_changes();
135145
let actual_resources = calculate_tx_resources(
136146
resources_manager,
@@ -222,10 +232,17 @@ impl Declare {
222232

223233
let mut tx_execution_context =
224234
self.get_execution_context(block_context.invoke_tx_max_n_steps);
225-
let fee_transfer_info =
226-
execute_fee_transfer(state, block_context, &mut tx_execution_context, actual_fee)?;
227-
228-
Ok((Some(fee_transfer_info), actual_fee))
235+
let fee_transfer_info = if self.skip_fee_transfer {
236+
None
237+
} else {
238+
Some(execute_fee_transfer(
239+
state,
240+
block_context,
241+
&mut tx_execution_context,
242+
actual_fee,
243+
)?)
244+
};
245+
Ok((fee_transfer_info, actual_fee))
229246
}
230247

231248
fn handle_nonce<S: State + StateReader>(&self, state: &mut S) -> Result<(), TransactionError> {
@@ -280,6 +297,22 @@ impl Declare {
280297
),
281298
)
282299
}
300+
301+
pub(crate) fn create_for_simulation(
302+
&self,
303+
skip_validate: bool,
304+
skip_execute: bool,
305+
skip_fee_transfer: bool,
306+
) -> Transaction {
307+
let tx = Declare {
308+
skip_validate,
309+
skip_execute,
310+
skip_fee_transfer,
311+
..self.clone()
312+
};
313+
314+
Transaction::Declare(tx)
315+
}
283316
}
284317

285318
// ---------------

src/transaction/declare_v2.rs

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use cairo_vm::felt::Felt252;
2424
use num_traits::Zero;
2525
use starknet_contract_class::EntryPointType;
2626
use std::collections::HashMap;
27-
#[derive(Debug)]
27+
28+
use super::Transaction;
29+
#[derive(Debug, Clone)]
2830
pub struct DeclareV2 {
2931
pub sender_address: Address,
3032
pub tx_type: TransactionType,
@@ -37,6 +39,9 @@ pub struct DeclareV2 {
3739
pub sierra_contract_class: SierraContractClass,
3840
pub hash_value: Felt252,
3941
pub casm_class: once_cell::unsync::OnceCell<CasmContractClass>,
42+
pub skip_validate: bool,
43+
pub skip_execute: bool,
44+
pub skip_fee_transfer: bool,
4045
}
4146

4247
impl DeclareV2 {
@@ -79,6 +84,9 @@ impl DeclareV2 {
7984
compiled_class_hash,
8085
hash_value,
8186
casm_class: Default::default(),
87+
skip_execute: false,
88+
skip_validate: false,
89+
skip_fee_transfer: false,
8290
};
8391

8492
internal_declare.verify_version()?;
@@ -147,10 +155,18 @@ impl DeclareV2 {
147155

148156
let mut tx_execution_context =
149157
self.get_execution_context(block_context.invoke_tx_max_n_steps);
150-
let fee_transfer_info =
151-
execute_fee_transfer(state, block_context, &mut tx_execution_context, actual_fee)?;
158+
let fee_transfer_info = if self.skip_fee_transfer {
159+
None
160+
} else {
161+
Some(execute_fee_transfer(
162+
state,
163+
block_context,
164+
&mut tx_execution_context,
165+
actual_fee,
166+
)?)
167+
};
152168

153-
Ok((Some(fee_transfer_info), actual_fee))
169+
Ok((fee_transfer_info, actual_fee))
154170
}
155171

156172
// TODO: delete once used
@@ -185,17 +201,22 @@ impl DeclareV2 {
185201

186202
let mut resources_manager = ExecutionResourcesManager::default();
187203

188-
let (validate_info, _remaining_gas) = self.run_validate_entrypoint(
189-
initial_gas,
190-
state,
191-
&mut resources_manager,
192-
block_context,
193-
)?;
204+
let (validate_info, _remaining_gas) = if self.skip_validate {
205+
(None, 0)
206+
} else {
207+
let (info, gas) = self.run_validate_entrypoint(
208+
initial_gas,
209+
state,
210+
&mut resources_manager,
211+
block_context,
212+
)?;
213+
(Some(info), gas)
214+
};
194215

195216
let storage_changes = state.count_actual_storage_changes();
196217
let actual_resources = calculate_tx_resources(
197218
resources_manager,
198-
&[Some(validate_info.clone())],
219+
&[validate_info.clone()],
199220
self.tx_type,
200221
storage_changes,
201222
None,
@@ -207,7 +228,7 @@ impl DeclareV2 {
207228
self.charge_fee(state, &actual_resources, block_context)?;
208229

209230
let concurrent_exec_info = TransactionExecutionInfo::create_concurrent_stage_execution_info(
210-
Some(validate_info),
231+
validate_info,
211232
None,
212233
actual_resources,
213234
Some(self.tx_type),
@@ -263,19 +284,41 @@ impl DeclareV2 {
263284
let mut tx_execution_context =
264285
self.get_execution_context(block_context.validate_max_n_steps);
265286

266-
let call_info = entry_point.execute(
267-
state,
268-
block_context,
269-
resources_manager,
270-
&mut tx_execution_context,
271-
false,
272-
)?;
273-
287+
let call_info = if self.skip_execute {
288+
None
289+
} else {
290+
Some(entry_point.execute(
291+
state,
292+
block_context,
293+
resources_manager,
294+
&mut tx_execution_context,
295+
false,
296+
)?)
297+
};
298+
let call_info = verify_no_calls_to_other_contracts(&call_info)?;
274299
remaining_gas -= call_info.gas_consumed;
275-
verify_no_calls_to_other_contracts(&call_info)?;
276300

277301
Ok((call_info, remaining_gas))
278302
}
303+
304+
// ---------------
305+
// Simulation
306+
// ---------------
307+
pub(crate) fn create_for_simulation(
308+
&self,
309+
skip_validate: bool,
310+
skip_execute: bool,
311+
skip_fee_transfer: bool,
312+
) -> Transaction {
313+
let tx = DeclareV2 {
314+
skip_validate,
315+
skip_execute,
316+
skip_fee_transfer,
317+
..self.clone()
318+
};
319+
320+
Transaction::DeclareV2(Box::new(tx))
321+
}
279322
}
280323

281324
#[cfg(test)]

src/transaction/deploy.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ use cairo_vm::felt::Felt252;
2828
use num_traits::Zero;
2929
use starknet_contract_class::EntryPointType;
3030

31-
#[derive(Debug)]
31+
use super::Transaction;
32+
33+
#[derive(Debug, Clone)]
3234
pub struct Deploy {
3335
pub hash_value: Felt252,
3436
pub version: Felt252,
@@ -37,6 +39,9 @@ pub struct Deploy {
3739
pub contract_hash: ClassHash,
3840
pub constructor_calldata: Vec<Felt252>,
3941
pub tx_type: TransactionType,
42+
pub skip_validate: bool,
43+
pub skip_execute: bool,
44+
pub skip_fee_transfer: bool,
4045
}
4146

4247
impl Deploy {
@@ -77,6 +82,9 @@ impl Deploy {
7782
contract_hash,
7883
constructor_calldata,
7984
tx_type: TransactionType::Deploy,
85+
skip_validate: false,
86+
skip_execute: false,
87+
skip_fee_transfer: false,
8088
})
8189
}
8290

@@ -223,6 +231,25 @@ impl Deploy {
223231
),
224232
)
225233
}
234+
235+
// ---------------
236+
// Simulation
237+
// ---------------
238+
pub(crate) fn create_for_simulation(
239+
&self,
240+
skip_validate: bool,
241+
skip_execute: bool,
242+
skip_fee_transfer: bool,
243+
) -> Transaction {
244+
let tx = Deploy {
245+
skip_validate,
246+
skip_execute,
247+
skip_fee_transfer,
248+
..self.clone()
249+
};
250+
251+
Transaction::Deploy(tx)
252+
}
226253
}
227254

228255
#[cfg(test)]

0 commit comments

Comments
 (0)