From 96168a143827aeff7c6f2f09d7c0161bb13558fb Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 30 Sep 2022 19:56:33 +0200 Subject: [PATCH] Add test for selfdestruct Also drop less useful EndToEndTest --- test/libsolidity/SolidityEndToEndTest.cpp | 23 ---------- .../semanticTests/various/selfdestruct.sol | 46 +++++++++++++++++++ 2 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 test/libsolidity/semanticTests/various/selfdestruct.sol diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 698f6de66..4d6a887bb 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -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) { char const* sourceCode = R"( diff --git a/test/libsolidity/semanticTests/various/selfdestruct.sol b/test/libsolidity/semanticTests/various/selfdestruct.sol new file mode 100644 index 000000000..b823bd14d --- /dev/null +++ b/test/libsolidity/semanticTests/various/selfdestruct.sol @@ -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