StakingEscrow: removed messages from require and removed one deposit function to decrease contract size

pull/2025/head
vzotova 2020-05-25 18:11:43 +03:00
parent 54297be311
commit 3caa46f367
8 changed files with 353 additions and 355 deletions

View File

@ -40,7 +40,7 @@ interface WorkLockInterface {
/**
* @notice Contract holds and locks stakers tokens.
* Each staker that locks their tokens will receive some compensation
* @dev |v5.1.2|
* @dev |v5.2.1|
*/
contract StakingEscrow is Issuer, IERC900History {
@ -476,20 +476,21 @@ contract StakingEscrow is Issuer, IERC900History {
*/
function bondWorker(address _worker) external onlyStaker {
StakerInfo storage info = stakerInfo[msg.sender];
require(_worker != info.worker, "Specified worker is already bonded with this staker");
// Specified worker is already bonded with this staker
require(_worker != info.worker);
uint16 currentPeriod = getCurrentPeriod();
if (info.worker != address(0)) { // If this staker had a worker ...
// Check that enough time has passed to change it
require(currentPeriod >= info.workerStartPeriod.add16(minWorkerPeriods),
"Not enough time has passed since the previous bonding worker");
require(currentPeriod >= info.workerStartPeriod.add16(minWorkerPeriods));
// Remove the old relation "worker->staker"
stakerFromWorker[info.worker] = address(0);
}
if (_worker != address(0)) {
require(stakerFromWorker[_worker] == address(0), "Specified worker is already in use");
require(stakerInfo[_worker].subStakes.length == 0 || _worker == msg.sender,
"Specified worker is a staker");
// Specified worker is already in use
require(stakerFromWorker[_worker] == address(0));
// Specified worker is a staker
require(stakerInfo[_worker].subStakes.length == 0 || _worker == msg.sender);
// Set new worker->staker relation
stakerFromWorker[_worker] = msg.sender;
}
@ -638,7 +639,8 @@ contract StakingEscrow is Issuer, IERC900History {
require(numberOfSubStakes > 0 && subStakesLength >= endIndex);
StakerInfo storage info = stakerInfo[staker];
require(info.subStakes.length == 0);
require(stakerFromWorker[staker] == address(0), "A staker can't be a worker for another staker");
// A staker can't be a worker for another staker
require(stakerFromWorker[staker] == address(0));
stakers.push(staker);
policyManager.register(staker, previousPeriod);
@ -701,15 +703,6 @@ contract StakingEscrow is Issuer, IERC900History {
deposit(_from, _from, _value, uint16(payload));
}
/**
* @notice Deposit tokens
* @param _value Amount of tokens to deposit
* @param _periods Amount of periods during which tokens will be locked
*/
function deposit(uint256 _value, uint16 _periods) external {
deposit(msg.sender, msg.sender, _value, _periods);
}
/**
* @notice Deposit tokens
* @param _staker Staker
@ -730,8 +723,8 @@ contract StakingEscrow is Issuer, IERC900History {
function deposit(address _staker, address _payer, uint256 _value, uint16 _periods) internal {
require(_value != 0);
StakerInfo storage info = stakerInfo[_staker];
require(stakerFromWorker[_staker] == address(0) || stakerFromWorker[_staker] == info.worker,
"A staker can't be a worker for another staker");
// A staker can't be a worker for another staker
require(stakerFromWorker[_staker] == address(0) || stakerFromWorker[_staker] == info.worker);
// initial stake of the staker
if (info.subStakes.length == 0) {
stakers.push(_staker);
@ -837,7 +830,8 @@ contract StakingEscrow is Issuer, IERC900History {
uint16 currentPeriod = getCurrentPeriod();
uint16 startPeriod = getStartPeriod(info, currentPeriod);
uint16 lastPeriod = getLastPeriodOfSubStake(subStake, startPeriod);
require(lastPeriod > currentPeriod, "The sub stake must active at least in the next period");
// The sub stake must be active at least in the next period
require(lastPeriod > currentPeriod);
uint256 oldValue = subStake.lockedValue;
subStake.lockedValue = uint128(oldValue.sub(_newValue));
@ -855,20 +849,22 @@ contract StakingEscrow is Issuer, IERC900History {
*/
function prolongStake(uint256 _index, uint16 _periods) external onlyStaker {
StakerInfo storage info = stakerInfo[msg.sender];
require(_periods > 0, "Incorrect parameters");
// Incorrect parameters
require(_periods > 0);
SubStakeInfo storage subStake = info.subStakes[_index];
uint16 currentPeriod = getCurrentPeriod();
uint16 startPeriod = getStartPeriod(info, currentPeriod);
uint16 lastPeriod = getLastPeriodOfSubStake(subStake, startPeriod);
require(lastPeriod > currentPeriod, "The sub stake must active at least in the next period");
// The sub stake must be active at least in the next period
require(lastPeriod > currentPeriod);
subStake.periods = subStake.periods.add16(_periods);
// if the sub stake ends in the next committed period then reset the `lastPeriod` field
if (lastPeriod == startPeriod) {
subStake.lastPeriod = 0;
}
require(uint32(lastPeriod - currentPeriod) + _periods >= minLockedPeriods,
"The extended sub stake must not be less than the minimum value");
// The extended sub stake must not be less than the minimum value
require(uint32(lastPeriod - currentPeriod) + _periods >= minLockedPeriods);
emit Locked(msg.sender, subStake.lockedValue, lastPeriod + 1, _periods);
emit Prolonged(msg.sender, subStake.lockedValue, lastPeriod, _periods);
}
@ -909,8 +905,10 @@ contract StakingEscrow is Issuer, IERC900History {
function commitToNextPeriod() external isInitialized {
address staker = stakerFromWorker[msg.sender];
StakerInfo storage info = stakerInfo[staker];
require(info.value > 0, "Staker must have a stake to make a commitment");
require(msg.sender == tx.origin, "Only worker with real address can make a commitment");
// Staker must have a stake to make a commitment
require(info.value > 0);
// Only worker with real address can make a commitment
require(msg.sender == tx.origin);
uint16 lastCommittedPeriod = getLastCommittedPeriod(staker);
mint(staker);

View File

@ -146,7 +146,7 @@ contract StakingInterface is BaseStakingInterface {
function depositAsStaker(uint256 _value, uint16 _periods) public onlyDelegateCall {
require(token.balanceOf(address(this)) >= _value);
token.approve(address(escrow), _value);
escrow.deposit(_value, _periods);
escrow.deposit(address(this), _value, _periods);
emit DepositedAsStaker(msg.sender, _value, _periods);
}

View File

@ -244,7 +244,7 @@ contract Intermediary {
function deposit(uint256 _value, uint16 _periods) external {
token.approve(address(escrow), _value);
escrow.deposit(_value, _periods);
escrow.deposit(address(this), _value, _periods);
}
function commitToNextPeriod() external {

View File

@ -594,12 +594,12 @@ def test_staking(testerchain,
# Staker can't deposit and lock too low value
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.deposit(1, 1).transact({'from': staker1})
tx = escrow.functions.deposit(staker1, 1, 1).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
# And can't deposit and lock too high value
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.deposit(2001, 1).transact({'from': staker1})
tx = escrow.functions.deposit(staker1, 2001, 1).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
# Can't make a commitment before initialization
@ -629,7 +629,7 @@ def test_staking(testerchain,
testerchain.wait_for_receipt(tx)
# Staker transfers some tokens to the escrow and lock them
tx = escrow.functions.deposit(1000, 10).transact({'from': staker1})
tx = escrow.functions.deposit(staker1, 1000, 10).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(staker1).transact({'from': staker1})
testerchain.wait_for_receipt(tx)

View File

@ -79,9 +79,9 @@ def test_minting(testerchain, token, escrow_contract, token_economics):
# Staker and Staker(2) transfer some tokens to the escrow and lock them
current_period = escrow.functions.getCurrentPeriod().call()
ursula1_stake = 1000
staker1_stake = 1000
staker2_stake = 500
tx = escrow.functions.deposit(ursula1_stake, 2).transact({'from': staker1})
tx = escrow.functions.deposit(staker1, staker1_stake, 2).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(staker1).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
@ -91,7 +91,7 @@ def test_minting(testerchain, token, escrow_contract, token_economics):
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': staker1})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.deposit(staker2_stake, 2).transact({'from': staker2})
tx = escrow.functions.deposit(staker2, staker2_stake, 2).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(staker2).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
@ -164,9 +164,9 @@ def test_minting(testerchain, token, escrow_contract, token_economics):
current_period = escrow.functions.getCurrentPeriod().call()
# Check result of minting
total_locked = ursula1_stake + staker2_stake
total_locked = staker1_stake + staker2_stake
ursula1_reward = calculate_reward(500, total_locked, 1) + calculate_reward(500, total_locked, 2)
assert ursula1_stake + ursula1_reward == escrow.functions.getAllTokens(staker1).call()
assert staker1_stake + ursula1_reward == escrow.functions.getAllTokens(staker1).call()
ursula2_reward = calculate_reward(500, total_locked, 2)
assert staker2_stake + ursula2_reward == escrow.functions.getAllTokens(staker2).call()
# Check that downtime value has not changed
@ -195,11 +195,11 @@ def test_minting(testerchain, token, escrow_contract, token_economics):
# Staker tries to mint again and doesn't receive a reward
# There are no more committed periods that are ready to mint
ursula1_stake += ursula1_reward
staker1_stake += ursula1_reward
staker2_stake += ursula2_reward
tx = escrow.functions.mint().transact({'from': staker1})
testerchain.wait_for_receipt(tx)
assert ursula1_stake == escrow.functions.getAllTokens(staker1).call()
assert staker1_stake == escrow.functions.getAllTokens(staker1).call()
events = staking_log.get_all_entries()
assert 2 == len(events)
@ -229,9 +229,9 @@ def test_minting(testerchain, token, escrow_contract, token_economics):
tx = escrow.functions.mint().transact({'from': staker2})
testerchain.wait_for_receipt(tx)
ursula1_reward = calculate_reward(500, 1000, 0) + calculate_reward(500, 1000, 1) + calculate_reward(500, 500, 0)
assert ursula1_stake + ursula1_reward == escrow.functions.getAllTokens(staker1).call()
assert staker1_stake + ursula1_reward == escrow.functions.getAllTokens(staker1).call()
assert staker2_stake == escrow.functions.getAllTokens(staker2).call()
ursula1_stake += ursula1_reward
staker1_stake += ursula1_reward
events = staking_log.get_all_entries()
assert 3 == len(events)
@ -251,7 +251,7 @@ def test_minting(testerchain, token, escrow_contract, token_economics):
tx = escrow.functions.mint().transact({'from': staker2})
testerchain.wait_for_receipt(tx)
ursula2_reward = calculate_reward(500, 500, 0)
assert ursula1_stake == escrow.functions.getAllTokens(staker1).call()
assert staker1_stake == escrow.functions.getAllTokens(staker1).call()
assert staker2_stake + ursula2_reward == escrow.functions.getAllTokens(staker2).call()
staker2_stake += ursula2_reward
@ -277,14 +277,14 @@ def test_minting(testerchain, token, escrow_contract, token_economics):
testerchain.wait_for_receipt(tx)
current_period = escrow.functions.getCurrentPeriod().call()
assert current_period - 2 == escrow.functions.getLastCommittedPeriod(staker1).call()
assert ursula1_stake == escrow.functions.getAllTokens(staker1).call()
assert staker1_stake == escrow.functions.getAllTokens(staker1).call()
# Staker still can't make a commitment
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.commitToNextPeriod().transact({'from': staker1})
testerchain.wait_for_receipt(tx)
# Staker(2) deposits and locks more tokens
tx = escrow.functions.deposit(250, 4).transact({'from': staker2})
tx = escrow.functions.deposit(staker2, 250, 4).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': staker2})
testerchain.wait_for_receipt(tx)
@ -310,7 +310,7 @@ def test_minting(testerchain, token, escrow_contract, token_economics):
testerchain.wait_for_receipt(tx)
ursula2_reward = calculate_reward(250, 750, 4) + calculate_reward(500, 750, 4)
assert ursula1_stake == escrow.functions.getAllTokens(staker1).call()
assert staker1_stake == escrow.functions.getAllTokens(staker1).call()
assert staker2_stake + ursula2_reward == escrow.functions.getAllTokens(staker2).call()
assert 4 == escrow.functions.getPastDowntimeLength(staker2).call()
downtime = escrow.functions.getPastDowntime(staker2, 3).call()
@ -423,7 +423,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
tx = escrow.functions.setAdjudicator(adjudicator.address).transact()
testerchain.wait_for_receipt(tx)
creator = testerchain.client.accounts[0]
ursula = testerchain.client.accounts[1]
staker = testerchain.client.accounts[1]
investigator = testerchain.client.accounts[2]
slashing_log = escrow.events.Slashed.createFilter(fromBlock='latest')
@ -435,43 +435,43 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
testerchain.wait_for_receipt(tx)
# Give Ursula deposit some tokens
tx = token.functions.transfer(ursula, 10000).transact({'from': creator})
tx = token.functions.transfer(staker, 10000).transact({'from': creator})
testerchain.wait_for_receipt(tx)
tx = token.functions.approve(escrow.address, 10000).transact({'from': ursula})
tx = token.functions.approve(escrow.address, 10000).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.deposit(100, 2).transact({'from': ursula})
tx = escrow.functions.deposit(staker, 100, 2).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(ursula).transact({'from': ursula})
tx = escrow.functions.bondWorker(staker).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.setReStake(False).transact({'from': ursula})
tx = escrow.functions.setReStake(False).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.setWindDown(True).transact({'from': ursula})
tx = escrow.functions.setWindDown(True).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
current_period = escrow.functions.getCurrentPeriod().call()
assert 100 == escrow.functions.getAllTokens(ursula).call()
assert 100 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 100 == escrow.functions.getAllTokens(staker).call()
assert 100 == escrow.functions.getLockedTokens(staker, 0).call()
assert 100 == escrow.functions.lockedPerPeriod(current_period).call()
assert 0 == escrow.functions.lockedPerPeriod(current_period + 1).call()
# Can't slash directly using the escrow contract
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.slashStaker(ursula, 100, investigator, 10).transact()
tx = escrow.functions.slashStaker(staker, 100, investigator, 10).transact()
testerchain.wait_for_receipt(tx)
# Penalty must be greater than zero
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.slashStaker(ursula, 0, investigator, 0).transact()
tx = escrow.functions.slashStaker(staker, 0, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
# Slash the whole stake
reward = escrow.functions.getReservedReward().call()
tx = adjudicator.functions.slashStaker(ursula, 100, investigator, 10).transact()
tx = adjudicator.functions.slashStaker(staker, 100, investigator, 10).transact()
testerchain.wait_for_receipt(tx)
# Staker has no more sub stakes
assert 0 == escrow.functions.getAllTokens(ursula).call()
assert 0 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 0 == escrow.functions.getAllTokens(staker).call()
assert 0 == escrow.functions.getLockedTokens(staker, 0).call()
assert 10 == token.functions.balanceOf(investigator).call()
assert 0 == escrow.functions.lockedPerPeriod(current_period).call()
assert reward + 90 == escrow.functions.getReservedReward().call()
@ -479,33 +479,33 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
events = slashing_log.get_all_entries()
assert 1 == len(events)
event_args = events[0]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert 100 == event_args['penalty']
assert investigator == event_args['investigator']
assert 10 == event_args['reward']
# New deposit and making a commitment
tx = escrow.functions.deposit(100, 5).transact({'from': ursula})
tx = escrow.functions.deposit(staker, 100, 5).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
current_period += 1
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
assert 100 == escrow.functions.getAllTokens(ursula).call()
assert 100 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 100 == escrow.functions.getLockedTokens(ursula, 1).call()
assert 100 == escrow.functions.getAllTokens(staker).call()
assert 100 == escrow.functions.getLockedTokens(staker, 0).call()
assert 100 == escrow.functions.getLockedTokens(staker, 1).call()
assert 100 == escrow.functions.lockedPerPeriod(current_period).call()
assert 100 == escrow.functions.lockedPerPeriod(current_period + 1).call()
# Slash part of one sub stake (there is only one sub stake)
reward = escrow.functions.getReservedReward().call()
tx = adjudicator.functions.slashStaker(ursula, 10, investigator, 11).transact()
tx = adjudicator.functions.slashStaker(staker, 10, investigator, 11).transact()
testerchain.wait_for_receipt(tx)
assert 90 == escrow.functions.getAllTokens(ursula).call()
assert 90 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 90 == escrow.functions.getLockedTokens(ursula, 1).call()
assert 90 == escrow.functions.getAllTokens(staker).call()
assert 90 == escrow.functions.getLockedTokens(staker, 0).call()
assert 90 == escrow.functions.getLockedTokens(staker, 1).call()
# The reward will be equal to the penalty (can't be more then penalty)
assert 20 == token.functions.balanceOf(investigator).call()
assert 90 == escrow.functions.lockedPerPeriod(current_period).call()
@ -515,32 +515,32 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
events = slashing_log.get_all_entries()
assert 2 == len(events)
event_args = events[1]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert 10 == event_args['penalty']
assert investigator == event_args['investigator']
assert 10 == event_args['reward']
# New deposit of a longer sub stake
tx = escrow.functions.deposit(100, 6).transact({'from': ursula})
tx = escrow.functions.deposit(staker, 100, 6).transact({'from': staker})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
current_period += 1
assert 90 == escrow.functions.getLockedTokensInPast(ursula, 1).call()
assert 190 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 100 == escrow.functions.getLockedTokens(ursula, 4).call()
assert 190 == escrow.functions.getAllTokens(ursula).call()
assert 90 == escrow.functions.getLockedTokensInPast(staker, 1).call()
assert 190 == escrow.functions.getLockedTokens(staker, 0).call()
assert 100 == escrow.functions.getLockedTokens(staker, 4).call()
assert 190 == escrow.functions.getAllTokens(staker).call()
assert 90 == escrow.functions.lockedPerPeriod(current_period - 1).call()
assert 190 == escrow.functions.lockedPerPeriod(current_period).call()
assert 0 == escrow.functions.lockedPerPeriod(current_period + 1).call()
# Slash again part of the first sub stake because new sub stake is longer (there are two main sub stakes)
reward = escrow.functions.getReservedReward().call()
tx = adjudicator.functions.slashStaker(ursula, 10, investigator, 0).transact()
tx = adjudicator.functions.slashStaker(staker, 10, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 180 == escrow.functions.getAllTokens(ursula).call()
assert 90 == escrow.functions.getLockedTokensInPast(ursula, 1).call()
assert 180 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 100 == escrow.functions.getLockedTokens(ursula, 4).call()
assert 180 == escrow.functions.getAllTokens(staker).call()
assert 90 == escrow.functions.getLockedTokensInPast(staker, 1).call()
assert 180 == escrow.functions.getLockedTokens(staker, 0).call()
assert 100 == escrow.functions.getLockedTokens(staker, 4).call()
assert 20 == token.functions.balanceOf(investigator).call()
assert 90 == escrow.functions.lockedPerPeriod(current_period - 1).call()
assert 180 == escrow.functions.lockedPerPeriod(current_period).call()
@ -549,33 +549,33 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
events = slashing_log.get_all_entries()
assert 3 == len(events)
event_args = events[2]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert 10 == event_args['penalty']
assert investigator == event_args['investigator']
assert 0 == event_args['reward']
# New deposit of a shorter sub stake
tx = escrow.functions.deposit(110, 2).transact({'from': ursula})
tx = escrow.functions.deposit(staker, 110, 2).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=2)
current_period += 2
assert 290 == escrow.functions.getLockedTokensInPast(ursula, 1).call()
assert 290 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 180 == escrow.functions.getLockedTokens(ursula, 2).call()
deposit = escrow.functions.getAllTokens(ursula).call() # Some reward is already minted
assert 290 == escrow.functions.getLockedTokensInPast(staker, 1).call()
assert 290 == escrow.functions.getLockedTokens(staker, 0).call()
assert 180 == escrow.functions.getLockedTokens(staker, 2).call()
deposit = escrow.functions.getAllTokens(staker).call() # Some reward is already minted
assert 290 == escrow.functions.lockedPerPeriod(current_period - 1).call()
assert 0 == escrow.functions.lockedPerPeriod(current_period).call()
# Slash only free amount of tokens
reward = escrow.functions.getReservedReward().call()
tx = adjudicator.functions.slashStaker(ursula, deposit - 290, investigator, 0).transact()
tx = adjudicator.functions.slashStaker(staker, deposit - 290, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 290 == escrow.functions.getAllTokens(ursula).call()
assert 290 == escrow.functions.getLockedTokensInPast(ursula, 1).call()
assert 290 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 180 == escrow.functions.getLockedTokens(ursula, 2).call()
assert 290 == escrow.functions.getAllTokens(staker).call()
assert 290 == escrow.functions.getLockedTokensInPast(staker, 1).call()
assert 290 == escrow.functions.getLockedTokens(staker, 0).call()
assert 180 == escrow.functions.getLockedTokens(staker, 2).call()
assert 20 == token.functions.balanceOf(investigator).call()
assert 290 == escrow.functions.lockedPerPeriod(current_period - 1).call()
assert 0 == escrow.functions.lockedPerPeriod(current_period).call()
@ -584,18 +584,18 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
events = slashing_log.get_all_entries()
assert 4 == len(events)
event_args = events[3]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert deposit - 290 == event_args['penalty']
assert investigator == event_args['investigator']
assert 0 == event_args['reward']
# Slash only the new sub stake because it's the shortest one (there are three main sub stakes)
tx = adjudicator.functions.slashStaker(ursula, 20, investigator, 0).transact()
tx = adjudicator.functions.slashStaker(staker, 20, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 270 == escrow.functions.getAllTokens(ursula).call()
assert 290 == escrow.functions.getLockedTokensInPast(ursula, 1).call()
assert 270 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 180 == escrow.functions.getLockedTokens(ursula, 2).call()
assert 270 == escrow.functions.getAllTokens(staker).call()
assert 290 == escrow.functions.getLockedTokensInPast(staker, 1).call()
assert 270 == escrow.functions.getLockedTokens(staker, 0).call()
assert 180 == escrow.functions.getLockedTokens(staker, 2).call()
assert 20 == token.functions.balanceOf(investigator).call()
assert 290 == escrow.functions.lockedPerPeriod(current_period - 1).call()
assert 0 == escrow.functions.lockedPerPeriod(current_period).call()
@ -604,19 +604,19 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
events = slashing_log.get_all_entries()
assert 5 == len(events)
event_args = events[4]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert 20 == event_args['penalty']
assert investigator == event_args['investigator']
assert 0 == event_args['reward']
# Slash the whole new sub stake and part of the next shortest (there are three main sub stakes)
reward = escrow.functions.getReservedReward().call()
tx = adjudicator.functions.slashStaker(ursula, 100, investigator, 0).transact()
tx = adjudicator.functions.slashStaker(staker, 100, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 170 == escrow.functions.getAllTokens(ursula).call()
assert 290 == escrow.functions.getLockedTokensInPast(ursula, 1).call()
assert 170 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 170 == escrow.functions.getLockedTokens(ursula, 2).call()
assert 170 == escrow.functions.getAllTokens(staker).call()
assert 290 == escrow.functions.getLockedTokensInPast(staker, 1).call()
assert 170 == escrow.functions.getLockedTokens(staker, 0).call()
assert 170 == escrow.functions.getLockedTokens(staker, 2).call()
assert 20 == token.functions.balanceOf(investigator).call()
assert 290 == escrow.functions.lockedPerPeriod(current_period - 1).call()
assert 0 == escrow.functions.lockedPerPeriod(current_period).call()
@ -625,78 +625,78 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
events = slashing_log.get_all_entries()
assert 6 == len(events)
event_args = events[5]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert 100 == event_args['penalty']
assert investigator == event_args['investigator']
assert 0 == event_args['reward']
# Making a commitment must handle correctly inactive sub stakes after slashing
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
# New deposit
tx = escrow.functions.deposit(100, 2).transact({'from': ursula})
tx = escrow.functions.deposit(staker, 100, 2).transact({'from': staker})
testerchain.wait_for_receipt(tx)
assert 170 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 270 == escrow.functions.getLockedTokens(ursula, 1).call()
assert 170 == escrow.functions.getLockedTokens(staker, 0).call()
assert 270 == escrow.functions.getLockedTokens(staker, 1).call()
assert 270 == escrow.functions.lockedPerPeriod(current_period + 1).call()
deposit = escrow.functions.getAllTokens(ursula).call() # Some reward is already minted
deposit = escrow.functions.getAllTokens(staker).call() # Some reward is already minted
unlocked_deposit = deposit - 270
reward = escrow.functions.getReservedReward().call()
# Slash the new sub stake which starts in the next period
# Because locked value is more in the next period than in the current period
tx = adjudicator.functions.slashStaker(ursula, unlocked_deposit + 10, investigator, 0).transact()
tx = adjudicator.functions.slashStaker(staker, unlocked_deposit + 10, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 170 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 260 == escrow.functions.getLockedTokens(ursula, 1).call()
assert 260 == escrow.functions.getAllTokens(ursula).call()
assert 170 == escrow.functions.getLockedTokens(staker, 0).call()
assert 260 == escrow.functions.getLockedTokens(staker, 1).call()
assert 260 == escrow.functions.getAllTokens(staker).call()
assert 260 == escrow.functions.lockedPerPeriod(current_period + 1).call()
assert reward + unlocked_deposit + 10 == escrow.functions.getReservedReward().call()
events = slashing_log.get_all_entries()
assert 7 == len(events)
event_args = events[6]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert unlocked_deposit + 10 == event_args['penalty']
assert investigator == event_args['investigator']
assert 0 == event_args['reward']
# After two periods two shortest sub stakes will be unlocked, lock again and slash after this
testerchain.time_travel(hours=1)
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
current_period += 2
assert 260 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 100 == escrow.functions.getLockedTokens(ursula, 1).call()
assert 0 == escrow.functions.getLockedTokens(ursula, 3).call()
assert 260 == escrow.functions.getLockedTokens(staker, 0).call()
assert 100 == escrow.functions.getLockedTokens(staker, 1).call()
assert 0 == escrow.functions.getLockedTokens(staker, 3).call()
assert 260 == escrow.functions.lockedPerPeriod(current_period - 1).call()
assert 260 == escrow.functions.lockedPerPeriod(current_period).call()
assert 0 == escrow.functions.lockedPerPeriod(current_period + 1).call()
tx = escrow.functions.lock(160, 2).transact({'from': ursula})
tx = escrow.functions.lock(160, 2).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
assert 260 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 260 == escrow.functions.getLockedTokens(ursula, 1).call()
assert 0 == escrow.functions.getLockedTokens(ursula, 3).call()
assert 260 == escrow.functions.getLockedTokens(staker, 0).call()
assert 260 == escrow.functions.getLockedTokens(staker, 1).call()
assert 0 == escrow.functions.getLockedTokens(staker, 3).call()
assert 260 == escrow.functions.lockedPerPeriod(current_period - 1).call()
assert 260 == escrow.functions.lockedPerPeriod(current_period).call()
assert 260 == escrow.functions.lockedPerPeriod(current_period + 1).call()
deposit = escrow.functions.getAllTokens(ursula).call() # Some reward is already minted
deposit = escrow.functions.getAllTokens(staker).call() # Some reward is already minted
unlocked_deposit = deposit - 260
# Slash two sub stakes:
# one which will be unlocked after current period and new sub stake
reward = escrow.functions.getReservedReward().call()
tx = adjudicator.functions.slashStaker(ursula, unlocked_deposit + 10, investigator, 0).transact()
tx = adjudicator.functions.slashStaker(staker, unlocked_deposit + 10, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 250 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 250 == escrow.functions.getLockedTokens(ursula, 1).call()
assert 0 == escrow.functions.getLockedTokens(ursula, 3).call()
assert 250 == escrow.functions.getAllTokens(ursula).call()
assert 250 == escrow.functions.getLockedTokens(staker, 0).call()
assert 250 == escrow.functions.getLockedTokens(staker, 1).call()
assert 0 == escrow.functions.getLockedTokens(staker, 3).call()
assert 250 == escrow.functions.getAllTokens(staker).call()
assert 260 == escrow.functions.lockedPerPeriod(current_period - 1).call()
assert 250 == escrow.functions.lockedPerPeriod(current_period).call()
assert 250 == escrow.functions.lockedPerPeriod(current_period + 1).call()
@ -705,19 +705,19 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
events = slashing_log.get_all_entries()
assert 8 == len(events)
event_args = events[7]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert unlocked_deposit + 10 == event_args['penalty']
assert investigator == event_args['investigator']
assert 0 == event_args['reward']
# Slash four sub stakes:
# two that will be unlocked after current period, new sub stake and another short sub stake
tx = adjudicator.functions.slashStaker(ursula, 90, investigator, 0).transact()
tx = adjudicator.functions.slashStaker(staker, 90, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 160 == escrow.functions.getLockedTokens(ursula, 0).call()
assert 160 == escrow.functions.getLockedTokens(ursula, 1).call()
assert 0 == escrow.functions.getLockedTokens(ursula, 3).call()
assert 160 == escrow.functions.getAllTokens(ursula).call()
assert 160 == escrow.functions.getLockedTokens(staker, 0).call()
assert 160 == escrow.functions.getLockedTokens(staker, 1).call()
assert 0 == escrow.functions.getLockedTokens(staker, 3).call()
assert 160 == escrow.functions.getAllTokens(staker).call()
assert 260 == escrow.functions.lockedPerPeriod(current_period - 1).call()
assert 160 == escrow.functions.lockedPerPeriod(current_period).call()
assert 160 == escrow.functions.lockedPerPeriod(current_period + 1).call()
@ -726,114 +726,114 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
events = slashing_log.get_all_entries()
assert 9 == len(events)
event_args = events[8]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert 90 == event_args['penalty']
assert investigator == event_args['investigator']
assert 0 == event_args['reward']
# Prepare second Ursula for tests
ursula2 = testerchain.client.accounts[3]
tx = token.functions.transfer(ursula2, 10000).transact({'from': creator})
staker2 = testerchain.client.accounts[3]
tx = token.functions.transfer(staker2, 10000).transact({'from': creator})
testerchain.wait_for_receipt(tx)
tx = token.functions.approve(escrow.address, 10000).transact({'from': ursula2})
tx = token.functions.approve(escrow.address, 10000).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
# Two deposits in consecutive periods
tx = escrow.functions.deposit(100, 4).transact({'from': ursula2})
tx = escrow.functions.deposit(staker2, 100, 4).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(ursula2).transact({'from': ursula2})
tx = escrow.functions.bondWorker(staker2).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.setWindDown(True).transact({'from': ursula2})
tx = escrow.functions.setWindDown(True).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula2})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker2})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
tx = escrow.functions.deposit(100, 2).transact({'from': ursula2})
tx = escrow.functions.deposit(staker2, 100, 2).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.setReStake(False).transact({'from': ursula2})
tx = escrow.functions.setReStake(False).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula2})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker2})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=2)
assert 100 == escrow.functions.getLockedTokensInPast(ursula2, 2).call()
assert 200 == escrow.functions.getLockedTokensInPast(ursula2, 1).call()
assert 200 == escrow.functions.getLockedTokens(ursula2, 0).call()
assert 200 == escrow.functions.getLockedTokens(ursula2, 1).call()
assert 100 == escrow.functions.getLockedTokensInPast(staker2, 2).call()
assert 200 == escrow.functions.getLockedTokensInPast(staker2, 1).call()
assert 200 == escrow.functions.getLockedTokens(staker2, 0).call()
assert 200 == escrow.functions.getLockedTokens(staker2, 1).call()
# Slash one sub stake to set the last period of this sub stake to the previous period
tx = adjudicator.functions.slashStaker(ursula2, 100, investigator, 0).transact()
tx = adjudicator.functions.slashStaker(staker2, 100, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 100 == escrow.functions.getLockedTokensInPast(ursula2, 2).call()
assert 200 == escrow.functions.getLockedTokensInPast(ursula2, 1).call()
assert 100 == escrow.functions.getLockedTokens(ursula2, 0).call()
assert 100 == escrow.functions.getLockedTokens(ursula2, 1).call()
assert 100 == escrow.functions.getLockedTokensInPast(staker2, 2).call()
assert 200 == escrow.functions.getLockedTokensInPast(staker2, 1).call()
assert 100 == escrow.functions.getLockedTokens(staker2, 0).call()
assert 100 == escrow.functions.getLockedTokens(staker2, 1).call()
events = slashing_log.get_all_entries()
assert 10 == len(events)
event_args = events[9]['args']
assert ursula2 == event_args['staker']
assert staker2 == event_args['staker']
assert 100 == event_args['penalty']
assert investigator == event_args['investigator']
assert 0 == event_args['reward']
# Slash the first sub stake
# and check that the second sub stake will not combine with the slashed amount of the first one
tx = adjudicator.functions.slashStaker(ursula2, 50, investigator, 0).transact()
tx = adjudicator.functions.slashStaker(staker2, 50, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 100 == escrow.functions.getLockedTokensInPast(ursula2, 2).call()
assert 200 == escrow.functions.getLockedTokensInPast(ursula2, 1).call()
assert 50 == escrow.functions.getLockedTokens(ursula2, 0).call()
assert 50 == escrow.functions.getLockedTokens(ursula2, 1).call()
assert 100 == escrow.functions.getLockedTokensInPast(staker2, 2).call()
assert 200 == escrow.functions.getLockedTokensInPast(staker2, 1).call()
assert 50 == escrow.functions.getLockedTokens(staker2, 0).call()
assert 50 == escrow.functions.getLockedTokens(staker2, 1).call()
events = slashing_log.get_all_entries()
assert 11 == len(events)
event_args = events[10]['args']
assert ursula2 == event_args['staker']
assert staker2 == event_args['staker']
assert 50 == event_args['penalty']
assert investigator == event_args['investigator']
assert 0 == event_args['reward']
# Prepare next case: new deposit is longer than previous sub stake
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula2})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker2})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
tx = escrow.functions.deposit(100, 3).transact({'from': ursula2})
tx = escrow.functions.deposit(staker2, 100, 3).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
assert 50 == escrow.functions.getLockedTokens(ursula2, 0).call()
assert 150 == escrow.functions.getLockedTokens(ursula2, 1).call()
assert 100 == escrow.functions.getLockedTokens(ursula2, 2).call()
assert 50 == escrow.functions.getLockedTokens(staker2, 0).call()
assert 150 == escrow.functions.getLockedTokens(staker2, 1).call()
assert 100 == escrow.functions.getLockedTokens(staker2, 2).call()
# Withdraw all not locked tokens
deposit = escrow.functions.getAllTokens(ursula2).call() # Some reward is already minted
deposit = escrow.functions.getAllTokens(staker2).call() # Some reward is already minted
unlocked_deposit = deposit - 150
tx = escrow.functions.withdraw(unlocked_deposit).transact({'from': ursula2})
tx = escrow.functions.withdraw(unlocked_deposit).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
# Slash the previous sub stake
# Stake in the current period should not change, because overflow starts from the next period
tx = adjudicator.functions.slashStaker(ursula2, 10, investigator, 0).transact()
tx = adjudicator.functions.slashStaker(staker2, 10, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 50 == escrow.functions.getLockedTokens(ursula2, 0).call()
assert 140 == escrow.functions.getLockedTokens(ursula2, 1).call()
assert 100 == escrow.functions.getLockedTokens(ursula2, 2).call()
assert 50 == escrow.functions.getLockedTokens(staker2, 0).call()
assert 140 == escrow.functions.getLockedTokens(staker2, 1).call()
assert 100 == escrow.functions.getLockedTokens(staker2, 2).call()
events = slashing_log.get_all_entries()
assert 12 == len(events)
event_args = events[11]['args']
assert ursula2 == event_args['staker']
assert staker2 == event_args['staker']
assert 10 == event_args['penalty']
assert investigator == event_args['investigator']
assert 0 == event_args['reward']
# Next test: optimization does not break saving old sub stake
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula2})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker2})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
assert 50 == escrow.functions.getLockedTokensInPast(ursula2, 1).call()
assert 140 == escrow.functions.getLockedTokens(ursula2, 0).call()
assert 100 == escrow.functions.getLockedTokens(ursula2, 1).call()
tx = adjudicator.functions.slashStaker(ursula2, 10, investigator, 0).transact()
assert 50 == escrow.functions.getLockedTokensInPast(staker2, 1).call()
assert 140 == escrow.functions.getLockedTokens(staker2, 0).call()
assert 100 == escrow.functions.getLockedTokens(staker2, 1).call()
tx = adjudicator.functions.slashStaker(staker2, 10, investigator, 0).transact()
testerchain.wait_for_receipt(tx)
assert 50 == escrow.functions.getLockedTokensInPast(ursula2, 1).call()
assert 130 == escrow.functions.getLockedTokens(ursula2, 0).call()
assert 100 == escrow.functions.getLockedTokens(ursula2, 1).call()
assert 50 == escrow.functions.getLockedTokensInPast(staker2, 1).call()
assert 130 == escrow.functions.getLockedTokens(staker2, 0).call()
assert 100 == escrow.functions.getLockedTokens(staker2, 1).call()

View File

@ -76,7 +76,7 @@ def test_staking(testerchain, token, escrow_contract):
# Can't deposit for too short a period (less than _minLockedPeriods coefficient)
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.deposit(1000, 1).transact({'from': staker1})
tx = escrow.functions.deposit(staker1, 1000, 1).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = token.functions.approveAndCall(escrow.address, 1000, testerchain.w3.toBytes(1))\
@ -85,7 +85,7 @@ def test_staking(testerchain, token, escrow_contract):
# Staker transfers some tokens to the escrow and locks them before initialization
current_period = escrow.functions.getCurrentPeriod().call()
tx = escrow.functions.deposit(1000, 2).transact({'from': staker1})
tx = escrow.functions.deposit(staker1, 1000, 2).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(staker1).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
@ -128,7 +128,7 @@ def test_staking(testerchain, token, escrow_contract):
testerchain.wait_for_receipt(tx)
# Ursula(2) stakes tokens also
tx = escrow.functions.deposit(500, 2).transact({'from': staker2})
tx = escrow.functions.deposit(staker2, 500, 2).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(staker2).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
@ -563,7 +563,7 @@ def test_max_sub_stakes(testerchain, token, escrow_contract):
testerchain.wait_for_receipt(tx)
# Lock one sub stake from current period and others from next one
tx = escrow.functions.deposit(100, 2).transact({'from': staker})
tx = escrow.functions.deposit(staker, 100, 2).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(staker).transact({'from': staker})
testerchain.wait_for_receipt(tx)
@ -575,14 +575,14 @@ def test_max_sub_stakes(testerchain, token, escrow_contract):
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
for index in range(MAX_SUB_STAKES - 1):
tx = escrow.functions.deposit(100, 2).transact({'from': staker})
tx = escrow.functions.deposit(staker, 100, 2).transact({'from': staker})
testerchain.wait_for_receipt(tx)
assert MAX_SUB_STAKES == escrow.functions.getSubStakesLength(staker).call()
assert 3000 == escrow.functions.getLockedTokens(staker, 1).call()
# Can't lock more because of reaching the maximum number of active sub stakes
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.deposit(100, 2).transact({'from': staker})
tx = escrow.functions.deposit(staker, 100, 2).transact({'from': staker})
testerchain.wait_for_receipt(tx)
# After two periods first sub stake will be unlocked and we can lock again
@ -597,11 +597,11 @@ def test_max_sub_stakes(testerchain, token, escrow_contract):
assert MAX_SUB_STAKES == escrow.functions.getSubStakesLength(staker).call()
# Before sub stake will be inactive it must be rewarded
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.deposit(100, 2).transact({'from': staker})
tx = escrow.functions.deposit(staker, 100, 2).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.mint().transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.deposit(100, 2).transact({'from': staker})
tx = escrow.functions.deposit(staker, 100, 2).transact({'from': staker})
testerchain.wait_for_receipt(tx)
assert 2900 == escrow.functions.getLockedTokens(staker, 0).call()
assert 100 == escrow.functions.getLockedTokens(staker, 1).call()
@ -609,7 +609,7 @@ def test_max_sub_stakes(testerchain, token, escrow_contract):
# Can't lock more because of reaching the maximum number of active sub stakes and they are not rewarded yet
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.deposit(100, 2).transact({'from': staker})
tx = escrow.functions.deposit(staker, 100, 2).transact({'from': staker})
testerchain.wait_for_receipt(tx)
@ -646,14 +646,14 @@ def test_allowable_locked_tokens(testerchain, token_economics, token, escrow_con
testerchain.wait_for_receipt(tx)
# Staker can't deposit and lock too low value (less than _minAllowableLockedTokens coefficient)
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.deposit(minimum_allowed - 1, duration).transact({'from': staker1})
tx = escrow.functions.deposit(staker1, minimum_allowed - 1, duration).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
# And can't deposit and lock too high value (more than _maxAllowableLockedTokens coefficient)
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.deposit(maximum_allowed + 1, duration).transact({'from': staker1})
tx = escrow.functions.deposit(staker1, maximum_allowed + 1, duration).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.deposit(minimum_allowed, duration).transact({'from': staker1})
tx = escrow.functions.deposit(staker1, minimum_allowed, duration).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
staker1_lock = minimum_allowed
tx = token.functions.approve(escrow.address, 0).transact({'from': staker1})
@ -680,7 +680,7 @@ def test_allowable_locked_tokens(testerchain, token_economics, token, escrow_con
tx = token.functions.approve(escrow.address, minimum_allowed).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = escrow.functions.deposit(minimum_allowed, 2 * duration).transact({'from': staker2})
tx = escrow.functions.deposit(staker2, minimum_allowed, 2 * duration).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
# Prepare for testing `lock()`: staker makes new sub-staker and unlocks first sub-stake

View File

@ -103,7 +103,7 @@ def test_upgrading(testerchain, token, token_economics, deploy_contract):
balance = token.functions.balanceOf(staker).call()
tx = token.functions.approve(contract.address, balance).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = contract.functions.deposit(balance, 1000).transact({'from': staker})
tx = contract.functions.deposit(staker, balance, 1000).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = contract.functions.setReStake(True).transact({'from': staker})
testerchain.wait_for_receipt(tx)
@ -286,7 +286,7 @@ def test_re_stake(testerchain, token, escrow_contract):
escrow = escrow_contract(10000)
creator = testerchain.client.accounts[0]
staker = testerchain.client.accounts[1]
ursula2 = testerchain.client.accounts[2]
staker2 = testerchain.client.accounts[2]
re_stake_log = escrow.events.ReStakeSet.createFilter(fromBlock='latest')
re_stake_lock_log = escrow.events.ReStakeLocked.createFilter(fromBlock='latest')
@ -351,7 +351,7 @@ def test_re_stake(testerchain, token, escrow_contract):
tx = token.functions.approve(escrow.address, 10000).transact({'from': staker})
testerchain.wait_for_receipt(tx)
sub_stake = 100
tx = escrow.functions.deposit(sub_stake, 10).transact({'from': staker})
tx = escrow.functions.deposit(staker, sub_stake, 10).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(staker).transact({'from': staker})
testerchain.wait_for_receipt(tx)
@ -453,34 +453,34 @@ def test_re_stake(testerchain, token, escrow_contract):
sub_stake_2 = sub_stake_1 // 2
stake = sub_stake_1 + sub_stake_2
sub_stake_duration = escrow.functions.getSubStakeInfo(staker, 0).call()[2]
tx = escrow.functions.deposit(sub_stake_2, sub_stake_duration).transact({'from': staker})
tx = escrow.functions.deposit(staker, sub_stake_2, sub_stake_duration).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = token.functions.transfer(ursula2, stake).transact({'from': creator})
tx = token.functions.transfer(staker2, stake).transact({'from': creator})
testerchain.wait_for_receipt(tx)
tx = token.functions.approve(escrow.address, stake).transact({'from': ursula2})
tx = token.functions.approve(escrow.address, stake).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.deposit(stake, sub_stake_duration).transact({'from': ursula2})
tx = escrow.functions.deposit(staker2, stake, sub_stake_duration).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(ursula2).transact({'from': ursula2})
tx = escrow.functions.bondWorker(staker2).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.setReStake(False).transact({'from': ursula2})
tx = escrow.functions.setReStake(False).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula2})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker2})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=1)
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula2})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker2})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=2)
# Checks preparation
period = escrow.functions.getCurrentPeriod().call()
assert stake == escrow.functions.getAllTokens(staker).call()
assert stake == escrow.functions.getAllTokens(ursula2).call()
assert stake == escrow.functions.getAllTokens(staker2).call()
assert stake == escrow.functions.getLockedTokens(staker, 0).call()
assert stake == escrow.functions.getLockedTokens(ursula2, 0).call()
assert stake == escrow.functions.getLockedTokens(staker2, 0).call()
assert sub_stake_1 == escrow.functions.getSubStakeInfo(staker, 0).call()[3]
assert sub_stake_2 == escrow.functions.getSubStakeInfo(staker, 1).call()[3]
assert 2 * stake == escrow.functions.lockedPerPeriod(period - 2).call()
@ -490,16 +490,16 @@ def test_re_stake(testerchain, token, escrow_contract):
# Compare minting with re-stake and without for two surpassed periods
# The first is Ursula2 because of Ursula1's re-stake will change sub stake ratio for `period - 1`
# (stake/lockedPerPeriod) and it will affect next minting
tx = escrow.functions.mint().transact({'from': ursula2})
tx = escrow.functions.mint().transact({'from': staker2})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.mint().transact({'from': staker})
testerchain.wait_for_receipt(tx)
ursula_reward = escrow.functions.getAllTokens(staker).call() - stake
ursula2_reward = escrow.functions.getAllTokens(ursula2).call() - stake
ursula2_reward = escrow.functions.getAllTokens(staker2).call() - stake
assert 0 < ursula2_reward
assert ursula_reward > ursula2_reward
# Ursula2's stake has not changed
assert stake == escrow.functions.getLockedTokens(ursula2, 0).call()
assert stake == escrow.functions.getLockedTokens(staker2, 0).call()
# To calculate amount of re-stake we can split Ursula1's reward according sub stakes ratio:
# first sub stake is 2/3 of entire stake and second sub stake is 1/3
@ -561,7 +561,7 @@ def test_re_stake(testerchain, token, escrow_contract):
@pytest.mark.slow
def test_worker(testerchain, token, escrow_contract, deploy_contract):
escrow = escrow_contract(10000, disable_reward=True)
creator, ursula1, ursula2, ursula3, worker1, worker2, worker3, *everyone_else = \
creator, staker1, staker2, ursula3, worker1, worker2, worker3, *everyone_else = \
testerchain.client.accounts
worker_log = escrow.events.WorkerBonded.createFilter(fromBlock='latest')
@ -580,7 +580,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
duration = 100
tx = token.functions.transfer(intermediary1.address, sub_stake).transact()
testerchain.wait_for_receipt(tx)
tx = intermediary1.functions.deposit(sub_stake, duration).transact({'from': ursula1})
tx = intermediary1.functions.deposit(sub_stake, duration).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
assert sub_stake == escrow.functions.getAllTokens(intermediary1.address).call()
assert NULL_ADDRESS == escrow.functions.getWorkerFromStaker(intermediary1.address).call()
@ -588,7 +588,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
tx = token.functions.transfer(intermediary2.address, sub_stake).transact()
testerchain.wait_for_receipt(tx)
tx = intermediary2.functions.deposit(sub_stake, duration).transact({'from': ursula2})
tx = intermediary2.functions.deposit(sub_stake, duration).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
assert sub_stake == escrow.functions.getAllTokens(intermediary2.address).call()
assert NULL_ADDRESS == escrow.functions.getWorkerFromStaker(intermediary2.address).call()
@ -605,16 +605,16 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
# Ursula can't make a commitment because there is no worker by default
with pytest.raises((TransactionFailed, ValueError)):
tx = intermediary1.functions.commitToNextPeriod().transact({'from': ursula1})
tx = intermediary1.functions.commitToNextPeriod().transact({'from': staker1})
testerchain.wait_for_receipt(tx)
# Ursula can't bond another staker as worker
with pytest.raises((TransactionFailed, ValueError)):
tx = intermediary1.functions.bondWorker(ursula3).transact({'from': ursula1})
tx = intermediary1.functions.bondWorker(ursula3).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
# Ursula bond worker and now worker can make a commitments
tx = intermediary1.functions.bondWorker(worker1).transact({'from': ursula1})
tx = intermediary1.functions.bondWorker(worker1).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
assert worker1 == escrow.functions.getWorkerFromStaker(intermediary1.address).call()
assert intermediary1.address == escrow.functions.stakerFromWorker(worker1).call()
@ -631,11 +631,11 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
# Only worker can make a commitment
with pytest.raises((TransactionFailed, ValueError)):
tx = intermediary1.functions.bondWorker(ursula3).transact({'from': ursula1})
tx = intermediary1.functions.bondWorker(ursula3).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
# Worker is in use so other stakers can't bond him
with pytest.raises((TransactionFailed, ValueError)):
tx = intermediary2.functions.bondWorker(worker1).transact({'from': ursula2})
tx = intermediary2.functions.bondWorker(worker1).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
# Worker can't be a staker
@ -648,17 +648,17 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
# Can't bond worker twice too soon
with pytest.raises((TransactionFailed, ValueError)):
tx = intermediary1.functions.bondWorker(worker2).transact({'from': ursula1})
tx = intermediary1.functions.bondWorker(worker2).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
# She can't unbond her worker too, until enough time has passed
with pytest.raises((TransactionFailed, ValueError)):
tx = intermediary1.functions.bondWorker(NULL_ADDRESS).transact({'from': ursula1})
tx = intermediary1.functions.bondWorker(NULL_ADDRESS).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
# Let's advance one period and unbond the worker
testerchain.time_travel(hours=1)
tx = intermediary1.functions.bondWorker(NULL_ADDRESS).transact({'from': ursula1})
tx = intermediary1.functions.bondWorker(NULL_ADDRESS).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
assert NULL_ADDRESS == escrow.functions.getWorkerFromStaker(intermediary1.address).call()
@ -673,7 +673,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
assert escrow.functions.getCurrentPeriod().call() == event_args['startPeriod']
# The staker can bond now a new worker, without waiting additional time.
tx = intermediary1.functions.bondWorker(worker2).transact({'from': ursula1})
tx = intermediary1.functions.bondWorker(worker2).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
assert worker2 == escrow.functions.getWorkerFromStaker(intermediary1.address).call()
assert intermediary1.address == escrow.functions.stakerFromWorker(worker2).call()
@ -696,7 +696,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
testerchain.wait_for_receipt(tx)
# Another staker can bond a free worker
tx = intermediary2.functions.bondWorker(worker1).transact({'from': ursula2})
tx = intermediary2.functions.bondWorker(worker1).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
assert worker1 == escrow.functions.getWorkerFromStaker(intermediary2.address).call()
assert intermediary2.address == escrow.functions.stakerFromWorker(worker1).call()
@ -717,10 +717,10 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
# Bond worker again
testerchain.time_travel(hours=1)
tx = intermediary2.functions.bondWorker(ursula2).transact({'from': ursula2})
tx = intermediary2.functions.bondWorker(staker2).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
assert ursula2 == escrow.functions.getWorkerFromStaker(intermediary2.address).call()
assert intermediary2.address == escrow.functions.stakerFromWorker(ursula2).call()
assert staker2 == escrow.functions.getWorkerFromStaker(intermediary2.address).call()
assert intermediary2.address == escrow.functions.stakerFromWorker(staker2).call()
assert NULL_ADDRESS == escrow.functions.stakerFromWorker(worker1).call()
number_of_events += 1
@ -728,7 +728,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
assert number_of_events == len(events)
event_args = events[-1]['args']
assert intermediary2.address == event_args['staker']
assert ursula2 == event_args['worker']
assert staker2 == event_args['worker']
assert escrow.functions.getCurrentPeriod().call() == event_args['startPeriod']
# The first worker is free and can deposit tokens and become a staker
@ -742,7 +742,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
# Ursula can't bond the first worker again because worker is a staker now
testerchain.time_travel(hours=1)
with pytest.raises((TransactionFailed, ValueError)):
tx = intermediary1.functions.bondWorker(worker1).transact({'from': ursula1})
tx = intermediary1.functions.bondWorker(worker1).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
# Ursula without intermediary contract can bond itself as worker
@ -804,7 +804,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
@pytest.mark.slow
def test_measure_work(testerchain, token, escrow_contract, deploy_contract):
escrow = escrow_contract(10000)
creator, ursula, *everyone_else = testerchain.w3.eth.accounts
creator, staker, *everyone_else = testerchain.w3.eth.accounts
work_measurement_log = escrow.events.WorkMeasurementSet.createFilter(fromBlock='latest')
# Initialize escrow contract
@ -822,77 +822,77 @@ def test_measure_work(testerchain, token, escrow_contract, deploy_contract):
# Prepare Ursula
stake = 1000
duration = 100
tx = token.functions.transfer(ursula, stake).transact()
tx = token.functions.transfer(staker, stake).transact()
testerchain.wait_for_receipt(tx)
tx = token.functions.approve(escrow.address, stake).transact({'from': ursula})
tx = token.functions.approve(escrow.address, stake).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.deposit(stake, duration).transact({'from': ursula})
tx = escrow.functions.deposit(staker, stake, duration).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(ursula).transact({'from': ursula})
tx = escrow.functions.bondWorker(staker).transact({'from': staker})
testerchain.wait_for_receipt(tx)
assert escrow.functions.getCompletedWork(ursula).call() == 0
assert escrow.functions.getCompletedWork(staker).call() == 0
# Make a commitment and mint to check that work is not measured by default
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=2)
tx = escrow.functions.mint().transact({'from': ursula})
tx = escrow.functions.mint().transact({'from': staker})
testerchain.wait_for_receipt(tx)
assert escrow.functions.getAllTokens(ursula).call() > stake
assert escrow.functions.getCompletedWork(ursula).call() == 0
assert escrow.functions.getAllTokens(staker).call() > stake
assert escrow.functions.getCompletedWork(staker).call() == 0
# Start work measurement
stake = escrow.functions.getAllTokens(ursula).call()
tx = worklock.functions.setWorkMeasurement(ursula, True).transact()
stake = escrow.functions.getAllTokens(staker).call()
tx = worklock.functions.setWorkMeasurement(staker, True).transact()
testerchain.wait_for_receipt(tx)
events = work_measurement_log.get_all_entries()
assert 1 == len(events)
event_args = events[0]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert event_args['measureWork']
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=2)
tx = escrow.functions.mint().transact({'from': ursula})
tx = escrow.functions.mint().transact({'from': staker})
testerchain.wait_for_receipt(tx)
reward = escrow.functions.getAllTokens(ursula).call() - stake
reward = escrow.functions.getAllTokens(staker).call() - stake
assert reward > 0
assert escrow.functions.getCompletedWork(ursula).call() == reward
assert escrow.functions.getCompletedWork(staker).call() == reward
# Mint again and check work done
stake = escrow.functions.getAllTokens(ursula).call()
work_done = escrow.functions.getCompletedWork(ursula).call()
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
stake = escrow.functions.getAllTokens(staker).call()
work_done = escrow.functions.getCompletedWork(staker).call()
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=2)
tx = escrow.functions.mint().transact({'from': ursula})
tx = escrow.functions.mint().transact({'from': staker})
testerchain.wait_for_receipt(tx)
reward = escrow.functions.getAllTokens(ursula).call() - stake
reward = escrow.functions.getAllTokens(staker).call() - stake
assert reward > 0
assert escrow.functions.getCompletedWork(ursula).call() == work_done + reward
assert escrow.functions.getCompletedWork(staker).call() == work_done + reward
# Stop work measurement
stake = escrow.functions.getAllTokens(ursula).call()
work_done = escrow.functions.getCompletedWork(ursula).call()
tx = worklock.functions.setWorkMeasurement(ursula, False).transact()
stake = escrow.functions.getAllTokens(staker).call()
work_done = escrow.functions.getCompletedWork(staker).call()
tx = worklock.functions.setWorkMeasurement(staker, False).transact()
testerchain.wait_for_receipt(tx)
events = work_measurement_log.get_all_entries()
assert 2 == len(events)
event_args = events[1]['args']
assert ursula == event_args['staker']
assert staker == event_args['staker']
assert not event_args['measureWork']
tx = escrow.functions.commitToNextPeriod().transact({'from': ursula})
tx = escrow.functions.commitToNextPeriod().transact({'from': staker})
testerchain.wait_for_receipt(tx)
testerchain.time_travel(hours=2)
tx = escrow.functions.mint().transact({'from': ursula})
tx = escrow.functions.mint().transact({'from': staker})
testerchain.wait_for_receipt(tx)
reward = escrow.functions.getAllTokens(ursula).call() - stake
reward = escrow.functions.getAllTokens(staker).call() - stake
assert reward > 0
assert escrow.functions.getCompletedWork(ursula).call() == work_done
assert escrow.functions.getCompletedWork(staker).call() == work_done
@pytest.mark.slow
@ -936,7 +936,7 @@ def test_wind_down(testerchain, token, escrow_contract, token_economics):
testerchain.wait_for_receipt(tx)
tx = token.functions.approve(escrow.address, sub_stake).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.deposit(sub_stake, duration).transact({'from': staker})
tx = escrow.functions.deposit(staker, sub_stake, duration).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.bondWorker(staker).transact({'from': staker})
testerchain.wait_for_receipt(tx)
@ -1075,7 +1075,7 @@ def test_wind_down(testerchain, token, escrow_contract, token_economics):
duration = 3
tx = token.functions.approve(escrow.address, sub_stake).transact({'from': staker})
testerchain.wait_for_receipt(tx)
tx = escrow.functions.deposit(sub_stake, duration).transact({'from': staker})
tx = escrow.functions.deposit(staker, sub_stake, duration).transact({'from': staker})
testerchain.wait_for_receipt(tx)
def check_first_sub_stake(first_duration: int):
@ -1323,7 +1323,7 @@ def test_snapshots(testerchain, token, escrow_contract):
tx = token.functions.approve(escrow.address, 10000).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
initial_deposit = 100
tx = escrow.functions.deposit(initial_deposit, 10).transact({'from': staker1})
tx = escrow.functions.deposit(staker1, initial_deposit, 10).transact({'from': staker1})
testerchain.wait_for_receipt(tx)
expected_staker1_balance.add_value(initial_deposit)
@ -1433,7 +1433,7 @@ def test_snapshots(testerchain, token, escrow_contract):
tx = token.functions.approve(escrow.address, 10000).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
deposit_staker2 = 100
tx = escrow.functions.deposit(deposit_staker2, 10).transact({'from': staker2})
tx = escrow.functions.deposit(staker2, deposit_staker2, 10).transact({'from': staker2})
testerchain.wait_for_receipt(tx)
assert deposit_staker2 == escrow.functions.getAllTokens(staker2).call()

View File

@ -185,9 +185,9 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
print(tabulate.tabulate(rows, headers=headers, tablefmt="simple"), end="\n\n")
# Accounts
origin, ursula1, ursula2, ursula3, alice1, alice2, *everyone_else = testerchain.client.accounts
origin, staker1, staker2, staker3, alice1, alice2, *everyone_else = testerchain.client.accounts
ursula_with_stamp = mock_ursula(testerchain, ursula1)
ursula_with_stamp = mock_ursula(testerchain, staker1)
# Contracts
token_agent = NucypherTokenAgent(registry=registry)
@ -219,18 +219,18 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
#
# Give Ursula and Alice some coins
#
transact_and_log("Transfer tokens", token_functions.transfer(ursula1, MIN_ALLOWED_LOCKED * 10), {'from': origin})
transact(token_functions.transfer(ursula2, MIN_ALLOWED_LOCKED * 10), {'from': origin})
transact(token_functions.transfer(ursula3, MIN_ALLOWED_LOCKED * 10), {'from': origin})
transact_and_log("Transfer tokens", token_functions.transfer(staker1, MIN_ALLOWED_LOCKED * 10), {'from': origin})
transact(token_functions.transfer(staker2, MIN_ALLOWED_LOCKED * 10), {'from': origin})
transact(token_functions.transfer(staker3, MIN_ALLOWED_LOCKED * 10), {'from': origin})
#
# Ursula and Alice give Escrow rights to transfer
#
transact_and_log("Approving transfer",
token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 6),
{'from': ursula1})
transact(token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 6), {'from': ursula2})
transact(token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 6), {'from': ursula3})
{'from': staker1})
transact(token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 6), {'from': staker2})
transact(token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 6), {'from': staker3})
#
# Batch deposit tokens
@ -263,46 +263,46 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
# Ursula and Alice transfer some tokens to the escrow and lock them
#
transact_and_log("Initial deposit tokens, first",
staker_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS),
{'from': ursula1})
staker_functions.deposit(staker1, MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS),
{'from': staker1})
transact_and_log("Initial deposit tokens, other",
staker_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS),
{'from': ursula2})
transact(staker_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS), {'from': ursula3})
staker_functions.deposit(staker2, MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS),
{'from': staker2})
transact(staker_functions.deposit(staker3, MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS), {'from': staker3})
transact(staker_functions.bondWorker(ursula1), {'from': ursula1})
transact(staker_functions.bondWorker(ursula2), {'from': ursula2})
transact(staker_functions.bondWorker(ursula3), {'from': ursula3})
transact(staker_functions.setReStake(False), {'from': ursula1})
transact(staker_functions.setReStake(False), {'from': ursula2})
transact(staker_functions.setWindDown(True), {'from': ursula1})
transact(staker_functions.setWindDown(True), {'from': ursula2})
transact(staker_functions.commitToNextPeriod(), {'from': ursula1})
transact(staker_functions.commitToNextPeriod(), {'from': ursula2})
transact(staker_functions.bondWorker(staker1), {'from': staker1})
transact(staker_functions.bondWorker(staker2), {'from': staker2})
transact(staker_functions.bondWorker(staker3), {'from': staker3})
transact(staker_functions.setReStake(False), {'from': staker1})
transact(staker_functions.setReStake(False), {'from': staker2})
transact(staker_functions.setWindDown(True), {'from': staker1})
transact(staker_functions.setWindDown(True), {'from': staker2})
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
transact(staker_functions.commitToNextPeriod(), {'from': staker2})
#
# Wait 1 period and make a commitment
#
testerchain.time_travel(periods=1)
transact_and_log("Make a commitment, first", staker_functions.commitToNextPeriod(), {'from': ursula1})
transact_and_log("Make a commitment, other", staker_functions.commitToNextPeriod(), {'from': ursula2})
transact_and_log("Make a commitment, first", staker_functions.commitToNextPeriod(), {'from': staker1})
transact_and_log("Make a commitment, other", staker_functions.commitToNextPeriod(), {'from': staker2})
#
# Wait 1 period and mint tokens
#
testerchain.time_travel(periods=1)
transact_and_log("Minting (1 stake), first", staker_functions.mint(), {'from': ursula1})
transact_and_log("Minting (1 stake), other", staker_functions.mint(), {'from': ursula2})
transact_and_log("Make a commitment again, first", staker_functions.commitToNextPeriod(), {'from': ursula1})
transact_and_log("Make a commitment again, other", staker_functions.commitToNextPeriod(), {'from': ursula2})
transact(staker_functions.commitToNextPeriod(), {'from': ursula3})
transact_and_log("Minting (1 stake), first", staker_functions.mint(), {'from': staker1})
transact_and_log("Minting (1 stake), other", staker_functions.mint(), {'from': staker2})
transact_and_log("Make a commitment again, first", staker_functions.commitToNextPeriod(), {'from': staker1})
transact_and_log("Make a commitment again, other", staker_functions.commitToNextPeriod(), {'from': staker2})
transact(staker_functions.commitToNextPeriod(), {'from': staker3})
#
# Commit again
#
testerchain.time_travel(periods=1)
transact_and_log("Make a commitment + mint, first", staker_functions.commitToNextPeriod(), {'from': ursula1})
transact_and_log("Make a commitment + mint, other", staker_functions.commitToNextPeriod(), {'from': ursula2})
transact_and_log("Make a commitment + mint, first", staker_functions.commitToNextPeriod(), {'from': staker1})
transact_and_log("Make a commitment + mint, other", staker_functions.commitToNextPeriod(), {'from': staker2})
#
# Create policy
@ -316,59 +316,59 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
current_timestamp = testerchain.w3.eth.getBlock(block_identifier='latest').timestamp
end_timestamp = current_timestamp + (number_of_periods - 1) * one_period
transact_and_log("Creating policy (1 node, 10 periods, pre-committed), first",
policy_functions.createPolicy(policy_id_1, alice1, end_timestamp, [ursula1]),
policy_functions.createPolicy(policy_id_1, alice1, end_timestamp, [staker1]),
{'from': alice1, 'value': value})
transact_and_log("Creating policy (1 node, 10 periods, pre-committed), other",
policy_functions.createPolicy(policy_id_2, alice1, end_timestamp, [ursula1]),
policy_functions.createPolicy(policy_id_2, alice1, end_timestamp, [staker1]),
{'from': alice1, 'value': value})
#
# Get locked tokens
#
transact_and_log("Getting locked tokens", staker_functions.getLockedTokens(ursula1, 0), {})
transact_and_log("Getting locked tokens", staker_functions.getLockedTokens(staker1, 0), {})
#
# Wait 1 period and withdraw tokens
#
testerchain.time_travel(periods=1)
transact_and_log("Withdraw", staker_functions.withdraw(1), {'from': ursula1})
transact_and_log("Withdraw", staker_functions.withdraw(1), {'from': staker1})
#
# Make a commitment with re-stake
#
transact(staker_functions.setReStake(True), {'from': ursula1})
transact(staker_functions.setReStake(True), {'from': ursula2})
transact(staker_functions.setReStake(True), {'from': staker1})
transact(staker_functions.setReStake(True), {'from': staker2})
# Used to remove spending for first call in a day for mint and commitToNextPeriod
transact(staker_functions.commitToNextPeriod(), {'from': ursula3})
transact(staker_functions.commitToNextPeriod(), {'from': staker3})
transact_and_log("Make a commitment + mint + re-stake",
staker_functions.commitToNextPeriod(),
{'from': ursula2})
{'from': staker2})
transact_and_log("Make a commitment + mint + re-stake + first fee + first fee rate",
staker_functions.commitToNextPeriod(),
{'from': ursula1})
{'from': staker1})
transact(staker_functions.setReStake(False), {'from': ursula1})
transact(staker_functions.setReStake(False), {'from': ursula2})
transact(staker_functions.setReStake(False), {'from': staker1})
transact(staker_functions.setReStake(False), {'from': staker2})
#
# Wait 2 periods and make a commitment after downtime
#
testerchain.time_travel(periods=2)
transact(staker_functions.commitToNextPeriod(), {'from': ursula3})
transact_and_log("Make a commitment after downtime", staker_functions.commitToNextPeriod(), {'from': ursula2})
transact(staker_functions.commitToNextPeriod(), {'from': staker3})
transact_and_log("Make a commitment after downtime", staker_functions.commitToNextPeriod(), {'from': staker2})
transact_and_log("Make a commitment after downtime + updating fee",
staker_functions.commitToNextPeriod(),
{'from': ursula1})
{'from': staker1})
#
# Ursula and Alice deposit some tokens to the escrow again
#
transact_and_log("Deposit tokens after making a commitment",
staker_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS),
{'from': ursula1})
transact(staker_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS), {'from': ursula2})
staker_functions.deposit(staker1, MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS),
{'from': staker1})
transact(staker_functions.deposit(staker2, MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS), {'from': staker2})
#
# Revoke policy
@ -391,23 +391,23 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
current_timestamp = testerchain.w3.eth.getBlock(block_identifier='latest').timestamp
end_timestamp = current_timestamp + (number_of_periods - 1) * one_period
transact_and_log("Creating policy (3 nodes, 100 periods, pre-committed), first",
policy_functions.createPolicy(policy_id_1, alice1, end_timestamp, [ursula1, ursula2, ursula3]),
policy_functions.createPolicy(policy_id_1, alice1, end_timestamp, [staker1, staker2, staker3]),
{'from': alice1, 'value': value})
transact_and_log("Creating policy (3 nodes, 100 periods, pre-committed), other",
policy_functions.createPolicy(policy_id_2, alice1, end_timestamp, [ursula1, ursula2, ursula3]),
policy_functions.createPolicy(policy_id_2, alice1, end_timestamp, [staker1, staker2, staker3]),
{'from': alice1, 'value': value})
value = 2 * number_of_periods * rate
transact_and_log("Creating policy (2 nodes, 100 periods, pre-committed), other",
policy_functions.createPolicy(policy_id_3, alice1, end_timestamp, [ursula1, ursula2]),
policy_functions.createPolicy(policy_id_3, alice1, end_timestamp, [staker1, staker2]),
{'from': alice1, 'value': value})
#
# Wait 1 period and mint tokens
#
testerchain.time_travel(periods=1)
transact(staker_functions.mint(), {'from': ursula3})
transact_and_log("Last minting + updating fee + updating fee rate", staker_functions.mint(), {'from': ursula1})
transact_and_log("Last minting + first fee + first fee rate", staker_functions.mint(), {'from': ursula2})
transact(staker_functions.mint(), {'from': staker3})
transact_and_log("Last minting + updating fee + updating fee rate", staker_functions.mint(), {'from': staker1})
transact_and_log("Last minting + first fee + first fee rate", staker_functions.mint(), {'from': staker2})
#
# Create policy again without pre-committed nodes
@ -420,30 +420,30 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
current_timestamp = testerchain.w3.eth.getBlock(block_identifier='latest').timestamp
end_timestamp = current_timestamp + (number_of_periods - 1) * one_period
transact_and_log("Creating policy (1 node, 100 periods)",
policy_functions.createPolicy(policy_id_1, alice2, end_timestamp, [ursula2]),
policy_functions.createPolicy(policy_id_1, alice2, end_timestamp, [staker2]),
{'from': alice1, 'value': value})
testerchain.time_travel(periods=1)
current_timestamp = testerchain.w3.eth.getBlock(block_identifier='latest').timestamp
end_timestamp = current_timestamp + (number_of_periods - 1) * one_period
transact_and_log("Creating policy (1 node, 100 periods), next period",
policy_functions.createPolicy(policy_id_2, alice2, end_timestamp, [ursula2]),
policy_functions.createPolicy(policy_id_2, alice2, end_timestamp, [staker2]),
{'from': alice1, 'value': value})
transact_and_log("Creating policy (1 node, 100 periods), another node",
policy_functions.createPolicy(policy_id_3, alice2, end_timestamp, [ursula1]),
policy_functions.createPolicy(policy_id_3, alice2, end_timestamp, [staker1]),
{'from': alice1, 'value': value})
#
# Mint and revoke policy
#
testerchain.time_travel(periods=10)
transact(staker_functions.commitToNextPeriod(), {'from': ursula1})
transact(staker_functions.commitToNextPeriod(), {'from': ursula3})
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
transact(staker_functions.commitToNextPeriod(), {'from': staker3})
testerchain.time_travel(periods=2)
transact(staker_functions.mint(), {'from': ursula3})
transact(staker_functions.mint(), {'from': staker3})
transact_and_log("Last minting after downtime + updating fee",
staker_functions.mint(),
{'from': ursula1})
{'from': staker1})
testerchain.time_travel(periods=10)
transact_and_log("Revoking policy after downtime, 1st policy",
@ -457,54 +457,54 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
{'from': alice2})
for index in range(5):
transact(staker_functions.commitToNextPeriod(), {'from': ursula1})
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
testerchain.time_travel(periods=1)
transact(staker_functions.mint(), {'from': ursula1})
transact(staker_functions.mint(), {'from': staker1})
#
# Check regular deposit
#
transact_and_log("Deposit tokens",
staker_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS),
{'from': ursula1})
staker_functions.deposit(staker1, MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS),
{'from': staker1})
#
# ApproveAndCall
#
testerchain.time_travel(periods=1)
transact(staker_functions.mint(), {'from': ursula1})
transact(staker_functions.mint(), {'from': staker1})
transact_and_log("ApproveAndCall",
token_functions.approveAndCall(staking_agent.contract_address,
MIN_ALLOWED_LOCKED * 2,
web3.toBytes(MIN_LOCKED_PERIODS)),
{'from': ursula1})
{'from': staker1})
#
# Locking tokens
#
testerchain.time_travel(periods=1)
transact(staker_functions.commitToNextPeriod(), {'from': ursula1})
transact_and_log("Locking tokens", staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS), {'from': ursula1})
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
transact_and_log("Locking tokens", staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS), {'from': staker1})
#
# Divide stake
#
transact_and_log("Divide stake", staker_functions.divideStake(1, MIN_ALLOWED_LOCKED, 2), {'from': ursula1})
transact(staker_functions.divideStake(3, MIN_ALLOWED_LOCKED, 2), {'from': ursula1})
transact_and_log("Divide stake", staker_functions.divideStake(1, MIN_ALLOWED_LOCKED, 2), {'from': staker1})
transact(staker_functions.divideStake(3, MIN_ALLOWED_LOCKED, 2), {'from': staker1})
#
# Divide almost finished stake
#
testerchain.time_travel(periods=1)
transact(staker_functions.commitToNextPeriod(), {'from': ursula1})
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
testerchain.time_travel(periods=1)
transact(staker_functions.commitToNextPeriod(), {'from': ursula1})
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
#
# Slashing tests
#
transact(staker_functions.commitToNextPeriod(), {'from': ursula1})
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
testerchain.time_travel(periods=1)
#
@ -513,50 +513,50 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
slashing_args = generate_args_for_slashing(ursula_with_stamp)
transact_and_log("Slash just value", adjudicator_functions.evaluateCFrag(*slashing_args), {'from': alice1})
deposit = staker_functions.stakerInfo(ursula1).call()[0]
unlocked = deposit - staker_functions.getLockedTokens(ursula1, 0).call()
transact(staker_functions.withdraw(unlocked), {'from': ursula1})
deposit = staker_functions.stakerInfo(staker1).call()[0]
unlocked = deposit - staker_functions.getLockedTokens(staker1, 0).call()
transact(staker_functions.withdraw(unlocked), {'from': staker1})
sub_stakes_length = str(staker_functions.getSubStakesLength(ursula1).call())
sub_stakes_length = str(staker_functions.getSubStakesLength(staker1).call())
slashing_args = generate_args_for_slashing(ursula_with_stamp)
transact_and_log("Slashing one sub stake and saving old one (" + sub_stakes_length + " sub stakes), 1st",
adjudicator_functions.evaluateCFrag(*slashing_args),
{'from': alice1})
sub_stakes_length = str(staker_functions.getSubStakesLength(ursula1).call())
sub_stakes_length = str(staker_functions.getSubStakesLength(staker1).call())
slashing_args = generate_args_for_slashing(ursula_with_stamp)
transact_and_log("Slashing one sub stake and saving old one (" + sub_stakes_length + " sub stakes), 2nd",
adjudicator_functions.evaluateCFrag(*slashing_args),
{'from': alice1})
sub_stakes_length = str(staker_functions.getSubStakesLength(ursula1).call())
sub_stakes_length = str(staker_functions.getSubStakesLength(staker1).call())
slashing_args = generate_args_for_slashing(ursula_with_stamp)
transact_and_log("Slashing one sub stake and saving old one (" + sub_stakes_length + " sub stakes), 3rd",
adjudicator_functions.evaluateCFrag(*slashing_args),
{'from': alice1})
sub_stakes_length = str(staker_functions.getSubStakesLength(ursula1).call())
sub_stakes_length = str(staker_functions.getSubStakesLength(staker1).call())
slashing_args = generate_args_for_slashing(ursula_with_stamp)
transact_and_log("Slashing two sub stakes and saving old one (" + sub_stakes_length + " sub stakes)",
adjudicator_functions.evaluateCFrag(*slashing_args),
{'from': alice1})
for index in range(18):
transact(staker_functions.commitToNextPeriod(), {'from': ursula1})
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
testerchain.time_travel(periods=1)
transact(staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS), {'from': ursula1})
deposit = staker_functions.stakerInfo(ursula1).call()[0]
unlocked = deposit - staker_functions.getLockedTokens(ursula1, 1).call()
transact(staker_functions.withdraw(unlocked), {'from': ursula1})
transact(staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS), {'from': staker1})
deposit = staker_functions.stakerInfo(staker1).call()[0]
unlocked = deposit - staker_functions.getLockedTokens(staker1, 1).call()
transact(staker_functions.withdraw(unlocked), {'from': staker1})
sub_stakes_length = str(staker_functions.getSubStakesLength(ursula1).call())
sub_stakes_length = str(staker_functions.getSubStakesLength(staker1).call())
slashing_args = generate_args_for_slashing(ursula_with_stamp)
transact_and_log("Slashing two sub stakes, shortest and new one (" + sub_stakes_length + " sub stakes)",
adjudicator_functions.evaluateCFrag(*slashing_args),
{'from': alice1})
sub_stakes_length = str(staker_functions.getSubStakesLength(ursula1).call())
sub_stakes_length = str(staker_functions.getSubStakesLength(staker1).call())
slashing_args = generate_args_for_slashing(ursula_with_stamp)
transact_and_log("Slashing three sub stakes, two shortest and new one (" + sub_stakes_length + " sub stakes)",
adjudicator_functions.evaluateCFrag(*slashing_args),
@ -565,7 +565,7 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
slashing_args = generate_args_for_slashing(ursula_with_stamp, corrupt_cfrag=False)
transact_and_log("Evaluating correct CFrag", adjudicator_functions.evaluateCFrag(*slashing_args), {'from': alice1})
transact_and_log("Prolong stake", staker_functions.prolongStake(0, 20), {'from': ursula1})
transact_and_log("Prolong stake", staker_functions.prolongStake(0, 20), {'from': staker1})
print("********* All Done! *********")