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