Skip to content

Commit

Permalink
Return error if the CCR was done to an empty contract (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt authored Mar 27, 2024
1 parent a578807 commit e301a72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
15 changes: 15 additions & 0 deletions suave/e2e/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e301a72

Please sign in to comment.