From 2814dc51b1989f8beb7890d998934cd0ec846b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=BA=C3=B1ez?= Date: Sun, 19 Jan 2020 02:50:18 +0100 Subject: [PATCH] Define and use a default network. For the moment the 'unknown' placeholder --- nucypher/blockchain/eth/registry.py | 9 ++++++--- nucypher/characters/base.py | 2 +- nucypher/cli/actions.py | 6 ++++-- nucypher/cli/commands/deploy.py | 2 +- nucypher/cli/commands/status.py | 7 +++++-- nucypher/config/node.py | 9 +++++++-- nucypher/utilities/sandbox/constants.py | 3 ++- 7 files changed, 26 insertions(+), 12 deletions(-) diff --git a/nucypher/blockchain/eth/registry.py b/nucypher/blockchain/eth/registry.py index 0898803e9..17e939428 100644 --- a/nucypher/blockchain/eth/registry.py +++ b/nucypher/blockchain/eth/registry.py @@ -39,6 +39,9 @@ class NetworksInventory: # TODO: Rename & relocate. See also #1564 FRANCES = 'frances' CASSANDRA = 'cassandra' + UNKNOWN = 'unknown' # TODO: Is there a better way to signal an unknown network? + DEFAULT = UNKNOWN # TODO: This assumes we DON'T have a default. Is that OK? - #1496 + __to_ethereum_chain_id = { # TODO: what about chain id when testing? MAINNET: 1, # Ethereum Mainnet MIRANDA: 5, # Goerli @@ -49,7 +52,7 @@ class NetworksInventory: # TODO: Rename & relocate. See also #1564 networks = tuple(__to_ethereum_chain_id.keys()) @classmethod - def get_ethereum_chain_id(cls, network): + def get_ethereum_chain_id(cls, network): # TODO: Use this (where?) to make sure we're in the right chain try: return cls.__to_ethereum_chain_id[network] except KeyError: @@ -179,7 +182,7 @@ class RegistrySourceManager: def get_primary_sources(cls): return [source for source in cls.__FALLBACK_CHAIN if source.is_primary] - def fetch_latest_publication(self, registry_class, network: str = 'goerli'): # TODO: see #1496 + def fetch_latest_publication(self, registry_class, network: str = NetworksInventory.DEFAULT): # TODO: see #1496 """ Get the latest contract registry data available from a registry source chain. """ @@ -278,7 +281,7 @@ class BaseContractRegistry(ABC): raise NotImplementedError @classmethod - def from_latest_publication(cls, *args, source_manager=None, network: str = 'goerli', **kwargs) -> 'BaseContractRegistry': + def from_latest_publication(cls, *args, source_manager=None, network: str = NetworksInventory.DEFAULT, **kwargs) -> 'BaseContractRegistry': # FIXME: entry point to fix #1496, #1564 """ Get the latest contract registry available from a registry source chain. """ diff --git a/nucypher/characters/base.py b/nucypher/characters/base.py index c5051aa4b..04953b477 100644 --- a/nucypher/characters/base.py +++ b/nucypher/characters/base.py @@ -191,7 +191,7 @@ class Character(Learner): # self.provider_uri = provider_uri if not self.federated_only: - self.registry = registry or InMemoryContractRegistry.from_latest_publication() + self.registry = registry or InMemoryContractRegistry.from_latest_publication(domains[0]) # FIXME: entry point to fix #1496, #1564 else: self.registry = NO_BLOCKCHAIN_CONNECTION.bool_value(False) diff --git a/nucypher/cli/actions.py b/nucypher/cli/actions.py index 1b301697b..237690262 100644 --- a/nucypher/cli/actions.py +++ b/nucypher/cli/actions.py @@ -386,7 +386,8 @@ def select_client_account(emitter, prompt: str = None, default: int = 0, registry=None, - show_balances: bool = True + show_balances: bool = True, + network: str = None ) -> str: """ Note: Setting show_balances to True, causes an eager contract and blockchain connection. @@ -404,7 +405,7 @@ def select_client_account(emitter, token_agent = None if show_balances: if not registry: - registry = InMemoryContractRegistry.from_latest_publication() + registry = InMemoryContractRegistry.from_latest_publication(network) token_agent = NucypherTokenAgent(registry=registry) # Real wallet accounts @@ -463,6 +464,7 @@ def handle_client_account_for_staking(emitter, client_account = select_client_account(prompt="Select staking account", emitter=emitter, registry=stakeholder.registry, + network=stakeholder.domains[0], # FIXME: 1496 provider_uri=stakeholder.wallet.blockchain.provider_uri) staking_address = client_account diff --git a/nucypher/cli/commands/deploy.py b/nucypher/cli/commands/deploy.py index a3688d121..d83d8c714 100644 --- a/nucypher/cli/commands/deploy.py +++ b/nucypher/cli/commands/deploy.py @@ -210,7 +210,7 @@ def download_registry(general_config, config_root, registry_outfile, network, fo prompt = f"Fetch and download latest contract registry from {github_source}?" click.confirm(prompt, abort=True) try: - registry = InMemoryContractRegistry.from_latest_publication(source_manager=source_manager) + registry = InMemoryContractRegistry.from_latest_publication(source_manager=source_manager, network=network) except RegistrySourceManager.NoSourcesAvailable: emitter.message("Registry not available.", color="red") raise click.Abort diff --git a/nucypher/cli/commands/status.py b/nucypher/cli/commands/status.py index ab5473038..11367c480 100644 --- a/nucypher/cli/commands/status.py +++ b/nucypher/cli/commands/status.py @@ -28,6 +28,7 @@ from nucypher.cli.options import ( group_options, option_geth, option_light, + option_network, option_poa, option_provider_uri, option_registry_filepath, @@ -40,12 +41,13 @@ class RegistryOptions: __option_name__ = 'registry_options' - def __init__(self, provider_uri, geth, poa, registry_filepath, light): + def __init__(self, provider_uri, geth, poa, registry_filepath, light, network): self.provider_uri = provider_uri self.geth = geth self.poa = poa self.registry_filepath = registry_filepath self.light = light + self.network = network def get_registry(self, emitter, debug): try: @@ -74,7 +76,7 @@ class RegistryOptions: if self.registry_filepath: registry = LocalContractRegistry(filepath=self.registry_filepath) else: - registry = InMemoryContractRegistry.from_latest_publication() + registry = InMemoryContractRegistry.from_latest_publication(network=self.network) return registry @@ -86,6 +88,7 @@ group_registry_options = group_options( poa=option_poa, light=option_light, registry_filepath=option_registry_filepath, + network=option_network, ) diff --git a/nucypher/config/node.py b/nucypher/config/node.py index 8aeca50f3..812c23fbb 100644 --- a/nucypher/config/node.py +++ b/nucypher/config/node.py @@ -32,7 +32,12 @@ from twisted.logger import Logger from umbral.signing import Signature from nucypher.blockchain.eth.interfaces import BlockchainInterfaceFactory -from nucypher.blockchain.eth.registry import BaseContractRegistry, InMemoryContractRegistry, LocalContractRegistry +from nucypher.blockchain.eth.registry import ( + BaseContractRegistry, + InMemoryContractRegistry, + LocalContractRegistry, + NetworksInventory +) from nucypher.config.base import BaseConfiguration from nucypher.config.keyring import NucypherKeyring from nucypher.config.storages import NodeStorage, ForgetfulNodeStorage, LocalFileBasedNodeStorage @@ -50,7 +55,7 @@ class CharacterConfiguration(BaseConfiguration): CHARACTER_CLASS = NotImplemented DEFAULT_CONTROLLER_PORT = NotImplemented - DEFAULT_DOMAIN = 'goerli' # FIXME: entry point to fix #1496, #1564 + DEFAULT_DOMAIN = NetworksInventory.DEFAULT # FIXME: entry point to fix #1496, #1564 DEFAULT_NETWORK_MIDDLEWARE = RestMiddleware TEMP_CONFIGURATION_DIR_PREFIX = 'tmp-nucypher' diff --git a/nucypher/utilities/sandbox/constants.py b/nucypher/utilities/sandbox/constants.py index df8165ad6..e33e261d3 100644 --- a/nucypher/utilities/sandbox/constants.py +++ b/nucypher/utilities/sandbox/constants.py @@ -28,6 +28,7 @@ from string import digits, ascii_uppercase from web3 import Web3 +from nucypher.blockchain.eth.registry import NetworksInventory from nucypher.blockchain.eth.token import NU from nucypher.config.characters import UrsulaConfiguration from nucypher.config.constants import BASE_DIR @@ -135,7 +136,7 @@ MOCK_CUSTOM_INSTALLATION_PATH_2 = '/tmp/nucypher-tmp-test-custom-2-{}'.format(ti MOCK_REGISTRY_FILEPATH = os.path.join(BASE_TEMP_DIR, f'{BASE_TEMP_PREFIX}mock-registry-{datetime.now().strftime(DATETIME_FORMAT)}.json') -TEMPORARY_DOMAIN = ':TEMPORARY_DOMAIN:' # for use with `--dev` node runtimes +TEMPORARY_DOMAIN = NetworksInventory.UNKNOWN # for use with `--dev` node runtimes # FIXME? GETH_DEV_URI = f'ipc://{BASE_TEMP_DIR}/geth.ipc' # Standard IPC path for `geth --dev`