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

Commit

Permalink
Contract wrappers: Catch empty reverts on live networks (#2433)
Browse files Browse the repository at this point in the history
* `@0x/utils`: Allow strict decoding of return values.

* `@0x/base-contract`: Catch empty call reverts on live networks.
`@0x/abi-gen`: Catch empty call reverts on live networks.

* `@0x/contracts-integrations`: Add mainnet contract wrapper `callAsync()` revert behavior tests.

* `@0x/contract-wrappers`: Regenerate wrappers to catch empty reverts on live networks.

* Update CHANGELOGs

* `@0x/contracts-integrations`: Fix solidity linter errors.

* `@0x/abi-gen`: Regenerate test outputs.

* `@0x/base-contract`: Update CHANGELOG.

Co-authored-by: Lawrence Forman <[email protected]>
  • Loading branch information
dorothy-zbornak and merklejerk authored Jan 13, 2020
1 parent 71731d2 commit ebd08d9
Show file tree
Hide file tree
Showing 33 changed files with 467 additions and 39 deletions.
9 changes: 9 additions & 0 deletions contracts/integrations/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "2.2.0",
"changes": [
{
"note": "Add mainnet contract wrapper `callAsync()` revert behavior tests.",
"pr": 2433
}
]
},
{
"version": "2.1.0",
"changes": [
Expand Down
60 changes: 60 additions & 0 deletions contracts/integrations/contracts/test/TestContractWrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2019 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pragma solidity ^0.5.9;


// solhint-disable no-empty-blocks
contract TestContractWrapper {

uint256 constant public VALID_RETURN_VALUE = 0xf984f922a56ea9a20a32a32f0f60f2d216ff0c0a0d16c986a97a7f1897a6613b;

function throwStringRevert() external returns (uint256) {
revert("ERROR");
}

function throwEmptyRevert() external returns (uint256) {
revert();
}

function throwInvalidOpcode() external returns (uint256) {
assembly {
invalid()
}
}

function returnForcedEmpty() external returns (uint256) {
assembly {
return(0x60, 0)
}
}

function returnTruncated() external returns (uint256) {
uint256 v = VALID_RETURN_VALUE;
assembly {
mstore(0x0, v)
return(0x0, 16)
}
}

function returnEmpty() external { }

function returnValid() external returns (uint256) {
return VALID_RETURN_VALUE;
}
}
2 changes: 1 addition & 1 deletion contracts/integrations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"config": {
"publicInterfaceContracts": "TestFramework",
"abis": "./test/generated-artifacts/@(TestDydxUser|TestEth2Dai|TestEth2DaiBridge|TestFramework|TestMainnetAggregatorFills|TestSignatureValidationWallet|TestUniswapBridge|TestUniswapExchange|TestUniswapExchangeFactory).json",
"abis": "./test/generated-artifacts/@(TestContractWrapper|TestDydxUser|TestEth2Dai|TestEth2DaiBridge|TestFramework|TestMainnetAggregatorFills|TestSignatureValidationWallet|TestUniswapBridge|TestUniswapExchange|TestUniswapExchangeFactory).json",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually."
},
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions contracts/integrations/test/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import { ContractArtifact } from 'ethereum-types';

import * as TestContractWrapper from '../test/generated-artifacts/TestContractWrapper.json';
import * as TestDydxUser from '../test/generated-artifacts/TestDydxUser.json';
import * as TestEth2Dai from '../test/generated-artifacts/TestEth2Dai.json';
import * as TestEth2DaiBridge from '../test/generated-artifacts/TestEth2DaiBridge.json';
Expand All @@ -15,6 +16,7 @@ import * as TestUniswapBridge from '../test/generated-artifacts/TestUniswapBridg
import * as TestUniswapExchange from '../test/generated-artifacts/TestUniswapExchange.json';
import * as TestUniswapExchangeFactory from '../test/generated-artifacts/TestUniswapExchangeFactory.json';
export const artifacts = {
TestContractWrapper: TestContractWrapper as ContractArtifact,
TestDydxUser: TestDydxUser as ContractArtifact,
TestEth2Dai: TestEth2Dai as ContractArtifact,
TestEth2DaiBridge: TestEth2DaiBridge as ContractArtifact,
Expand Down
53 changes: 53 additions & 0 deletions contracts/integrations/test/mainnet_contract_wrapper_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { blockchainTests, expect } from '@0x/contracts-test-utils';
import { BigNumber } from '@0x/utils';

import { TestContractWrapperContract } from './wrappers';

blockchainTests.live('Contract wrapper mainnet callAsync revert behavior tests', env => {
// Mainnet address of the `TestContractWrapper` contract.
const TEST_CONTRACT_ADDRESS = '0x3C120F51aa2360E6C7078dbc849591dd14F21405';
const REVERT_STRING = 'ERROR';
const VALID_RESULT = new BigNumber('0xf984f922a56ea9a20a32a32f0f60f2d216ff0c0a0d16c986a97a7f1897a6613b');
let testContract: TestContractWrapperContract;

before(async () => {
testContract = new TestContractWrapperContract(TEST_CONTRACT_ADDRESS, env.provider, env.txDefaults);
});

describe('callAsync()', () => {
it('can decode valid result', async () => {
const result = await testContract.returnValid().callAsync();
expect(result).to.bignumber.eq(VALID_RESULT);
});

it('can decode an empty result', async () => {
const result = await testContract.returnEmpty().callAsync();
expect(result).to.eq(undefined);
});

it('catches a string revert', async () => {
const tx = testContract.throwStringRevert().callAsync();
return expect(tx).to.revertWith(REVERT_STRING);
});

it('catches an empty revert', async () => {
const tx = testContract.throwEmptyRevert().callAsync();
return expect(tx).to.be.rejectedWith('reverted with no data');
});

it('catches invalid opcode', async () => {
const tx = testContract.throwInvalidOpcode().callAsync();
return expect(tx).to.be.rejectedWith('reverted with no data');
});

it('catches a forced empty result', async () => {
const tx = testContract.returnForcedEmpty().callAsync();
return expect(tx).to.be.rejectedWith('reverted with no data');
});

it('catches a truncated result', async () => {
const tx = testContract.returnTruncated().callAsync();
return expect(tx).to.be.rejectedWith('decode beyond the end of calldata');
});
});
});
1 change: 1 addition & 0 deletions contracts/integrations/test/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Warning: This file is auto-generated by contracts-gen. Don't edit manually.
* -----------------------------------------------------------------------------
*/
export * from '../test/generated-wrappers/test_contract_wrapper';
export * from '../test/generated-wrappers/test_dydx_user';
export * from '../test/generated-wrappers/test_eth2_dai';
export * from '../test/generated-wrappers/test_eth2_dai_bridge';
Expand Down
1 change: 1 addition & 0 deletions contracts/integrations/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"include": ["./src/**/*", "./test/**/*", "./generated-wrappers/**/*"],
"files": [
"generated-artifacts/TestFramework.json",
"test/generated-artifacts/TestContractWrapper.json",
"test/generated-artifacts/TestDydxUser.json",
"test/generated-artifacts/TestEth2Dai.json",
"test/generated-artifacts/TestEth2DaiBridge.json",
Expand Down
13 changes: 13 additions & 0 deletions packages/abi-gen/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
[
{
"version": "5.1.0",
"changes": [
{
"note": "Catch empty call reverts on live networks.",
"pr": 2433
},
{
"note": "Regenerate test outputs.",
"pr": 2433
}
]
},
{
"timestamp": 1578272714,
"version": "5.0.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ async callAsync(
const rawCallResult = await self._performCallAsync({ ...callData, data: this.getABIEncodedTransactionData() }, defaultBlock);
{{/ifEquals}}
const abiEncoder = self._lookupAbiEncoder(functionSignature);
BaseContract._throwIfUnexpectedEmptyCallResult(rawCallResult, abiEncoder);
return abiEncoder.strictDecodeReturnValue<{{> return_type outputs=outputs}}>(rawCallResult);
},
getABIEncodedTransactionData(): string {
Expand Down
Loading

0 comments on commit ebd08d9

Please sign in to comment.