Skip to content

Commit f30b65d

Browse files
committed
fixup! feat(core): reimplement THP event loop restarts
[no changelog]
1 parent 584c8c1 commit f30b65d

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

core/src/trezor/wire/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ async def handle_session(iface: WireInterface) -> None:
105105
_THP_CHANNELS.append(ctx._channels)
106106
try:
107107
channel = await ctx.get_next_message()
108-
await received_message_handler.handle_received_message(channel)
108+
while await received_message_handler.handle_received_message(channel):
109+
pass
109110
finally:
110111
# Wait for all active workflows to finish.
111112
await workflow.join_all()

core/src/trezor/wire/thp/received_message_handler.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,23 @@
4545
_TREZOR_STATE_PAIRED_AUTOCONNECT = b"\x02"
4646

4747

48-
async def handle_received_message(channel: Channel) -> None:
49-
"""Handle a message received from the channel."""
48+
async def handle_received_message(channel: Channel) -> bool:
49+
"""
50+
Handle a message received from the channel.
51+
52+
Returns False if we can restart the event loop.
53+
"""
5054
try:
5155
state = channel.get_channel_state()
5256
if state is ChannelState.ENCRYPTED_TRANSPORT:
53-
return await _handle_state_ENCRYPTED_TRANSPORT(channel)
57+
await _handle_state_ENCRYPTED_TRANSPORT(channel)
58+
return False
5459
elif _is_channel_state_pairing(state):
55-
return await _handle_pairing(channel)
60+
await _handle_pairing(channel)
61+
return False
5662
elif state is ChannelState.TH1:
57-
# TODO: can we also restart event loop here?
58-
return await _handle_state_handshake(channel)
63+
await _handle_state_handshake(channel)
64+
return channel.get_channel_state() == ChannelState.TC1
5965
else:
6066
raise ThpError("Unimplemented channel state")
6167

@@ -71,6 +77,7 @@ async def handle_received_message(channel: Channel) -> None:
7177
await channel.ctx.write_error(
7278
channel.get_channel_id_int(), ThpErrorType.DEVICE_LOCKED
7379
)
80+
return False
7481

7582

7683
async def _handle_state_handshake(

tests/device_tests/thp/test_pairing.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ def _nfc_pairing(client: Client, protocol: ProtocolV2Channel) -> None:
285285
assert tag_trezor_msg.tag == computed_tag
286286

287287

288-
@pytest.mark.skip(reason="broken by #5546")
289288
def test_connection_confirmation_cancel(client: Client) -> None:
290289
protocol = prepare_protocol_for_pairing(client)
291290
_nfc_pairing(client, protocol)
@@ -330,7 +329,6 @@ def test_connection_confirmation_cancel(client: Client) -> None:
330329
protocol._read_message(ThpEndResponse)
331330

332331

333-
@pytest.mark.skip(reason="broken by #5546")
334332
def test_autoconnect_credential_request_cancel(client: Client) -> None:
335333
protocol = prepare_protocol_for_pairing(client)
336334
_nfc_pairing(client, protocol)
@@ -372,7 +370,6 @@ def test_autoconnect_credential_request_cancel(client: Client) -> None:
372370
assert failure.code == FailureType.ActionCancelled
373371

374372

375-
@pytest.mark.skip(reason="broken by #5546")
376373
def test_credential_phase(client: Client) -> None:
377374
protocol = prepare_protocol_for_pairing(client)
378375
_nfc_pairing(client, protocol)

tests/ui_tests/fixtures.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30171,7 +30171,10 @@
3017130171
"T3W1_cs_thp-test_handshake.py::test_allocate_channel": "535037bfe5f1459cfdf305915835d8bf2a9a427c3f60264a8cc3ca6f306a61b1",
3017230172
"T3W1_cs_thp-test_handshake.py::test_handshake": "599b7c45309b3e09f382610f3e2198c264fc35aefb4effe7816040e6ec29f91d",
3017330173
"T3W1_cs_thp-test_multiple_hosts.py::test_concurrent_handshakes": "535037bfe5f1459cfdf305915835d8bf2a9a427c3f60264a8cc3ca6f306a61b1",
30174+
"T3W1_cs_thp-test_pairing.py::test_autoconnect_credential_request_cancel": "457ab9e14416f52c1bb1ea257c7dd4bd43c781a71ba4400fb700cf376200cd27",
3017430175
"T3W1_cs_thp-test_pairing.py::test_channel_replacement": "2ec597b20ac7bbd981d5b446e41c8ee99530b887232b0e6e050eb543776eef78",
30176+
"T3W1_cs_thp-test_pairing.py::test_connection_confirmation_cancel": "c22a79b68dd1f57a5db58388cd0344cb5111dbc68afef20e3362853d8ea54f8a",
30177+
"T3W1_cs_thp-test_pairing.py::test_credential_phase": "10102c757f759459e147cf7723de2144d090e75b937b515c63b82a0d47cc2736",
3017530178
"T3W1_cs_thp-test_pairing.py::test_credential_request_in_encrypted_transport_phase": "447df4a910a08d586c42bded66f8d954d6f265856c4037a0bc51fd16f2613b68",
3017630179
"T3W1_cs_thp-test_pairing.py::test_pairing_cancel_1": "26682c257ae82f7a94438d5543f28d9f72a780dacc34676465e83de9dc719a5d",
3017730180
"T3W1_cs_thp-test_pairing.py::test_pairing_cancel_2": "98b20959693b19764fb61dc09c4cb971ba3ea9b5c8ea072dfe8ea752e5f5899e",
@@ -31626,7 +31629,10 @@
3162631629
"T3W1_de_thp-test_handshake.py::test_allocate_channel": "2bf2200d3f158d1cffae639cd632a3b651fe53af5942b65eb881cfce12817592",
3162731630
"T3W1_de_thp-test_handshake.py::test_handshake": "4089127fa2d033a6d9c4a09781f54ce1f92fc1684ce2ffdacc153586685e8392",
3162831631
"T3W1_de_thp-test_multiple_hosts.py::test_concurrent_handshakes": "2bf2200d3f158d1cffae639cd632a3b651fe53af5942b65eb881cfce12817592",
31632+
"T3W1_de_thp-test_pairing.py::test_autoconnect_credential_request_cancel": "356d3b7c5aed10f8b91d9e8e35d06edf483a25afa2ce9444b442f9810fa6fe8f",
3162931633
"T3W1_de_thp-test_pairing.py::test_channel_replacement": "f6d77878c5385ae01b727db3d1253041ec8b8d5594084a68bb64c4ef9065d5bf",
31634+
"T3W1_de_thp-test_pairing.py::test_connection_confirmation_cancel": "34b20c5e6fdd46511dce62e10f92ec5b08cd55ad06ba0acef6704ddd931000e4",
31635+
"T3W1_de_thp-test_pairing.py::test_credential_phase": "ba5a1c0ad53e4e6d45f6bb3daec1354da95814e2347e21ee22d459cb7b178238",
3163031636
"T3W1_de_thp-test_pairing.py::test_credential_request_in_encrypted_transport_phase": "f17b7adbd2e15cdbc7fc3c1b75626e7e58149940ed6ee21ab0eee453a03d958a",
3163131637
"T3W1_de_thp-test_pairing.py::test_pairing_cancel_1": "a3e81c59c99554a9065c35de00cc51f7a58c3c05b972c71424c94ee56e648b49",
3163231638
"T3W1_de_thp-test_pairing.py::test_pairing_cancel_2": "f2c3692d802ae2ae92085da1ebcb104de820fa3124ce7ce340bcf67116a1f842",
@@ -33081,7 +33087,10 @@
3308133087
"T3W1_en_thp-test_handshake.py::test_allocate_channel": "2b19d878184abddf53159d4acb504a1e86a7c2d5fd15de433495742ba7df9cc8",
3308233088
"T3W1_en_thp-test_handshake.py::test_handshake": "e8338fb6f6d8dabdb3ba79c521852b3db26cf2d65de359a133074e1858173aba",
3308333089
"T3W1_en_thp-test_multiple_hosts.py::test_concurrent_handshakes": "2b19d878184abddf53159d4acb504a1e86a7c2d5fd15de433495742ba7df9cc8",
33090+
"T3W1_en_thp-test_pairing.py::test_autoconnect_credential_request_cancel": "4d40957d3dca48a1e792042d1e5b7fe90200336ddd7df008e66027844eba1fab",
3308433091
"T3W1_en_thp-test_pairing.py::test_channel_replacement": "40cfae080c587feac48b4d4c781c0e4c15edf37fbf4468e53b6ac43b4fd53000",
33092+
"T3W1_en_thp-test_pairing.py::test_connection_confirmation_cancel": "a920958bb70e5be597f04a644eb529c77a7c0a6ac87fe77ce037d5299526e6da",
33093+
"T3W1_en_thp-test_pairing.py::test_credential_phase": "d23e359bcac554a1096b04229ab229c41df6648f1cd610802448b83b5b09c2e9",
3308533094
"T3W1_en_thp-test_pairing.py::test_credential_request_in_encrypted_transport_phase": "9ecedd8a32fa670d27f1c69bbd3cfd0f0a96a6e98b687342d9f5f14a68b28255",
3308633095
"T3W1_en_thp-test_pairing.py::test_pairing_cancel_1": "ee9dd4f800b7d1d55b03897e8c3a9ac12587b5c88b62e3e164dda2f82b2d2b89",
3308733096
"T3W1_en_thp-test_pairing.py::test_pairing_cancel_2": "311ee774cb007244ab02479513f34636d5b3525ded32c9c639ae402d9f4c6647",
@@ -34536,7 +34545,10 @@
3453634545
"T3W1_es_thp-test_handshake.py::test_allocate_channel": "991c2fedae415c4284948d276edc08daeea466ca5849934ed2784cc8884ee589",
3453734546
"T3W1_es_thp-test_handshake.py::test_handshake": "446c4b55e2b04cf4f12f20dd2a544a139cd1f04db2de732de69f0ab1595d34a9",
3453834547
"T3W1_es_thp-test_multiple_hosts.py::test_concurrent_handshakes": "991c2fedae415c4284948d276edc08daeea466ca5849934ed2784cc8884ee589",
34548+
"T3W1_es_thp-test_pairing.py::test_autoconnect_credential_request_cancel": "cb482a59c593c3c8803c35dc8021e1ae9518a680caeaea4af39813a56eb435fd",
3453934549
"T3W1_es_thp-test_pairing.py::test_channel_replacement": "55b7542cab8a983244e79de523287963e077e5237a4db04f4d41d418e03e1b68",
34550+
"T3W1_es_thp-test_pairing.py::test_connection_confirmation_cancel": "6f433c176315ce1654a4c675ccc4f20b83facaf78124d5004e91d3cd9b6cdd2b",
34551+
"T3W1_es_thp-test_pairing.py::test_credential_phase": "4f82c37913009db474c48928432aa22037c480d3851193a1e6543ca8e99f1c5b",
3454034552
"T3W1_es_thp-test_pairing.py::test_credential_request_in_encrypted_transport_phase": "b3e3bd3176b15dfbfe98794cd1047ec8012465b4e6ee16425c0f650faae03ff3",
3454134553
"T3W1_es_thp-test_pairing.py::test_pairing_cancel_1": "c2591097c84b15ac795d77afd9d7903ee8a75c4062c266aed2d21685b4a5df23",
3454234554
"T3W1_es_thp-test_pairing.py::test_pairing_cancel_2": "5958e821d7498782bffc4278bb531602fa86673389c4be22140e38cd0e263c74",
@@ -35991,7 +36003,10 @@
3599136003
"T3W1_fr_thp-test_handshake.py::test_allocate_channel": "250f727140e1737e1e25e4620b4557d886c40e53f8205987dacb8904050dc474",
3599236004
"T3W1_fr_thp-test_handshake.py::test_handshake": "4a1b056b6117bfb79336e6e174efff3006017d838bdcf4b16f46fd3ed4628d2e",
3599336005
"T3W1_fr_thp-test_multiple_hosts.py::test_concurrent_handshakes": "250f727140e1737e1e25e4620b4557d886c40e53f8205987dacb8904050dc474",
36006+
"T3W1_fr_thp-test_pairing.py::test_autoconnect_credential_request_cancel": "cd3c958a5b358779765d7d2485ddff48d22fd9a5340d6aca2e48f0e0e939b013",
3599436007
"T3W1_fr_thp-test_pairing.py::test_channel_replacement": "ed61fb08c9858fbb1148660d1228f7d7c497f7a89f9066486d5c7bb37f8ef873",
36008+
"T3W1_fr_thp-test_pairing.py::test_connection_confirmation_cancel": "7b1db2a82d9528fb3dda6ecfa6de511b2066df3eb157f898e33b5c49c9181767",
36009+
"T3W1_fr_thp-test_pairing.py::test_credential_phase": "397f983b9b2f45053592b77071d6974a748024c3211b4533563a43a0e2651dd8",
3599536010
"T3W1_fr_thp-test_pairing.py::test_credential_request_in_encrypted_transport_phase": "4f856ed456252093228bf49937960d22062647ae0a6be1d8b5e68f1565f12dde",
3599636011
"T3W1_fr_thp-test_pairing.py::test_pairing_cancel_1": "117f093c6764135753dbc7b1df400eb4d9e56996520ea4ca5864d6e5d19ff381",
3599736012
"T3W1_fr_thp-test_pairing.py::test_pairing_cancel_2": "5cdac2aa6934b76fd04e8a3d9e3407411229f29915549babbfc38bc8195691dc",
@@ -37446,7 +37461,10 @@
3744637461
"T3W1_pt_thp-test_handshake.py::test_allocate_channel": "637658c0c6bbf3267f67895c12aeea91280f0471133d7865e4b0d19bdd1da874",
3744737462
"T3W1_pt_thp-test_handshake.py::test_handshake": "c3514f5e9f61efb7bb753a10d9af0f9b932f1b741948bfb1ff576ee557c1ceb1",
3744837463
"T3W1_pt_thp-test_multiple_hosts.py::test_concurrent_handshakes": "637658c0c6bbf3267f67895c12aeea91280f0471133d7865e4b0d19bdd1da874",
37464+
"T3W1_pt_thp-test_pairing.py::test_autoconnect_credential_request_cancel": "475b9204de9b71fa3637f14787bea5b1612ad64789c3663936dc15498b362a56",
3744937465
"T3W1_pt_thp-test_pairing.py::test_channel_replacement": "52dce5f754788e21d59789e6a0c5a3a72b4b1810e44d59d1f368896fe93229a1",
37466+
"T3W1_pt_thp-test_pairing.py::test_connection_confirmation_cancel": "f5ba7f22b3b5dd1038588d7433d5597318724811a2d208fa206434cbfed45c22",
37467+
"T3W1_pt_thp-test_pairing.py::test_credential_phase": "d5459f1f73004a82b32cfb9128f5395c6da1a11a312928a10e52765a7e43f2c5",
3745037468
"T3W1_pt_thp-test_pairing.py::test_credential_request_in_encrypted_transport_phase": "1d096a92e581f540d4a434ca92bf2b52f2e8d59f450d92a7744de89999a78e36",
3745137469
"T3W1_pt_thp-test_pairing.py::test_pairing_cancel_1": "1354f82a9d51bba8655fc8fd8695c0d0327f5babd7b7e0329bdeaa86e0e44f1c",
3745237470
"T3W1_pt_thp-test_pairing.py::test_pairing_cancel_2": "47ba37925299d73ea3bc6e9269d53fb3307635f480ce9a299c896828daa28191",

0 commit comments

Comments
 (0)