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 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6bc085c
test(core): update tests to reflect cache refactor
M1nd3r Nov 15, 2024
6a4a2c4
style: fix article
M1nd3r Nov 15, 2024
8fe6fe5
refactor(core): abstract cache and context
M1nd3r Nov 15, 2024
f51e6dc
chore(core): update core to reflect cache and context refactor
M1nd3r Nov 15, 2024
7823b61
fixup! refactor(core): abstract cache and context [no changelog]
M1nd3r Nov 18, 2024
a18ae02
fixup! chore(core): update core to reflect cache and context refactor…
M1nd3r Nov 18, 2024
1661868
fixup! fixup! chore(core): update core to reflect cache and context r…
M1nd3r Nov 18, 2024
5e4dea4
fixup! test(core): update tests to reflect cache refactor [no changelog]
M1nd3r Nov 19, 2024
87bb39e
test(core): add setUpClass and tearDownClass to core unit tests
M1nd3r Nov 19, 2024
efaddeb
test(core): replace __init__ in unit tests with setUpClass and tearDo…
M1nd3r Nov 19, 2024
7645546
fixup! test(core): add setUpClass and tearDownClass to core unit test…
M1nd3r Nov 19, 2024
f180c32
fixup! refactor(core): abstract cache and context [no changelog]
M1nd3r Nov 20, 2024
91d477c
fixup! refactor(core): abstract cache and context [no changelog]
M1nd3r Nov 20, 2024
efda21a
fixup! chore(core): update core to reflect cache and context refactor…
M1nd3r Nov 20, 2024
8c6ed70
fixup! fixup! refactor(core): abstract cache and context [no changelog]
M1nd3r Nov 20, 2024
f431265
docs(core): add and modify docs to context and cache
M1nd3r Nov 20, 2024
cebd147
fixup! refactor(core): abstract cache and context [no changelog]
M1nd3r Nov 20, 2024
0a878b7
fixup! fixup! refactor(core): abstract cache and context [no changelog]
M1nd3r Nov 22, 2024
3585691
fixup! fixup! fixup! refactor(core): abstract cache and context [no c…
M1nd3r Nov 22, 2024
7f11fea
fixup! docs(core): add and modify docs to context and cache [no chang…
M1nd3r Nov 22, 2024
d303dce
fixup! fixup! fixup! fixup! refactor(core): abstract cache and contex…
M1nd3r Nov 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions core/tests/test_apps.bitcoin.approver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from common import H_, await_result, unittest # isort:skip

import storage.cache
import storage.cache_codec
from trezor import wire
from trezor.crypto import bip32
from trezor.enums import InputScriptType, OutputScriptType
Expand All @@ -11,6 +11,8 @@
TxInput,
TxOutput,
)
from trezor.wire import context
from trezor.wire.codec.codec_context import CodecContext

from apps.bitcoin.authorization import FEE_RATE_DECIMALS, CoinJoinAuthorization
from apps.bitcoin.sign_tx.approvers import CoinJoinApprover
Expand All @@ -20,6 +22,11 @@


class TestApprover(unittest.TestCase):

def __init__(self):
context.CURRENT_CONTEXT = CodecContext(None, bytearray(64))
super().__init__()

def setUp(self):
self.coin = coins.by_name("Bitcoin")
self.fee_rate_percent = 0.3
Expand Down Expand Up @@ -47,7 +54,7 @@ def setUp(self):
coin_name=self.coin.coin_name,
script_type=InputScriptType.SPENDTAPROOT,
)
storage.cache.start_session()
storage.cache_codec.start_session()

def make_coinjoin_request(self, inputs):
return CoinJoinRequest(
Expand Down
10 changes: 8 additions & 2 deletions core/tests/test_apps.bitcoin.authorization.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from common import H_, unittest # isort:skip

import storage.cache
import storage.cache_codec
from trezor.enums import InputScriptType
from trezor.messages import AuthorizeCoinJoin, GetOwnershipProof, SignTx
from trezor.wire import context
from trezor.wire.codec.codec_context import CodecContext

from apps.bitcoin.authorization import CoinJoinAuthorization
from apps.common import coins
Expand All @@ -12,6 +14,10 @@

class TestAuthorization(unittest.TestCase):

def __init__(self):
context.CURRENT_CONTEXT = CodecContext(None, bytearray(64))
super().__init__()

coin = coins.by_name("Bitcoin")

def setUp(self):
Expand All @@ -26,7 +32,7 @@ def setUp(self):
)

self.authorization = CoinJoinAuthorization(self.msg_auth)
storage.cache.start_session()
storage.cache_codec.start_session()

def test_ownership_proof_account_depth_mismatch(self):
# Account depth mismatch.
Expand Down
27 changes: 22 additions & 5 deletions core/tests/test_apps.bitcoin.keychain.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
from common import * # isort:skip

from storage import cache
from storage import cache_common
from trezor import wire
from trezor.crypto import bip39
from trezor.wire import context

from apps.bitcoin.keychain import _get_coin_by_name, _get_keychain_for_coin
from trezor.wire.codec.codec_context import CodecContext
from storage import cache_codec


class TestBitcoinKeychain(unittest.TestCase):

def __init__(self):
context.CURRENT_CONTEXT = CodecContext(None, bytearray(64))
mmilata marked this conversation as resolved.
Show resolved Hide resolved
super().__init__()

def setUp(self):
cache.start_session()
cache_codec.start_session()
seed = bip39.seed(" ".join(["all"] * 12), "")
cache.set(cache.APP_COMMON_SEED, seed)
cache_codec.get_active_session().set(cache_common.APP_COMMON_SEED, seed)

def test_bitcoin(self):
coin = _get_coin_by_name("Bitcoin")
Expand Down Expand Up @@ -88,10 +96,19 @@ def test_unknown(self):

@unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin")
class TestAltcoinKeychains(unittest.TestCase):

def __init__(self):
# Context is needed to test decorators and handleInitialize
# It allows access to codec cache from different parts of the code
from trezor.wire import context

context.CURRENT_CONTEXT = CodecContext(None, bytearray(64))
super().__init__()

def setUp(self):
cache.start_session()
cache_codec.start_session()
seed = bip39.seed(" ".join(["all"] * 12), "")
cache.set(cache.APP_COMMON_SEED, seed)
cache_codec.get_active_session().set(cache_common.APP_COMMON_SEED, seed)

def test_bcash(self):
coin = _get_coin_by_name("Bcash")
Expand Down
16 changes: 12 additions & 4 deletions core/tests/test_apps.common.keychain.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
from common import * # isort:skip

from mock_storage import mock_storage
from storage import cache
from storage import cache, cache_common
from trezor import wire
from trezor.crypto import bip39
from trezor.enums import SafetyCheckLevel
from trezor.wire import context

from apps.common import safety_checks
from apps.common.keychain import Keychain, LRUCache, get_keychain, with_slip44_keychain
from apps.common.paths import PATTERN_SEP5, PathSchema
from trezor.wire.codec.codec_context import CodecContext
from storage import cache_codec


class TestKeychain(unittest.TestCase):

def __init__(self):
context.CURRENT_CONTEXT = CodecContext(None, bytearray(64))
super().__init__()

def setUp(self):
cache.start_session()
cache_codec.start_session()

def tearDown(self):
cache.clear_all()
Expand Down Expand Up @@ -71,7 +79,7 @@ def test_no_schemas(self):

def test_get_keychain(self):
seed = bip39.seed(" ".join(["all"] * 12), "")
cache.set(cache.APP_COMMON_SEED, seed)
context.cache_set(cache_common.APP_COMMON_SEED, seed)

schema = PathSchema.parse("m/44'/1'", 0)
keychain = await_result(get_keychain("secp256k1", [schema]))
Expand All @@ -85,7 +93,7 @@ def test_get_keychain(self):

def test_with_slip44(self):
seed = bip39.seed(" ".join(["all"] * 12), "")
cache.set(cache.APP_COMMON_SEED, seed)
context.cache_set(cache_common.APP_COMMON_SEED, seed)

slip44_id = 42
valid_path = [H_(44), H_(slip44_id), H_(0)]
Expand Down
15 changes: 11 additions & 4 deletions core/tests/test_apps.ethereum.keychain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import unittest

from storage import cache
from trezor import utils, wire
from storage import cache_common
from trezor import wire
from trezor.crypto import bip39
from trezor.wire import context

from apps.common.keychain import get_keychain
from apps.common.paths import HARDENED
from trezor.wire.codec.codec_context import CodecContext
from storage import cache_codec

if not utils.BITCOIN_ONLY:
from ethereum_common import encode_network, make_network
Expand Down Expand Up @@ -71,10 +74,14 @@ def _check_keychain(self, keychain, slip44_id):
addr,
)

def __init__(self):
context.CURRENT_CONTEXT = CodecContext(None, bytearray(64))
super().__init__()

def setUp(self):
cache.start_session()
cache_codec.start_session()
seed = bip39.seed(" ".join(["all"] * 12), "")
cache.set(cache.APP_COMMON_SEED, seed)
cache_codec.get_active_session().set(cache_common.APP_COMMON_SEED, seed)

def from_address_n(self, address_n):
slip44 = _slip44_from_address_n(address_n)
Expand Down
Loading