split up pool/stake management simulations, change some types

This commit is contained in:
Michael Zhu
2019-11-05 17:08:36 -08:00
parent 44f268a7ee
commit 120d554a6b
7 changed files with 66 additions and 29 deletions

View File

@@ -44,8 +44,8 @@ export interface Assertion {
executeAsync: (...args: any[]) => Promise<any>; executeAsync: (...args: any[]) => Promise<any>;
} }
export interface AssertionResult { export interface AssertionResult<TBefore = unknown> {
beforeInfo: any; beforeInfo: TBefore;
afterInfo: any; afterInfo: any;
} }
@@ -73,7 +73,7 @@ export class FunctionAssertion<TBefore> implements Assertion {
* Runs the wrapped function and fails if the before or after assertions fail. * Runs the wrapped function and fails if the before or after assertions fail.
* @param ...args The args to the contract wrapper function. * @param ...args The args to the contract wrapper function.
*/ */
public async executeAsync(...args: any[]): Promise<AssertionResult> { public async executeAsync(...args: any[]): Promise<AssertionResult<TBefore>> {
// Call the before condition. // Call the before condition.
const beforeInfo = await this.condition.before(...args); const beforeInfo = await this.condition.before(...args);

View File

@@ -26,7 +26,7 @@ export class Actor {
public readonly deployment: DeploymentManager; public readonly deployment: DeploymentManager;
public readonly simulationEnvironment?: SimulationEnvironment; public readonly simulationEnvironment?: SimulationEnvironment;
public simulationActions: { public simulationActions: {
[action: string]: (...args: any[]) => Promise<IteratorResult<AssertionResult | void>>; [action: string]: AsyncIterableIterator<AssertionResult | void>;
} = {}; } = {};
protected readonly _transactionFactory: TransactionFactory; protected readonly _transactionFactory: TransactionFactory;

View File

@@ -47,8 +47,8 @@ export function PoolOperatorMixin<TBase extends Constructor>(Base: TBase): TBase
if (this.actor.simulationEnvironment !== undefined) { if (this.actor.simulationEnvironment !== undefined) {
this.actor.simulationActions = { this.actor.simulationActions = {
...this.actor.simulationActions, ...this.actor.simulationActions,
validCreateStakingPool: this._validCreateStakingPool().next, validCreateStakingPool: this._validCreateStakingPool(),
validDecreaseStakingPoolOperatorShare: this._validDecreaseStakingPoolOperatorShare().next, validDecreaseStakingPoolOperatorShare: this._validDecreaseStakingPoolOperatorShare(),
}; };
} }
} }

View File

@@ -35,8 +35,8 @@ export function StakerMixin<TBase extends Constructor>(Base: TBase): TBase & Con
const { balanceStore } = this.actor.simulationEnvironment; const { balanceStore } = this.actor.simulationEnvironment;
this.actor.simulationActions = { this.actor.simulationActions = {
...this.actor.simulationActions, ...this.actor.simulationActions,
validStake: this._validStake(balanceStore).next, validStake: this._validStake(balanceStore),
validUnstake: this._validUnstake(balanceStore).next, validUnstake: this._validUnstake(balanceStore),
}; };
} }
} }

View File

@@ -0,0 +1,47 @@
import { BlockchainBalanceStore } from '@0x/contracts-exchange';
import { blockchainTests } from '@0x/contracts-test-utils';
import * as _ from 'lodash';
import { PoolOperator } from '../actors';
import { DeploymentManager } from '../utils/deployment_manager';
import { AssertionResult } from '../utils/function_assertions';
import { Simulation, SimulationEnvironment } from './simulation';
export class PoolManagementSimulation extends Simulation {
constructor(environment: SimulationEnvironment) {
super(environment);
}
protected async *_assertionGenerator(): AsyncIterableIterator<AssertionResult | void> {
const { deployment } = this.environment;
const operator = new PoolOperator({
name: 'Operator',
deployment,
simulationEnvironment: this.environment,
});
const actions = [
operator.simulationActions.validCreateStakingPool,
operator.simulationActions.validDecreaseStakingPoolOperatorShare,
];
while (true) {
const action = _.sample(actions);
yield (await action!.next()).value;
}
}
}
blockchainTests.skip('Pool management fuzz test', env => {
it('fuzz', async () => {
const deployment = await DeploymentManager.deployAsync(env, {
numErc20TokensToDeploy: 0,
numErc721TokensToDeploy: 0,
numErc1155TokensToDeploy: 0,
});
const balanceStore = new BlockchainBalanceStore({}, {});
const sim = new PoolManagementSimulation({ balanceStore, deployment });
return sim.fuzzAsync();
});
});

View File

@@ -20,7 +20,9 @@ export abstract class Simulation {
public async fuzzAsync(steps?: number): Promise<void> { public async fuzzAsync(steps?: number): Promise<void> {
if (steps !== undefined) { if (steps !== undefined) {
_.times(steps, async () => await this.stepAsync()); for (let i = 0; i < steps; i++) {
await this.stepAsync();
}
} else { } else {
while (true) { while (true) {
await this.stepAsync(); await this.stepAsync();
@@ -28,5 +30,5 @@ export abstract class Simulation {
} }
} }
protected abstract _assertionGenerator(): AsyncIterableIterator<AssertionResult>; protected abstract _assertionGenerator(): AsyncIterableIterator<AssertionResult | void>;
} }

View File

@@ -2,50 +2,38 @@ import { BlockchainBalanceStore } from '@0x/contracts-exchange';
import { blockchainTests } from '@0x/contracts-test-utils'; import { blockchainTests } from '@0x/contracts-test-utils';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { PoolOperator, Staker } from '../actors'; import { Staker } from '../actors';
import { DeploymentManager } from '../utils/deployment_manager'; import { DeploymentManager } from '../utils/deployment_manager';
import { AssertionResult } from '../utils/function_assertions'; import { AssertionResult } from '../utils/function_assertions';
import { Simulation, SimulationEnvironment } from './simulation'; import { Simulation, SimulationEnvironment } from './simulation';
class PoolManagementSimulation extends Simulation { export class StakeManagementSimulation extends Simulation {
constructor(environment: SimulationEnvironment) { constructor(environment: SimulationEnvironment) {
super(environment); super(environment);
} }
protected async *_assertionGenerator(): AsyncIterableIterator<AssertionResult> { protected async *_assertionGenerator(): AsyncIterableIterator<AssertionResult | void> {
const { deployment, balanceStore } = this.environment; const { deployment, balanceStore } = this.environment;
const staker = new Staker({ name: 'Staker', deployment, simulationEnvironment: this.environment }); const staker = new Staker({ name: 'Staker', deployment, simulationEnvironment: this.environment });
await staker.configureERC20TokenAsync(deployment.tokens.zrx); await staker.configureERC20TokenAsync(deployment.tokens.zrx);
balanceStore.registerTokenOwner(staker.address, staker.name); balanceStore.registerTokenOwner(staker.address, staker.name);
const operator = new PoolOperator({ const actions = [staker.simulationActions.validStake, staker.simulationActions.validUnstake];
name: 'Operator',
deployment,
simulationEnvironment: this.environment,
});
const actions = [
staker.simulationActions.validStake,
staker.simulationActions.validUnstake,
operator.simulationActions.validCreateStakingPool,
operator.simulationActions.validDecreaseStakingPoolOperatorShare,
];
while (true) { while (true) {
const action = _.sample(actions); const action = _.sample(actions);
await action!(); yield (await action!.next()).value;
} }
} }
} }
blockchainTests.only('Pool management fuzz test', env => { blockchainTests.skip('Stake management fuzz test', env => {
it('fuzz', async () => { it('fuzz', async () => {
const deployment = await DeploymentManager.deployAsync(env, { const deployment = await DeploymentManager.deployAsync(env, {
numErc20TokensToDeploy: 0, numErc20TokensToDeploy: 0,
numErc721TokensToDeploy: 0, numErc721TokensToDeploy: 0,
numErc1155TokensToDeploy: 0, numErc1155TokensToDeploy: 0,
}); });
const balanceStore = new BlockchainBalanceStore( const balanceStore = new BlockchainBalanceStore(
{ {
StakingProxy: deployment.staking.stakingProxy.address, StakingProxy: deployment.staking.stakingProxy.address,
@@ -54,7 +42,7 @@ blockchainTests.only('Pool management fuzz test', env => {
{ erc20: { ZRX: deployment.tokens.zrx } }, { erc20: { ZRX: deployment.tokens.zrx } },
); );
const sim = new PoolManagementSimulation({ balanceStore, deployment }); const sim = new StakeManagementSimulation({ balanceStore, deployment });
return sim.fuzzAsync(); return sim.fuzzAsync();
}); });
}); });