diff --git a/tests/blockchain/eth/entities/agents/test_staking_escrow_agent.py b/tests/blockchain/eth/entities/agents/test_staking_escrow_agent.py index b54960f96..c9be8e7fe 100644 --- a/tests/blockchain/eth/entities/agents/test_staking_escrow_agent.py +++ b/tests/blockchain/eth/entities/agents/test_staking_escrow_agent.py @@ -18,7 +18,7 @@ along with nucypher. If not, see . import os import pytest -from eth_utils.address import to_checksum_address +from eth_utils.address import to_checksum_address, is_address from nucypher.blockchain.eth.agents import StakingEscrowAgent from nucypher.blockchain.eth.chains import Blockchain @@ -45,17 +45,15 @@ def test_deposit_tokens(testerchain, agency, token_economics): # Previously, she needs to approve this transfer on the token contract. # - _txhash = token_agent.approve_transfer(amount=token_economics.minimum_allowed_locked * 10, # Approve - target_address=staking_agent.contract_address, + _receipt = token_agent.approve_transfer(amount=token_economics.minimum_allowed_locked * 10, # Approve + target_address=staking_agent.contract_address, + sender_address=staker_account) + + receipt = staking_agent.deposit_tokens(amount=locked_tokens, + lock_periods=token_economics.minimum_locked_periods, sender_address=staker_account) - txhash = staking_agent.deposit_tokens(amount=locked_tokens, - lock_periods=token_economics.minimum_locked_periods, - sender_address=staker_account) - # Check the receipt for the contract address success code - # TODO: What are we checking here? Why transaction rejected? - receipt = testerchain.wait_for_receipt(txhash) assert receipt['status'] == 1, "Transaction Rejected" assert receipt['logs'][2]['address'] == staking_agent.contract_address @@ -110,67 +108,58 @@ def test_stakers_and_workers_relationships(testerchain, agency): @pytest.mark.slow() -def test_get_staker_population(agency, blockchain_ursulas): - token_agent, staking_agent, policy_agent = agency - agent = staking_agent - assert agent.get_staker_population() == len(blockchain_ursulas) +def test_get_staker_population(agency, stakers): + _token_agent, staking_agent, _policy_agent = agency + + # Apart from all the stakers in the fixture, we also added a new staker above + assert staking_agent.get_staker_population() == len(stakers) + 1 @pytest.mark.slow() def test_get_swarm(agency, blockchain_ursulas): - token_agent, staking_agent, policy_agent = agency - agent = staking_agent - swarm = agent.swarm() + _token_agent, staking_agent, _policy_agent = agency + + swarm = staking_agent.swarm() swarm_addresses = list(swarm) - assert len(swarm_addresses) == len(blockchain_ursulas) + assert len(swarm_addresses) == len(blockchain_ursulas) + 1 # Grab a staker address from the swarm staker_addr = swarm_addresses[0] assert isinstance(staker_addr, str) - - try: - int(staker_addr, 16) # Verify the address is hex - except ValueError: - pytest.fail() + assert is_address(staker_addr) @pytest.mark.slow() @pytest.mark.usefixtures("blockchain_ursulas") def test_sample_stakers(agency): - token_agent, staking_agent, policy_agent = agency - agent = staking_agent - stakers_population = agent.get_staker_population() + _token_agent, staking_agent, _policy_agent = agency + stakers_population = staking_agent.get_staker_population() with pytest.raises(StakingEscrowAgent.NotEnoughStakers): - agent.sample(quantity=stakers_population + 1, duration=1) # One more than we have deployed + staking_agent.sample(quantity=stakers_population + 1, duration=1) # One more than we have deployed - stakers = agent.sample(quantity=3, duration=5) + stakers = staking_agent.sample(quantity=3, duration=5) assert len(stakers) == 3 # Three... assert len(set(stakers)) == 3 # ...unique addresses -def test_get_current_period(agency): - token_agent, staking_agent, policy_agent = agency - agent = staking_agent - testerchain = agent.blockchain - start_period = agent.get_current_period() +def test_get_current_period(agency, testerchain): + _token_agent, staking_agent,_policy_agent = agency + start_period = staking_agent.get_current_period() testerchain.time_travel(periods=1) - end_period = agent.get_current_period() + end_period = staking_agent.get_current_period() assert end_period > start_period @pytest.mark.slow() -def test_confirm_activity(agency): - token_agent, staking_agent, policy_agent = agency - agent = staking_agent - testerchain = agent.blockchain - origin, someone, *everybody_else = testerchain.interface.w3.eth.accounts - _txhash = agent.set_worker(node_address=someone, worker_address=someone) - txhash = agent.confirm_activity(node_address=someone) - testerchain = agent.blockchain - receipt = testerchain.wait_for_receipt(txhash) +def test_confirm_activity(agency, testerchain): + _token_agent, staking_agent, _policy_agent = agency + + staker_account, worker_account, *other = testerchain.unassigned_accounts + + receipt = staking_agent.confirm_activity(worker_address=worker_account) assert receipt['status'] == 1, "Transaction Rejected" - assert receipt['logs'][0]['address'] == agent.contract_address + assert receipt['logs'][0]['address'] == staking_agent.contract_address @pytest.mark.skip('To be implemented') @@ -212,25 +201,23 @@ def test_divide_stake(agency, token_economics): @pytest.mark.slow() -def test_collect_staking_reward(agency): - token_agent, staking_agent, policy_agent = agency - agent = staking_agent - testerchain = agent.blockchain - origin, someone, *everybody_else = testerchain.interface.w3.eth.accounts +def test_collect_staking_reward(agency, testerchain): + token_agent, staking_agent, _policy_agent = agency + + staker_account, worker_account, *other = testerchain.unassigned_accounts # Confirm Activity - _txhash = agent.confirm_activity(node_address=someone) + _receipt = staking_agent.confirm_activity(worker_address=worker_account) testerchain.time_travel(periods=2) # Mint - _txhash = agent.mint(node_address=someone) + _receipt = staking_agent.mint(staker_address=staker_account) - old_balance = token_agent.get_balance(address=someone) + old_balance = token_agent.get_balance(address=staker_account) - txhash = agent.collect_staking_reward(staker_address=someone) - receipt = testerchain.wait_for_receipt(txhash) + receipt = staking_agent.collect_staking_reward(staker_address=staker_account) assert receipt['status'] == 1, "Transaction Rejected" - assert receipt['logs'][-1]['address'] == agent.contract_address + assert receipt['logs'][-1]['address'] == staking_agent.contract_address - new_balance = token_agent.get_balance(address=someone) # not the shoes + new_balance = token_agent.get_balance(address=staker_account) # not the shoes assert new_balance > old_balance