Skip to content

Commit 1f74e31

Browse files
Fix logic responsible for choosing hash method for CASM class hash calculation (#1670)
1 parent 6ae88c2 commit 1f74e31

File tree

5 files changed

+30
-31
lines changed

5 files changed

+30
-31
lines changed

docs/migration_guide.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ Migration guide
22
===============
33

44
***************************
5-
x.y.z Migration guide
5+
0.29.0-rc.1 Migration guide
66
***************************
77

8-
x.y.z Bugfixes
9-
--------------
8+
0.29.0-rc.1 Bugfixes
9+
--------------------
1010

1111
1. Fixed parsing ABI that contains signed integers (e.g. ``i128``).
12+
2. Fixed logic for choosing hash method used to calculated CASM class hash.
1213

1314
***************************
1415
0.29.0-rc.0 Migration guide

starknet_py/contract.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Dict, List, Optional, Tuple, TypeVar, Union
88

99
from marshmallow import ValidationError
10+
from semver import Version
1011

1112
from starknet_py.abi.v0 import Abi as AbiV0
1213
from starknet_py.abi.v0 import AbiParser as AbiParserV0
@@ -23,7 +24,7 @@
2324
from starknet_py.common import create_compiled_contract, create_sierra_compiled_contract
2425
from starknet_py.constants import DEFAULT_DEPLOYER_ADDRESS
2526
from starknet_py.contract_utils import _extract_compiled_class_hash, _unpack_provider
26-
from starknet_py.hash.casm_class_hash import get_casm_hash_method_for_rpc_version
27+
from starknet_py.hash.casm_class_hash import get_casm_hash_method_for_starknet_version
2728
from starknet_py.hash.selector import get_selector_from_name
2829
from starknet_py.net.account.base_account import BaseAccount
2930
from starknet_py.net.client import Client
@@ -722,8 +723,9 @@ async def declare_v3(
722723
:return: DeclareResult instance.
723724
"""
724725

725-
rpc_version = await account.client.spec_version()
726-
hash_method = get_casm_hash_method_for_rpc_version(rpc_version)
726+
block = await account.client.get_block()
727+
starknet_version = Version.parse(block.starknet_version)
728+
hash_method = get_casm_hash_method_for_starknet_version(starknet_version)
727729

728730
compiled_class_hash = _extract_compiled_class_hash(
729731
compiled_contract_casm, compiled_class_hash, hash_method=hash_method

starknet_py/hash/casm_class_hash.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
CASM_CLASS_VERSION = "COMPILED_CLASS_V1"
1818

1919

20-
def get_casm_hash_method_for_rpc_version(rpc_version: str) -> HashMethod:
21-
# RPC 0.10.0 and later use Blake2s
22-
version = Version.parse(rpc_version)
23-
24-
if version >= Version.parse("0.10.0"):
20+
def get_casm_hash_method_for_starknet_version(starknet_version: Version) -> HashMethod:
21+
# Starknet 0.14.1 and later use Blake2s
22+
if starknet_version >= Version.parse("0.14.1"):
2523
return HashMethod.BLAKE2S
2624

2725
return HashMethod.POSEIDON

starknet_py/net/full_node_client.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ def __init__(
113113
"""
114114
self.url = node_url
115115
self._client = RpcHttpClient(url=node_url, session=session)
116-
self._spec_version: Optional[str] = None
117116

118117
async def get_block(
119118
self,
@@ -773,15 +772,14 @@ async def spec_version(self) -> str:
773772
774773
:return: String with version of the Starknet JSON-RPC specification.
775774
"""
776-
if self._spec_version is None:
777-
self._spec_version = cast(
778-
str,
779-
await self._client.call(
780-
method_name="specVersion",
781-
params={},
782-
),
783-
)
784-
return self._spec_version
775+
spec_version = cast(
776+
str,
777+
await self._client.call(
778+
method_name="specVersion",
779+
params={},
780+
),
781+
)
782+
return spec_version
785783

786784
async def get_transaction_status(self, tx_hash: Hash) -> TransactionStatusResponse:
787785
res = await self._client.call(

starknet_py/tests/unit/hash/casm_class_hash_test.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from starknet_py.common import create_casm_class
55
from starknet_py.hash.casm_class_hash import (
66
compute_casm_class_hash,
7-
get_casm_hash_method_for_rpc_version,
7+
get_casm_hash_method_for_starknet_version,
88
)
99
from starknet_py.hash.hash_method import HashMethod
1010
from starknet_py.tests.e2e.fixtures.constants import PRECOMPILED_CONTRACTS_DIR
@@ -57,20 +57,20 @@ def test_precompiled_compute_casm_class_hash_with_poseidon(casm_contract_class_s
5757
@pytest.mark.parametrize(
5858
"rpc_version, expected_hash_method",
5959
[
60-
("0.8.0", HashMethod.POSEIDON),
61-
("0.9.0", HashMethod.POSEIDON),
62-
("0.9.1", HashMethod.POSEIDON),
63-
("0.10.0", HashMethod.BLAKE2S),
64-
("0.10.1", HashMethod.BLAKE2S),
65-
("0.11.0", HashMethod.BLAKE2S),
60+
("0.13.5", HashMethod.POSEIDON),
61+
("0.14.0", HashMethod.POSEIDON),
62+
("0.14.1", HashMethod.BLAKE2S),
63+
("0.15.0", HashMethod.BLAKE2S),
6664
("1.0.0", HashMethod.BLAKE2S),
65+
("1.10.0", HashMethod.BLAKE2S),
6766
],
6867
)
69-
def test_get_casm_hash_method_for_rpc_version(rpc_version, expected_hash_method):
70-
"""Test that the correct hash method is returned for different RPC versions."""
71-
hash_method = get_casm_hash_method_for_rpc_version(rpc_version)
68+
def test_get_casm_hash_method_for_starknet_version(rpc_version, expected_hash_method):
69+
"""Test that the correct hash method is returned for different Starknet versions."""
70+
hash_method = get_casm_hash_method_for_starknet_version(rpc_version)
7271
assert hash_method == expected_hash_method
7372

73+
7474
@pytest.mark.parametrize(
7575
"contract, expected_casm_class_hash_blake2s",
7676
[

0 commit comments

Comments
 (0)