lotus/chain/invoker_test.go
Jakub Sztandera e720f5d3a6 Extract most of the rest and make VMContext an interface
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
2019-07-12 12:43:55 +02:00

62 lines
1.2 KiB
Go

package chain
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/filecoin-project/go-lotus/chain/types"
)
type basicContract struct{}
type basicParams struct {
b byte
}
func (b *basicParams) UnmarshalCBOR(in []byte) (int, error) {
b.b = in[0]
return 1, nil
}
func (b basicContract) Exports() []interface{} {
return []interface{}{
b.InvokeSomething0,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
b.InvokeSomething10,
}
}
func (basicContract) InvokeSomething0(act *types.Actor, vmctx types.VMContext,
params *basicParams) (InvokeRet, error) {
return InvokeRet{
returnCode: params.b,
}, nil
}
func (basicContract) InvokeSomething10(act *types.Actor, vmctx types.VMContext,
params *basicParams) (InvokeRet, error) {
return InvokeRet{
returnCode: params.b + 10,
}, nil
}
func TestInvokerBasic(t *testing.T) {
inv := invoker{}
code, err := inv.transform(basicContract{})
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)
assert.Equal(t, byte(12), ret.returnCode, "return code should be 1")
}