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

Commit 4ba3de3

Browse files
fmolettajuanbono
andauthored
Execute L1Handler transactions using the RpcState (#1103)
* Add test case * Fix get_onchain_data_segment_length * Debug setup * Fix get_onchain_data_segment_length * Add StorageChangesCount struct * Update test values * Update test values * fmt * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Add test case with declare * Deserialize Declare transactions * Create blockifier Declare transaction * Fix/Refactor `State::count actual storage changes` (#1086) * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add the ability to execute `DeployAccount` transactions using the `RpcState` (#1089) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Fix bug in `From<StarknetRsContractClass> for CompiledClass` implementation (#1090) * Fix bug in CompiledClass * Add tests * fetch class hash from the next block in declare tx * Return an error if a class_hash is not declared + add tests for declare tx * Fix error msg * Add support for DeclareV0-1 in sir_tests * Make Sierra class optional in declare v2 + other changes * Add support for DeclareV2 * Uncomment test * fix * Use new_with_sierra_class_hash_and_tx_hash * use CompiledClassHash instead of CompiledClass where applicatble * Handle nonce in declare v2 + run fmt * Set casm class before counting state changes in declare v2 * Changes * Make sierra class hash non-optional * fix + clippy * Use state_reader instead of creating a state to fetch the next block s contract classes * Add removed test * Update test values * Make validate_invocation and fee_transfer_info fields optional + add L1_HANDLER transaction RpcState * Add L1Handler to blockifier_tests::execute_tx * Add blockifier test case * Add L1Handler to sir_tests::execute_tx * Add one more test case * fmt --------- Co-authored-by: Juan Bono <juanbono94@gmail.com>
1 parent 152a3e4 commit 4ba3de3

File tree

6 files changed

+107
-17
lines changed

6 files changed

+107
-17
lines changed

rpc_state_reader/src/lib.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ mod tests {
138138
);
139139

140140
assert_eq!(
141-
tx_trace.validate_invocation.calldata,
141+
tx_trace.validate_invocation.as_ref().unwrap().calldata,
142142
Some(vec![
143143
stark_felt!("1"),
144144
stark_felt!("690c876e61beda61e994543af68038edac4e1cb1990ab06e52a2d27e56a1232"),
@@ -157,9 +157,16 @@ mod tests {
157157
stark_felt!("38bd34c31a0a5c"),
158158
])
159159
);
160-
assert_eq!(tx_trace.validate_invocation.retdata, Some(vec![]));
160+
assert_eq!(
161+
tx_trace.validate_invocation.as_ref().unwrap().retdata,
162+
Some(vec![])
163+
);
161164
assert_eq_sorted!(
162-
tx_trace.validate_invocation.execution_resources,
165+
tx_trace
166+
.validate_invocation
167+
.as_ref()
168+
.unwrap()
169+
.execution_resources,
163170
ExecutionResources {
164171
n_steps: 790,
165172
n_memory_holes: 51,
@@ -170,7 +177,15 @@ mod tests {
170177
]),
171178
}
172179
);
173-
assert_eq!(tx_trace.validate_invocation.internal_calls.len(), 1);
180+
assert_eq!(
181+
tx_trace
182+
.validate_invocation
183+
.as_ref()
184+
.unwrap()
185+
.internal_calls
186+
.len(),
187+
1
188+
);
174189

175190
assert_eq!(
176191
tx_trace.function_invocation.as_ref().unwrap().calldata,
@@ -243,19 +258,23 @@ mod tests {
243258
);
244259

245260
assert_eq!(
246-
tx_trace.fee_transfer_invocation.calldata,
261+
tx_trace.fee_transfer_invocation.as_ref().unwrap().calldata,
247262
Some(vec![
248263
stark_felt!("1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8"),
249264
stark_felt!("2b0322a23ba4"),
250265
stark_felt!("0"),
251266
])
252267
);
253268
assert_eq!(
254-
tx_trace.fee_transfer_invocation.retdata,
269+
tx_trace.fee_transfer_invocation.as_ref().unwrap().retdata,
255270
Some(vec![1u128.into()])
256271
);
257272
assert_eq_sorted!(
258-
tx_trace.fee_transfer_invocation.execution_resources,
273+
tx_trace
274+
.fee_transfer_invocation
275+
.as_ref()
276+
.unwrap()
277+
.execution_resources,
259278
ExecutionResources {
260279
n_steps: 586,
261280
n_memory_holes: 42,
@@ -265,7 +284,15 @@ mod tests {
265284
]),
266285
}
267286
);
268-
assert_eq!(tx_trace.fee_transfer_invocation.internal_calls.len(), 1);
287+
assert_eq!(
288+
tx_trace
289+
.fee_transfer_invocation
290+
.as_ref()
291+
.unwrap()
292+
.internal_calls
293+
.len(),
294+
1
295+
);
269296
}
270297

271298
#[test]

rpc_state_reader/src/rpc_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ pub struct RpcResponse<T> {
158158

159159
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
160160
pub struct TransactionTrace {
161-
pub validate_invocation: RpcCallInfo,
161+
pub validate_invocation: Option<RpcCallInfo>,
162162
pub function_invocation: Option<RpcCallInfo>,
163-
pub fee_transfer_invocation: RpcCallInfo,
163+
pub fee_transfer_invocation: Option<RpcCallInfo>,
164164
pub signature: Vec<StarkFelt>,
165165
pub revert_error: Option<String>,
166166
}

rpc_state_reader/src/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub fn deserialize_transaction_json(
9999
"unimplemented declare version: {x}"
100100
))),
101101
},
102+
"L1_HANDLER" => Ok(Transaction::L1Handler(serde_json::from_value(transaction)?)),
102103
x => Err(serde::de::Error::custom(format!(
103104
"unimplemented transaction type deserialization: {x}"
104105
))),

rpc_state_reader/tests/blockifier_tests.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use blockifier::{
99
transaction::{
1010
account_transaction::AccountTransaction,
1111
objects::TransactionExecutionInfo,
12-
transactions::{DeclareTransaction, DeployAccountTransaction, ExecutableTransaction},
12+
transactions::{
13+
DeclareTransaction, DeployAccountTransaction, ExecutableTransaction,
14+
L1HandlerTransaction,
15+
},
1316
},
1417
};
1518
use blockifier::{
@@ -207,6 +210,21 @@ pub fn execute_tx(
207210
let declare = DeclareTransaction::new(tx, tx_hash, contract_class).unwrap();
208211
AccountTransaction::Declare(declare)
209212
}
213+
SNTransaction::L1Handler(tx) => {
214+
// As L1Hanlder is not an account transaction we execute it here and return the result
215+
let blockifier_tx = L1HandlerTransaction {
216+
tx,
217+
tx_hash,
218+
paid_fee_on_l1: starknet_api::transaction::Fee(u128::MAX),
219+
};
220+
return (
221+
blockifier_tx
222+
.execute(&mut state, &block_context, true, true)
223+
.unwrap(),
224+
trace,
225+
receipt,
226+
);
227+
}
210228
_ => unimplemented!(),
211229
};
212230

@@ -327,6 +345,16 @@ fn blockifier_test_recent_tx() {
327345
281513, // real block 281514
328346
RpcChain::MainNet
329347
)]
348+
#[test_case(
349+
"0x26be3e906db66973de1ca5eec1ddb4f30e3087dbdce9560778937071c3d3a83",
350+
351268, // real block 351269
351+
RpcChain::MainNet
352+
)]
353+
#[test_case(
354+
"0x4f552c9430bd21ad300db56c8f4cae45d554a18fac20bf1703f180fac587d7e",
355+
351225, // real block 351226
356+
RpcChain::MainNet
357+
)]
330358
// DeployAccount for different account providers (as of October 2023):
331359
// All of them were deployed on testnet using starkli
332360
// OpenZeppelin (v0.7.0)

rpc_state_reader/tests/sir_tests.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use starknet_in_rust::{
2828
state_cache::StorageEntry,
2929
BlockInfo,
3030
},
31-
transaction::{Declare, DeclareV2, DeployAccount, InvokeFunction},
31+
transaction::{Declare, DeclareV2, DeployAccount, InvokeFunction, L1Handler},
3232
utils::{Address, ClassHash},
3333
};
3434

@@ -208,6 +208,13 @@ pub fn execute_tx_configurable(
208208
declare.create_for_simulation(skip_validate, false, false, false)
209209
}
210210
}
211+
SNTransaction::L1Handler(tx) => L1Handler::from_sn_api_tx(
212+
tx,
213+
Felt252::from_bytes_be(tx_hash.0.bytes()),
214+
Some(Felt252::from(u128::MAX)),
215+
)
216+
.unwrap()
217+
.create_for_simulation(skip_validate, false),
211218
_ => unimplemented!(),
212219
};
213220

@@ -352,6 +359,16 @@ fn test_get_gas_price() {
352359
281513, // real block 281514
353360
RpcChain::MainNet
354361
)]
362+
#[test_case(
363+
"0x26be3e906db66973de1ca5eec1ddb4f30e3087dbdce9560778937071c3d3a83",
364+
351268, // real block 351269
365+
RpcChain::MainNet
366+
)]
367+
#[test_case(
368+
"0x4f552c9430bd21ad300db56c8f4cae45d554a18fac20bf1703f180fac587d7e",
369+
351225, // real block 351226
370+
RpcChain::MainNet
371+
)]
355372
// DeployAccount for different account providers (as of October 2023):
356373
// All of them were deployed on testnet using starkli
357374
// OpenZeppelin (v0.7.0)

src/transaction/l1_handler.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,7 @@ impl L1Handler {
200200
}
201201

202202
/// Creates a L1Handler for simulation purposes.
203-
pub(crate) fn create_for_simulation(
204-
&self,
205-
skip_validate: bool,
206-
skip_execute: bool,
207-
) -> Transaction {
203+
pub fn create_for_simulation(&self, skip_validate: bool, skip_execute: bool) -> Transaction {
208204
let tx = L1Handler {
209205
skip_validate,
210206
skip_execute,
@@ -213,6 +209,27 @@ impl L1Handler {
213209

214210
Transaction::L1Handler(tx)
215211
}
212+
213+
/// Creates a `L1Handler` from a starknet api `L1HandlerTransaction`.
214+
pub fn from_sn_api_tx(
215+
tx: starknet_api::transaction::L1HandlerTransaction,
216+
tx_hash: Felt252,
217+
paid_fee_on_l1: Option<Felt252>,
218+
) -> Result<Self, TransactionError> {
219+
L1Handler::new_with_tx_hash(
220+
Address(Felt252::from_bytes_be(tx.contract_address.0.key().bytes())),
221+
Felt252::from_bytes_be(tx.entry_point_selector.0.bytes()),
222+
tx.calldata
223+
.0
224+
.as_ref()
225+
.iter()
226+
.map(|f| Felt252::from_bytes_be(f.bytes()))
227+
.collect(),
228+
Felt252::from_bytes_be(tx.nonce.0.bytes()),
229+
paid_fee_on_l1,
230+
tx_hash,
231+
)
232+
}
216233
}
217234

218235
#[cfg(test)]

0 commit comments

Comments
 (0)