Skip to content

Commit 22eee38

Browse files
Starknet v0.12.2 update (#1152)
* add new gateway methods * changed get_state_update to match new implementation * changed non prefixed hexes to felts * update migration guide * update migration guide * mark tests with non-prefixed hex running on devnet and testnet with `xfail` * more tests marked as `xfail` * more tests marked as `xfail` * remove condition in docs `xfail` tests * lint * review changes * add docs info * change str() to cast() in get_public_key * lint * fighting with codecov vol. 1 * fix poetry version to 1.5.1 * fix #1155 * lint * fight with codecov vol. 2 * i hate codecov * fix docs * add docstrings * another docs fix * fix #1159 * i hate codecov vol.2 * i hate codecov vol. 3
1 parent aefc710 commit 22eee38

File tree

12 files changed

+256
-19
lines changed

12 files changed

+256
-19
lines changed

.github/workflows/checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Install poetry
2525
run: |
2626
python -m pip install --upgrade pip
27-
pip install poetry==1.5.1
27+
pip install poetry
2828
poetry config installer.modern-installation false
2929
3030
- name: Set up Python 3.9

docs/migration_guide.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Migration guide
55
0.18.0 Migration guide
66
**********************
77

8-
This version of starknet.py brings support Starknet 0.12.1 and `RPC v0.4.0 <https://github.com/starkware-libs/starknet-specs/releases/tag/v0.4.0>`!
8+
This version of starknet.py brings support Starknet 0.12.1, 0.12.2 and `RPC v0.4.0 <https://github.com/starkware-libs/starknet-specs/releases/tag/v0.4.0>`_!
99

1010

1111
1. :class:`TransactionReceipt` dataclass properties have been changed (more details in RPC specification linked above).
@@ -33,7 +33,7 @@ This version of starknet.py brings support Starknet 0.12.1 and `RPC v0.4.0 <http
3333

3434
.. currentmodule:: starknet_py.net.client
3535

36-
5. Lowered ``check_interval`` parameter default value in :meth:``Client.wait_for_tx`` from 5 seconds to 2.
36+
5. Lowered ``check_interval`` parameter default value in :meth:`Client.wait_for_tx` from 5 seconds to 2.
3737

3838
.. currentmodule:: starknet_py.net.client_models
3939

@@ -43,9 +43,15 @@ This version of starknet.py brings support Starknet 0.12.1 and `RPC v0.4.0 <http
4343

4444
7. :func:`decode_shortstring` now is returned without ``\x00`` in front of the decoded string.
4545

46+
.. currentmodule:: starknet_py.net.gateway_client
47+
48+
8. Added two new methods to :class:`GatewayClient` - :meth:`GatewayClient.get_public_key` and :meth:`GatewayClient.get_signature`.
49+
9. :meth:`GatewayClient.get_state_update` now accepts additional parameter - `include_block`.
50+
4651
.. currentmodule:: starknet_py.net.signer.stark_curve_signer
4752

48-
8. :class:`KeyPair` and :meth:`KeyPair.from_private_key` now can accept keys in string representation.
53+
10. :class:`KeyPair` and :meth:`KeyPair.from_private_key` now can accept keys in string representation.
54+
4955

5056
0.18.0 Bugfixes
5157
---------------

starknet_py/net/account/account_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from unittest.mock import AsyncMock, patch
23

34
import pytest
@@ -49,6 +50,11 @@ async def test_get_balance_default_token_address(net, call_contract):
4950
assert call.to_addr == parse_address(FEE_CONTRACT_ADDRESS)
5051

5152

53+
# TODO (#1154): remove line below
54+
@pytest.mark.xfail(
55+
"--client=gateway" in sys.argv,
56+
reason="0.12.2 returns Felts in state_root, devnet returns NonPrefixedHex",
57+
)
5258
@pytest.mark.asyncio
5359
async def test_account_get_balance(account, map_contract):
5460
balance = await account.get_balance()

starknet_py/net/client_models.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,16 @@ class PendingBlockStateUpdate:
529529
state_diff: StateDiff
530530

531531

532+
@dataclass
533+
class StateUpdateWithBlock:
534+
"""
535+
Dataclass representing a change in state of a block with the block.
536+
"""
537+
538+
block: GatewayBlock
539+
state_update: BlockStateUpdate
540+
541+
532542
@dataclass
533543
class ContractCode:
534544
"""
@@ -671,3 +681,24 @@ class TransactionStatusResponse:
671681
transaction_status: TransactionStatus
672682
finality_status: Optional[TransactionFinalityStatus] = None
673683
execution_status: Optional[TransactionExecutionStatus] = None
684+
685+
686+
@dataclass
687+
class SignatureInput:
688+
"""
689+
Dataclass representing a signature input.
690+
"""
691+
692+
block_hash: int
693+
state_diff_commitment: int
694+
695+
696+
@dataclass
697+
class SignatureOnStateDiff:
698+
"""
699+
Dataclass representing signature on state diff commitment and block hash.
700+
"""
701+
702+
block_number: int
703+
signature: List[int]
704+
signature_input: SignatureInput

starknet_py/net/gateway_client.py

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Hash,
2121
SentTransactionResponse,
2222
SierraContractClass,
23+
SignatureOnStateDiff,
24+
StateUpdateWithBlock,
2325
Tag,
2426
Transaction,
2527
TransactionReceipt,
@@ -49,7 +51,9 @@
4951
DeployAccountTransactionResponseSchema,
5052
EstimatedFeeSchema,
5153
SentTransactionSchema,
54+
SignatureOnStateDiffSchema,
5255
StarknetBlockSchema,
56+
StateUpdateWithBlockSchema,
5357
TransactionReceiptSchema,
5458
TransactionStatusSchema,
5559
TypesOfContractClassSchema,
@@ -143,21 +147,33 @@ async def get_state_update(
143147
self,
144148
block_hash: Optional[Union[Hash, Tag]] = None,
145149
block_number: Optional[Union[int, Tag]] = None,
146-
) -> BlockStateUpdate:
150+
include_block: bool = False,
151+
) -> Union[BlockStateUpdate, StateUpdateWithBlock]:
147152
"""
148-
Get the information about the result of executing the requested block
153+
Get the information about the result of executing the requested block.
149154
150-
:param block_hash: Block's hash
151-
:param block_number: Block's number (default "pending")
152-
:return: BlockStateUpdate object representing changes in the requested block
155+
:param block_hash: Block's hash.
156+
:param block_number: Block's number (default "pending").
157+
:param include_block: Flag deciding whether to include the queried block. Defaults to false.
158+
:return: BlockStateUpdate object representing changes in the requested block.
153159
"""
154160
block_identifier = get_block_identifier(
155161
block_hash=block_hash, block_number=block_number
156162
)
163+
164+
params = {
165+
"includeBlock": str(include_block).lower(),
166+
**block_identifier,
167+
}
168+
157169
res = await self._feeder_gateway_client.call(
158-
method_name="get_state_update",
159-
params=block_identifier,
170+
method_name="get_state_update", params=params
160171
)
172+
173+
if include_block:
174+
return StateUpdateWithBlockSchema().load(
175+
res, unknown=EXCLUDE
176+
) # pyright: ignore
161177
return BlockStateUpdateSchema().load(res, unknown=EXCLUDE) # pyright: ignore
162178

163179
async def get_storage_at(
@@ -461,6 +477,42 @@ async def get_full_contract(
461477
res, unknown=EXCLUDE
462478
) # pyright: ignore
463479

480+
async def get_signature(
481+
self,
482+
block_hash: Optional[Union[Hash, Tag]] = None,
483+
block_number: Optional[Union[int, Tag]] = None,
484+
) -> SignatureOnStateDiff:
485+
"""
486+
Information on what is this signature and how it is calulated here:
487+
https://community.starknet.io/t/introducing-p2p-authentication-and-mismatch-resolution-in-v0-12-2/97993#signature-on-state-diff-commitment-and-block-hash-3
488+
489+
:param block_hash: Block's hash or literals `"pending"` or `"latest"`.
490+
:param block_number: Block's number or literals `"pending"` or `"latest"`.
491+
492+
:return: Signature on state diff.
493+
"""
494+
block_identifier = get_block_identifier(
495+
block_hash=block_hash, block_number=block_number
496+
)
497+
res = await self._feeder_gateway_client.call(
498+
method_name="get_signature",
499+
params={**block_identifier},
500+
)
501+
return SignatureOnStateDiffSchema().load(
502+
res, unknown=EXCLUDE
503+
) # pyright: ignore
504+
505+
async def get_public_key(self) -> str:
506+
"""
507+
Method returning current public key.
508+
509+
:return: Public key.
510+
"""
511+
public_key = await self._feeder_gateway_client.call(
512+
method_name="get_public_key"
513+
)
514+
return cast(str, public_key)
515+
464516

465517
def get_block_identifier(
466518
block_hash: Optional[Union[Hash, Tag]] = None,

starknet_py/net/schemas/gateway.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
SierraContractClass,
4040
SierraEntryPoint,
4141
SierraEntryPointsByType,
42+
SignatureInput,
43+
SignatureOnStateDiff,
44+
StateUpdateWithBlock,
4245
StorageDiffItem,
4346
TransactionReceipt,
4447
TransactionStatusResponse,
@@ -258,7 +261,7 @@ class StarknetBlockSchema(Schema):
258261
parent_block_hash = Felt(data_key="parent_block_hash", required=True)
259262
block_number = fields.Integer(data_key="block_number")
260263
status = BlockStatusField(data_key="status", required=True)
261-
root = NonPrefixedHex(data_key="state_root")
264+
root = Felt(data_key="state_root")
262265
transactions = fields.List(
263266
fields.Nested(TypesOfTransactionsSchema(unknown=EXCLUDE)),
264267
data_key="transactions",
@@ -420,8 +423,8 @@ def make_dataclass(self, data, **kwargs) -> GatewayStateDiff:
420423

421424
class BlockStateUpdateSchema(Schema):
422425
block_hash = Felt(data_key="block_hash", required=True)
423-
new_root = NonPrefixedHex(data_key="new_root", required=True)
424-
old_root = NonPrefixedHex(data_key="old_root", required=True)
426+
new_root = Felt(data_key="new_root", required=True)
427+
old_root = Felt(data_key="old_root", required=True)
425428
state_diff = fields.Nested(StateDiffSchema(), data_key="state_diff", required=True)
426429

427430
@post_load
@@ -439,6 +442,17 @@ def fix_field(field: Dict, inner_class: Any) -> List[Any]:
439442
return BlockStateUpdate(**data)
440443

441444

445+
class StateUpdateWithBlockSchema(Schema):
446+
block = fields.Nested(StarknetBlockSchema(), data_key="block", required=True)
447+
state_update = fields.Nested(
448+
BlockStateUpdateSchema(), data_key="state_update", required=True
449+
)
450+
451+
@post_load
452+
def make_dataclass(self, data, **kwargs) -> StateUpdateWithBlock:
453+
return StateUpdateWithBlock(**data)
454+
455+
442456
class EntryPointSchema(Schema):
443457
offset = Felt(data_key="offset", required=True)
444458
selector = Felt(data_key="selector", required=True)
@@ -623,3 +637,24 @@ class TransactionStatusSchema(Schema):
623637
@post_load
624638
def make_result(self, data, **kwargs) -> TransactionStatusResponse:
625639
return TransactionStatusResponse(**data)
640+
641+
642+
class SignatureInputSchema(Schema):
643+
block_hash = Felt(data_key="block_hash", required=True)
644+
state_diff_commitment = Felt(data_key="state_diff_commitment", required=True)
645+
646+
@post_load
647+
def make_dataclass(self, data, **kwargs) -> SignatureInput:
648+
return SignatureInput(**data)
649+
650+
651+
class SignatureOnStateDiffSchema(Schema):
652+
block_number = Felt(data_key="block_number", required=True)
653+
signature = fields.List(Felt(), data_key="signature", required=True)
654+
signature_input = fields.Nested(
655+
SignatureInputSchema(), data_key="signature_input", required=True
656+
)
657+
658+
@post_load
659+
def make_dataclass(self, data, **kwargs) -> SignatureOnStateDiff:
660+
return SignatureOnStateDiff(**data)

starknet_py/net/signer/test_stark_curve_signer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,8 @@ def test_key_pair():
6464

6565
assert isinstance(key_pair.public_key, int)
6666
assert isinstance(key_pair.private_key, int)
67+
68+
key_pair = KeyPair.from_private_key("0x789")
69+
70+
assert isinstance(key_pair.public_key, int)
71+
assert isinstance(key_pair.private_key, int)

starknet_py/tests/e2e/account/account_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from typing import cast
23
from unittest.mock import AsyncMock, patch
34

@@ -148,6 +149,11 @@ async def test_get_class_hash_at(map_contract, account):
148149
assert class_hash != 0
149150

150151

152+
# TODO (#1154): remove line below
153+
@pytest.mark.xfail(
154+
"--client=gateway" in sys.argv,
155+
reason="0.12.2 returns Felts in state_root, devnet returns NonPrefixedHex",
156+
)
151157
@pytest.mark.asyncio()
152158
async def test_get_nonce(account, map_contract):
153159
nonce = await account.get_nonce()

starknet_py/tests/e2e/block_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
import pytest
24

35
from starknet_py.contract import Contract
@@ -22,6 +24,11 @@ async def declare_contract(account: BaseAccount, compiled_contract: str):
2224
await declare_result.wait_for_acceptance()
2325

2426

27+
# TODO (#1154): remove line below
28+
@pytest.mark.xfail(
29+
"--client=gateway" in sys.argv,
30+
reason="0.12.2 returns Felts in state_root, devnet returns NonPrefixedHex",
31+
)
2532
@pytest.mark.asyncio
2633
async def test_pending_block(account, map_compiled_contract):
2734
await declare_contract(account, map_compiled_contract)
@@ -35,6 +42,11 @@ async def test_pending_block(account, map_compiled_contract):
3542
assert isinstance(blk, PendingStarknetBlock)
3643

3744

45+
# TODO (#1154): remove line below
46+
@pytest.mark.xfail(
47+
"--client=gateway" in sys.argv,
48+
reason="0.12.2 returns Felts in state_root, devnet returns NonPrefixedHex",
49+
)
3850
@pytest.mark.asyncio
3951
async def test_latest_block(account, map_compiled_contract):
4052
await declare_contract(account, map_compiled_contract)

0 commit comments

Comments
 (0)