Skip to content

Commit 8d840b3

Browse files
committed
Address remaining comments from PR #3554:
- ``sx`` -> ``sub``. - Some clarifying changes on type hints and arg names in tests. - Docs update to clarify handlers. This whole area of the docs will get a lot of attention and refactoring in a future PR. But at least the current docs are more descriptive.
1 parent 28e8e92 commit 8d840b3

File tree

8 files changed

+185
-172
lines changed

8 files changed

+185
-172
lines changed

docs/internals.rst

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,12 @@ the user is done with it.
445445
.. code-block:: python
446446
447447
>>> async def new_heads_handler(
448-
... context: NewHeadsSubscriptionContext,
448+
... handler_context: NewHeadsSubscriptionContext,
449449
... ) -> None:
450-
... result = context.result
450+
... result = handler_context.result
451451
... print(f"New block header: {result}\n")
452452
... if result["number"] > 1234567:
453-
... await context.subscription.unsubscribe()
453+
... await handler_context.subscription.unsubscribe()
454454
455455
>>> async def ws_subscription_example():
456456
... async with AsyncWeb3(WebSocketProvider(f"ws://127.0.0.1:8546")) as w3:
@@ -470,7 +470,17 @@ The manager can also subscribe to many subscriptions at one time. The
470470
friendly API for managing subscriptions. Since each connection and provider instance
471471
has its own message listener task and subscription manager instance, you can subscribe
472472
to many subscriptions at once and handle the many responses that come in over the socket
473-
connections.
473+
connections via handlers. The handlers contain:
474+
475+
- ``async_w3``: The ``AsyncWeb3`` instance that the subscription was made on.
476+
- ``subscription``: The subscription instance that the handler is attached to.
477+
- ``result``: The response that came in over the socket connection for the subscription.
478+
479+
Subscriptions also accept a ``handler_context`` argument that can be used to pass
480+
additional information to the handler when subscribing to a subscription. This can be
481+
used to pass in an event object, for example, that can be used to parse a log event
482+
when it comes in.
483+
474484
475485
.. code-block:: python
476486
@@ -490,23 +500,28 @@ connections.
490500
... )
491501
492502
>>> async def new_heads_handler(
493-
... context: NewHeadsSubscriptionContext,
503+
... handler_context: NewHeadsSubscriptionContext,
494504
... ) -> None:
495-
... print(f"New block header: {context.result}\n")
496-
... if context.result["number"] > 1234567:
497-
... await context.subscription.unsubscribe()
505+
... header = handler_context.result
506+
... print(f"New block header: {header}\n")
507+
... if header["number"] > 1234567:
508+
... await handler_context.subscription.unsubscribe()
498509
499510
>>> async def pending_txs_handler(
500-
... context: PendingTxSubscriptionContext,
511+
... handler_context: PendingTxSubscriptionContext,
501512
... ) -> None:
502513
... ...
503514
504515
>>> async def log_handler(
505-
... context: LogsSubscriptionContext,
516+
... handler_context: LogsSubscriptionContext,
506517
... ) -> None:
507-
... ...
518+
... log_receipt = handler_context.result
519+
... # event is now available in the handler context, because we pass it to in the
520+
... # ``handler_context`` when subscribing to the log
521+
... event_data = handler_context.event.process_log(log_receipt)
522+
... print(f"Log event data: {event_data}\n")
508523
509-
>>> async def sx_manager():
524+
>>> async def sub_manager():
510525
... local_w3 = await AsyncWeb3(AsyncIPCProvider(LOCAL_IPC, label="mainnet-ipc"))
511526
... # subscribe to many subscriptions via the subscription manager with handlers
512527
... await local_w3.subscription_manager.subscribe(
@@ -522,6 +537,8 @@ connections.
522537
... address=local_w3.to_checksum_address("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
523538
... topics=[HexStr("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")],
524539
... handler=log_handler,
540+
... # optional ``handler_context`` args to help parse a response
541+
... handler_context={"event": my_event},
525542
... ),
526543
... ]
527544
... )
@@ -538,7 +555,7 @@ connections.
538555
... local_w3.subscription_manager.handle_subscriptions(),
539556
... )
540557
541-
>>> asyncio.run(sx_manager())
558+
>>> asyncio.run(sub_manager())
542559
543560
544561
The ``process_subscriptions()`` method on the

tests/core/providers/test_websocket_provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121
from web3._utils.caching import (
2222
RequestInformation,
23+
generate_cache_key,
2324
)
2425
from web3._utils.module_testing.module_testing_utils import (
2526
WebSocketMessageStreamMock,
@@ -274,8 +275,7 @@ async def test_listen_event_awaits_msg_processing_when_subscription_queue_is_ful
274275
subscription_id=sub_id,
275276
)
276277
async_w3.provider._request_processor._request_information_cache.cache(
277-
# cache key is the result of `generate_cache_key` with the sub_id as the arg
278-
"0138b5d63d66121d8a6e680d23720fa7",
278+
generate_cache_key(sub_id),
279279
sub_request_information,
280280
)
281281

tests/core/subscriptions/test_subscription_manager.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,63 +40,63 @@ async def subscription_manager():
4040

4141

4242
@pytest.mark.asyncio
43-
async def test_subscription_manager_raises_for_sx_with_the_same_label(
43+
async def test_subscription_manager_raises_for_sub_with_the_same_label(
4444
subscription_manager,
4545
):
46-
sx1 = NewHeadsSubscription(label="foo")
47-
await subscription_manager.subscribe(sx1)
46+
sub1 = NewHeadsSubscription(label="foo")
47+
await subscription_manager.subscribe(sub1)
4848

4949
with pytest.raises(
5050
Web3ValueError,
5151
match="Subscription label already exists. Subscriptions must have unique "
5252
"labels.\n label: foo",
5353
):
54-
sx2 = LogsSubscription(label="foo")
55-
await subscription_manager.subscribe(sx2)
54+
sub2 = LogsSubscription(label="foo")
55+
await subscription_manager.subscribe(sub2)
5656

5757
# make sure the subscription was subscribed to and not added to the manager
58-
assert subscription_manager.subscriptions == [sx1]
59-
assert subscription_manager._subscriptions_by_label == {"foo": sx1}
60-
assert subscription_manager._subscriptions_by_id == {"0x0": sx1}
58+
assert subscription_manager.subscriptions == [sub1]
59+
assert subscription_manager._subscriptions_by_label == {"foo": sub1}
60+
assert subscription_manager._subscriptions_by_id == {"0x0": sub1}
6161

6262

6363
@pytest.mark.asyncio
6464
async def test_subscription_manager_get_by_id(subscription_manager):
65-
sx = NewHeadsSubscription(label="foo")
66-
await subscription_manager.subscribe(sx)
67-
assert subscription_manager.get_by_id("0x0") == sx
65+
sub = NewHeadsSubscription(label="foo")
66+
await subscription_manager.subscribe(sub)
67+
assert subscription_manager.get_by_id("0x0") == sub
6868
assert subscription_manager.get_by_id("0x1") is None
6969

7070

7171
@pytest.mark.asyncio
7272
async def test_subscription_manager_get_by_label(subscription_manager):
73-
sx = NewHeadsSubscription(label="foo")
74-
await subscription_manager.subscribe(sx)
75-
assert subscription_manager.get_by_label("foo") == sx
73+
sub = NewHeadsSubscription(label="foo")
74+
await subscription_manager.subscribe(sub)
75+
assert subscription_manager.get_by_label("foo") == sub
7676
assert subscription_manager.get_by_label("bar") is None
7777

7878

7979
@pytest.mark.asyncio
8080
async def test_unsubscribe_one_by_one_clears_all_subscriptions(
8181
subscription_manager,
8282
):
83-
sx1 = NewHeadsSubscription(label="foo")
84-
sx2 = PendingTxSubscription(label="bar")
85-
await subscription_manager.subscribe(sx1)
86-
await subscription_manager.subscribe(sx2)
83+
sub1 = NewHeadsSubscription(label="foo")
84+
sub2 = PendingTxSubscription(label="bar")
85+
await subscription_manager.subscribe(sub1)
86+
await subscription_manager.subscribe(sub2)
8787

88-
await subscription_manager.unsubscribe(sx1)
89-
assert subscription_manager.subscriptions == [sx2]
88+
await subscription_manager.unsubscribe(sub1)
89+
assert subscription_manager.subscriptions == [sub2]
9090

91-
await subscription_manager.unsubscribe(sx2)
91+
await subscription_manager.unsubscribe(sub2)
9292
assert subscription_manager.subscriptions == []
9393

9494

9595
@pytest.mark.asyncio
9696
async def test_unsubscribe_all_clears_all_subscriptions(subscription_manager):
97-
sx1 = NewHeadsSubscription(label="foo")
98-
sx2 = PendingTxSubscription(label="bar")
99-
await subscription_manager.subscribe([sx1, sx2])
97+
sub1 = NewHeadsSubscription(label="foo")
98+
sub2 = PendingTxSubscription(label="bar")
99+
await subscription_manager.subscribe([sub1, sub2])
100100

101101
await subscription_manager.unsubscribe_all()
102102
assert subscription_manager.subscriptions == []

0 commit comments

Comments
 (0)