From 44a6696f24ea1ccd960fc49d8025f9271bda2ee4 Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 24 May 2024 12:19:19 +0200 Subject: [PATCH] build(core): improve model detection instead of MODEL_IS_T2B1, we now catch INTERNAL_MODEL == Txyz this is still not perfect because it can't detect 'in'/'not in', which sucks but what can we do --- .../site_tools/micropython/__init__.py | 46 ++++++++++++------- core/src/apps/base.py | 4 +- core/src/trezor/utils.py | 4 -- core/tests/test_apps.bitcoin.signtx_decred.py | 4 +- core/tests/test_apps.common.coins.py | 2 +- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/core/site_scons/site_tools/micropython/__init__.py b/core/site_scons/site_tools/micropython/__init__.py index d4f422ff61..2c074287f5 100644 --- a/core/site_scons/site_tools/micropython/__init__.py +++ b/core/site_scons/site_tools/micropython/__init__.py @@ -42,29 +42,41 @@ def generate(env): # replace "utils.BITCOIN_ONLY" with literal constant (True/False) # so the compiler can optimize out the things we don't want btc_only = env["bitcoin_only"] == "1" - is_t2b1 = env["TREZOR_MODEL"] == "R" backlight = env["backlight"] optiga = env["optiga"] layout_tt = env["ui_layout"] == "UI_LAYOUT_TT" layout_tr = env["ui_layout"] == "UI_LAYOUT_TR" thp = env["thp"] interim = f"{target[:-4]}.i" # replace .mpy with .i - sed_scripts = " ".join( - [ - rf"-e 's/utils\.MODEL_IS_T2B1/{is_t2b1}/g'", - rf"-e 's/utils\.BITCOIN_ONLY/{btc_only}/g'", - rf"-e 's/utils\.USE_BACKLIGHT/{backlight}/g'", - rf"-e 's/utils\.USE_OPTIGA/{optiga}/g'", - rf"-e 's/utils\.UI_LAYOUT == \"TT\"/{layout_tt}/g'", - rf"-e 's/utils\.UI_LAYOUT == \"TR\"/{layout_tr}/g'", - rf"-e 's/utils\.USE_THP/{thp}/g'", - r"-e 's/if TYPE_CHECKING/if False/'", - r"-e 's/import typing/# \0/'", - r"-e '/from typing import (/,/^\s*)/ {s/^/# /; }'", - r"-e 's/from typing import/# \0/'", - ] - ) - return f"$SED {sed_scripts} {source} > {interim} && $MPY_CROSS -o {target} -s {source_name} {interim}" + sed_scripts = [ + rf"-e 's/utils\.BITCOIN_ONLY/{btc_only}/g'", + rf"-e 's/utils\.USE_BACKLIGHT/{backlight}/g'", + rf"-e 's/utils\.USE_OPTIGA/{optiga}/g'", + rf"-e 's/utils\.UI_LAYOUT == \"TT\"/{layout_tt}/g'", + rf"-e 's/utils\.UI_LAYOUT == \"TR\"/{layout_tr}/g'", + rf"-e 's/utils\.USE_THP/{thp}/g'", + r"-e 's/if TYPE_CHECKING/if False/'", + r"-e 's/import typing/# \0/'", + r"-e '/from typing import (/,/^\s*)/ {s/^/# /; }'", + r"-e 's/from typing import/# \0/'", + ] + + MODEL_SYMS = { + "T": "T2T1", + "R": "T2B1", + "T3T1": "T3T1", + } + + for model_sym, internal_model in MODEL_SYMS.items(): + model_matches = env["TREZOR_MODEL"] == model_sym + sed_scripts.extend( + ( + rf"-e 's/utils\.INTERNAL_MODEL == \"{internal_model}\"/{model_matches}/g'", + rf"-e 's/utils\.INTERNAL_MODEL != \"{internal_model}\"/{not model_matches}/g'", + ) + ) + + return f"$SED {' '.join(sed_scripts)} {source} > {interim} && $MPY_CROSS -o {target} -s {source_name} {interim}" env["BUILDERS"]["FrozenModule"] = SCons.Builder.Builder( generator=generate_frozen_module, diff --git a/core/src/apps/base.py b/core/src/apps/base.py index 428d186b3c..4717eca9d3 100644 --- a/core/src/apps/base.py +++ b/core/src/apps/base.py @@ -126,8 +126,8 @@ def get_features() -> Features: Capability.Translations, ] - # We do not support some currencies on T2B1 - if not utils.MODEL_IS_T2B1: + # We don't support some currencies on later models (see #2793) + if utils.INTERNAL_MODEL == "T2T1": f.capabilities.extend( [ Capability.NEM, diff --git a/core/src/trezor/utils.py b/core/src/trezor/utils.py index 995af3198e..e580b7be12 100644 --- a/core/src/trezor/utils.py +++ b/core/src/trezor/utils.py @@ -30,10 +30,6 @@ from trezorutils import ( # noqa: F401 ) from typing import TYPE_CHECKING -# Will get replaced by "True" / "False" in the build process -# However, needs to stay as an exported symbol for the unit tests -MODEL_IS_T2B1: bool = INTERNAL_MODEL == "T2B1" - DISABLE_ANIMATION = 0 if __debug__: diff --git a/core/tests/test_apps.bitcoin.signtx_decred.py b/core/tests/test_apps.bitcoin.signtx_decred.py index 001141b015..a8f4fc7e0d 100644 --- a/core/tests/test_apps.bitcoin.signtx_decred.py +++ b/core/tests/test_apps.bitcoin.signtx_decred.py @@ -1,6 +1,6 @@ from common import * # isort:skip -if not utils.MODEL_IS_T2B1: +if utils.INTERNAL_MODEL not in ("T2B1", "T3T1"): from trezor.crypto import bip39 from trezor.enums import AmountUnit, OutputScriptType from trezor.enums.RequestType import TXFINISHED, TXINPUT, TXMETA, TXOUTPUT @@ -405,5 +405,5 @@ class TestSignTxDecred(unittest.TestCase): if __name__ == "__main__": - if not utils.MODEL_IS_T2B1: + if utils.INTERNAL_MODEL not in ("T2B1", "T3T1"): unittest.main() diff --git a/core/tests/test_apps.common.coins.py b/core/tests/test_apps.common.coins.py index 3c92b39c51..ba3008e824 100644 --- a/core/tests/test_apps.common.coins.py +++ b/core/tests/test_apps.common.coins.py @@ -22,7 +22,7 @@ class TestCoins(unittest.TestCase): ("ZEC", "Zcash", 7352), ("TAZ", "Zcash Testnet", 7461), ] - if not utils.MODEL_IS_T2B1: + if utils.INTERNAL_MODEL not in ("T2B1", "T3T1"): ref.extend( [ ("NMC", "Namecoin", 52),