Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge branch 'v0.3.0-backport' into fix-proposer-bonus
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkanani authored Feb 24, 2021
2 parents 5bc4e0a + 8c48051 commit f2da660
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 32 deletions.
18 changes: 18 additions & 0 deletions contracts/staking/EventsHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@ contract EventsHub is Initializable {
) public onlyStakeManager {
emit RewardParams(rewardDecreasePerCheckpoint, maxRewardedCheckpoints, checkpointRewardDelta);
}

event UpdateCommissionRate(
uint256 indexed validatorId,
uint256 indexed newCommissionRate,
uint256 indexed oldCommissionRate
);

function logUpdateCommissionRate(
uint256 validatorId,
uint256 newCommissionRate,
uint256 oldCommissionRate
) public onlyStakeManager {
emit UpdateCommissionRate(
validatorId,
newCommissionRate,
oldCommissionRate
);
}
}
38 changes: 20 additions & 18 deletions contracts/staking/StakingInfo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ contract IStakeManagerLocal {
address signer;
address contractAddress;
Status status;
uint256 commissionRate;
uint256 lastCommissionUpdate;
uint256 delegatorsReward;
uint256 delegatedAmount;
uint256 initialRewardPerStake;
}

mapping(uint256 => Validator) public validators;
bytes32 public accountStateRoot;
uint256 public activeAmount; // delegation amount from validator contract
uint256 public validatorRewards;

function currentValidatorSetTotalStake() public view returns (uint256);

Expand All @@ -39,7 +36,6 @@ contract IStakeManagerLocal {
function isValidator(uint256 validatorId) public view returns (bool);
}


contract StakingInfo is Ownable {
using SafeMath for uint256;
mapping(uint256 => uint256) public validatorNonce;
Expand Down Expand Up @@ -182,7 +178,7 @@ contract StakingInfo is Ownable {

modifier onlyValidatorContract(uint256 validatorId) {
address _contract;
(, , , , , , _contract, , , , ,,) = IStakeManagerLocal(
(, , , , , , _contract, ) = IStakeManagerLocal(
registry.getStakeManagerAddress()
)
.validators(validatorId);
Expand All @@ -194,7 +190,7 @@ contract StakingInfo is Ownable {
modifier StakeManagerOrValidatorContract(uint256 validatorId) {
address _contract;
address _stakeManager = registry.getStakeManagerAddress();
(, , , , , , _contract, , , , ,,) = IStakeManagerLocal(_stakeManager).validators(
(, , , , , , _contract, ) = IStakeManagerLocal(_stakeManager).validators(
validatorId
);
require(_contract == msg.sender || _stakeManager == msg.sender,
Expand Down Expand Up @@ -398,24 +394,28 @@ contract StakingInfo is Ownable {
uint256 activationEpoch,
uint256 deactivationEpoch,
address signer,
IStakeManagerLocal.Status status
uint256 _status
)
{
IStakeManagerLocal stakeManager = IStakeManagerLocal(
registry.getStakeManagerAddress()
);
uint256 delegatorsReward;
address _contract;
IStakeManagerLocal.Status status;
(
amount,
reward,
activationEpoch,
deactivationEpoch,
,
signer,
,
status, , , delegatorsReward,,
_contract,
status
) = stakeManager.validators(validatorId);
reward = reward.add(delegatorsReward);
_status = uint256(status);
if (_contract != address(0x0)) {
reward += IStakeManagerLocal(_contract).validatorRewards();
}
}

function totalValidatorStake(uint256 validatorId)
Expand All @@ -424,12 +424,13 @@ contract StakingInfo is Ownable {
returns (uint256 validatorStake)
{
address contractAddress;
uint256 delegatedAmount;
(validatorStake, , , , , ,contractAddress, , , , ,delegatedAmount,) = IStakeManagerLocal(
(validatorStake, , , , , , contractAddress, ) = IStakeManagerLocal(
registry.getStakeManagerAddress()
)
.validators(validatorId);
validatorStake = validatorStake.add(delegatedAmount);
if (contractAddress != address(0x0)) {
validatorStake += IStakeManagerLocal(contractAddress).activeAmount();
}
}

function getAccountStateRoot()
Expand All @@ -446,7 +447,7 @@ contract StakingInfo is Ownable {
view
returns (address ValidatorContract)
{
(, , , , , , ValidatorContract, , , , ,,) = IStakeManagerLocal(
(, , , , , , ValidatorContract, ) = IStakeManagerLocal(
registry.getStakeManagerAddress()
)
.validators(validatorId);
Expand Down Expand Up @@ -494,11 +495,12 @@ contract StakingInfo is Ownable {
emit DelegatorUnstaked(validatorId, user, amount);
}

// deprecated
function logUpdateCommissionRate(
uint256 validatorId,
uint256 newCommissionRate,
uint256 oldCommissionRate
) public onlyStakeManager {
) public onlyValidatorContract(validatorId) {
emit UpdateCommissionRate(
validatorId,
newCommissionRate,
Expand Down
19 changes: 8 additions & 11 deletions contracts/staking/stakeManager/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ contract StakeManager is
address _extensionCode
) external onlyGovernance {
require(isContract(_extensionCode));
eventsHub = address(0x0);
extensionCode = _extensionCode;
NFTContract = StakingNFT(_NFTContract);
logger = StakingInfo(_stakingLogger);
Expand Down Expand Up @@ -638,18 +639,14 @@ contract StakeManager is
function updateCommissionRate(uint256 validatorId, uint256 newCommissionRate) external onlyStaker(validatorId) {
_updateRewards(validatorId);

uint256 _epoch = currentEpoch;
uint256 _lastCommissionUpdate = validators[validatorId].lastCommissionUpdate;

require( // withdrawalDelay == dynasty
(_lastCommissionUpdate.add(WITHDRAWAL_DELAY) <= _epoch) || _lastCommissionUpdate == 0, // For initial setting of commission rate
"Cooldown"
delegatedFwd(
extensionCode,
abi.encodeWithSelector(
StakeManagerExtension(extensionCode).updateCommissionRate.selector,
validatorId,
newCommissionRate
)
);

require(newCommissionRate <= MAX_COMMISION_RATE, "Incorrect value");
logger.logUpdateCommissionRate(validatorId, newCommissionRate, validators[validatorId].commissionRate);
validators[validatorId].commissionRate = newCommissionRate;
validators[validatorId].lastCommissionUpdate = _epoch;
}

function withdrawDelegatorsReward(uint256 validatorId) public onlyDelegation(validatorId) returns (uint256) {
Expand Down
17 changes: 16 additions & 1 deletion contracts/staking/stakeManager/StakeManagerExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ contract StakeManagerExtension is StakeManagerStorage, Initializable, StakeManag
ValidatorShare contractAddress = ValidatorShare(validators[i].contractAddress);
if (contractAddress != ValidatorShare(0)) {
validators[i].delegatorsReward = contractAddress.validatorRewards_deprecated().add(INITIALIZED_AMOUNT);
validators[i].delegatedAmount = contractAddress.activeAmount_deprecated();
validators[i].delegatedAmount = contractAddress.activeAmount();
validators[i].commissionRate = contractAddress.commissionRate_deprecated();
}

Expand All @@ -153,6 +153,21 @@ contract StakeManagerExtension is StakeManagerStorage, Initializable, StakeManag
_getOrCacheEventsHub().logRewardParams(_rewardDecreasePerCheckpoint, _maxRewardedCheckpoints, _checkpointRewardDelta);
}

function updateCommissionRate(uint256 validatorId, uint256 newCommissionRate) external {
uint256 _epoch = currentEpoch;
uint256 _lastCommissionUpdate = validators[validatorId].lastCommissionUpdate;

require( // withdrawalDelay == dynasty
(_lastCommissionUpdate.add(WITHDRAWAL_DELAY) <= _epoch) || _lastCommissionUpdate == 0, // For initial setting of commission rate
"Cooldown"
);

require(newCommissionRate <= MAX_COMMISION_RATE, "Incorrect value");
_getOrCacheEventsHub().logUpdateCommissionRate(validatorId, newCommissionRate, validators[validatorId].commissionRate);
validators[validatorId].commissionRate = newCommissionRate;
validators[validatorId].lastCommissionUpdate = _epoch;
}

function _getOrCacheEventsHub() private returns(EventsHub) {
EventsHub _eventsHub = EventsHub(eventsHub);
if (_eventsHub == EventsHub(0x0)) {
Expand Down
10 changes: 8 additions & 2 deletions contracts/staking/validatorShare/ValidatorShare.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I

uint256 public totalStake_deprecated;
uint256 public rewardPerShare;
uint256 public activeAmount_deprecated;
uint256 public activeAmount;

bool public delegation;

Expand Down Expand Up @@ -172,6 +172,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
_burn(user, shares);

stakeManager.updateValidatorState(validatorId, -int256(amount));
activeAmount = activeAmount.sub(amount);

stakingLogger.logShareBurned(validatorId, user, amount, shares);
stakingLogger.logStakeUpdate(validatorId);
Expand Down Expand Up @@ -203,7 +204,10 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
uint256 _amountToSlashWithdrawalPool = _withdrawPool.mul(_amountToSlash).div(delegationAmount);

// slash inactive pool
stakeManager.decreaseValidatorDelegatedAmount(validatorId, _amountToSlash.sub(_amountToSlashWithdrawalPool));
uint256 stakeSlashed = _amountToSlash.sub(_amountToSlashWithdrawalPool);
stakeManager.decreaseValidatorDelegatedAmount(validatorId, stakeSlashed);
activeAmount = activeAmount.sub(stakeSlashed);

withdrawPool = withdrawPool.sub(_amountToSlashWithdrawalPool);
return _amountToSlash;
}
Expand Down Expand Up @@ -277,6 +281,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I

_burn(msg.sender, shares);
stakeManager.updateValidatorState(validatorId, -int256(claimAmount));
activeAmount = activeAmount.sub(claimAmount);

uint256 _withdrawPoolShare = claimAmount.mul(precision).div(withdrawExchangeRate());
withdrawPool = withdrawPool.add(claimAmount);
Expand Down Expand Up @@ -372,6 +377,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
_amount = rate.mul(shares).div(precision);

stakeManager.updateValidatorState(validatorId, int256(_amount));
activeAmount = activeAmount.add(_amount);

StakingInfo logger = stakingLogger;
logger.logShareMinted(validatorId, user, _amount, shares);
Expand Down

0 comments on commit f2da660

Please sign in to comment.