2019-07-26 04:54:22 +00:00
|
|
|
package vm
|
2019-07-11 15:38:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-07-15 14:44:30 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2019-07-11 15:38:37 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-12 10:43:15 +00:00
|
|
|
|
2019-07-22 18:17:42 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors/aerrors"
|
2019-07-12 10:43:15 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-11 15:38:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type basicContract struct{}
|
|
|
|
type basicParams struct {
|
2019-07-15 14:44:30 +00:00
|
|
|
B byte
|
2019-07-11 15:38:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 14:44:30 +00:00
|
|
|
func init() {
|
|
|
|
cbor.RegisterCborType(basicParams{})
|
2019-07-12 21:06:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 16:15:44 +00:00
|
|
|
func (b basicContract) Exports() []interface{} {
|
|
|
|
return []interface{}{
|
|
|
|
b.InvokeSomething0,
|
2019-07-12 21:06:22 +00:00
|
|
|
b.BadParam,
|
2019-07-11 16:15:44 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
b.InvokeSomething10,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:43:15 +00:00
|
|
|
func (basicContract) InvokeSomething0(act *types.Actor, vmctx types.VMContext,
|
2019-07-22 18:17:42 +00:00
|
|
|
params *basicParams) ([]byte, aerrors.ActorError) {
|
|
|
|
return nil, aerrors.New(params.B, "params.B")
|
2019-07-11 15:38:37 +00:00
|
|
|
}
|
2019-07-12 21:06:22 +00:00
|
|
|
func (basicContract) BadParam(act *types.Actor, vmctx types.VMContext,
|
2019-07-22 18:17:42 +00:00
|
|
|
params *basicParams) ([]byte, aerrors.ActorError) {
|
|
|
|
return nil, aerrors.New(255, "bad params")
|
2019-07-12 21:06:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:43:15 +00:00
|
|
|
func (basicContract) InvokeSomething10(act *types.Actor, vmctx types.VMContext,
|
2019-07-22 18:17:42 +00:00
|
|
|
params *basicParams) ([]byte, aerrors.ActorError) {
|
|
|
|
return nil, aerrors.New(params.B+10, "params.B")
|
2019-07-11 15:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvokerBasic(t *testing.T) {
|
|
|
|
inv := invoker{}
|
|
|
|
code, err := inv.transform(basicContract{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-15 14:44:30 +00:00
|
|
|
{
|
|
|
|
bParam, err := cbor.DumpObject(basicParams{B: 1})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-07-22 18:17:42 +00:00
|
|
|
_, aerr := code[0](nil, &VMContext{}, bParam)
|
|
|
|
|
|
|
|
assert.Equal(t, byte(1), aerrors.RetCode(aerr), "return code should be 1")
|
|
|
|
if aerrors.IsFatal(aerr) {
|
|
|
|
t.Fatal("err should not be fatal")
|
|
|
|
}
|
2019-07-15 14:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
bParam, err := cbor.DumpObject(basicParams{B: 2})
|
|
|
|
assert.NoError(t, err)
|
2019-07-12 21:06:22 +00:00
|
|
|
|
2019-07-22 18:17:42 +00:00
|
|
|
_, aerr := code[10](nil, &VMContext{}, bParam)
|
|
|
|
assert.Equal(t, byte(12), aerrors.RetCode(aerr), "return code should be 12")
|
|
|
|
if aerrors.IsFatal(aerr) {
|
|
|
|
t.Fatal("err should not be fatal")
|
|
|
|
}
|
2019-07-15 14:44:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 18:17:42 +00:00
|
|
|
_, aerr := code[1](nil, &VMContext{}, []byte{0})
|
|
|
|
if aerrors.IsFatal(aerr) {
|
|
|
|
t.Fatal("err should not be fatal")
|
|
|
|
}
|
|
|
|
assert.Equal(t, byte(1), aerrors.RetCode(aerr), "return code should be 1")
|
2019-07-15 14:44:30 +00:00
|
|
|
|
2019-07-11 15:38:37 +00:00
|
|
|
}
|