From 196cc4313f24eb2e6d775b69b995a70750c1dbbf Mon Sep 17 00:00:00 2001 From: Lawrence Forman Date: Mon, 23 Sep 2019 13:14:14 -0400 Subject: [PATCH] `@0x/contract-staking`: Make solidity uniformly hideous ;-). --- .../contracts/src/fees/MixinExchangeFees.sol | 9 +++---- .../contracts/src/libs/LibCobbDouglas.sol | 4 +-- .../staking_pools/MixinCumulativeRewards.sol | 3 +-- .../staking_pools/MixinStakingPoolRewards.sol | 15 ++++------- .../contracts/src/sys/MixinFinalizer.sol | 3 +-- .../contracts/test/TestDelegatorRewards.sol | 27 +++++++------------ .../staking/contracts/test/TestFinalizer.sol | 17 +++++++----- 7 files changed, 32 insertions(+), 46 deletions(-) diff --git a/contracts/staking/contracts/src/fees/MixinExchangeFees.sol b/contracts/staking/contracts/src/fees/MixinExchangeFees.sol index c838a8f50d..91e9f92403 100644 --- a/contracts/staking/contracts/src/fees/MixinExchangeFees.sol +++ b/contracts/staking/contracts/src/fees/MixinExchangeFees.sol @@ -89,8 +89,7 @@ contract MixinExchangeFees is return; } - uint256 poolStake = - getTotalStakeDelegatedToPool(poolId).currentEpochBalance; + uint256 poolStake = getTotalStakeDelegatedToPool(poolId).currentEpochBalance; // Ignore pools with dust stake. if (poolStake < minimumPoolStake) { return; @@ -105,12 +104,10 @@ contract MixinExchangeFees is // If the pool was previously inactive in this epoch, initialize it. if (pool.feesCollected == 0) { // Compute member and total weighted stake. - (pool.membersStake, pool.weightedStake) = - _computeMembersAndWeightedStake(poolId, poolStake); + (pool.membersStake, pool.weightedStake) = _computeMembersAndWeightedStake(poolId, poolStake); // Increase the total weighted stake. - totalWeightedStakeThisEpoch = - totalWeightedStakeThisEpoch.safeAdd(pool.weightedStake); + totalWeightedStakeThisEpoch = totalWeightedStakeThisEpoch.safeAdd(pool.weightedStake); // Increase the number of active pools. numActivePoolsThisEpoch += 1; diff --git a/contracts/staking/contracts/src/libs/LibCobbDouglas.sol b/contracts/staking/contracts/src/libs/LibCobbDouglas.sol index aec288fe1f..347758bde2 100644 --- a/contracts/staking/contracts/src/libs/LibCobbDouglas.sol +++ b/contracts/staking/contracts/src/libs/LibCobbDouglas.sol @@ -71,9 +71,9 @@ library LibCobbDouglas { // `totalRewards * stakeRatio / e^(alpha * (ln(stakeRatio / feeRatio)))` // Compute - // `e^(alpha * (ln(feeRatio/stakeRatio)))` if feeRatio <= stakeRatio + // `e^(alpha * ln(feeRatio/stakeRatio))` if feeRatio <= stakeRatio // or - // `e^(ln(stakeRatio/feeRatio))` if feeRatio > stakeRatio + // `e^(alpa * ln(stakeRatio/feeRatio))` if feeRatio > stakeRatio int256 n = feeRatio <= stakeRatio ? LibFixedMath.div(feeRatio, stakeRatio) : LibFixedMath.div(stakeRatio, feeRatio); diff --git a/contracts/staking/contracts/src/staking_pools/MixinCumulativeRewards.sol b/contracts/staking/contracts/src/staking_pools/MixinCumulativeRewards.sol index a7498677a2..decfa1fc05 100644 --- a/contracts/staking/contracts/src/staking_pools/MixinCumulativeRewards.sol +++ b/contracts/staking/contracts/src/staking_pools/MixinCumulativeRewards.sol @@ -162,8 +162,7 @@ contract MixinCumulativeRewards is internal { // Check if we should do any work - uint256 currentMostRecentEpoch = - _cumulativeRewardsByPoolLastStored[poolId]; + uint256 currentMostRecentEpoch = _cumulativeRewardsByPoolLastStored[poolId]; if (epoch == currentMostRecentEpoch) { return; } diff --git a/contracts/staking/contracts/src/staking_pools/MixinStakingPoolRewards.sol b/contracts/staking/contracts/src/staking_pools/MixinStakingPoolRewards.sol index 21e2f4df33..f52f89d325 100644 --- a/contracts/staking/contracts/src/staking_pools/MixinStakingPoolRewards.sol +++ b/contracts/staking/contracts/src/staking_pools/MixinStakingPoolRewards.sol @@ -81,8 +81,7 @@ contract MixinStakingPoolRewards is // rewards. IStructs.Pool memory pool = _poolById[poolId]; // Get any unfinalized rewards. - (uint256 unfinalizedTotalRewards, uint256 unfinalizedMembersStake) = - _getUnfinalizedPoolRewards(poolId); + (uint256 unfinalizedTotalRewards, uint256 unfinalizedMembersStake) = _getUnfinalizedPoolRewards(poolId); // Get the operators' portion. (reward,) = _computeSplitStakingPoolRewards( pool.operatorShare, @@ -203,21 +202,18 @@ contract MixinStakingPoolRewards is // 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. - IStructs.Fraction memory mostRecentCumulativeReward = - _getMostRecentCumulativeReward(poolId); + IStructs.Fraction memory mostRecentCumulativeReward = _getMostRecentCumulativeReward(poolId); // Compute new cumulative reward IStructs.Fraction memory cumulativeReward; - (cumulativeReward.numerator, cumulativeReward.denominator) = - LibFractions.add( + (cumulativeReward.numerator, cumulativeReward.denominator) = LibFractions.add( mostRecentCumulativeReward.numerator, mostRecentCumulativeReward.denominator, membersReward, membersStake ); // Normalize to prevent overflows. - (cumulativeReward.numerator, cumulativeReward.denominator) = - LibFractions.normalize( + (cumulativeReward.numerator, cumulativeReward.denominator) = LibFractions.normalize( cumulativeReward.numerator, cumulativeReward.denominator ); @@ -404,8 +400,7 @@ contract MixinStakingPoolRewards is // Get the most recent cumulative reward, which will serve as a // reference point when updating dependencies - IStructs.Fraction memory mostRecentCumulativeReward = - _getMostRecentCumulativeReward(poolId); + IStructs.Fraction memory mostRecentCumulativeReward = _getMostRecentCumulativeReward(poolId); // Record dependency on current epoch. if (_delegatedStakeToPoolByOwner.currentEpochBalance != 0 diff --git a/contracts/staking/contracts/src/sys/MixinFinalizer.sol b/contracts/staking/contracts/src/sys/MixinFinalizer.sol index 8208141ba2..624e724d4f 100644 --- a/contracts/staking/contracts/src/sys/MixinFinalizer.sol +++ b/contracts/staking/contracts/src/sys/MixinFinalizer.sol @@ -286,8 +286,7 @@ contract MixinFinalizer is // Clip the reward to always be under // `rewardsAvailable - totalRewardsPaid`, // in case cobb-douglas overflows, which should be unlikely. - uint256 rewardsRemaining = - state.rewardsAvailable.safeSub(state.totalRewardsFinalized); + uint256 rewardsRemaining = state.rewardsAvailable.safeSub(state.totalRewardsFinalized); if (rewardsRemaining < rewards) { rewards = rewardsRemaining; } diff --git a/contracts/staking/contracts/test/TestDelegatorRewards.sol b/contracts/staking/contracts/test/TestDelegatorRewards.sol index 88eb9b5ead..1686f49e4b 100644 --- a/contracts/staking/contracts/test/TestDelegatorRewards.sol +++ b/contracts/staking/contracts/test/TestDelegatorRewards.sol @@ -82,8 +82,7 @@ contract TestDelegatorRewards is ) external { - unfinalizedPoolRewardsByEpoch[currentEpoch][poolId] = - UnfinalizedPoolReward({ + unfinalizedPoolRewardsByEpoch[currentEpoch][poolId] = UnfinalizedPoolReward({ operatorReward: operatorReward, membersReward: membersReward, membersStake: membersStake @@ -132,10 +131,8 @@ contract TestDelegatorRewards is external { _initGenesisCumulativeRewards(poolId); - IStructs.StoredBalance memory initialStake = - _delegatedStakeToPoolByOwner[delegator][poolId]; - IStructs.StoredBalance storage _stake = - _delegatedStakeToPoolByOwner[delegator][poolId]; + IStructs.StoredBalance memory initialStake = _delegatedStakeToPoolByOwner[delegator][poolId]; + IStructs.StoredBalance storage _stake = _delegatedStakeToPoolByOwner[delegator][poolId]; _stake.isInitialized = true; _stake.currentEpochBalance += uint96(stake); _stake.nextEpochBalance += uint96(stake); @@ -159,10 +156,8 @@ contract TestDelegatorRewards is external { _initGenesisCumulativeRewards(poolId); - IStructs.StoredBalance memory initialStake = - _delegatedStakeToPoolByOwner[delegator][poolId]; - IStructs.StoredBalance storage _stake = - _delegatedStakeToPoolByOwner[delegator][poolId]; + IStructs.StoredBalance memory initialStake = _delegatedStakeToPoolByOwner[delegator][poolId]; + IStructs.StoredBalance storage _stake = _delegatedStakeToPoolByOwner[delegator][poolId]; if (_stake.currentEpoch < currentEpoch) { _stake.currentEpochBalance = _stake.nextEpochBalance; } @@ -188,10 +183,8 @@ contract TestDelegatorRewards is external { _initGenesisCumulativeRewards(poolId); - IStructs.StoredBalance memory initialStake = - _delegatedStakeToPoolByOwner[delegator][poolId]; - IStructs.StoredBalance storage _stake = - _delegatedStakeToPoolByOwner[delegator][poolId]; + IStructs.StoredBalance memory initialStake = _delegatedStakeToPoolByOwner[delegator][poolId]; + IStructs.StoredBalance storage _stake = _delegatedStakeToPoolByOwner[delegator][poolId]; if (_stake.currentEpoch < currentEpoch) { _stake.currentEpochBalance = _stake.nextEpochBalance; } @@ -244,8 +237,7 @@ contract TestDelegatorRewards is uint256 membersStake ) { - UnfinalizedPoolReward memory reward = - unfinalizedPoolRewardsByEpoch[currentEpoch][poolId]; + UnfinalizedPoolReward memory reward = unfinalizedPoolRewardsByEpoch[currentEpoch][poolId]; delete unfinalizedPoolRewardsByEpoch[currentEpoch][poolId]; _setOperatorShare(poolId, reward.operatorReward, reward.membersReward); @@ -266,8 +258,7 @@ contract TestDelegatorRewards is uint256 membersStake ) { - UnfinalizedPoolReward storage reward = - unfinalizedPoolRewardsByEpoch[currentEpoch][poolId]; + UnfinalizedPoolReward storage reward = unfinalizedPoolRewardsByEpoch[currentEpoch][poolId]; totalReward = reward.operatorReward + reward.membersReward; membersStake = reward.membersStake; } diff --git a/contracts/staking/contracts/test/TestFinalizer.sol b/contracts/staking/contracts/test/TestFinalizer.sol index 2e897e914b..4ff64b5d33 100644 --- a/contracts/staking/contracts/test/TestFinalizer.sol +++ b/contracts/staking/contracts/test/TestFinalizer.sol @@ -77,8 +77,9 @@ contract TestFinalizer is external { require(feesCollected > 0, "FEES_MUST_BE_NONZERO"); - mapping (bytes32 => IStructs.ActivePool) storage activePools = - _getActivePoolsFromEpoch(currentEpoch); + mapping (bytes32 => IStructs.ActivePool) storage activePools = _getActivePoolsFromEpoch( + currentEpoch + ); IStructs.ActivePool memory pool = activePools[poolId]; require(pool.feesCollected == 0, "POOL_ALREADY_ADDED"); _operatorSharesByPool[poolId] = operatorShare; @@ -128,8 +129,9 @@ contract TestFinalizer is view returns (UnfinalizedPoolReward memory reward) { - (reward.totalReward, reward.membersStake) = - _getUnfinalizedPoolRewards(poolId); + (reward.totalReward, reward.membersStake) = _getUnfinalizedPoolRewards( + poolId + ); } /// @dev Expose `_getActivePoolFromEpoch`. @@ -151,8 +153,11 @@ contract TestFinalizer is returns (uint256 operatorReward, uint256 membersReward) { uint32 operatorShare = _operatorSharesByPool[poolId]; - (operatorReward, membersReward) = - _computeSplitStakingPoolRewards(operatorShare, reward, membersStake); + (operatorReward, membersReward) = _computeSplitStakingPoolRewards( + operatorShare, + reward, + membersStake + ); address(_operatorRewardsReceiver).transfer(operatorReward); address(_membersRewardsReceiver).transfer(membersReward); emit DepositStakingPoolRewards(poolId, reward, membersStake);