Skip to content

Commit dc5adbd

Browse files
starknet_os: os resources test - deploy account tx constant factor
1 parent 25d975a commit dc5adbd

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
@@ -494,11 +494,11 @@
494494
},
495495
"DeployAccount": {
496496
"constant": {
497-
"n_steps": 4583,
497+
"n_steps": 5020,
498498
"n_memory_holes": 0,
499499
"builtin_instance_counter": {
500-
"range_check_builtin": 93,
501-
"pedersen_builtin": 11,
500+
"range_check_builtin": 113,
501+
"pedersen_builtin": 12,
502502
"poseidon_builtin": 11
503503
}
504504
},

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};
@@ -446,10 +448,29 @@ async fn test_execute_txs_inner_resources() {
446448
let version = StarknetVersion::LATEST;
447449
let mut raw_vc: RawVersionedConstants =
448450
serde_json::from_str(VersionedConstants::json_str(&version).unwrap()).unwrap();
449-
// TODO(Dori): DeployAccount, L1Handler.
450-
const N_TXS: usize = 2;
451+
// TODO(Dori): L1Handler.
452+
const N_TXS: usize = 3;
451453

452-
let (os_resources_contract_address, _, mut test_builder) = setup_test_builder().await;
454+
let (os_resources_contract_address, deployable_class_hash, mut test_builder) =
455+
setup_test_builder().await;
456+
457+
// Prepare the deploy account tx in advance, so we can fund the address before moving to the
458+
// next block (just so the funding tx is not in our measurement block). Use the special contract
459+
// to prevent noise from changing contract address.
460+
let deploy_tx = DeployAccountTransaction::create(
461+
deploy_account_tx(
462+
deploy_account_tx_args! {
463+
class_hash: deployable_class_hash,
464+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
465+
constructor_calldata: calldata![Felt::ZERO],
466+
},
467+
Nonce::default(),
468+
),
469+
&test_builder.chain_id(),
470+
)
471+
.unwrap();
472+
test_builder.add_fund_address_tx_with_default_amount(deploy_tx.contract_address);
473+
test_builder.move_to_next_block();
453474

454475
// Invoke.
455476
// Calldata [0, 0, 0, []] → class_hash=0 → __execute__ returns immediately.
@@ -492,6 +513,9 @@ async fn test_execute_txs_inner_resources() {
492513
.unwrap();
493514
test_builder.add_cairo1_declare_tx(tx, data_gas_contract_sierra);
494515

516+
// Deploy account (pre-prepared).
517+
test_builder.add_deploy_account_tx(deploy_tx);
518+
495519
// Execute the business logic and extract the business logic resources for each tx.
496520
let test_runner = test_builder.build().await;
497521
let business_logic_resources: [ExecutionResources; N_TXS] = test_runner
@@ -524,21 +548,22 @@ async fn test_execute_txs_inner_resources() {
524548
test_output.perform_default_validations();
525549

526550
// Fetch the OS resources for each tx.
527-
let [invoke_overhead, declare_overhead]: [ExecutionResources; N_TXS] = test_output
528-
.runner_output
529-
.txs_trace
530-
.iter()
531-
.rev()
532-
.take(N_TXS)
533-
.rev()
534-
.map(|trace| trace.get_resources().unwrap().clone())
535-
.zip(business_logic_resources)
536-
.map(|(os_resources, business_logic_resources)| {
537-
(&os_resources - &business_logic_resources).filter_unused_builtins()
538-
})
539-
.collect::<Vec<_>>()
540-
.try_into()
541-
.unwrap();
551+
let [invoke_overhead, declare_overhead, deploy_account_overhead]: [ExecutionResources; N_TXS] =
552+
test_output
553+
.runner_output
554+
.txs_trace
555+
.iter()
556+
.rev()
557+
.take(N_TXS)
558+
.rev()
559+
.map(|trace| trace.get_resources().unwrap().clone())
560+
.zip(business_logic_resources)
561+
.map(|(os_resources, business_logic_resources)| {
562+
(&os_resources - &business_logic_resources).filter_unused_builtins()
563+
})
564+
.collect::<Vec<_>>()
565+
.try_into()
566+
.unwrap();
542567

543568
// Invoke: variable cost, with scaling of 2.
544569
// TODO(Dori): Compute linear factor cost.
@@ -585,6 +610,35 @@ async fn test_execute_txs_inner_resources() {
585610
.execute_txs_inner
586611
.insert(TransactionType::Declare, VariableResourceParams::Constant(declare_overhead));
587612

613+
// Deploy account: variable cost, with scaling of 2.
614+
// TODO(Dori): Compute linear factor cost.
615+
let VariableResourceParams::WithFactor(mut deploy_account_resources_params) =
616+
raw_vc.os_resources.execute_txs_inner.get(&TransactionType::DeployAccount).unwrap().clone()
617+
else {
618+
panic!(
619+
"Deploy account resources params has unexpected structure: {:?}",
620+
raw_vc.os_resources.execute_txs_inner.get(&TransactionType::DeployAccount).unwrap()
621+
);
622+
};
623+
let VariableCallDataFactor::Scaled(ref deploy_account_scaling_factor) =
624+
deploy_account_resources_params.calldata_factor
625+
else {
626+
panic!(
627+
"Deploy account scaling factor has unexpected structure: {:?}",
628+
deploy_account_resources_params.calldata_factor
629+
);
630+
};
631+
assert_eq!(
632+
deploy_account_scaling_factor.scaling_factor, 2,
633+
"Deploy account scaling factor has unexpected value: {:?}",
634+
deploy_account_scaling_factor.scaling_factor
635+
);
636+
deploy_account_resources_params.constant = deploy_account_overhead;
637+
raw_vc.os_resources.execute_txs_inner.insert(
638+
TransactionType::DeployAccount,
639+
VariableResourceParams::WithFactor(deploy_account_resources_params),
640+
);
641+
588642
// Verify computation.
589643
expect_file![VersionedConstants::json_path(&version).unwrap()]
590644
.assert_eq(&raw_vc.to_string_pretty());

0 commit comments

Comments
 (0)