Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EVM] Selfdesctruct test #5273

Merged
merged 19 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions fvm/evm/emulator/emulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,80 @@ func TestContractInteraction(t *testing.T) {
})
}

// Self destruct test deploys a contract with a selfdestruct function
// this function is called and we make sure the balance the contract had
// is returned to the address provided, and the contract address no longer
// contains data.
func TestSelfdestruct(t *testing.T) {
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(testAccount *testutils.EOATestAccount) {
testContract := testutils.GetSelfDestructTestContract(t)

testAddress := types.NewAddressFromString("testaddr")

startBalance := big.NewInt(0).Mul(big.NewInt(1000), big.NewInt(gethParams.Ether))
deployBalance := big.NewInt(0).Mul(big.NewInt(10), big.NewInt(gethParams.Ether))
var contractAddr types.Address

// setup the test with funded account and deploying a selfdestruct contract.
RunWithNewEmulator(t, backend, rootAddr, func(env *emulator.Emulator) {
RunWithNewBlockView(t, env, func(blk types.BlockView) {
_, err := blk.DirectCall(types.NewDepositCall(testAddress, startBalance))
require.NoError(t, err)
})

RunWithNewBlockView(t, env, func(blk types.BlockView) {
res, err := blk.DirectCall(
types.NewDeployCall(
testAddress,
testContract.ByteCode,
math.MaxUint64,
deployBalance),
)
require.NoError(t, err)
contractAddr = res.DeployedContractAddress
})

// call the destroy method which executes selfdestruct call.
RunWithNewBlockView(t, env, func(blk types.BlockView) {
res, err := blk.DirectCall(&types.DirectCall{
Type: types.DirectCallTxType,
From: testAddress,
To: contractAddr,
Data: testContract.MakeCallData(t, "destroy"),
Value: big.NewInt(0),
GasLimit: 100_000,
})
require.NoError(t, err)
require.False(t, res.Failed)
})

// after calling selfdestruct the balance should be returned to the caller and
// equal initial funded balance of the caller.
RunWithNewReadOnlyBlockView(t, env, func(blk types.ReadOnlyBlockView) {
bal, err := blk.BalanceOf(testAddress)
require.NoError(t, err)
require.Equal(t, startBalance, bal)

bal, err = blk.BalanceOf(contractAddr)
require.NoError(t, err)
require.Equal(t, big.NewInt(0), bal)

nonce, err := blk.NonceOf(contractAddr)
require.NoError(t, err)
require.Zero(t, nonce)

code, err := blk.CodeOf(contractAddr)
require.NoError(t, err)
require.Len(t, code, 0)
})
})
})
})
})
}

func TestTransfers(t *testing.T) {
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
Expand Down
32 changes: 32 additions & 0 deletions fvm/evm/testutils/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,38 @@ func GetDummyKittyTestContract(t testing.TB) *TestContract {
}
}

func GetSelfDestructTestContract(tb testing.TB) *TestContract {
bytecode, err := hex.DecodeString("6080604052608180600f5f395ff3fe6080604052348015600e575f80fd5b50600436106026575f3560e01c806383197ef014602a575b5f80fd5b60306032565b005b3373ffffffffffffffffffffffffffffffffffffffff16fffea2646970667358221220a29c41b43845784caa870cd38e1eb6abe7b1f79a50d59ea6ae51d8337eb3b2fd64736f6c63430008160033")
require.NoError(tb, err)

return &TestContract{
Code: `
contract TestDestruct {
constructor() payable {}

function destroy() public {
selfdestruct(payable(msg.sender));
}
ramtinms marked this conversation as resolved.
Show resolved Hide resolved
}
`,
ABI: `[
{
"inputs": [],
"stateMutability": "payable",
"type": "constructor"
},
{
"inputs": [],
"name": "destroy",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`,
ByteCode: bytecode,
}
}

func RunWithDeployedContract(t testing.TB, tc *TestContract, led atree.Ledger, flowEVMRootAddress flow.Address, f func(*TestContract)) {
DeployContract(t, tc, led, flowEVMRootAddress)
f(tc)
Expand Down
Loading