keep-starknet-strange / madara

DEPRECATED in favor of https://github.com/madara-alliance/madara
MIT License
531 stars 301 forks source link

adding tests for transactions v3 #1607

Closed Gerson2102 closed 3 months ago

Gerson2102 commented 4 months ago

Adding some tests for txv3, some of them are not working.

Pull Request type

Please add the labels corresponding to the type of changes your PR introduces:

What is the current behavior?

Resolves: #1574

NO ## Other information The tests that are supposed to fail are working properly. But those tests that have to success, are failing. Maybe I'm doing something wrong here, i dont think that is a coincidence.
Gerson2102 commented 4 months ago

@tdelabro I just updated the PR sir.

tdelabro commented 4 months ago

@Gerson2102 the CI is failing

Gerson2102 commented 4 months ago

In the code I'm seeing some tests related to strk and eth fees, i have to do something like that? Or is already tested?

tdelabro commented 4 months ago

@Gerson2102 again there is commits that are not your is this PR.

You should rebase your comment onto the head on the main branch. I don't know how you proceed but it make unreviewable, as I'm reviewing changes from other already merged PRs at the same time.

I recommend you give a look to this: https://docs.gitlab.com/ee/topics/git/git_rebase.html

tdelabro commented 4 months ago

the CI is failing

Gerson2102 commented 4 months ago

I'm working in this test:


#[test]
fn declare_transaction_v3_then_it_works() {
    new_test_ext::<MockRuntime>().execute_with(|| {
        basic_test_setup(2);

        let chain_id = Starknet::chain_id();
        let none_origin = RuntimeOrigin::none();

        let contract_class = get_contract_class("HelloStarknet.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: get_account_address(None, AccountType::V1(AccountTypeV1Inner::NoValidate)),
            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));
    })
}

I'm doing that test based in other test. I just want to declare the tx v3, and check if its working, but I'm getting an error in the assert_ok!(). I dont know if I'm doing something wrong here.

the error:

Expected Ok(_). Got Err(
    Module(
        ModuleError {
            index: 1,
            error: [
                1,
                0,
                0,
                0,
            ],
            message: Some(
                "TransactionExecutionFailed",
            ),
        },
    ),
)

The test I based it on:


#[test]
#[ignore]
fn storage_changes_should_revert_on_transaction_revert() {
    new_test_ext::<MockRuntime>().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 transaction_revert_class = get_contract_class("TransactionRevert.casm.json", 1);
        let transaction_revert_class_hash = ClassHash(
            StarkFelt::try_from("0x7d2bcb1df4970245665a19b23a4d3877eb86a661e8d98b89afc4531134b99f6").unwrap(),
        );
        let transaction_revert_compiled_class_hash = CompiledClassHash(
            StarkFelt::try_from("0x1c02b663e928ed213d3a0fa206efb59182fa2ba41f5c204daa56c4a434b53e5").unwrap(),
        );

        let mut declare_tx = starknet_api::transaction::DeclareTransactionV2 {
            sender_address: account_addr,
            class_hash: transaction_revert_class_hash,
            compiled_class_hash: transaction_revert_compiled_class_hash,
            nonce: Nonce::default(),
            max_fee: Fee(u128::MAX),
            signature: TransactionSignature(vec![]),
        };

        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::V2(declare_tx),
            tx_hash,
            ClassInfo::new(&transaction_revert_class, 1, 1).unwrap(),
        )
        .unwrap();

        assert_ok!(Starknet::declare(none_origin, transaction));
        assert_eq!(
            Starknet::contract_class_by_class_hash(transaction_revert_class_hash.0).unwrap(),
            transaction_revert_class
        );

        let salt = ContractAddressSalt(StarkFelt::ZERO);

        let mut invoke_tx = starknet_api::transaction::InvokeTransactionV1 {
            sender_address: account_addr,
            signature: TransactionSignature(vec![]),
            nonce: Nonce(StarkFelt::ONE),
            calldata: Calldata(Arc::new(vec![
                StarkFelt::ONE,
                StarkFelt::try_from(UDC_ADDRESS).unwrap(),  // udc address
                StarkFelt::try_from(UDC_SELECTOR).unwrap(), // deployContract selector
                StarkFelt::try_from("0x4").unwrap(),        // calldata len
                transaction_revert_class_hash.0,            // contract class hash
                salt.0,                                     // salt
                StarkFelt::ONE,                             // unique
                StarkFelt::ZERO,                            // constructor calldata len
            ])),
            max_fee: Fee(u128::MAX),
        };

        let tx_hash = invoke_tx.compute_hash(chain_id, false);
        invoke_tx.signature = sign_message_hash(tx_hash);
        let transaction = InvokeTransaction {
            tx: starknet_api::transaction::InvokeTransaction::V1(invoke_tx),
            tx_hash,
            only_query: false,
        };
        let contract_address: FieldElement = get_udc_deployed_address(
            Felt252Wrapper::from(salt).into(),
            Felt252Wrapper::from(transaction_revert_class_hash).into(),
            &UdcUniqueness::Unique(UdcUniqueSettings {
                deployer_address: Felt252Wrapper::from(account_addr).into(),
                udc_contract_address: FieldElement::from_hex_be(UDC_ADDRESS).unwrap(),
            }),
            &[],
        );
        let contract_address: ContractAddress = Felt252Wrapper::from(contract_address).into();

        // deploy contract
        assert_ok!(Starknet::invoke(RuntimeOrigin::none(), transaction));

        let increase_balance_function_selector = get_selector_from_name("increase_balance").unwrap();

        // create increase balance transaction
        let increase_balance_tx = starknet_api::transaction::InvokeTransactionV1 {
            sender_address: account_addr,
            signature: TransactionSignature(vec![]),
            nonce: Nonce(StarkFelt::TWO),
            max_fee: Fee(u128::MAX),
            calldata: Calldata(Arc::new(vec![
                StarkFelt::ONE,
                contract_address.0.0,
                Felt252Wrapper::from(increase_balance_function_selector).into(),
                StarkFelt::try_from("0x1").unwrap(),
                StarkFelt::try_from("0xa").unwrap(),
            ])),
        };

        // the transaction reverts and returns Ok
        assert_ok!(Starknet::invoke(RuntimeOrigin::none(), increase_balance_tx.clone().into()));

        // the storage value should be 0 after the transaction reverts
        let get_balance_function_selector = get_selector_from_name("get_balance").unwrap();

        let get_balance_function_selector_entrypoint = Felt252Wrapper::from(get_balance_function_selector).into();

        let default_calldata = Calldata(Default::default());

        let balance_value =
            Starknet::call_contract(contract_address, get_balance_function_selector_entrypoint, default_calldata)
                .unwrap();
        assert_eq!(balance_value, vec![Felt252Wrapper::ZERO])
    })
}
tdelabro commented 4 months ago

@Gerson2102 https://github.com/keep-starknet-strange/madara/blob/main/crates/pallets/starknet/src/lib.rs#L582 There is a log in this line giving you explanation about what went wrong. Try to read it. Youn can also replace it with a println it may be more easy to see when running tests

Gerson2102 commented 4 months ago

In declare_transaction_v3_then_it_works() I'm having problems because I'm running out of gas. And in the deploy_transaction_v3_then_it_works() test i found that i need to first declare the transactions, and then we have to deploy it. So i added the declare part, and I'm running out of gas again.

This is the code for the deploy test:

#[test]
fn deploy_transaction_v3_then_it_works() {
    new_test_ext::<MockRuntime>().execute_with(|| {
        basic_test_setup(2);

        let chain_id = Starknet::chain_id();
        let none_origin = RuntimeOrigin::none();

        // Declarar la clase de contrato primero
        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 declare_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.clone(), declare_transaction));

        // Ahora desplegar la cuenta
        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));
    })
}
tdelabro commented 4 months ago

@Gerson2102 the test you added are failing

Gerson2102 commented 4 months ago

@Gerson2102 the test you added are failing

Than you so much for ur guidance so far sir. Still working on it.

Gerson2102 commented 4 months ago

This is the error that I'm getting on the last test in declare_tx.rs file:

failures:

---- tests::declare_tx::given_contract_declare_tx3_than_works stdout ----
thread 'tests::declare_tx::given_contract_declare_tx3_than_works' panicked at crates/pallets/starknet/src/tests/declare_tx.rs:63:97:
called `Result::unwrap()` on an `Err` value: ContractClassVersionMismatch { declare_version: TransactionVersion(StarkFelt("0x0000000000000000000000000000000000000000000000000000000000000003")), cairo_version: 0 }

failures:
    tests::declare_tx::given_contract_declare_tx3_than_works
Gerson2102 commented 4 months ago

I dont know which address should I use for the compiled class hash:

let compiled_erc20_class_hash = CompiledClassHash(
        StarkFelt::try_from("0x00df4d3042eec107abe704619f13d92bbe01a58029311b7a1886b23dcbb4ea87").unwrap(),
    );

I put it different because i was looking at another test that was using different addresses.

Gerson2102 commented 3 months ago

Now im getting an error related to the gas amount:

Transaction execution failed: FeeCheckError(MaxL1GasAmountExceeded { max_amount: 10000, actual_amount: 136824 })
thread 'tests::declare_tx::given_contract_declare_tx3_than_works' panicked at crates/pallets/starknet/src/tests/declare_tx.rs:458:9:
Expected Ok(_). Got Err(
    Module(
        ModuleError {
            index: 1,
            error: [
                1,
                0,
                0,
                0,
            ],
            message: Some(
                "TransactionExecutionFailed",
            ),
        },
    ),
)

I dont know if i should increment it or something.

tdelabro commented 3 months ago

@Gerson2102 yup, you should definitely increment it.

Change the content of create_resource_bounds

Gerson2102 commented 3 months ago

@tdelabro I think I'm done sir. I'm just trying to fix the CI issues.

Gerson2102 commented 3 months ago

When i run this npx prettier --write . is not touching those files that are failing. So, i dont know how to fix it :(

tdelabro commented 3 months ago

[warn] docs/content/articles/cn/madara-beast-article.md [warn] docs/contributor-starter-pack.md

I don't know how to fix those either. Prettier is supposed to do it

tdelabro commented 3 months ago

@Gerson2102 I will try to pull it myself and run prettier on my computer and then push

Gerson2102 commented 3 months ago

@Gerson2102 I will try to pull it myself and run prettier on my computer and then push

No worries, i already fixed it.

Gerson2102 commented 3 months ago

Now all looks green :)

tdelabro commented 3 months ago

GG @Gerson2102! You did it! Thanks a lot