@0x/contracts-exchange: Update signature and rich error decoder tests

to use `hexRandom()` and `blockchainTests()`.
This commit is contained in:
Lawrence Forman
2019-07-30 14:00:36 -04:00
parent fc5963fa3d
commit 7fb87d4039
2 changed files with 25 additions and 64 deletions

View File

@@ -1,37 +1,23 @@
import {
addressUtils,
chaiSetup,
blockchainTests,
expect,
hexRandom,
OrderStatus,
orderUtils,
provider,
txDefaults,
web3Wrapper,
} from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { ExchangeRevertErrors, generatePseudoRandomSalt } from '@0x/order-utils';
import { RevertError } from '@0x/utils';
import * as chai from 'chai';
import * as crypto from 'crypto';
import * as _ from 'lodash';
import { artifacts, TestLibExchangeRichErrorDecoderContract } from '../src';
chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
describe('LibExchangeRichErrorDecoder', () => {
blockchainTests.resets('LibExchangeRichErrorDecoder', ({ provider, txDefaults }) => {
const SIGNATURE_LENGTH = 66;
const ASSET_DATA_LENGTH = 36;
const ERROR_DATA_LENGTH = 100;
let decoder: TestLibExchangeRichErrorDecoderContract;
before(async () => {
await blockchainLifecycle.startAsync();
});
after(async () => {
await blockchainLifecycle.revertAsync();
});
before(async () => {
decoder = await TestLibExchangeRichErrorDecoderContract.deployFrom0xArtifactAsync(
artifacts.TestLibExchangeRichErrorDecoder,
@@ -40,11 +26,6 @@ describe('LibExchangeRichErrorDecoder', () => {
);
});
function generateRandomBytes(length: number): string {
const bytes = crypto.randomBytes(length).toString('hex');
return `0x${bytes}`;
}
function createDecodeTest(revertType: new (...args: any[]) => RevertError, parameters: any[]): void {
const revert = new revertType(...parameters);
const encoded = revert.encode();
@@ -76,8 +57,8 @@ describe('LibExchangeRichErrorDecoder', () => {
const orderHash = orderUtils.generatePseudoRandomOrderHash();
const signer = addressUtils.generatePseudoRandomAddress();
const validator = addressUtils.generatePseudoRandomAddress();
const signature = generateRandomBytes(SIGNATURE_LENGTH);
const errorData = generateRandomBytes(ERROR_DATA_LENGTH);
const signature = hexRandom(SIGNATURE_LENGTH);
const errorData = hexRandom(ERROR_DATA_LENGTH);
createDecodeTest(ExchangeRevertErrors.SignatureError, [errorCode, orderHash, signer, signature]);
createDecodeTest(ExchangeRevertErrors.SignatureValidatorNotApprovedError, [signer, validator]);
createDecodeTest(ExchangeRevertErrors.SignatureValidatorError, [
@@ -125,14 +106,14 @@ describe('LibExchangeRichErrorDecoder', () => {
(() => {
const errorCode = ExchangeRevertErrors.AssetProxyDispatchErrorCode.UnknownAssetProxy;
const orderHash = orderUtils.generatePseudoRandomOrderHash();
const assetData = generateRandomBytes(ASSET_DATA_LENGTH);
const assetData = hexRandom(ASSET_DATA_LENGTH);
createDecodeTest(ExchangeRevertErrors.AssetProxyDispatchError, [errorCode, orderHash, assetData]);
})();
(() => {
const orderHash = orderUtils.generatePseudoRandomOrderHash();
const assetData = generateRandomBytes(ASSET_DATA_LENGTH);
const errorData = generateRandomBytes(ERROR_DATA_LENGTH);
const assetData = hexRandom(ASSET_DATA_LENGTH);
const errorData = hexRandom(ERROR_DATA_LENGTH);
createDecodeTest(ExchangeRevertErrors.AssetProxyTransferError, [orderHash, assetData, errorData]);
})();
@@ -151,13 +132,13 @@ describe('LibExchangeRichErrorDecoder', () => {
(() => {
const transactionHash = orderUtils.generatePseudoRandomOrderHash();
const signer = addressUtils.generatePseudoRandomAddress();
const signature = generateRandomBytes(SIGNATURE_LENGTH);
const signature = hexRandom(SIGNATURE_LENGTH);
createDecodeTest(ExchangeRevertErrors.TransactionSignatureError, [transactionHash, signer, signature]);
})();
(() => {
const transactionHash = orderUtils.generatePseudoRandomOrderHash();
const errorData = generateRandomBytes(ERROR_DATA_LENGTH);
const errorData = hexRandom(ERROR_DATA_LENGTH);
createDecodeTest(ExchangeRevertErrors.TransactionExecutionError, [transactionHash, errorData]);
})();

View File

@@ -1,15 +1,14 @@
import {
addressUtils,
chaiSetup,
blockchainTests,
constants,
expect,
hexConcat,
hexRandom,
LogDecoder,
OrderFactory,
orderUtils,
provider,
TransactionFactory,
txDefaults,
web3Wrapper,
} from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import {
@@ -20,9 +19,7 @@ import {
transactionHashUtils,
} from '@0x/order-utils';
import { SignatureType, SignedOrder, SignedZeroExTransaction } from '@0x/types';
import { BigNumber, providerUtils, StringRevertError } from '@0x/utils';
import * as chai from 'chai';
import * as crypto from 'crypto';
import { BigNumber, StringRevertError } from '@0x/utils';
import { LogWithDecodedArgs } from 'ethereum-types';
import ethUtil = require('ethereumjs-util');
@@ -35,11 +32,8 @@ import {
import { ValidatorWalletAction, ValidatorWalletDataType } from './utils';
chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
// tslint:disable:no-unnecessary-type-assertion
describe('MixinSignatureValidator', () => {
blockchainTests.resets('MixinSignatureValidator', env => {
let chainId: number;
let signatureValidator: TestSignatureValidatorContract;
let validatorWallet: TestValidatorWalletContract;
@@ -49,26 +43,20 @@ describe('MixinSignatureValidator', () => {
let notSignerAddress: string;
before(async () => {
await blockchainLifecycle.startAsync();
});
after(async () => {
await blockchainLifecycle.revertAsync();
});
before(async () => {
chainId = await providerUtils.getChainIdAsync(provider);
const accounts = await web3Wrapper.getAvailableAddressesAsync();
chainId = await env.getChainIdAsync();
const accounts = await env.getAccountAddressesAsync();
signerAddress = accounts[0];
notSignerAddress = accounts[1];
signatureValidator = await TestSignatureValidatorContract.deployFrom0xArtifactAsync(
artifacts.TestSignatureValidator,
provider,
txDefaults,
env.provider,
env.txDefaults,
new BigNumber(chainId),
);
validatorWallet = await TestValidatorWalletContract.deployFrom0xArtifactAsync(
artifacts.TestValidatorWallet,
provider,
txDefaults,
env.provider,
env.txDefaults,
signatureValidator.address,
);
validatorWalletRevertReason = await validatorWallet.REVERT_REASON.callAsync();
@@ -87,16 +75,8 @@ describe('MixinSignatureValidator', () => {
signerPrivateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(signerAddress)];
});
beforeEach(async () => {
await blockchainLifecycle.startAsync();
});
afterEach(async () => {
await blockchainLifecycle.revertAsync();
});
const SIGNATURE_LENGTH = 65;
const generateRandomBytes = (count: number): string => ethUtil.bufferToHex(crypto.randomBytes(count));
const generateRandomSignature = (): string => generateRandomBytes(SIGNATURE_LENGTH);
const generateRandomSignature = (): string => hexRandom(SIGNATURE_LENGTH);
const hashBytes = (bytesHex: string): string => ethUtil.bufferToHex(ethUtil.sha3(ethUtil.toBuffer(bytesHex)));
const signDataHex = (dataHex: string, privateKey: Buffer): string => {
const ecSignature = ethUtil.ecsign(ethUtil.toBuffer(dataHex), signerPrivateKey);
@@ -609,7 +589,7 @@ describe('MixinSignatureValidator', () => {
// We don't actually do anything with the transaction so we can just
// fill it with random data.
signedTransaction = await transactionFactory.newSignedTransactionAsync({
data: generateRandomBytes(TRANSACTION_DATA_LENGTH),
data: hexRandom(TRANSACTION_DATA_LENGTH),
});
});
@@ -807,7 +787,7 @@ describe('MixinSignatureValidator', () => {
let signatureValidatorLogDecoder: LogDecoder;
before(async () => {
signatureValidatorLogDecoder = new LogDecoder(web3Wrapper, artifacts);
signatureValidatorLogDecoder = new LogDecoder(env.web3Wrapper, artifacts);
});
it('should emit a SignatureValidatorApprovalSet with correct args when a validator is approved', async () => {