Skip to content

Commit

Permalink
Merge branch 'vg/feat/onchain-governor' into vg/feat/treasury-contrib…
Browse files Browse the repository at this point in the history
…ution
  • Loading branch information
victorges committed Aug 18, 2023
2 parents cd6d147 + f0fb8ee commit cf12cb4
Show file tree
Hide file tree
Showing 27 changed files with 1,966 additions and 1,731 deletions.
91 changes: 48 additions & 43 deletions contracts/bonding/BondingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import "../token/ILivepeerToken.sol";
import "../token/IMinter.sol";
import "../rounds/IRoundsManager.sol";
import "../snapshots/IMerkleSnapshot.sol";
import "./IBondingCheckpoints.sol";
import "./IBondingVotes.sol";

import "@openzeppelin/contracts/utils/math/SafeMath.sol";

Expand Down Expand Up @@ -129,6 +129,11 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
_;
}

modifier autoCheckpoint(address account) {
_;
checkpointBondingState(account);
}

/**
* @notice BondingManager constructor. Only invokes constructor of base Manager contract with provided Controller address
* @dev This constructor will not initialize any state variables besides `controller`. The following setter functions
Expand Down Expand Up @@ -382,9 +387,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
address _finder,
uint256 _slashAmount,
uint256 _finderFee
) external whenSystemNotPaused onlyVerifier {
_autoClaimEarnings(_transcoder);

) external whenSystemNotPaused onlyVerifier autoClaimEarnings(_transcoder) autoCheckpoint(_transcoder) {
Delegator storage del = delegators[_transcoder];

if (del.bondedAmount > 0) {
Expand All @@ -405,8 +408,6 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
);
}

checkpointBondingState(_transcoder, del, transcoders[_transcoder]);

// Account for penalty
uint256 burnAmount = penalty;

Expand Down Expand Up @@ -434,7 +435,12 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
* @notice Claim token pools shares for a delegator from its lastClaimRound through the end round
* @param _endRound The last round for which to claim token pools shares for a delegator
*/
function claimEarnings(uint256 _endRound) external whenSystemNotPaused currentRoundInitialized {
function claimEarnings(uint256 _endRound)
external
whenSystemNotPaused
currentRoundInitialized
autoCheckpoint(msg.sender)
{
// Silence unused param compiler warning
_endRound;

Expand All @@ -447,7 +453,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
function setCurrentRoundTotalActiveStake() external onlyRoundsManager {
currentRoundTotalActiveStake = nextRoundTotalActiveStake;

bondingCheckpoints().checkpointTotalActiveStake(currentRoundTotalActiveStake, roundsManager().currentRound());
bondingVotes().checkpointTotalActiveStake(currentRoundTotalActiveStake, roundsManager().currentRound());
}

/**
Expand Down Expand Up @@ -565,9 +571,6 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
if (currPool.cumulativeRewardFactor == 0) {
currPool.cumulativeRewardFactor = cumulativeFactorsPool(newDelegate, newDelegate.lastRewardRound)
.cumulativeRewardFactor;
if (currPool.cumulativeRewardFactor == 0) {
currPool.cumulativeRewardFactor = PreciseMathUtils.percPoints(1, 1);
}
}
if (currPool.cumulativeFeeFactor == 0) {
currPool.cumulativeFeeFactor = cumulativeFactorsPool(newDelegate, newDelegate.lastFeeRound)
Expand All @@ -582,8 +585,6 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
// Update bonded amount
del.bondedAmount = currentBondedAmount.add(_amount);

checkpointBondingState(_owner, del, transcoders[_owner]);

increaseTotalStake(_to, delegationAmount, _currDelegateNewPosPrev, _currDelegateNewPosNext);

if (_amount > 0) {
Expand All @@ -592,6 +593,9 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
}

emit Bond(_to, currentDelegate, _owner, _amount, del.bondedAmount);

// the `autoCheckpoint` modifier has been replaced with its internal function as a `Stack too deep` error work-around
checkpointBondingState(_owner, del, transcoders[_to]);
}

/**
Expand All @@ -607,7 +611,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
// in the delegators doesn't get updated on bond or claim earnings though, so we use currentRound() + 1
// which is the only guaranteed round where the currently stored stake will be active.
uint256 startRound = roundsManager().currentRound() + 1;
bondingCheckpoints().checkpointBondingState(
bondingVotes().checkpointBondingState(
_owner,
startRound,
_delegator.bondedAmount,
Expand All @@ -621,10 +625,10 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
/**
* @notice Checkpoints the bonding state for a given account.
* @dev This is to allow checkpointing an account that has an inconsistent checkpoint with its current state.
* Implemented as a deploy utility to checkpoint the existing state when deploying the BondingCheckpoints contract.
* Implemented as a deploy utility to checkpoint the existing state when deploying the BondingVotes contract.
* @param _account The account to initialize the bonding checkpoint for
*/
function checkpointBondingState(address _account) external {
function checkpointBondingState(address _account) public {
checkpointBondingState(_account, delegators[_account], transcoders[_account]);
}

Expand Down Expand Up @@ -749,7 +753,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
uint256 _amount,
address _newPosPrev,
address _newPosNext
) public whenSystemNotPaused currentRoundInitialized autoClaimEarnings(msg.sender) {
) public whenSystemNotPaused currentRoundInitialized autoClaimEarnings(msg.sender) autoCheckpoint(msg.sender) {
require(delegatorStatus(msg.sender) == DelegatorStatus.Bonded, "caller must be bonded");

Delegator storage del = delegators[msg.sender];
Expand Down Expand Up @@ -780,9 +784,6 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
}
}

// No problem that startRound may have been cleared above, checkpoints are always made for currentRound()+1
checkpointBondingState(msg.sender, del, transcoders[msg.sender]);

// If msg.sender was resigned this statement will only decrease delegators[currentDelegate].delegatedAmount
decreaseTotalStake(currentDelegate, _amount, _newPosPrev, _newPosNext);

Expand Down Expand Up @@ -849,6 +850,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
public
whenSystemNotPaused
currentRoundInitialized
autoCheckpoint(msg.sender)
{
uint256 currentRound = roundsManager().currentRound();

Expand Down Expand Up @@ -901,8 +903,6 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
// Set last round that transcoder called reward
t.lastRewardRound = currentRound;

checkpointBondingState(msg.sender, delegators[msg.sender], t);

emit Reward(msg.sender, transcoderRewards);
}

Expand Down Expand Up @@ -1225,7 +1225,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
* @param _transcoder Storage pointer to a transcoder struct for a delegator's delegate
* @param _startRound The round for the start cumulative factors
* @param _endRound The round for the end cumulative factors. Normally this is the current round as historical
* lookup is only supported through BondingCheckpoints
* lookup is only supported through BondingVotes
* @param _stake The delegator's initial stake before including earned rewards
* @param _fees The delegator's initial fees before including earned fees
* @return cStake , cFees where cStake is the delegator's cumulative stake including earned rewards and cFees is the delegator's cumulative fees including earned fees
Expand Down Expand Up @@ -1291,6 +1291,19 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
uint256 _amount,
address _newPosPrev,
address _newPosNext
) internal autoCheckpoint(_delegate) {
return increaseTotalStakeUncheckpointed(_delegate, _amount, _newPosPrev, _newPosNext);
}

/**
* @dev Implementation of increaseTotalStake that does not checkpoint the caller, to be used by functions that
* guarantee the checkpointing themselves.
*/
function increaseTotalStakeUncheckpointed(
address _delegate,
uint256 _amount,
address _newPosPrev,
address _newPosNext
) internal {
Transcoder storage t = transcoders[_delegate];

Expand Down Expand Up @@ -1322,12 +1335,8 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
}
}

Delegator storage del = delegators[_delegate];

// Increase delegate's delegated amount
del.delegatedAmount = newStake;

checkpointBondingState(_delegate, del, t);
delegators[_delegate].delegatedAmount = newStake;
}

/**
Expand All @@ -1340,7 +1349,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
uint256 _amount,
address _newPosPrev,
address _newPosNext
) internal {
) internal autoCheckpoint(_delegate) {
Transcoder storage t = transcoders[_delegate];

uint256 currStake = transcoderTotalStake(_delegate);
Expand All @@ -1365,12 +1374,8 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
t.earningsPoolPerRound[nextRound].setStake(newStake);
}

Delegator storage del = delegators[_delegate];

// Decrease old delegate's delegated amount
del.delegatedAmount = newStake;

checkpointBondingState(_delegate, del, t);
delegators[_delegate].delegatedAmount = newStake;
}

/**
Expand Down Expand Up @@ -1438,7 +1443,8 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {

/**
* @dev Update a transcoder with rewards and update the transcoder pool with an optional list hint if needed.
* See SortedDoublyLL.sol for details on list hints
* See SortedDoublyLL.sol for details on list hints. This function updates the transcoder state but does not
* checkpoint it as it assumes the caller will ensure that.
* @param _transcoder Address of transcoder
* @param _rewards Amount of rewards
* @param _round Round that transcoder is updated
Expand Down Expand Up @@ -1474,11 +1480,14 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
// the earnings claiming algorithm and instead that amount is accounted for in the transcoder's cumulativeRewards field
earningsPool.updateCumulativeRewardFactor(prevEarningsPool, delegatorsRewards);
// Update transcoder's total stake with rewards
increaseTotalStake(_transcoder, _rewards, _newPosPrev, _newPosNext);
increaseTotalStakeUncheckpointed(_transcoder, _rewards, _newPosPrev, _newPosNext);
}

/**
* @dev Update a delegator with token pools shares from its lastClaimRound through a given round
*
* Notice that this function udpates the delegator storage but does not checkpoint its state. Since it is internal
* it assumes the top-level caller will checkpoint it instead.
* @param _delegator Delegator address
* @param _endRound The last round for which to update a delegator's stake with earnings pool shares
* @param _lastClaimRound The round for which a delegator has last claimed earnings
Expand Down Expand Up @@ -1537,8 +1546,6 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
// Rewards are bonded by default
del.bondedAmount = currentBondedAmount;
del.fees = currentFees;

checkpointBondingState(_delegator, del, transcoders[_delegator]);
}

/**
Expand All @@ -1554,7 +1561,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
uint256 _unbondingLockId,
address _newPosPrev,
address _newPosNext
) internal {
) internal autoCheckpoint(_delegator) {
Delegator storage del = delegators[_delegator];
UnbondingLock storage lock = del.unbondingLocks[_unbondingLockId];

Expand All @@ -1564,8 +1571,6 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
// Increase delegator's bonded amount
del.bondedAmount = del.bondedAmount.add(amount);

checkpointBondingState(_delegator, del, transcoders[_delegator]);

// Delete lock
delete del.unbondingLocks[_unbondingLockId];

Expand Down Expand Up @@ -1610,8 +1615,8 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
return payable(controller.getContract(keccak256("Treasury")));
}

function bondingCheckpoints() internal view returns (IBondingCheckpoints) {
return IBondingCheckpoints(controller.getContract(keccak256("BondingCheckpoints")));
function bondingVotes() internal view returns (IBondingVotes) {
return IBondingVotes(controller.getContract(keccak256("BondingVotes")));
}

function _onlyTicketBroker() internal view {
Expand Down
Loading

0 comments on commit cf12cb4

Please sign in to comment.