@0x/contract-staking: Make solidity uniformly hideous ;-).

This commit is contained in:
Lawrence Forman
2019-09-23 13:14:14 -04:00
parent 9dd8c61a2f
commit 196cc4313f
7 changed files with 32 additions and 46 deletions

View File

@@ -89,8 +89,7 @@ contract MixinExchangeFees is
return; return;
} }
uint256 poolStake = uint256 poolStake = getTotalStakeDelegatedToPool(poolId).currentEpochBalance;
getTotalStakeDelegatedToPool(poolId).currentEpochBalance;
// Ignore pools with dust stake. // Ignore pools with dust stake.
if (poolStake < minimumPoolStake) { if (poolStake < minimumPoolStake) {
return; return;
@@ -105,12 +104,10 @@ contract MixinExchangeFees is
// If the pool was previously inactive in this epoch, initialize it. // If the pool was previously inactive in this epoch, initialize it.
if (pool.feesCollected == 0) { if (pool.feesCollected == 0) {
// Compute member and total weighted stake. // Compute member and total weighted stake.
(pool.membersStake, pool.weightedStake) = (pool.membersStake, pool.weightedStake) = _computeMembersAndWeightedStake(poolId, poolStake);
_computeMembersAndWeightedStake(poolId, poolStake);
// Increase the total weighted stake. // Increase the total weighted stake.
totalWeightedStakeThisEpoch = totalWeightedStakeThisEpoch = totalWeightedStakeThisEpoch.safeAdd(pool.weightedStake);
totalWeightedStakeThisEpoch.safeAdd(pool.weightedStake);
// Increase the number of active pools. // Increase the number of active pools.
numActivePoolsThisEpoch += 1; numActivePoolsThisEpoch += 1;

View File

@@ -71,9 +71,9 @@ library LibCobbDouglas {
// `totalRewards * stakeRatio / e^(alpha * (ln(stakeRatio / feeRatio)))` // `totalRewards * stakeRatio / e^(alpha * (ln(stakeRatio / feeRatio)))`
// Compute // Compute
// `e^(alpha * (ln(feeRatio/stakeRatio)))` if feeRatio <= stakeRatio // `e^(alpha * ln(feeRatio/stakeRatio))` if feeRatio <= stakeRatio
// or // or
// `e^(ln(stakeRatio/feeRatio))` if feeRatio > stakeRatio // `e^(alpa * ln(stakeRatio/feeRatio))` if feeRatio > stakeRatio
int256 n = feeRatio <= stakeRatio ? int256 n = feeRatio <= stakeRatio ?
LibFixedMath.div(feeRatio, stakeRatio) : LibFixedMath.div(feeRatio, stakeRatio) :
LibFixedMath.div(stakeRatio, feeRatio); LibFixedMath.div(stakeRatio, feeRatio);

View File

@@ -162,8 +162,7 @@ contract MixinCumulativeRewards is
internal internal
{ {
// Check if we should do any work // Check if we should do any work
uint256 currentMostRecentEpoch = uint256 currentMostRecentEpoch = _cumulativeRewardsByPoolLastStored[poolId];
_cumulativeRewardsByPoolLastStored[poolId];
if (epoch == currentMostRecentEpoch) { if (epoch == currentMostRecentEpoch) {
return; return;
} }

View File

@@ -81,8 +81,7 @@ contract MixinStakingPoolRewards is
// rewards. // rewards.
IStructs.Pool memory pool = _poolById[poolId]; IStructs.Pool memory pool = _poolById[poolId];
// Get any unfinalized rewards. // Get any unfinalized rewards.
(uint256 unfinalizedTotalRewards, uint256 unfinalizedMembersStake) = (uint256 unfinalizedTotalRewards, uint256 unfinalizedMembersStake) = _getUnfinalizedPoolRewards(poolId);
_getUnfinalizedPoolRewards(poolId);
// Get the operators' portion. // Get the operators' portion.
(reward,) = _computeSplitStakingPoolRewards( (reward,) = _computeSplitStakingPoolRewards(
pool.operatorShare, pool.operatorShare,
@@ -203,21 +202,18 @@ contract MixinStakingPoolRewards is
// Fetch the last epoch at which we stored an entry for this pool; // Fetch the last epoch at which we stored an entry for this pool;
// this is the most up-to-date cumulative rewards for this pool. // this is the most up-to-date cumulative rewards for this pool.
IStructs.Fraction memory mostRecentCumulativeReward = IStructs.Fraction memory mostRecentCumulativeReward = _getMostRecentCumulativeReward(poolId);
_getMostRecentCumulativeReward(poolId);
// Compute new cumulative reward // Compute new cumulative reward
IStructs.Fraction memory cumulativeReward; IStructs.Fraction memory cumulativeReward;
(cumulativeReward.numerator, cumulativeReward.denominator) = (cumulativeReward.numerator, cumulativeReward.denominator) = LibFractions.add(
LibFractions.add(
mostRecentCumulativeReward.numerator, mostRecentCumulativeReward.numerator,
mostRecentCumulativeReward.denominator, mostRecentCumulativeReward.denominator,
membersReward, membersReward,
membersStake membersStake
); );
// Normalize to prevent overflows. // Normalize to prevent overflows.
(cumulativeReward.numerator, cumulativeReward.denominator) = (cumulativeReward.numerator, cumulativeReward.denominator) = LibFractions.normalize(
LibFractions.normalize(
cumulativeReward.numerator, cumulativeReward.numerator,
cumulativeReward.denominator cumulativeReward.denominator
); );
@@ -404,8 +400,7 @@ contract MixinStakingPoolRewards is
// Get the most recent cumulative reward, which will serve as a // Get the most recent cumulative reward, which will serve as a
// reference point when updating dependencies // reference point when updating dependencies
IStructs.Fraction memory mostRecentCumulativeReward = IStructs.Fraction memory mostRecentCumulativeReward = _getMostRecentCumulativeReward(poolId);
_getMostRecentCumulativeReward(poolId);
// Record dependency on current epoch. // Record dependency on current epoch.
if (_delegatedStakeToPoolByOwner.currentEpochBalance != 0 if (_delegatedStakeToPoolByOwner.currentEpochBalance != 0

View File

@@ -286,8 +286,7 @@ contract MixinFinalizer is
// Clip the reward to always be under // Clip the reward to always be under
// `rewardsAvailable - totalRewardsPaid`, // `rewardsAvailable - totalRewardsPaid`,
// in case cobb-douglas overflows, which should be unlikely. // in case cobb-douglas overflows, which should be unlikely.
uint256 rewardsRemaining = uint256 rewardsRemaining = state.rewardsAvailable.safeSub(state.totalRewardsFinalized);
state.rewardsAvailable.safeSub(state.totalRewardsFinalized);
if (rewardsRemaining < rewards) { if (rewardsRemaining < rewards) {
rewards = rewardsRemaining; rewards = rewardsRemaining;
} }

View File

@@ -82,8 +82,7 @@ contract TestDelegatorRewards is
) )
external external
{ {
unfinalizedPoolRewardsByEpoch[currentEpoch][poolId] = unfinalizedPoolRewardsByEpoch[currentEpoch][poolId] = UnfinalizedPoolReward({
UnfinalizedPoolReward({
operatorReward: operatorReward, operatorReward: operatorReward,
membersReward: membersReward, membersReward: membersReward,
membersStake: membersStake membersStake: membersStake
@@ -132,10 +131,8 @@ contract TestDelegatorRewards is
external external
{ {
_initGenesisCumulativeRewards(poolId); _initGenesisCumulativeRewards(poolId);
IStructs.StoredBalance memory initialStake = IStructs.StoredBalance memory initialStake = _delegatedStakeToPoolByOwner[delegator][poolId];
_delegatedStakeToPoolByOwner[delegator][poolId]; IStructs.StoredBalance storage _stake = _delegatedStakeToPoolByOwner[delegator][poolId];
IStructs.StoredBalance storage _stake =
_delegatedStakeToPoolByOwner[delegator][poolId];
_stake.isInitialized = true; _stake.isInitialized = true;
_stake.currentEpochBalance += uint96(stake); _stake.currentEpochBalance += uint96(stake);
_stake.nextEpochBalance += uint96(stake); _stake.nextEpochBalance += uint96(stake);
@@ -159,10 +156,8 @@ contract TestDelegatorRewards is
external external
{ {
_initGenesisCumulativeRewards(poolId); _initGenesisCumulativeRewards(poolId);
IStructs.StoredBalance memory initialStake = IStructs.StoredBalance memory initialStake = _delegatedStakeToPoolByOwner[delegator][poolId];
_delegatedStakeToPoolByOwner[delegator][poolId]; IStructs.StoredBalance storage _stake = _delegatedStakeToPoolByOwner[delegator][poolId];
IStructs.StoredBalance storage _stake =
_delegatedStakeToPoolByOwner[delegator][poolId];
if (_stake.currentEpoch < currentEpoch) { if (_stake.currentEpoch < currentEpoch) {
_stake.currentEpochBalance = _stake.nextEpochBalance; _stake.currentEpochBalance = _stake.nextEpochBalance;
} }
@@ -188,10 +183,8 @@ contract TestDelegatorRewards is
external external
{ {
_initGenesisCumulativeRewards(poolId); _initGenesisCumulativeRewards(poolId);
IStructs.StoredBalance memory initialStake = IStructs.StoredBalance memory initialStake = _delegatedStakeToPoolByOwner[delegator][poolId];
_delegatedStakeToPoolByOwner[delegator][poolId]; IStructs.StoredBalance storage _stake = _delegatedStakeToPoolByOwner[delegator][poolId];
IStructs.StoredBalance storage _stake =
_delegatedStakeToPoolByOwner[delegator][poolId];
if (_stake.currentEpoch < currentEpoch) { if (_stake.currentEpoch < currentEpoch) {
_stake.currentEpochBalance = _stake.nextEpochBalance; _stake.currentEpochBalance = _stake.nextEpochBalance;
} }
@@ -244,8 +237,7 @@ contract TestDelegatorRewards is
uint256 membersStake uint256 membersStake
) )
{ {
UnfinalizedPoolReward memory reward = UnfinalizedPoolReward memory reward = unfinalizedPoolRewardsByEpoch[currentEpoch][poolId];
unfinalizedPoolRewardsByEpoch[currentEpoch][poolId];
delete unfinalizedPoolRewardsByEpoch[currentEpoch][poolId]; delete unfinalizedPoolRewardsByEpoch[currentEpoch][poolId];
_setOperatorShare(poolId, reward.operatorReward, reward.membersReward); _setOperatorShare(poolId, reward.operatorReward, reward.membersReward);
@@ -266,8 +258,7 @@ contract TestDelegatorRewards is
uint256 membersStake uint256 membersStake
) )
{ {
UnfinalizedPoolReward storage reward = UnfinalizedPoolReward storage reward = unfinalizedPoolRewardsByEpoch[currentEpoch][poolId];
unfinalizedPoolRewardsByEpoch[currentEpoch][poolId];
totalReward = reward.operatorReward + reward.membersReward; totalReward = reward.operatorReward + reward.membersReward;
membersStake = reward.membersStake; membersStake = reward.membersStake;
} }

View File

@@ -77,8 +77,9 @@ contract TestFinalizer is
external external
{ {
require(feesCollected > 0, "FEES_MUST_BE_NONZERO"); require(feesCollected > 0, "FEES_MUST_BE_NONZERO");
mapping (bytes32 => IStructs.ActivePool) storage activePools = mapping (bytes32 => IStructs.ActivePool) storage activePools = _getActivePoolsFromEpoch(
_getActivePoolsFromEpoch(currentEpoch); currentEpoch
);
IStructs.ActivePool memory pool = activePools[poolId]; IStructs.ActivePool memory pool = activePools[poolId];
require(pool.feesCollected == 0, "POOL_ALREADY_ADDED"); require(pool.feesCollected == 0, "POOL_ALREADY_ADDED");
_operatorSharesByPool[poolId] = operatorShare; _operatorSharesByPool[poolId] = operatorShare;
@@ -128,8 +129,9 @@ contract TestFinalizer is
view view
returns (UnfinalizedPoolReward memory reward) returns (UnfinalizedPoolReward memory reward)
{ {
(reward.totalReward, reward.membersStake) = (reward.totalReward, reward.membersStake) = _getUnfinalizedPoolRewards(
_getUnfinalizedPoolRewards(poolId); poolId
);
} }
/// @dev Expose `_getActivePoolFromEpoch`. /// @dev Expose `_getActivePoolFromEpoch`.
@@ -151,8 +153,11 @@ contract TestFinalizer is
returns (uint256 operatorReward, uint256 membersReward) returns (uint256 operatorReward, uint256 membersReward)
{ {
uint32 operatorShare = _operatorSharesByPool[poolId]; uint32 operatorShare = _operatorSharesByPool[poolId];
(operatorReward, membersReward) = (operatorReward, membersReward) = _computeSplitStakingPoolRewards(
_computeSplitStakingPoolRewards(operatorShare, reward, membersStake); operatorShare,
reward,
membersStake
);
address(_operatorRewardsReceiver).transfer(operatorReward); address(_operatorRewardsReceiver).transfer(operatorReward);
address(_membersRewardsReceiver).transfer(membersReward); address(_membersRewardsReceiver).transfer(membersReward);
emit DepositStakingPoolRewards(poolId, reward, membersStake); emit DepositStakingPoolRewards(poolId, reward, membersStake);