Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support websockets API #1515

Merged
merged 287 commits into from
Apr 8, 2025
Merged

Conversation

franciszekjob
Copy link
Collaborator

@franciszekjob franciszekjob commented Nov 7, 2024

Relates #1498

Introduced changes

Support websockets API

  • Add WebsocketClient

  • This PR contains breaking changes

Comment on lines +91 to +120
def get_block_identifier(
block_hash: Optional[Union[Hash, Tag]] = None,
block_number: Optional[Union[int, Tag]] = None,
default_tag: Optional[Tag] = "pending",
) -> dict:
return {
"block_id": _get_raw_block_identifier(block_hash, block_number, default_tag)
}


def _get_raw_block_identifier(
block_hash: Optional[Union[Hash, Tag]] = None,
block_number: Optional[Union[int, Tag]] = None,
default_tag: Optional[Tag] = "pending",
) -> Union[dict, Hash, Tag, None]:
if block_hash is not None and block_number is not None:
raise ValueError(
"Arguments block_hash and block_number are mutually exclusive."
)

if block_hash in ("latest", "pending") or block_number in ("latest", "pending"):
return block_hash or block_number

if block_hash is not None:
return {"block_hash": _to_rpc_felt(block_hash)}

if block_number is not None:
return {"block_number": block_number}

return default_tag
Copy link
Collaborator Author

@franciszekjob franciszekjob Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Moved it to it doesn't sit in full node client's file. It could be theoretically placed in a file which is not client specific or we could rename this file to e.g. rpc_utils.py.

Comment on lines +81 to +84
@post_load
def make_dataclass(self, data, **kwargs) -> BlockHeader:
return BlockHeader(**data)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: It was missing.

@franciszekjob franciszekjob changed the title Franciszekjob/1498 3 websockets Support websockets API Apr 2, 2025
Comment on lines +322 to +337
def _handle_received_message(self, message: Union[str, bytes]):
"""
Handles the received message.

:param message: The message received from the WebSocket server.
"""
data = cast(Dict, json.loads(message))

# case when the message is a response to `subscribe_{method}`
if "id" in data and data["id"] in self._pending_responses:
future = self._pending_responses.pop(data["id"])
future.set_result(data)

# case when the message is a notification
elif "method" in data:
self._handle_notification(data)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Both subscription responses and notifications are handled via recv_streaming (used in async iterator for the connection) because we cannot simultaneously use recv for immediate responses and maintain a continuous listener with recv_streaming. Consequently, all incoming messages are processed through the streaming interface.

@franciszekjob franciszekjob marked this pull request as ready for review April 2, 2025 21:39
@franciszekjob franciszekjob requested a review from ddoktorski April 2, 2025 21:40
@franciszekjob franciszekjob requested a review from ddoktorski April 7, 2025 23:31
@franciszekjob franciszekjob merged commit f0e2ffb into development Apr 8, 2025
10 checks passed
@franciszekjob franciszekjob deleted the franciszekjob/1498-3-websockets branch April 8, 2025 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants