Skip to content

Commit 67b8cfa

Browse files
starknet_os: os resources test - deploy account tx constant factor
1 parent 96d404c commit 67b8cfa

3 files changed

Lines changed: 87 additions & 23 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
@@ -502,11 +502,11 @@
502502
},
503503
"DeployAccount": {
504504
"constant": {
505-
"n_steps": 4583,
505+
"n_steps": 5020,
506506
"n_memory_holes": 0,
507507
"builtin_instance_counter": {
508-
"range_check_builtin": 93,
509-
"pedersen_builtin": 11,
508+
"range_check_builtin": 113,
509+
"pedersen_builtin": 12,
510510
"poseidon_builtin": 11
511511
}
512512
},

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
@@ -50,6 +50,16 @@ mod OsResourcesTestContract {
5050
starknet::VALIDATED
5151
}
5252

53+
#[external(v0)]
54+
fn __validate_deploy__(
55+
self: @ContractState,
56+
class_hash: felt252,
57+
contract_address_salt: felt252,
58+
some_args: Span<felt252>,
59+
) -> felt252 {
60+
starknet::VALIDATED
61+
}
62+
5363
#[external(v0)]
5464
fn __validate__(
5565
self: @ContractState,

crates/starknet_os_flow_tests/src/os_resources_test.rs

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ use indexmap::IndexMap;
2020
use starknet_api::block::StarknetVersion;
2121
use starknet_api::contract_class::compiled_class_hash::{HashVersion, HashableCompiledClass};
2222
use starknet_api::contract_class::{ClassInfo, ContractClass, SierraVersion};
23-
use starknet_api::core::{ClassHash, ContractAddress, EthAddress};
23+
use starknet_api::core::{ClassHash, ContractAddress, EthAddress, Nonce};
2424
use starknet_api::executable_transaction::{
2525
DeclareTransaction,
26+
DeployAccountTransaction,
2627
InvokeTransaction,
2728
TransactionType,
2829
};
2930
use starknet_api::test_utils::declare::declare_tx;
31+
use starknet_api::test_utils::deploy_account::deploy_account_tx;
3032
use starknet_api::test_utils::invoke::invoke_tx;
3133
use starknet_api::transaction::{L2ToL1Payload, MessageToL1};
3234
use starknet_api::versioned_constants_logic::VersionedConstantsTrait;
33-
use starknet_api::{calldata, declare_tx_args, invoke_tx_args};
35+
use starknet_api::{calldata, declare_tx_args, deploy_account_tx_args, invoke_tx_args};
3436
use starknet_os::hint_processor::constants::BUILTIN_INSTANCE_SIZES;
3537
use starknet_os::hint_processor::os_logger::ResourceFinalizer;
3638
use starknet_os::test_utils::{SHA256_BATCH_RESOURCES_LINEAR, SHA256_BLOCK_TO_ROUND};
@@ -473,10 +475,29 @@ async fn test_execute_txs_inner_resources() {
473475
let version = StarknetVersion::LATEST;
474476
let mut raw_vc: RawVersionedConstants =
475477
serde_json::from_str(VersionedConstants::json_str(&version).unwrap()).unwrap();
476-
// TODO(Dori): DeployAccount, L1Handler.
477-
const N_TXS: usize = 2;
478+
// TODO(Dori): L1Handler.
479+
const N_TXS: usize = 3;
478480

479-
let (os_resources_contract_address, _, mut test_builder) = setup_test_builder().await;
481+
let (os_resources_contract_address, deployable_class_hash, mut test_builder) =
482+
setup_test_builder().await;
483+
484+
// Prepare the deploy account tx in advance, so we can fund the address before moving to the
485+
// next block (just so the funding tx is not in our measurement block). Use the special contract
486+
// to prevent noise from changing contract address.
487+
let deploy_tx = DeployAccountTransaction::create(
488+
deploy_account_tx(
489+
deploy_account_tx_args! {
490+
class_hash: deployable_class_hash,
491+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
492+
constructor_calldata: calldata![Felt::ZERO],
493+
},
494+
Nonce::default(),
495+
),
496+
&test_builder.chain_id(),
497+
)
498+
.unwrap();
499+
test_builder.add_fund_address_tx_with_default_amount(deploy_tx.contract_address);
500+
test_builder.move_to_next_block();
480501

481502
// Invoke.
482503
// Calldata [0, 0, 0, []] → class_hash=0 → __execute__ returns immediately.
@@ -519,6 +540,9 @@ async fn test_execute_txs_inner_resources() {
519540
.unwrap();
520541
test_builder.add_cairo1_declare_tx(tx, data_gas_contract_sierra);
521542

543+
// Deploy account (pre-prepared).
544+
test_builder.add_deploy_account_tx(deploy_tx);
545+
522546
// Execute the business logic and extract the business logic resources for each tx.
523547
let test_runner = test_builder.build().await;
524548
let business_logic_resources: [ExecutionResources; N_TXS] = test_runner
@@ -551,21 +575,22 @@ async fn test_execute_txs_inner_resources() {
551575
test_output.perform_default_validations();
552576

553577
// Fetch the OS resources for each tx.
554-
let [invoke_overhead, declare_overhead]: [ExecutionResources; N_TXS] = test_output
555-
.runner_output
556-
.txs_trace
557-
.iter()
558-
.rev()
559-
.take(N_TXS)
560-
.rev()
561-
.map(|trace| trace.get_resources().unwrap().clone())
562-
.zip(business_logic_resources)
563-
.map(|(os_resources, business_logic_resources)| {
564-
(&os_resources - &business_logic_resources).filter_unused_builtins()
565-
})
566-
.collect::<Vec<_>>()
567-
.try_into()
568-
.unwrap();
578+
let [invoke_overhead, declare_overhead, deploy_account_overhead]: [ExecutionResources; N_TXS] =
579+
test_output
580+
.runner_output
581+
.txs_trace
582+
.iter()
583+
.rev()
584+
.take(N_TXS)
585+
.rev()
586+
.map(|trace| trace.get_resources().unwrap().clone())
587+
.zip(business_logic_resources)
588+
.map(|(os_resources, business_logic_resources)| {
589+
(&os_resources - &business_logic_resources).filter_unused_builtins()
590+
})
591+
.collect::<Vec<_>>()
592+
.try_into()
593+
.unwrap();
569594

570595
// Invoke: variable cost, with scaling of 2.
571596
// TODO(Dori): Compute linear factor cost.
@@ -612,6 +637,35 @@ async fn test_execute_txs_inner_resources() {
612637
.execute_txs_inner
613638
.insert(TransactionType::Declare, VariableResourceParams::Constant(declare_overhead));
614639

640+
// Deploy account: variable cost, with scaling of 2.
641+
// TODO(Dori): Compute linear factor cost.
642+
let VariableResourceParams::WithFactor(mut deploy_account_resources_params) =
643+
raw_vc.os_resources.execute_txs_inner.get(&TransactionType::DeployAccount).unwrap().clone()
644+
else {
645+
panic!(
646+
"Deploy account resources params has unexpected structure: {:?}",
647+
raw_vc.os_resources.execute_txs_inner.get(&TransactionType::DeployAccount).unwrap()
648+
);
649+
};
650+
let VariableCallDataFactor::Scaled(ref deploy_account_scaling_factor) =
651+
deploy_account_resources_params.calldata_factor
652+
else {
653+
panic!(
654+
"Deploy account scaling factor has unexpected structure: {:?}",
655+
deploy_account_resources_params.calldata_factor
656+
);
657+
};
658+
assert_eq!(
659+
deploy_account_scaling_factor.scaling_factor, 2,
660+
"Deploy account scaling factor has unexpected value: {:?}",
661+
deploy_account_scaling_factor.scaling_factor
662+
);
663+
deploy_account_resources_params.constant = deploy_account_overhead;
664+
raw_vc.os_resources.execute_txs_inner.insert(
665+
TransactionType::DeployAccount,
666+
VariableResourceParams::WithFactor(deploy_account_resources_params),
667+
);
668+
615669
// Verify computation.
616670
expect_file![VersionedConstants::json_path(&version).unwrap()]
617671
.assert_eq(&raw_vc.to_string_pretty());

0 commit comments

Comments
 (0)