|
|
|
@ -5,27 +5,6 @@ from typing import TYPE_CHECKING
|
|
|
|
|
from storage.cache_common import SESSIONLESS_FLAG, SessionlessCache
|
|
|
|
|
from trezor import utils
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from typing import Callable, ParamSpec, TypeVar
|
|
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
P = ParamSpec("P")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_thp_is_not_used(f: Callable[P, T]) -> Callable[P, T]:
|
|
|
|
|
"""A type-safe decorator to raise an exception when the function is called with THP enabled.
|
|
|
|
|
|
|
|
|
|
This decorator should be removed after the caches for Codec_v1 and THP are properly refactored and separated.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def inner(*args: P.args, **kwargs: P.kwargs) -> T:
|
|
|
|
|
if utils.USE_THP:
|
|
|
|
|
raise Exception("Cannot call this function with the new THP enabled")
|
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# XXX
|
|
|
|
|
# Allocation notes:
|
|
|
|
|
# Instantiation of a DataCache subclass should make as little garbage as possible, so
|
|
|
|
@ -51,8 +30,6 @@ _SESSIONLESS_CACHE.clear()
|
|
|
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
|
# Common functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clear_all() -> None:
|
|
|
|
|
global autolock_last_touch
|
|
|
|
@ -71,19 +48,26 @@ def get_int_all_sessions(key: int) -> builtins.set[int]:
|
|
|
|
|
return _PROTOCOL_CACHE.get_int_all_sessions(key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Sessionless functions
|
|
|
|
|
def get_sessionless_cache() -> SessionlessCache:
|
|
|
|
|
return _SESSIONLESS_CACHE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Codec_v1 specific functions
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from typing import Callable, ParamSpec, TypeVar
|
|
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
P = ParamSpec("P")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_thp_is_not_used(f: Callable[P, T]) -> Callable[P, T]:
|
|
|
|
|
"""A type-safe decorator to raise an exception when the function is called with THP enabled.
|
|
|
|
|
|
|
|
|
|
@check_thp_is_not_used
|
|
|
|
|
def start_session(received_session_id: bytes | None = None) -> bytes:
|
|
|
|
|
return cache_codec.start_session(received_session_id)
|
|
|
|
|
This decorator should be removed after the caches for Codec_v1 and THP are properly refactored and separated.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def inner(*args: P.args, **kwargs: P.kwargs) -> T:
|
|
|
|
|
if utils.USE_THP:
|
|
|
|
|
raise Exception("Cannot call this function with the new THP enabled")
|
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
@check_thp_is_not_used
|
|
|
|
|
def end_current_session() -> None:
|
|
|
|
|
cache_codec.end_current_session()
|
|
|
|
|
return inner
|
|
|
|
|