From 2981c952077ca94e9faf015735e98bfd20a448a0 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Fri, 12 Jul 2019 23:06:22 +0200 Subject: [PATCH] Fix invoker when UnmarshalCBOR errors Underlying issue: https://git.io/fjXU6 License: MIT Signed-off-by: Jakub Sztandera --- chain/invoker.go | 4 +++- chain/invoker_test.go | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/chain/invoker.go b/chain/invoker.go index 3128d0c0c..cf0ff87b2 100644 --- a/chain/invoker.go +++ b/chain/invoker.go @@ -124,7 +124,9 @@ func (*invoker) transform(instance Invokee) (nativeCode, error) { if err != nil { return []reflect.Value{ reflect.ValueOf(InvokeRet{}), - reflect.ValueOf(err), + // Below is a hack, fixed in Go 1.13 + // https://git.io/fjXU6 + reflect.ValueOf(&err).Elem(), } } return meth.Call([]reflect.Value{ diff --git a/chain/invoker_test.go b/chain/invoker_test.go index 571716c54..4d5a56b14 100644 --- a/chain/invoker_test.go +++ b/chain/invoker_test.go @@ -1,6 +1,7 @@ package chain import ( + "errors" "testing" "github.com/stretchr/testify/assert" @@ -18,10 +19,17 @@ func (b *basicParams) UnmarshalCBOR(in []byte) (int, error) { return 1, nil } +type badParam struct { +} + +func (b *badParam) UnmarshalCBOR(in []byte) (int, error) { + return -1, errors.New("some error") +} + func (b basicContract) Exports() []interface{} { return []interface{}{ b.InvokeSomething0, - nil, + b.BadParam, nil, nil, nil, @@ -40,6 +48,11 @@ func (basicContract) InvokeSomething0(act *types.Actor, vmctx types.VMContext, returnCode: params.b, }, nil } +func (basicContract) BadParam(act *types.Actor, vmctx types.VMContext, + params *badParam) (InvokeRet, error) { + panic("should not execute") +} + func (basicContract) InvokeSomething10(act *types.Actor, vmctx types.VMContext, params *basicParams) (InvokeRet, error) { return InvokeRet{ @@ -58,4 +71,7 @@ func TestInvokerBasic(t *testing.T) { ret, err = code[10](nil, &VMContext{}, []byte{2}) assert.NoError(t, err) assert.Equal(t, byte(12), ret.returnCode, "return code should be 1") + + ret, err = code[1](nil, &VMContext{}, []byte{2}) + assert.Error(t, err) }