parent
9869b42ce5
commit
50fe43646a
@ -1,40 +1,30 @@
|
|||||||
from storage.cache_thp import ChannelCache, SessionThpCache
|
from storage.cache_thp import ChannelCache, SessionThpCache
|
||||||
from trezor import log, loop
|
from trezor import log
|
||||||
|
|
||||||
from . import thp_session as THP
|
from . import thp_session as THP
|
||||||
|
|
||||||
|
|
||||||
def handle_received_ACK(
|
def is_ack_valid(cache: SessionThpCache | ChannelCache, sync_bit: int) -> bool:
|
||||||
cache: SessionThpCache | ChannelCache,
|
if not _is_ack_expected(cache):
|
||||||
sync_bit: int,
|
return False
|
||||||
waiting_for_ack_timeout: loop.spawn | None = None,
|
|
||||||
) -> None:
|
|
||||||
|
|
||||||
if _ack_is_not_expected(cache):
|
if not _has_ack_correct_sync_bit(cache, sync_bit):
|
||||||
_conditionally_log_debug("Received unexpected ACK message")
|
return False
|
||||||
return
|
|
||||||
if _ack_has_incorrect_sync_bit(cache, sync_bit):
|
|
||||||
_conditionally_log_debug("Received ACK message with wrong sync bit")
|
|
||||||
return
|
|
||||||
|
|
||||||
# ACK is expected and it has correct sync bit
|
return True
|
||||||
_conditionally_log_debug("Received ACK message with correct sync bit")
|
|
||||||
if waiting_for_ack_timeout is not None:
|
|
||||||
waiting_for_ack_timeout.close()
|
|
||||||
_conditionally_log_debug('Closed "waiting for ack" task')
|
|
||||||
THP.sync_set_can_send_message(cache, True)
|
|
||||||
|
|
||||||
|
|
||||||
def _ack_is_not_expected(cache: SessionThpCache | ChannelCache) -> bool:
|
def _is_ack_expected(cache: SessionThpCache | ChannelCache) -> bool:
|
||||||
return THP.sync_can_send_message(cache)
|
is_expected: bool = not THP.sync_can_send_message(cache)
|
||||||
|
if not is_expected and __debug__:
|
||||||
|
log.debug(__name__, "Received unexpected ACK message")
|
||||||
|
return is_expected
|
||||||
|
|
||||||
|
|
||||||
def _ack_has_incorrect_sync_bit(
|
def _has_ack_correct_sync_bit(
|
||||||
cache: SessionThpCache | ChannelCache, sync_bit: int
|
cache: SessionThpCache | ChannelCache, sync_bit: int
|
||||||
) -> bool:
|
) -> bool:
|
||||||
return THP.sync_get_send_bit(cache) != sync_bit
|
is_correct: bool = THP.sync_get_send_bit(cache) == sync_bit
|
||||||
|
if __debug__ and not is_correct:
|
||||||
|
log.debug(__name__, "Received ACK message with wrong sync bit")
|
||||||
def _conditionally_log_debug(message):
|
return is_correct
|
||||||
if __debug__:
|
|
||||||
log.debug(__name__, message)
|
|
||||||
|
Loading…
Reference in new issue