Merge pull request #13593 from ethereum/test-selfdestruct

Add test for selfdestruct
This commit is contained in:
Daniel 2022-10-17 17:43:08 +02:00 committed by GitHub
commit 2cb618a5c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 23 deletions

View File

@ -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"(

View 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