Appeased the linter

This commit is contained in:
Greg Hysen
2019-07-30 20:55:43 +02:00
parent 884864cc58
commit 0d4dd5ff0d
6 changed files with 108 additions and 78 deletions

View File

@@ -22,12 +22,14 @@ pragma experimental ABIEncoderV2;
contract TestAbi { contract TestAbi {
/// @dev complex input is dynamic and more difficult to decode than simple input.
struct ComplexInput { struct ComplexInput {
uint256 foo; uint256 foo;
bytes bar; bytes bar;
string car; string car;
} }
/// @dev complex input is dynamic and more difficult to decode than simple input.
struct ComplexOutput { struct ComplexOutput {
ComplexInput input; ComplexInput input;
bytes lorem; bytes lorem;
@@ -35,73 +37,13 @@ contract TestAbi {
string dolor; string dolor;
} }
function noInputNoOutput() /// @dev The fallback function calls into this contract and executes one of the above functions.
public /// This allows us to test `getABIDecodedTransactionData` and `getABIDecodedReturnData` that is
pure /// include in contract wrappers.
{ // solhint-disable no-complex-fallback
// NOP
require(true == true);
}
function noInputSimpleOutput()
public
pure
returns (uint256)
{
return 1991;
}
function simpleInputNoOutput(uint256)
public
pure
{
// NOP
require(true == true);
}
function simpleInputSimpleOutput(uint256)
public
pure
returns (uint256)
{
return 1991;
}
function complexInputComplexOutput(ComplexInput memory complexInput)
public
pure
returns (ComplexOutput memory)
{
return ComplexOutput({
input: complexInput,
lorem: hex'12345678',
ipsum: hex'87654321',
dolor: "amet"
});
}
function multiInputMultiOutput(
uint256,
bytes memory,
string memory
)
public
pure
returns (
bytes memory,
bytes memory,
string memory
)
{
return (
hex'12345678',
hex'87654321',
"amet"
);
}
function () function ()
external external
payable
{ {
address addr = address(this); address addr = address(this);
assembly { assembly {
@@ -138,4 +80,75 @@ contract TestAbi {
return(0, returndatasize()) return(0, returndatasize())
} }
} }
/// @dev Tests decoding when both input and output are empty.
function noInputNoOutput()
public
pure
{
// NOP
require(true == true);
}
/// @dev Tests decoding when input is empty and output is non-empty.
function noInputSimpleOutput()
public
pure
returns (uint256)
{
return 1991;
}
/// @dev Tests decoding when input is not empty but output is empty.
function simpleInputNoOutput(uint256)
public
pure
{
// NOP
require(true == true);
}
/// @dev Tests decoding when both input and output are non-empty.
function simpleInputSimpleOutput(uint256)
public
pure
returns (uint256)
{
return 1991;
}
/// @dev Tests decoding when the input and output are complex.
function complexInputComplexOutput(ComplexInput memory complexInput)
public
pure
returns (ComplexOutput memory)
{
return ComplexOutput({
input: complexInput,
lorem: hex'12345678',
ipsum: hex'87654321',
dolor: "amet"
});
}
/// @dev Tests decoding when the input and output are complex and have more than one argument.
function multiInputMultiOutput(
uint256,
bytes memory,
string memory
)
public
pure
returns (
bytes memory,
bytes memory,
string memory
)
{
return (
hex'12345678',
hex'87654321',
"amet"
);
}
} }

View File

@@ -2,7 +2,6 @@ import { chaiSetup, provider, txDefaults, web3Wrapper } from '@0x/contracts-test
import { BlockchainLifecycle } from '@0x/dev-utils'; import { BlockchainLifecycle } from '@0x/dev-utils';
import { BigNumber } from '@0x/utils'; import { BigNumber } from '@0x/utils';
import * as chai from 'chai'; import * as chai from 'chai';
import { DecodedLogArgs, LogWithDecodedArgs } from 'ethereum-types';
import { artifacts, TestAbiContract } from '../src'; import { artifacts, TestAbiContract } from '../src';
@@ -11,7 +10,7 @@ const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
describe.only('TestAbi', () => { describe('TestAbi', () => {
let testAbi: TestAbiContract; let testAbi: TestAbiContract;
const runTestAsync = async (contractMethod: any, input: any, output: any) => { const runTestAsync = async (contractMethod: any, input: any, output: any) => {
const transaction = contractMethod.getABIEncodedTransactionData(input); const transaction = contractMethod.getABIEncodedTransactionData(input);
@@ -64,14 +63,14 @@ describe.only('TestAbi', () => {
car: 'zoom zoom', car: 'zoom zoom',
}; };
const output = { const output = {
input: input, input,
lorem: '0x12345678', lorem: '0x12345678',
ipsum: '0x87654321', ipsum: '0x87654321',
dolor: 'amet', dolor: 'amet',
}; };
await runTestAsync(testAbi.complexInputComplexOutput, input, output); await runTestAsync(testAbi.complexInputComplexOutput, input, output);
}); });
it('should successfully encode/decode (complex input / complex output)', async () => { it('should successfully encode/decode (multi-input / multi-output)', async () => {
const input = [new BigNumber(1991), '0x1234', 'zoom zoom']; const input = [new BigNumber(1991), '0x1234', 'zoom zoom'];
const output = ['0x12345678', '0x87654321', 'amet']; const output = ['0x12345678', '0x87654321', 'amet'];
const transaction = testAbi.multiInputMultiOutput.getABIEncodedTransactionData( const transaction = testAbi.multiInputMultiOutput.getABIEncodedTransactionData(

View File

@@ -3,7 +3,11 @@
"version": "4.5.0", "version": "4.5.0",
"changes": [ "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 "pr": 2018
} }
] ]

View File

@@ -806,7 +806,8 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => {
const dataType = new AbiEncoder.Int(testDataItem); const dataType = new AbiEncoder.Int(testDataItem);
const args = new BigNumber(0); const args = new BigNumber(0);
const encodedArgs = dataType.encode(args, encodingRules); 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 // Encode Args and validate result
expect(() => { expect(() => {
dataType.decode(encodedArgsTruncated); dataType.decode(encodedArgsTruncated);

View File

@@ -1,9 +1,9 @@
import * as chai from 'chai'; import * as chai from 'chai';
import * as _ from 'lodash';
import 'mocha'; import 'mocha';
import { AbiEncoder, BigNumber } from '../../src/'; import { AbiEncoder, BigNumber } from '../../src/';
import { chaiSetup } from '../utils/chai_setup'; import { chaiSetup } from '../utils/chai_setup';
import * as _ from 'lodash';
import * as AbiSamples from './abi_samples/method_abis'; import * as AbiSamples from './abi_samples/method_abis';
@@ -13,7 +13,13 @@ const expect = chai.expect;
describe('ABI Encoder: Method Encoding / Decoding', () => { describe('ABI Encoder: Method Encoding / Decoding', () => {
const defaultEncodingRules: AbiEncoder.EncodingRules = { shouldOptimize: false }; // optimizer is tested separately. const defaultEncodingRules: AbiEncoder.EncodingRules = { shouldOptimize: false }; // optimizer is tested separately.
const defaultDecodingRules: AbiEncoder.DecodingRules = { shouldConvertStructsToObjects: false }; 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 // Validate encoding
// note - the encoder takes an array of parameters as input; // 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. // if there is only 1 parameter then we wrap it in an array (`methodArgsAsAray`) to save code.
@@ -25,12 +31,19 @@ describe('ABI Encoder: Method Encoding / Decoding', () => {
const decodedValueAsArray = _.isArray(decodedValue) ? decodedValue : _.toArray(decodedValue); const decodedValueAsArray = _.isArray(decodedValue) ? decodedValue : _.toArray(decodedValue);
expect(decodedValueAsArray, 'testing `.decode`').to.be.deep.equal(methodArgsAsArray); expect(decodedValueAsArray, 'testing `.decode`').to.be.deep.equal(methodArgsAsArray);
// Validate strict decoding // 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); expect(strictDecodedValue, 'testing `.strictDecode`').to.be.deep.equal(methodArgs);
}; };
it('Types with default widths', async () => { it('Types with default widths', async () => {
const method = new AbiEncoder.Method(AbiSamples.typesWithDefaultWidthsAbi); 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 = const expectedEncoding =
'0x09f2b0c30000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000015600000000000000000000000000000000000000000000000000000000000000'; '0x09f2b0c30000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000015600000000000000000000000000000000000000000000000000000000000000';
runTest(method, methodArgs, expectedEncoding); runTest(method, methodArgs, expectedEncoding);