Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Appeased the linter
Browse files Browse the repository at this point in the history
  • Loading branch information
hysz committed Jul 30, 2019
1 parent 7abec53 commit dc30e10
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 50 deletions.
2 changes: 1 addition & 1 deletion contracts/utils/CHANGELOG.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"note": "Added tests for decoding log arguments when artifact dependencies are included/excluded.",
"pr": 1995
},

{
"note": "Added tests for`getABIDecodedTransactionData` and `getABIDecodedReturnData` in contract wrappers.",
"pr": 2018
Expand Down
91 changes: 52 additions & 39 deletions contracts/utils/contracts/test/TestAbi.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,66 @@ pragma experimental ABIEncoderV2;

contract TestAbi {

/// @dev complex input is dynamic and more difficult to decode than simple input.
struct ComplexInput {
uint256 foo;
bytes bar;
string car;
}

/// @dev complex input is dynamic and more difficult to decode than simple input.
struct ComplexOutput {
ComplexInput input;
bytes lorem;
bytes ipsum;
string dolor;
}

/// @dev The fallback function calls into this contract and executes one of the above functions.
/// This allows us to test `getABIDecodedTransactionData` and `getABIDecodedReturnData` that is
/// include in contract wrappers.
// solhint-disable no-complex-fallback
function ()
external
payable
{
address addr = address(this);
assembly {
// copy calldata to memory
calldatacopy(
0x0,
0x0,
calldatasize()
)
// execute transaction
let success := call(
gas, // send all gas.
addr, // call into this contract.
0, // don't send any ether.
0x0, // input is `txData`.
calldatasize(), // input length is that of `txData`.
0x0, // any return data goes at mem address 0x0.
0 // there is no fixed return value size.
)

// copy return data to memory
returndatacopy(
0x0,
0x0,
returndatasize()
)

// rethrow any exceptions
if iszero(success) {
revert(0, returndatasize())
}

// return call results
return(0, returndatasize())
}
}

/// @dev Tests decoding when both input and output are empty.
function noInputNoOutput()
public
pure
Expand All @@ -43,6 +90,7 @@ contract TestAbi {
require(true == true);
}

/// @dev Tests decoding when input is empty and output is non-empty.
function noInputSimpleOutput()
public
pure
Expand All @@ -51,6 +99,7 @@ contract TestAbi {
return 1991;
}

/// @dev Tests decoding when input is not empty but output is empty.
function simpleInputNoOutput(uint256)
public
pure
Expand All @@ -59,6 +108,7 @@ contract TestAbi {
require(true == true);
}

/// @dev Tests decoding when both input and output are non-empty.
function simpleInputSimpleOutput(uint256)
public
pure
Expand All @@ -67,6 +117,7 @@ contract TestAbi {
return 1991;
}

/// @dev Tests decoding when the input and output are complex.
function complexInputComplexOutput(ComplexInput memory complexInput)
public
pure
Expand All @@ -80,6 +131,7 @@ contract TestAbi {
});
}

/// @dev Tests decoding when the input and output are complex and have more than one argument.
function multiInputMultiOutput(
uint256,
bytes memory,
Expand All @@ -99,43 +151,4 @@ contract TestAbi {
"amet"
);
}

function ()
external
{
address addr = address(this);
assembly {
// copy calldata to memory
calldatacopy(
0x0,
0x0,
calldatasize()
)
// execute transaction
let success := call(
gas, // send all gas.
addr, // call into this contract.
0, // don't send any ether.
0x0, // input is `txData`.
calldatasize(), // input length is that of `txData`.
0x0, // any return data goes at mem address 0x0.
0 // there is no fixed return value size.
)

// copy return data to memory
returndatacopy(
0x0,
0x0,
returndatasize()
)

// rethrow any exceptions
if iszero(success) {
revert(0, returndatasize())
}

// return call results
return(0, returndatasize())
}
}
}
3 changes: 1 addition & 2 deletions contracts/utils/test/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { chaiSetup, provider, txDefaults, web3Wrapper } from '@0x/contracts-test
import { BlockchainLifecycle } from '@0x/dev-utils';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import { DecodedLogArgs, LogWithDecodedArgs } from 'ethereum-types';

import { artifacts, TestAbiContract } from '../src';

Expand Down Expand Up @@ -64,7 +63,7 @@ describe('TestAbi', () => {
car: 'zoom zoom',
};
const output = {
input: input,
input,
lorem: '0x12345678',
ipsum: '0x87654321',
dolor: 'amet',
Expand Down
6 changes: 5 additions & 1 deletion packages/utils/CHANGELOG.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
"version": "4.5.0",
"changes": [
{
"note": "updated to include `strictDecode` for decoding method arguments",
"note": "Updated to include `strictDecode` for decoding method arguments",
"pr": 2018
},
{
"note": "Throw exception when trying to decode beyond boundaries of calldata",
"pr": 2018
}
]
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/test/abi_encoder/evm_data_types_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,8 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => {
const dataType = new AbiEncoder.Int(testDataItem);
const args = new BigNumber(0);
const encodedArgs = dataType.encode(args, encodingRules);
const encodedArgsTruncated = encodedArgs.substr(0, 60);
const truncatedCalldataLength = 60;
const encodedArgsTruncated = encodedArgs.substr(0, truncatedCalldataLength);
// Encode Args and validate result
expect(() => {
dataType.decode(encodedArgsTruncated);
Expand Down
25 changes: 19 additions & 6 deletions packages/utils/test/abi_encoder/methods_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as chai from 'chai';
import * as _ from 'lodash';
import 'mocha';

import { AbiEncoder, BigNumber } from '../../src/';
import { chaiSetup } from '../utils/chai_setup';
import * as _ from 'lodash';

import * as AbiSamples from './abi_samples/method_abis';

Expand All @@ -13,7 +13,13 @@ const expect = chai.expect;
describe('ABI Encoder: Method Encoding / Decoding', () => {
const defaultEncodingRules: AbiEncoder.EncodingRules = { shouldOptimize: false }; // optimizer is tested separately.
const defaultDecodingRules: AbiEncoder.DecodingRules = { shouldConvertStructsToObjects: false };
const runTest = (encoder: AbiEncoder.Method, methodArgs: any, expectedEncoding: string, encodingRules: AbiEncoder.EncodingRules = defaultEncodingRules, decodingRules: AbiEncoder.DecodingRules = defaultDecodingRules) => {
const runTest = <T>(
encoder: AbiEncoder.Method,
methodArgs: any,
expectedEncoding: string,
encodingRules: AbiEncoder.EncodingRules = defaultEncodingRules,
decodingRules: AbiEncoder.DecodingRules = defaultDecodingRules,
) => {
// Validate encoding
// note - the encoder takes an array of parameters as input;
// if there is only 1 parameter then we wrap it in an array (`methodArgsAsAray`) to save code.
Expand All @@ -25,12 +31,19 @@ describe('ABI Encoder: Method Encoding / Decoding', () => {
const decodedValueAsArray = _.isArray(decodedValue) ? decodedValue : _.toArray(decodedValue);
expect(decodedValueAsArray, 'testing `.decode`').to.be.deep.equal(methodArgsAsArray);
// Validate strict decoding
const strictDecodedValue = encoder.strictDecode(encoding, decodingRules);
const strictDecodedValue = encoder.strictDecode<T>(encoding, decodingRules);
expect(strictDecodedValue, 'testing `.strictDecode`').to.be.deep.equal(methodArgs);
};
it('Types with default widths', async () => {
const method = new AbiEncoder.Method(AbiSamples.typesWithDefaultWidthsAbi);
const methodArgs = [new BigNumber(1), new BigNumber(-1), '0x56', [new BigNumber(1)], [new BigNumber(-1)], ['0x56']];
const methodArgs = [
new BigNumber(1),
new BigNumber(-1),
'0x56',
[new BigNumber(1)],
[new BigNumber(-1)],
['0x56'],
];
const expectedEncoding =
'0x09f2b0c30000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000015600000000000000000000000000000000000000000000000000000000000000';
runTest(method, methodArgs, expectedEncoding);
Expand Down Expand Up @@ -177,7 +190,7 @@ describe('ABI Encoder: Method Encoding / Decoding', () => {
const expectedEncoding =
'0x5b998f3500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000046669766500000000000000000000000000000000000000000000000000000000';
runTest(method, methodArgs, expectedEncoding);
});
});
it('Dynamic Tuple (Object input)', async () => {
const method = new AbiEncoder.Method(AbiSamples.dynamicTupleAbi);
const methodArgs = [new BigNumber(5), 'five'];
Expand Down Expand Up @@ -266,7 +279,7 @@ describe('ABI Encoder: Method Encoding / Decoding', () => {
];
const expectedEncoding =
'0x4b49031c000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000440000000000000000000000000000000000000000000000000000000000000088000000000000000000000000000000000000000000000000000000000000009800000000000000000000000000000000000000000000000000000000000000ae0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000034746865206c6974746c6520706970696e67207069706572207069706564206120706970696e6720706970706572207061707065720000000000000000000000000000000000000000000000000000000000000000000000000000000000000081746865206b6964206b6e6f777320686f7720746f20777269746520706f656d732c20776861742063616e204920736179202d2d2049206775657373207468657265732061206c6f74204920636f756c642073617920746f2074727920746f2066696c6c2074686973206c696e6520776974682061206c6f74206f6620746578742e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000163874563783498732482743928742389723894723984700000000000000000000000000000000000000000000000000000000000000000000000000000000006e72834723982374239847239847298472489274987489742847289472394874987498478743294237434923473298472398423748923748923748923472389472894789474893742894728947389427498237432987423894723894732894723894372498237498237428934723980000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000027283473298473248923749238742398742398472894729843278942374982374892374892743982000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000000b736f6d6520737472696e670000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013736f6d6520616e6f7468657220737472696e67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024746865726520617265206a75737420746f6f206d616e7920737472696e6773757020696e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000046865726500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002079616c6c2067686f6e6e61206d616b65206d65206c6f7365206d79206d696e640000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000034746865206c6974746c6520706970696e67207069706572207069706564206120706970696e6720706970706572207061707065720000000000000000000000000000000000000000000000000000000000000000000000000000000000000081746865206b6964206b6e6f777320686f7720746f20777269746520706f656d732c20776861742063616e204920736179202d2d2049206775657373207468657265732061206c6f74204920636f756c642073617920746f2074727920746f2066696c6c2074686973206c696e6520776974682061206c6f74206f6620746578742e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f0ac511500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000081746865206b6964206b6e6f777320686f7720746f20777269746520706f656d732c20776861742063616e204920736179202d2d2049206775657373207468657265732061206c6f74204920636f756c642073617920746f2074727920746f2066696c6c2074686973206c696e6520776974682061206c6f74206f6620746578742e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003d69d500000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000100000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498000000000000000000000000000000000000000000000000000000000000004e616b64686a61736a6b646861736a6b6c647368646a6168646b6a73616864616a6b73646873616a6b646873616a6b646861646a6b617368646a6b73616468616a6b646873616a6b64687361646a6b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002829384723894723843743289742389472398473289472348927489274894738427428947389facdea0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000089b51500000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000100000000000000000000000000746dafa5ebde1f4699f4981d3221892e41d24895000000000000000000000000000000000000000000000000000000000000004e6b73646873616a6b646873616a6b646861646a6b617368646a6b73616468616a6b646873616a6b64687361646a6b616b64686a61736a6b646861736a6b6c647368646a6168646b6a73616864616a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002829384723894398473289472348927489272384374328974238947274894738427428947389facde100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000fa3150000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000010000000000000000000000000089571d322189e415ebde1f4699f498d24246dafa000000000000000000000000000000000000000000000000000000000000004e73646873616a6b646873616a6b646861646a6b617368646a616b64686a61736a6b646861736a6b6c647368646a6168646b6a73616864616a6b6b73616468616a6b646873616a6b64687361646a6b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002838947238437432829384729742389472398473289472348927489274894738427428947389facdef000000000000000000000000000000000000000000000000';
const customDecodingRules = {shouldConvertStructsToObjects: true}; // custom to improve readability
const customDecodingRules = { shouldConvertStructsToObjects: true }; // custom to improve readability
runTest(method, methodArgs, expectedEncoding, defaultEncodingRules, customDecodingRules);
});
});

0 comments on commit dc30e10

Please sign in to comment.