From ae9f4b40fa58cd0a4ab8c720c531c6bbb1691665 Mon Sep 17 00:00:00 2001 From: Kieran Prasch Date: Sun, 29 Oct 2023 19:42:02 +0100 Subject: [PATCH] Use a constant for the temporary domain --- examples/local_simple_taco.py | 1 - nucypher/acumen/perception.py | 3 +-- nucypher/cli/actions/select.py | 2 +- tests/acceptance/conftest.py | 10 +++---- tests/conftest.py | 2 +- tests/constants.py | 10 +++++-- tests/fixtures.py | 17 +++--------- tests/integration/conftest.py | 8 +++--- tests/integration/learning/test_domains.py | 6 ++--- tests/unit/conftest.py | 5 ++-- tests/unit/registry/test_registry_basics.py | 10 +++---- tests/unit/registry/test_registry_soures.py | 17 ++++++------ tests/utils/registry.py | 29 +++++---------------- 13 files changed, 50 insertions(+), 70 deletions(-) diff --git a/examples/local_simple_taco.py b/examples/local_simple_taco.py index 77e3ec30e..91a89854b 100644 --- a/examples/local_simple_taco.py +++ b/examples/local_simple_taco.py @@ -1,5 +1,4 @@ from nucypher.blockchain.eth import domains -from nucypher.blockchain.eth import domains from nucypher.blockchain.eth.signers import InMemorySigner from nucypher.characters.chaotic import NiceGuyEddie as _Enrico from nucypher.characters.chaotic import ThisBobAlwaysDecrypts diff --git a/nucypher/acumen/perception.py b/nucypher/acumen/perception.py index f4308fcbb..62ae6528f 100644 --- a/nucypher/acumen/perception.py +++ b/nucypher/acumen/perception.py @@ -9,10 +9,9 @@ from nucypher_core import FleetStateChecksum, NodeMetadata from typing import Any, Dict, Iterable, List, NamedTuple, Optional, Union from nucypher import characters +from nucypher.blockchain.eth.domains import TACoDomain from nucypher.utilities.logging import Logger from .nicknames import Nickname -from nucypher.blockchain.eth import domains -from ..blockchain.eth.domains import TACoDomain class ArchivedFleetState(NamedTuple): diff --git a/nucypher/cli/actions/select.py b/nucypher/cli/actions/select.py index a31a0ed76..f7426a3af 100644 --- a/nucypher/cli/actions/select.py +++ b/nucypher/cli/actions/select.py @@ -64,7 +64,7 @@ def select_client_account( blockchain = BlockchainInterfaceFactory.get_interface(endpoint=polygon_endpoint) if signer_uri and not signer: - testnet = domain != domains.MAINNET.name + testnet = str(domain) != str(domains.MAINNET) signer = Signer.from_signer_uri(signer_uri, testnet=testnet) enumerated_accounts = dict(enumerate(signer.accounts)) diff --git a/tests/acceptance/conftest.py b/tests/acceptance/conftest.py index bd41385ef..895e65e97 100644 --- a/tests/acceptance/conftest.py +++ b/tests/acceptance/conftest.py @@ -21,7 +21,7 @@ from tests.constants import ( INSECURE_DEVELOPMENT_PASSWORD, MIN_OPERATOR_SECONDS, TEST_ETH_PROVIDER_URI, - TESTERCHAIN_CHAIN_ID, + TESTERCHAIN_CHAIN_ID, TEMPORARY_DOMAIN, ) from tests.utils.blockchain import TesterBlockchain from tests.utils.registry import ApeRegistrySource @@ -273,10 +273,10 @@ def deployed_contracts( @pytest.fixture(scope="module", autouse=True) -def test_registry(deployed_contracts, module_mocker, temporary_domain): +def test_registry(deployed_contracts, module_mocker): with tests.utils.registry.mock_registry_sources(mocker=module_mocker): RegistrySourceManager._FALLBACK_CHAIN = (ApeRegistrySource,) - source = ApeRegistrySource(domain=temporary_domain) + source = ApeRegistrySource(domain=TEMPORARY_DOMAIN) registry = ContractRegistry(source=source) yield registry @@ -403,7 +403,7 @@ def multichain_ursulas(ursulas, multichain_ids, mock_rpc_condition): @pytest.fixture(scope="module", autouse=True) -def mock_condition_blockchains(module_mocker, temporary_domain): +def mock_condition_blockchains(module_mocker): """adds testerchain's chain ID to permitted conditional chains""" module_mocker.patch.dict( "nucypher.policy.conditions.evm._CONDITION_CHAINS", @@ -412,7 +412,7 @@ def mock_condition_blockchains(module_mocker, temporary_domain): module_mocker.patch( "nucypher.blockchain.eth.domains.get_domain", - return_value=temporary_domain + return_value=TEMPORARY_DOMAIN ) diff --git a/tests/conftest.py b/tests/conftest.py index 1de13a493..d7f5d5154 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -137,7 +137,7 @@ def disable_check_grant_requirements(session_mocker): @pytest.fixture(scope="module", autouse=True) -def mock_condition_blockchains(module_mocker, temporary_domain): +def mock_condition_blockchains(module_mocker): """adds testerchain's chain ID to permitted conditional chains""" module_mocker.patch.dict( "nucypher.policy.conditions.evm._CONDITION_CHAINS", diff --git a/tests/constants.py b/tests/constants.py index aee733162..9ffb4653b 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -7,11 +7,11 @@ from random import SystemRandom from hexbytes import HexBytes from web3 import Web3 -from nucypher.blockchain.eth.domains import ChainInfo +from nucypher.blockchain.eth.domains import ChainInfo, TACoDomain from nucypher.blockchain.eth.token import NU from nucypher.config.constants import ( NUCYPHER_ENVVAR_KEYSTORE_PASSWORD, - NUCYPHER_ENVVAR_OPERATOR_ETH_PASSWORD, + NUCYPHER_ENVVAR_OPERATOR_ETH_PASSWORD, TEMPORARY_DOMAIN_NAME, ) # @@ -74,6 +74,12 @@ TESTERCHAIN_CHAIN_ID = 131277322940537 TESTERCHAIN_CHAIN_INFO = ChainInfo(131277322940537, "eth-tester") +TEMPORARY_DOMAIN = TACoDomain( + name=TEMPORARY_DOMAIN_NAME, + eth_chain=TESTERCHAIN_CHAIN_INFO, + polygon_chain=TESTERCHAIN_CHAIN_INFO, +) + # # Insecure Secrets diff --git a/tests/fixtures.py b/tests/fixtures.py index 71811dcf0..666441ba0 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -18,7 +18,6 @@ from web3 import Web3 import tests from nucypher.blockchain.eth.actors import Operator -from nucypher.blockchain.eth.domains import TACoDomain from nucypher.blockchain.eth.interfaces import BlockchainInterfaceFactory from nucypher.blockchain.eth.signers.software import KeystoreSigner from nucypher.blockchain.eth.trackers.dkg import EventScannerTask @@ -50,8 +49,7 @@ from tests.constants import ( MOCK_CUSTOM_INSTALLATION_PATH_2, MOCK_ETH_PROVIDER_URI, TEST_ETH_PROVIDER_URI, - TESTERCHAIN_CHAIN_ID, TESTERCHAIN_CHAIN_INFO, -) + TESTERCHAIN_CHAIN_ID, TEMPORARY_DOMAIN, ) from tests.mock.interfaces import MockBlockchain from tests.mock.performance_mocks import ( mock_cert_generation, @@ -306,15 +304,6 @@ def lonely_ursula_maker(ursula_test_config, testerchain): # Blockchain # -@pytest.fixture(scope='session') -def temporary_domain(): - _domain = TACoDomain( - name=TEMPORARY_DOMAIN_NAME, - eth_chain=TESTERCHAIN_CHAIN_INFO, - polygon_chain=TESTERCHAIN_CHAIN_INFO, - ) - return _domain - @pytest.fixture(scope="module") def mock_registry_sources(module_mocker): @@ -547,9 +536,9 @@ def worker_configuration_file_location(custom_filepath) -> Path: @pytest.fixture(autouse=True) -def mock_teacher_nodes(mocker, temporary_domain): +def mock_teacher_nodes(mocker): mock_nodes = tuple(u.rest_url() for u in MOCK_KNOWN_URSULAS_CACHE.values())[0:2] - mocker.patch.dict(TEACHER_NODES, {temporary_domain: mock_nodes}, clear=True) + mocker.patch.dict(TEACHER_NODES, {TEMPORARY_DOMAIN: mock_nodes}, clear=True) @pytest.fixture(autouse=True) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 04a8b59bc..5839c6d90 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -31,7 +31,7 @@ from tests.constants import ( KEYFILE_NAME_TEMPLATE, MOCK_KEYSTORE_PATH, NUMBER_OF_MOCK_KEYSTORE_ACCOUNTS, - TESTERCHAIN_CHAIN_ID, + TESTERCHAIN_CHAIN_ID, TEMPORARY_DOMAIN, ) from tests.mock.interfaces import MockBlockchain from tests.mock.io import MockStdinWrapper @@ -129,9 +129,9 @@ def mock_interface(module_mocker): @pytest.fixture(scope='module') -def test_registry(module_mocker, temporary_domain): +def test_registry(module_mocker): with mock_registry_sources(mocker=module_mocker): - mock_source = MockRegistrySource(domain=temporary_domain) + mock_source = MockRegistrySource(domain=TEMPORARY_DOMAIN) registry = ContractRegistry(source=mock_source) yield registry @@ -276,7 +276,7 @@ def monkeypatch_get_staking_provider_from_operator(monkeymodule): @pytest.fixture(scope="module", autouse=True) -def mock_condition_blockchains(module_mocker, temporary_domain): +def mock_condition_blockchains(module_mocker): """adds testerchain's chain ID to permitted conditional chains""" module_mocker.patch.dict( "nucypher.policy.conditions.evm._CONDITION_CHAINS", diff --git a/tests/integration/learning/test_domains.py b/tests/integration/learning/test_domains.py index ba7299062..072dba5ec 100644 --- a/tests/integration/learning/test_domains.py +++ b/tests/integration/learning/test_domains.py @@ -8,7 +8,7 @@ from nucypher.blockchain.eth.registry import ContractRegistry from nucypher.characters.lawful import Ursula from nucypher.config.storages import LocalFileBasedNodeStorage from nucypher.network.nodes import TEACHER_NODES -from tests.constants import TESTERCHAIN_CHAIN_INFO +from tests.constants import TESTERCHAIN_CHAIN_INFO, TEMPORARY_DOMAIN from tests.utils.registry import MockRegistrySource from tests.utils.ursula import make_ursulas @@ -33,9 +33,9 @@ def domain_2(): @pytest.fixture(scope="module") -def test_registry(module_mocker, domain_1, domain_2, temporary_domain): +def test_registry(module_mocker, domain_1, domain_2): with tests.utils.registry.mock_registry_sources( - mocker=module_mocker, _domains=[domain_1, domain_2, temporary_domain] + mocker=module_mocker, _domains=[domain_1, domain_2, TEMPORARY_DOMAIN] ): # doesn't really matter what domain is used here registry = ContractRegistry(MockRegistrySource(domain=domain_1)) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index a470551ea..3d0a528b7 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -9,6 +9,7 @@ from nucypher.config.constants import TEMPORARY_DOMAIN_NAME from nucypher.crypto.ferveo import dkg from nucypher.crypto.powers import TransactingPower from nucypher.network.nodes import Teacher +from tests.constants import TEMPORARY_DOMAIN from tests.mock.interfaces import MockBlockchain, MockEthereumClient from tests.utils.registry import MockRegistrySource, mock_registry_sources @@ -19,9 +20,9 @@ def pytest_addhooks(pluginmanager): @pytest.fixture(scope='module') -def test_registry(module_mocker, temporary_domain): +def test_registry(module_mocker): with mock_registry_sources(mocker=module_mocker): - source = MockRegistrySource(domain=temporary_domain) + source = MockRegistrySource(domain=TEMPORARY_DOMAIN) yield ContractRegistry(source=source) diff --git a/tests/unit/registry/test_registry_basics.py b/tests/unit/registry/test_registry_basics.py index 1d20867a8..02f88b13b 100644 --- a/tests/unit/registry/test_registry_basics.py +++ b/tests/unit/registry/test_registry_basics.py @@ -2,7 +2,7 @@ import pytest from nucypher.blockchain.eth.registry import ContractRegistry from nucypher.config.constants import TEMPORARY_DOMAIN_NAME -from tests.constants import TESTERCHAIN_CHAIN_ID +from tests.constants import TESTERCHAIN_CHAIN_ID, TEMPORARY_DOMAIN from tests.utils.registry import MockRegistrySource @@ -34,8 +34,8 @@ def data(record): @pytest.fixture(scope="function") -def source(data, temporary_domain): - source = MockRegistrySource(domain=temporary_domain) +def source(data): + source = MockRegistrySource(domain=TEMPORARY_DOMAIN) source.data = data return source @@ -77,9 +77,9 @@ def test_local_registry_unknown_contract_name_search(registry): ) -def test_local_contract_registry_ambiguous_search_terms(data, name, record, address, temporary_domain): +def test_local_contract_registry_ambiguous_search_terms(data, name, record, address): data[TESTERCHAIN_CHAIN_ID]["fakeContract"] = record[name] - source = MockRegistrySource(domain=temporary_domain) + source = MockRegistrySource(domain=TEMPORARY_DOMAIN) source.data = data registry = ContractRegistry(source=source) with pytest.raises(ContractRegistry.AmbiguousSearchTerms): diff --git a/tests/unit/registry/test_registry_soures.py b/tests/unit/registry/test_registry_soures.py index e49855ae3..e066e75ca 100644 --- a/tests/unit/registry/test_registry_soures.py +++ b/tests/unit/registry/test_registry_soures.py @@ -12,6 +12,7 @@ from nucypher.blockchain.eth.registry import ( RegistrySourceManager, ) from nucypher.config.constants import TEMPORARY_DOMAIN_NAME +from tests.constants import TEMPORARY_DOMAIN @pytest.fixture(scope="function") @@ -46,8 +47,8 @@ def test_registry_filepath(tmpdir, registry_data): @pytest.mark.usefixtures("mock_200_response") -def test_github_registry_source(registry_data, temporary_domain): - source = GithubRegistrySource(domain=temporary_domain) +def test_github_registry_source(registry_data): + source = GithubRegistrySource(domain=TEMPORARY_DOMAIN) assert source.domain.name == TEMPORARY_DOMAIN_NAME assert str(source.domain) == TEMPORARY_DOMAIN_NAME assert bytes(source.domain) == TEMPORARY_DOMAIN_NAME.encode("utf-8") @@ -57,9 +58,9 @@ def test_github_registry_source(registry_data, temporary_domain): assert data == source.data -def test_local_registry_source(registry_data, test_registry_filepath, temporary_domain): +def test_local_registry_source(registry_data, test_registry_filepath): source = LocalRegistrySource( - filepath=test_registry_filepath, domain=temporary_domain + filepath=test_registry_filepath, domain=TEMPORARY_DOMAIN ) assert source.domain.name == TEMPORARY_DOMAIN_NAME assert str(source.domain) == TEMPORARY_DOMAIN_NAME @@ -70,13 +71,13 @@ def test_local_registry_source(registry_data, test_registry_filepath, temporary_ assert data == source.data -def test_embedded_registry_source(registry_data, test_registry_filepath, mocker, temporary_domain): +def test_embedded_registry_source(registry_data, test_registry_filepath, mocker): mocker.patch.object( EmbeddedRegistrySource, "get_publication_endpoint", return_value=test_registry_filepath, ) - source = EmbeddedRegistrySource(domain=temporary_domain) + source = EmbeddedRegistrySource(domain=TEMPORARY_DOMAIN) assert source.domain.name == TEMPORARY_DOMAIN_NAME assert str(source.domain) == TEMPORARY_DOMAIN_NAME assert bytes(source.domain) == TEMPORARY_DOMAIN_NAME.encode("utf-8") @@ -87,7 +88,7 @@ def test_embedded_registry_source(registry_data, test_registry_filepath, mocker, def test_registry_source_manager_fallback( - registry_data, test_registry_filepath, mocker, temporary_domain + registry_data, test_registry_filepath, mocker ): github_source_get = mocker.patch.object( GithubRegistrySource, "get", side_effect=RegistrySource.Unavailable @@ -102,7 +103,7 @@ def test_registry_source_manager_fallback( GithubRegistrySource, EmbeddedRegistrySource, ) - source_manager = RegistrySourceManager(domain=temporary_domain) + source_manager = RegistrySourceManager(domain=TEMPORARY_DOMAIN) assert source_manager.domain.name == TEMPORARY_DOMAIN_NAME assert str(source_manager.domain) == TEMPORARY_DOMAIN_NAME assert bytes(source_manager.domain) == TEMPORARY_DOMAIN_NAME.encode("utf-8") diff --git a/tests/utils/registry.py b/tests/utils/registry.py index 87c754df8..91fc4b228 100644 --- a/tests/utils/registry.py +++ b/tests/utils/registry.py @@ -12,37 +12,22 @@ from nucypher.blockchain.eth.registry import ( RegistrySourceManager, ) from nucypher.config.constants import TEMPORARY_DOMAIN_NAME -from tests.constants import TESTERCHAIN_CHAIN_INFO +from tests.constants import TEMPORARY_DOMAIN @contextmanager def mock_registry_sources(mocker, _domains: List[TACoDomain] = None): if not _domains: - _domains = [ - TACoDomain( - name=TEMPORARY_DOMAIN_NAME, - eth_chain=TESTERCHAIN_CHAIN_INFO, - polygon_chain=TESTERCHAIN_CHAIN_INFO, - ) - ] + _domains = [TEMPORARY_DOMAIN] - supported_domains = dict() - for domain in _domains: - test_domain = TACoDomain( - name=str(domain), - eth_chain=TESTERCHAIN_CHAIN_INFO, - polygon_chain=TESTERCHAIN_CHAIN_INFO, - ) - supported_domains[str(domain)] = test_domain - - - _supported_domains = mocker.patch('nucypher.blockchain.eth.domains.SUPPORTED_DOMAINS', new_callable=dict) - _supported_domains.update(supported_domains) + _supported_domains = mocker.patch.dict( + 'nucypher.blockchain.eth.domains.SUPPORTED_DOMAINS', + {domain.name: domain for domain in _domains}, + ) mocker.patch.object(MockRegistrySource, "ALLOWED_DOMAINS", list(map(str, _domains))) mocker.patch.object(RegistrySourceManager, "_FALLBACK_CHAIN", (MockRegistrySource,)) - - yield # run the test + yield class MockRegistrySource(RegistrySource):