trezorlib: support receiving piggybacked ACKs#6752
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughChannel adds a Sequence Diagram(s)sequenceDiagram
autonumber
participant Host as Host
participant Channel as Channel\n(thp.Channel)
participant Device as Device/Transport
Host->>Channel: _read()
Channel->>Channel: if _next_message present?
alt buffered message present
Channel-->>Host: return buffered message (clears _next_message)
else no buffered message
Channel->>Device: read_from_wire()
Device-->>Channel: message (ctrl byte with ACK bit + payload)
Channel->>Channel: _read_ack(message) → compute ack_bit_ok
alt standalone ACK (data is empty)
Channel-->>Host: return ACK
else piggybacked ACK and ack_bit_ok true
Channel->>Channel: store message in _next_message
Channel-->>Host: continue (next _read will return buffered message)
else ACK invalid or carries unexpected data
Channel->>Channel: log warning & skip
Channel->>Device: read_from_wire() (retry)
end
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
| model | device_test | click_test | persistence_test |
|---|---|---|---|
| T2T1 | test(all) main(all) ![]() |
test(all) main(all) ![]() |
test(all) main(all) ![]() |
| T3B1 | test(all) main(all) ![]() |
test(all) main(all) ![]() |
test(all) main(all) ![]() |
| T3T1 | test(all) main(all) ![]() |
test(all) main(all) ![]() |
test(all) main(all) ![]() |
| T3W1 | test(all) main(all) ![]() |
test(all) main(all) ![]() |
test(all) main(all) ![]() |
Latest CI run: 24426627820
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
python/src/trezorlib/thp/channel.py (1)
470-470: Consider replacing assertion with explicit error handling.The
assert self._next_message is Nonewill raiseAssertionErrorif a previous piggybacked message wasn't consumed. While this shouldn't happen under normal operation, an explicit check with a descriptive error would be more robust and informative in production (where assertions may be disabled with-O).♻️ Proposed defensive check
elif self.is_ack_piggybacking_allowed and ack_bit_ok: - assert self._next_message is None + if self._next_message is not None: + LOG.error("Buffered message not consumed before new piggybacked ACK") + raise ProtocolError("Unexpected piggybacked ACK: previous message not consumed") # process ACK bit, return message in next _read self._next_message = message🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@python/src/trezorlib/thp/channel.py` at line 470, Replace the bare assertion on self._next_message with an explicit runtime check that raises a clear exception (e.g., RuntimeError or ValueError) when a previous piggybacked message is still present; locate the assertion `assert self._next_message is None` in the Channel handling code and change it to an if-statement that raises an error with a descriptive message like "Unexpected leftover piggybacked message: _next_message not consumed" (optionally include the value of self._next_message) so the failure is informative and not skipped when Python is run with optimizations.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@python/src/trezorlib/thp/channel.py`:
- Around line 466-480: The ACK handling can wrongly buffer malformed ACKs that
carry data: inside the ACK processing block (where message.is_ack(), ack_bit_ok,
is_ack_piggybacking_allowed and self._next_message are used) explicitly detect
ACKs that have non-empty data (message.is_ack() and len(message.data) != 0) and
treat them as invalid—log an error/warning and continue instead of entering the
piggybacking branch; ensure you do not assign to self._next_message for these
malformed ACKs and preserve existing checks for ack_bit_ok when deciding to
continue or return.
---
Nitpick comments:
In `@python/src/trezorlib/thp/channel.py`:
- Line 470: Replace the bare assertion on self._next_message with an explicit
runtime check that raises a clear exception (e.g., RuntimeError or ValueError)
when a previous piggybacked message is still present; locate the assertion
`assert self._next_message is None` in the Channel handling code and change it
to an if-statement that raises an error with a descriptive message like
"Unexpected leftover piggybacked message: _next_message not consumed"
(optionally include the value of self._next_message) so the failure is
informative and not skipped when Python is run with optimizations.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2ceb8610-55a1-49e3-9cf2-bc9fe8cff26b
📒 Files selected for processing (2)
python/src/trezorlib/thp/channel.pypython/src/trezorlib/thp/control_byte.py
|
Removed |
[no changelog]
aa12d7c to
d6f54f5
Compare




































Fixes #6500, related to #6315. Current THP implementation on device always sends standalone ACKs but #6676 will opportunistically omit them.