mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #13593 from ethereum/test-selfdestruct
Add test for selfdestruct
This commit is contained in:
commit
2cb618a5c3
@ -912,29 +912,6 @@ BOOST_AUTO_TEST_CASE(transfer_ether)
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(selfdestruct)
|
|
||||||
{
|
|
||||||
char const* sourceCode = R"(
|
|
||||||
contract test {
|
|
||||||
constructor() payable {}
|
|
||||||
function a(address payable receiver) public returns (uint ret) {
|
|
||||||
selfdestruct(receiver);
|
|
||||||
return 10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
u256 amount(130);
|
|
||||||
h160 address(23);
|
|
||||||
ALSO_VIA_YUL(
|
|
||||||
DISABLE_EWASM_TESTRUN()
|
|
||||||
|
|
||||||
compileAndRun(sourceCode, amount);
|
|
||||||
ABI_CHECK(callContractFunction("a(address)", address), bytes());
|
|
||||||
BOOST_CHECK(!addressHasCode(m_contractAddress));
|
|
||||||
BOOST_CHECK_EQUAL(balanceAt(address), amount);
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(keccak256)
|
BOOST_AUTO_TEST_CASE(keccak256)
|
||||||
{
|
{
|
||||||
char const* sourceCode = R"(
|
char const* sourceCode = R"(
|
||||||
|
46
test/libsolidity/semanticTests/various/selfdestruct.sol
Normal file
46
test/libsolidity/semanticTests/various/selfdestruct.sol
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
contract C {
|
||||||
|
event Terminated();
|
||||||
|
|
||||||
|
constructor() payable {
|
||||||
|
}
|
||||||
|
|
||||||
|
function terminate() external {
|
||||||
|
emit Terminated();
|
||||||
|
selfdestruct(payable(msg.sender));
|
||||||
|
// Execution stops here, so the second one is not executed.
|
||||||
|
selfdestruct(payable(msg.sender));
|
||||||
|
emit Terminated();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract D {
|
||||||
|
C public c;
|
||||||
|
|
||||||
|
constructor() payable {
|
||||||
|
c = new C{value: 1 ether}();
|
||||||
|
}
|
||||||
|
|
||||||
|
function f() external {
|
||||||
|
c.terminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
function exists() external returns (bool) {
|
||||||
|
return address(c).code.length != 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// constructor(), 1 ether ->
|
||||||
|
// gas irOptimized: 188203
|
||||||
|
// gas legacy: 261114
|
||||||
|
// gas legacyOptimized: 181602
|
||||||
|
// c() -> 0x137aa4dfc0911524504fcd4d98501f179bc13b4a
|
||||||
|
// balance: 0x137aa4dfc0911524504fcd4d98501f179bc13b4a -> 1000000000000000000
|
||||||
|
// balance -> 0
|
||||||
|
// exists() -> true
|
||||||
|
// f() ->
|
||||||
|
// ~ emit Terminated() from 0x137aa4dfc0911524504fcd4d98501f179bc13b4a
|
||||||
|
// balance: 0x137aa4dfc0911524504fcd4d98501f179bc13b4a -> 0
|
||||||
|
// ~ emit Terminated() from 0x137aa4dfc0911524504fcd4d98501f179bc13b4a
|
||||||
|
// balance -> 1000000000000000000
|
||||||
|
// ~ emit Terminated() from 0x137aa4dfc0911524504fcd4d98501f179bc13b4a
|
||||||
|
// exists() -> false
|
Loading…
Reference in New Issue
Block a user