Refactor tests to use new getters

This commit is contained in:
Amir Bandeali
2019-10-05 16:45:36 -07:00
parent 2ef546210d
commit 43399a9ad9
11 changed files with 96 additions and 151 deletions

View File

@@ -43,7 +43,8 @@ contract MixinStorage is
// mapping from StakeStatus to the total amount of stake in that status for the entire
// staking system.
mapping (uint8 => IStructs.StoredBalance) public globalStakeByStatus;
// (access using _loadSyncedBalance or _loadUnsyncedBalance)
mapping (uint8 => IStructs.StoredBalance) internal _globalStakeByStatus;
// mapping from StakeStatus to address of staker to stored balance
// (access using _loadSyncedBalance or _loadUnsyncedBalance)

View File

@@ -62,14 +62,6 @@ interface IStructs {
uint96 nextEpochBalance;
}
/// @dev Balance struct for stake.
/// @param currentEpochBalance Balance in the current epoch.
/// @param nextEpochBalance Balance in the next epoch.
struct StakeBalance {
uint256 currentEpochBalance;
uint256 nextEpochBalance;
}
/// @dev Statuses that stake can exist in.
enum StakeStatus {
ACTIVE,

View File

@@ -48,7 +48,7 @@ contract MixinStake is
// update global total of active stake
_increaseCurrentAndNextBalance(
globalStakeByStatus[uint8(IStructs.StakeStatus.ACTIVE)],
_globalStakeByStatus[uint8(IStructs.StakeStatus.ACTIVE)],
amount
);
@@ -94,7 +94,7 @@ contract MixinStake is
// update global total of inactive stake
_decreaseCurrentAndNextBalance(
globalStakeByStatus[uint8(IStructs.StakeStatus.INACTIVE)],
_globalStakeByStatus[uint8(IStructs.StakeStatus.INACTIVE)],
amount
);
@@ -151,8 +151,8 @@ contract MixinStake is
// update global total of stake in the statuses being moved between
_moveStake(
globalStakeByStatus[uint8(from.status)],
globalStakeByStatus[uint8(to.status)],
_globalStakeByStatus[uint8(from.status)],
_globalStakeByStatus[uint8(to.status)],
amount
);

View File

@@ -29,52 +29,36 @@ contract MixinStakeBalances is
{
using LibSafeMath for uint256;
/// @dev Returns the total active stake across the entire staking system.
/// @return Global active stake.
function getGlobalActiveStake()
/// @dev Gets global stake for a given status.
/// @param stakeStatus ACTIVE, INACTIVE, or DELEGATED
/// @return Global stake for given status.
function getGlobalStakeByStatus(IStructs.StakeStatus stakeStatus)
external
view
returns (IStructs.StakeBalance memory balance)
returns (IStructs.StoredBalance memory balance)
{
IStructs.StoredBalance memory storedBalance = _loadSyncedBalance(
globalStakeByStatus[uint8(IStructs.StakeStatus.ACTIVE)]
balance = _loadSyncedBalance(
_globalStakeByStatus[uint8(stakeStatus)]
);
return IStructs.StakeBalance({
currentEpochBalance: storedBalance.currentEpochBalance,
nextEpochBalance: storedBalance.nextEpochBalance
});
return balance;
}
/// @dev Returns the total inactive stake across the entire staking system.
/// @return Global inactive stake.
function getGlobalInactiveStake()
/// @dev Gets an owner's stake balances by status.
/// @param staker Owner of stake.
/// @param stakeStatus ACTIVE, INACTIVE, or DELEGATED
/// @return Owner's stake balances for given status.
function getOwnerStakeByStatus(
address staker,
IStructs.StakeStatus stakeStatus
)
external
view
returns (IStructs.StakeBalance memory balance)
returns (IStructs.StoredBalance memory balance)
{
IStructs.StoredBalance memory storedBalance = _loadSyncedBalance(
globalStakeByStatus[uint8(IStructs.StakeStatus.INACTIVE)]
balance = _loadSyncedBalance(
_ownerStakeByStatus[uint8(stakeStatus)][staker]
);
return IStructs.StakeBalance({
currentEpochBalance: storedBalance.currentEpochBalance,
nextEpochBalance: storedBalance.nextEpochBalance
});
}
/// @dev Returns the total stake delegated across the entire staking system.
/// @return Global delegated stake.
function getGlobalDelegatedStake()
external
view
returns (IStructs.StakeBalance memory balance)
{
IStructs.StoredBalance memory storedBalance = _loadSyncedBalance(
globalStakeByStatus[uint8(IStructs.StakeStatus.DELEGATED)]
);
return IStructs.StakeBalance({
currentEpochBalance: storedBalance.currentEpochBalance,
nextEpochBalance: storedBalance.nextEpochBalance
});
return balance;
}
/// @dev Returns the total stake for a given staker.
@@ -88,68 +72,17 @@ contract MixinStakeBalances is
return getZrxVault().balanceOf(staker);
}
/// @dev Returns the active stake for a given staker.
/// @param staker of stake.
/// @return Active stake for staker.
function getActiveStake(address staker)
external
view
returns (IStructs.StakeBalance memory balance)
{
IStructs.StoredBalance memory storedBalance =
_loadSyncedBalance(_ownerStakeByStatus[uint8(IStructs.StakeStatus.ACTIVE)][staker]);
return IStructs.StakeBalance({
currentEpochBalance: storedBalance.currentEpochBalance,
nextEpochBalance: storedBalance.nextEpochBalance
});
}
/// @dev Returns the inactive stake for a given staker.
/// @param staker of stake.
/// @return Inactive stake for staker.
function getInactiveStake(address staker)
external
view
returns (IStructs.StakeBalance memory balance)
{
IStructs.StoredBalance memory storedBalance =
_loadSyncedBalance(_ownerStakeByStatus[uint8(IStructs.StakeStatus.INACTIVE)][staker]);
return IStructs.StakeBalance({
currentEpochBalance: storedBalance.currentEpochBalance,
nextEpochBalance: storedBalance.nextEpochBalance
});
}
/// @dev Returns the stake delegated by a given staker.
/// @param staker of stake.
/// @return Delegated stake for staker.
function getStakeDelegatedByOwner(address staker)
external
view
returns (IStructs.StakeBalance memory balance)
{
IStructs.StoredBalance memory storedBalance =
_loadSyncedBalance(_ownerStakeByStatus[uint8(IStructs.StakeStatus.DELEGATED)][staker]);
return IStructs.StakeBalance({
currentEpochBalance: storedBalance.currentEpochBalance,
nextEpochBalance: storedBalance.nextEpochBalance
});
}
/// @dev Returns the stake delegated to a specific staking pool, by a given staker.
/// @param staker of stake.
/// @param poolId Unique Id of pool.
/// @return Stake delegaated to pool by staker.
/// @return Stake delegated to pool by staker.
function getStakeDelegatedToPoolByOwner(address staker, bytes32 poolId)
public
view
returns (IStructs.StakeBalance memory balance)
returns (IStructs.StoredBalance memory balance)
{
IStructs.StoredBalance memory storedBalance = _loadSyncedBalance(_delegatedStakeToPoolByOwner[staker][poolId]);
return IStructs.StakeBalance({
currentEpochBalance: storedBalance.currentEpochBalance,
nextEpochBalance: storedBalance.nextEpochBalance
});
balance = _loadSyncedBalance(_delegatedStakeToPoolByOwner[staker][poolId]);
return balance;
}
/// @dev Returns the total stake delegated to a specific staking pool,
@@ -159,12 +92,9 @@ contract MixinStakeBalances is
function getTotalStakeDelegatedToPool(bytes32 poolId)
public
view
returns (IStructs.StakeBalance memory balance)
returns (IStructs.StoredBalance memory balance)
{
IStructs.StoredBalance memory storedBalance = _loadSyncedBalance(_delegatedStakeByPoolId[poolId]);
return IStructs.StakeBalance({
currentEpochBalance: storedBalance.currentEpochBalance,
nextEpochBalance: storedBalance.nextEpochBalance
});
balance = _loadSyncedBalance(_delegatedStakeByPoolId[poolId]);
return balance;
}
}

View File

@@ -176,7 +176,7 @@ contract MixinStakeStorage is
private
{
// note - this compresses into a single `sstore` when optimizations are enabled,
// since the StakeBalance struct occupies a single word of storage.
// since the StoredBalance struct occupies a single word of storage.
balancePtr.currentEpoch = balance.currentEpoch;
balancePtr.nextEpochBalance = balance.nextEpochBalance;
balancePtr.currentEpochBalance = balance.currentEpochBalance;

View File

@@ -27,8 +27,8 @@ contract TestProtocolFees is
TestStakingNoWETH
{
struct TestPool {
uint256 operatorStake;
uint256 membersStake;
uint96 operatorStake;
uint96 membersStake;
mapping(address => bool) isMaker;
}
@@ -57,8 +57,8 @@ contract TestProtocolFees is
/// @dev Create a test pool.
function createTestPool(
bytes32 poolId,
uint256 operatorStake,
uint256 membersStake,
uint96 operatorStake,
uint96 membersStake,
address[] calldata makerAddresses
)
external
@@ -99,11 +99,12 @@ contract TestProtocolFees is
function getTotalStakeDelegatedToPool(bytes32 poolId)
public
view
returns (IStructs.StakeBalance memory balance)
returns (IStructs.StoredBalance memory balance)
{
TestPool memory pool = _testPools[poolId];
uint256 stake = pool.operatorStake + pool.membersStake;
return IStructs.StakeBalance({
uint96 stake = pool.operatorStake + pool.membersStake;
return IStructs.StoredBalance({
currentEpoch: currentEpoch.downcastToUint32(),
currentEpochBalance: stake,
nextEpochBalance: stake
});
@@ -113,10 +114,11 @@ contract TestProtocolFees is
function getStakeDelegatedToPoolByOwner(address, bytes32 poolId)
public
view
returns (IStructs.StakeBalance memory balance)
returns (IStructs.StoredBalance memory balance)
{
TestPool memory pool = _testPools[poolId];
return IStructs.StakeBalance({
return IStructs.StoredBalance({
currentEpoch: currentEpoch.downcastToUint32(),
currentEpochBalance: pool.operatorStake,
nextEpochBalance: pool.operatorStake
});

View File

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

View File

@@ -3,24 +3,24 @@ import { BigNumber, RevertError } from '@0x/utils';
import * as _ from 'lodash';
import { StakingApiWrapper } from '../utils/api_wrapper';
import { StakeBalance, StakeBalances, StakeInfo, StakeStatus } from '../utils/types';
import { StakeBalances, StakeInfo, StakeStatus, StoredBalance } from '../utils/types';
import { BaseActor } from './base_actor';
export class StakerActor extends BaseActor {
private readonly _poolIds: string[];
private static _incrementNextBalance(balance: StakeBalance, amount: BigNumber): void {
private static _incrementNextBalance(balance: StoredBalance, amount: BigNumber): void {
balance.nextEpochBalance = balance.nextEpochBalance.plus(amount);
}
private static _decrementNextBalance(balance: StakeBalance, amount: BigNumber): void {
private static _decrementNextBalance(balance: StoredBalance, amount: BigNumber): void {
balance.nextEpochBalance = balance.nextEpochBalance.minus(amount);
}
private static _incrementCurrentAndNextBalance(balance: StakeBalance, amount: BigNumber): void {
private static _incrementCurrentAndNextBalance(balance: StoredBalance, amount: BigNumber): void {
balance.currentEpochBalance = balance.currentEpochBalance.plus(amount);
balance.nextEpochBalance = balance.nextEpochBalance.plus(amount);
}
private static _decrementCurrentAndNextBalance(balance: StakeBalance, amount: BigNumber): void {
private static _decrementCurrentAndNextBalance(balance: StoredBalance, amount: BigNumber): void {
balance.currentEpochBalance = balance.currentEpochBalance.minus(amount);
balance.nextEpochBalance = balance.nextEpochBalance.minus(amount);
}
@@ -191,23 +191,38 @@ export class StakerActor extends BaseActor {
...this._poolIds.map(poolId => nextBalances.delegatedStakeByPool[poolId]),
...this._poolIds.map(poolId => nextBalances.totalDelegatedStakeByPool[poolId]),
]) {
balance.currentEpoch = balances.currentEpoch.plus(1);
balance.currentEpochBalance = balance.nextEpochBalance;
}
return nextBalances;
}
private async _getBalancesAsync(): Promise<StakeBalances> {
const balances: StakeBalances = {
currentEpoch: await this._stakingApiWrapper.stakingContract.currentEpoch.callAsync(),
zrxBalance: await this._stakingApiWrapper.zrxTokenContract.balanceOf.callAsync(this._owner),
stakeBalance: await this._stakingApiWrapper.stakingContract.getTotalStake.callAsync(this._owner),
stakeBalanceInVault: await this._stakingApiWrapper.zrxVaultContract.balanceOf.callAsync(this._owner),
activeStakeBalance: await this._stakingApiWrapper.stakingContract.getActiveStake.callAsync(this._owner),
inactiveStakeBalance: await this._stakingApiWrapper.stakingContract.getInactiveStake.callAsync(this._owner),
delegatedStakeBalance: await this._stakingApiWrapper.stakingContract.getStakeDelegatedByOwner.callAsync(
activeStakeBalance: await this._stakingApiWrapper.stakingContract.getOwnerStakeByStatus.callAsync(
this._owner,
StakeStatus.Active,
),
inactiveStakeBalance: await this._stakingApiWrapper.stakingContract.getOwnerStakeByStatus.callAsync(
this._owner,
StakeStatus.Inactive,
),
delegatedStakeBalance: await this._stakingApiWrapper.stakingContract.getOwnerStakeByStatus.callAsync(
this._owner,
StakeStatus.Delegated,
),
globalActiveStakeBalance: await this._stakingApiWrapper.stakingContract.getGlobalStakeByStatus.callAsync(
StakeStatus.Active,
),
globalInactiveStakeBalance: await this._stakingApiWrapper.stakingContract.getGlobalStakeByStatus.callAsync(
StakeStatus.Inactive,
),
globalDelegatedStakeBalance: await this._stakingApiWrapper.stakingContract.getGlobalStakeByStatus.callAsync(
StakeStatus.Delegated,
),
globalActiveStakeBalance: await this._stakingApiWrapper.stakingContract.getGlobalActiveStake.callAsync(),
globalInactiveStakeBalance: await this._stakingApiWrapper.stakingContract.getGlobalInactiveStake.callAsync(),
globalDelegatedStakeBalance: await this._stakingApiWrapper.stakingContract.getGlobalDelegatedStake.callAsync(),
delegatedStakeByPool: {},
totalDelegatedStakeByPool: {},
};

View File

@@ -8,6 +8,7 @@ import { StakingProxyReadOnlyModeSetEventArgs } from '../src';
import { deployAndConfigureContractsAsync, StakingApiWrapper } from './utils/api_wrapper';
import { toBaseUnitAmount } from './utils/number_utils';
import { StakeStatus } from './utils/types';
// tslint:disable:no-unnecessary-type-assertion
blockchainTests.resets('Catastrophe Tests', env => {
@@ -39,7 +40,10 @@ blockchainTests.resets('Catastrophe Tests', env => {
await stakingApiWrapper.stakingContract.stake.awaitTransactionSuccessAsync(amountToStake, {
from: actors[0],
});
const activeStakeBalance = await stakingApiWrapper.stakingContract.getActiveStake.callAsync(actors[0]);
const activeStakeBalance = await stakingApiWrapper.stakingContract.getOwnerStakeByStatus.callAsync(
actors[0],
StakeStatus.Active,
);
expect(activeStakeBalance.currentEpochBalance).to.be.bignumber.equal(amountToStake);
});
it('should not change state when in read-only mode', async () => {
@@ -50,7 +54,10 @@ blockchainTests.resets('Catastrophe Tests', env => {
await stakingApiWrapper.stakingContract.stake.awaitTransactionSuccessAsync(amountToStake, {
from: actors[0],
});
const activeStakeBalance = await stakingApiWrapper.stakingContract.getActiveStake.callAsync(actors[0]);
const activeStakeBalance = await stakingApiWrapper.stakingContract.getOwnerStakeByStatus.callAsync(
actors[0],
StakeStatus.Active,
);
expect(activeStakeBalance.currentEpochBalance).to.be.bignumber.equal(ZERO);
});
it('should read values correctly when in read-only mode', async () => {
@@ -62,8 +69,9 @@ blockchainTests.resets('Catastrophe Tests', env => {
// set to read-only mode
await stakingApiWrapper.stakingProxyContract.setReadOnlyMode.awaitTransactionSuccessAsync(true);
// read stake balance in read-only mode
const activeStakeBalanceReadOnly = await stakingApiWrapper.stakingContract.getActiveStake.callAsync(
const activeStakeBalanceReadOnly = await stakingApiWrapper.stakingContract.getOwnerStakeByStatus.callAsync(
actors[0],
StakeStatus.Active,
);
expect(activeStakeBalanceReadOnly.currentEpochBalance).to.be.bignumber.equal(amountToStake);
});
@@ -76,7 +84,10 @@ blockchainTests.resets('Catastrophe Tests', env => {
await stakingApiWrapper.stakingContract.stake.awaitTransactionSuccessAsync(amountToStake, {
from: actors[0],
});
const activeStakeBalance = await stakingApiWrapper.stakingContract.getActiveStake.callAsync(actors[0]);
const activeStakeBalance = await stakingApiWrapper.stakingContract.getOwnerStakeByStatus.callAsync(
actors[0],
StakeStatus.Active,
);
expect(activeStakeBalance.currentEpochBalance).to.be.bignumber.equal(amountToStake);
});
it('should emit event when setting read-only mode', async () => {

View File

@@ -239,10 +239,8 @@ blockchainTests.resets('Stake Statuses', env => {
// run test, checking balances in epochs [n .. n + 2]
// in epoch `n` - `next` is set
// in epoch `n+1` - `current` is set
// in epoch `n+2` - only withdrawable balance should change.
await staker.moveStakeAsync(from, to, amount.div(2));
await staker.goToNextEpochAsync();
await staker.goToNextEpochAsync();
};
it('active -> active', async () => {
await testMovePartialStake(new StakeInfo(StakeStatus.Active), new StakeInfo(StakeStatus.Active));

View File

@@ -63,13 +63,8 @@ export interface StoredBalance {
nextEpochBalance: BigNumber;
}
export interface StakeBalance {
currentEpochBalance: BigNumber;
nextEpochBalance: BigNumber;
}
export interface StakeBalanceByPool {
[key: string]: StakeBalance;
[key: string]: StoredBalance;
}
export enum StakeStatus {
@@ -89,15 +84,16 @@ export class StakeInfo {
}
export interface StakeBalances {
currentEpoch: BigNumber;
zrxBalance: BigNumber;
stakeBalance: BigNumber;
stakeBalanceInVault: BigNumber;
activeStakeBalance: StakeBalance;
inactiveStakeBalance: StakeBalance;
delegatedStakeBalance: StakeBalance;
globalActiveStakeBalance: StakeBalance;
globalInactiveStakeBalance: StakeBalance;
globalDelegatedStakeBalance: StakeBalance;
activeStakeBalance: StoredBalance;
inactiveStakeBalance: StoredBalance;
delegatedStakeBalance: StoredBalance;
globalActiveStakeBalance: StoredBalance;
globalInactiveStakeBalance: StoredBalance;
globalDelegatedStakeBalance: StoredBalance;
delegatedStakeByPool: StakeBalanceByPool;
totalDelegatedStakeByPool: StakeBalanceByPool;
}