Skip to content

Commit ab6835a

Browse files
war-indrknzzcptartur
authored
Remove part of the cairo lang dependency (software-mansion#592)
* Remove usages of `CastableToHash` (software-mansion#587) * Remove CastableToHash import * Change hash to int * Replace `FIELD_PRIME` in felt.py (software-mansion#588) * Add FIELD_PRIME constant * Change FIELD_PRIME to hex * Replace `get_random_private_key` (software-mansion#597) * Replace get_random_private_key * Change key generation method * Move func to test utils * Add comment * Replace `get_selector_from_name` and `get_storage_var_address` implementations (software-mansion#586) * Remove uses of starkware.starknet.public.api * Add eth-utils dependency * Change import path * Apply review * Move functions to separate files * Fix path in cairo-lang comment * Add unit tests * Remove `ContractAddressSalt.get_random_value()` usages (software-mansion#596) * add custom `get_random_value` * fix imports * rename to `get_random_salt` * move `get_random_salt` to deployer.py * make it private * do not use private `get_random_salt` * Replace `compute_hash_on_elements` (software-mansion#602) * replace compute_hash_on_elements * replace calculate_contract_address_from_hash * remove unused PREFIX_TRANSACTION * add tests * fix pyright * add type for data * Update starknet_py/utils/crypto/facade.py Co-authored-by: Kamil Jankowski <[email protected]> * move to constants * remove unnecessary function * remove `hash_call_with` function & `hash_func` parameter * fix the imports Co-authored-by: Kamil Jankowski <[email protected]> * Replace transaction hash calculation (software-mansion#621) * add functions to compute transaction hashes * replace usages from starkware * deprecate compute_invoke_hash * add tests * rename to compute_transaction_hash * fix error * add docstrings * replace 0 with DEFAULT_ENTRY_POINT_SELECTOR * modify test case * update lock * fixes after merge * update lock * fix imports in the `fmt: off` files * fix import * Add dataclasses for transactions (software-mansion#649) * `Declare` is working :o * format * [wip] adding transactions * it is working! * format * remove QUERY_VERSION_BASE and change TransactionType to own implementation * change `general_config` to `chain_id` * [wip] bulk * format * repair bulk * remove unused blockIdentifier * bring DEFAULT_DECLARE_SENDER_ADDRESS to constants * fix import * add tx_type as @classmethod * DEFAULT_DECLARE_SENDER_ADDRESS = 1 * post-merge fixes * fix typing errors * format * resolve last typing issues * format * remove unnecessary file * update docstrings and comments in transaction.py * format * Update starknet_py/net/account/account_client.py Co-authored-by: Kamil Jankowski <[email protected]> * rename `InvokeFunction` to `Invoke` * apply part of the review changes. The biggest one: remove SignableTransaction and replace it with AccountTransaction * tx_type as a property * fix docs * rename `tx_type` to `type` remove unnecessary `post_dump` method * format * remove comment * make `_remove_entry_point_selector` private * remove duplicated TransactionType * NoneFelt docstring * fix docs * review suggestions * _prepare_invoke_function -> _prepare_invoke * add marshmallow-dataclass as dependency * InvokeFunction -> Invoke in the docstring * update typing * Revert "add marshmallow-dataclass as dependency" This reverts commit 7e0a163. * change marshmallow-dataclass version * remove casting * remove types.py * move `pylint:disable` Co-authored-by: Kamil Jankowski <[email protected]> * Replace compress and decompress program (software-mansion#683) * Use own compress and decompress program * Fix lint * Fix typing * Inline `_compress_program` * Add basic Declare test * Fix tests typing * Remove `_contract_attribute_name` * remove unused imports * update lock * rename function * add ABC to the AccountTransaction * update base signer interface * update comment * move MASK_250 * Migration guide (remove cairo lang) (software-mansion#670) * migration guide v1 * add `the` * add info about imports * formatting * Update docs/migration_guide.rst Co-authored-by: Artur Michałek <[email protected]> * add `Transaction's dataclasses` section * fix bullet list * remove unnecessary line * suggested changes * Update docs/migration_guide.rst Co-authored-by: Artur Michałek <[email protected]> * review suggestions Co-authored-by: Artur Michałek <[email protected]> * clean after merge * update lock * format * aftermerge fixes * fix types * fix failing docs * add tests for `Transaction.calculate_hash` * Replace`StarkErrorCode` (software-mansion#737) * remove usage of StarknetErrorCode * one last usage of `get_selector_from_name` from cairo lang * Support calculating class_hash (software-mansion#660) * Add compute_class_hash * Add tests * Add tests * Remove pre-0.10.0 class_hash calculation * Fix sorted keys in serialized class * Remove else branch * Remove else branch * Hash module (software-mansion#677) * Create hash module * Format * Fix docs build * Add docstrings * Add pedersen_hash tests * Format * Fix test * Replace ContractClass (software-mansion#686) * Rename DeclaredContract to ContractClass * Replace ContractClass * Restore class hash < 0.10.0 [WIP] * Use re.sub * Remove int casting * Add tests * Add compute_class_hash * Remove pre-0.10.0 class_hash calculation * Fix sorted keys in serialized class * Remove else branch * Remove else branch * Hash module (software-mansion#677) * Create hash module * Format * Fix docs build * Add docstrings * Add pedersen_hash tests * Format * Fix test * Replace ContractClass (software-mansion#686) * Rename DeclaredContract to ContractClass * Replace ContractClass * Restore class hash < 0.10.0 [WIP] * Use re.sub * Remove int casting * Post merge fixes * Rename contract_class module * Update starknet_py/hash/class_hash.py * Add create_contract_class to compiler module * Apply review * Apply suggestions from code review Co-authored-by: Artur Michałek <[email protected]> * Add CompiledContract (software-mansion#708) * Add CompiledContract * Review * Add suggestions from review * Remove factory method * Fix ci * Default abi to empty list * Post merge * Fix circular import * Remove annotations import --------- Co-authored-by: Artur Michałek <[email protected]> Co-authored-by: war-in <[email protected]> * Add comment about default_factory * Fix lock * Validate class_hash with cairo-lang * Replace imports of already implemented methods * Update migration guide * Change deprecation version * Circular imports detection (software-mansion#760) * Add circular.py * Fix circular imports * Remove import * Update circular.py Co-authored-by: war-in <[email protected]> * Add circular check to CI * Fix CI --------- Co-authored-by: war-in <[email protected]> * Fix lock * Fix lint * Final changes * Fix docs --------- Co-authored-by: Kamil Jankowski <[email protected]> Co-authored-by: Artur Michałek <[email protected]>
1 parent 91d2572 commit ab6835a

File tree

88 files changed

+1450
-503
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1450
-503
lines changed

.github/workflows/checks.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
poetry install
3333
3434
lint:
35-
name: Check lock, formatting, linting and types
35+
name: Check lock, formatting, linting and typing
3636
runs-on: ubuntu-latest
3737
needs: setup
3838
strategy:
@@ -100,6 +100,9 @@ jobs:
100100
if: steps.cache-contracts.outputs.cache-hit != 'true'
101101
run: |
102102
poetry run poe compile_contracts
103+
- name: Check circular imports
104+
run: |
105+
poetry run poe circular_imports_check
103106
- name: Unit & e2e test
104107
run: |
105108
poetry run poe test_ci

circular.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import importlib.util
2+
import os
3+
import sys
4+
5+
6+
def _import_from_path(module_name, file_path):
7+
spec = importlib.util.spec_from_file_location(module_name, file_path)
8+
module = importlib.util.module_from_spec(spec)
9+
sys.modules[module_name] = module
10+
spec.loader.exec_module(module)
11+
12+
13+
def test_circular_imports():
14+
for path, _, files in os.walk("starknet_py"):
15+
py_files = [f for f in files if f.endswith(".py")]
16+
for file_ in py_files:
17+
file_path = os.path.join(path, file_)
18+
_import_from_path(file_, file_path)

docs/api/models.rst

+1-9
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@ Module containing base models and functions to operate on them.
55

66
.. py:module:: starknet_py.net.models
77
8-
.. autoclass:: InvokeFunction
8+
.. autoclass:: Invoke
99

1010
.. autoclass:: Transaction
1111
:exclude-members: __init__, __new__
1212

13-
.. autoclass:: BlockIdentifier
14-
15-
16-
.. autoenum:: TransactionType
17-
:members:
18-
1913
.. autoenum:: StarknetChainId
2014
:members:
2115

2216
.. autofunction:: compute_invoke_hash
23-
.. autofunction:: compute_address
24-

docs/migration_guide.rst

+110-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,125 @@ Migration guide
22
===============
33

44
**********************
5-
0.14.0 Migration guide
5+
0.15.0 Migration guide
66
**********************
77

8-
This version deprecates several modules and fixes underlying issues with several others.
8+
0.15.0 makes the first step to remove the cairo-lang package as StarkNet.py dependency!
9+
10+
Some classes/functions from cairo-lang package are rewritten and are a part of starknet.py:
11+
12+
- :ref:`transaction dataclasses <Transaction dataclasses>`
13+
- ``get_selector_from_name`` and ``get_storage_var_address`` functions
14+
- ``DeclaredContract`` is now :ref:`ContractClass <ContractClass>`
15+
- ``compute_class_hash`` function
16+
17+
Deprecation
18+
-----------
19+
20+
- ``compute_invoke_hash`` is deprecated in favour of ``compute_transaction_hash``
21+
- ``starknet_py.common.create_contract_class`` is deprecated in favour of ``starknet_py.common.create_compiled_contract``
922

1023
Breaking changes
1124
----------------
1225

26+
1. ``InvokeFunction`` is replaced by the ``Invoke`` dataclass (behaviour is the same, just the name is changed).
27+
28+
2. Removed from client_models.py:
29+
30+
- Invoke,
31+
- InvokeFunction,
32+
- StarknetTransaction,
33+
- AccountTransaction,
34+
- ContractClass,
35+
- Declare,
36+
- DeployAccount.
37+
38+
3. Transaction's ``tx_type`` field is renamed to ``type``.
39+
40+
4. The ``types.py`` is removed (outdated file containing only imports):
41+
42+
- import ``decode_shortstring`` and ``encode_shortstring`` from ``starknet_py.cairo.felt``,
43+
- import ``Invoke`` and ``Transaction`` from ``starknet_py.net.models.transaction``,
44+
- import ``parse_address`` from ``starknet_py.net.models.address``,
45+
- import ``net_address_from_net`` from ``starknet_py.net.networks``.
46+
47+
5. Changes in the location of some of the functions:
48+
.. list-table::
49+
:widths: 25 25 50
50+
:header-rows: 1
51+
52+
* - Function
53+
- Old Path
54+
- New Path
55+
* - compute_address
56+
- starknet_py.net.models.address
57+
- starknet_py.hash.address
58+
* - compute_transaction_hash, compute_deploy_account_transaction_hash, compute_declare_transaction_hash
59+
- starknet_py.utils.crypto.transaction_hash
60+
- starknet_py.hash.transaction
61+
* - compute_hash_on_elements
62+
- starknet_py.utils.crypto.facade
63+
- starknet_py.hash.utils
64+
* - message_signature
65+
- starknet_py.utils.crypto.facade
66+
- starknet_py.hash.utils
67+
* - pedersen_hash
68+
- starknet_py.utils.crypto.facade
69+
- starknet_py.hash.utils
70+
* -
71+
-
72+
-
73+
* - compute_class_hash
74+
- starkware.starknet.core.os.class_hash
75+
- starknet_py.hash.class_hash
76+
* - get_selector_from_name
77+
- starkware.starknet.public.abi
78+
- starknet_py.hash.selector
79+
* - get_storage_var_address
80+
- starkware.starknet.public.abi
81+
- starknet_py.hash.storage
82+
83+
84+
Transaction dataclasses
85+
-----------------------
86+
87+
All transaction's dataclasses can be imported from the ``starknet_py.net.models.transaction`` module.
88+
The main differences between them and those from the Cairo-lang:
89+
90+
- ``tx_type`` field is renamed to ``type``,
91+
- fields are not validated while creating.
92+
93+
All of them can be used as usual.
94+
95+
96+
ContractClass
97+
-------------
98+
99+
``DeclaredContract`` has been renamed to ``ContractClass``.
100+
There also exists ``CompiledContract`` dataclass, which specifies **abi** attribute to be required.
101+
102+
|
103+
104+
.. raw:: html
105+
106+
<hr>
107+
108+
|
109+
110+
**********************
111+
0.14.0 Migration guide
112+
**********************
113+
114+
This version deprecates several modules and fixes underlying issues with several others.
115+
116+
0.14.0 Breaking changes
117+
-----------------------
118+
13119
1. Renamed first parameter of :class:`~starknet_py.net.udc_deployer.deployer.ContractDeployment` from ``udc`` to ``call``, that is returned from :meth:`~starknet_py.net.udc_deployer.deployer.Deployer.create_deployment_call`.
14120

15121

16-
Deprecations
17-
------------
122+
0.14.0 Deprecations
123+
-------------------
18124

19125
1. :ref:`compiler` module. It will be removed in the future. We recommend transitioning to building contracts through Starknet CLI or external tools and using only compiled contracts with starknet.py.
20126
2. ``utils.data_transformer`` module. It has been replaced with :ref:`serializers` module.

poetry.lock

+1-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ marshmallow = "^3.15.0"
2323
marshmallow-oneofschema = "^3.0.1"
2424
typing-extensions = "^4.3.0"
2525
cairo-lang = "==0.10.3"
26+
eth-utils = "^2.1.0"
2627
furo = "^2022.12.7"
2728
marshmallow-dataclass = "<8.5.0"
2829

@@ -65,6 +66,7 @@ format_check.shell = "isort --check . && black --check ."
6566
format_diff.shell = "isort --diff . && black --diff ."
6667
typecheck = "pyright starknet_py"
6768
compile_contracts = "bash starknet_py/tests/e2e/mock/compile_contracts.sh"
69+
circular_imports_check.shell = "poetry run pytest circular.py"
6870
requirements_check.shell = "poetry export -f requirements.txt --without-hashes --extras docs | cmp - requirements.txt"
6971
ci = ["lint", "format_check", "typecheck", "requirements_check", "test_ci"]
7072

starknet_py/cairo/felt.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
TypePointer,
88
)
99
from starkware.cairo.lang.compiler.identifier_definition import StructDefinition
10-
from starkware.crypto.signature.signature import FIELD_PRIME
10+
11+
from starknet_py.constants import FIELD_PRIME
1112

1213
CairoData = List[int]
1314

starknet_py/cairo/felt_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
2-
from starkware.crypto.signature.signature import FIELD_PRIME
32

43
from starknet_py.cairo.felt import cairo_vm_range_check
4+
from starknet_py.constants import FIELD_PRIME
55

66

77
@pytest.mark.parametrize("value", [-1, FIELD_PRIME, FIELD_PRIME + 1, -FIELD_PRIME])

starknet_py/common.py

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
import warnings
2-
from typing import List, Literal, Optional, Union
3-
4-
from starkware.starknet.services.api.contract_class import ContractClass
2+
from typing import List, Literal, Optional, Union, cast
53

64
from starknet_py.compile.compiler import Compiler, StarknetCompilationSource
5+
from starknet_py.net.client_models import CompiledContract, ContractClass
6+
from starknet_py.net.schemas.gateway import CompiledContractSchema, ContractClassSchema
77

88

99
def create_compiled_contract(
1010
compilation_source: Optional[StarknetCompilationSource] = None,
1111
compiled_contract: Optional[str] = None,
1212
search_paths: Optional[List[str]] = None,
13-
) -> ContractClass:
13+
) -> CompiledContract:
14+
"""
15+
Creates CompiledContract instance.
16+
17+
:param compilation_source:
18+
source of the contract to be compiled.
19+
20+
.. deprecated:: 0.15.0
21+
Argument compilation_source has been deprecated. Use compiled_contract instead.
22+
:param compiled_contract: compiled contract string.
23+
:param search_paths:
24+
paths used to resolve compilation dependencies.
25+
26+
.. deprecated:: 0.15.0
27+
Argument search_paths has been deprecated. Use compiled_contract instead.
28+
:return: CompiledContract instance.
29+
"""
1430
warnings.warn(
15-
"Function create_compiled_contract is deprecated and will be removed in the future. "
16-
"Consider using create_contract_class instead.",
31+
"Argument compilation_source is deprecated and will be removed in the future. "
32+
"Consider using already compiled contracts instead.",
1733
category=DeprecationWarning,
1834
)
1935

@@ -26,19 +42,27 @@ def create_compiled_contract(
2642
compiled_contract = Compiler(
2743
contract_source=compilation_source, cairo_path=search_paths
2844
).compile_contract()
29-
definition = create_contract_class(compiled_contract)
30-
return definition
45+
return cast(CompiledContract, CompiledContractSchema().loads(compiled_contract))
3146

3247

3348
def create_contract_class(
3449
compiled_contract: str,
3550
) -> ContractClass:
3651
"""
37-
Creates ContractDefinition either from already compiled contract.
52+
Creates ContractClass from already compiled contract.
53+
54+
.. deprecated:: 0.15.0
55+
Function create_contract_class is deprecated and will be removed in the future.
56+
Use :meth:`create_compiled_contract` instead.
3857
3958
:return: a ContractClass.
4059
"""
41-
return ContractClass.loads(compiled_contract)
60+
warnings.warn(
61+
"Function create_contract_class is deprecated and will be removed in the future. "
62+
"Consider using create_compiled_contract instead.",
63+
category=DeprecationWarning,
64+
)
65+
return cast(ContractClass, ContractClassSchema().loads(compiled_contract))
4266

4367

4468
def int_from_hex(number: Union[str, int]) -> int:

starknet_py/compile/compiler.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
assemble_starknet_contract,
1515
)
1616
from starkware.starknet.compiler.starknet_pass_manager import starknet_pass_manager
17-
from starkware.starknet.services.api.contract_class import ContractClass
17+
18+
from starknet_py.net.client_models import ContractClass
19+
from starknet_py.net.schemas.gateway import ContractClassSchema
1820

1921
CairoSourceCode = str
2022
CairoFilename = str
@@ -66,11 +68,11 @@ def create_contract_class(
6668
compiled_contract: str,
6769
) -> ContractClass:
6870
"""
69-
Creates ContractDefinition either from already compiled contract
71+
Creates ContractClass from already compiled contract.
7072
71-
:return: a ContractDefinition
73+
:return: a ContractClass instance.
7274
"""
73-
return ContractClass.loads(compiled_contract)
75+
return typing.cast(ContractClass, ContractClassSchema().loads(compiled_contract))
7476

7577

7678
def load_cairo_source_code(filename: CairoFilename) -> str:

starknet_py/compile/compiler_test.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from starkware.cairo.lang.compiler.constants import LIBS_DIR_ENVVAR
77
from starkware.cairo.lang.compiler.import_loader import ImportLoaderError
88
from starkware.starknet.compiler.validation_utils import PreprocessorError
9-
from starkware.starknet.services.api.contract_class import ContractClass
109

11-
from starknet_py.compile.compiler import Compiler, create_contract_class
10+
from starknet_py.common import create_contract_class
11+
from starknet_py.compile.compiler import Compiler
12+
from starknet_py.net.client_models import ContractClass
1213
from starknet_py.tests.e2e.fixtures.constants import CONTRACTS_DIR
1314

1415
directory = os.path.dirname(__file__)

0 commit comments

Comments
 (0)