From a06f157ce8ef51af9153da54c5cc3da8fd6cc772 Mon Sep 17 00:00:00 2001 From: Fiiranek Date: Mon, 13 Jan 2025 14:50:12 +0100 Subject: [PATCH 1/8] Use `-n auto` when running tests --- pyproject.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b122274be..aca85b798 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,14 +73,14 @@ test = [ ] test_ci = ["test_ci_v1", "test_ci_v2"] -test_ci_v1 = "coverage run -a -m pytest --contract_dir=v1 starknet_py --ignore=starknet_py/tests/e2e/docs --ignore=starknet_py/tests/e2e/tests_on_networks" -test_ci_v2 = "coverage run -a -m pytest --contract_dir=v2 starknet_py --ignore=starknet_py/tests/e2e/docs --ignore=starknet_py/tests/e2e/tests_on_networks" +test_ci_v1 = "coverage run -a -m pytest -n auto --contract_dir=v1 starknet_py --ignore=starknet_py/tests/e2e/docs --ignore=starknet_py/tests/e2e/tests_on_networks" +test_ci_v2 = "coverage run -a -m pytest -n auto --contract_dir=v2 starknet_py --ignore=starknet_py/tests/e2e/docs --ignore=starknet_py/tests/e2e/tests_on_networks" test_ci_on_networks = "coverage run -a -m pytest --contract_dir=v2 starknet_py/tests/e2e/tests_on_networks" test_ci_docs = ["test_ci_docs_v1", "test_ci_docs_v2"] -test_ci_docs_v1 = "coverage run -a -m pytest --contract_dir=v1 starknet_py/tests/e2e/docs" -test_ci_docs_v2 = "coverage run -a -m pytest --contract_dir=v2 starknet_py/tests/e2e/docs" +test_ci_docs_v1 = "coverage run -a -m pytest -n auto --contract_dir=v1 starknet_py/tests/e2e/docs" +test_ci_docs_v2 = "coverage run -a -m pytest -n auto --contract_dir=v2 starknet_py/tests/e2e/docs" test_report = "coverage report -m" test_html.shell = "coverage html && open ./htmlcov/index.html" From 0aef0955d258b3ef13fdbabee0a0a9d0251632c4 Mon Sep 17 00:00:00 2001 From: Fiiranek Date: Mon, 13 Jan 2025 15:59:16 +0100 Subject: [PATCH 2/8] Refactor `test_call_contract` and `test_get_storage_at` tests --- starknet_py/tests/e2e/client/client_test.py | 12 ++++---- .../fixtures/prepare_net_for_gateway_test.py | 12 ++++++++ .../e2e/client/fixtures/prepare_network.py | 29 +++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/starknet_py/tests/e2e/client/client_test.py b/starknet_py/tests/e2e/client/client_test.py index 5d140b695..83a7dff5f 100644 --- a/starknet_py/tests/e2e/client/client_test.py +++ b/starknet_py/tests/e2e/client/client_test.py @@ -110,14 +110,14 @@ async def test_get_block_by_number( @pytest.mark.asyncio -async def test_get_storage_at(client, contract_address): +async def test_get_storage_at(client, contract_address_2): storage = await client.get_storage_at( - contract_address=contract_address, + contract_address=contract_address_2, key=get_storage_var_address("balance"), block_hash="latest", ) - assert storage == 1897 + assert storage == 1777 @pytest.mark.asyncio @@ -241,16 +241,16 @@ async def test_estimate_fee_for_multiple_transactions( @pytest.mark.asyncio -async def test_call_contract(client, contract_address): +async def test_call_contract(client, contract_address_2): call = Call( - to_addr=contract_address, + to_addr=contract_address_2, selector=get_selector_from_name("get_balance"), calldata=[], ) result = await client.call_contract(call, block_number="latest") - assert result == [1897] + assert result == [1777] @pytest.mark.asyncio diff --git a/starknet_py/tests/e2e/client/fixtures/prepare_net_for_gateway_test.py b/starknet_py/tests/e2e/client/fixtures/prepare_net_for_gateway_test.py index 94587985a..423ecbf25 100755 --- a/starknet_py/tests/e2e/client/fixtures/prepare_net_for_gateway_test.py +++ b/starknet_py/tests/e2e/client/fixtures/prepare_net_for_gateway_test.py @@ -14,6 +14,7 @@ class PreparedNetworkData: # pylint: disable=too-many-instance-attributes class_hash: int contract_address: int + contract_address_2: int invoke_transaction_hash: int block_with_invoke_number: int declare_transaction_hash: int @@ -29,6 +30,7 @@ async def prepare_net_for_tests( deploy_account_details: AccountToBeDeployedDetails, transaction_hash: int, contract: Contract, + contract_2: Contract, declare_class_hash: int, ) -> PreparedNetworkData: # pylint: disable=too-many-locals @@ -42,9 +44,17 @@ async def prepare_net_for_tests( ) await invoke_res.wait_for_acceptance() + invoke_res_2 = await contract_2.functions["increase_balance"].invoke_v3( + amount=1777, l1_resource_bounds=MAX_RESOURCE_BOUNDS_L1 + ) + await invoke_res_2.wait_for_acceptance() + block_with_invoke_number = ( await account.client.get_transaction_receipt(invoke_res.hash) ).block_number + block_with_invoke_number_2 = ( + await account.client.get_transaction_receipt(invoke_res_2.hash) + ).block_number address, key_pair, salt, class_hash = deploy_account_details deploy_account_tx = await get_deploy_account_transaction( @@ -64,6 +74,7 @@ async def prepare_net_for_tests( block_with_deploy_account_hash = declare_account_receipt.block_hash assert block_with_invoke_number is not None + assert block_with_invoke_number_2 is not None assert block_with_declare_number is not None assert block_with_declare_hash is not None assert block_with_deploy_account_number is not None @@ -72,6 +83,7 @@ async def prepare_net_for_tests( return PreparedNetworkData( class_hash=declare_class_hash, contract_address=contract.address, + contract_address_2=contract_2.address, invoke_transaction_hash=invoke_res.hash, block_with_invoke_number=block_with_invoke_number, declare_transaction_hash=transaction_hash, diff --git a/starknet_py/tests/e2e/client/fixtures/prepare_network.py b/starknet_py/tests/e2e/client/fixtures/prepare_network.py index ed7f049b8..c25229233 100644 --- a/starknet_py/tests/e2e/client/fixtures/prepare_network.py +++ b/starknet_py/tests/e2e/client/fixtures/prepare_network.py @@ -48,6 +48,24 @@ async def deployed_balance_contract( return deploy_result.deployed_contract +@pytest_asyncio.fixture(scope="package") +async def deployed_balance_contract_2( + account: BaseAccount, + balance_class_and_transaction_hash, + balance_abi, +) -> Contract: + class_hash, _ = balance_class_and_transaction_hash + deploy_result = await Contract.deploy_contract_v3( + account=account, + abi=balance_abi, + class_hash=class_hash, + l1_resource_bounds=MAX_RESOURCE_BOUNDS_L1, + ) + await deploy_result.wait_for_acceptance() + + return deploy_result.deployed_contract + + @pytest.fixture(scope="package") def balance_abi() -> List: compiled_contract = create_sierra_compiled_contract( @@ -139,6 +157,15 @@ def contract_address(prepare_network: Tuple[str, PreparedNetworkData]) -> int: return prepared_data.contract_address +@pytest.fixture() +def contract_address_2(prepare_network: Tuple[str, PreparedNetworkData]) -> int: + """ + Returns an address of the deployed contract + """ + _, prepared_data = prepare_network + return prepared_data.contract_address_2 + + @pytest.fixture() def class_hash(prepare_network: Tuple[str, PreparedNetworkData]) -> int: """ @@ -155,6 +182,7 @@ async def prepare_network( deploy_account_details_factory: AccountToBeDeployedDetailsFactory, balance_class_and_transaction_hash: Tuple[int, int], deployed_balance_contract: Contract, + deployed_balance_contract_2: Contract, ) -> AsyncGenerator[Tuple[str, PreparedNetworkData], None]: """ Adds transactions to the network. Returns network address and PreparedNetworkData @@ -168,6 +196,7 @@ async def prepare_network( deploy_account_details=details, transaction_hash=transaction_hash, contract=deployed_balance_contract, + contract_2=deployed_balance_contract_2, declare_class_hash=class_hash, ) From 53718ca3f746ff95d71ca9bb9e6aa340013dbe08 Mon Sep 17 00:00:00 2001 From: Fiiranek Date: Mon, 13 Jan 2025 16:40:16 +0100 Subject: [PATCH 3/8] Add Docker speculos image caching --- .github/workflows/checks.yml | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 999b203c2..6b29f7c3b 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -182,9 +182,24 @@ jobs: # ====================== SETUP LEDGER SPECULOS ====================== # - - name: Pull speculos image - run: docker pull ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} + - name: Restore cached speculos image + id: speculos-docker-image-cache + uses: actions/cache@v4 + with: + path: /tmp/ledger-image.tar + key: speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }} + restore-keys: | + speculos-image- + - name: Load cached speculos image + if: steps.speculos-docker-image-cache.outputs.cache-hit == 'true' + run: docker load < /tmp/ledger-image.tar + + - name: Pull speculos image (if not cached) + if: steps.speculos-docker-image-cache.outputs.cache-hit != 'true' + run: | + docker pull ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} + docker save ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} > /tmp/ledger-image.tar - name: Clone LedgerHQ Starknet app repository run: git clone https://github.com/LedgerHQ/app-starknet.git From 3e09e1c376532cea16e252a5da3f570162c90018 Mon Sep 17 00:00:00 2001 From: Fiiranek Date: Mon, 13 Jan 2025 16:44:48 +0100 Subject: [PATCH 4/8] Add saving cache step --- .github/workflows/checks.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 6b29f7c3b..aefb6ae5f 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -201,6 +201,13 @@ jobs: docker pull ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} docker save ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} > /tmp/ledger-image.tar + - name: Save pulled speculos image to cache + if: steps.speculos-docker-image-cache.outputs.cache-hit != 'true' + uses: actions/cache@v4 + with: + path: /tmp/ledger-image.tar + key: speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }} + - name: Clone LedgerHQ Starknet app repository run: git clone https://github.com/LedgerHQ/app-starknet.git From 751c333b78a0ad2bcf5dbb235bd50be3567bf8f9 Mon Sep 17 00:00:00 2001 From: Fiiranek Date: Mon, 13 Jan 2025 16:55:04 +0100 Subject: [PATCH 5/8] Trigger CI From f304295e8283918a3cabd26545de0a4fc426cb24 Mon Sep 17 00:00:00 2001 From: Fiiranek Date: Mon, 13 Jan 2025 20:37:43 +0100 Subject: [PATCH 6/8] Update step name; Update image file name --- .github/workflows/checks.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index aefb6ae5f..1255c6399 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -193,19 +193,19 @@ jobs: - name: Load cached speculos image if: steps.speculos-docker-image-cache.outputs.cache-hit == 'true' - run: docker load < /tmp/ledger-image.tar + run: docker load < /tmp/speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }}.tar - - name: Pull speculos image (if not cached) + - name: Pull speculos image if: steps.speculos-docker-image-cache.outputs.cache-hit != 'true' run: | docker pull ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} - docker save ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} > /tmp/ledger-image.tar + docker save ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} > /tmp/speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }}.tar - name: Save pulled speculos image to cache if: steps.speculos-docker-image-cache.outputs.cache-hit != 'true' uses: actions/cache@v4 with: - path: /tmp/ledger-image.tar + path: /tmp/speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }}.tar key: speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }} - name: Clone LedgerHQ Starknet app repository From f14f72a576370f0d7c12409e365ee605ee24bda3 Mon Sep 17 00:00:00 2001 From: Fiiranek Date: Tue, 14 Jan 2025 14:08:55 +0100 Subject: [PATCH 7/8] Remove caching speculos image --- .github/workflows/checks.yml | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 1255c6399..999b203c2 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -182,31 +182,9 @@ jobs: # ====================== SETUP LEDGER SPECULOS ====================== # - - name: Restore cached speculos image - id: speculos-docker-image-cache - uses: actions/cache@v4 - with: - path: /tmp/ledger-image.tar - key: speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }} - restore-keys: | - speculos-image- - - - name: Load cached speculos image - if: steps.speculos-docker-image-cache.outputs.cache-hit == 'true' - run: docker load < /tmp/speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }}.tar - - name: Pull speculos image - if: steps.speculos-docker-image-cache.outputs.cache-hit != 'true' - run: | - docker pull ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} - docker save ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} > /tmp/speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }}.tar + run: docker pull ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools@sha256:${{ env.LEDGER_APP_DEV_TOOLS_SHA }} - - name: Save pulled speculos image to cache - if: steps.speculos-docker-image-cache.outputs.cache-hit != 'true' - uses: actions/cache@v4 - with: - path: /tmp/speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }}.tar - key: speculos-image-${{ env.LEDGER_APP_DEV_TOOLS_SHA }} - name: Clone LedgerHQ Starknet app repository run: git clone https://github.com/LedgerHQ/app-starknet.git From e429c044132bc5ba609952b175deb2cabe118bef Mon Sep 17 00:00:00 2001 From: Fiiranek Date: Tue, 21 Jan 2025 16:24:33 +0100 Subject: [PATCH 8/8] Add comment with explanation of adding `contract_address_2` --- starknet_py/tests/e2e/client/fixtures/prepare_network.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/starknet_py/tests/e2e/client/fixtures/prepare_network.py b/starknet_py/tests/e2e/client/fixtures/prepare_network.py index c25229233..7f3d33f52 100644 --- a/starknet_py/tests/e2e/client/fixtures/prepare_network.py +++ b/starknet_py/tests/e2e/client/fixtures/prepare_network.py @@ -157,6 +157,9 @@ def contract_address(prepare_network: Tuple[str, PreparedNetworkData]) -> int: return prepared_data.contract_address +# `contract_address` was used in other tests, which modified its storage values. This overlap +# caused test interdependencies, leading to inconsistent results in `test_get_storage_at` +# and `test_call_contract`, hence the introduction of `contract_address_2`. @pytest.fixture() def contract_address_2(prepare_network: Tuple[str, PreparedNetworkData]) -> int: """