Prepares flattened contract registry for integration with circumflex.

pull/328/head
Kieran Prasch 2018-06-23 13:26:08 -07:00
parent cf5bef9e0d
commit eb55117996
2 changed files with 14 additions and 10 deletions

View File

@ -33,12 +33,16 @@ class EthereumContractRegistry:
class UnknownContract(RegistryError): class UnknownContract(RegistryError):
pass pass
class CorruptedRegistrar(RegistryError): class IllegalRegistrar(RegistryError):
"""Raised when invalid data is encountered in the registry""" """Raised when invalid data is encountered in the registry"""
def __init__(self, registry_filepath: str=None): def __init__(self, registry_filepath: str=None):
self.__registry_filepath = registry_filepath or self.__default_registry_path 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: def __write(self, registry_data: list) -> None:
""" """
Writes the registry data list as JSON to the registry file. If no 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.write(json.dumps(registry_data))
registry_file.truncate() registry_file.truncate()
def __read(self) -> list: def read(self) -> list:
""" """
Reads the registry file and parses the JSON and returns a 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 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 registry_data = list() # Existing, but empty registry
except FileNotFoundError: 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 return registry_data
@ -82,7 +86,7 @@ class EthereumContractRegistry:
need to use this. need to use this.
""" """
contract_data = [contract_name, contract_address, contract_abi] contract_data = [contract_name, contract_address, contract_abi]
registry_data = self.__read() registry_data = self.read()
registry_data.append(contract_data) registry_data.append(contract_data)
self.__write(registry_data) self.__write(registry_data)
@ -95,17 +99,17 @@ class EthereumContractRegistry:
raise ValueError("Pass contract_name or contract_address, not both.") raise ValueError("Pass contract_name or contract_address, not both.")
contracts = list() contracts = list()
registry_data = self.__read() registry_data = self.read()
for name, addr, abi in registry_data: 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)) contracts.append((name, addr, abi))
if not contracts: if not contracts:
raise self.UnknownContract raise self.UnknownContract
if contract_address and len(contracts) > 1: if contract_address and len(contracts) > 1:
m = "Multiple records returned for address {}" 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] return contracts if contract_name else contracts[0]

View File

@ -3,7 +3,7 @@ import pytest
from nucypher.blockchain.eth.interfaces import EthereumContractRegistry from nucypher.blockchain.eth.interfaces import EthereumContractRegistry
def test_registrar_object(tempfile_path): def test_contract_registrary(tempfile_path):
with pytest.raises(EthereumContractRegistry.RegistryError): with pytest.raises(EthereumContractRegistry.RegistryError):
bad_registrar = EthereumContractRegistry(registry_filepath='/fake/file/path/registry.json') bad_registrar = EthereumContractRegistry(registry_filepath='/fake/file/path/registry.json')