From 30ae3bd1e8bd0567785e06d6a0814e233d4a64b1 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Tue, 23 Jan 2024 16:13:25 +0100 Subject: [PATCH 1/9] add self destruct test --- fvm/evm/testutils/contract.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/fvm/evm/testutils/contract.go b/fvm/evm/testutils/contract.go index f67cced4c94..69b9e5bbbe3 100644 --- a/fvm/evm/testutils/contract.go +++ b/fvm/evm/testutils/contract.go @@ -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)); + } + } + `, + 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) From 4132476276dbd0148c09eaa3148f77a5bcb453b2 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Tue, 23 Jan 2024 16:13:29 +0100 Subject: [PATCH 2/9] add self destruct test --- fvm/evm/emulator/emulator_test.go | 70 +++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/fvm/evm/emulator/emulator_test.go b/fvm/evm/emulator/emulator_test.go index 3cca27b0906..da57a99c4ca 100644 --- a/fvm/evm/emulator/emulator_test.go +++ b/fvm/evm/emulator/emulator_test.go @@ -290,6 +290,76 @@ 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) + + 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) { From 846d486b87a34f066d2d89709cce554c4caa66a4 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Tue, 23 Jan 2024 19:04:23 +0100 Subject: [PATCH 3/9] check nonce --- fvm/evm/emulator/emulator_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fvm/evm/emulator/emulator_test.go b/fvm/evm/emulator/emulator_test.go index da57a99c4ca..631df805ab9 100644 --- a/fvm/evm/emulator/emulator_test.go +++ b/fvm/evm/emulator/emulator_test.go @@ -350,6 +350,10 @@ func TestSelfdestruct(t *testing.T) { 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) From 83b5e3c86db810349fea0f67c1f144fbd3713627 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 25 Jan 2024 14:12:31 +0100 Subject: [PATCH 4/9] update the test according to eip6780 --- fvm/evm/emulator/emulator_test.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/fvm/evm/emulator/emulator_test.go b/fvm/evm/emulator/emulator_test.go index 631df805ab9..d43036bc5cd 100644 --- a/fvm/evm/emulator/emulator_test.go +++ b/fvm/evm/emulator/emulator_test.go @@ -292,16 +292,16 @@ 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. +// is returned to the address provided, and the contract data stays according to the +// EIP 6780 https://eips.ethereum.org/EIPS/eip-6780 in case where the selfdestruct +// is not caleld in the same transaction as deployment. 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) + 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 @@ -325,6 +325,16 @@ func TestSelfdestruct(t *testing.T) { contractAddr = res.DeployedContractAddress }) + RunWithNewReadOnlyBlockView(t, env, func(blk types.ReadOnlyBlockView) { + bal, err := blk.BalanceOf(testAddress) + require.NoError(t, err) + require.Equal(t, big.NewInt(0).Sub(startBalance, deployBalance), bal) + + bal, err = blk.BalanceOf(contractAddr) + require.NoError(t, err) + require.Equal(t, deployBalance, bal) + }) + // call the destroy method which executes selfdestruct call. RunWithNewBlockView(t, env, func(blk types.BlockView) { res, err := blk.DirectCall(&types.DirectCall{ @@ -352,11 +362,11 @@ func TestSelfdestruct(t *testing.T) { nonce, err := blk.NonceOf(contractAddr) require.NoError(t, err) - require.Zero(t, nonce) + require.Equal(t, uint64(1), nonce) code, err := blk.CodeOf(contractAddr) require.NoError(t, err) - require.Len(t, code, 0) + require.True(t, len(code) > 0) }) }) }) From 45b42ef3071f44b7049817cd3fa268d2147cc716 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Fri, 16 Feb 2024 16:36:04 +0100 Subject: [PATCH 5/9] update test contract with destroy --- fvm/evm/testutils/contracts/test.sol | 6 +- fvm/evm/testutils/contracts/test_abi.json | 319 +++++++++++---------- fvm/evm/testutils/contracts/test_bytes.hex | 2 +- 3 files changed, 169 insertions(+), 158 deletions(-) diff --git a/fvm/evm/testutils/contracts/test.sol b/fvm/evm/testutils/contracts/test.sol index 9d57b1e7ad6..054521cc330 100644 --- a/fvm/evm/testutils/contracts/test.sol +++ b/fvm/evm/testutils/contracts/test.sol @@ -29,6 +29,10 @@ contract Storage { return block.prevrandao; } + function destroy() public { + selfdestruct(payable(msg.sender)); + } + function verifyArchCallToFlowBlockHeight(uint64 expected) public view returns (uint64){ (bool ok, bytes memory data) = cadenceArch.staticcall(abi.encodeWithSignature("flowBlockHeight()")); require(ok, "unsuccessful call to arch "); @@ -44,4 +48,4 @@ contract Storage { require(expected == output, "output doesnt match the expected value"); return output; } -} \ No newline at end of file +} diff --git a/fvm/evm/testutils/contracts/test_abi.json b/fvm/evm/testutils/contracts/test_abi.json index f9184621011..7dbf0825d0b 100644 --- a/fvm/evm/testutils/contracts/test_abi.json +++ b/fvm/evm/testutils/contracts/test_abi.json @@ -1,157 +1,164 @@ [ - { - "inputs": [], - "stateMutability": "payable", - "type": "constructor" - }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "num", - "type": "uint256" - } - ], - "name": "blockHash", - "outputs": [ - { - "internalType": "bytes32", - "name": "", - "type": "bytes32" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "blockNumber", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "blockTime", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "cadenceArch", - "outputs": [ - { - "internalType": "address", - "name": "", - "type": "address" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "random", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "retrieve", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "num", - "type": "uint256" - } - ], - "name": "store", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "uint64", - "name": "expected", - "type": "uint64" - } - ], - "name": "verifyArchCallToFlowBlockHeight", - "outputs": [ - { - "internalType": "uint64", - "name": "", - "type": "uint64" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "bool", - "name": "expected", - "type": "bool" - }, - { - "internalType": "address", - "name": "arg0", - "type": "address" - }, - { - "internalType": "bytes32", - "name": "arg1", - "type": "bytes32" - }, - { - "internalType": "bytes", - "name": "arg2", - "type": "bytes" - } - ], - "name": "verifyArchCallToVerifyCOAOwnershipProof", - "outputs": [ - { - "internalType": "bool", - "name": "", - "type": "bool" - } - ], - "stateMutability": "view", - "type": "function" - } -] \ No newline at end of file + { + "inputs": [], + "stateMutability": "payable", + "type": "constructor" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "num", + "type": "uint256" + } + ], + "name": "blockHash", + "outputs": [ + { + "internalType": "bytes32", + "name": "", + "type": "bytes32" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "blockNumber", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "blockTime", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "cadenceArch", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "destroy", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "random", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "retrieve", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "num", + "type": "uint256" + } + ], + "name": "store", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint64", + "name": "expected", + "type": "uint64" + } + ], + "name": "verifyArchCallToFlowBlockHeight", + "outputs": [ + { + "internalType": "uint64", + "name": "", + "type": "uint64" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bool", + "name": "expected", + "type": "bool" + }, + { + "internalType": "address", + "name": "arg0", + "type": "address" + }, + { + "internalType": "bytes32", + "name": "arg1", + "type": "bytes32" + }, + { + "internalType": "bytes", + "name": "arg2", + "type": "bytes" + } + ], + "name": "verifyArchCallToVerifyCOAOwnershipProof", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + } +] diff --git a/fvm/evm/testutils/contracts/test_bytes.hex b/fvm/evm/testutils/contracts/test_bytes.hex index c6a59c4f1fa..1e1e4e77b47 100644 --- a/fvm/evm/testutils/contracts/test_bytes.hex +++ b/fvm/evm/testutils/contracts/test_bytes.hex @@ -1 +1 @@ -6080604052610c4d806100115f395ff3fe608060405234801561000f575f80fd5b5060043610610091575f3560e01c80635ec01e4d116100645780635ec01e4d1461011f5780636057361d1461013d578063828dd0481461015957806385df51fd14610189578063d0d250bd146101b957610091565b80632e64cec11461009557806348b15166146100b357806352e24024146100d157806357e871e714610101575b5f80fd5b61009d6101d7565b6040516100aa9190610588565b60405180910390f35b6100bb6101df565b6040516100c89190610588565b60405180910390f35b6100eb60048036038101906100e691906105ef565b6101e6565b6040516100f89190610629565b60405180910390f35b610109610393565b6040516101169190610588565b60405180910390f35b61012761039a565b6040516101349190610588565b60405180910390f35b6101576004803603810190610152919061066c565b6103a1565b005b610173600480360381019061016e9190610895565b6103aa565b6040516101809190610924565b60405180910390f35b6101a3600480360381019061019e919061066c565b610559565b6040516101b0919061094c565b60405180910390f35b6101c1610563565b6040516101ce9190610974565b60405180910390f35b5f8054905090565b5f42905090565b5f805f6801000000000000000173ffffffffffffffffffffffffffffffffffffffff166040516024016040516020818303038152906040527f53e87d66000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505060405161029991906109f9565b5f60405180830381855afa9150503d805f81146102d1576040519150601f19603f3d011682016040523d82523d5f602084013e6102d6565b606091505b50915091508161031b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161031290610a69565b60405180910390fd5b5f818060200190518101906103309190610a9b565b90508067ffffffffffffffff168567ffffffffffffffff1614610388576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161037f90610b36565b60405180910390fd5b809350505050919050565b5f43905090565b5f44905090565b805f8190555050565b5f805f6801000000000000000173ffffffffffffffffffffffffffffffffffffffff168686866040516024016103e293929190610b9c565b6040516020818303038152906040527f5ee837e7000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505060405161046c91906109f9565b5f60405180830381855afa9150503d805f81146104a4576040519150601f19603f3d011682016040523d82523d5f602084013e6104a9565b606091505b5091509150816104ee576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104e590610a69565b60405180910390fd5b5f818060200190518101906105039190610bec565b90508015158815151461054b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161054290610b36565b60405180910390fd5b809350505050949350505050565b5f81409050919050565b6801000000000000000181565b5f819050919050565b61058281610570565b82525050565b5f60208201905061059b5f830184610579565b92915050565b5f604051905090565b5f80fd5b5f80fd5b5f67ffffffffffffffff82169050919050565b6105ce816105b2565b81146105d8575f80fd5b50565b5f813590506105e9816105c5565b92915050565b5f60208284031215610604576106036105aa565b5b5f610611848285016105db565b91505092915050565b610623816105b2565b82525050565b5f60208201905061063c5f83018461061a565b92915050565b61064b81610570565b8114610655575f80fd5b50565b5f8135905061066681610642565b92915050565b5f60208284031215610681576106806105aa565b5b5f61068e84828501610658565b91505092915050565b5f8115159050919050565b6106ab81610697565b81146106b5575f80fd5b50565b5f813590506106c6816106a2565b92915050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6106f5826106cc565b9050919050565b610705816106eb565b811461070f575f80fd5b50565b5f81359050610720816106fc565b92915050565b5f819050919050565b61073881610726565b8114610742575f80fd5b50565b5f813590506107538161072f565b92915050565b5f80fd5b5f80fd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b6107a782610761565b810181811067ffffffffffffffff821117156107c6576107c5610771565b5b80604052505050565b5f6107d86105a1565b90506107e4828261079e565b919050565b5f67ffffffffffffffff82111561080357610802610771565b5b61080c82610761565b9050602081019050919050565b828183375f83830152505050565b5f610839610834846107e9565b6107cf565b9050828152602081018484840111156108555761085461075d565b5b610860848285610819565b509392505050565b5f82601f83011261087c5761087b610759565b5b813561088c848260208601610827565b91505092915050565b5f805f80608085870312156108ad576108ac6105aa565b5b5f6108ba878288016106b8565b94505060206108cb87828801610712565b93505060406108dc87828801610745565b925050606085013567ffffffffffffffff8111156108fd576108fc6105ae565b5b61090987828801610868565b91505092959194509250565b61091e81610697565b82525050565b5f6020820190506109375f830184610915565b92915050565b61094681610726565b82525050565b5f60208201905061095f5f83018461093d565b92915050565b61096e816106eb565b82525050565b5f6020820190506109875f830184610965565b92915050565b5f81519050919050565b5f81905092915050565b5f5b838110156109be5780820151818401526020810190506109a3565b5f8484015250505050565b5f6109d38261098d565b6109dd8185610997565b93506109ed8185602086016109a1565b80840191505092915050565b5f610a0482846109c9565b915081905092915050565b5f82825260208201905092915050565b7f756e7375636365737366756c2063616c6c20746f2061726368000000000000005f82015250565b5f610a53601983610a0f565b9150610a5e82610a1f565b602082019050919050565b5f6020820190508181035f830152610a8081610a47565b9050919050565b5f81519050610a95816105c5565b92915050565b5f60208284031215610ab057610aaf6105aa565b5b5f610abd84828501610a87565b91505092915050565b7f6f757470757420646f65736e74206d61746368207468652065787065637465645f8201527f2076616c75650000000000000000000000000000000000000000000000000000602082015250565b5f610b20602683610a0f565b9150610b2b82610ac6565b604082019050919050565b5f6020820190508181035f830152610b4d81610b14565b9050919050565b5f82825260208201905092915050565b5f610b6e8261098d565b610b788185610b54565b9350610b888185602086016109a1565b610b9181610761565b840191505092915050565b5f606082019050610baf5f830186610965565b610bbc602083018561093d565b8181036040830152610bce8184610b64565b9050949350505050565b5f81519050610be6816106a2565b92915050565b5f60208284031215610c0157610c006105aa565b5b5f610c0e84828501610bd8565b9150509291505056fea26469706673582212200645c22bab8a46d284684ca77d210bb3c64f2ff98286830dea50f10dd4ae421c64736f6c63430008160033 \ No newline at end of file +6080604052610ce3806100115f395ff3fe608060405234801561000f575f80fd5b506004361061009c575f3560e01c80636057361d116100645780636057361d14610148578063828dd0481461016457806383197ef01461019457806385df51fd1461019e578063d0d250bd146101ce5761009c565b80632e64cec1146100a057806348b15166146100be57806352e24024146100dc57806357e871e71461010c5780635ec01e4d1461012a575b5f80fd5b6100a86101ec565b6040516100b591906105b6565b60405180910390f35b6100c66101f4565b6040516100d391906105b6565b60405180910390f35b6100f660048036038101906100f1919061061d565b6101fb565b6040516101039190610657565b60405180910390f35b6101146103a8565b60405161012191906105b6565b60405180910390f35b6101326103af565b60405161013f91906105b6565b60405180910390f35b610162600480360381019061015d919061069a565b6103b6565b005b61017e600480360381019061017991906108c3565b6103bf565b60405161018b9190610952565b60405180910390f35b61019c61056e565b005b6101b860048036038101906101b3919061069a565b610587565b6040516101c5919061097a565b60405180910390f35b6101d6610591565b6040516101e391906109a2565b60405180910390f35b5f8054905090565b5f42905090565b5f805f6801000000000000000173ffffffffffffffffffffffffffffffffffffffff166040516024016040516020818303038152906040527f53e87d66000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff83818316178352505050506040516102ae9190610a27565b5f60405180830381855afa9150503d805f81146102e6576040519150601f19603f3d011682016040523d82523d5f602084013e6102eb565b606091505b509150915081610330576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161032790610a97565b60405180910390fd5b5f818060200190518101906103459190610ac9565b90508067ffffffffffffffff168567ffffffffffffffff161461039d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161039490610b64565b60405180910390fd5b809350505050919050565b5f43905090565b5f44905090565b805f8190555050565b5f805f6801000000000000000173ffffffffffffffffffffffffffffffffffffffff168686866040516024016103f793929190610bca565b6040516020818303038152906040527f5ee837e7000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff83818316178352505050506040516104819190610a27565b5f60405180830381855afa9150503d805f81146104b9576040519150601f19603f3d011682016040523d82523d5f602084013e6104be565b606091505b509150915081610503576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104fa90610c50565b60405180910390fd5b5f818060200190518101906105189190610c82565b905080151588151514610560576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161055790610b64565b60405180910390fd5b809350505050949350505050565b3373ffffffffffffffffffffffffffffffffffffffff16ff5b5f81409050919050565b6801000000000000000181565b5f819050919050565b6105b08161059e565b82525050565b5f6020820190506105c95f8301846105a7565b92915050565b5f604051905090565b5f80fd5b5f80fd5b5f67ffffffffffffffff82169050919050565b6105fc816105e0565b8114610606575f80fd5b50565b5f81359050610617816105f3565b92915050565b5f60208284031215610632576106316105d8565b5b5f61063f84828501610609565b91505092915050565b610651816105e0565b82525050565b5f60208201905061066a5f830184610648565b92915050565b6106798161059e565b8114610683575f80fd5b50565b5f8135905061069481610670565b92915050565b5f602082840312156106af576106ae6105d8565b5b5f6106bc84828501610686565b91505092915050565b5f8115159050919050565b6106d9816106c5565b81146106e3575f80fd5b50565b5f813590506106f4816106d0565b92915050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610723826106fa565b9050919050565b61073381610719565b811461073d575f80fd5b50565b5f8135905061074e8161072a565b92915050565b5f819050919050565b61076681610754565b8114610770575f80fd5b50565b5f813590506107818161075d565b92915050565b5f80fd5b5f80fd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b6107d58261078f565b810181811067ffffffffffffffff821117156107f4576107f361079f565b5b80604052505050565b5f6108066105cf565b905061081282826107cc565b919050565b5f67ffffffffffffffff8211156108315761083061079f565b5b61083a8261078f565b9050602081019050919050565b828183375f83830152505050565b5f61086761086284610817565b6107fd565b9050828152602081018484840111156108835761088261078b565b5b61088e848285610847565b509392505050565b5f82601f8301126108aa576108a9610787565b5b81356108ba848260208601610855565b91505092915050565b5f805f80608085870312156108db576108da6105d8565b5b5f6108e8878288016106e6565b94505060206108f987828801610740565b935050604061090a87828801610773565b925050606085013567ffffffffffffffff81111561092b5761092a6105dc565b5b61093787828801610896565b91505092959194509250565b61094c816106c5565b82525050565b5f6020820190506109655f830184610943565b92915050565b61097481610754565b82525050565b5f60208201905061098d5f83018461096b565b92915050565b61099c81610719565b82525050565b5f6020820190506109b55f830184610993565b92915050565b5f81519050919050565b5f81905092915050565b5f5b838110156109ec5780820151818401526020810190506109d1565b5f8484015250505050565b5f610a01826109bb565b610a0b81856109c5565b9350610a1b8185602086016109cf565b80840191505092915050565b5f610a3282846109f7565b915081905092915050565b5f82825260208201905092915050565b7f756e7375636365737366756c2063616c6c20746f2061726368200000000000005f82015250565b5f610a81601a83610a3d565b9150610a8c82610a4d565b602082019050919050565b5f6020820190508181035f830152610aae81610a75565b9050919050565b5f81519050610ac3816105f3565b92915050565b5f60208284031215610ade57610add6105d8565b5b5f610aeb84828501610ab5565b91505092915050565b7f6f757470757420646f65736e74206d61746368207468652065787065637465645f8201527f2076616c75650000000000000000000000000000000000000000000000000000602082015250565b5f610b4e602683610a3d565b9150610b5982610af4565b604082019050919050565b5f6020820190508181035f830152610b7b81610b42565b9050919050565b5f82825260208201905092915050565b5f610b9c826109bb565b610ba68185610b82565b9350610bb68185602086016109cf565b610bbf8161078f565b840191505092915050565b5f606082019050610bdd5f830186610993565b610bea602083018561096b565b8181036040830152610bfc8184610b92565b9050949350505050565b7f756e7375636365737366756c2063616c6c20746f2061726368000000000000005f82015250565b5f610c3a601983610a3d565b9150610c4582610c06565b602082019050919050565b5f6020820190508181035f830152610c6781610c2e565b9050919050565b5f81519050610c7c816106d0565b92915050565b5f60208284031215610c9757610c966105d8565b5b5f610ca484828501610c6e565b9150509291505056fea264697066735822122065299275808c81a91d0fa6748ecaa18badeb2146c070cb28af0754e09be419de64736f6c63430008180033 From c564f7d8150d5445e4eceade578f9e642e80fcf9 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Fri, 16 Feb 2024 16:36:18 +0100 Subject: [PATCH 6/9] remove unneeded contract --- fvm/evm/testutils/contract.go | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/fvm/evm/testutils/contract.go b/fvm/evm/testutils/contract.go index f8ff7d89781..e152dcfab31 100644 --- a/fvm/evm/testutils/contract.go +++ b/fvm/evm/testutils/contract.go @@ -8,9 +8,8 @@ import ( gethABI "github.com/ethereum/go-ethereum/accounts/abi" gethCommon "github.com/ethereum/go-ethereum/common" - "github.com/stretchr/testify/require" - "github.com/onflow/atree" + "github.com/stretchr/testify/require" "github.com/onflow/flow-go/fvm/evm/emulator" "github.com/onflow/flow-go/fvm/evm/testutils/contracts" @@ -54,38 +53,6 @@ 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)); - } - } - `, - 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, types.NewAddress(gethCommon.Address{}), tc, led, flowEVMRootAddress) f(tc) From 09140a789554b34c280cd21fd7ac898835de6304 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Fri, 16 Feb 2024 16:36:46 +0100 Subject: [PATCH 7/9] update test --- fvm/evm/emulator/emulator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fvm/evm/emulator/emulator_test.go b/fvm/evm/emulator/emulator_test.go index 7ba9b79a215..832d4a45c0c 100644 --- a/fvm/evm/emulator/emulator_test.go +++ b/fvm/evm/emulator/emulator_test.go @@ -375,7 +375,7 @@ func TestSelfdestruct(t *testing.T) { testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) { testutils.RunWithEOATestAccount(t, backend, rootAddr, func(testAccount *testutils.EOATestAccount) { - testContract := testutils.GetSelfDestructTestContract(t) + testContract := testutils.GetStorageTestContract(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)) From dbad6e79a05ac7dadd0b69f9a65fd33d91ff5424 Mon Sep 17 00:00:00 2001 From: ramtinms Date: Tue, 20 Feb 2024 15:33:34 -0800 Subject: [PATCH 8/9] fix test --- fvm/evm/emulator/emulator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fvm/evm/emulator/emulator_test.go b/fvm/evm/emulator/emulator_test.go index 9b4d8f2be03..1cc4c7b77dc 100644 --- a/fvm/evm/emulator/emulator_test.go +++ b/fvm/evm/emulator/emulator_test.go @@ -423,7 +423,7 @@ func TestSelfdestruct(t *testing.T) { GasLimit: 100_000, }) require.NoError(t, err) - require.False(t, res.Failed) + require.False(t, res.Failed()) }) // after calling selfdestruct the balance should be returned to the caller and From c3231189c032297de4d71e7633fc2cc5a10198aa Mon Sep 17 00:00:00 2001 From: ramtinms Date: Tue, 20 Feb 2024 15:50:01 -0800 Subject: [PATCH 9/9] update tests --- fvm/evm/emulator/emulator_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fvm/evm/emulator/emulator_test.go b/fvm/evm/emulator/emulator_test.go index 9836452c844..7d04f804c6e 100644 --- a/fvm/evm/emulator/emulator_test.go +++ b/fvm/evm/emulator/emulator_test.go @@ -404,7 +404,7 @@ func TestSelfdestruct(t *testing.T) { // 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)) + _, err := blk.DirectCall(types.NewDepositCall(testAddress, startBalance, 0)) require.NoError(t, err) }) @@ -414,7 +414,8 @@ func TestSelfdestruct(t *testing.T) { testAddress, testContract.ByteCode, math.MaxUint64, - deployBalance), + deployBalance, + 0), ) require.NoError(t, err) contractAddr = res.DeployedContractAddress