From 219fc093b11092f1a75d6dfb372a1fb810d0bf50 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 11 Jul 2019 17:47:05 +0200 Subject: [PATCH] Fix lint License: MIT Signed-off-by: Jakub Sztandera --- chain/invoker.go | 16 ++++++++++++---- chain/invoker_test.go | 8 ++++---- chain/vm.go | 1 + 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/chain/invoker.go b/chain/invoker.go index c2f90e81b..c5c494bfa 100644 --- a/chain/invoker.go +++ b/chain/invoker.go @@ -57,7 +57,7 @@ type unmarshalCBOR interface { var tUnmarhsalCBOR = reflect.TypeOf((*unmarshalCBOR)(nil)).Elem() var tError = reflect.TypeOf((*error)(nil)).Elem() -func (_ *invoker) transform(instance interface{}) (nativeCode, error) { +func (*invoker) transform(instance interface{}) (nativeCode, error) { itype := reflect.TypeOf(instance) newErr := func(str string) error { return fmt.Errorf("transform(%s): %s", itype.Name(), str) @@ -95,11 +95,11 @@ func (_ *invoker) transform(instance interface{}) (nativeCode, error) { } if !t.In(3).Implements(tUnmarhsalCBOR) { - return nil, newErr("paramter doesn't implement UnmarshalCBOR") + return nil, newErr("parameter doesn't implement UnmarshalCBOR") } if t.In(3).Kind() != reflect.Ptr { - return nil, newErr("paramter has to be a pointer") + return nil, newErr("parameter has to be a pointer") } if t.NumOut() != 2 { @@ -124,12 +124,20 @@ func (_ *invoker) transform(instance interface{}) (nativeCode, error) { code := make(nativeCode, maxn+1) _ = code for id, meth := range invokes { + meth := meth code[id] = reflect.MakeFunc(reflect.TypeOf((invokeFunc)(nil)), func(in []reflect.Value) []reflect.Value { paramT := meth.Type.In(3).Elem() param := reflect.New(paramT) - param.Interface().(unmarshalCBOR).UnmarshalCBOR(in[2].Interface().([]byte)) + inBytes := in[2].Interface().([]byte) + _, err := param.Interface().(unmarshalCBOR).UnmarshalCBOR(inBytes) + if err != nil { + return []reflect.Value{ + reflect.ValueOf(InvokeRet{}), + reflect.ValueOf(err), + } + } return meth.Func.Call([]reflect.Value{ reflect.ValueOf(instance), in[0], in[1], param, }) diff --git a/chain/invoker_test.go b/chain/invoker_test.go index 73c0a0956..a1af168db 100644 --- a/chain/invoker_test.go +++ b/chain/invoker_test.go @@ -16,16 +16,16 @@ func (b *basicParams) UnmarshalCBOR(in []byte) (int, error) { return 1, nil } -func (_ basicContract) InvokeSomething0(act *Actor, vmctx *VMContext, +func (basicContract) InvokeSomething0(act *Actor, vmctx *VMContext, params *basicParams) (InvokeRet, error) { return InvokeRet{ returnCode: params.b, }, nil } -func (_ basicContract) InvokeSomething10(act *Actor, vmctx *VMContext, +func (basicContract) InvokeSomething10(act *Actor, vmctx *VMContext, params *basicParams) (InvokeRet, error) { return InvokeRet{ - returnCode: params.b, + returnCode: params.b + 10, }, nil } @@ -39,5 +39,5 @@ func TestInvokerBasic(t *testing.T) { ret, err = code[10](nil, nil, []byte{1}) assert.NoError(t, err) - assert.Equal(t, byte(1), ret.returnCode, "return code should be 1") + assert.Equal(t, byte(11), ret.returnCode, "return code should be 1") } diff --git a/chain/vm.go b/chain/vm.go index bc26c5b73..422f5821d 100644 --- a/chain/vm.go +++ b/chain/vm.go @@ -86,6 +86,7 @@ func NewVM(base cid.Cid, height uint64, maddr address.Address, cs *ChainStore) ( buf: buf, blockHeight: height, blockMiner: maddr, + inv: newInvoker(), }, nil }