Skip to content

Commit

Permalink
allow unlocksStake to decrease stake below delegations limit
Browse files Browse the repository at this point in the history
  • Loading branch information
uprendis committed Feb 23, 2024
1 parent f483d6a commit 3d09170
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions contracts/sfc/SFCLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ contract SFCLib is SFCBase {
require(success || !strict, "gov votes recounting failed");
}

function _rawUndelegate(address delegator, uint256 toValidatorID, uint256 amount, bool strict) internal {
function _rawUndelegate(address delegator, uint256 toValidatorID, uint256 amount, bool strict, bool forceful, bool checkDelegatedStake) internal {
getStake[delegator][toValidatorID] -= amount;
getValidator[toValidatorID].receivedStake = getValidator[toValidatorID].receivedStake.sub(amount);
totalStake = totalStake.sub(amount);
Expand All @@ -172,11 +172,15 @@ contract SFCLib is SFCBase {
}

uint256 selfStakeAfterwards = getSelfStake(toValidatorID);
if (selfStakeAfterwards != 0) {
if (getValidator[toValidatorID].status == OK_STATUS) {
require(selfStakeAfterwards >= c.minSelfStake(), "insufficient self-stake");
require(_checkDelegatedStakeLimit(toValidatorID), "validator's delegations limit is exceeded");
if (selfStakeAfterwards != 0 && getValidator[toValidatorID].status == OK_STATUS) {
if (!(selfStakeAfterwards >= c.minSelfStake())) {
if (forceful) {
revert("insufficient self-stake");
} else {
_setValidatorDeactivated(toValidatorID, WITHDRAWN_BIT);
}
}
require(!checkDelegatedStake || _checkDelegatedStakeLimit(toValidatorID), "validator's delegations limit is exceeded");
} else {
_setValidatorDeactivated(toValidatorID, WITHDRAWN_BIT);
}
Expand All @@ -195,7 +199,7 @@ contract SFCLib is SFCBase {

require(getWithdrawalRequest[delegator][toValidatorID][wrID].amount == 0, "wrID already exists");

_rawUndelegate(delegator, toValidatorID, amount, true);
_rawUndelegate(delegator, toValidatorID, amount, true, false, true);

getWithdrawalRequest[delegator][toValidatorID][wrID].amount = amount;
getWithdrawalRequest[delegator][toValidatorID][wrID].epoch = currentEpoch();
Expand Down Expand Up @@ -224,7 +228,7 @@ contract SFCLib is SFCBase {
emit UnlockedStake(delegator, toValidatorID, amount - unlockedStake, 0);
}

_rawUndelegate(delegator, toValidatorID, amount, false);
_rawUndelegate(delegator, toValidatorID, amount, false, true, false);

_syncValidator(toValidatorID, false);

Expand Down Expand Up @@ -564,7 +568,7 @@ contract SFCLib is SFCBase {
}
ld.lockedStake -= amount;
if (penalty != 0) {
_rawUndelegate(delegator, toValidatorID, penalty, true);
_rawUndelegate(delegator, toValidatorID, penalty, true, false, false);
treasuryAddress.call.value(penalty)("");
}

Expand Down

0 comments on commit 3d09170

Please sign in to comment.