def test_get_network_name(): name = Keeper.get_network_name(Keeper.get_network_id()) assert name in Keeper._network_name_map.values() os.environ['KEEPER_NETWORK_NAME'] = 'yellow' assert 'KEEPER_NETWORK_NAME' in os.environ name = Keeper.get_network_name(Keeper.get_network_id()) assert name == 'yellow' del os.environ['KEEPER_NETWORK_NAME'] assert Keeper.get_network_name(1) == Keeper._network_name_map.get(1) assert Keeper.get_network_name(2) == Keeper._network_name_map.get(2) assert Keeper.get_network_name(3) == Keeper._network_name_map.get(3) assert Keeper.get_network_name(4) == Keeper._network_name_map.get(4) assert Keeper.get_network_name(42) == Keeper._network_name_map.get(42) assert Keeper.get_network_name(77) == Keeper._network_name_map.get(77) assert Keeper.get_network_name(99) == Keeper._network_name_map.get(99) assert Keeper.get_network_name(137) == Keeper._network_name_map.get(137) assert Keeper.get_network_name(8996) == Keeper._network_name_map.get(8996) assert Keeper.get_network_name(8997) == Keeper._network_name_map.get(8997) assert Keeper.get_network_name(42220) == Keeper._network_name_map.get( 42220) assert Keeper.get_network_name(44787) == Keeper._network_name_map.get( 44787) assert Keeper.get_network_name(62320) == Keeper._network_name_map.get( 62320) assert Keeper.get_network_name(80001) == Keeper._network_name_map.get( 80001) assert Keeper.get_network_name(1313161554) == Keeper._network_name_map.get( 1313161554) assert Keeper.get_network_name(1313161555) == Keeper._network_name_map.get( 1313161555) assert Keeper.get_network_name(1313161556) == Keeper._network_name_map.get( 1313161556) assert Keeper.get_network_name(0) == 'development'
def get_contract_dict_by_name(contract_name, artifacts_path): """ Retrieve the Contract instance for a given contract name. :param contract_name: str :param artifacts_path: str the path to keeper contracts artifacts (`abi` .json files) :return: the smart contract's definition from the json abi file, dict """ network_name = Keeper.get_network_name(Keeper.get_network_id()).lower() # file_name = '{}.{}.json'.format(contract_name, network_name) # path = os.path.join(keeper.artifacts_path, file_name) path = ContractHandler._get_contract_file_path(artifacts_path, contract_name, network_name, artifacts_path) if not (path and os.path.exists(path)): path = ContractHandler._get_contract_file_path( artifacts_path, contract_name, network_name.lower(), artifacts_path) if not (path and os.path.exists(path)): path = ContractHandler._get_contract_file_path( artifacts_path, contract_name, Keeper.DEFAULT_NETWORK_NAME, artifacts_path) if not (path and os.path.exists(path)): raise FileNotFoundError(f'Keeper contract {contract_name} file ' f'not found in {artifacts_path} ' f'using network name {network_name}') with open(path) as f: contract_dict = json.loads(f.read()) return contract_dict
def verify_contracts(): """ Verify that the contracts are deployed correctly in the network. :raise Exception: raise exception if the contracts are not deployed correctly. """ artifacts_path = Keeper.get_instance().artifacts_path logger.info( f'Keeper contract artifacts (JSON abi files) at: {artifacts_path}') if os.environ.get('KEEPER_NETWORK_NAME'): logger.warning( f'The `KEEPER_NETWORK_NAME` env var is set to ' f'{os.environ.get("KEEPER_NETWORK_NAME")}. ' f'This enables the user to override the method of how the network name ' f'is inferred from network id.') # try to find contract with this network name contract_name = Diagnostics.TEST_CONTRACT_NAME network_id = Keeper.get_network_id() network_name = Keeper.get_network_name(network_id) logger.info(f'Using keeper contracts from network {network_name}, ' f'network id is {network_id}') logger.info( f'Looking for keeper contracts ending with ".{network_name}.json", ' f'e.g. "{contract_name}.{network_name}.json".') existing_contract_names = os.listdir(artifacts_path) try: ContractHandler.get(contract_name) except Exception as e: logger.error(e) logger.error( f'Cannot find the keeper contracts. \n' f'Current network id is {network_id} and network name is {network_name}.' f'Expected to find contracts ending with ".{network_name}.json",' f' e.g. "{contract_name}.{network_name}.json"') raise ContractsNotFound( f'Keeper contracts for keeper network {network_name} were not found ' f'in {artifacts_path}. \n' f'Found the following contracts: \n\t{existing_contract_names}' ) keeper = Keeper.get_instance() contracts = [ keeper.token, keeper.did_registry, keeper.agreement_manager, keeper.template_manager, keeper.condition_manager, keeper.access_condition, keeper.sign_condition, keeper.lock_payment_condition, keeper.access_template, keeper.escrow_payment_condition, keeper.hash_lock_condition ] addresses = '\n'.join([f'\t{c.name}: {c.address}' for c in contracts]) logging.info('Finished loading keeper contracts:\n' '%s', addresses)
def test_get_network_id(): network_id = Keeper.get_network_id() assert isinstance(network_id, int) assert network_id in Keeper._network_name_map