Add backstop tests

This commit is contained in:
Amir Bandeali
2019-10-06 13:17:02 -07:00
parent a7ef54dbff
commit 9e41c648dc
10 changed files with 93 additions and 6 deletions

View File

@@ -2,7 +2,7 @@
"artifactsDir": "./generated-artifacts",
"contractsDir": "./contracts",
"useDockerisedSolc": false,
"isOfflineMode": false,
"isOfflineMode": true,
"compilerSettings": {
"evmVersion": "constantinople",
"optimizer": {

View File

@@ -61,7 +61,7 @@ contract ZrxVaultBackstop {
// Ensure that the stakingProxy has been in read-only mode for a long enough time
require(
block.timestamp.safeSub(readOnlyState.lastSetTimestamp) >= 40 days,
"READ_ONLY_MODE_LENGTH_TOO_SHORT"
"READ_ONLY_MODE_DURATION_TOO_SHORT"
);
zrxVault.enterCatastrophicFailure();

View File

@@ -37,7 +37,7 @@
},
"config": {
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
"abis": "./generated-artifacts/@(IStaking|IStakingEvents|IStakingProxy|IStorage|IStorageInit|IStructs|IZrxVault|LibCobbDouglas|LibFixedMath|LibFixedMathRichErrors|LibProxy|LibSafeDowncast|LibStakingRichErrors|MixinAbstract|MixinConstants|MixinCumulativeRewards|MixinDeploymentConstants|MixinExchangeFees|MixinExchangeManager|MixinFinalizer|MixinParams|MixinScheduler|MixinStake|MixinStakeBalances|MixinStakeStorage|MixinStakingPool|MixinStakingPoolRewards|MixinStorage|ReadOnlyProxy|Staking|StakingProxy|TestAssertStorageParams|TestCobbDouglas|TestCumulativeRewardTracking|TestDelegatorRewards|TestExchangeManager|TestFinalizer|TestInitTarget|TestLibFixedMath|TestLibProxy|TestLibProxyReceiver|TestLibSafeDowncast|TestMixinStakeStorage|TestProtocolFees|TestStaking|TestStakingNoWETH|TestStakingProxy|TestStorageLayoutAndConstants|ZrxVault).json"
"abis": "./generated-artifacts/@(IStaking|IStakingEvents|IStakingProxy|IStorage|IStorageInit|IStructs|IZrxVault|LibCobbDouglas|LibFixedMath|LibFixedMathRichErrors|LibProxy|LibSafeDowncast|LibStakingRichErrors|MixinAbstract|MixinConstants|MixinCumulativeRewards|MixinDeploymentConstants|MixinExchangeFees|MixinExchangeManager|MixinFinalizer|MixinParams|MixinScheduler|MixinStake|MixinStakeBalances|MixinStakeStorage|MixinStakingPool|MixinStakingPoolRewards|MixinStorage|ReadOnlyProxy|Staking|StakingProxy|TestAssertStorageParams|TestCobbDouglas|TestCumulativeRewardTracking|TestDelegatorRewards|TestExchangeManager|TestFinalizer|TestInitTarget|TestLibFixedMath|TestLibProxy|TestLibProxyReceiver|TestLibSafeDowncast|TestMixinStakeStorage|TestProtocolFees|TestStaking|TestStakingNoWETH|TestStakingProxy|TestStorageLayoutAndConstants|ZrxVault|ZrxVaultBackstop).json"
},
"repository": {
"type": "git",

View File

@@ -54,11 +54,13 @@ import * as TestStakingNoWETH from '../generated-artifacts/TestStakingNoWETH.jso
import * as TestStakingProxy from '../generated-artifacts/TestStakingProxy.json';
import * as TestStorageLayoutAndConstants from '../generated-artifacts/TestStorageLayoutAndConstants.json';
import * as ZrxVault from '../generated-artifacts/ZrxVault.json';
import * as ZrxVaultBackstop from '../generated-artifacts/ZrxVaultBackstop.json';
export const artifacts = {
ReadOnlyProxy: ReadOnlyProxy as ContractArtifact,
Staking: Staking as ContractArtifact,
StakingProxy: StakingProxy as ContractArtifact,
ZrxVault: ZrxVault as ContractArtifact,
ZrxVaultBackstop: ZrxVaultBackstop as ContractArtifact,
MixinExchangeFees: MixinExchangeFees as ContractArtifact,
MixinExchangeManager: MixinExchangeManager as ContractArtifact,
MixinConstants: MixinConstants as ContractArtifact,

View File

@@ -52,3 +52,4 @@ export * from '../generated-wrappers/test_staking_no_w_e_t_h';
export * from '../generated-wrappers/test_staking_proxy';
export * from '../generated-wrappers/test_storage_layout_and_constants';
export * from '../generated-wrappers/zrx_vault';
export * from '../generated-wrappers/zrx_vault_backstop';

View File

@@ -90,7 +90,7 @@ blockchainTests.resets('Catastrophe Tests', env => {
);
expect(undelegatedStakeBalance.currentEpochBalance).to.be.bignumber.equal(amountToStake);
});
it('should emit event when setting read-only mode', async () => {
it('should emit event and store correct configuration when setting read-only mode', async () => {
// set to read-only mode
const txReceipt = await stakingApiWrapper.stakingProxyContract.setReadOnlyMode.awaitTransactionSuccessAsync(
true,
@@ -98,6 +98,11 @@ blockchainTests.resets('Catastrophe Tests', env => {
expect(txReceipt.logs.length).to.be.equal(1);
const trueLog = txReceipt.logs[0] as LogWithDecodedArgs<StakingProxyReadOnlyModeSetEventArgs>;
expect(trueLog.args.readOnlyMode).to.be.true();
const timestamp = await env.web3Wrapper.getBlockTimestampAsync(txReceipt.blockNumber);
expect(trueLog.args.timestamp).to.bignumber.equal(timestamp);
const readOnlyState = await stakingApiWrapper.stakingProxyContract.readOnlyState.callAsync();
expect(readOnlyState[0]).to.be.true();
expect(readOnlyState[1]).to.bignumber.equal(timestamp);
});
});
});

View File

@@ -336,6 +336,17 @@ blockchainTests.resets('ZrxVault unit tests', env => {
expect(tx).to.revertWith(expectedError);
expect(await zrxVault.isInCatastrophicFailure.callAsync()).to.be.false();
});
it('Catastrophic Failure Mode can only be turned on once', async () => {
const authorized = nonOwnerAddresses[0];
await zrxVault.addAuthorizedAddress.awaitTransactionSuccessAsync(authorized, { from: owner });
await zrxVault.enterCatastrophicFailure.awaitTransactionSuccessAsync({
from: authorized,
});
const expectedError = new StakingRevertErrors.OnlyCallableIfNotInCatastrophicFailureError();
return expect(zrxVault.enterCatastrophicFailure.awaitTransactionSuccessAsync()).to.revertWith(
expectedError,
);
});
});
describe('Affected functionality', () => {

View File

@@ -15,6 +15,7 @@ import {
TestCobbDouglasContract,
TestStakingContract,
TestStakingEvents,
ZrxVaultBackstopContract,
ZrxVaultContract,
} from '../../src';
@@ -32,6 +33,7 @@ export class StakingApiWrapper {
public zrxTokenContract: DummyERC20TokenContract;
public wethContract: WETH9Contract;
public cobbDouglasContract: TestCobbDouglasContract;
public zrxVaultBackstopContract: ZrxVaultBackstopContract;
public utils = {
// Epoch Utils
fastForwardToNextEpochAsync: async (): Promise<void> => {
@@ -174,13 +176,14 @@ export class StakingApiWrapper {
zrxTokenContract: DummyERC20TokenContract,
wethContract: WETH9Contract,
cobbDouglasContract: TestCobbDouglasContract,
zrxVaultBackstopContract: ZrxVaultBackstopContract,
) {
this._web3Wrapper = env.web3Wrapper;
this.zrxVaultContract = zrxVaultContract;
this.zrxTokenContract = zrxTokenContract;
this.wethContract = wethContract;
this.cobbDouglasContract = cobbDouglasContract;
this.zrxVaultBackstopContract = zrxVaultBackstopContract;
this.stakingContractAddress = stakingContract.address;
this.stakingProxyContract = stakingProxyContract;
// disguise the staking proxy as a StakingContract
@@ -274,10 +277,23 @@ export async function deployAndConfigureContractsAsync(
artifacts,
);
const zrxVaultBackstopContract = await ZrxVaultBackstopContract.deployFrom0xArtifactAsync(
artifacts.ZrxVaultBackstop,
env.provider,
env.txDefaults,
artifacts,
stakingProxyContract.address,
zrxVaultContract.address,
);
// configure erc20 proxy to accept calls from zrx vault
await erc20ProxyContract.addAuthorizedAddress.awaitTransactionSuccessAsync(zrxVaultContract.address);
// set staking proxy contract in zrx vault
await zrxVaultContract.setStakingProxy.awaitTransactionSuccessAsync(stakingProxyContract.address);
// add zrxVaultBackstop as an authorized address
await zrxVaultContract.addAuthorizedAddress.awaitTransactionSuccessAsync(zrxVaultBackstopContract.address, {
from: ownerAddress,
});
return new StakingApiWrapper(
env,
ownerAddress,
@@ -287,5 +303,6 @@ export async function deployAndConfigureContractsAsync(
zrxTokenContract,
wethContract,
cobbDouglasContract,
zrxVaultBackstopContract,
);
}

View File

@@ -0,0 +1,50 @@
import { ERC20Wrapper } from '@0x/contracts-asset-proxy';
import { blockchainTests, describe, expect, increaseTimeAndMineBlockAsync } from '@0x/contracts-test-utils';
import { StringRevertError } from '@0x/utils';
import { LogWithDecodedArgs } from 'ethereum-types';
import { ZrxVaultInCatastrophicFailureModeEventArgs } from '../src';
import { deployAndConfigureContractsAsync, StakingApiWrapper } from './utils/api_wrapper';
blockchainTests.resets('ZrxVaultBackstop', env => {
let stakingApiWrapper: StakingApiWrapper;
let authorizedAddress: string;
let notAuthorizedAddress: string;
before(async () => {
const accounts = await env.web3Wrapper.getAvailableAddressesAsync();
[authorizedAddress, notAuthorizedAddress] = accounts;
const erc20Wrapper = new ERC20Wrapper(env.provider, [], authorizedAddress);
stakingApiWrapper = await deployAndConfigureContractsAsync(env, authorizedAddress, erc20Wrapper);
});
describe('enterCatastrophicFailureIfProlongedReadOnlyMode', () => {
it('should revert if read-only mode is not set', async () => {
const expectedError = new StringRevertError('READ_ONLY_MODE_NOT_SET');
expect(
stakingApiWrapper.zrxVaultBackstopContract.enterCatastrophicFailureIfProlongedReadOnlyMode.awaitTransactionSuccessAsync(),
).to.revertWith(expectedError);
});
it('should revert if read-only mode has been set for less than 40 days', async () => {
await stakingApiWrapper.stakingProxyContract.setReadOnlyMode.awaitTransactionSuccessAsync(true, {
from: authorizedAddress,
});
const expectedError = new StringRevertError('READ_ONLY_MODE_DURATION_TOO_SHORT');
expect(
stakingApiWrapper.zrxVaultBackstopContract.enterCatastrophicFailureIfProlongedReadOnlyMode.awaitTransactionSuccessAsync(),
).to.revertWith(expectedError);
});
it('should enter catastophic failure mode if read-only mode has been set for 40 days', async () => {
await stakingApiWrapper.stakingProxyContract.setReadOnlyMode.awaitTransactionSuccessAsync(true, {
from: authorizedAddress,
});
const fourtyDaysInSec = 40 * 24 * 60 * 60;
await increaseTimeAndMineBlockAsync(fourtyDaysInSec);
const txReceipt = await stakingApiWrapper.zrxVaultBackstopContract.enterCatastrophicFailureIfProlongedReadOnlyMode.awaitTransactionSuccessAsync(
{ from: notAuthorizedAddress },
);
expect(txReceipt.logs.length).to.equal(1);
const logArgs = (txReceipt.logs[0] as LogWithDecodedArgs<ZrxVaultInCatastrophicFailureModeEventArgs>).args;
expect(logArgs.sender).to.equal(stakingApiWrapper.zrxVaultBackstopContract.address);
});
});
});

View File

@@ -51,7 +51,8 @@
"generated-artifacts/TestStakingNoWETH.json",
"generated-artifacts/TestStakingProxy.json",
"generated-artifacts/TestStorageLayoutAndConstants.json",
"generated-artifacts/ZrxVault.json"
"generated-artifacts/ZrxVault.json",
"generated-artifacts/ZrxVaultBackstop.json"
],
"exclude": ["./deploy/solc/solc_bin"]
}