Skip to content

Commit

Permalink
Merge pull request #167 from LedgerHQ/sgl/timeout
Browse files Browse the repository at this point in the history
backend: Add optional tick_timeout argument to exchange functions
  • Loading branch information
sgliner-ledger authored Mar 7, 2024
2 parents e712ae2 + 6db50d5 commit 0ec6e7c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ doc = [
"docutils==0.16",
]
speculos = [
"speculos>=0.5.0",
"speculos>=0.5.1",
"mnemonic",
]
ledgercomm = [
Expand Down
12 changes: 9 additions & 3 deletions src/ragger/backend/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ def receive(self) -> RAPDU:
"""
raise NotImplementedError

def exchange(self, cla: int, ins: int, p1: int = 0, p2: int = 0, data: bytes = b"") -> RAPDU:
def exchange(self,
cla: int,
ins: int,
p1: int = 0,
p2: int = 0,
data: bytes = b"",
tick_timeout: int = 5 * 60 * 10) -> RAPDU:
"""
Formats and sends an APDU to the backend, then receives its response.
Expand All @@ -181,10 +187,10 @@ def exchange(self, cla: int, ins: int, p1: int = 0, p2: int = 0, data: bytes = b
:return: The APDU response
:rtype: RAPDU
"""
return self.exchange_raw(pack_APDU(cla, ins, p1, p2, data))
return self.exchange_raw(pack_APDU(cla, ins, p1, p2, data), tick_timeout=tick_timeout)

@abstractmethod
def exchange_raw(self, data: bytes = b"") -> RAPDU:
def exchange_raw(self, data: bytes = b"", tick_timeout: int = 5 * 60 * 10) -> RAPDU:
"""
Sends the given APDU to the backend, then receives its response.
Expand Down
4 changes: 2 additions & 2 deletions src/ragger/backend/speculos.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ def receive(self) -> RAPDU:
return result

@raise_policy_enforcer
def exchange_raw(self, data: bytes = b"") -> RAPDU:
def exchange_raw(self, data: bytes = b"", tick_timeout: int = 5 * 60 * 10) -> RAPDU:
self.apdu_logger.info("=> %s", data.hex())
return RAPDU(0x9000, self._client._apdu_exchange(data))
return RAPDU(0x9000, self._client._apdu_exchange(data, tick_timeout=tick_timeout))

@raise_policy_enforcer
def _get_last_async_response(self, response) -> RAPDU:
Expand Down
2 changes: 1 addition & 1 deletion src/ragger/backend/stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def send_raw(self, data: bytes = b""):
def receive(self) -> RAPDU:
return RAPDU(0x9000, b"")

def exchange_raw(self, data: bytes = b"") -> RAPDU:
def exchange_raw(self, data: bytes = b"", tick_timeout: int = 5 * 60 * 10) -> RAPDU:
return RAPDU(0x9000, b"")

@contextmanager
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/backend/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def left_click(self):
def exchange_async_raw(self, *args, **kwargs):
return self.mock.exchange_async_raw(*args, **kwargs)

def exchange_raw(self, *args, **kwargs):
return self.mock.exchange_raw(*args, **kwargs)
def exchange_raw(self, data: bytes, tick_timeout: int):
return self.mock.exchange_raw(data, tick_timeout)

def receive(self):
return self.mock.receive()
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_exchange(self):
self.assertFalse(self.backend.mock.send_raw.called)
result = self.backend.exchange(cla, ins, p1, p2)
self.assertTrue(self.backend.mock.exchange_raw.called)
self.assertEqual(self.backend.mock.exchange_raw.call_args, ((expected, ), ))
self.assertEqual(self.backend.mock.exchange_raw.call_args, ((expected, 5 * 60 * 10), ))
self.assertEqual(result, self.backend.mock.exchange_raw())

def test_exchange_async(self):
Expand Down

0 comments on commit 0ec6e7c

Please sign in to comment.