invalidDecreaseStakingPoolOperatorShareAssertion

This commit is contained in:
Michael Zhu
2019-12-17 13:38:16 -08:00
parent b80ae5796b
commit 3bf37d6afd
3 changed files with 62 additions and 4 deletions

View File

@@ -5,7 +5,10 @@ import { TransactionReceiptWithDecodedLogs } from 'ethereum-types';
import * as _ from 'lodash';
import { invalidCreateStakingPoolAssertion, validCreateStakingPoolAssertion } from '../assertions/createStakingPool';
import { validDecreaseStakingPoolOperatorShareAssertion } from '../assertions/decreaseStakingPoolOperatorShare';
import {
invalidDecreaseStakingPoolOperatorShareAssertion,
validDecreaseStakingPoolOperatorShareAssertion,
} from '../assertions/decreaseStakingPoolOperatorShare';
import { AssertionResult } from '../assertions/function_assertion';
import { Distributions, Pseudorandom } from '../utils/pseudorandom';
@@ -44,6 +47,7 @@ export function PoolOperatorMixin<TBase extends Constructor>(Base: TBase): TBase
validCreateStakingPool: this._validCreateStakingPool(),
invalidCreateStakingPool: this._invalidCreateStakingPool(),
validDecreaseStakingPoolOperatorShare: this._validDecreaseStakingPoolOperatorShare(),
invalidDecreaseStakingPoolOperatorShare: this._invalidDecreaseStakingPoolOperatorShare(),
};
}
@@ -123,6 +127,24 @@ export function PoolOperatorMixin<TBase extends Constructor>(Base: TBase): TBase
}
}
}
private async *_invalidDecreaseStakingPoolOperatorShare(): AsyncIterableIterator<AssertionResult | void> {
const { stakingPools } = this.actor.simulationEnvironment!;
const assertion = invalidDecreaseStakingPoolOperatorShareAssertion(this.actor.deployment);
while (true) {
const poolId = Pseudorandom.sample(this._getOperatorPoolIds(stakingPools));
if (poolId === undefined) {
yield undefined;
} else {
const operatorShare = Pseudorandom.integer(
stakingPools[poolId].operatorShare + 1,
constants.MAX_UINT32,
Distributions.Kumaraswamy(0.2, 0.2),
).toNumber();
yield assertion.executeAsync([poolId, operatorShare], { from: this.actor.address });
}
}
}
};
}

View File

@@ -1,4 +1,4 @@
import { StakingPoolById } from '@0x/contracts-staking';
import { constants, StakingPoolById, StakingRevertErrors } from '@0x/contracts-staking';
import { expect } from '@0x/contracts-test-utils';
import { TxData } from 'ethereum-types';
@@ -32,3 +32,37 @@ export function validDecreaseStakingPoolOperatorShareAssertion(
},
});
}
/**
* Returns a FunctionAssertion for `decreaseStakingPoolOperatorShare` which assumes the given
* operator share is larger than the current operator share for the pool. The FunctionAssertion
* checks that the transaction reverts with the correct error in this scenario.
*/
export function invalidDecreaseStakingPoolOperatorShareAssertion(
deployment: DeploymentManager,
): FunctionAssertion<[string, number], void, void> {
const { stakingWrapper } = deployment.staking;
return new FunctionAssertion<[string, number], void, void>(stakingWrapper, 'decreaseStakingPoolOperatorShare', {
after: async (_beforeInfo: void, result: FunctionResult, args: [string, number], _txData: Partial<TxData>) => {
// Ensure that the tx reverted.
expect(result.success).to.be.false();
// Check revert error
const [poolId, operatorShare] = args;
const expectedError =
operatorShare > constants.PPM
? new StakingRevertErrors.OperatorShareError(
StakingRevertErrors.OperatorShareErrorCodes.OperatorShareTooLarge,
poolId,
operatorShare,
)
: new StakingRevertErrors.OperatorShareError(
StakingRevertErrors.OperatorShareErrorCodes.CanOnlyDecreaseOperatorShare,
poolId,
operatorShare,
);
expect(result.data).to.equal(expectedError);
},
});
}

View File

@@ -20,8 +20,10 @@ export class PoolManagementSimulation extends Simulation {
...operators.map(operator => [operator.simulationActions.validCreateStakingPool, 0.38]),
// 2% chance of executing invalidCreateStakingPool assertion for a random operator
...operators.map(operator => [operator.simulationActions.invalidCreateStakingPool, 0.02]),
// 60% chance of executing validDecreaseStakingPoolOperatorShare for a random operator
...operators.map(operator => [operator.simulationActions.validDecreaseStakingPoolOperatorShare, 0.6]),
// 58% chance of executing validDecreaseStakingPoolOperatorShare for a random operator
...operators.map(operator => [operator.simulationActions.validDecreaseStakingPoolOperatorShare, 0.58]),
// 2% chance of executing invalidDecreaseStakingPoolOperatorShare for a random operator
...operators.map(operator => [operator.simulationActions.invalidDecreaseStakingPoolOperatorShare, 0.02]),
]) as [Array<AsyncIterableIterator<AssertionResult | void>>, number[]];
while (true) {
const action = Pseudorandom.sample(actions, weights);