More tests for re-stake

pull/823/head
szotov 2019-03-09 15:19:43 +03:00
parent 586adbc302
commit 2f26a3bb64
5 changed files with 91 additions and 43 deletions

View File

@ -314,7 +314,7 @@ contract MinersEscrow is Issuer {
* @param _miner Miner
**/
function isReStakeLocked(address _miner) public view returns (bool) {
return getCurrentPeriod() <= minerInfo[_miner].lockReStakeUntilPeriod;
return getCurrentPeriod() < minerInfo[_miner].lockReStakeUntilPeriod;
}
//------------------------Main methods------------------------

View File

@ -16,6 +16,8 @@ contract MinersEscrowForUserEscrowMock {
uint16 public periods;
uint16 public confirmedPeriod;
uint256 public index;
bool public reStake;
uint16 public lockReStakeUntilPeriod;
constructor(NuCypherToken _token) public {
token = _token;
@ -30,20 +32,17 @@ contract MinersEscrowForUserEscrowMock {
}
function lock(uint256 _value, uint16 _periods) public {
require(node == msg.sender);
lockedValue += _value;
periods += _periods;
}
function divideStake(uint256 _index, uint256 _newValue, uint16 _periods) public {
require(node == msg.sender);
index = _index;
lockedValue += _newValue;
periods += _periods;
}
function withdraw(uint256 _value) public {
require(node == msg.sender);
value -= _value;
token.transfer(msg.sender, _value);
}
@ -53,14 +52,20 @@ contract MinersEscrowForUserEscrowMock {
}
function confirmActivity() external {
require(node == msg.sender);
confirmedPeriod += 1;
}
function mint() external {
require(node == msg.sender);
value += 1000;
}
function setReStake(bool _reStake) public {
reStake = _reStake;
}
function lockReStake(uint16 _lockReStakeUntilPeriod) public {
lockReStakeUntilPeriod = _lockReStakeUntilPeriod;
}
}

View File

@ -37,26 +37,10 @@ from cryptography.hazmat.primitives import hashes
NULL_ADDR = '0x' + '0' * 40
VALUE_FIELD = 0
DECIMALS_FIELD = 1
CONFIRMED_PERIOD_1_FIELD = 2
CONFIRMED_PERIOD_2_FIELD = 3
LAST_ACTIVE_PERIOD_FIELD = 4
RE_STAKE_FIELD = 3
CLIENT_FIELD = 0
RATE_FIELD = 1
FIRST_REWARD_FIELD = 2
START_PERIOD_FIELD = 3
LAST_PERIOD_FIELD = 4
DISABLED_FIELD = 5
REWARD_FIELD = 0
REWARD_RATE_FIELD = 1
LAST_MINED_PERIOD_FIELD = 2
ACTIVE_STATE = 0
UPGRADE_WAITING_STATE = 1
FINISHED_STATE = 2
SECRET_LENGTH = 32
escrow_secret = os.urandom(SECRET_LENGTH)
policy_manager_secret = os.urandom(SECRET_LENGTH)
@ -331,6 +315,27 @@ def test_all(testerchain, token, escrow, policy_manager, adjudicator, user_escro
tx = escrow.functions.deposit(1, 1).transact({'from': ursula1})
testerchain.wait_for_receipt(tx)
# Create the first user escrow, set and lock re-stake parameter
user_escrow_1, _ = testerchain.interface.deploy_contract(
'UserEscrow', user_escrow_linker.address, token.address)
user_escrow_proxy_1 = testerchain.interface.w3.eth.contract(
abi=user_escrow_proxy.abi,
address=user_escrow_1.address,
ContractFactoryClass=Contract)
tx = user_escrow_1.functions.transferOwnership(ursula3).transact({'from': creator})
testerchain.wait_for_receipt(tx)
assert not escrow.functions.minerInfo(user_escrow_1.address).call()[RE_STAKE_FIELD]
tx = user_escrow_proxy_1.functions.setReStake(True).transact({'from': ursula3})
testerchain.wait_for_receipt(tx)
period = escrow.functions.getCurrentPeriod().call()
tx = user_escrow_proxy_1.functions.lockReStake(period + 22).transact({'from': ursula3})
testerchain.wait_for_receipt(tx)
assert escrow.functions.minerInfo(user_escrow_1.address).call()[RE_STAKE_FIELD]
# Can't unlock re-stake parameter now
with pytest.raises((TransactionFailed, ValueError)):
tx = user_escrow_proxy_1.functions.setReStake(False).transact({'from': ursula3})
testerchain.wait_for_receipt(tx)
# Initialize escrow
reward = 10 ** 9
tx = token.functions.transfer(escrow.address, reward).transact({'from': creator})
@ -339,22 +344,13 @@ def test_all(testerchain, token, escrow, policy_manager, adjudicator, user_escro
execute_multisig_transaction(testerchain, multisig, [contracts_owners[0], contracts_owners[1]], tx)
# Deposit some tokens to the user escrow and lock them
user_escrow_1, _ = testerchain.interface.deploy_contract(
'UserEscrow', user_escrow_linker.address, token.address)
user_escrow_proxy_1 = testerchain.interface.w3.eth.contract(
abi=user_escrow_proxy.abi,
address=user_escrow_1.address,
ContractFactoryClass=Contract)
tx = user_escrow_1.functions.transferOwnership(ursula3).transact({'from': creator})
testerchain.wait_for_receipt(tx)
tx = token.functions.approve(user_escrow_1.address, 10000).transact({'from': creator})
testerchain.wait_for_receipt(tx)
tx = user_escrow_1.functions.initialDeposit(10000, 20 * 60 * 60).transact({'from': creator})
testerchain.wait_for_receipt(tx)
user_escrow_2, _ = testerchain.interface.deploy_contract(
'UserEscrow', user_escrow_linker.address, token.address)
tx = user_escrow_2.functions.transferOwnership(ursula4).transact({'from': creator})
testerchain.wait_for_receipt(tx)
tx = token.functions.approve(user_escrow_2.address, 10000).transact({'from': creator})
@ -480,6 +476,12 @@ def test_all(testerchain, token, escrow, policy_manager, adjudicator, user_escro
tx = user_escrow_proxy_1.functions.confirmActivity().transact({'from': ursula3})
testerchain.wait_for_receipt(tx)
# Turn on re-stake for Ursula1
assert not escrow.functions.minerInfo(ursula1).call()[RE_STAKE_FIELD]
tx = escrow.functions.setReStake(True).transact({'from': ursula1})
testerchain.wait_for_receipt(tx)
assert escrow.functions.minerInfo(ursula1).call()[RE_STAKE_FIELD]
testerchain.time_travel(hours=1)
tx = escrow.functions.confirmActivity().transact({'from': ursula1})
testerchain.wait_for_receipt(tx)
@ -568,6 +570,12 @@ def test_all(testerchain, token, escrow, policy_manager, adjudicator, user_escro
tx = user_escrow_proxy_1.functions.confirmActivity().transact({'from': ursula3})
testerchain.wait_for_receipt(tx)
# Turn off re-stake for Ursula1
assert escrow.functions.minerInfo(ursula1).call()[RE_STAKE_FIELD]
tx = escrow.functions.setReStake(False).transact({'from': ursula1})
testerchain.wait_for_receipt(tx)
assert not escrow.functions.minerInfo(ursula1).call()[RE_STAKE_FIELD]
testerchain.time_travel(hours=1)
tx = escrow.functions.confirmActivity().transact({'from': ursula1})
testerchain.wait_for_receipt(tx)
@ -913,7 +921,17 @@ def test_all(testerchain, token, escrow, policy_manager, adjudicator, user_escro
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
# Can't unlock re-stake parameter yet
with pytest.raises((TransactionFailed, ValueError)):
tx = user_escrow_proxy_1.functions.setReStake(False).transact({'from': ursula3})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
# Now can turn off re-stake
tx = user_escrow_proxy_1.functions.setReStake(False).transact({'from': ursula3})
testerchain.wait_for_receipt(tx)
assert not escrow.functions.minerInfo(user_escrow_1.address).call()[RE_STAKE_FIELD]
tx = escrow.functions.mint().transact({'from': ursula1})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.mint().transact({'from': ursula2})

View File

@ -211,9 +211,9 @@ def test_re_stake(testerchain, token, escrow_contract):
tx = escrow.functions.setReStake(True).transact({'from': ursula})
testerchain.wait_for_receipt(tx)
assert escrow.functions.minerInfo(ursula).call()[RE_STAKE_FIELD]
tx = escrow.functions.lockReStake(period + 5).transact({'from': ursula})
tx = escrow.functions.lockReStake(period + 6).transact({'from': ursula})
testerchain.wait_for_receipt(tx)
# Can't set re-stake parameter during 5 periods
# Can't set re-stake parameter during 6 periods
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.setReStake(False).transact({'from': ursula})
testerchain.wait_for_receipt(tx)

View File

@ -109,7 +109,7 @@ def test_escrow(testerchain, token, user_escrow):
# TODO test state of the proxy contract
@pytest.mark.slow
def test_miner(testerchain, token, escrow, user_escrow, user_escrow_proxy):
def test_miner(testerchain, token, escrow, user_escrow, user_escrow_proxy, proxy):
"""
Test miner functions in the user escrow
"""
@ -163,24 +163,27 @@ def test_miner(testerchain, token, escrow, user_escrow, user_escrow_proxy):
tx = user_escrow.functions.withdrawTokens(100).transact({'from': user})
testerchain.wait_for_receipt(tx)
# User can't use the miners escrow directly because only the user escrow owns tokens in the miners escrow
# User can't use the proxy contract directly
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.lock(100, 1).transact({'from': user})
tx = proxy.functions.lock(100, 1).transact({'from': user})
testerchain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.divideStake(1, 100, 1).transact({'from': user})
tx = proxy.functions.divideStake(1, 100, 1).transact({'from': user})
testerchain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.confirmActivity().transact({'from': user})
tx = proxy.functions.confirmActivity().transact({'from': user})
testerchain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.mint().transact({'from': user})
tx = proxy.functions.mint().transact({'from': user})
testerchain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.withdraw(100).transact({'from': user})
tx = proxy.functions.withdrawAsMiner(100).transact({'from': user})
testerchain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.withdrawAll().transact({'from': user})
tx = proxy.functions.setReStake(True).transact({'from': user})
testerchain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = proxy.functions.lockReStake(0).transact({'from': user})
testerchain.wait_for_receipt(tx)
locks = user_escrow_proxy.events.Locked.createFilter(fromBlock='latest')
@ -189,6 +192,8 @@ def test_miner(testerchain, token, escrow, user_escrow, user_escrow_proxy):
mints = user_escrow_proxy.events.Mined.createFilter(fromBlock='latest')
miner_withdraws = user_escrow_proxy.events.WithdrawnAsMiner.createFilter(fromBlock='latest')
withdraws = user_escrow.events.TokensWithdrawn.createFilter(fromBlock='latest')
re_stakes = user_escrow_proxy.events.ReStakeSet.createFilter(fromBlock='latest')
re_stake_locks = user_escrow_proxy.events.ReStakeLocked.createFilter(fromBlock='latest')
# Use miners methods through the user escrow
tx = user_escrow_proxy.functions.lock(100, 1).transact({'from': user})
@ -218,6 +223,14 @@ def test_miner(testerchain, token, escrow, user_escrow, user_escrow_proxy):
assert 9000 == token.functions.balanceOf(escrow.address).call()
assert 3000 == token.functions.balanceOf(user_escrow.address).call()
# Test re-stake methods
tx = user_escrow_proxy.functions.setReStake(True).transact({'from': user})
testerchain.wait_for_receipt(tx)
assert escrow.functions.reStake().call()
tx = user_escrow_proxy.functions.lockReStake(123).transact({'from': user})
testerchain.wait_for_receipt(tx)
assert 123 == escrow.functions.lockReStakeUntilPeriod().call()
events = locks.get_all_entries()
assert 1 == len(events)
event_args = events[0]['args']
@ -252,6 +265,18 @@ def test_miner(testerchain, token, escrow, user_escrow, user_escrow_proxy):
assert user == event_args['owner']
assert 1000 == event_args['value']
events = re_stakes.get_all_entries()
assert 1 == len(events)
event_args = events[0]['args']
assert user == event_args['owner']
assert event_args['reStake']
events = re_stake_locks.get_all_entries()
assert 1 == len(events)
event_args = events[0]['args']
assert user == event_args['owner']
assert 123 == event_args['lockUntilPeriod']
# User can withdraw reward for mining but no more than locked
with pytest.raises((TransactionFailed, ValueError)):
tx = user_escrow.functions.withdrawTokens(2500).transact({'from': user})