def session_agency(_session_testerchain): testerchain = _session_testerchain testerchain.registry.clear() agents = _make_agency(testerchain) yield agents testerchain.registry.clear() Agency.clear()
def three_agents(testerchain): """ Musketeers, if you will. Launch the big three contracts on provided chain, make agents for each and return them. """ """Launch all Nucypher ethereum contracts""" origin = testerchain.etherbase_account token_deployer = NucypherTokenDeployer(blockchain=testerchain, deployer_address=origin) token_deployer.deploy() miner_escrow_deployer = MinerEscrowDeployer(deployer_address=origin) miner_escrow_deployer.deploy( secret_hash=os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH)) policy_manager_deployer = PolicyManagerDeployer(deployer_address=origin) policy_manager_deployer.deploy( secret_hash=os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH)) token_agent = token_deployer.make_agent() # 1: Token miner_agent = miner_escrow_deployer.make_agent() # 2 Miner Escrow policy_agent = policy_manager_deployer.make_agent() # 3 Policy Agent adjudicator_deployer = MiningAdjudicatorDeployer(deployer_address=origin) adjudicator_deployer.deploy( secret_hash=os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH)) yield token_agent, miner_agent, policy_agent Agency.clear()
def agency(testerchain): """Launch all Nucypher ethereum contracts""" origin = testerchain.etherbase_account token_deployer = NucypherTokenDeployer(blockchain=testerchain, deployer_address=origin) token_deployer.deploy() staking_escrow_deployer = StakingEscrowDeployer(deployer_address=origin, blockchain=testerchain) staking_escrow_deployer.deploy( secret_hash=os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH)) policy_manager_deployer = PolicyManagerDeployer(deployer_address=origin, blockchain=testerchain) policy_manager_deployer.deploy( secret_hash=os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH)) adjudicator_deployer = AdjudicatorDeployer(deployer_address=origin, blockchain=testerchain) adjudicator_deployer.deploy( secret_hash=os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH)) token_agent = token_deployer.make_agent() # 1 Token staking_agent = staking_escrow_deployer.make_agent() # 2 Miner Escrow policy_agent = policy_manager_deployer.make_agent() # 3 Policy Agent _adjudicator_agent = adjudicator_deployer.make_agent() # 4 Adjudicator # TODO: Perhaps we should get rid of returning these agents here. # What's important is deploying and creating the first agent for each contract, # and since agents are singletons, in tests it's only necessary to call the agent # constructor again to receive the existing agent. # # For example: # staking_agent = StakingEscrowAgent() # # This is more clear than how we currently obtain an agent instance in tests: # _, staking_agent, _ = agency # # Other advantages is that it's closer to how agents should be use (i.e., there # are no fixtures IRL) and it's more extensible (e.g., AdjudicatorAgent) yield token_agent, staking_agent, policy_agent Agency.clear()
def test_stake_init(click_runner, stakeholder_configuration_file_location, stake_value, mock_registry_filepath, token_economics, testerchain, agency, manual_staker): # Simulate "Reconnection" cached_blockchain = BlockchainInterface.reconnect() registry = cached_blockchain.registry assert registry.filepath == mock_registry_filepath def from_dict(*args, **kwargs): return testerchain BlockchainInterface.from_dict = from_dict # Staker address has not stakes staking_agent = Agency.get_agent(StakingEscrowAgent) stakes = list(staking_agent.get_all_stakes(staker_address=manual_staker)) assert not stakes stake_args = ('stake', 'init', '--config-file', stakeholder_configuration_file_location, '--registry-filepath', mock_registry_filepath, '--staking-address', manual_staker, '--value', stake_value.to_tokens(), '--duration', token_economics.minimum_locked_periods, '--force') # TODO: This test it writing to the default system directory and ignoring updates to the passes filepath user_input = f'0\n' + f'{INSECURE_DEVELOPMENT_PASSWORD}\n' + f'Y\n' result = click_runner.invoke(nucypher_cli, stake_args, input=user_input, catch_exceptions=False) assert result.exit_code == 0 # Test integration with BaseConfiguration with open(stakeholder_configuration_file_location, 'r') as config_file: _config_data = json.loads(config_file.read()) # Verify the stake is on-chain # Test integration with Agency stakes = list(staking_agent.get_all_stakes(staker_address=manual_staker)) assert len(stakes) == 1 # Test integration with NU start_period, end_period, value = stakes[0] assert NU(int(value), 'NuNit') == stake_value assert (end_period - start_period) == token_economics.minimum_locked_periods - 1 # Test integration with Stake stake = Stake.from_stake_info(index=0, checksum_address=manual_staker, stake_info=stakes[0]) assert stake.value == stake_value assert stake.duration == token_economics.minimum_locked_periods
def test_set_worker(software_stakeholder, manual_worker): stake = software_stakeholder.stakes[1] staker = software_stakeholder.get_active_staker(stake.owner_address) staking_agent = Agency.get_agent(StakingEscrowAgent) software_stakeholder.set_worker(staker_address=staker.checksum_address, worker_address=manual_worker) assert staking_agent.get_worker_from_staker( staker_address=staker.checksum_address) == manual_worker
def software_stakeholder(testerchain, agency, stakeholder_config_file_location): # Setup path = stakeholder_config_file_location if os.path.exists(path): os.remove(path) # 0xaAa482c790b4301bE18D75A0D1B11B2ACBEF798B stakeholder_private_key = '255f64a948eeb1595b8a2d1e76740f4683eca1c8f1433d13293db9b6e27676cc' address = testerchain.provider.ethereum_tester.add_account( stakeholder_private_key, password=INSECURE_DEVELOPMENT_PASSWORD) testerchain.provider.ethereum_tester.unlock_account( address, password=INSECURE_DEVELOPMENT_PASSWORD) tx = { 'to': address, 'from': testerchain.etherbase_account, 'value': Web3.toWei('1', 'ether') } txhash = testerchain.client.w3.eth.sendTransaction(tx) _receipt = testerchain.wait_for_receipt(txhash) # Mock TransactingPower consumption (Etherbase) transacting_power = TransactingPower( account=testerchain.etherbase_account, password=INSECURE_DEVELOPMENT_PASSWORD, blockchain=testerchain) transacting_power.activate() token_agent = Agency.get_agent(NucypherTokenAgent) token_agent.transfer(amount=NU(200_000, 'NU').to_nunits(), sender_address=testerchain.etherbase_account, target_address=address) # Create stakeholder from on-chain values given accounts over a web3 provider stakeholder = StakeHolder(blockchain=testerchain, funding_account=address, funding_password=INSECURE_DEVELOPMENT_PASSWORD, trezor=False) # Teardown yield stakeholder if os.path.exists(path): os.remove(path)
def test_divide_stake(software_stakeholder, token_economics): stake = software_stakeholder.stakes[0] target_value = token_economics.minimum_allowed_locked pre_divide_stake_value = stake.value original_stake, new_stake = software_stakeholder.divide_stake( address=stake.owner_address, password=INSECURE_DEVELOPMENT_PASSWORD, index=0, duration=10, value=target_value) staking_agent = Agency.get_agent(StakingEscrowAgent) stakes = list( staking_agent.get_all_stakes(staker_address=stake.owner_address)) assert len(stakes) == 2 assert new_stake.value == target_value assert original_stake.value == (pre_divide_stake_value - target_value)
def test_initialize_stake_with_existing_account(software_stakeholder, stake_value, token_economics): # There are no stakes. assert len(software_stakeholder.stakers) == 0 assert len(software_stakeholder.stakes) == 0 # No Stakes with pytest.raises(IndexError): stake = software_stakeholder.stakes[0] # Really... there are no stakes. staking_agent = Agency.get_agent(StakingEscrowAgent) stakes = list( staking_agent.get_all_stakes( staker_address=software_stakeholder.accounts[0])) assert len(stakes) == 0 # Stake, deriving a new account with a password, # sending tokens and ethers from the funding account # to the staker's account, then initializing a new stake. stake = software_stakeholder.initialize_stake( checksum_address=software_stakeholder.accounts[0], amount=stake_value, duration=token_economics.minimum_locked_periods) # Wait for stake to begin software_stakeholder.blockchain.time_travel(periods=1) # Ensure the stakeholder is tracking the new staker and stake. assert len(software_stakeholder.stakers) == 1 assert len(software_stakeholder.stakes) == 1 # Ensure common stake perspective between stakeholder and stake assert stake.blockchain == software_stakeholder.blockchain assert stake.value == stake_value assert stake.duration == token_economics.minimum_locked_periods stakes = list( staking_agent.get_all_stakes(staker_address=stake.owner_address)) assert len(stakes) == 1
def manual_staker(testerchain): # 0xaaa23A5c74aBA6ca5E7c09337d5317A7C4563075 staker_private_key = '13378db1c2af06933000504838afc2d52efa383206454deefb1836f8f4cd86f8' address = testerchain.provider.ethereum_tester.add_account( staker_private_key, password=INSECURE_DEVELOPMENT_PASSWORD) tx = { 'to': address, 'from': testerchain.etherbase_account, 'value': Web3.toWei('1', 'ether') } txhash = testerchain.client.w3.eth.sendTransaction(tx) _receipt = testerchain.wait_for_receipt(txhash) token_agent = Agency.get_agent(NucypherTokenAgent) token_agent.transfer(amount=NU(200_000, 'NU').to_nunits(), sender_address=testerchain.etherbase_account, target_address=address) yield address
def clear_out_agency(): yield Agency.clear()
def test_nucypher_deploy_allocation_contracts(click_runner, testerchain, deploy_user_input, mock_primary_registry_filepath, mock_allocation_infile, token_economics): TesterBlockchain.sever_connection() Agency.clear() if os.path.isfile(MOCK_ALLOCATION_REGISTRY_FILEPATH): os.remove(MOCK_ALLOCATION_REGISTRY_FILEPATH) assert not os.path.isfile(MOCK_ALLOCATION_REGISTRY_FILEPATH) # We start with a blockchain node, and nothing else... if os.path.isfile(mock_primary_registry_filepath): os.remove(mock_primary_registry_filepath) assert not os.path.isfile(mock_primary_registry_filepath) command = ['contracts', '--registry-outfile', mock_primary_registry_filepath, '--provider-uri', TEST_PROVIDER_URI, '--poa', '--no-sync'] user_input = deploy_user_input result = click_runner.invoke(deploy, command, input=user_input, catch_exceptions=False) assert result.exit_code == 0 # # Main # deploy_command = ('allocations', '--registry-infile', MOCK_REGISTRY_FILEPATH, '--allocation-infile', mock_allocation_infile.filepath, '--allocation-outfile', MOCK_ALLOCATION_REGISTRY_FILEPATH, '--provider-uri', 'tester://pyevm', '--poa') account_index = '0\n' yes = 'Y\n' node_password = f'{INSECURE_DEVELOPMENT_PASSWORD}\n' user_input = account_index + yes + node_password + yes result = click_runner.invoke(deploy, deploy_command, input=user_input, catch_exceptions=False) assert result.exit_code == 0 # ensure that a pre-allocation recipient has the allocated token quantity. beneficiary = testerchain.interface.w3.eth.accounts[-1] allocation_registry = AllocationRegistry(registry_filepath=MOCK_ALLOCATION_REGISTRY_FILEPATH) user_escrow_agent = UserEscrowAgent(beneficiary=beneficiary, allocation_registry=allocation_registry) assert user_escrow_agent.unvested_tokens == token_economics.minimum_allowed_locked # # Tear Down # # Destroy existing blockchain BlockchainInterface.disconnect()
def agency(testerchain): agents = _make_agency(testerchain) yield agents testerchain.registry.clear() Agency.clear()
def test_nucypher_deploy_contracts(click_runner, mock_primary_registry_filepath, mock_allocation_infile, token_economics): Agency.clear() # # Setup # # We start with a blockchain node, and nothing else... if os.path.isfile(mock_primary_registry_filepath): os.remove(mock_primary_registry_filepath) assert not os.path.isfile(mock_primary_registry_filepath) # # Main # command = [ 'contracts', '--registry-outfile', mock_primary_registry_filepath, '--provider-uri', TEST_PROVIDER_URI, '--poa' ] user_input = '0\n' + 'Y\n' + (f'{INSECURE_SECRETS[1]}\n' * 8) + 'DEPLOY' result = click_runner.invoke(deploy, command, input=user_input, catch_exceptions=False) assert result.exit_code == 0 # Ensure there is a report on each contract for registry_name in Deployer.contract_names: assert registry_name in result.output # Check that the primary contract registry was written # and peek at some of the registered entries assert os.path.isfile(mock_primary_registry_filepath) with open(mock_primary_registry_filepath, 'r') as file: # Ensure every contract's name was written to the file, somehow raw_registry_data = file.read() for registry_name in Deployer.contract_names: assert registry_name in raw_registry_data # Ensure the Registry is JSON deserializable registry_data = json.loads(raw_registry_data) # and that is has the correct number of entries assert len(registry_data) == 9 # Read several records token_record, escrow_record, dispatcher_record, *other_records = registry_data registered_name, registered_address, registered_abi = token_record # # Agency # token_agent = NucypherTokenAgent() assert token_agent.contract_name == registered_name assert token_agent.registry_contract_name == registered_name assert token_agent.contract_address == registered_address # Now show that we can use contract Agency and read from the blockchain assert token_agent.get_balance() == 0 staking_agent = StakingEscrowAgent() assert staking_agent.get_current_period() # and at least the others can be instantiated assert PolicyAgent()