Skip to content

Commit

Permalink
Remove part of the cairo lang dependency (#592)
Browse files Browse the repository at this point in the history
* Remove usages of `CastableToHash` (#587)

* Remove CastableToHash import

* Change hash to int

* Replace `FIELD_PRIME` in felt.py (#588)

* Add FIELD_PRIME constant

* Change FIELD_PRIME to hex

* Replace `get_random_private_key` (#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 (#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 (#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` (#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 (#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 (#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 (#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) (#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` (#737)

* remove usage of StarknetErrorCode

* one last usage of `get_selector_from_name` from cairo lang

* Support calculating class_hash (#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 (#677)

* Create hash module

* Format

* Fix docs build

* Add docstrings

* Add pedersen_hash tests

* Format

* Fix test

* Replace ContractClass (#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 (#677)

* Create hash module

* Format

* Fix docs build

* Add docstrings

* Add pedersen_hash tests

* Format

* Fix test

* Replace ContractClass (#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 (#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 (#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]>
  • Loading branch information
3 people authored Feb 20, 2023
1 parent 91d2572 commit ab6835a
Show file tree
Hide file tree
Showing 88 changed files with 1,450 additions and 503 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
poetry install
lint:
name: Check lock, formatting, linting and types
name: Check lock, formatting, linting and typing
runs-on: ubuntu-latest
needs: setup
strategy:
Expand Down Expand Up @@ -100,6 +100,9 @@ jobs:
if: steps.cache-contracts.outputs.cache-hit != 'true'
run: |
poetry run poe compile_contracts
- name: Check circular imports
run: |
poetry run poe circular_imports_check
- name: Unit & e2e test
run: |
poetry run poe test_ci
Expand Down
18 changes: 18 additions & 0 deletions circular.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import importlib.util
import os
import sys


def _import_from_path(module_name, file_path):
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)


def test_circular_imports():
for path, _, files in os.walk("starknet_py"):
py_files = [f for f in files if f.endswith(".py")]
for file_ in py_files:
file_path = os.path.join(path, file_)
_import_from_path(file_, file_path)
10 changes: 1 addition & 9 deletions docs/api/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@ Module containing base models and functions to operate on them.

.. py:module:: starknet_py.net.models
.. autoclass:: InvokeFunction
.. autoclass:: Invoke

.. autoclass:: Transaction
:exclude-members: __init__, __new__

.. autoclass:: BlockIdentifier


.. autoenum:: TransactionType
:members:

.. autoenum:: StarknetChainId
:members:

.. autofunction:: compute_invoke_hash
.. autofunction:: compute_address

114 changes: 110 additions & 4 deletions docs/migration_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,125 @@ Migration guide
===============

**********************
0.14.0 Migration guide
0.15.0 Migration guide
**********************

This version deprecates several modules and fixes underlying issues with several others.
0.15.0 makes the first step to remove the cairo-lang package as StarkNet.py dependency!

Some classes/functions from cairo-lang package are rewritten and are a part of starknet.py:

- :ref:`transaction dataclasses <Transaction dataclasses>`
- ``get_selector_from_name`` and ``get_storage_var_address`` functions
- ``DeclaredContract`` is now :ref:`ContractClass <ContractClass>`
- ``compute_class_hash`` function

Deprecation
-----------

- ``compute_invoke_hash`` is deprecated in favour of ``compute_transaction_hash``
- ``starknet_py.common.create_contract_class`` is deprecated in favour of ``starknet_py.common.create_compiled_contract``

Breaking changes
----------------

1. ``InvokeFunction`` is replaced by the ``Invoke`` dataclass (behaviour is the same, just the name is changed).

2. Removed from client_models.py:

- Invoke,
- InvokeFunction,
- StarknetTransaction,
- AccountTransaction,
- ContractClass,
- Declare,
- DeployAccount.

3. Transaction's ``tx_type`` field is renamed to ``type``.

4. The ``types.py`` is removed (outdated file containing only imports):

- import ``decode_shortstring`` and ``encode_shortstring`` from ``starknet_py.cairo.felt``,
- import ``Invoke`` and ``Transaction`` from ``starknet_py.net.models.transaction``,
- import ``parse_address`` from ``starknet_py.net.models.address``,
- import ``net_address_from_net`` from ``starknet_py.net.networks``.

5. Changes in the location of some of the functions:
.. list-table::
:widths: 25 25 50
:header-rows: 1

* - Function
- Old Path
- New Path
* - compute_address
- starknet_py.net.models.address
- starknet_py.hash.address
* - compute_transaction_hash, compute_deploy_account_transaction_hash, compute_declare_transaction_hash
- starknet_py.utils.crypto.transaction_hash
- starknet_py.hash.transaction
* - compute_hash_on_elements
- starknet_py.utils.crypto.facade
- starknet_py.hash.utils
* - message_signature
- starknet_py.utils.crypto.facade
- starknet_py.hash.utils
* - pedersen_hash
- starknet_py.utils.crypto.facade
- starknet_py.hash.utils
* -
-
-
* - compute_class_hash
- starkware.starknet.core.os.class_hash
- starknet_py.hash.class_hash
* - get_selector_from_name
- starkware.starknet.public.abi
- starknet_py.hash.selector
* - get_storage_var_address
- starkware.starknet.public.abi
- starknet_py.hash.storage


Transaction dataclasses
-----------------------

All transaction's dataclasses can be imported from the ``starknet_py.net.models.transaction`` module.
The main differences between them and those from the Cairo-lang:

- ``tx_type`` field is renamed to ``type``,
- fields are not validated while creating.

All of them can be used as usual.


ContractClass
-------------

``DeclaredContract`` has been renamed to ``ContractClass``.
There also exists ``CompiledContract`` dataclass, which specifies **abi** attribute to be required.

|
.. raw:: html

<hr>

|
**********************
0.14.0 Migration guide
**********************

This version deprecates several modules and fixes underlying issues with several others.

0.14.0 Breaking changes
-----------------------

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`.


Deprecations
------------
0.14.0 Deprecations
-------------------

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.
2. ``utils.data_transformer`` module. It has been replaced with :ref:`serializers` module.
Expand Down
13 changes: 1 addition & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ marshmallow = "^3.15.0"
marshmallow-oneofschema = "^3.0.1"
typing-extensions = "^4.3.0"
cairo-lang = "==0.10.3"
eth-utils = "^2.1.0"
furo = "^2022.12.7"
marshmallow-dataclass = "<8.5.0"

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

Expand Down
3 changes: 2 additions & 1 deletion starknet_py/cairo/felt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
TypePointer,
)
from starkware.cairo.lang.compiler.identifier_definition import StructDefinition
from starkware.crypto.signature.signature import FIELD_PRIME

from starknet_py.constants import FIELD_PRIME

CairoData = List[int]

Expand Down
2 changes: 1 addition & 1 deletion starknet_py/cairo/felt_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from starkware.crypto.signature.signature import FIELD_PRIME

from starknet_py.cairo.felt import cairo_vm_range_check
from starknet_py.constants import FIELD_PRIME


@pytest.mark.parametrize("value", [-1, FIELD_PRIME, FIELD_PRIME + 1, -FIELD_PRIME])
Expand Down
44 changes: 34 additions & 10 deletions starknet_py/common.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import warnings
from typing import List, Literal, Optional, Union

from starkware.starknet.services.api.contract_class import ContractClass
from typing import List, Literal, Optional, Union, cast

from starknet_py.compile.compiler import Compiler, StarknetCompilationSource
from starknet_py.net.client_models import CompiledContract, ContractClass
from starknet_py.net.schemas.gateway import CompiledContractSchema, ContractClassSchema


def create_compiled_contract(
compilation_source: Optional[StarknetCompilationSource] = None,
compiled_contract: Optional[str] = None,
search_paths: Optional[List[str]] = None,
) -> ContractClass:
) -> CompiledContract:
"""
Creates CompiledContract instance.
:param compilation_source:
source of the contract to be compiled.
.. deprecated:: 0.15.0
Argument compilation_source has been deprecated. Use compiled_contract instead.
:param compiled_contract: compiled contract string.
:param search_paths:
paths used to resolve compilation dependencies.
.. deprecated:: 0.15.0
Argument search_paths has been deprecated. Use compiled_contract instead.
:return: CompiledContract instance.
"""
warnings.warn(
"Function create_compiled_contract is deprecated and will be removed in the future. "
"Consider using create_contract_class instead.",
"Argument compilation_source is deprecated and will be removed in the future. "
"Consider using already compiled contracts instead.",
category=DeprecationWarning,
)

Expand All @@ -26,19 +42,27 @@ def create_compiled_contract(
compiled_contract = Compiler(
contract_source=compilation_source, cairo_path=search_paths
).compile_contract()
definition = create_contract_class(compiled_contract)
return definition
return cast(CompiledContract, CompiledContractSchema().loads(compiled_contract))


def create_contract_class(
compiled_contract: str,
) -> ContractClass:
"""
Creates ContractDefinition either from already compiled contract.
Creates ContractClass from already compiled contract.
.. deprecated:: 0.15.0
Function create_contract_class is deprecated and will be removed in the future.
Use :meth:`create_compiled_contract` instead.
:return: a ContractClass.
"""
return ContractClass.loads(compiled_contract)
warnings.warn(
"Function create_contract_class is deprecated and will be removed in the future. "
"Consider using create_compiled_contract instead.",
category=DeprecationWarning,
)
return cast(ContractClass, ContractClassSchema().loads(compiled_contract))


def int_from_hex(number: Union[str, int]) -> int:
Expand Down
10 changes: 6 additions & 4 deletions starknet_py/compile/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
assemble_starknet_contract,
)
from starkware.starknet.compiler.starknet_pass_manager import starknet_pass_manager
from starkware.starknet.services.api.contract_class import ContractClass

from starknet_py.net.client_models import ContractClass
from starknet_py.net.schemas.gateway import ContractClassSchema

CairoSourceCode = str
CairoFilename = str
Expand Down Expand Up @@ -66,11 +68,11 @@ def create_contract_class(
compiled_contract: str,
) -> ContractClass:
"""
Creates ContractDefinition either from already compiled contract
Creates ContractClass from already compiled contract.
:return: a ContractDefinition
:return: a ContractClass instance.
"""
return ContractClass.loads(compiled_contract)
return typing.cast(ContractClass, ContractClassSchema().loads(compiled_contract))


def load_cairo_source_code(filename: CairoFilename) -> str:
Expand Down
5 changes: 3 additions & 2 deletions starknet_py/compile/compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from starkware.cairo.lang.compiler.constants import LIBS_DIR_ENVVAR
from starkware.cairo.lang.compiler.import_loader import ImportLoaderError
from starkware.starknet.compiler.validation_utils import PreprocessorError
from starkware.starknet.services.api.contract_class import ContractClass

from starknet_py.compile.compiler import Compiler, create_contract_class
from starknet_py.common import create_contract_class
from starknet_py.compile.compiler import Compiler
from starknet_py.net.client_models import ContractClass
from starknet_py.tests.e2e.fixtures.constants import CONTRACTS_DIR

directory = os.path.dirname(__file__)
Expand Down
Loading

0 comments on commit ab6835a

Please sign in to comment.