Skip to content

Commit f0e2ffb

Browse files
Support websockets API (#1515) [skip ci]
* Fix cyclic imports test * Add ws-related dataclasses and schemas * Add `WSClient` and `RpcWSClient` * Add `FullNodeWSClient` * Cleanup `FullNodeWSClient` * Refactor `FullNodeWSClient._handle_received_message()` * Fix circular imports * Add `ReorgNotificationSchema` * Add `devnet_ws` fixture * Fix `resource_bounds` params name * Add default values in `get_storage_proof()` * Add `devnet_ws_client` fixture * Add `ws_client` and `full_node_ws_client` fixtures * Move `devnet_ws` to separate file * Refactor `FullNodeWSClient._handle_received_message()`; Fix typechecks * Fix formatting and linting * Fix linting * Update `resource_bounds` params description * Apply other code review suggestions * Fix description in migration guide * Fix example snippets in readme * Add mocked values in tests * Add setup devnet workflow * Change `devnet.yml` to be composite action * Rename `l1_transaction_hash` to `transaction_hash` in `starknet_getMessagesStatus` * Move `devnet.yml` to `composite-actions` dir * Format * Rename devnet composite action * Update devnet action path * Add missing `shell` property * Refactor devnet installation action; Add temporary workflow which will create devnet cache * Add inputs to steps using devnet action * Add missing checkout step * Add missing `devnet_sha` input to devnet setup workflow * Fix devnet installation action - use `devnet_sha` input * Trgigger CI * Remove `install_devnet.yml` workflow * Add todo in `checks.yml` * Remove temporary step for showing dirs * Move `parsed_abi_v2` into `test_event_serialization_v2` * Fix formatting * Add echo for displaying contracts v2 directory contents * Fix displaying contents of contracts v2 directory * Temporary change for displaying directory contents * Temporarily list contract v2 directory * Temporarily display owd * Revert "Temporarily display owd" This reverts commit 2ad7fe7. * Include Python 3.9 in `Setup Tests` job * Remove CI changes apart from modified devnet installation * Temporarily display directory with compiled contracts * Fix ls * Fix ls * Unconditionally compile contracts v1 and v2 * Use asdf action * Implement `test_get_transaction_status_with_failure_reason`; Code cleanup * Fix linting; Tests cleanup * Update regex in `test_rejection_reason_in_transaction_receipt` * Skip `test_compute_deploy_account_v3_transaction_hash` * Format * Skip `test_block_with_receipts_latest` * Fix failing docs tests * Fix linting * Remove contracts conditional compilation on CI * Add contracts compilation steps in docs tests * Skip `test_get_transaction_by_block_id`; Add todo * Skip `test_get_transaction_by_block_id`; Add todo * Skip `test_using_full_node_client`; Add todo * Skip `test_get_transaction_by_block_id`; Add todo * Add todos * Add todos in workflow * Update resource bounds params * Minor refactor of resource bounds params usage * Apply code review suggestion * Add `storage_root` field to `ContractLeafData` * Rename `block` to `block_id` param in ws methods * Add `is_reverted` field to `FunctionInvocation` * Add todo * Remove `block_id` param from `starknet_subscribeTransactionStatus` endpoint * Remove `contracts_storage_proof` field from `ContractsProof` * Fix `storage_root` data key * Implement `test_get_messages_status` * Fix `test_latest_resource_bounds_take_precedence` * Fix `test_deploy_prefunded_account` * Remove mocks from full node client * Add `l1_data_gas` to `ExecutionResources` * Update `CommonTransactionV3Fields.compute_resource_bounds_for_fee` * Add `l1_data_gas` to ExecutionResourcesSchema` * Fix `FullNodeClient.estimate_fee` * Fix contract tests * Temporarily skip some tests; Fix other tests * Update devnet sha * Remove code, tests and docs for old txs * Fix lint and typecheck * Fix formatting * Fix lint and typecheck * Remove old txs usages, adjust tests and docs * Fix tests * Remove todos * Remove `test_account_get_balance_eth` * Fix other tests * Skip test * Adjust devnet * Fix skip mark * Remove skip marks; Fix tests * Update ledger app sha * Refactor assertion in test * Restore original `test_ci_v2` * Remove `ResourceBounds.init_with_l1_gas_only` * Add todos * Formatting * Adjust todos and fixmes * Use `argent_account_class_hash` in `test_deploy_account_and_transfer` * Fix `test_deploy_account_and_transfer` * Revert "Fix `test_deploy_account_and_transfer`" This reverts commit d4c2eed. * Revert "Use `argent_account_class_hash` in `test_deploy_account_and_transfer`" This reverts commit 240c8ad. * Skip and add todo for `test_deploy_account_and_transfer` * Update skip message for `test_get_transaction_by_block_id` * Update todo * Remove `--initial-balance` flag for devnet start * Revert "Remove `--initial-balance` flag for devnet start" This reverts commit d9a2091. * Fix models and schemas for `get_storage_proof`; Add tests * Add storage proof response json * Restore values in storage proof response json * Use shorter example response for `get_storage_proof` test * Adjust storage proof tests * Remove helper function * Update migration guide * Update migration guide * Fix linting * Fix formatting * Remove unused imports * Add file with `ContractsStorageKeysSchema` * Fix `test_get_storage_proof` * Remove multipliers from `EstimatedFee.to_resource_bounds` * update todo * Fix and update network tests * Fix devnet client tests * Fix CI * Use asdf action * Restore `Download contracts` step in CI * Temporarily list contracts dir * Update listing dirs * Update listing dirs * Temporary CI change * Display compiled contracts path * Compile contract before running tests in CI * Partially implement `test_get_compiled_casm` * Fix params in `FullNodeClient.get_compiled_casm` * Update dependencies * Restore read api for txs other than v3 * Refactor broadcasted txn schemas * Add todo as skip reason * Add `test_sign_invoke_v3_auto_estimate` * Run `poetry lock` * Restore test values in `test_get_transaction_by_block_id_and_index` * Fix docs * Docstrings formatting * Add todos * Remove old transactions (#1557) * Fix `test_transaction_not_received_max_fee_too_big` * Fix and update network tests (#1563) * Add rust toolchain installation * Add `HintSchema`; Rename fields * Update field names in models * Add tests for `get_compiled_casm` * Move models and schema from executables api to separate files * Add docs * Fix migration guide * Rename file * Fix `test_get_compiled_casm` for devnet * Remove unused import * Add `STARKNET_PY_MARSHMALLOW_UNKNOWN_EXCLUDE` to CI * Change `index_delta_minus_1` to `index_delta_minus1` * Add fixes in executables schemas and models * Use schema with excluding unknown fields for `CasmClassSchema` * Update todos * Support `starknet_getCompiledCasm` (#1514) * Add todos * Add missing websocket API methods; Add tests * Fix formatting * Remove composite action * Format and lint * Add more tests * Further improvements of websocket client * Remove api docs (will be added in subsequent PR) * Run `poetry lock` * Update docstrings * Add `WebsocketClientError` * Fix `pytest_plugins` * Remove unused schemas; Refactor passing block number and block hash params * Remove unused `WSClient` * Add `devnet_ws` fixture * Add `test_new_heads_subscription_block_not_found` * Remove prints * Fix passing block id * Update websocket schemas * Add devnet websocket fixture * Remove `test_new_heads_subscription_block_not_found` * Restructure files; Add new tests * Remove unused model * Add missing docstrings * Update subscription id type to `str` (RPC 0.8.1) * Fix formatting * Remove unused schema * Add `is_connected` property; Add connection test * Little comments cleanup * Rename `reorg_notification_handler` to `on_chain_reorg` * Fix formatting * Fix serialize method in `PendingTransactionsNotificationResultField` * Apply code review suggestions * Change subscription id type to int * Change type of subscription id to str
1 parent 45fdb01 commit f0e2ffb

File tree

15 files changed

+1076
-32
lines changed

15 files changed

+1076
-32
lines changed

poetry.lock

Lines changed: 81 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ dependencies = [
2020
"aiohttp>=3.8.4,<4.0.0",
2121
"pycryptodome>=3.17,<4.0",
2222
"crypto-cpp-py==1.4.5",
23-
"eth-keyfile>=0.8.1,<1.0.0"
23+
"eth-keyfile>=0.8.1,<1.0.0",
24+
"websockets>=15.0.1,<16.0.0",
2425
]
2526

2627
[project.optional-dependencies]

starknet_py/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"starknet_py.tests.e2e.fixtures.contracts_v1",
88
"starknet_py.tests.e2e.fixtures.misc",
99
"starknet_py.tests.e2e.fixtures.devnet",
10+
"starknet_py.tests.e2e.fixtures.devnet_ws",
1011
"starknet_py.tests.e2e.fixtures.constants",
1112
"starknet_py.tests.e2e.client.fixtures.transactions",
1213
"starknet_py.tests.e2e.client.fixtures.prepare_network",

starknet_py/net/client_models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
Hash = Union[int, str]
3535
Tag = Literal["pending", "latest"]
36+
LatestTag = Literal["latest"]
3637

3738

3839
@dataclass

starknet_py/net/client_utils.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import Dict, Union, cast
2+
from typing import Dict, Optional, Union, cast
33

44
from typing_extensions import get_args
55

@@ -86,3 +86,35 @@ def _create_broadcasted_txn(transaction: AccountTransaction) -> dict:
8686
Dict,
8787
BroadcastedTransactionSchema().dump(obj=transaction),
8888
)
89+
90+
91+
def get_block_identifier(
92+
block_hash: Optional[Union[Hash, Tag]] = None,
93+
block_number: Optional[Union[int, Tag]] = None,
94+
default_tag: Optional[Tag] = "pending",
95+
) -> dict:
96+
return {
97+
"block_id": _get_raw_block_identifier(block_hash, block_number, default_tag)
98+
}
99+
100+
101+
def _get_raw_block_identifier(
102+
block_hash: Optional[Union[Hash, Tag]] = None,
103+
block_number: Optional[Union[int, Tag]] = None,
104+
default_tag: Optional[Tag] = "pending",
105+
) -> Union[dict, Hash, Tag, None]:
106+
if block_hash is not None and block_number is not None:
107+
raise ValueError(
108+
"Arguments block_hash and block_number are mutually exclusive."
109+
)
110+
111+
if block_hash in ("latest", "pending") or block_number in ("latest", "pending"):
112+
return block_hash or block_number
113+
114+
if block_hash is not None:
115+
return {"block_hash": _to_rpc_felt(block_hash)}
116+
117+
if block_number is not None:
118+
return {"block_number": block_number}
119+
120+
return default_tag

starknet_py/net/full_node_client.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@
4141
)
4242
from starknet_py.net.client_utils import (
4343
_create_broadcasted_txn,
44+
_get_raw_block_identifier,
4445
_is_valid_eth_address,
4546
_to_rpc_felt,
4647
_to_storage_key,
4748
encode_l1_message,
49+
get_block_identifier,
4850
)
4951
from starknet_py.net.executable_models import CasmClass
5052
from starknet_py.net.http_client import RpcHttpClient
@@ -862,31 +864,3 @@ async def trace_block_transactions(
862864
List[BlockTransactionTrace],
863865
BlockTransactionTraceSchema().load(res, many=True),
864866
)
865-
866-
867-
def get_block_identifier(
868-
block_hash: Optional[Union[Hash, Tag]] = None,
869-
block_number: Optional[Union[int, Tag]] = None,
870-
) -> dict:
871-
return {"block_id": _get_raw_block_identifier(block_hash, block_number)}
872-
873-
874-
def _get_raw_block_identifier(
875-
block_hash: Optional[Union[Hash, Tag]] = None,
876-
block_number: Optional[Union[int, Tag]] = None,
877-
) -> Union[dict, Hash, Tag, None]:
878-
if block_hash is not None and block_number is not None:
879-
raise ValueError(
880-
"Arguments block_hash and block_number are mutually exclusive."
881-
)
882-
883-
if block_hash in ("latest", "pending") or block_number in ("latest", "pending"):
884-
return block_hash or block_number
885-
886-
if block_hash is not None:
887-
return {"block_hash": _to_rpc_felt(block_hash)}
888-
889-
if block_number is not None:
890-
return {"block_number": block_number}
891-
892-
return "pending"

starknet_py/net/schemas/rpc/block.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from starknet_py.net.client_models import (
44
BlockHashAndNumber,
5+
BlockHeader,
56
BlockStateUpdate,
67
ContractsNonce,
78
DeclaredContractHash,
@@ -77,6 +78,10 @@ class BlockHeaderSchema(Schema):
7778
l1_da_mode = L1DAModeField(data_key="l1_da_mode", required=True)
7879
starknet_version = fields.String(data_key="starknet_version", required=True)
7980

81+
@post_load
82+
def make_dataclass(self, data, **kwargs) -> BlockHeader:
83+
return BlockHeader(**data)
84+
8085

8186
class BlockHashAndNumberSchema(Schema):
8287
block_hash = Felt(data_key="block_hash", required=True)

0 commit comments

Comments
 (0)