From eb55117996d6f243d2d117d9974af41a0f8f6a14 Mon Sep 17 00:00:00 2001 From: Kieran Prasch Date: Sat, 23 Jun 2018 13:26:08 -0700 Subject: [PATCH] Prepares flattened contract registry for integration with circumflex. --- nucypher/blockchain/eth/interfaces.py | 22 +++++++++++-------- .../eth/interfaces/test_registry.py | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/nucypher/blockchain/eth/interfaces.py b/nucypher/blockchain/eth/interfaces.py index 4b06d6676..1dba75f6a 100644 --- a/nucypher/blockchain/eth/interfaces.py +++ b/nucypher/blockchain/eth/interfaces.py @@ -33,12 +33,16 @@ class EthereumContractRegistry: class UnknownContract(RegistryError): pass - class CorruptedRegistrar(RegistryError): + class IllegalRegistrar(RegistryError): """Raised when invalid data is encountered in the registry""" def __init__(self, registry_filepath: str=None): self.__registry_filepath = registry_filepath or self.__default_registry_path + @property + def registry_filepath(self): + return self.__registry_filepath + def __write(self, registry_data: list) -> None: """ Writes the registry data list as JSON to the registry file. If no @@ -50,7 +54,7 @@ class EthereumContractRegistry: registry_file.write(json.dumps(registry_data)) registry_file.truncate() - def __read(self) -> list: + def read(self) -> list: """ Reads the registry file and parses the JSON and returns a list. If the file is empty or the JSON is corrupt, it will return an empty @@ -69,7 +73,7 @@ class EthereumContractRegistry: registry_data = list() # Existing, but empty registry except FileNotFoundError: - raise self.RegistryError("No registar at filepath: {}".format(self.__registry_filepath)) + raise self.RegistryError("No registy at filepath: {}".format(self.__registry_filepath)) return registry_data @@ -82,7 +86,7 @@ class EthereumContractRegistry: need to use this. """ contract_data = [contract_name, contract_address, contract_abi] - registry_data = self.__read() + registry_data = self.read() registry_data.append(contract_data) self.__write(registry_data) @@ -95,17 +99,17 @@ class EthereumContractRegistry: raise ValueError("Pass contract_name or contract_address, not both.") contracts = list() - registry_data = self.__read() + registry_data = self.read() for name, addr, abi in registry_data: - if (contract_name or contract_address) == name: + if contract_name == name or contract_address == addr: contracts.append((name, addr, abi)) if not contracts: raise self.UnknownContract if contract_address and len(contracts) > 1: m = "Multiple records returned for address {}" - raise self.CorruptedRegistrar(m.format(contract_address)) + raise self.IllegalRegistrar(m.format(contract_address)) return contracts if contract_name else contracts[0] @@ -385,8 +389,8 @@ class DeployerCircumflex(ControlCircumflex): # contract = contract_factory(address=address) self._registry.enroll(contract_name=contract_name, - contract_address=contract.address, - contract_abi=contract_factory.abi) + contract_address=contract.address, + contract_abi=contract_factory.abi) return contract, txhash diff --git a/tests/blockchain/eth/interfaces/test_registry.py b/tests/blockchain/eth/interfaces/test_registry.py index f015c680b..53e253f84 100644 --- a/tests/blockchain/eth/interfaces/test_registry.py +++ b/tests/blockchain/eth/interfaces/test_registry.py @@ -3,7 +3,7 @@ import pytest from nucypher.blockchain.eth.interfaces import EthereumContractRegistry -def test_registrar_object(tempfile_path): +def test_contract_registrary(tempfile_path): with pytest.raises(EthereumContractRegistry.RegistryError): bad_registrar = EthereumContractRegistry(registry_filepath='/fake/file/path/registry.json')