mirror of https://github.com/nucypher/nucypher.git
First optimization step for StakingEscrow
parent
f99d60b862
commit
854b4d9eb7
|
@ -105,12 +105,13 @@ class SolidityCompiler:
|
|||
|
||||
self.log.info("Compiling with import remappings {}".format(", ".join(remappings)))
|
||||
|
||||
optimization_runs = 10 # TODO: Move..?
|
||||
optimization_runs = 200 # TODO: Move..?
|
||||
try:
|
||||
compiled_sol = compile_files(source_files=source_paths,
|
||||
import_remappings=remappings,
|
||||
allow_paths=project_root,
|
||||
optimize=optimization_runs)
|
||||
optimize=True,
|
||||
optimize_runs=optimization_runs)
|
||||
|
||||
self.log.info("Successfully compiled {} contracts with {} optimization runs".format(len(compiled_sol),
|
||||
optimization_runs))
|
||||
|
|
|
@ -56,11 +56,20 @@ contract Issuer is Upgradeable {
|
|||
)
|
||||
public
|
||||
{
|
||||
require(_token.totalSupply() > 0 &&
|
||||
totalSupply = _token.totalSupply();
|
||||
require(totalSupply > 0 &&
|
||||
_miningCoefficient != 0 &&
|
||||
_hoursPerPeriod != 0 &&
|
||||
_lockedPeriodsCoefficient != 0 &&
|
||||
_rewardedPeriods != 0);
|
||||
uint256 maxLockedPeriods = _rewardedPeriods + _lockedPeriodsCoefficient;
|
||||
require(maxLockedPeriods > _rewardedPeriods &&
|
||||
// worst case for `totalLockedValue * k2`, when totalLockedValue == totalSupply
|
||||
totalSupply * miningCoefficient / totalSupply == miningCoefficient &&
|
||||
// worst case for `(totalSupply - currentSupply) * lockedValue * (k1 + allLockedPeriods)`,
|
||||
// when currentSupply == 0, lockedValue == totalSupply
|
||||
totalSupply * totalSupply * maxLockedPeriods / totalSupply / totalSupply == maxLockedPeriods,
|
||||
"Specified parameters cause overflow");
|
||||
token = _token;
|
||||
miningCoefficient = _miningCoefficient;
|
||||
secondsPerPeriod = _hoursPerPeriod.mul32(1 hours);
|
||||
|
@ -90,9 +99,8 @@ contract Issuer is Upgradeable {
|
|||
function initialize() public onlyOwner {
|
||||
require(currentSupply1 == 0);
|
||||
currentMintingPeriod = getCurrentPeriod();
|
||||
totalSupply = token.totalSupply();
|
||||
uint256 reservedReward = token.balanceOf(address(this));
|
||||
uint256 currentTotalSupply = totalSupply.sub(reservedReward);
|
||||
uint256 currentTotalSupply = totalSupply - reservedReward;
|
||||
currentSupply1 = currentTotalSupply;
|
||||
currentSupply2 = currentTotalSupply;
|
||||
emit Initialized(reservedReward);
|
||||
|
@ -117,36 +125,36 @@ contract Issuer is Upgradeable {
|
|||
if (currentSupply1 == totalSupply || currentSupply2 == totalSupply) {
|
||||
return 0;
|
||||
}
|
||||
uint256 currentSupply = _currentPeriod <= currentMintingPeriod ?
|
||||
Math.min(currentSupply1, currentSupply2) :
|
||||
Math.max(currentSupply1, currentSupply2);
|
||||
|
||||
uint256 maxReward = getReservedReward();
|
||||
uint256 currentReward = _currentPeriod <= currentMintingPeriod ?
|
||||
totalSupply - Math.min(currentSupply1, currentSupply2) : maxReward;
|
||||
|
||||
//(totalSupply - currentSupply) * lockedValue * (k1 + allLockedPeriods) / (totalLockedValue * k2)
|
||||
uint256 allLockedPeriods = uint256(_allLockedPeriods <= rewardedPeriods ?
|
||||
_allLockedPeriods : rewardedPeriods)
|
||||
.add(lockedPeriodsCoefficient);
|
||||
uint256 denominator = _totalLockedValue.mul(miningCoefficient);
|
||||
amount = totalSupply.sub(currentSupply)
|
||||
.mul(_lockedValue)
|
||||
.mul(allLockedPeriods)
|
||||
.div(denominator);
|
||||
uint256 allLockedPeriods =
|
||||
AdditionalMath.min16(_allLockedPeriods, rewardedPeriods) + lockedPeriodsCoefficient;
|
||||
amount = (currentReward * _lockedValue * allLockedPeriods) /
|
||||
(_totalLockedValue * miningCoefficient);
|
||||
|
||||
// rounding the last reward
|
||||
if (amount == 0) {
|
||||
amount = 1;
|
||||
} else if (amount > maxReward) {
|
||||
amount = maxReward;
|
||||
}
|
||||
|
||||
if (_currentPeriod <= currentMintingPeriod) {
|
||||
if (currentSupply1 > currentSupply2) {
|
||||
currentSupply1 = currentSupply1.add(amount);
|
||||
currentSupply1 += amount;
|
||||
} else {
|
||||
currentSupply2 = currentSupply2.add(amount);
|
||||
currentSupply2 += amount;
|
||||
}
|
||||
} else {
|
||||
currentMintingPeriod = _currentPeriod;
|
||||
if (currentSupply1 > currentSupply2) {
|
||||
currentSupply2 = currentSupply1.add(amount);
|
||||
currentSupply2 = currentSupply1 + amount;
|
||||
} else {
|
||||
currentSupply1 = currentSupply2.add(amount);
|
||||
currentSupply1 = currentSupply2 + amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -156,8 +164,8 @@ contract Issuer is Upgradeable {
|
|||
* @param _amount Amount of tokens
|
||||
**/
|
||||
function unMint(uint256 _amount) internal {
|
||||
currentSupply1 = currentSupply1.sub(_amount);
|
||||
currentSupply2 = currentSupply2.sub(_amount);
|
||||
currentSupply1 = currentSupply1 - _amount;
|
||||
currentSupply2 = currentSupply2 - _amount;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -185,6 +193,7 @@ contract Issuer is Upgradeable {
|
|||
function finishUpgrade(address _target) public {
|
||||
super.finishUpgrade(_target);
|
||||
Issuer issuer = Issuer(_target);
|
||||
totalSupply = issuer.totalSupply();
|
||||
token = issuer.token();
|
||||
miningCoefficient = issuer.miningCoefficient();
|
||||
secondsPerPeriod = issuer.secondsPerPeriod();
|
||||
|
|
|
@ -221,7 +221,7 @@ contract StakingEscrow is Issuer {
|
|||
{
|
||||
// if the next period (after current) is confirmed
|
||||
if (_info.confirmedPeriod1 > _currentPeriod || _info.confirmedPeriod2 > _currentPeriod) {
|
||||
return _currentPeriod.add16(1);
|
||||
return _currentPeriod + 1;
|
||||
}
|
||||
return _currentPeriod;
|
||||
}
|
||||
|
@ -234,7 +234,14 @@ contract StakingEscrow is Issuer {
|
|||
function getLastPeriodOfSubStake(SubStakeInfo storage _subStake, uint16 _startPeriod)
|
||||
internal view returns (uint16)
|
||||
{
|
||||
return _subStake.lastPeriod != 0 ? _subStake.lastPeriod : _startPeriod.add16(_subStake.periods);
|
||||
if (_subStake.lastPeriod != 0) {
|
||||
return _subStake.lastPeriod;
|
||||
}
|
||||
uint32 lastPeriod = uint32(_startPeriod) + _subStake.periods;
|
||||
if (lastPeriod > MAX_UINT16) {
|
||||
return MAX_UINT16;
|
||||
}
|
||||
return uint16(lastPeriod);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -246,7 +253,6 @@ contract StakingEscrow is Issuer {
|
|||
public view returns (uint16)
|
||||
{
|
||||
StakerInfo storage info = stakerInfo[_staker];
|
||||
require(_index < info.subStakes.length);
|
||||
SubStakeInfo storage subStake = info.subStakes[_index];
|
||||
uint16 startPeriod = getStartPeriod(info, getCurrentPeriod());
|
||||
return getLastPeriodOfSubStake(subStake, startPeriod);
|
||||
|
@ -268,7 +274,7 @@ contract StakingEscrow is Issuer {
|
|||
SubStakeInfo storage subStake = _info.subStakes[i];
|
||||
if (subStake.firstPeriod <= _period &&
|
||||
getLastPeriodOfSubStake(subStake, startPeriod) >= _period) {
|
||||
lockedValue = lockedValue.add(subStake.lockedValue);
|
||||
lockedValue += subStake.lockedValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -302,16 +308,6 @@ contract StakingEscrow is Issuer {
|
|||
return getLockedTokens(info, currentPeriod, previousPeriod);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Get the value of locked tokens for a staker in the current period
|
||||
* @param _staker Staker
|
||||
**/
|
||||
function getLockedTokens(address _staker)
|
||||
public view returns (uint256)
|
||||
{
|
||||
return getLockedTokens(_staker, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Get the last active staker's period
|
||||
* @param _staker Staker
|
||||
|
@ -361,7 +357,7 @@ contract StakingEscrow is Issuer {
|
|||
if (lockedTokens != 0) {
|
||||
activeStakers[resultIndex][0] = uint256(staker);
|
||||
activeStakers[resultIndex++][1] = lockedTokens;
|
||||
allLockedTokens = allLockedTokens.add(lockedTokens);
|
||||
allLockedTokens += lockedTokens;
|
||||
}
|
||||
}
|
||||
assembly {
|
||||
|
@ -561,16 +557,15 @@ contract StakingEscrow is Issuer {
|
|||
* @param _periods Amount of periods during which tokens will be locked
|
||||
**/
|
||||
function lock(address _staker, uint256 _value, uint16 _periods) internal {
|
||||
require(_value <= token.balanceOf(address(this)) &&
|
||||
_value >= minAllowableLockedTokens &&
|
||||
require(_value >= minAllowableLockedTokens &&
|
||||
_periods >= minLockedPeriods);
|
||||
|
||||
uint16 currentPeriod = getCurrentPeriod();
|
||||
uint16 nextPeriod = currentPeriod.add16(1);
|
||||
StakerInfo storage info = stakerInfo[_staker];
|
||||
uint256 lockedTokens = getLockedTokens(info, currentPeriod, nextPeriod);
|
||||
require(_value.add(lockedTokens) <= info.value &&
|
||||
_value.add(lockedTokens) <= maxAllowableLockedTokens);
|
||||
uint256 requestedLockedTokens = _value.add(lockedTokens);
|
||||
require(requestedLockedTokens <= info.value && requestedLockedTokens <= maxAllowableLockedTokens);
|
||||
|
||||
if (info.confirmedPeriod1 != nextPeriod && info.confirmedPeriod2 != nextPeriod) {
|
||||
saveSubStake(info, nextPeriod, 0, _periods, _value);
|
||||
|
@ -629,9 +624,7 @@ contract StakingEscrow is Issuer {
|
|||
**/
|
||||
function divideStake(uint256 _index, uint256 _newValue, uint16 _periods) public onlyStaker {
|
||||
StakerInfo storage info = stakerInfo[msg.sender];
|
||||
require(_newValue >= minAllowableLockedTokens &&
|
||||
_periods > 0 &&
|
||||
_index < info.subStakes.length);
|
||||
require(_newValue >= minAllowableLockedTokens && _periods > 0);
|
||||
SubStakeInfo storage subStake = info.subStakes[_index];
|
||||
uint16 currentPeriod = getCurrentPeriod();
|
||||
uint16 startPeriod = getStartPeriod(info, currentPeriod);
|
||||
|
@ -641,9 +634,10 @@ contract StakingEscrow is Issuer {
|
|||
uint256 oldValue = subStake.lockedValue;
|
||||
subStake.lockedValue = oldValue.sub(_newValue);
|
||||
require(subStake.lockedValue >= minAllowableLockedTokens);
|
||||
saveSubStake(info, subStake.firstPeriod, 0, subStake.periods.add16(_periods), _newValue);
|
||||
uint16 requestedPeriods = subStake.periods.add16(_periods);
|
||||
saveSubStake(info, subStake.firstPeriod, 0, requestedPeriods, _newValue);
|
||||
emit Divided(msg.sender, oldValue, lastPeriod, _newValue, _periods);
|
||||
emit Locked(msg.sender, _newValue, subStake.firstPeriod, subStake.periods + _periods);
|
||||
emit Locked(msg.sender, _newValue, subStake.firstPeriod, requestedPeriods);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -682,8 +676,7 @@ contract StakingEscrow is Issuer {
|
|||
// but when the staker locks more then we should use the next period
|
||||
uint256 lockedTokens = Math.max(getLockedTokens(info, currentPeriod, nextPeriod),
|
||||
getLockedTokens(info, currentPeriod, currentPeriod));
|
||||
require(_value <= token.balanceOf(address(this)) &&
|
||||
_value <= info.value.sub(lockedTokens));
|
||||
require(_value <= info.value.sub(lockedTokens));
|
||||
info.value -= _value;
|
||||
token.safeTransfer(msg.sender, _value);
|
||||
emit Withdrawn(msg.sender, _value);
|
||||
|
@ -694,12 +687,12 @@ contract StakingEscrow is Issuer {
|
|||
**/
|
||||
function confirmActivity() external {
|
||||
address staker = getStakerFromWorker(msg.sender);
|
||||
require(stakerInfo[staker].value > 0, "Staker must have a stake to confirm activity");
|
||||
StakerInfo storage info = stakerInfo[staker];
|
||||
require(info.value > 0, "Staker must have a stake to confirm activity");
|
||||
require(msg.sender == tx.origin, "Only worker with real address can confirm activity");
|
||||
|
||||
uint16 lastActivePeriod = getLastActivePeriod(staker);
|
||||
mint(staker);
|
||||
StakerInfo storage info = stakerInfo[staker];
|
||||
uint16 currentPeriod = getCurrentPeriod();
|
||||
uint16 nextPeriod = currentPeriod.add16(1);
|
||||
|
||||
|
@ -744,7 +737,7 @@ contract StakingEscrow is Issuer {
|
|||
// because we won't be able to calculate last active period
|
||||
// see getLastActivePeriod(address)
|
||||
StakerInfo storage info = stakerInfo[msg.sender];
|
||||
uint16 previousPeriod = getCurrentPeriod().sub16(1);
|
||||
uint16 previousPeriod = getCurrentPeriod() - 1;
|
||||
if (info.confirmedPeriod1 <= previousPeriod &&
|
||||
info.confirmedPeriod2 <= previousPeriod &&
|
||||
(info.confirmedPeriod1 != EMPTY_CONFIRMED_PERIOD ||
|
||||
|
@ -761,7 +754,7 @@ contract StakingEscrow is Issuer {
|
|||
**/
|
||||
function mint(address _staker) internal {
|
||||
uint16 currentPeriod = getCurrentPeriod();
|
||||
uint16 previousPeriod = currentPeriod.sub16(1);
|
||||
uint16 previousPeriod = currentPeriod - 1;
|
||||
StakerInfo storage info = stakerInfo[_staker];
|
||||
|
||||
if (info.confirmedPeriod1 > previousPeriod &&
|
||||
|
@ -789,10 +782,10 @@ contract StakingEscrow is Issuer {
|
|||
uint256 reward = 0;
|
||||
if (info.confirmedPeriod1 != EMPTY_CONFIRMED_PERIOD &&
|
||||
info.confirmedPeriod1 < info.confirmedPeriod2) {
|
||||
reward = reward.add(mint(_staker, info, 1, currentPeriod, startPeriod));
|
||||
reward = mint(_staker, info, 1, currentPeriod, startPeriod);
|
||||
} else if (info.confirmedPeriod2 != EMPTY_CONFIRMED_PERIOD &&
|
||||
info.confirmedPeriod2 < info.confirmedPeriod1) {
|
||||
reward = reward.add(mint(_staker, info, 2, currentPeriod, startPeriod));
|
||||
reward = mint(_staker, info, 2, currentPeriod, startPeriod);
|
||||
}
|
||||
if (info.confirmedPeriod2 <= previousPeriod &&
|
||||
info.confirmedPeriod2 > info.confirmedPeriod1) {
|
||||
|
@ -1015,7 +1008,7 @@ contract StakingEscrow is Issuer {
|
|||
saveOldSubStake(_info, shortestSubStake.firstPeriod, _penalty, _decreasePeriod);
|
||||
_penalty = 0;
|
||||
} else {
|
||||
shortestSubStake.lastPeriod = _decreasePeriod.sub16(1);
|
||||
shortestSubStake.lastPeriod = _decreasePeriod - 1;
|
||||
_penalty -= shortestSubStake.lockedValue;
|
||||
appliedPenalty = shortestSubStake.lockedValue;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,8 @@ from nucypher.blockchain.economics import TokenEconomics
|
|||
from nucypher.blockchain.eth.token import NU
|
||||
|
||||
SECRET_LENGTH = 32
|
||||
TOTAL_SUPPLY = 2 * 10 ** 40
|
||||
INITIAL_SUPPLY = 10 ** 26
|
||||
TOTAL_SUPPLY = 2 * 10 ** 36
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
|
@ -36,9 +37,9 @@ def token(testerchain, deploy_contract):
|
|||
|
||||
@pytest.mark.slow
|
||||
def test_issuer(testerchain, token, deploy_contract):
|
||||
economics = TokenEconomics(initial_supply=10 ** 30,
|
||||
economics = TokenEconomics(initial_supply=INITIAL_SUPPLY,
|
||||
total_supply=TOTAL_SUPPLY,
|
||||
staking_coefficient=10 ** 43,
|
||||
staking_coefficient=10 ** 39,
|
||||
locked_periods_coefficient=10 ** 4,
|
||||
maximum_rewarded_periods=10 ** 4,
|
||||
hours_per_period=1)
|
||||
|
@ -130,9 +131,9 @@ def test_inflation_rate(testerchain, token, deploy_contract):
|
|||
During one period inflation rate must be the same
|
||||
"""
|
||||
|
||||
economics = TokenEconomics(initial_supply=10 ** 30,
|
||||
economics = TokenEconomics(initial_supply=INITIAL_SUPPLY,
|
||||
total_supply=TOTAL_SUPPLY,
|
||||
staking_coefficient=2 * 10 ** 19,
|
||||
staking_coefficient=2 * 10 ** 15,
|
||||
locked_periods_coefficient=1,
|
||||
maximum_rewarded_periods=1,
|
||||
hours_per_period=1)
|
||||
|
@ -262,7 +263,7 @@ def test_upgrading(testerchain, token, deploy_contract):
|
|||
assert 2 == contract.functions.lockedPeriodsCoefficient().call()
|
||||
assert 2 == contract.functions.rewardedPeriods().call()
|
||||
assert period == contract.functions.currentMintingPeriod().call()
|
||||
assert 2 * 10 ** 40 == contract.functions.totalSupply().call()
|
||||
assert TOTAL_SUPPLY == contract.functions.totalSupply().call()
|
||||
# Check method from new ABI
|
||||
tx = contract.functions.setValueToCheck(3).transact({'from': creator})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
@ -296,7 +297,7 @@ def test_upgrading(testerchain, token, deploy_contract):
|
|||
assert 1 == contract.functions.lockedPeriodsCoefficient().call()
|
||||
assert 1 == contract.functions.rewardedPeriods().call()
|
||||
assert period == contract.functions.currentMintingPeriod().call()
|
||||
assert 2 * 10 ** 40 == contract.functions.totalSupply().call()
|
||||
assert TOTAL_SUPPLY == contract.functions.totalSupply().call()
|
||||
# After rollback can't use new ABI
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = contract.functions.setValueToCheck(2).transact({'from': creator})
|
||||
|
|
|
@ -315,7 +315,7 @@ def test_mining(testerchain, token, escrow_contract, token_economics):
|
|||
# Ursula(2) withdraws all
|
||||
testerchain.time_travel(hours=2)
|
||||
ursula2_stake = escrow.functions.getAllTokens(ursula2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
tx = escrow.functions.withdraw(ursula2_stake).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 0 == escrow.functions.getAllTokens(ursula2).call()
|
||||
|
@ -367,7 +367,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
testerchain.time_travel(hours=1)
|
||||
current_period = escrow.functions.getCurrentPeriod().call()
|
||||
assert 100 == escrow.functions.getAllTokens(ursula).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 100 == escrow.functions.lockedPerPeriod(current_period).call()
|
||||
assert 0 == escrow.functions.lockedPerPeriod(current_period + 1).call()
|
||||
|
||||
|
@ -386,7 +386,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
testerchain.wait_for_receipt(tx)
|
||||
# Staker has no more sub stakes
|
||||
assert 0 == escrow.functions.getAllTokens(ursula).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 10 == token.functions.balanceOf(investigator).call()
|
||||
assert 0 == escrow.functions.lockedPerPeriod(current_period).call()
|
||||
assert reward + 90 == escrow.functions.getReservedReward().call()
|
||||
|
@ -409,7 +409,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
tx = escrow.functions.confirmActivity().transact({'from': ursula})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 100 == escrow.functions.getAllTokens(ursula).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula, 1).call()
|
||||
assert 100 == escrow.functions.lockedPerPeriod(current_period).call()
|
||||
assert 100 == escrow.functions.lockedPerPeriod(current_period + 1).call()
|
||||
|
@ -419,7 +419,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
tx = adjudicator.functions.slashStaker(ursula, 10, investigator, 11).transact()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 90 == escrow.functions.getAllTokens(ursula).call()
|
||||
assert 90 == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert 90 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 90 == escrow.functions.getLockedTokens(ursula, 1).call()
|
||||
# The reward will be equal to the penalty (can't be more then penalty)
|
||||
assert 20 == token.functions.balanceOf(investigator).call()
|
||||
|
@ -441,7 +441,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
testerchain.time_travel(hours=1)
|
||||
current_period += 1
|
||||
assert 90 == escrow.functions.getLockedTokensInPast(ursula, 1).call()
|
||||
assert 190 == escrow.functions.getLockedTokens(ursula).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.lockedPerPeriod(current_period - 1).call()
|
||||
|
@ -454,7 +454,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
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).call()
|
||||
assert 180 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula, 4).call()
|
||||
assert 20 == token.functions.balanceOf(investigator).call()
|
||||
assert 90 == escrow.functions.lockedPerPeriod(current_period - 1).call()
|
||||
|
@ -477,7 +477,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
testerchain.time_travel(hours=2)
|
||||
current_period += 2
|
||||
assert 290 == escrow.functions.getLockedTokensInPast(ursula, 1).call()
|
||||
assert 290 == escrow.functions.getLockedTokens(ursula).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 mined
|
||||
assert 290 == escrow.functions.lockedPerPeriod(current_period - 1).call()
|
||||
|
@ -489,7 +489,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
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).call()
|
||||
assert 290 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 180 == escrow.functions.getLockedTokens(ursula, 2).call()
|
||||
assert 20 == token.functions.balanceOf(investigator).call()
|
||||
assert 290 == escrow.functions.lockedPerPeriod(current_period - 1).call()
|
||||
|
@ -509,7 +509,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
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).call()
|
||||
assert 270 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 180 == escrow.functions.getLockedTokens(ursula, 2).call()
|
||||
assert 20 == token.functions.balanceOf(investigator).call()
|
||||
assert 290 == escrow.functions.lockedPerPeriod(current_period - 1).call()
|
||||
|
@ -530,7 +530,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
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).call()
|
||||
assert 170 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 170 == escrow.functions.getLockedTokens(ursula, 2).call()
|
||||
assert 20 == token.functions.balanceOf(investigator).call()
|
||||
assert 290 == escrow.functions.lockedPerPeriod(current_period - 1).call()
|
||||
|
@ -552,7 +552,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
# New deposit
|
||||
tx = escrow.functions.deposit(100, 2).transact({'from': ursula})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 170 == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert 170 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 270 == escrow.functions.getLockedTokens(ursula, 1).call()
|
||||
assert 270 == escrow.functions.lockedPerPeriod(current_period + 1).call()
|
||||
deposit = escrow.functions.getAllTokens(ursula).call() # Some reward is already mined
|
||||
|
@ -563,7 +563,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
# 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()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 170 == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert 170 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 260 == escrow.functions.getLockedTokens(ursula, 1).call()
|
||||
assert 260 == escrow.functions.getAllTokens(ursula).call()
|
||||
assert 260 == escrow.functions.lockedPerPeriod(current_period + 1).call()
|
||||
|
@ -584,7 +584,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
testerchain.time_travel(hours=1)
|
||||
|
||||
current_period += 2
|
||||
assert 260 == escrow.functions.getLockedTokens(ursula).call()
|
||||
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.lockedPerPeriod(current_period - 1).call()
|
||||
|
@ -594,7 +594,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
testerchain.wait_for_receipt(tx)
|
||||
tx = escrow.functions.confirmActivity().transact({'from': ursula})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 260 == escrow.functions.getLockedTokens(ursula).call()
|
||||
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.lockedPerPeriod(current_period - 1).call()
|
||||
|
@ -608,7 +608,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
reward = escrow.functions.getReservedReward().call()
|
||||
tx = adjudicator.functions.slashStaker(ursula, unlocked_deposit + 10, investigator, 0).transact()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 250 == escrow.functions.getLockedTokens(ursula).call()
|
||||
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()
|
||||
|
@ -629,7 +629,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
# 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()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 160 == escrow.functions.getLockedTokens(ursula).call()
|
||||
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()
|
||||
|
@ -668,7 +668,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
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).call()
|
||||
assert 200 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 200 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
|
||||
# Slash one sub stake to set the last period of this sub stake to the previous period
|
||||
|
@ -676,7 +676,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
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).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
|
||||
events = slashing_log.get_all_entries()
|
||||
|
@ -693,7 +693,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
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).call()
|
||||
assert 50 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 50 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
|
||||
events = slashing_log.get_all_entries()
|
||||
|
@ -710,7 +710,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
testerchain.time_travel(hours=1)
|
||||
tx = escrow.functions.deposit(100, 3).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 50 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 50 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 150 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula2, 2).call()
|
||||
|
||||
|
@ -724,7 +724,7 @@ def test_slashing(testerchain, token, escrow_contract, token_economics, deploy_c
|
|||
# Stake in the current period should not change, because overflow starts from the next period
|
||||
tx = adjudicator.functions.slashStaker(ursula2, 10, investigator, 0).transact()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 50 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 50 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 140 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula2, 2).call()
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ from eth_tester.exceptions import TransactionFailed
|
|||
from eth_utils import to_checksum_address
|
||||
|
||||
MAX_SUB_STAKES = 30
|
||||
MAX_UINT16 = 65535
|
||||
|
||||
|
||||
@pytest.mark.slow
|
||||
|
@ -66,9 +67,9 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
# Check that nothing is locked
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula1).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(testerchain.client.accounts[3]).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula1, 0).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(testerchain.client.accounts[3], 0).call()
|
||||
|
||||
# Ursula can't deposit tokens before Escrow initialization
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
|
@ -112,7 +113,7 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
testerchain.wait_for_receipt(tx)
|
||||
assert 1000 == token.functions.balanceOf(escrow.address).call()
|
||||
assert 9000 == token.functions.balanceOf(ursula1).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula1).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula1, 0).call()
|
||||
assert 1000 == escrow.functions.getLockedTokens(ursula1, 1).call()
|
||||
assert 1000 == escrow.functions.getLockedTokens(ursula1, 2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula1, 3).call()
|
||||
|
@ -139,7 +140,7 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
testerchain.wait_for_receipt(tx)
|
||||
assert 1500 == token.functions.balanceOf(escrow.address).call()
|
||||
assert 9500 == token.functions.balanceOf(ursula2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
|
||||
events = deposit_log.get_all_entries()
|
||||
|
@ -184,8 +185,8 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
# Checks locked tokens in the next period
|
||||
testerchain.time_travel(hours=1)
|
||||
current_period = escrow.functions.getCurrentPeriod().call()
|
||||
assert 1000 == escrow.functions.getLockedTokens(ursula1).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 1000 == escrow.functions.getLockedTokens(ursula1, 0).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
|
||||
# Both stakers are active and have locked tokens in next period
|
||||
all_locked, stakers = escrow.functions.getActiveStakers(1, 0, 0).call()
|
||||
|
@ -283,7 +284,7 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
|
||||
# Wait 1 period and checks locking
|
||||
testerchain.time_travel(hours=1)
|
||||
assert 1500 == escrow.functions.getLockedTokens(ursula1).call()
|
||||
assert 1500 == escrow.functions.getLockedTokens(ursula1, 0).call()
|
||||
|
||||
# Only one staker is active
|
||||
all_locked, stakers = escrow.functions.getActiveStakers(1, 0, 0).call()
|
||||
|
@ -297,7 +298,7 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
testerchain.wait_for_receipt(tx)
|
||||
testerchain.time_travel(hours=1)
|
||||
current_period = escrow.functions.getCurrentPeriod().call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula1).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula1, 0).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula1, 1).call()
|
||||
|
||||
events = activity_log.get_all_entries()
|
||||
|
@ -353,12 +354,12 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
assert 100 == event_args['value']
|
||||
|
||||
# Value of locked tokens will be updated in next period
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula1).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula1, 0).call()
|
||||
assert 600 == escrow.functions.getLockedTokens(ursula1, 1).call()
|
||||
assert 600 == escrow.functions.getLockedTokens(ursula1, 2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula1, 3).call()
|
||||
testerchain.time_travel(hours=1)
|
||||
assert 600 == escrow.functions.getLockedTokens(ursula1).call()
|
||||
assert 600 == escrow.functions.getLockedTokens(ursula1, 0).call()
|
||||
assert 600 == escrow.functions.getLockedTokens(ursula1, 1).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula1, 2).call()
|
||||
|
||||
|
@ -367,12 +368,12 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
testerchain.wait_for_receipt(tx)
|
||||
tx = escrow.functions.confirmActivity().transact({'from': ursula1})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 600 == escrow.functions.getLockedTokens(ursula1).call()
|
||||
assert 600 == escrow.functions.getLockedTokens(ursula1, 0).call()
|
||||
assert 1100 == escrow.functions.getLockedTokens(ursula1, 1).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula1, 2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula1, 3).call()
|
||||
testerchain.time_travel(hours=1)
|
||||
assert 1100 == escrow.functions.getLockedTokens(ursula1).call()
|
||||
assert 1100 == escrow.functions.getLockedTokens(ursula1, 0).call()
|
||||
|
||||
# Ursula(2) increases lock by deposit more tokens using approveAndCall
|
||||
tx = token.functions.approveAndCall(escrow.address, 500, testerchain.w3.toBytes(2))\
|
||||
|
@ -380,7 +381,7 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
testerchain.wait_for_receipt(tx)
|
||||
tx = escrow.functions.confirmActivity().transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 1000 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2, 2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula2, 3).call()
|
||||
|
@ -392,7 +393,7 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
assert current_period + 1 == escrow.functions.getLastPeriodOfSubStake(ursula2, 1).call()
|
||||
tx = escrow.functions.divideStake(1, 200, 1).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 1000 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 1000 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
assert 200 == escrow.functions.getLockedTokens(ursula2, 2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula2, 3).call()
|
||||
|
@ -428,7 +429,7 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
# But can lock
|
||||
tx = escrow.functions.lock(200, 2).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 400 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
assert 200 == escrow.functions.getLockedTokens(ursula2, 2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula2, 3).call()
|
||||
|
@ -440,7 +441,7 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
# Divide stake again
|
||||
tx = escrow.functions.divideStake(2, 100, 1).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 400 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
assert 300 == escrow.functions.getLockedTokens(ursula2, 2).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula2, 3).call()
|
||||
|
@ -457,7 +458,7 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
# Prolong some sub stake
|
||||
tx = escrow.functions.prolongStake(3, 1).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 400 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
assert 300 == escrow.functions.getLockedTokens(ursula2, 2).call()
|
||||
assert 200 == escrow.functions.getLockedTokens(ursula2, 3).call()
|
||||
|
@ -476,7 +477,7 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
testerchain.wait_for_receipt(tx)
|
||||
tx = escrow.functions.prolongStake(2, 1).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert 500 == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert 400 == escrow.functions.getLockedTokens(ursula2, 1).call()
|
||||
assert 400 == escrow.functions.getLockedTokens(ursula2, 2).call()
|
||||
assert 200 == escrow.functions.getLockedTokens(ursula2, 3).call()
|
||||
|
@ -507,25 +508,35 @@ def test_staking(testerchain, token, escrow_contract):
|
|||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = escrow.functions.divideStake(0, 200, 10).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
# Can't divide nonexistent stake
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = escrow.functions.divideStake(10, 100, 1).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
# And can't prolong old stake
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = escrow.functions.prolongStake(0, 10).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
current_period = escrow.functions.getCurrentPeriod().call()
|
||||
events = activity_log.get_all_entries()
|
||||
assert 13 == len(events)
|
||||
event_args = events[11]['args']
|
||||
assert ursula2 == event_args['staker']
|
||||
assert escrow.functions.getCurrentPeriod().call() == event_args['period']
|
||||
assert current_period == event_args['period']
|
||||
assert 400 == event_args['value']
|
||||
event_args = events[12]['args']
|
||||
assert ursula2 == event_args['staker']
|
||||
assert escrow.functions.getCurrentPeriod().call() + 1 == event_args['period']
|
||||
assert current_period + 1 == event_args['period']
|
||||
assert 200 == event_args['value']
|
||||
|
||||
assert 5 == len(deposit_log.get_all_entries())
|
||||
assert 1 == len(withdraw_log.get_all_entries())
|
||||
|
||||
# Test max locking duration
|
||||
tx = escrow.functions.lock(200, MAX_UINT16).transact({'from': ursula2})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 200 == escrow.functions.getLockedTokens(ursula2, MAX_UINT16 - current_period).call()
|
||||
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_max_sub_stakes(testerchain, token, escrow_contract):
|
||||
|
@ -571,7 +582,7 @@ def test_max_sub_stakes(testerchain, token, escrow_contract):
|
|||
tx = escrow.functions.confirmActivity().transact({'from': ursula})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
testerchain.time_travel(hours=1)
|
||||
assert 2900 == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert 2900 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 0 == escrow.functions.getLockedTokens(ursula, 1).call()
|
||||
assert MAX_SUB_STAKES == escrow.functions.getSubStakesLength(ursula).call()
|
||||
# Before sub stake will be inactive it must be mined
|
||||
|
@ -582,7 +593,7 @@ def test_max_sub_stakes(testerchain, token, escrow_contract):
|
|||
testerchain.wait_for_receipt(tx)
|
||||
tx = escrow.functions.deposit(100, 2).transact({'from': ursula})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert 2900 == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert 2900 == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert 100 == escrow.functions.getLockedTokens(ursula, 1).call()
|
||||
assert MAX_SUB_STAKES == escrow.functions.getSubStakesLength(ursula).call()
|
||||
|
||||
|
|
|
@ -79,6 +79,13 @@ def test_upgrading(testerchain, token, token_economics, deploy_contract):
|
|||
policy_manager, _ = deploy_contract(
|
||||
'PolicyManagerForStakingEscrowMock', token.address, contract.address
|
||||
)
|
||||
# Can't set wrong address
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = contract.functions.setPolicyManager(BlockchainInterface.NULL_ADDRESS).transact()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = contract.functions.setPolicyManager(contract_library_v1.address).transact()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
tx = contract.functions.setPolicyManager(policy_manager.address).transact()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
worklock, _ = deploy_contract(
|
||||
|
@ -259,7 +266,7 @@ def test_re_stake(testerchain, token, escrow_contract):
|
|||
testerchain.time_travel(hours=1)
|
||||
period = escrow.functions.getCurrentPeriod().call()
|
||||
assert sub_stake == escrow.functions.getAllTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period).call()
|
||||
assert 0 == escrow.functions.lockedPerPeriod(period + 1).call()
|
||||
|
||||
|
@ -269,14 +276,14 @@ def test_re_stake(testerchain, token, escrow_contract):
|
|||
testerchain.time_travel(hours=1)
|
||||
period = escrow.functions.getCurrentPeriod().call()
|
||||
assert sub_stake == escrow.functions.getAllTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period - 1).call()
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period).call()
|
||||
tx = escrow.functions.mint().transact({'from': ursula})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
# Reward is not locked and stake is not changed
|
||||
assert sub_stake < escrow.functions.getAllTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period - 1).call()
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period).call()
|
||||
|
||||
|
@ -314,14 +321,14 @@ def test_re_stake(testerchain, token, escrow_contract):
|
|||
testerchain.time_travel(hours=1)
|
||||
period = escrow.functions.getCurrentPeriod().call()
|
||||
assert sub_stake == escrow.functions.getAllTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period - 1).call()
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period).call()
|
||||
tx = escrow.functions.mint().transact({'from': ursula})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
# Entire reward is locked
|
||||
balance = escrow.functions.getAllTokens(ursula).call()
|
||||
new_sub_stake = escrow.functions.getLockedTokens(ursula).call()
|
||||
new_sub_stake = escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert sub_stake < balance
|
||||
assert balance == new_sub_stake
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period - 1).call()
|
||||
|
@ -332,14 +339,14 @@ def test_re_stake(testerchain, token, escrow_contract):
|
|||
period = escrow.functions.getCurrentPeriod().call()
|
||||
sub_stake = new_sub_stake
|
||||
assert sub_stake == escrow.functions.getAllTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period - 1).call()
|
||||
assert 0 == escrow.functions.lockedPerPeriod(period).call()
|
||||
tx = escrow.functions.mint().transact({'from': ursula})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
# Entire reward is locked
|
||||
balance = escrow.functions.getAllTokens(ursula).call()
|
||||
new_sub_stake = escrow.functions.getLockedTokens(ursula).call()
|
||||
new_sub_stake = escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert sub_stake < balance
|
||||
assert balance == new_sub_stake
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period - 1).call()
|
||||
|
@ -375,8 +382,8 @@ def test_re_stake(testerchain, token, escrow_contract):
|
|||
period = escrow.functions.getCurrentPeriod().call()
|
||||
assert stake == escrow.functions.getAllTokens(ursula).call()
|
||||
assert stake == escrow.functions.getAllTokens(ursula2).call()
|
||||
assert stake == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert stake == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert stake == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert stake == escrow.functions.getLockedTokens(ursula2, 0).call()
|
||||
assert sub_stake_1 == escrow.functions.getSubStakeInfo(ursula, 0).call()[3]
|
||||
assert sub_stake_2 == escrow.functions.getSubStakeInfo(ursula, 1).call()[3]
|
||||
assert 2 * stake == escrow.functions.lockedPerPeriod(period - 2).call()
|
||||
|
@ -395,14 +402,14 @@ def test_re_stake(testerchain, token, escrow_contract):
|
|||
assert 0 < ursula2_reward
|
||||
assert ursula_reward > ursula2_reward
|
||||
# Ursula2's stake has not changed
|
||||
assert stake == escrow.functions.getLockedTokens(ursula2).call()
|
||||
assert stake == escrow.functions.getLockedTokens(ursula2, 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
|
||||
re_stake_for_second_sub_stake = ursula_reward // 3
|
||||
re_stake_for_first_sub_stake = ursula_reward - re_stake_for_second_sub_stake
|
||||
# Check re-stake for Ursula1's sub stakes
|
||||
assert stake + ursula_reward == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert stake + ursula_reward == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert sub_stake_1 + re_stake_for_first_sub_stake == escrow.functions.getSubStakeInfo(ursula, 0).call()[3]
|
||||
assert sub_stake_2 + re_stake_for_second_sub_stake == escrow.functions.getSubStakeInfo(ursula, 1).call()[3]
|
||||
|
||||
|
@ -440,7 +447,7 @@ def test_re_stake(testerchain, token, escrow_contract):
|
|||
testerchain.time_travel(hours=1)
|
||||
period = escrow.functions.getCurrentPeriod().call()
|
||||
sub_stake = escrow.functions.getLockedTokensInPast(ursula, 1).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert sub_stake == escrow.functions.getAllTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period - 1).call()
|
||||
tx = escrow.functions.mint().transact({'from': ursula})
|
||||
|
@ -449,7 +456,7 @@ def test_re_stake(testerchain, token, escrow_contract):
|
|||
# Reward is not locked and stake is not changed
|
||||
assert sub_stake < escrow.functions.getAllTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokensInPast(ursula, 1).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula).call()
|
||||
assert sub_stake == escrow.functions.getLockedTokens(ursula, 0).call()
|
||||
assert sub_stake == escrow.functions.lockedPerPeriod(period - 1).call()
|
||||
|
||||
|
||||
|
|
|
@ -300,7 +300,7 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
|
|||
#
|
||||
# Get locked tokens
|
||||
#
|
||||
log.info("Getting locked tokens = " + str(staker_functions.getLockedTokens(ursula1).estimateGas()))
|
||||
log.info("Getting locked tokens = " + str(staker_functions.getLockedTokens(ursula1, 0).estimateGas()))
|
||||
|
||||
#
|
||||
# Wait 1 period and withdraw tokens
|
||||
|
@ -643,7 +643,7 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
|
|||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
deposit = staker_functions.stakerInfo(ursula1).call()[0]
|
||||
unlocked = deposit - staker_functions.getLockedTokens(ursula1).call()
|
||||
unlocked = deposit - staker_functions.getLockedTokens(ursula1, 0).call()
|
||||
tx = staker_functions.withdraw(unlocked).transact({'from': ursula1})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
|
|
Loading…
Reference in New Issue