def new_local_registry(): filename = f'{BASE_TEMP_PREFIX}mock-empty-registry-{datetime.now().strftime(DATETIME_FORMAT)}.json' registry_filepath = BASE_TEMP_DIR / filename registry = LocalContractRegistry(filepath=registry_filepath) registry.write(InMemoryContractRegistry().read()) yield registry if registry_filepath.exists(): registry_filepath.unlink()
def new_local_registry(): filename = f'{BASE_TEMP_PREFIX}mock-empty-registry-{datetime.now().strftime(DATETIME_FORMAT)}.json' registry_filepath = os.path.join(BASE_TEMP_DIR, filename) registry = LocalContractRegistry(filepath=registry_filepath) registry.write(InMemoryContractRegistry().read()) yield registry if os.path.exists(registry_filepath): os.remove(registry_filepath)
def test_contract_registry(tempfile_path): # ABC with pytest.raises(TypeError): BaseContractRegistry(filepath='test') with pytest.raises(BaseContractRegistry.RegistryError): bad_registry = LocalContractRegistry(filepath='/fake/file/path/registry.json') bad_registry.search(contract_address='0xdeadbeef') # Tests everything is as it should be when initially created test_registry = LocalContractRegistry(filepath=tempfile_path) assert test_registry.read() == list() # Test contract enrollment and dump_chain test_name = 'TestContract' test_addr = '0xDEADBEEF' test_abi = ['fake', 'data'] test_version = "some_version" test_registry.enroll(contract_name=test_name, contract_address=test_addr, contract_abi=test_abi, contract_version=test_version) # Search by name... contract_records = test_registry.search(contract_name=test_name) assert len(contract_records) == 1, 'More than one record for {}'.format(test_name) assert len(contract_records[0]) == 4, 'Registry record is the wrong length' name, version, address, abi = contract_records[0] assert name == test_name assert address == test_addr assert abi == test_abi assert version == test_version # ...or by address contract_record = test_registry.search(contract_address=test_addr) name, version, address, abi = contract_record assert name == test_name assert address == test_addr assert abi == test_abi assert version == test_version # Check that searching for an unknown contract raises with pytest.raises(BaseContractRegistry.UnknownContract): test_registry.search(contract_name='this does not exist') current_dataset = test_registry.read() # Corrupt the registry with a duplicate address current_dataset.append([test_name, test_addr, test_abi]) test_registry.write(current_dataset) # Check that searching for an unknown contract raises with pytest.raises(BaseContractRegistry.InvalidRegistry): test_registry.search(contract_address=test_addr)
def agency_local_registry(testerchain, agency, test_registry): registry = LocalContractRegistry(filepath=MOCK_REGISTRY_FILEPATH) registry.write(test_registry.read()) yield registry if MOCK_REGISTRY_FILEPATH.exists(): MOCK_REGISTRY_FILEPATH.unlink()
def agency_local_registry(testerchain, agency, test_registry): registry = LocalContractRegistry(filepath=MOCK_REGISTRY_FILEPATH) registry.write(test_registry.read()) yield registry if os.path.exists(MOCK_REGISTRY_FILEPATH): os.remove(MOCK_REGISTRY_FILEPATH)
def test_manual_deployment_of_idle_network(click_runner): if os.path.exists(ALTERNATE_REGISTRY_FILEPATH_2): os.remove(ALTERNATE_REGISTRY_FILEPATH_2) assert not os.path.exists(ALTERNATE_REGISTRY_FILEPATH_2) registry = LocalContractRegistry(filepath=ALTERNATE_REGISTRY_FILEPATH_2) registry.write(InMemoryContractRegistry().read()) # TODO: Manual deployments from scratch require an existing but empty registry (i.e., a json file just with "[]") user_input = '0\n' + YES_ENTER + 'DEPLOY' # 1. Deploy NuCypherToken command = ('contracts', '--contract-name', NUCYPHER_TOKEN_CONTRACT_NAME, '--provider', TEST_PROVIDER_URI, '--network', TEMPORARY_DOMAIN, '--registry-infile', ALTERNATE_REGISTRY_FILEPATH_2) result = click_runner.invoke(deploy, command, input=user_input, catch_exceptions=False) assert result.exit_code == 0, result.output assert os.path.exists(ALTERNATE_REGISTRY_FILEPATH_2) new_registry = LocalContractRegistry(filepath=ALTERNATE_REGISTRY_FILEPATH_2) deployed_contracts = [NUCYPHER_TOKEN_CONTRACT_NAME] assert list(new_registry.enrolled_names) == deployed_contracts # 2. Deploy StakingEscrow in INIT mode command = ('contracts', '--contract-name', STAKING_ESCROW_CONTRACT_NAME, '--mode', 'init', '--provider', TEST_PROVIDER_URI, '--network', TEMPORARY_DOMAIN, '--registry-infile', ALTERNATE_REGISTRY_FILEPATH_2) result = click_runner.invoke(deploy, command, input=user_input, catch_exceptions=False) assert result.exit_code == 0 deployed_contracts.extend([STAKING_ESCROW_STUB_CONTRACT_NAME, DISPATCHER_CONTRACT_NAME]) assert list(new_registry.enrolled_names) == deployed_contracts # 3. Deploy PolicyManager command = ('contracts', '--contract-name', POLICY_MANAGER_CONTRACT_NAME, '--provider', TEST_PROVIDER_URI, '--network', TEMPORARY_DOMAIN, '--registry-infile', ALTERNATE_REGISTRY_FILEPATH_2) result = click_runner.invoke(deploy, command, input=user_input, catch_exceptions=False) assert result.exit_code == 0 deployed_contracts.extend([POLICY_MANAGER_CONTRACT_NAME, DISPATCHER_CONTRACT_NAME]) assert list(new_registry.enrolled_names) == deployed_contracts # 4. Deploy Adjudicator command = ('contracts', '--contract-name', ADJUDICATOR_CONTRACT_NAME, '--provider', TEST_PROVIDER_URI, '--network', TEMPORARY_DOMAIN, '--registry-infile', ALTERNATE_REGISTRY_FILEPATH_2) result = click_runner.invoke(deploy, command, input=user_input, catch_exceptions=False) assert result.exit_code == 0 deployed_contracts.extend([ADJUDICATOR_CONTRACT_NAME, DISPATCHER_CONTRACT_NAME]) assert list(new_registry.enrolled_names) == deployed_contracts # 5. Deploy StakingEscrow in IDLE mode command = ('contracts', '--contract-name', STAKING_ESCROW_CONTRACT_NAME, '--mode', 'idle', '--provider', TEST_PROVIDER_URI, '--network', TEMPORARY_DOMAIN, '--registry-infile', ALTERNATE_REGISTRY_FILEPATH_2) result = click_runner.invoke(deploy, command, input=user_input, catch_exceptions=False) assert result.exit_code == 0 deployed_contracts.extend([STAKING_ESCROW_CONTRACT_NAME]) assert list(new_registry.enrolled_names) == deployed_contracts # 6. Activate StakingEscrow command = ('contracts', '--contract-name', STAKING_ESCROW_CONTRACT_NAME, '--activate', '--provider', TEST_PROVIDER_URI, '--network', TEMPORARY_DOMAIN, '--registry-infile', ALTERNATE_REGISTRY_FILEPATH_2) user_input = '0\n' + YES_ENTER + YES_ENTER + INSECURE_DEVELOPMENT_PASSWORD result = click_runner.invoke(deploy, command, input=user_input, catch_exceptions=False) assert result.exit_code == 0 assert list(new_registry.enrolled_names) == deployed_contracts