Switch from UnmarshalCBOR interface to calling CBOR

Now invoker is calling CBOR directly.

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera 2019-07-15 16:44:30 +02:00
parent 0ac3545013
commit 2364a73b92
2 changed files with 33 additions and 34 deletions

View File

@ -8,6 +8,7 @@ import (
actors "github.com/filecoin-project/go-lotus/chain/actors" actors "github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/types" "github.com/filecoin-project/go-lotus/chain/types"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
) )
type invoker struct { type invoker struct {
@ -50,15 +51,10 @@ func (inv *invoker) register(c cid.Cid, instance Invokee) {
inv.builtInCode[c] = code inv.builtInCode[c] = code
} }
type unmarshalCBOR interface {
UnmarshalCBOR([]byte) (int, error)
}
type Invokee interface { type Invokee interface {
Exports() []interface{} Exports() []interface{}
} }
var tUnmarhsalCBOR = reflect.TypeOf((*unmarshalCBOR)(nil)).Elem()
var tVMContext = reflect.TypeOf((*types.VMContext)(nil)).Elem() var tVMContext = reflect.TypeOf((*types.VMContext)(nil)).Elem()
var tError = reflect.TypeOf((*error)(nil)).Elem() var tError = reflect.TypeOf((*error)(nil)).Elem()
@ -89,12 +85,8 @@ func (*invoker) transform(instance Invokee) (nativeCode, error) {
return nil, newErr("second argument should be types.VMContext") return nil, newErr("second argument should be types.VMContext")
} }
if !t.In(2).Implements(tUnmarhsalCBOR) {
return nil, newErr("parameter doesn't implement UnmarshalCBOR")
}
if t.In(2).Kind() != reflect.Ptr { if t.In(2).Kind() != reflect.Ptr {
return nil, newErr("parameter has to be a pointer") return nil, newErr("parameter has to be a pointer to parameter")
} }
if t.NumOut() != 2 { if t.NumOut() != 2 {
@ -118,7 +110,7 @@ func (*invoker) transform(instance Invokee) (nativeCode, error) {
param := reflect.New(paramT) param := reflect.New(paramT)
inBytes := in[2].Interface().([]byte) inBytes := in[2].Interface().([]byte)
_, err := param.Interface().(unmarshalCBOR).UnmarshalCBOR(inBytes) err := cbor.DecodeInto(inBytes, param.Interface())
if err != nil { if err != nil {
return []reflect.Value{ return []reflect.Value{
reflect.ValueOf(types.InvokeRet{}), reflect.ValueOf(types.InvokeRet{}),
@ -127,6 +119,7 @@ func (*invoker) transform(instance Invokee) (nativeCode, error) {
reflect.ValueOf(&err).Elem(), reflect.ValueOf(&err).Elem(),
} }
} }
return meth.Call([]reflect.Value{ return meth.Call([]reflect.Value{
in[0], in[1], param, in[0], in[1], param,
}) })

View File

@ -1,9 +1,9 @@
package chain package chain
import ( import (
"errors"
"testing" "testing"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/filecoin-project/go-lotus/chain/types" "github.com/filecoin-project/go-lotus/chain/types"
@ -11,19 +11,11 @@ import (
type basicContract struct{} type basicContract struct{}
type basicParams struct { type basicParams struct {
b byte B byte
} }
func (b *basicParams) UnmarshalCBOR(in []byte) (int, error) { func init() {
b.b = in[0] cbor.RegisterCborType(basicParams{})
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{} { func (b basicContract) Exports() []interface{} {
@ -45,18 +37,20 @@ func (b basicContract) Exports() []interface{} {
func (basicContract) InvokeSomething0(act *types.Actor, vmctx types.VMContext, func (basicContract) InvokeSomething0(act *types.Actor, vmctx types.VMContext,
params *basicParams) (types.InvokeRet, error) { params *basicParams) (types.InvokeRet, error) {
return types.InvokeRet{ return types.InvokeRet{
ReturnCode: params.b, ReturnCode: params.B,
}, nil }, nil
} }
func (basicContract) BadParam(act *types.Actor, vmctx types.VMContext, func (basicContract) BadParam(act *types.Actor, vmctx types.VMContext,
params *badParam) (types.InvokeRet, error) { params *basicParams) (types.InvokeRet, error) {
panic("should not execute") return types.InvokeRet{
ReturnCode: 255,
}, nil
} }
func (basicContract) InvokeSomething10(act *types.Actor, vmctx types.VMContext, func (basicContract) InvokeSomething10(act *types.Actor, vmctx types.VMContext,
params *basicParams) (types.InvokeRet, error) { params *basicParams) (types.InvokeRet, error) {
return types.InvokeRet{ return types.InvokeRet{
ReturnCode: params.b + 10, ReturnCode: params.B + 10,
}, nil }, nil
} }
@ -64,14 +58,26 @@ func TestInvokerBasic(t *testing.T) {
inv := invoker{} inv := invoker{}
code, err := inv.transform(basicContract{}) code, err := inv.transform(basicContract{})
assert.NoError(t, err) assert.NoError(t, err)
ret, err := code[0](nil, nil, []byte{1})
assert.NoError(t, err)
assert.Equal(t, byte(1), ret.ReturnCode, "return code should be 1")
ret, err = code[10](nil, &VMContext{}, []byte{2}) {
assert.NoError(t, err) bParam, err := cbor.DumpObject(basicParams{B: 1})
assert.Equal(t, byte(12), ret.ReturnCode, "return code should be 1") assert.NoError(t, err)
ret, err = code[1](nil, &VMContext{}, []byte{2}) ret, err := code[0](nil, &VMContext{}, bParam)
assert.NoError(t, err)
assert.Equal(t, byte(1), ret.ReturnCode, "return code should be 1")
}
{
bParam, err := cbor.DumpObject(basicParams{B: 2})
assert.NoError(t, err)
ret, err := code[10](nil, &VMContext{}, bParam)
assert.NoError(t, err)
assert.Equal(t, byte(12), ret.ReturnCode, "return code should be 12")
}
_, err = code[1](nil, &VMContext{}, []byte{0})
assert.Error(t, err) assert.Error(t, err)
} }