From 6684f8b414bb54848eab08e7bfbe4b59de3b0144 Mon Sep 17 00:00:00 2001 From: Gerson2102 Date: Fri, 31 May 2024 13:49:12 -0600 Subject: [PATCH] addressing timothy comments, adding tx v3 tests, declare, deploy and invoke. --- CHANGELOG.md | 4 +- .../pallets/starknet/src/tests/declare_tx.rs | 70 +++++++++++- .../starknet/src/tests/deploy_account_tx.rs | 72 +++++++++++- .../pallets/starknet/src/tests/invoke_tx.rs | 104 ------------------ 4 files changed, 139 insertions(+), 111 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3ee7ae4de..5e33147158 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Madara Changelog ## Next release +- test: Deleting tests for wallets +- test: Adding txv3 tests - feat: L1 gas price/fix @@ -61,7 +63,7 @@ - feat: Remove generic hasher from block hash computation - refacto: git submodules removed - test: Add pallet test for transaction declare V0 -- test: Deleting tests for wallets + ## v0.7.0 diff --git a/crates/pallets/starknet/src/tests/declare_tx.rs b/crates/pallets/starknet/src/tests/declare_tx.rs index 1103be3a77..1334f62e9b 100644 --- a/crates/pallets/starknet/src/tests/declare_tx.rs +++ b/crates/pallets/starknet/src/tests/declare_tx.rs @@ -9,19 +9,60 @@ use sp_runtime::transaction_validity::{ InvalidTransaction, TransactionSource, TransactionValidityError, ValidTransaction, }; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce, PatriciaKey}; +use starknet_api::data_availability::DataAvailabilityMode; use starknet_api::hash::StarkFelt; use starknet_api::transaction::{ - DeclareTransaction as StarknetApiDeclareTransaction, DeclareTransactionV0V1, DeclareTransactionV2, Fee, - TransactionSignature, + DeclareTransaction as StarknetApiDeclareTransaction, DeclareTransactionV0V1, DeclareTransactionV2, + DeclareTransactionV3, Fee, TransactionSignature, }; use starknet_crypto::FieldElement; use super::mock::default_mock::*; use super::mock::*; -use super::utils::{get_contract_class, sign_message_hash}; +use super::utils::{create_resource_bounds, get_contract_class, sign_message_hash}; use crate::tests::{set_infinite_tokens, set_nonce}; use crate::Error; +fn create_declare_erc20_v3_transaction( + chain_id: Felt252Wrapper, + account_type: AccountType, + sender_address: Option, + signature: Option, + nonce: Option, +) -> BlockifierDeclareTransaction { + let sender_address = sender_address.unwrap_or_else(|| get_account_address(None, account_type)); + let erc20_class = get_contract_class("ERC20.json", 0); + let erc20_class_hash = + ClassHash(StarkFelt::try_from("0x057eca87f4b19852cfd4551cf4706ababc6251a8781733a0a11cf8e94211da95").unwrap()); + + let compiled_erc20_class_hash = CompiledClassHash( + StarkFelt::try_from("0x00df4d3042eec107abe704619f13d92bbe01a58029311b7a1886b23dcbb4ea87").unwrap(), + ); + + let mut tx = StarknetApiDeclareTransaction::V3(DeclareTransactionV3 { + resource_bounds: create_resource_bounds(), + tip: starknet_api::transaction::Tip::default(), + signature: TransactionSignature::default(), + nonce: nonce.unwrap_or_default(), + class_hash: erc20_class_hash, + compiled_class_hash: compiled_erc20_class_hash, + sender_address, + nonce_data_availability_mode: DataAvailabilityMode::L1, + fee_data_availability_mode: DataAvailabilityMode::L1, + paymaster_data: starknet_api::transaction::PaymasterData(vec![StarkFelt::ZERO]), + account_deployment_data: starknet_api::transaction::AccountDeploymentData(vec![StarkFelt::ZERO]), + }); + + let tx_hash = tx.compute_hash(chain_id, false); + // Force to do that because ComputeTransactionHash cannot be implemented on DeclareTransactionV0V1 + // directly... + if let StarknetApiDeclareTransaction::V3(tx) = &mut tx { + tx.signature = signature.unwrap_or_else(|| sign_message_hash(tx_hash)); + } + + BlockifierDeclareTransaction::new(tx, tx_hash, ClassInfo::new(&erc20_class, 0, 1).unwrap()).unwrap() +} + fn create_declare_erc20_v1_transaction( chain_id: Felt252Wrapper, account_type: AccountType, @@ -395,3 +436,26 @@ fn test_declare_using_transaction_v0() { assert!(Starknet::validate_unsigned(TransactionSource::InBlock, &crate::Call::declare { transaction }).is_ok()); }); } + +#[test] +fn given_contract_declare_tx3_than_works() { + new_test_ext::().execute_with(|| { + basic_test_setup(2); + + let none_origin = RuntimeOrigin::none(); + let chain_id = Starknet::chain_id(); + + let transaction = create_declare_erc20_v3_transaction( + chain_id, + AccountType::V0(AccountTypeV0Inner::NoValidate), + None, + None, + None, + ); + let class_hash = transaction.class_hash(); + let contract_class = transaction.contract_class(); + + assert_ok!(Starknet::declare(none_origin.clone(), transaction.clone())); + assert_eq!(Starknet::contract_class_by_class_hash(class_hash.0).unwrap(), contract_class); + }); +} diff --git a/crates/pallets/starknet/src/tests/deploy_account_tx.rs b/crates/pallets/starknet/src/tests/deploy_account_tx.rs index 149d89f881..78d5673b5b 100644 --- a/crates/pallets/starknet/src/tests/deploy_account_tx.rs +++ b/crates/pallets/starknet/src/tests/deploy_account_tx.rs @@ -7,17 +7,18 @@ use mp_transactions::compute_hash::ComputeTransactionHash; use sp_runtime::traits::ValidateUnsigned; use sp_runtime::transaction_validity::{InvalidTransaction, TransactionSource, TransactionValidityError}; use starknet_api::core::{calculate_contract_address, ClassHash, ContractAddress, Nonce}; +use starknet_api::data_availability::DataAvailabilityMode; use starknet_api::hash::StarkFelt; use starknet_api::transaction::{ - Calldata, ContractAddressSalt, DeployAccountTransactionV1, Event as StarknetEvent, EventContent, EventData, - EventKey, Fee, TransactionSignature, + Calldata, ContractAddressSalt, DeployAccountTransactionV1, DeployAccountTransactionV3, Event as StarknetEvent, + EventContent, EventData, EventKey, Fee, TransactionSignature, }; use starknet_core::utils::get_selector_from_name; use starknet_crypto::FieldElement; use super::mock::default_mock::*; use super::mock::*; -use super::utils::{sign_message_hash, sign_message_hash_braavos}; +use super::utils::{create_resource_bounds, sign_message_hash, sign_message_hash_braavos}; use crate::tests::constants::{ACCOUNT_PUBLIC_KEY, SALT, TRANSFER_SELECTOR_NAME}; use crate::tests::{get_deploy_account_dummy, set_infinite_tokens, set_nonce}; use crate::{Error, StorageView}; @@ -42,6 +43,26 @@ fn deploy_v1_to_blockifier_deploy( ) } +fn deploy_v3_to_blockifier_deploy( + tx: DeployAccountTransactionV3, + chain_id: Felt252Wrapper, +) -> DeployAccountTransaction { + let tx_hash = tx.compute_hash(chain_id, false); + let contract_address = calculate_contract_address( + tx.contract_address_salt, + tx.class_hash, + &tx.constructor_calldata, + Default::default(), + ) + .unwrap(); + + DeployAccountTransaction::new( + starknet_api::transaction::DeployAccountTransaction::V3(tx), + tx_hash, + contract_address, + ) +} + fn helper_create_deploy_account_tx( chain_id: Felt252Wrapper, salt: ContractAddressSalt, @@ -60,6 +81,28 @@ fn helper_create_deploy_account_tx( deploy_v1_to_blockifier_deploy(tx, chain_id) } +fn helper_create_deploy_account_tx3( + chain_id: Felt252Wrapper, + salt: ContractAddressSalt, + calldata: Calldata, + account_class_hash: ClassHash, +) -> DeployAccountTransaction { + let tx = DeployAccountTransactionV3 { + resource_bounds: create_resource_bounds(), + tip: starknet_api::transaction::Tip::default(), + signature: TransactionSignature::default(), + nonce: Nonce(StarkFelt::ZERO), + class_hash: account_class_hash, + contract_address_salt: salt, + constructor_calldata: calldata, + nonce_data_availability_mode: DataAvailabilityMode::L1, + fee_data_availability_mode: DataAvailabilityMode::L1, + paymaster_data: starknet_api::transaction::PaymasterData(vec![StarkFelt::ZERO]), + }; + + deploy_v3_to_blockifier_deploy(tx, chain_id) +} + #[test] fn given_contract_run_deploy_account_tx_works() { new_test_ext::().execute_with(|| { @@ -509,3 +552,26 @@ fn test_verify_nonce_in_unsigned_tx() { ); }); } + +#[test] +fn given_contract_run_deploy_account_tx3_works() { + new_test_ext::().execute_with(|| { + basic_test_setup(2); + let none_origin = RuntimeOrigin::none(); + let chain_id = Starknet::chain_id(); + // TEST ACCOUNT CONTRACT + // - ref testnet tx(0x0751b4b5b95652ad71b1721845882c3852af17e2ed0c8d93554b5b292abb9810) + let salt = ContractAddressSalt( + StarkFelt::try_from("0x03b37cbe4e9eac89d54c5f7cc6329a63a63e8c8db2bf936f981041e086752463").unwrap(), + ); + let (account_class_hash, calldata) = account_helper(AccountType::V0(AccountTypeV0Inner::NoValidate)); + + let deploy_tx = helper_create_deploy_account_tx3(chain_id, salt, calldata, account_class_hash); + let contract_address = deploy_tx.contract_address; + + set_infinite_tokens::(&contract_address); + + assert_ok!(Starknet::deploy_account(none_origin, deploy_tx)); + assert_eq!(Starknet::contract_class_hash_by_address(contract_address), account_class_hash.0); + }); +} diff --git a/crates/pallets/starknet/src/tests/invoke_tx.rs b/crates/pallets/starknet/src/tests/invoke_tx.rs index 7c09998ee4..4648765604 100644 --- a/crates/pallets/starknet/src/tests/invoke_tx.rs +++ b/crates/pallets/starknet/src/tests/invoke_tx.rs @@ -906,27 +906,6 @@ fn given_hardcoded_contract_set_erc20_balance_to_zero() { }); } -#[test] -fn given_hardcoded_contract_run_invoke_tx_v3_fails_sender_not_deployed() { - new_test_ext::().execute_with(|| { - basic_test_setup(2); - - let none_origin = RuntimeOrigin::none(); - - // Wrong address (not deployed) - let contract_address = ContractAddress(PatriciaKey( - StarkFelt::try_from("0x03e437FB56Bb213f5708Fcd6966502070e276c093ec271aA33433b89E21fd32f").unwrap(), - )); - - let mut transaction = get_invoke_v3_dummy(Starknet::chain_id(), NONCE_ZERO); - if let starknet_api::transaction::InvokeTransaction::V3(tx) = &mut transaction.tx { - tx.sender_address = contract_address; - }; - - assert_err!(Starknet::invoke(none_origin, transaction), Error::::AccountNotDeployed); - }) -} - #[test] fn given_hardcoded_contract_run_invoke_tx_v3_with_inner_call_in_validate_then_it_fails() { new_test_ext::().execute_with(|| { @@ -985,86 +964,3 @@ fn given_account_not_deployed_invoke_tx_v3_fails_for_nonce_not_one() { ); }) } - -#[test] -fn declare_transaction_v3_then_it_works() { - new_test_ext::().execute_with(|| { - basic_test_setup(2); - - let chain_id = Starknet::chain_id(); - let none_origin = RuntimeOrigin::none(); - let account_addr = get_account_address(None, AccountType::V1(AccountTypeV1Inner::NoValidate)); - - let contract_class = get_contract_class("TransactionRevert.casm.json", 1); - let contract_class_hash = ClassHash( - StarkFelt::try_from("0x7d2bcb1df4970245665a19b23a4d3877eb86a661e8d98b89afc4531134b99f6").unwrap(), - ); - let contract_compiled_class_hash = CompiledClassHash( - StarkFelt::try_from("0x1c02b663e928ed213d3a0fa206efb59182fa2ba41f5c204daa56c4a434b53e5").unwrap(), - ); - - let mut declare_tx = starknet_api::transaction::DeclareTransactionV3 { - resource_bounds: create_resource_bounds(), - tip: starknet_api::transaction::Tip::default(), - signature: TransactionSignature::default(), - nonce: Nonce(StarkFelt::ZERO), - class_hash: contract_class_hash, - compiled_class_hash: contract_compiled_class_hash, - sender_address: account_addr, - nonce_data_availability_mode: DataAvailabilityMode::L1, - fee_data_availability_mode: DataAvailabilityMode::L1, - paymaster_data: starknet_api::transaction::PaymasterData(vec![StarkFelt::ZERO]), - account_deployment_data: starknet_api::transaction::AccountDeploymentData(vec![StarkFelt::ZERO]), - }; - - let tx_hash = declare_tx.compute_hash(chain_id, false); - declare_tx.signature = sign_message_hash(tx_hash); - - let transaction = DeclareTransaction::new( - starknet_api::transaction::DeclareTransaction::V3(declare_tx), - tx_hash, - ClassInfo::new(&contract_class, 1, 1).unwrap(), - ) - .unwrap(); - - assert_ok!(Starknet::declare(none_origin, transaction)); - }) -} - -#[test] -fn deploy_transaction_v3_then_it_works() { - new_test_ext::().execute_with(|| { - basic_test_setup(2); - - let chain_id = Starknet::chain_id(); - let none_origin = RuntimeOrigin::none(); - - let contract_class_hash = ClassHash( - StarkFelt::try_from("0x7d2bcb1df4970245665a19b23a4d3877eb86a661e8d98b89afc4531134b99f6").unwrap(), - ); - - let mut deploy_tx = starknet_api::transaction::DeployAccountTransactionV3 { - resource_bounds: create_resource_bounds(), - tip: starknet_api::transaction::Tip::default(), - signature: TransactionSignature::default(), - nonce: Nonce(StarkFelt::ZERO), - class_hash: contract_class_hash, - contract_address_salt: ContractAddressSalt::default(), - constructor_calldata: Calldata::default(), - nonce_data_availability_mode: DataAvailabilityMode::L1, - fee_data_availability_mode: DataAvailabilityMode::L1, - paymaster_data: starknet_api::transaction::PaymasterData(vec![StarkFelt::ZERO]), - }; - - let tx_hash = deploy_tx.compute_hash(chain_id, false); - deploy_tx.signature = sign_message_hash(tx_hash); - - let transaction = DeployAccountTransaction::new( - starknet_api::transaction::DeployAccountTransaction::V3(deploy_tx), - tx_hash, - ContractAddress::default(), - ); - - assert_ok!(Starknet::deploy_account(none_origin, transaction)); - }) -}