Skip to content

Commit e7199e0

Browse files
starknet_os_flow_tests: migrate test_v1_bound_accounts_cairo0
1 parent 210b6e4 commit e7199e0

File tree

3 files changed

+87
-6
lines changed

3 files changed

+87
-6
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/starknet_os_flow_tests/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ cairo-lang-starknet-classes.workspace = true
1313
itertools.workspace = true
1414
rstest.workspace = true
1515
serde_json.workspace = true
16+
starknet-core.workspace = true
17+
starknet-crypto.workspace = true
1618
starknet-types-core.workspace = true
1719
starknet_api.workspace = true
1820
starknet_committer = { workspace = true, features = ["testing"] }

crates/starknet_os_flow_tests/src/tests.rs

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ use starknet_api::core::{
1919
};
2020
use starknet_api::executable_transaction::{
2121
DeclareTransaction,
22+
InvokeTransaction,
2223
L1HandlerTransaction as ExecutableL1HandlerTransaction,
2324
};
2425
use starknet_api::execution_resources::GasAmount;
2526
use starknet_api::state::StorageKey;
2627
use starknet_api::test_utils::declare::declare_tx;
28+
use starknet_api::test_utils::invoke::invoke_tx;
2729
use starknet_api::test_utils::{
2830
CHAIN_ID_FOR_TESTS,
2931
CURRENT_BLOCK_TIMESTAMP,
@@ -56,6 +58,8 @@ use starknet_committer::block_committer::input::{
5658
StateDiff,
5759
};
5860
use starknet_committer::patricia_merkle_tree::types::CompiledClassHash;
61+
use starknet_core::crypto::ecdsa_sign;
62+
use starknet_crypto::{get_public_key, Signature};
5963
use starknet_os::hints::hint_implementation::deprecated_compiled_class::class_hash::compute_deprecated_class_hash;
6064
use starknet_os::io::os_output::MessageToL2;
6165
use starknet_types_core::felt::Felt;
@@ -69,6 +73,7 @@ use crate::test_manager::{TestManager, TestParameters, FUNDED_ACCOUNT_ADDRESS};
6973
use crate::utils::{
7074
divide_vec_into_n_parts,
7175
get_class_hash_of_feature_contract,
76+
get_class_info_of_cairo0_contract,
7277
get_class_info_of_feature_contract,
7378
};
7479

@@ -862,13 +867,85 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
862867
async fn test_v1_bound_accounts_cairo0() {
863868
let test_contract = &V1_BOUND_CAIRO0_CONTRACT;
864869
let class_hash = ClassHash(compute_deprecated_class_hash(test_contract).unwrap());
870+
let vc = VersionedConstants::latest_constants();
871+
let (mut test_manager, mut nonce_manager, _) =
872+
TestManager::<DictStateReader>::new_with_default_initial_state([]).await;
865873

866-
assert!(
867-
VersionedConstants::latest_constants()
868-
.os_constants
869-
.v1_bound_accounts_cairo0
870-
.contains(&class_hash)
874+
assert!(vc.os_constants.v1_bound_accounts_cairo0.contains(&class_hash));
875+
876+
// Declare the V1-bound account.
877+
let declare_args = declare_tx_args! {
878+
version: TransactionVersion::ZERO,
879+
max_fee: Fee(1_000_000_000_000_000),
880+
class_hash,
881+
sender_address: *FUNDED_ACCOUNT_ADDRESS,
882+
};
883+
let account_declare_tx = declare_tx(declare_args);
884+
let class_info = get_class_info_of_cairo0_contract((&**test_contract).clone());
885+
let tx =
886+
DeclareTransaction::create(account_declare_tx, class_info, &CHAIN_ID_FOR_TESTS).unwrap();
887+
test_manager.add_cairo0_declare_tx(tx, StarknetAPICompiledClassHash(class_hash.0));
888+
889+
// Deploy it.
890+
let salt = Felt::ZERO;
891+
let (deploy_tx, v1_bound_account_address) = get_deploy_contract_tx_and_address_with_salt(
892+
class_hash,
893+
Calldata::default(),
894+
nonce_manager.next(*FUNDED_ACCOUNT_ADDRESS),
895+
*NON_TRIVIAL_RESOURCE_BOUNDS,
896+
salt,
871897
);
898+
test_manager.add_invoke_tx(deploy_tx, None);
899+
900+
// Initialize the account.
901+
let private_key = Felt::ONE;
902+
let public_key = get_public_key(&private_key);
903+
let guardian = Felt::ZERO;
904+
let invoke_tx_args = invoke_tx_args! {
905+
sender_address: *FUNDED_ACCOUNT_ADDRESS,
906+
nonce: nonce_manager.next(*FUNDED_ACCOUNT_ADDRESS),
907+
calldata: create_calldata(v1_bound_account_address, "initialize", &[public_key, guardian]),
908+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
909+
};
910+
test_manager.add_invoke_tx_from_args(invoke_tx_args, &CHAIN_ID_FOR_TESTS, None);
872911

873-
// TODO(Dori): Implement the test.
912+
// Create a validate tx and add signature to the transaction. The dummy account used to call
913+
// `__validate__` does not check the signature, so we can use the signature field for
914+
// `__validate__`. This is done after creating the transaction so that we will have access
915+
// to the transaction hash.
916+
let validate_tx_args = invoke_tx_args! {
917+
sender_address: *FUNDED_ACCOUNT_ADDRESS,
918+
nonce: nonce_manager.next(*FUNDED_ACCOUNT_ADDRESS),
919+
calldata: create_calldata(
920+
v1_bound_account_address, "__validate__", &[Felt::ZERO, Felt::ZERO]
921+
),
922+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
923+
tip: vc.os_constants.v1_bound_accounts_max_tip,
924+
};
925+
let validate_tx =
926+
InvokeTransaction::create(invoke_tx(validate_tx_args.clone()), &CHAIN_ID_FOR_TESTS)
927+
.unwrap();
928+
assert_eq!(validate_tx.version(), TransactionVersion::THREE);
929+
let Signature { r, s } = ecdsa_sign(&private_key, &validate_tx.tx_hash()).unwrap().into();
930+
let validate_tx_args = invoke_tx_args! {
931+
signature: TransactionSignature(Arc::new(vec![r, s])),
932+
..validate_tx_args
933+
};
934+
test_manager.add_invoke_tx_from_args(validate_tx_args, &CHAIN_ID_FOR_TESTS, None);
935+
936+
// Run test and verify the signer was set.
937+
let test_output =
938+
test_manager.execute_test_with_default_block_contexts(&TestParameters::default()).await;
939+
940+
let expected_storage_updates = HashMap::from([(
941+
v1_bound_account_address,
942+
HashMap::from([(
943+
StarknetStorageKey(get_storage_var_address("_signer", &[])),
944+
StarknetStorageValue(public_key),
945+
)]),
946+
)]);
947+
let perform_global_validations = true;
948+
let partial_state_diff =
949+
Some(&StateDiff { storage_updates: expected_storage_updates, ..Default::default() });
950+
test_output.perform_validations(perform_global_validations, partial_state_diff);
874951
}

0 commit comments

Comments
 (0)