@0x/contracts-exchange: Change how order hashes and signatures are computed for wrapper_unit_tests.

This commit is contained in:
Lawrence Forman
2019-08-06 17:19:06 -04:00
parent ca28b8f93e
commit ea1501abd1
2 changed files with 149 additions and 45 deletions

View File

@@ -31,6 +31,7 @@ contract TestWrapperFunctions is
uint8 internal constant MAX_ORDER_STATUS = uint8(OrderStatus.CANCELLED);
uint256 internal constant ALWAYS_FAILING_SALT = uint256(-1);
string internal constant ALWAYS_FAILING_SALT_REVERT_REASON = "ALWAYS_FAILING_SALT";
string internal constant BAD_SIGNATURE_REVERT_REASON = "BAD_SIGNATURE";
// solhint-disable no-unused-vars
event FillOrderCalled(
@@ -68,6 +69,10 @@ contract TestWrapperFunctions is
if (order.salt == ALWAYS_FAILING_SALT) {
revert(ALWAYS_FAILING_SALT_REVERT_REASON);
}
// Fail if the signature is invalid.
if (keccak256(signature) != keccak256(_getValidOrderSignature(order))) {
revert(BAD_SIGNATURE_REVERT_REASON);
}
// We aren't interested in correctness here because we are testing the
// behavior of the caller, not `_fillOrder()` itself. We just need some
@@ -104,7 +109,23 @@ contract TestWrapperFunctions is
orderInfo.orderTakerAssetFilledAmount = uint128(order.salt);
// High byte of `order.salt` is the `orderStatus`.
orderInfo.orderStatus = uint8(order.salt >> 248) % (MAX_ORDER_STATUS + 1);
// `orderHash` is just `keccak256(order.salt)`.
orderInfo.orderHash = keccak256(abi.encode(order.salt));
orderInfo.orderHash = _getOrderHash(order);
}
function _getOrderHash(Order memory order)
internal
pure
returns (bytes32 hash)
{
// `orderHash` is just `keccak256(order.makerFeeAssetData, order.takerAssetData, order.salt)`.
hash = keccak256(abi.encodePacked(order.makerAssetData, order.takerAssetData, order.salt));
}
function _getValidOrderSignature(Order memory order)
internal
pure
returns (bytes memory signature)
{
return abi.encodePacked(keccak256(abi.encodePacked(_getOrderHash(order))));
}
}

View File

@@ -1,3 +1,4 @@
import { ReferenceFunctions as LibReferenceFunctions } from '@0x/contracts-exchange-libs';
import {
blockchainTests,
constants,
@@ -6,9 +7,14 @@ import {
hexRandom,
TransactionHelper,
} from '@0x/contracts-test-utils';
import { ReferenceFunctions as UtilReferenceFunctions } from '@0x/contracts-utils';
import { ExchangeRevertErrors } from '@0x/order-utils';
import { FillResults, OrderInfo, OrderStatus, OrderWithoutDomain as Order } from '@0x/types';
import { AnyRevertError, BigNumber, StringRevertError } from '@0x/utils';
import {
AnyRevertError,
BigNumber,
StringRevertError
} from '@0x/utils';
import { LogEntry, LogWithDecodedArgs } from 'ethereum-types';
import * as ethjs from 'ethereumjs-util';
import * as _ from 'lodash';
@@ -22,14 +28,17 @@ import {
blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const { ONE_ETHER } = constants;
const { addFillResults, getPartialAmountFloor } = LibReferenceFunctions;
const { safeSub } = UtilReferenceFunctions;
const randomAddress = () => hexRandom(constants.ADDRESS_LENGTH);
const randomAssetData = () => hexRandom(34);
const randomSignature = () => `${hexRandom(65)}02`;
const randomSignature = () => `${hexRandom(32 * 3)}`;
const randomAmount = (maxAmount: BigNumber = ONE_ETHER) => maxAmount.times(_.random(0, 100, true).toFixed(12));
const randomTimestamp = () => new BigNumber(Math.floor(_.now() / 1000) + _.random(0, 34560));
const randomSalt = () => new BigNumber(hexRandom(constants.WORD_LENGTH).substr(2), 16);
const ALWAYS_FAILING_SALT = constants.MAX_UINT256;
const ALWAYS_FAILING_SALT_REVERT_ERROR = new StringRevertError('ALWAYS_FAILING_SALT');
const BAD_SIGNATURE_REVERT_REASON = new StringRevertError('BAD_SIGNATURE');
const EMPTY_FILL_RESULTS = {
makerAssetFilledAmount: constants.ZERO_AMOUNT,
takerAssetFilledAmount: constants.ZERO_AMOUNT,
@@ -71,14 +80,21 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
// Computes the expected (fake) order hash generated by the `TestWrapperFunctions` contract.
function getExpectedOrderHash(order: Order): string {
// It's just `keccak256(order.salt)`.
return ethjs.bufferToHex(ethjs.sha3(ethjs.setLengthLeft(
`0x${order.salt.toString(16)}`, constants.WORD_LENGTH)));
return ethjs.bufferToHex(ethjs.sha3(Buffer.concat([
ethjs.toBuffer(order.makerAssetData),
ethjs.toBuffer(order.takerAssetData),
ethjs.setLengthLeft(`0x${order.salt.toString(16)}`, constants.WORD_LENGTH),
])));
}
// Computes a valid signature for an order.
function createOrderSignature(order: Order): string {
return ethjs.bufferToHex(ethjs.sha3(ethjs.toBuffer(getExpectedOrderHash(order))));
}
// Computes the expected (fake) fill results from `TestWrapperFunctions` `_fillOrder` implementation.
function getExpectedFillResults(order: Order): FillResults {
if (order.salt === ALWAYS_FAILING_SALT) {
function getExpectedFillResults(order: Order, signature: string): FillResults {
if (order.salt === ALWAYS_FAILING_SALT || signature !== createOrderSignature(order)) {
return EMPTY_FILL_RESULTS;
}
return {
@@ -136,8 +152,8 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
// the `takerAssetFilledAmount`.
takerAssetAmount: fillAmount,
});
const signature = randomSignature();
const expectedResult = getExpectedFillResults(order);
const signature = createOrderSignature(order);
const expectedResult = getExpectedFillResults(order, signature);
const expectedCalls = [[ order, fillAmount, signature ]];
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.fillOrKillOrder,
@@ -162,7 +178,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const tx = testContract.fillOrKillOrder.awaitTransactionSuccessAsync(
order,
fillAmount,
randomSignature(),
createOrderSignature(order),
);
return expect(tx).to.revertWith(expectedError);
});
@@ -180,7 +196,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const tx = testContract.fillOrKillOrder.awaitTransactionSuccessAsync(
order,
fillAmount,
randomSignature(),
createOrderSignature(order),
);
return expect(tx).to.revertWith(expectedError);
});
@@ -194,7 +210,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const tx = testContract.fillOrKillOrder.awaitTransactionSuccessAsync(
order,
fillAmount,
randomSignature(),
createOrderSignature(order),
);
return expect(tx).to.revertWith(expectedError);
});
@@ -204,8 +220,8 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
it('calls `fillOrder()` and returns its result', async () => {
const fillAmount = randomAmount();
const order = randomOrder();
const signature = randomSignature();
const expectedResult = getExpectedFillResults(order);
const signature = createOrderSignature(order);
const expectedResult = getExpectedFillResults(order, signature);
const expectedCalls = [[ order, fillAmount, signature ]];
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.fillOrderNoThrow,
@@ -222,7 +238,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const order = randomOrder({
salt: ALWAYS_FAILING_SALT,
});
const signature = randomSignature();
const signature = createOrderSignature(order);
const expectedResult = EMPTY_FILL_RESULTS;
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.fillOrderNoThrow,
@@ -251,8 +267,8 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 1;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const expectedResult = orders.map(getExpectedFillResults);
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const expectedResult = _.times(COUNT, i => getExpectedFillResults(orders[i], signatures[i]));
const expectedCalls = _.zip(orders, fillAmounts, signatures);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.batchFillOrders,
@@ -268,8 +284,8 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 8;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const expectedResult = orders.map(getExpectedFillResults);
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const expectedResult = _.times(COUNT, i => getExpectedFillResults(orders[i], signatures[i]));
const expectedCalls = _.zip(orders, fillAmounts, signatures);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.batchFillOrders,
@@ -284,11 +300,11 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
it('works with duplicate orders', async () => {
const COUNT = 2;
const order = randomOrder();
const signature = randomSignature();
const signature = createOrderSignature(order);
const orders = _.times(COUNT, () => order);
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount.dividedToIntegerBy(COUNT));
const signatures = _.times(COUNT, () => signature);
const expectedResult = orders.map(getExpectedFillResults);
const expectedResult = _.times(COUNT, i => getExpectedFillResults(orders[i], signatures[i]));
const expectedCalls = _.zip(orders, fillAmounts, signatures);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.batchFillOrders,
@@ -304,7 +320,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 8;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT - 1, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const expectedError = new AnyRevertError(); // Just a generic revert.
const tx = txHelper.getResultAndReceiptAsync(
testContract.batchFillOrders,
@@ -319,7 +335,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 8;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT - 1, () => randomSignature());
const signatures = _.times(COUNT - 1, i => createOrderSignature(orders[i]));
const expectedError = new AnyRevertError(); // Just a generic revert.
const tx = txHelper.getResultAndReceiptAsync(
testContract.batchFillOrders,
@@ -347,8 +363,8 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 1;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const expectedResult = orders.map(getExpectedFillResults);
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const expectedResult = _.times(COUNT, i => getExpectedFillResults(orders[i], signatures[i]));
const expectedCalls = _.zip(orders, fillAmounts, signatures);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.batchFillOrKillOrders,
@@ -364,8 +380,8 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 8;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const expectedResult = orders.map(getExpectedFillResults);
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const expectedResult = _.times(COUNT, i => getExpectedFillResults(orders[i], signatures[i]));
const expectedCalls = _.zip(orders, fillAmounts, signatures);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.batchFillOrKillOrders,
@@ -380,11 +396,11 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
it('works with duplicate orders', async () => {
const COUNT = 2;
const order = randomOrder();
const signature = randomSignature();
const signature = createOrderSignature(order);
const orders = _.times(COUNT, () => order);
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => signature);
const expectedResult = orders.map(getExpectedFillResults);
const expectedResult = _.times(COUNT, i => getExpectedFillResults(orders[i], signatures[i]));
const expectedCalls = _.zip(orders, fillAmounts, signatures);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.batchFillOrKillOrders,
@@ -402,7 +418,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const orders = _.times(COUNT, () => randomOrder());
const failingOrder = orders[FAILING_ORDER_INDEX];
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
// `_fillOrder()` is overridden to always return `order.takerAssetAmount` as
// the `takerAssetFilledAmount`.
failingOrder.takerAssetAmount = failingOrder.takerAssetAmount.minus(1);
@@ -424,7 +440,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const orders = _.times(COUNT, () => randomOrder());
const failingOrder = orders[FAILING_ORDER_INDEX];
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
// `_fillOrder()` is overridden to always return `order.takerAssetAmount` as
// the `takerAssetFilledAmount`.
failingOrder.takerAssetAmount = failingOrder.takerAssetAmount.plus(1);
@@ -444,7 +460,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 8;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT - 1, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const expectedError = new AnyRevertError(); // Just a generic revert.
const tx = txHelper.getResultAndReceiptAsync(
testContract.batchFillOrKillOrders,
@@ -459,7 +475,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 8;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT - 1, () => randomSignature());
const signatures = _.times(COUNT - 1, i => createOrderSignature(orders[i]));
const expectedError = new AnyRevertError(); // Just a generic revert.
const tx = txHelper.getResultAndReceiptAsync(
testContract.batchFillOrKillOrders,
@@ -471,6 +487,73 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
});
});
describe('marketSellOrders', () => {
function simulateMarketSellOrders(
orders: Order[],
takerAssetFillAmount: BigNumber,
signatures: string[],
): [FillResults, [[Order, BigNumber, string]]] {
const fillOrderCalls = [];
let fillResults = _.cloneDeep(EMPTY_FILL_RESULTS);
const takerAssetData = orders[0].takerAssetData;
for (const [ order, signature ] of _.zip(orders, signatures) as [[Order, string]]) {
const remainingTakerAssetFillAmount = safeSub(
takerAssetFillAmount,
fillResults.takerAssetFilledAmount,
);
const expectedSignature = createOrderSignature(
{
...order,
takerAssetData,
},
);
if (order.salt !== ALWAYS_FAILING_SALT && signature !== expectedSignature) {
fillOrderCalls.push([order, remainingTakerAssetFillAmount, signature]);
fillResults = addFillResults(fillResults, getExpectedFillResults(order, signature));
}
if (fillResults.takerAssetFilledAmount.gte(takerAssetFillAmount)) {
break;
}
}
return [fillResults, fillOrderCalls as any];
}
it('works with one order', async () => {
const COUNT = 1;
const orders = _.times(COUNT, () => randomOrder());
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const takerAssetFillAmount = _.reduce(
orders,
(total, o) => o.takerAssetAmount.plus(total),
constants.ZERO_AMOUNT,
);
const [ expectedResult, expectedCalls ] = simulateMarketSellOrders(
orders,
takerAssetFillAmount,
signatures,
);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.marketSellOrders,
orders,
takerAssetFillAmount,
signatures,
);
expect(actualResult).to.deep.eq(expectedResult);
assertFillOrderCallsFromLogs(receipt.logs, expectedCalls);
});
it('reverts with no orders', async () => {
const expectedError = new AnyRevertError();
const tx = txHelper.getResultAndReceiptAsync(
testContract.marketSellOrders,
[],
constants.ZERO_AMOUNT,
[],
);
return expect(tx).to.revertWith(expectedError);
});
});
describe('batchFillOrdersNoThrow', () => {
it('works with no fills', async () => {
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
@@ -487,8 +570,8 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 1;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const expectedResult = orders.map(getExpectedFillResults);
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const expectedResult = _.times(COUNT, i => getExpectedFillResults(orders[i], signatures[i]));
const expectedCalls = _.zip(orders, fillAmounts, signatures);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.batchFillOrdersNoThrow,
@@ -504,8 +587,8 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 8;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const expectedResult = orders.map(getExpectedFillResults);
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const expectedResult = _.times(COUNT, i => getExpectedFillResults(orders[i], signatures[i]));
const expectedCalls = _.zip(orders, fillAmounts, signatures);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.batchFillOrdersNoThrow,
@@ -520,11 +603,11 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
it('works with duplicate orders', async () => {
const COUNT = 2;
const order = randomOrder();
const signature = randomSignature();
const signature = createOrderSignature(order);
const orders = _.times(COUNT, () => order);
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount.dividedBy(COUNT));
const signatures = _.times(COUNT, () => signature);
const expectedResult = orders.map(getExpectedFillResults);
const expectedResult = _.times(COUNT, i => getExpectedFillResults(orders[i], signatures[i]));
const expectedCalls = _.zip(orders, fillAmounts, signatures);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
testContract.batchFillOrdersNoThrow,
@@ -541,10 +624,10 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const FAILING_ORDER_INDEX = 6;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const failingOrder = orders[FAILING_ORDER_INDEX];
failingOrder.salt = ALWAYS_FAILING_SALT;
const expectedResult = orders.map(getExpectedFillResults);
const expectedResult = _.times(COUNT, i => getExpectedFillResults(orders[i], signatures[i]));
const expectedCalls = _.zip(orders, fillAmounts, signatures);
expectedCalls.splice(FAILING_ORDER_INDEX, 1);
const [ actualResult, receipt ] = await txHelper.getResultAndReceiptAsync(
@@ -561,7 +644,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 8;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT - 1, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT, () => randomSignature());
const signatures = _.times(COUNT, i => createOrderSignature(orders[i]));
const expectedError = new AnyRevertError(); // Just a generic revert.
const tx = txHelper.getResultAndReceiptAsync(
testContract.batchFillOrdersNoThrow,
@@ -576,7 +659,7 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
const COUNT = 8;
const orders = _.times(COUNT, () => randomOrder());
const fillAmounts = _.times(COUNT, i => orders[i].takerAssetAmount);
const signatures = _.times(COUNT - 1, () => randomSignature());
const signatures = _.times(COUNT - 1, i => createOrderSignature(orders[i]));
const expectedError = new AnyRevertError(); // Just a generic revert.
const tx = txHelper.getResultAndReceiptAsync(
testContract.batchFillOrdersNoThrow,