Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstraction of cache and context #4357

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open

Conversation

M1nd3r
Copy link
Contributor

@M1nd3r M1nd3r commented Nov 15, 2024

Abstraction of cache and context so that the current codec_v1 implementation can be switched for THP version.

Copy link

github-actions bot commented Nov 15, 2024

core UI changes device test click test persistence test
T2T1 Model T test(screens) main(screens) test(screens) main(screens) test(screens) main(screens)
T3B1 Safe 3 test(screens) main(screens) test(screens) main(screens) test(screens) main(screens)
T3T1 Safe 5 test(screens) main(screens) test(screens) main(screens) test(screens) main(screens)
All main(screens)

Copy link

github-actions bot commented Nov 15, 2024

legacy UI changes device test(screens) main(screens)

@M1nd3r M1nd3r force-pushed the M1nd3r/cache-and-context branch 2 times, most recently from dbaba59 to 9400f19 Compare November 15, 2024 20:54
@M1nd3r M1nd3r force-pushed the M1nd3r/cache-and-context branch 2 times, most recently from 545adf2 to 48f58b0 Compare November 15, 2024 20:58
@M1nd3r M1nd3r changed the title [draft] Codec cache and context abstraction [draft] Abstraction of context and codec cache Nov 15, 2024
@M1nd3r M1nd3r force-pushed the M1nd3r/cache-and-context branch 2 times, most recently from addd607 to 3411ee1 Compare November 15, 2024 22:12
@M1nd3r M1nd3r changed the title [draft] Abstraction of context and codec cache Abstraction of cache and context Nov 18, 2024
@@ -557,6 +557,7 @@ if FROZEN:
))

SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/wire/*.py'))
SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/wire/codec/*.py'))
Copy link
Contributor Author

@M1nd3r M1nd3r Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The codec specific files are separated so that they can be easily ommited for THP prod builds. When the THP implementation is added, it could look like this:

if THP:
   SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/wire/thp/*.py'))
if not THP or PYOPT == '0':
   SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/wire/codec/*.py'))

Copy link
Contributor Author

@M1nd3r M1nd3r Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: The condition or PYOPT == '0' is in the snippet because the codec will be used on DebugLink even with THP builds.

@M1nd3r M1nd3r marked this pull request as ready for review November 18, 2024 08:45
@M1nd3r M1nd3r requested a review from mmilata November 18, 2024 08:45
Copy link
Member

@mmilata mmilata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will continue tomorrow

core/tests/test_apps.bitcoin.keychain.py Show resolved Hide resolved
core/src/apps/common/authorization.py Show resolved Hide resolved
core/src/apps/common/backup.py Show resolved Hide resolved
@M1nd3r M1nd3r self-assigned this Nov 19, 2024
Copy link
Member

@mmilata mmilata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not found any major issue.

@matejcik can you please go through 8fe6fe5 real quick if you can spot anything that breaks the workflow restart/module unloading?

core/src/storage/cache_common.py Outdated Show resolved Hide resolved
@@ -156,210 +12,33 @@ def __init__(self) -> None:
# bytearrays, then later call `clear()` on all the existing objects, which resets them
# to zero length. This is producing some trash - `b[:]` allocates a slice.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment should go where the code below it went.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I thought about it previously. The problem with this "comment" is that it explains the process of allocation and the logic behind it.

  • Allocation is two-step: first allocate, then clear.
  • Note that using b[:] allocates a slice.

The allocation process actually happens just below where the commit is:


_SESSIONLESS_CACHE = ...  # step 1
_PROTOCOL_CACHE.initialize()    # step 1 and step 2
_SESSIONLESS_CACHE.clear() # step 2

The problem I see is that it is more abstract now - the "real" allocation (and use of slices) happens partly inside a _PROTOCOL_CACHE and SessionlessCache - that are in different files now.

I see a few solutions:

  1. keep it as it is now - it works as a general documentation
  2. remove the comment, move the implementation information into docs
  3. have the comment in each of the implementations (cache_codec, cache_thp)
  4. some mix of 2. and 3., divide the allocation notes and use relevant parts where necessary

@mmilata WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with 3., some duplication won't hurt. Feel free to extend the docs if it makes sense to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here: 0a878b7

core/src/storage/cache_common.py Outdated Show resolved Hide resolved
core/src/storage/cache_common.py Outdated Show resolved Hide resolved
core/src/storage/cache_common.py Outdated Show resolved Hide resolved
core/src/trezor/wire/protocol_common.py Outdated Show resolved Hide resolved
core/src/trezor/wire/codec/codec_context.py Outdated Show resolved Hide resolved
core/src/trezor/wire/message_handler.py Outdated Show resolved Hide resolved
Filter = Callable[[int, Handler], Handler]

# If set to False protobuf messages marked with "experimental_message" option are rejected.
EXPERIMENTAL_ENABLED = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: shouldn't this also be somewhere under storage/cache to avoid being wiped by a restart?

core/tests/test_apps.bitcoin.keychain.py Show resolved Hide resolved
@@ -58,9 +58,6 @@ async def read(

async def write(self, msg: protobuf.MessageType) -> None: ...

def write_force(self, msg: protobuf.MessageType) -> Awaitable[None]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Removed write_force as it is not used by any code in main or in the PR. @mmilata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🔎 Needs review
Development

Successfully merging this pull request may close these issues.

2 participants