This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contract wrappers: Catch empty reverts on live networks (#2433)
* `@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
1 parent
71731d2
commit ebd08d9
Showing
33 changed files
with
467 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
contracts/integrations/contracts/test/TestContractWrapper.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
contracts/integrations/test/mainnet_contract_wrapper_test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.