From e301a72413ea3b0e4ca9e9ee479ec57b52ab1217 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Wed, 27 Mar 2024 08:30:03 +0000 Subject: [PATCH] Return error if the CCR was done to an empty contract (#227) --- internal/ethapi/api.go | 10 ++++++++++ suave/e2e/workflow_test.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 71c00d9b5..810fff12d 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -2031,6 +2031,16 @@ func runMEVM(ctx context.Context, b Backend, state *state.StateDB, header *types return nil, result, nil, nil } + if result.ReturnData == nil { + // If the CCR returns empty data, it might be because it was done with a non-existent + // contract. Validate if that is true (code == 0) and return error. + // Note that we cannot do this check before "ApplyMessage" because we might be + // discarding the execution of precompiles which do not have code. + if code := state.GetCode(*msg.To); len(code) == 0 { + return nil, nil, nil, fmt.Errorf("target contract does not exist") + } + } + if storageAccessTracer.hasStoredState { return nil, nil, nil, fmt.Errorf("confidential request cannot modify state storage") } diff --git a/suave/e2e/workflow_test.go b/suave/e2e/workflow_test.go index 67e289bbe..9d4f05283 100644 --- a/suave/e2e/workflow_test.go +++ b/suave/e2e/workflow_test.go @@ -1366,6 +1366,21 @@ func TestE2E_Precompile_RandomBytes(t *testing.T) { require.Len(t, res[0], 64) } +func TestE2E_EmptyAddress(t *testing.T) { + // it should not be possible to make a CCR to an empty address + fr := newFramework(t) + defer fr.Close() + + clt := fr.NewSDKClient() + + // 0xa contract does not exist + contractAddr := common.Address{0xa} + sourceContract := sdk.GetContract(contractAddr, exampleCallSourceContract.Abi, clt) + + _, err := sourceContract.SendTransaction("consoleLog", []interface{}{}, nil) + require.Error(t, err) +} + type clientWrapper struct { t *testing.T