Skip to content

Commit 28e8e92

Browse files
committed
Context-based approach to subscription handlers args management:
- Introduce a ``handler_context`` to expose the w3, subscription, and result, as well as any user-defined context args. - Add ``handler_context`` to subscriptions that allow for defining custom arguments in the subscription handler implementation, available via the context. - Instead of using transient flags, use the handler context directly to create handler tests and assert that the handlers were called and the ``passed`` flag was set to ``True``. - ``event`` for logs subscriptions should be handled by the handler context. - Changes from comments on #3554; update docs. - newsfragment for #3554
1 parent e9ecefa commit 28e8e92

File tree

9 files changed

+297
-171
lines changed

9 files changed

+297
-171
lines changed

docs/internals.rst

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,12 @@ the user is done with it.
445445
.. code-block:: python
446446
447447
>>> async def new_heads_handler(
448-
... async_w3: AsyncWeb3,
449-
... sx: EthSubscription,
450-
... response: BlockData,
448+
... context: NewHeadsSubscriptionContext,
451449
... ) -> None:
452-
... print(f"New block header: {response}\n")
453-
... if response["number"] > 1234567:
454-
... await sx.unsubscribe()
450+
... result = context.result
451+
... print(f"New block header: {result}\n")
452+
... if result["number"] > 1234567:
453+
... await context.subscription.unsubscribe()
455454
456455
>>> async def ws_subscription_example():
457456
... async with AsyncWeb3(WebSocketProvider(f"ws://127.0.0.1:8546")) as w3:
@@ -483,30 +482,27 @@ connections.
483482
>>> from web3.utils.subscriptions import (
484483
... EthSubscription,
485484
... NewHeadsSubscription,
485+
... NewHeadsSubscriptionContext,
486486
... PendingTxSubscription,
487+
... PendingTxSubscriptionContext,
487488
... LogsSubscription,
489+
... LogsSubscriptionContext,
488490
... )
489491
490492
>>> async def new_heads_handler(
491-
... async_w3: AsyncWeb3,
492-
... sx: EthSubscription,
493-
... response: BlockData,
493+
... context: NewHeadsSubscriptionContext,
494494
... ) -> None:
495-
... print(f"New block header: {response}\n")
496-
... if response["number"] > 1234567:
497-
... await sx.unsubscribe()
495+
... print(f"New block header: {context.result}\n")
496+
... if context.result["number"] > 1234567:
497+
... await context.subscription.unsubscribe()
498498
499499
>>> async def pending_txs_handler(
500-
... async_w3: AsyncWeb3,
501-
... sx: EthSubscription,
502-
... response: TxData,
500+
... context: PendingTxSubscriptionContext,
503501
... ) -> None:
504502
... ...
505503
506504
>>> async def log_handler(
507-
... async_w3: AsyncWeb3,
508-
... sx: EthSubscription,
509-
... response: LogData,
505+
... context: LogsSubscriptionContext,
510506
... ) -> None:
511507
... ...
512508

newsfragments/3554.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a subscription manager to persistent connection providers, with support for handler methods for ``eth_subscribe`` subscriptions.

tests/core/subscriptions/test_subscriptions.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import pytest
2-
from unittest.mock import (
3-
Mock,
4-
)
52

63
from web3.utils.subscriptions import (
74
LogsSubscription,
@@ -64,9 +61,8 @@ def test_logs_subscription_properties(handler):
6461
"0x0000000000000000000000000000000000000000000000000000000000000001",
6562
"0x0000000000000000000000000000000000000000000000000000000000000002",
6663
]
67-
event = Mock()
6864
logs_subscription = LogsSubscription(
69-
address=address, topics=topics, handler=handler, event=event, label="logs label"
65+
address=address, topics=topics, handler=handler, label="logs label"
7066
)
7167
assert logs_subscription._handler is handler
7268
assert logs_subscription.label == "logs label"
@@ -76,7 +72,6 @@ def test_logs_subscription_properties(handler):
7672
)
7773
assert logs_subscription.address == address
7874
assert logs_subscription.topics == topics
79-
assert logs_subscription.event is event
8075

8176

8277
def test_syncing_subscription_properties(handler):

web3/_utils/module_testing/module_testing_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def assert_contains_log(
106106
emitter_contract_address: ChecksumAddress,
107107
txn_hash_with_log: HexStr,
108108
) -> None:
109+
assert len(result) > 0
109110
log_entry = result[0]
110111
assert log_entry["blockNumber"] == block_with_txn_with_log["number"]
111112
assert log_entry["blockHash"] == block_with_txn_with_log["hash"]

0 commit comments

Comments
 (0)