2019-07-26 04:54:22 +00:00
|
|
|
package vm
|
2019-07-11 15:38:37 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-10 23:03:17 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-07-11 15:38:37 +00:00
|
|
|
"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-09-10 23:03:17 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-07-12 10:43:15 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
2020-03-03 03:12:56 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime/exitcode"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
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-09-10 23:03:17 +00:00
|
|
|
func (b *basicParams) MarshalCBOR(w io.Writer) error {
|
|
|
|
_, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(b.B)))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *basicParams) UnmarshalCBOR(r io.Reader) error {
|
|
|
|
maj, val, err := cbg.CborReadHeader(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if maj != cbg.MajUnsignedInt {
|
|
|
|
return fmt.Errorf("bad cbor type")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.B = byte(val)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 03:12:56 +00:00
|
|
|
func (basicContract) InvokeSomething0(rt runtime.Runtime, params *basicParams) *adt.EmptyValue {
|
|
|
|
rt.Abortf(exitcode.ExitCode(params.B), "params.B")
|
|
|
|
return nil
|
2019-07-11 15:38:37 +00:00
|
|
|
}
|
2019-09-10 23:03:17 +00:00
|
|
|
|
2020-03-03 03:12:56 +00:00
|
|
|
func (basicContract) BadParam(rt runtime.Runtime, params *basicParams) *adt.EmptyValue {
|
|
|
|
rt.Abortf(255, "bad params")
|
|
|
|
return nil
|
2019-07-12 21:06:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 03:12:56 +00:00
|
|
|
func (basicContract) InvokeSomething10(rt runtime.Runtime, params *basicParams) *adt.EmptyValue {
|
|
|
|
rt.Abortf(exitcode.ExitCode(params.B+10), "params.B")
|
|
|
|
return nil
|
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
|
|
|
{
|
2019-09-10 23:03:17 +00:00
|
|
|
bParam, err := actors.SerializeParams(&basicParams{B: 1})
|
2019-07-15 14:44:30 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
_, aerr := code[0](&Runtime{}, bParam)
|
2019-07-22 18:17:42 +00:00
|
|
|
|
2020-04-02 18:24:38 +00:00
|
|
|
assert.Equal(t, exitcode.ExitCode(1), aerrors.RetCode(aerr), "return code should be 1")
|
2019-07-22 18:17:42 +00:00
|
|
|
if aerrors.IsFatal(aerr) {
|
|
|
|
t.Fatal("err should not be fatal")
|
|
|
|
}
|
2019-07-15 14:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-09-10 23:03:17 +00:00
|
|
|
bParam, err := actors.SerializeParams(&basicParams{B: 2})
|
2019-07-15 14:44:30 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-12 21:06:22 +00:00
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
_, aerr := code[10](&Runtime{}, bParam)
|
2020-04-02 18:24:38 +00:00
|
|
|
assert.Equal(t, exitcode.ExitCode(12), aerrors.RetCode(aerr), "return code should be 12")
|
2019-07-22 18:17:42 +00:00
|
|
|
if aerrors.IsFatal(aerr) {
|
|
|
|
t.Fatal("err should not be fatal")
|
|
|
|
}
|
2019-07-15 14:44:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
_, aerr := code[1](&Runtime{}, []byte{99})
|
2019-07-22 18:17:42 +00:00
|
|
|
if aerrors.IsFatal(aerr) {
|
|
|
|
t.Fatal("err should not be fatal")
|
|
|
|
}
|
2020-04-02 18:24:38 +00:00
|
|
|
assert.Equal(t, exitcode.ExitCode(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
|
|
|
}
|