Skip to content

Commit

Permalink
Merge pull request #275 from orbs-network/issue/contract-errors
Browse files Browse the repository at this point in the history
Issue/contract errors
  • Loading branch information
talkol authored Sep 27, 2018
2 parents ec31c04 + 10f3f4f commit 66fb14e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 23 deletions.
18 changes: 17 additions & 1 deletion services/processor/native/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func (s *service) verifyInternalMethodCall(contractInfo *sdk.ContractInfo, metho
func (s *service) processMethodCall(executionContextId sdk.Context, contractInfo *sdk.ContractInfo, methodInfo *sdk.MethodInfo, args *protocol.MethodArgumentArray) (contractOutputArgs *protocol.MethodArgumentArray, contractOutputErr error, err error) {
defer func() {
if r := recover(); r != nil {
err = errors.Errorf("call method '%s' panicked: %v", methodInfo.Name, r)
contractOutputErr = errors.Errorf("%s", r)
s.reporting.Info("contract execution failed by contract panic", log.Error(contractOutputErr))
contractOutputArgs = s.createMethodOutputArgsWithString(contractOutputErr.Error())
}
}()

Expand Down Expand Up @@ -76,6 +78,12 @@ func (s *service) processMethodCall(executionContextId sdk.Context, contractInfo

// create contract output error
contractOutputErr, err = s.createContractOutputError(methodInfo, outValues[len(outValues)-1])
if contractOutputErr != nil {
s.reporting.Info("contract execution failed by contract error", log.Error(contractOutputErr))
contractOutputArgs = s.createMethodOutputArgsWithString(contractOutputErr.Error())
}

// done
return contractOutputArgs, contractOutputErr, err
}

Expand Down Expand Up @@ -173,3 +181,11 @@ func (s *service) createContractOutputError(methodInfo *sdk.MethodInfo, value re
}
return nil, nil
}

func (s *service) createMethodOutputArgsWithString(str string) *protocol.MethodArgumentArray {
return (&protocol.MethodArgumentArrayBuilder{
Arguments: []*protocol.MethodArgumentBuilder{
{Name: "string", Type: protocol.METHOD_ARGUMENT_TYPE_STRING_VALUE, StringValue: str},
},
}).Build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var METHOD_THROW = sdk.MethodInfo{
}

func (c *contract) throw(ctx sdk.Context) error {
return errors.New("contract returns error")
return errors.New("example error returned by contract")
}

///////////////////////////////////////////////////////////////////////////
Expand All @@ -130,7 +130,7 @@ var METHOD_PANIC = sdk.MethodInfo{
}

func (c *contract) panic(ctx sdk.Context) error {
panic("contract panicked")
panic("example panic thrown by contract")
}

///////////////////////////////////////////////////////////////////////////
Expand Down
15 changes: 9 additions & 6 deletions services/processor/native/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package native
import (
"github.com/orbs-network/orbs-contract-sdk/go/sdk"
"github.com/orbs-network/orbs-network-go/instrumentation/log"
"github.com/orbs-network/orbs-network-go/services/processor/native/adapter"
"github.com/orbs-network/orbs-spec/types/go/protocol"
"github.com/orbs-network/orbs-spec/types/go/services"
"github.com/orbs-network/orbs-spec/types/go/services/handlers"
"sync"
"github.com/orbs-network/orbs-network-go/services/processor/native/adapter"
)

type service struct {
reporting log.BasicLogger
compiler adapter.Compiler
compiler adapter.Compiler

mutex *sync.RWMutex
contractSdkHandlerUnderMutex handlers.ContractSdkCallHandler
Expand All @@ -25,7 +25,7 @@ func NewNativeProcessor(
reporting log.BasicLogger,
) services.Processor {
return &service{
compiler: compiler,
compiler: compiler,
reporting: reporting.For(log.Service("processor-native")),
mutex: &sync.RWMutex{},
}
Expand All @@ -50,7 +50,8 @@ func (s *service) ProcessCall(input *services.ProcessCallInput) (*services.Proce
contractInfo, methodInfo, err := s.retrieveContractAndMethodInfoFromRepository(executionContextId, string(input.ContractName), string(input.MethodName))
if err != nil {
return &services.ProcessCallOutput{
OutputArgumentArray: (&protocol.MethodArgumentArrayBuilder{}).Build(),
// TODO: do we need to remove system errors from OutputArguments? https://github.com/orbs-network/orbs-spec/issues/97
OutputArgumentArray: s.createMethodOutputArgsWithString(err.Error()),
CallResult: protocol.EXECUTION_RESULT_ERROR_UNEXPECTED,
}, err
}
Expand All @@ -59,7 +60,8 @@ func (s *service) ProcessCall(input *services.ProcessCallInput) (*services.Proce
err = s.verifyMethodPermissions(contractInfo, methodInfo, input.CallingService, input.CallingPermissionScope, input.AccessScope)
if err != nil {
return &services.ProcessCallOutput{
OutputArgumentArray: (&protocol.MethodArgumentArrayBuilder{}).Build(),
// TODO: do we need to remove system errors from OutputArguments? https://github.com/orbs-network/orbs-spec/issues/97
OutputArgumentArray: s.createMethodOutputArgsWithString(err.Error()),
CallResult: protocol.EXECUTION_RESULT_ERROR_UNEXPECTED,
}, err
}
Expand All @@ -71,7 +73,8 @@ func (s *service) ProcessCall(input *services.ProcessCallInput) (*services.Proce
}
if err != nil {
return &services.ProcessCallOutput{
OutputArgumentArray: outputArgs,
// TODO: do we need to remove system errors from OutputArguments? https://github.com/orbs-network/orbs-spec/issues/97
OutputArgumentArray: s.createMethodOutputArgsWithString(err.Error()),
CallResult: protocol.EXECUTION_RESULT_ERROR_UNEXPECTED,
}, err
}
Expand Down
15 changes: 2 additions & 13 deletions services/processor/native/test/call_argument_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

func TestProcessCallArguments(t *testing.T) {
func TestProcessCall_Arguments(t *testing.T) {
tests := []struct {
name string
input *services.ProcessCallInput
Expand Down Expand Up @@ -60,18 +60,6 @@ func TestProcessCallArguments(t *testing.T) {
expectedError: true,
expectedResult: protocol.EXECUTION_RESULT_ERROR_UNEXPECTED,
},
{
name: "ThatThrowsError",
input: processCallInput().WithMethod("BenchmarkContract", "throw").Build(),
expectedError: true,
expectedResult: protocol.EXECUTION_RESULT_ERROR_SMART_CONTRACT,
},
{
name: "ThatPanics",
input: processCallInput().WithMethod("BenchmarkContract", "panic").Build(),
expectedError: true,
expectedResult: protocol.EXECUTION_RESULT_ERROR_UNEXPECTED,
},
{
name: "WithInvalidMethodMissingErrorFails",
input: processCallInput().WithMethod("BenchmarkContract", "invalidNoError").Build(),
Expand All @@ -92,6 +80,7 @@ func TestProcessCallArguments(t *testing.T) {
output, err := h.service.ProcessCall(test.input)
if test.expectedError {
require.Error(t, err, "call should fail")
require.NotEmpty(t, output.OutputArgumentArray.ArgumentsIterator().NextArguments().StringValue(), "call return args should contain an error string")
} else {
require.NoError(t, err, "call should succeed")
require.Equal(t, test.expectedOutput, output.OutputArgumentArray, "call return args should be equal")
Expand Down
49 changes: 49 additions & 0 deletions services/processor/native/test/call_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package test

import (
"github.com/orbs-network/orbs-network-go/test/builders"
"github.com/orbs-network/orbs-spec/types/go/protocol"
"github.com/orbs-network/orbs-spec/types/go/services"
"github.com/stretchr/testify/require"
"testing"
)

func TestProcessCall_Errors(t *testing.T) {
tests := []struct {
name string
input *services.ProcessCallInput
expectedError bool
expectedResult protocol.ExecutionResult
expectedOutput *protocol.MethodArgumentArray
}{
{
name: "ThatThrowsError",
input: processCallInput().WithMethod("BenchmarkContract", "throw").Build(),
expectedError: true,
expectedResult: protocol.EXECUTION_RESULT_ERROR_SMART_CONTRACT,
expectedOutput: builders.MethodArgumentsArray("example error returned by contract"),
},
{
name: "ThatPanics",
input: processCallInput().WithMethod("BenchmarkContract", "panic").Build(),
expectedError: true,
expectedResult: protocol.EXECUTION_RESULT_ERROR_SMART_CONTRACT,
expectedOutput: builders.MethodArgumentsArray("example panic thrown by contract"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
h := newHarness()

output, err := h.service.ProcessCall(test.input)
if test.expectedError {
require.Error(t, err, "call should fail")
require.Equal(t, test.expectedOutput, output.OutputArgumentArray, "call return args should be equal")
} else {
require.NoError(t, err, "call should succeed")
require.Equal(t, test.expectedOutput, output.OutputArgumentArray, "call return args should be equal")
}
require.Equal(t, test.expectedResult, output.CallResult, "call result should be equal")
})
}
}
2 changes: 1 addition & 1 deletion services/processor/native/test/call_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func TestProcessCallPermissions(t *testing.T) {
func TestProcessCall_Permissions(t *testing.T) {
tests := []struct {
name string
input *services.ProcessCallInput
Expand Down

0 comments on commit 66fb14e

Please sign in to comment.