Skip to content

Commit ff31b35

Browse files
starknet_os: os resources test - deploy account tx constant factor
1 parent 22acdc7 commit ff31b35

3 files changed

Lines changed: 98 additions & 25 deletions

File tree

crates/blockifier/resources/blockifier_versioned_constants_0_14_4.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,11 @@
503503
},
504504
"DeployAccount": {
505505
"constant": {
506-
"n_steps": 4583,
506+
"n_steps": 5026,
507507
"n_memory_holes": 0,
508508
"builtin_instance_counter": {
509-
"range_check_builtin": 93,
510-
"pedersen_builtin": 11,
509+
"range_check_builtin": 113,
510+
"pedersen_builtin": 12,
511511
"poseidon_builtin": 11
512512
}
513513
},

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
@@ -46,6 +46,16 @@ mod OsResourcesTestContract {
4646
starknet::VALIDATED
4747
}
4848

49+
#[external(v0)]
50+
fn __validate_deploy__(
51+
self: @ContractState,
52+
class_hash: felt252,
53+
contract_address_salt: felt252,
54+
some_args: Span<felt252>,
55+
) -> felt252 {
56+
starknet::VALIDATED
57+
}
58+
4959
#[external(v0)]
5060
fn __validate__(
5161
self: @ContractState, stable_class_hash: ClassHash, stable_address: ContractAddress,

crates/starknet_os_flow_tests/src/os_resources_test.rs

Lines changed: 85 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ use expect_test::expect_file;
1919
use indexmap::IndexMap;
2020
use starknet_api::block::StarknetVersion;
2121
use starknet_api::contract_class::SierraVersion;
22-
use starknet_api::core::{ClassHash, ContractAddress, EthAddress};
23-
use starknet_api::executable_transaction::{InvokeTransaction, TransactionType};
22+
use starknet_api::core::{ClassHash, ContractAddress, EthAddress, Nonce};
23+
use starknet_api::executable_transaction::{
24+
DeployAccountTransaction,
25+
InvokeTransaction,
26+
TransactionType,
27+
};
28+
use starknet_api::test_utils::deploy_account::deploy_account_tx;
2429
use starknet_api::test_utils::invoke::invoke_tx;
2530
use starknet_api::transaction::fields::ContractAddressSalt;
2631
use starknet_api::transaction::{L2ToL1Payload, MessageToL1};
2732
use starknet_api::versioned_constants_logic::VersionedConstantsTrait;
28-
use starknet_api::{calldata, declare_tx_args, invoke_tx_args};
33+
use starknet_api::{calldata, declare_tx_args, deploy_account_tx_args, invoke_tx_args};
2934
use starknet_os::hint_processor::constants::BUILTIN_INSTANCE_SIZES;
3035
use starknet_os::hint_processor::os_logger::ResourceFinalizer;
3136
use starknet_os::test_utils::{SHA256_BATCH_RESOURCES_LINEAR, SHA256_BLOCK_TO_ROUND};
@@ -482,11 +487,36 @@ async fn test_execute_txs_inner_resources() {
482487
let version = StarknetVersion::LATEST;
483488
let mut raw_vc: RawVersionedConstants =
484489
serde_json::from_str(VersionedConstants::json_str(&version).unwrap()).unwrap();
485-
// TODO(Dori): DeployAccount, L1Handler.
486-
const N_TXS: usize = 2;
490+
// TODO(Dori): L1Handler.
491+
const N_TXS: usize = 3;
487492

488-
let OsResourcesTestSetup { stable_contract_address, mut test_builder, .. } =
489-
setup_test_builder().await;
493+
let OsResourcesTestSetup {
494+
stable_contract_address,
495+
stable_contract_class_hash,
496+
mut test_builder,
497+
..
498+
} = setup_test_builder().await;
499+
500+
// Prepare the deploy account tx in advance, so we can fund the address before moving to the
501+
// next block (just so the funding tx is not in our measurement block). Use the stable contract
502+
// to prevent noise from changing contract address.
503+
let deploy_tx = DeployAccountTransaction::create(
504+
deploy_account_tx(
505+
deploy_account_tx_args! {
506+
class_hash: stable_contract_class_hash,
507+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
508+
constructor_calldata: calldata![Felt::ZERO],
509+
// The stable contract was already deployed (from deployer address zero) with
510+
// trivial salt, so use non-trivial salt to get a new address.
511+
contract_address_salt: ContractAddressSalt(Felt::from(100)),
512+
},
513+
Nonce::default(),
514+
),
515+
&test_builder.chain_id(),
516+
)
517+
.unwrap();
518+
test_builder.add_fund_address_tx_with_default_amount(deploy_tx.contract_address);
519+
test_builder.move_to_next_block();
490520

491521
// Invoke.
492522
let invoke_args = invoke_tx_args! {
@@ -514,6 +544,9 @@ async fn test_execute_txs_inner_resources() {
514544
&test_builder.chain_id(),
515545
);
516546

547+
// Deploy account (pre-prepared).
548+
test_builder.add_deploy_account_tx(deploy_tx);
549+
517550
// Execute the business logic and extract the business logic resources for each tx.
518551
let test_runner = test_builder.build().await;
519552
let business_logic_resources: [ExecutionResources; N_TXS] = test_runner
@@ -546,21 +579,22 @@ async fn test_execute_txs_inner_resources() {
546579
test_output.perform_default_validations();
547580

548581
// Fetch the OS resources for each tx.
549-
let [invoke_overhead, declare_overhead]: [ExecutionResources; N_TXS] = test_output
550-
.runner_output
551-
.txs_trace
552-
.iter()
553-
.rev()
554-
.take(N_TXS)
555-
.rev()
556-
.map(|trace| trace.get_resources().unwrap().clone())
557-
.zip(business_logic_resources)
558-
.map(|(os_resources, business_logic_resources)| {
559-
(&os_resources - &business_logic_resources).filter_unused_builtins()
560-
})
561-
.collect::<Vec<_>>()
562-
.try_into()
563-
.unwrap();
582+
let [invoke_overhead, declare_overhead, deploy_account_overhead]: [ExecutionResources; N_TXS] =
583+
test_output
584+
.runner_output
585+
.txs_trace
586+
.iter()
587+
.rev()
588+
.take(N_TXS)
589+
.rev()
590+
.map(|trace| trace.get_resources().unwrap().clone())
591+
.zip(business_logic_resources)
592+
.map(|(os_resources, business_logic_resources)| {
593+
(&os_resources - &business_logic_resources).filter_unused_builtins()
594+
})
595+
.collect::<Vec<_>>()
596+
.try_into()
597+
.unwrap();
564598

565599
// Invoke: variable cost, with scaling of 2.
566600
// TODO(Dori): Compute linear factor cost.
@@ -607,6 +641,35 @@ async fn test_execute_txs_inner_resources() {
607641
.execute_txs_inner
608642
.insert(TransactionType::Declare, VariableResourceParams::Constant(declare_overhead));
609643

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

0 commit comments

Comments
 (0)