Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Readability Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hysz committed Sep 5, 2019
1 parent 20cabc1 commit cd23a16
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 167 deletions.
15 changes: 10 additions & 5 deletions contracts/staking/contracts/src/immutable/MixinStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,24 @@ contract MixinStorage is
address internal stakingContract;

// mapping from Owner to Amount of Active Stake
mapping (address => IStructs.DelayedBalance) internal activeStakeByOwner;
// (access using _loadAndSyncBalance or _loadUnsyncedBalance)
mapping (address => IStructs.StoredBalance) internal activeStakeByOwner;

// mapping from Owner to Amount of Inactive Stake
mapping (address => IStructs.DelayedBalance) internal inactiveStakeByOwner;
// (access using _loadAndSyncBalance or _loadUnsyncedBalance)
mapping (address => IStructs.StoredBalance) internal inactiveStakeByOwner;

// mapping from Owner to Amount Delegated
mapping (address => IStructs.DelayedBalance) internal delegatedStakeByOwner;
// (access using _loadAndSyncBalance or _loadUnsyncedBalance)
mapping (address => IStructs.StoredBalance) internal delegatedStakeByOwner;

// mapping from Owner to Pool Id to Amount Delegated
mapping (address => mapping (bytes32 => IStructs.DelayedBalance)) internal delegatedStakeToPoolByOwner;
// (access using _loadAndSyncBalance or _loadUnsyncedBalance)
mapping (address => mapping (bytes32 => IStructs.StoredBalance)) internal delegatedStakeToPoolByOwner;

// mapping from Pool Id to Amount Delegated
mapping (bytes32 => IStructs.DelayedBalance) internal delegatedStakeByPoolId;
// (access using _loadAndSyncBalance or _loadUnsyncedBalance)
mapping (bytes32 => IStructs.StoredBalance) internal delegatedStakeByPoolId;

// mapping from Owner to Amount of Withdrawable Stake
mapping (address => uint256) internal withdrawableStakeByOwner;
Expand Down
4 changes: 2 additions & 2 deletions contracts/staking/contracts/src/interfaces/IStakingEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ interface IStakingEvents {
event MoveStake(
address indexed owner,
uint256 amount,
uint8 fromState,
uint8 fromStatus,
bytes32 indexed fromPool,
uint8 toState,
uint8 toStatus,
bytes32 indexed toProol
);

Expand Down
30 changes: 18 additions & 12 deletions contracts/staking/contracts/src/interfaces/IStructs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ interface IStructs {
address makerAddress;
}

/// @dev State for Staking Pools (see MixinStakingPool).
/// @dev Status for Staking Pools (see MixinStakingPool).
/// @param operatorAddress Address of pool operator.
/// @param operatorShare Portion of pool rewards owned by operator.
struct Pool {
address payable operatorAddress;
uint8 operatorShare;
}

/// @dev State for a pool that actively traded during the current epoch.
/// @dev Status for a pool that actively traded during the current epoch.
/// (see MixinExchangeFees).
/// @param poolId Unique Id of staking pool.
/// @param feesCollected Fees collected in ETH by this pool in the current epoch.
Expand All @@ -59,11 +59,17 @@ interface IStructs {
uint256 delegatedStake;
}

/// @dev A delayed balance allows values to be computed
struct DelayedBalance {
/// @dev Encapsulates a balance for the current and next epochs.
/// Note that these balances may be stale if the current epoch
/// is greater than `storedAtEpoch`.
/// Always load this struct using _loadAndSyncBalance or _loadUnsyncedBalance.
/// @param current balance in the current epoch.
/// @param next balance in the next epoch.
/// @param storedAtEpoch the reference epoch for this struct.
struct StoredBalance {
uint96 current;
uint96 next;
uint64 lastStored;
uint64 storedAtEpoch;
}

/// @dev Balance struct for stake.
Expand All @@ -74,18 +80,18 @@ interface IStructs {
uint256 next;
}

/// @dev States that stake can exist in.
enum StakeState {
/// @dev Statuses that stake can exist in.
enum StakeStatus {
ACTIVE,
INACTIVE,
DELEGATED
}

/// @dev Info used to describe a state.
/// @param state of the stake.
/// @param poolId Unique Id of pool. This is set when state=DELEGATED.
struct StakeStateInfo {
StakeState state;
/// @dev Info used to describe a status.
/// @param status of the stake.
/// @param poolId Unique Id of pool. This is set when status=DELEGATED.
struct StakeInfo {
StakeStatus status;
bytes32 poolId;
}

Expand Down
32 changes: 16 additions & 16 deletions contracts/staking/contracts/src/libs/LibStakingRichErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ library LibStakingRichErrors {
bytes4 internal constant POOL_ALREADY_EXISTS_ERROR_SELECTOR =
0x2a5e4dcf;

// bytes4(keccak256("EthVaultNotSet()"))
bytes4 internal constant ETH_VAULT_NOT_SET =
0xdb3f0be8;
// bytes4(keccak256("EthVaultNotSetError()"))
bytes4 internal constant ETH_VAULT_NOT_SET_ERROR_SELECTOR =
0xa067f596;

// bytes4(keccak256("RewardVaultNotSet()"))
bytes4 internal constant REWARD_VAULT_NOT_SET =
0xfcb260f7;
// bytes4(keccak256("RewardVaultNotSetError()"))
bytes4 internal constant REWARD_VAULT_NOT_SET_ERROR_SELECTOR =
0xe6976d70;

// bytes4(keccak256("InvalidStakeState(uint256)"))
bytes4 internal constant INVALID_STAKE_STATE =
0xe6586728;
// bytes4(keccak256("InvalidStakeStatusError(uint256)"))
bytes4 internal constant INVALID_STAKE_STATUS_ERROR_SELECTOR =
0xb7161acd;

// solhint-disable func-name-mixedcase
function MiscalculatedRewardsError(
Expand Down Expand Up @@ -456,34 +456,34 @@ library LibStakingRichErrors {
);
}

function EthVaultNotSet()
function EthVaultNotSetError()
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
ETH_VAULT_NOT_SET
ETH_VAULT_NOT_SET_ERROR_SELECTOR
);
}

function RewardVaultNotSet()
function RewardVaultNotSetError()
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
REWARD_VAULT_NOT_SET
REWARD_VAULT_NOT_SET_ERROR_SELECTOR
);
}

function InvalidStakeState(uint256 state)
function InvalidStakeStatusError(uint256 status)
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
INVALID_STAKE_STATE,
state
INVALID_STAKE_STATUS_ERROR_SELECTOR,
status
);
}
}
76 changes: 38 additions & 38 deletions contracts/staking/contracts/src/stake/MixinStake.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ contract MixinStake is
using LibSafeMath for uint256;

/// @dev Stake ZRX tokens. Tokens are deposited into the ZRX Vault. Unstake to retrieve the ZRX.
/// Stake is in the 'Active' state.
/// Stake is in the 'Active' status.
/// @param amount of ZRX to stake.
function stake(uint256 amount)
external
Expand All @@ -55,7 +55,7 @@ contract MixinStake is
_depositFromOwnerIntoZrxVault(owner, amount);

// mint stake
_mintBalance(activeStakeByOwner[owner], amount);
_incrementCurrentAndNextBalance(activeStakeByOwner[owner], amount);

// notify
emit Stake(
Expand All @@ -65,7 +65,7 @@ contract MixinStake is
}

/// @dev Unstake. Tokens are withdrawn from the ZRX Vault and returned to the owner.
/// Stake must be in the 'inactive' state for at least one full epoch to unstake.
/// Stake must be in the 'inactive' status for at least one full epoch to unstake.
/// @param amount of ZRX to unstake.
function unstake(uint256 amount)
external
Expand All @@ -84,7 +84,7 @@ contract MixinStake is
}

// burn inactive stake
_burnBalance(inactiveStakeByOwner[owner], amount);
_decrementCurrentAndNextBalance(inactiveStakeByOwner[owner], amount);

// update withdrawable field
withdrawableStakeByOwner[owner] = currentWithdrawableStake.safeSub(amount);
Expand All @@ -99,67 +99,67 @@ contract MixinStake is
);
}

/// @dev Moves stake between states: 'active', 'inactive' or 'delegated'.
/// @dev Moves stake between statuses: 'active', 'inactive' or 'delegated'.
/// This change comes into effect next epoch.
/// @param from state to move stake out of.
/// @param to state to move stake into.
/// @param from status to move stake out of.
/// @param to status to move stake into.
/// @param amount of stake to move.
function moveStake(
IStructs.StakeStateInfo calldata from,
IStructs.StakeStateInfo calldata to,
IStructs.StakeInfo calldata from,
IStructs.StakeInfo calldata to,
uint256 amount
)
external
{
// sanity check - do nothing if moving stake between the same state
if (from.state != IStructs.StakeState.DELEGATED && from.state == to.state) {
// sanity check - do nothing if moving stake between the same status
if (from.status != IStructs.StakeStatus.DELEGATED && from.status == to.status) {
return;
} else if (from.state == IStructs.StakeState.DELEGATED && from.poolId == to.poolId) {
} else if (from.status == IStructs.StakeStatus.DELEGATED && from.poolId == to.poolId) {
return;
}

address payable owner = msg.sender;

// handle delegation; this must be done before moving stake as the current
// (out-of-sync) state is used during delegation.
if (from.state == IStructs.StakeState.DELEGATED) {
// (out-of-sync) status is used during delegation.
if (from.status == IStructs.StakeStatus.DELEGATED) {
_undelegateStake(
from.poolId,
owner,
amount
);
}

if (to.state == IStructs.StakeState.DELEGATED) {
if (to.status == IStructs.StakeStatus.DELEGATED) {
_delegateStake(
to.poolId,
owner,
amount
);
}

// cache the current withdrawal state if we're moving out of the inactive state.
uint256 cachedWithdrawableStakeByOwner = (from.state == IStructs.StakeState.INACTIVE)
// cache the current withdrawal amoiunt, which may change if we're moving out of the inactive status.
uint256 withdrawableStake = (from.status == IStructs.StakeStatus.INACTIVE)
? getWithdrawableStake(owner)
: 0;

// execute move
IStructs.DelayedBalance storage fromPtr = _getBalancePtrFromState(from.state);
IStructs.DelayedBalance storage toPtr = _getBalancePtrFromState(to.state);
IStructs.StoredBalance storage fromPtr = _getBalancePtrFromStatus(owner, from.status);
IStructs.StoredBalance storage toPtr = _getBalancePtrFromStatus(owner, to.status);
_moveStake(fromPtr, toPtr, amount);

// update withdrawable field, if necessary
if (from.state == IStructs.StakeState.INACTIVE) {
withdrawableStakeByOwner[owner] = _computeWithdrawableStake(owner, cachedWithdrawableStakeByOwner);
if (from.status == IStructs.StakeStatus.INACTIVE) {
withdrawableStakeByOwner[owner] = _computeWithdrawableStake(owner, withdrawableStake);
}

// notify
emit MoveStake(
owner,
amount,
uint8(from.state),
uint8(from.status),
from.poolId,
uint8(to.state),
uint8(to.status),
to.poolId
);
}
Expand All @@ -183,10 +183,10 @@ contract MixinStake is
_syncCumulativeRewardsNeededByDelegator(poolId, currentEpoch);

// increment how much stake the owner has delegated to the input pool
_incrementBalance(delegatedStakeToPoolByOwner[owner][poolId], amount);
_incrementNextBalance(delegatedStakeToPoolByOwner[owner][poolId], amount);

// increment how much stake has been delegated to pool
_incrementBalance(delegatedStakeByPoolId[poolId], amount);
_incrementNextBalance(delegatedStakeByPoolId[poolId], amount);
}

/// @dev Un-Delegates a owners stake from a staking pool.
Expand All @@ -208,33 +208,33 @@ contract MixinStake is
_syncCumulativeRewardsNeededByDelegator(poolId, currentEpoch);

// decrement how much stake the owner has delegated to the input pool
_decrementBalance(delegatedStakeToPoolByOwner[owner][poolId], amount);
_decrementNextBalance(delegatedStakeToPoolByOwner[owner][poolId], amount);

// decrement how much stake has been delegated to pool
_decrementBalance(delegatedStakeByPoolId[poolId], amount);
_decrementNextBalance(delegatedStakeByPoolId[poolId], amount);
}

/// @dev Returns a storage pointer to a user's stake in a given state.
/// @param state of user's stake to lookup.
/// @dev Returns a storage pointer to a user's stake in a given status.
/// @param owner of stake to query.
/// @param status of user's stake to lookup.
/// @return a storage pointer to the corresponding stake stake
function _getBalancePtrFromState(IStructs.StakeState state)
function _getBalancePtrFromStatus(address owner, IStructs.StakeStatus status)
private
view
returns (IStructs.DelayedBalance storage)
returns (IStructs.StoredBalance storage)
{
// lookup state
address owner = msg.sender;
if (state == IStructs.StakeState.ACTIVE) {
// lookup status
if (status == IStructs.StakeStatus.ACTIVE) {
return activeStakeByOwner[owner];
} else if (state == IStructs.StakeState.INACTIVE) {
} else if (status == IStructs.StakeStatus.INACTIVE) {
return inactiveStakeByOwner[owner];
} else if (state == IStructs.StakeState.DELEGATED) {
} else if (status == IStructs.StakeStatus.DELEGATED) {
return delegatedStakeByOwner[owner];
}

// invalid state
// invalid status
LibRichErrors.rrevert(
LibStakingRichErrors.InvalidStakeState(uint256(state))
LibStakingRichErrors.InvalidStakeStatusError(uint256(status))
);

// required to compile ~ we should never hit this.
Expand Down
Loading

0 comments on commit cd23a16

Please sign in to comment.