This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 563
add exception revert test case #345
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
contract State { | ||
uint256 a = 0; | ||
function set(uint256 input) public { | ||
a = input; | ||
require(a < 10); | ||
} | ||
function force_set(uint256 input) public { | ||
a = input; | ||
} | ||
function query() public view returns(uint256) { | ||
return a; | ||
} | ||
} | ||
|
||
contract TestRevert { | ||
State state; | ||
constructor() { | ||
state = new State(); | ||
} | ||
function try_set(uint256 input) public { | ||
try state.set(input) { | ||
} catch (bytes memory) { | ||
} | ||
} | ||
function set(uint256 input) public { | ||
state.force_set(input); | ||
} | ||
function query() public view returns(uint256) { | ||
return state.query(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/solidity/suites/exception/contracts/test/Migrations.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,19 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
contract Migrations { | ||
address public owner = msg.sender; | ||
uint public last_completed_migration; | ||
|
||
modifier restricted() { | ||
require( | ||
msg.sender == owner, | ||
"This function is restricted to the contract's owner" | ||
); | ||
_; | ||
} | ||
|
||
function setCompleted(uint completed) public restricted { | ||
last_completed_migration = completed; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/solidity/suites/exception/migrations/1_initial_migration.js
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,5 @@ | ||
const Migrations = artifacts.require("Migrations"); | ||
|
||
module.exports = function (deployer) { | ||
deployer.deploy(Migrations); | ||
}; |
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,15 @@ | ||
{ | ||
"name": "exception", | ||
"version": "1.0.0", | ||
"author": "huangyi <[email protected]>", | ||
"license": "GPL-3.0-or-later", | ||
"scripts": { | ||
"test-ganache": "yarn truffle test", | ||
"test-ethermint": "yarn truffle test --network ethermint" | ||
}, | ||
"devDependencies": { | ||
"truffle": "^5.1.42", | ||
"truffle-assertions": "^0.9.2", | ||
"web3": "^1.2.11" | ||
} | ||
} |
Empty file.
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,31 @@ | ||
const TestRevert = artifacts.require("TestRevert") | ||
const truffleAssert = require('truffle-assertions'); | ||
|
||
async function expectRevert(promise) { | ||
try { | ||
await promise; | ||
} catch (error) { | ||
if (error.message.indexOf('revert') === -1) { | ||
expect('revert').to.equal(error.message, 'Wrong kind of exception received'); | ||
} | ||
return; | ||
} | ||
expect.fail('Expected an exception but none was received'); | ||
} | ||
|
||
contract('TestRevert', (accounts) => { | ||
let revert | ||
|
||
beforeEach(async () => { | ||
revert = await TestRevert.new() | ||
}) | ||
it('should revert', async () => { | ||
await revert.try_set(10) | ||
no = await revert.query() | ||
assert.equal(no, '0', 'The set should be reverted') | ||
|
||
await revert.set(10) | ||
no = await revert.query() | ||
assert.equal(no, '10', 'The force set should not be reverted') | ||
}) | ||
}) |
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,17 @@ | ||
module.exports = { | ||
networks: { | ||
// Development network is just left as truffle's default settings | ||
ethermint: { | ||
host: "127.0.0.1", // Localhost (default: none) | ||
port: 8545, // Standard Ethereum port (default: none) | ||
network_id: "*", // Any network (default: none) | ||
gas: 5000000, // Gas sent with each transaction | ||
gasPrice: 1000000000, // 1 gwei (in wei) | ||
}, | ||
}, | ||
compilers: { | ||
solc: { | ||
version: "0.8.6", | ||
}, | ||
}, | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will just throw and end the execution. You should use the
expectRevert
function which is defined aboveThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the test case actually success on ganache, only fail on ethermint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
await revert.set(10)
this can success on ganache?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay I saw the code. Yeah it should be success.