Skip to content

Commit

Permalink
Add test for selfdestruct
Browse files Browse the repository at this point in the history
Also drop less useful EndToEndTest
  • Loading branch information
axic committed Oct 1, 2022
1 parent 2201526 commit 96168a1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
23 changes: 0 additions & 23 deletions test/libsolidity/SolidityEndToEndTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"(
Expand Down
46 changes: 46 additions & 0 deletions test/libsolidity/semanticTests/various/selfdestruct.sol
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 96168a1

Please sign in to comment.