Skip to content

Commit 4a8df3d

Browse files
starknet_os: os resources test - deploy account tx constant factor
1 parent f4340dd commit 4a8df3d

3 files changed

Lines changed: 87 additions & 22 deletions

File tree

crates/blockifier/resources/blockifier_versioned_constants_0_14_3.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,11 @@
495495
},
496496
"DeployAccount": {
497497
"constant": {
498-
"n_steps": 4583,
498+
"n_steps": 5018,
499499
"n_memory_holes": 0,
500500
"builtin_instance_counter": {
501-
"range_check_builtin": 93,
502-
"pedersen_builtin": 11,
501+
"range_check_builtin": 113,
502+
"pedersen_builtin": 12,
503503
"poseidon_builtin": 11
504504
}
505505
},

crates/blockifier_test_utils/resources/feature_contracts/cairo1/os_resources_test_contract.cairo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ mod OsResourcesTestContract {
5959
starknet::VALIDATED
6060
}
6161

62+
#[external(v0)]
63+
fn __validate_deploy__(
64+
self: @ContractState,
65+
class_hash: felt252,
66+
contract_address_salt: felt252,
67+
some_args: Span<felt252>,
68+
) -> felt252 {
69+
starknet::VALIDATED
70+
}
71+
6272
#[external(v0)]
6373
fn __validate__(
6474
self: @ContractState,

crates/starknet_os_flow_tests/src/os_resources_test.rs

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ use indexmap::IndexMap;
2121
use starknet_api::block::StarknetVersion;
2222
use starknet_api::contract_class::compiled_class_hash::HashVersion;
2323
use starknet_api::contract_class::SierraVersion;
24-
use starknet_api::core::{ContractAddress, EthAddress};
24+
use starknet_api::core::{ContractAddress, EthAddress, Nonce};
2525
use starknet_api::executable_transaction::{
2626
DeclareTransaction,
27+
DeployAccountTransaction,
2728
InvokeTransaction,
2829
TransactionType,
2930
};
3031
use starknet_api::test_utils::declare::declare_tx;
32+
use starknet_api::test_utils::deploy_account::deploy_account_tx;
3133
use starknet_api::test_utils::invoke::invoke_tx;
3234
use starknet_api::transaction::{L2ToL1Payload, MessageToL1};
3335
use starknet_api::versioned_constants_logic::VersionedConstantsTrait;
34-
use starknet_api::{calldata, declare_tx_args, invoke_tx_args};
36+
use starknet_api::{calldata, declare_tx_args, deploy_account_tx_args, invoke_tx_args};
3537
use starknet_os::hint_processor::constants::BUILTIN_INSTANCE_SIZES;
3638
use starknet_os::hint_processor::os_logger::ResourceFinalizer;
3739
use starknet_os::test_utils::{SHA256_BATCH_RESOURCES_LINEAR, SHA256_BLOCK_TO_ROUND};
@@ -400,11 +402,31 @@ async fn test_execute_txs_inner_resources() {
400402
let version = StarknetVersion::LATEST;
401403
let mut raw_vc: RawVersionedConstants =
402404
serde_json::from_str(VersionedConstants::json_str(&version).unwrap()).unwrap();
403-
// TODO(Dori): DeployAccount, L1Handler.
404-
const N_TXS: usize = 2;
405+
// TODO(Dori): L1Handler.
406+
const N_TXS: usize = 3;
405407

406408
let (os_resources_contract_address, mut test_builder) = setup_test_builder().await;
407409

410+
// Prepare the deploy account tx in advance, so we can fund the address before moving to the
411+
// next block (just so the funding tx is not in our measurement block). The OS resources
412+
// contract is also an account contract, so we can use it to deploy an account.
413+
let os_resources_contract = FeatureContract::OsResourcesTest(RunnableCairo1::Casm);
414+
let class_hash = get_class_hash_of_feature_contract(os_resources_contract);
415+
let deploy_tx = DeployAccountTransaction::create(
416+
deploy_account_tx(
417+
deploy_account_tx_args! {
418+
class_hash,
419+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
420+
constructor_calldata: calldata![Felt::ZERO],
421+
},
422+
Nonce::default(),
423+
),
424+
&test_builder.chain_id(),
425+
)
426+
.unwrap();
427+
test_builder.add_fund_address_tx_with_default_amount(deploy_tx.contract_address);
428+
test_builder.move_to_next_block();
429+
408430
// Invoke.
409431
// Calldata [0, 0, 0] → class_hash=0 → __execute__ returns immediately.
410432
let invoke_args = invoke_tx_args! {
@@ -439,6 +461,9 @@ async fn test_execute_txs_inner_resources() {
439461
.unwrap();
440462
test_builder.add_cairo1_declare_tx(tx, &sierra);
441463

464+
// Deploy account (pre-prepared).
465+
test_builder.add_deploy_account_tx(deploy_tx);
466+
442467
// Execute the business logic and extract the business logic resources for each tx.
443468
let test_runner = test_builder.build().await;
444469
let business_logic_resources: [ExecutionResources; N_TXS] = test_runner
@@ -471,21 +496,22 @@ async fn test_execute_txs_inner_resources() {
471496
test_output.perform_default_validations();
472497

473498
// Fetch the OS resources for each tx.
474-
let [invoke_overhead, declare_overhead]: [ExecutionResources; N_TXS] = test_output
475-
.runner_output
476-
.txs_trace
477-
.iter()
478-
.rev()
479-
.take(N_TXS)
480-
.rev()
481-
.map(|trace| trace.get_resources().unwrap().clone())
482-
.zip(business_logic_resources)
483-
.map(|(os_resources, business_logic_resources)| {
484-
(&os_resources - &business_logic_resources).filter_unused_builtins()
485-
})
486-
.collect::<Vec<_>>()
487-
.try_into()
488-
.unwrap();
499+
let [invoke_overhead, declare_overhead, deploy_account_overhead]: [ExecutionResources; N_TXS] =
500+
test_output
501+
.runner_output
502+
.txs_trace
503+
.iter()
504+
.rev()
505+
.take(N_TXS)
506+
.rev()
507+
.map(|trace| trace.get_resources().unwrap().clone())
508+
.zip(business_logic_resources)
509+
.map(|(os_resources, business_logic_resources)| {
510+
(&os_resources - &business_logic_resources).filter_unused_builtins()
511+
})
512+
.collect::<Vec<_>>()
513+
.try_into()
514+
.unwrap();
489515

490516
// Invoke: variable cost, with scaling of 2.
491517
// TODO(Dori): Compute linear factor cost.
@@ -532,6 +558,35 @@ async fn test_execute_txs_inner_resources() {
532558
.execute_txs_inner
533559
.insert(TransactionType::Declare, VariableResourceParams::Constant(declare_overhead));
534560

561+
// Deploy account: variable cost, with scaling of 2.
562+
// TODO(Dori): Compute linear factor cost.
563+
let VariableResourceParams::WithFactor(mut deploy_account_resources_params) =
564+
raw_vc.os_resources.execute_txs_inner.get(&TransactionType::DeployAccount).unwrap().clone()
565+
else {
566+
panic!(
567+
"Deploy account resources params has unexpected structure: {:?}",
568+
raw_vc.os_resources.execute_txs_inner.get(&TransactionType::DeployAccount).unwrap()
569+
);
570+
};
571+
let VariableCallDataFactor::Scaled(ref deploy_account_scaling_factor) =
572+
deploy_account_resources_params.calldata_factor
573+
else {
574+
panic!(
575+
"Deploy account scaling factor has unexpected structure: {:?}",
576+
deploy_account_resources_params.calldata_factor
577+
);
578+
};
579+
assert_eq!(
580+
deploy_account_scaling_factor.scaling_factor, 2,
581+
"Deploy account scaling factor has unexpected value: {:?}",
582+
deploy_account_scaling_factor.scaling_factor
583+
);
584+
deploy_account_resources_params.constant = deploy_account_overhead;
585+
raw_vc.os_resources.execute_txs_inner.insert(
586+
TransactionType::DeployAccount,
587+
VariableResourceParams::WithFactor(deploy_account_resources_params),
588+
);
589+
535590
// Verify computation.
536591
expect_file![VersionedConstants::json_path(&version).unwrap()]
537592
.assert_eq(&raw_vc.to_string_pretty());

0 commit comments

Comments
 (0)