Skip to content

Commit ecd0878

Browse files
feat(blockifier): exclude l1_data_gas from accounts in VC
1 parent b9e6c91 commit ecd0878

File tree

4 files changed

+85
-22
lines changed

4 files changed

+85
-22
lines changed

crates/blockifier/src/execution/syscalls/hint_processor.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,15 @@ impl ResourceAsFelts {
280280

281281
pub fn valid_resource_bounds_as_felts(
282282
resource_bounds: &ValidResourceBounds,
283+
exclude_l1_data_gas: bool,
283284
) -> SyscallResult<Vec<ResourceAsFelts>> {
284285
let mut resource_bounds_vec: Vec<_> = vec![
285286
ResourceAsFelts::new(Resource::L1Gas, &resource_bounds.get_l1_bounds())?,
286287
ResourceAsFelts::new(Resource::L2Gas, &resource_bounds.get_l2_bounds())?,
287288
];
289+
if exclude_l1_data_gas {
290+
return Ok(resource_bounds_vec);
291+
}
288292
if let ValidResourceBounds::AllResources(AllResourceBounds { l1_data_gas, .. }) =
289293
resource_bounds
290294
{
@@ -461,17 +465,21 @@ impl<'a> SyscallHintProcessor<'a> {
461465
) -> SyscallResult<Relocatable> {
462466
let returned_version = self.base.tx_version_for_get_execution_info();
463467
let original_version = self.base.context.tx_context.tx_info.signed_version();
468+
let exclude_l1_data_gas = self.base.exclude_l1_data_gas();
464469

465470
// If the transaction version was overridden, `self.execution_info_ptr` cannot be used.
466471
if returned_version != original_version {
467-
return self.allocate_execution_info_segment(vm, returned_version);
472+
return self.allocate_execution_info_segment(vm, returned_version, exclude_l1_data_gas);
468473
}
469474

470475
match self.execution_info_ptr {
471476
Some(execution_info_ptr) => Ok(execution_info_ptr),
472477
None => {
473-
let execution_info_ptr =
474-
self.allocate_execution_info_segment(vm, original_version)?;
478+
let execution_info_ptr = self.allocate_execution_info_segment(
479+
vm,
480+
original_version,
481+
exclude_l1_data_gas,
482+
)?;
475483
self.execution_info_ptr = Some(execution_info_ptr);
476484
Ok(execution_info_ptr)
477485
}
@@ -482,9 +490,10 @@ impl<'a> SyscallHintProcessor<'a> {
482490
&mut self,
483491
vm: &mut VirtualMachine,
484492
tx_info: &CurrentTransactionInfo,
493+
exclude_l1_data_gas: bool,
485494
) -> SyscallResult<(Relocatable, Relocatable)> {
486495
let flat_resource_bounds: Vec<_> =
487-
valid_resource_bounds_as_felts(&tx_info.resource_bounds)?
496+
valid_resource_bounds_as_felts(&tx_info.resource_bounds, exclude_l1_data_gas)?
488497
.into_iter()
489498
.flat_map(ResourceAsFelts::flatten)
490499
.collect();
@@ -593,9 +602,11 @@ impl<'a> SyscallHintProcessor<'a> {
593602
&mut self,
594603
vm: &mut VirtualMachine,
595604
tx_version_override: TransactionVersion,
605+
exclude_l1_data_gas: bool,
596606
) -> SyscallResult<Relocatable> {
597607
let block_info_ptr = self.allocate_block_info_segment(vm)?;
598-
let tx_info_ptr = self.allocate_tx_info_segment(vm, tx_version_override)?;
608+
let tx_info_ptr =
609+
self.allocate_tx_info_segment(vm, tx_version_override, exclude_l1_data_gas)?;
599610

600611
let additional_info: Vec<MaybeRelocatable> = vec![
601612
block_info_ptr.into(),
@@ -645,6 +656,7 @@ impl<'a> SyscallHintProcessor<'a> {
645656
&mut self,
646657
vm: &mut VirtualMachine,
647658
tx_version_override: TransactionVersion,
659+
exclude_l1_data_gas: bool,
648660
) -> SyscallResult<Relocatable> {
649661
let tx_info = &self.base.context.tx_context.clone().tx_info;
650662
let (tx_signature_start_ptr, tx_signature_end_ptr) =
@@ -667,7 +679,7 @@ impl<'a> SyscallHintProcessor<'a> {
667679
match tx_info {
668680
TransactionInfo::Current(context) => {
669681
let (tx_resource_bounds_start_ptr, tx_resource_bounds_end_ptr) =
670-
&self.allocate_tx_resource_bounds_segment(vm, context)?;
682+
&self.allocate_tx_resource_bounds_segment(vm, context, exclude_l1_data_gas)?;
671683

672684
let (tx_paymaster_data_start_ptr, tx_paymaster_data_end_ptr) =
673685
&self.allocate_data_segment(vm, &context.paymaster_data.0)?;

crates/blockifier/src/execution/syscalls/syscall_base.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ impl<'state> SyscallHandlerBase<'state> {
213213
tx_context.tx_info.signed_version()
214214
}
215215

216+
/// Return wether the class is in the data gas accounts set.
217+
pub fn exclude_l1_data_gas(&self) -> bool {
218+
let class_hash = self.call.class_hash;
219+
let versioned_constants = &self.context.tx_context.block_context.versioned_constants;
220+
versioned_constants.os_constants.data_gas_accounts.contains(&class_hash)
221+
}
222+
216223
pub fn emit_event(&mut self, event: EventContent) -> SyscallResult<()> {
217224
exceeds_event_size_limit(
218225
self.context.versioned_constants(),

crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use crate::transaction::objects::{
5151
TransactionVersion::ONE,
5252
false,
5353
false,
54+
false,
5455
false;
5556
"Native: Validate execution mode: block info fields should be zeroed. Transaction V1."
5657
)
@@ -63,6 +64,7 @@ use crate::transaction::objects::{
6364
TransactionVersion::ONE,
6465
false,
6566
false,
67+
false,
6668
false;
6769
"Native: Execute execution mode: block info should be as usual. Transaction V1."
6870
)
@@ -75,6 +77,7 @@ use crate::transaction::objects::{
7577
TransactionVersion::THREE,
7678
false,
7779
false,
80+
false,
7881
false;
7982
"Native: Validate execution mode: block info fields should be zeroed. Transaction V3."
8083
)
@@ -87,6 +90,7 @@ use crate::transaction::objects::{
8790
TransactionVersion::THREE,
8891
false,
8992
false,
93+
false,
9094
false;
9195
"Native: Execute execution mode: block info should be as usual. Transaction V3."
9296
)
@@ -99,6 +103,7 @@ use crate::transaction::objects::{
99103
TransactionVersion::ONE,
100104
false,
101105
false,
106+
false,
102107
false;
103108
"Native: Legacy contract. Execute execution mode: block info should be as usual. Transaction
104109
V1."
@@ -112,6 +117,7 @@ use crate::transaction::objects::{
112117
TransactionVersion::THREE,
113118
false,
114119
false,
120+
false,
115121
false;
116122
"Native: Legacy contract. Execute execution mode: block info should be as usual. Transaction
117123
V3."
@@ -125,6 +131,7 @@ use crate::transaction::objects::{
125131
TransactionVersion::THREE,
126132
true,
127133
false,
134+
false,
128135
false;
129136
"Native: Execute execution mode: block info should be as usual. Transaction V3. Query"
130137
)
@@ -137,6 +144,7 @@ use crate::transaction::objects::{
137144
TransactionVersion::THREE,
138145
false,
139146
true,
147+
false,
140148
false;
141149
"Native: V1 bound account: execute"
142150
)
@@ -149,6 +157,7 @@ use crate::transaction::objects::{
149157
TransactionVersion::THREE,
150158
true,
151159
true,
160+
false,
152161
false;
153162
"Native: V1 bound account: query"
154163
)
@@ -159,6 +168,7 @@ use crate::transaction::objects::{
159168
TransactionVersion::ONE,
160169
false,
161170
false,
171+
false,
162172
false;
163173
"Validate execution mode: block info fields should be zeroed. Transaction V1.")]
164174
#[test_case(
@@ -167,6 +177,7 @@ use crate::transaction::objects::{
167177
TransactionVersion::ONE,
168178
false,
169179
false,
180+
false,
170181
false;
171182
"Execute execution mode: block info should be as usual. Transaction V1.")]
172183
#[test_case(
@@ -175,6 +186,7 @@ use crate::transaction::objects::{
175186
TransactionVersion::THREE,
176187
false,
177188
false,
189+
false,
178190
false;
179191
"Validate execution mode: block info fields should be zeroed. Transaction V3.")]
180192
#[test_case(
@@ -183,6 +195,7 @@ use crate::transaction::objects::{
183195
TransactionVersion::THREE,
184196
false,
185197
false,
198+
false,
186199
false;
187200
"Execute execution mode: block info should be as usual. Transaction V3.")]
188201
#[test_case(
@@ -191,6 +204,7 @@ use crate::transaction::objects::{
191204
TransactionVersion::ONE,
192205
false,
193206
false,
207+
false,
194208
false;
195209
"Legacy contract. Execute execution mode: block info should be as usual. Transaction V1.")]
196210
#[test_case(
@@ -199,6 +213,7 @@ use crate::transaction::objects::{
199213
TransactionVersion::THREE,
200214
false,
201215
false,
216+
false,
202217
false;
203218
"Legacy contract. Execute execution mode: block info should be as usual. Transaction V3.")]
204219
#[test_case(
@@ -207,6 +222,7 @@ use crate::transaction::objects::{
207222
TransactionVersion::THREE,
208223
true,
209224
false,
225+
false,
210226
false;
211227
"Execute execution mode: block info should be as usual. Transaction V3. Query.")]
212228
#[test_case(
@@ -215,6 +231,7 @@ use crate::transaction::objects::{
215231
TransactionVersion::THREE,
216232
false,
217233
true,
234+
false,
218235
false;
219236
"V1 bound account: execute")]
220237
#[test_case(
@@ -223,16 +240,36 @@ use crate::transaction::objects::{
223240
TransactionVersion::THREE,
224241
false,
225242
true,
226-
true;
243+
true,
244+
false;
227245
"V1 bound account: execute, high tip")]
228246
#[test_case(
229247
FeatureContract::TestContract(CairoVersion::Cairo1(RunnableCairo1::Casm)),
230248
ExecutionMode::Execute,
231249
TransactionVersion::THREE,
232250
true,
233251
true,
252+
false,
234253
false;
235254
"V1 bound account: query")]
255+
#[test_case(
256+
FeatureContract::TestContract(CairoVersion::Cairo1(RunnableCairo1::Casm)),
257+
ExecutionMode::Execute,
258+
TransactionVersion::THREE,
259+
false,
260+
false,
261+
false,
262+
true;
263+
"Exclude l1 data gas: execute")]
264+
#[test_case(
265+
FeatureContract::TestContract(CairoVersion::Cairo1(RunnableCairo1::Casm)),
266+
ExecutionMode::Execute,
267+
TransactionVersion::THREE,
268+
true,
269+
false,
270+
false,
271+
true;
272+
"Exclude l1 data gas: query")]
236273
fn test_get_execution_info(
237274
test_contract: FeatureContract,
238275
execution_mode: ExecutionMode,
@@ -241,13 +278,21 @@ fn test_get_execution_info(
241278
v1_bound_account: bool,
242279
// Whether the tip is larger than `v1_bound_accounts_max_tip`.
243280
high_tip: bool,
281+
exclude_l1_data_gas: bool,
244282
) {
245283
let mut test_contract_data: FeatureContractData = test_contract.into();
246284
if v1_bound_account {
285+
assert!(
286+
!exclude_l1_data_gas,
287+
"Unable to set both exclude_l1_data_gas and v1_bound_account."
288+
);
247289
let optional_class_hash =
248290
VersionedConstants::latest_constants().os_constants.v1_bound_accounts_cairo1.first();
249291
test_contract_data.class_hash =
250292
*optional_class_hash.expect("No v1 bound accounts found in versioned constants.");
293+
} else if exclude_l1_data_gas {
294+
test_contract_data.class_hash =
295+
*VersionedConstants::latest_constants().os_constants.data_gas_accounts.first().unwrap();
251296
}
252297
let state =
253298
&mut test_state_ex(&ChainInfo::create_for_testing(), BALANCE, &[(test_contract_data, 1)]);
@@ -329,15 +374,17 @@ fn test_get_execution_info(
329374
(_, version) if version == TransactionVersion::ONE => vec![
330375
felt!(0_u16), // Length of resource bounds array.
331376
],
332-
(_, _) => vec![felt!(3_u8)] // Length of resource bounds array.
333-
.into_iter()
334-
.chain(
335-
valid_resource_bounds_as_felts(&all_resource_bounds)
336-
.unwrap()
337-
.into_iter()
338-
.flat_map(|bounds| bounds.flatten()),
339-
)
340-
.collect(),
377+
(_, _) => {
378+
vec![felt!(if exclude_l1_data_gas { 2_u8 } else { 3 })] // Length of resource bounds array.
379+
.into_iter()
380+
.chain(
381+
valid_resource_bounds_as_felts(&all_resource_bounds, exclude_l1_data_gas)
382+
.unwrap()
383+
.into_iter()
384+
.flat_map(|bounds| bounds.flatten()),
385+
)
386+
.collect()
387+
}
341388
};
342389

343390
let expected_tx_info: Vec<Felt>;

crates/starknet_os/src/hints/hint_implementation/execution/utils.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ impl<IG: IdentifierGetter> LoadCairoObject<IG> for ValidResourceBounds {
5050
address: Relocatable,
5151
constants: &HashMap<String, Felt>,
5252
) -> OsHintResult {
53-
valid_resource_bounds_as_felts(self).map_err(OsHintError::ResourceBoundsParsing)?.load_into(
54-
vm,
55-
identifier_getter,
56-
address,
57-
constants,
58-
)
53+
valid_resource_bounds_as_felts(self, false)
54+
.map_err(OsHintError::ResourceBoundsParsing)?
55+
.load_into(vm, identifier_getter, address, constants)
5956
}
6057
}

0 commit comments

Comments
 (0)