Do not store global inactive state

This commit is contained in:
Amir Bandeali
2019-10-05 18:05:11 -07:00
parent aa0a1bb54d
commit 13afc65b54
6 changed files with 63 additions and 45 deletions

View File

@@ -52,6 +52,24 @@ contract ZrxVault is
// Asset data for the ERC20 Proxy // Asset data for the ERC20 Proxy
bytes internal _zrxAssetData; bytes internal _zrxAssetData;
/// @dev Only stakingProxy can call this function.
modifier onlyStakingProxy() {
_assertSenderIsStakingProxy();
_;
}
/// @dev Function can only be called in catastrophic failure mode.
modifier onlyInCatastrophicFailure() {
_assertInCatastrophicFailure();
_;
}
/// @dev Function can only be called not in catastropic failure mode
modifier onlyNotInCatastrophicFailure() {
_assertNotInCatastrophicFailure();
_;
}
/// @dev Constructor. /// @dev Constructor.
/// @param _zrxProxyAddress Address of the 0x Zrx Proxy. /// @param _zrxProxyAddress Address of the 0x Zrx Proxy.
/// @param _zrxTokenAddress Address of the Zrx Token. /// @param _zrxTokenAddress Address of the Zrx Token.
@@ -169,6 +187,15 @@ contract ZrxVault is
return _balances[staker]; return _balances[staker];
} }
/// @dev Returns the entire balance of Zrx tokens in the vault.
function balanceOfVault()
external
view
returns (uint256)
{
return _zrxToken.balanceOf(address(this));
}
/// @dev Withdraw an `amount` of Zrx Tokens to `staker` from the vault. /// @dev Withdraw an `amount` of Zrx Tokens to `staker` from the vault.
/// @param staker of Zrx Tokens. /// @param staker of Zrx Tokens.
/// @param amount of Zrx Tokens to withdraw. /// @param amount of Zrx Tokens to withdraw.
@@ -190,21 +217,7 @@ contract ZrxVault is
); );
} }
modifier onlyStakingProxy() { /// @dev Asserts that sender is stakingProxy contract.
_assertSenderIsStakingProxy();
_;
}
modifier onlyInCatastrophicFailure() {
_assertInCatastrophicFailure();
_;
}
modifier onlyNotInCatastrophicFailure() {
_assertNotInCatastrophicFailure();
_;
}
function _assertSenderIsStakingProxy() function _assertSenderIsStakingProxy()
private private
view view
@@ -216,6 +229,7 @@ contract ZrxVault is
} }
} }
/// @dev Asserts that vault is in catastrophic failure mode.
function _assertInCatastrophicFailure() function _assertInCatastrophicFailure()
private private
view view
@@ -225,6 +239,7 @@ contract ZrxVault is
} }
} }
/// @dev Asserts that vault is not in catastrophic failure mode.
function _assertNotInCatastrophicFailure() function _assertNotInCatastrophicFailure()
private private
view view

View File

@@ -41,10 +41,9 @@ contract MixinStorage is
// address for read-only proxy to call // address for read-only proxy to call
address public readOnlyProxyCallee; address public readOnlyProxyCallee;
// mapping from StakeStatus to the total amount of stake in that status for the entire // amount of stake currently being delegated in the system
// staking system.
// (access using _loadSyncedBalance or _loadUnsyncedBalance) // (access using _loadSyncedBalance or _loadUnsyncedBalance)
mapping (uint8 => IStructs.StoredBalance) internal _globalStakeByStatus; IStructs.StoredBalance internal _globalDelegatedStake;
// mapping from StakeStatus to address of staker to stored balance // mapping from StakeStatus to address of staker to stored balance
// (access using _loadSyncedBalance or _loadUnsyncedBalance) // (access using _loadSyncedBalance or _loadUnsyncedBalance)

View File

@@ -95,4 +95,10 @@ interface IZrxVault {
external external
view view
returns (uint256); returns (uint256);
/// @dev Returns the entire balance of Zrx tokens in the vault.
function balanceOfVault()
external
view
returns (uint256);
} }

View File

@@ -46,12 +46,6 @@ contract MixinStake is
amount amount
); );
// update global total of active stake
_increaseCurrentAndNextBalance(
_globalStakeByStatus[uint8(IStructs.StakeStatus.INACTIVE)],
amount
);
// notify // notify
emit Stake( emit Stake(
staker, staker,
@@ -92,12 +86,6 @@ contract MixinStake is
amount amount
); );
// update global total of inactive stake
_decreaseCurrentAndNextBalance(
_globalStakeByStatus[uint8(IStructs.StakeStatus.INACTIVE)],
amount
);
// withdraw equivalent amount of ZRX from vault // withdraw equivalent amount of ZRX from vault
getZrxVault().withdrawFrom(staker, amount); getZrxVault().withdrawFrom(staker, amount);
@@ -149,13 +137,6 @@ contract MixinStake is
amount amount
); );
// update global total of stake in the statuses being moved between
_moveStake(
_globalStakeByStatus[uint8(from.status)],
_globalStakeByStatus[uint8(to.status)],
amount
);
// notify // notify
emit MoveStake( emit MoveStake(
staker, staker,
@@ -197,6 +178,12 @@ contract MixinStake is
_delegatedStakeByPoolId[poolId], _delegatedStakeByPoolId[poolId],
amount amount
); );
// Increase next balance of global delegated stake
_increaseNextBalance(
_globalDelegatedStake,
amount
);
} }
/// @dev Un-Delegates a owners stake from a staking pool. /// @dev Un-Delegates a owners stake from a staking pool.
@@ -229,5 +216,11 @@ contract MixinStake is
_delegatedStakeByPoolId[poolId], _delegatedStakeByPoolId[poolId],
amount amount
); );
// decrease next balance of global delegated stake
_decreaseNextBalance(
_globalDelegatedStake,
amount
);
} }
} }

View File

@@ -30,22 +30,27 @@ contract MixinStakeBalances is
using LibSafeMath for uint256; using LibSafeMath for uint256;
/// @dev Gets global stake for a given status. /// @dev Gets global stake for a given status.
/// @param stakeStatus ACTIVE, INACTIVE, or DELEGATED /// @param stakeStatus INACTIVE or DELEGATED
/// @return Global stake for given status. /// @return Global stake for given status.
function getGlobalStakeByStatus(IStructs.StakeStatus stakeStatus) function getGlobalStakeByStatus(IStructs.StakeStatus stakeStatus)
external external
view view
returns (IStructs.StoredBalance memory balance) returns (IStructs.StoredBalance memory balance)
{ {
balance = _loadSyncedBalance( balance = _loadSyncedBalance(_globalDelegatedStake);
_globalStakeByStatus[uint8(stakeStatus)] if (stakeStatus == IStructs.StakeStatus.INACTIVE) {
); // Inactive stake is the difference between total stake and delegated stake
// Note that any Zrx erroneously sent to the vault will be counted as inactive stake
uint256 totalStake = getZrxVault().balanceOfVault();
balance.currentEpochBalance = totalStake.safeSub(balance.currentEpochBalance).downcastToUint96();
balance.nextEpochBalance = totalStake.safeSub(balance.nextEpochBalance).downcastToUint96();
}
return balance; return balance;
} }
/// @dev Gets an owner's stake balances by status. /// @dev Gets an owner's stake balances by status.
/// @param staker Owner of stake. /// @param staker Owner of stake.
/// @param stakeStatus ACTIVE, INACTIVE, or DELEGATED /// @param stakeStatus INACTIVE or DELEGATED
/// @return Owner's stake balances for given status. /// @return Owner's stake balances for given status.
function getOwnerStakeByStatus( function getOwnerStakeByStatus(
address staker, address staker,
@@ -65,7 +70,7 @@ contract MixinStakeBalances is
/// @param staker of stake. /// @param staker of stake.
/// @return Total active stake for staker. /// @return Total active stake for staker.
function getTotalStake(address staker) function getTotalStake(address staker)
external public
view view
returns (uint256) returns (uint256)
{ {

View File

@@ -119,8 +119,8 @@ contract TestStorageLayoutAndConstants is
slot := add(slot, 0x1) slot := add(slot, 0x1)
assertSlotAndOffset( assertSlotAndOffset(
_globalStakeByStatus_slot, _globalDelegatedStake_slot,
_globalStakeByStatus_offset, _globalDelegatedStake_offset,
slot, slot,
offset offset
) )