Exchange signature validation fuzz tests (#2425)

* `@0x/contracts-integrations`: Add Exchange signature validation fuzz tests.

* `@0x/contracts-integrations`: Switch from actor pattern to just pure function generators.

Co-authored-by: Lawrence Forman <me@merklejerk.com>
This commit is contained in:
Lawrence Forman
2020-01-07 17:35:25 -05:00
committed by GitHub
parent 8d10736934
commit de12da18da
10 changed files with 806 additions and 1 deletions

View File

@@ -22,6 +22,7 @@ import {
} from '@0x/contracts-staking';
import { BlockchainTestsEnvironment, constants } from '@0x/contracts-test-utils';
import { BigNumber } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import { TxData } from 'ethereum-types';
import * as _ from 'lodash';
@@ -207,6 +208,7 @@ export class DeploymentManager {
// Construct the new instance and return it.
return new DeploymentManager(
environment.web3Wrapper,
assetProxies,
governor,
exchange,
@@ -522,6 +524,7 @@ export class DeploymentManager {
}
protected constructor(
public web3Wrapper: Web3Wrapper,
public assetProxies: AssetProxyContracts,
public governor: ZeroExGovernorContract,
public exchange: ExchangeContract,

View File

@@ -48,6 +48,7 @@ export class SimulationEnvironment {
export abstract class Simulation {
public readonly generator = this._assertionGenerator();
public resets = false;
constructor(public environment: SimulationEnvironment) {}
@@ -66,11 +67,15 @@ export abstract class Simulation {
protected abstract _assertionGenerator(): AsyncIterableIterator<AssertionResult | void>;
private async _stepAsync(): Promise<void> {
const snapshotId = this.resets ? await this.environment.deployment.web3Wrapper.takeSnapshotAsync() : undefined;
try {
await this.generator.next();
} catch (error) {
logger.logFailure(error, this.environment.state());
throw error;
}
if (snapshotId !== undefined) {
await this.environment.deployment.web3Wrapper.revertSnapshotAsync(snapshotId);
}
}
}

View File

@@ -78,6 +78,17 @@ class PRNGWrapper {
return ONE.minus(ONE.minus(u).exponentiatedBy(ONE.dividedBy(beta))).exponentiatedBy(ONE.dividedBy(alpha));
};
}
/*
* Pseudorandom version of `hexRandom()`. If no distribution is provided,
* samples all byte values uniformly.
*/
public hex(bytesLength: number = 32, distribution: () => Numberish = this.rng): string {
const buf = Buffer.from(_.times(bytesLength, () => this.integer(0, 255, distribution).toNumber())).toString(
'hex',
);
return `0x${buf}`;
}
}
export const Pseudorandom = new PRNGWrapper();