2022-08-29 14:25:30 +00:00
|
|
|
// stm: #unit
|
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"
|
|
|
|
|
2022-10-19 17:11:44 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
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
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-10-08 01:09:33 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2022-10-19 17:11:44 +00:00
|
|
|
cbor2 "github.com/filecoin-project/go-state-types/cbor"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/exitcode"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2022-10-19 17:11:44 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/rt"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
2022-10-19 17:11:44 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
2023-11-13 23:59:34 +00:00
|
|
|
runtime2 "github.com/filecoin-project/specs-actors/v2/actors/runtime"
|
2019-07-11 15:38:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type basicContract struct{}
|
2022-10-19 17:11:44 +00:00
|
|
|
|
|
|
|
func (b basicContract) Code() cid.Cid {
|
|
|
|
return cid.Undef
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b basicContract) State() cbor2.Er {
|
|
|
|
// works well enough as a dummy state
|
|
|
|
return new(basicParams)
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:38:37 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-19 17:11:44 +00:00
|
|
|
func (b basicContract) Exports() []interface{} {
|
|
|
|
return []interface{}{
|
|
|
|
b.InvokeSomething0,
|
|
|
|
b.BadParam,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
b.InvokeSomething10,
|
2019-07-11 16:15:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
func (basicContract) InvokeSomething0(rt runtime2.Runtime, params *basicParams) *abi.EmptyValue {
|
2020-03-03 03:12:56 +00:00
|
|
|
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-10-08 01:09:33 +00:00
|
|
|
func (basicContract) BadParam(rt runtime2.Runtime, params *basicParams) *abi.EmptyValue {
|
2020-03-03 03:12:56 +00:00
|
|
|
rt.Abortf(255, "bad params")
|
|
|
|
return nil
|
2019-07-12 21:06:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
func (basicContract) InvokeSomething10(rt runtime2.Runtime, params *basicParams) *abi.EmptyValue {
|
2020-03-03 03:12:56 +00:00
|
|
|
rt.Abortf(exitcode.ExitCode(params.B+10), "params.B")
|
|
|
|
return nil
|
2019-07-11 15:38:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 19:19:32 +00:00
|
|
|
type basicRtMessage struct{}
|
|
|
|
|
|
|
|
var _ runtime2.Message = (*basicRtMessage)(nil)
|
|
|
|
|
|
|
|
func (*basicRtMessage) Caller() address.Address {
|
|
|
|
a, err := address.NewIDAddress(0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*basicRtMessage) Receiver() address.Address {
|
|
|
|
a, err := address.NewIDAddress(1)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*basicRtMessage) ValueReceived() abi.TokenAmount {
|
|
|
|
return big.NewInt(0)
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:38:37 +00:00
|
|
|
func TestInvokerBasic(t *testing.T) {
|
feat: Add additional test annotations (#8272)
* Annotate api,proxy_util,blockstore_badger, policy tests
* Annotate splitstore: bsbadger / markset
* Annotate splitstore feature
* Annotate union/timed blockstore tests
* Annotate openrpc, diff_adt tests
* Annotate error,drand,events tests
* Annotate predicates_test
* Fix annotations
* Annotate tscache, gen tests
* Annotate fundmanager test
* Annotate repub and selection tests
* Annotate statetree_test
* Annotate forks_test
* Annotate searchwait_test.go
* Fix duplicated @@ symbols
* Annotate chain stmgr/store tests
* Annotate more (types) tests
* More tests annotated
* Annotate conformance chaos actor tests
* Annotate more integration tests
* Annotate journal system tests
* Annotate more tests.
* Annotate gas,head buffer behaviors
* Fix markset annotations
* doc: test annotations for the markets dagstore wrapper
* Annotate miner_api test in dagstore
* Annotate more test files
* Remove bad annotations from fsrepo
* Annotate wdpost system
* Remove bad annotations
* Renamce "conformance" to "chaos_actor" tests
* doc: stm annotations for blockheader & election proof tests
* Annotate remaining "A" tests
* annotate: stm for error_test
* memrepo_test.go
* Annotate "b" file tests
* message_test.go
* doc: stm annotate for fsrepo_test
* Annotate "c" file tests
* Annotate "D" test files
* message_test.go
* doc: stm annotate for chain, node/config & client
* docs: stm annotate node_test
* Annotate u,v,wl tests
* doc: stm annotations for various test files
* Annotate "T" test files
* doc: stm annotate for proxy_util_test & policy_test
* doc: stm annotate for various tests
* doc: final few stm annotations
* Add mempool unit tests
* Add two more memPool Add tests
* Update submodules
* Add check function tests
* Add stm annotations, refactor test helper
* Annotate api,proxy_util,blockstore_badger, policy tests
* Annotate splitstore: bsbadger / markset
solving merge conflicts
* Annotate splitstore feature
* Annotate union/timed blockstore tests
* Annotate openrpc, diff_adt tests
* Annotate error,drand,events tests
* Annotate predicates_test
* Fix annotations
* Annotate tscache, gen tests
* Annotate fundmanager test
* Annotate statetree_test
* Annotate forks_test
* Annotate searchwait_test.go
* Fix duplicated @@ symbols
* Annotate chain stmgr/store tests
* Annotate more (types) tests
* More tests annotated
* Annotate conformance chaos actor tests
* Annotate more integration tests
* Annotate journal system tests
* Annotate more tests.
* Annotate gas,head buffer behaviors
solve merge conflict
* Fix markset annotations
* Annotate miner_api test in dagstore
* Annotate more test files
* doc: test annotations for the markets dagstore wrapper
* Annotate wdpost system
* Renamce "conformance" to "chaos_actor" tests
* Annotate remaining "A" tests
* doc: stm annotations for blockheader & election proof tests
* annotate: stm for error_test
* Annotate "b" file tests
* memrepo_test.go
* Annotate "c" file tests
* message_test.go
* Annotate "D" test files
* doc: stm annotate for fsrepo_test
* Annotate u,v,wl tests
* message_test.go
* doc: stm annotate for chain, node/config & client
* docs: stm annotate node_test
* Annotate "T" test files
* doc: stm annotations for various test files
* Add mempool unit tests
solve merge conflict
* doc: stm annotate for proxy_util_test & policy_test
* doc: stm annotate for various tests
* doc: final few stm annotations
* Add two more memPool Add tests
* Update submodules
* Add check function tests
solve conflict
* Add stm annotations, refactor test helper
solve merge conflict
* Change CLI test kinds to "unit"
* Fix double merged test
* Fix ccupgrade_test merge
* Fix lint issues
* Add stm annotation to types_Test
* Test vectors submodule
* Add file annotation to burn_test
Co-authored-by: Nikola Divic <divicnikola@gmail.com>
Co-authored-by: TheMenko <themenkoprojects@gmail.com>
2022-03-16 17:37:34 +00:00
|
|
|
//stm: @INVOKER_TRANSFORM_001
|
2020-09-25 00:51:34 +00:00
|
|
|
inv := ActorRegistry{}
|
2022-10-19 17:11:44 +00:00
|
|
|
registry := builtin.MakeRegistryLegacy([]rt.VMActor{basicContract{}})
|
|
|
|
code, err := inv.transform(registry[0])
|
2019-07-11 15:38:37 +00:00
|
|
|
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)
|
|
|
|
|
2021-07-28 19:19:32 +00:00
|
|
|
_, aerr := code[0](&Runtime{Message: &basicRtMessage{}}, 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
|
|
|
|
2021-07-28 19:19:32 +00:00
|
|
|
_, aerr := code[10](&Runtime{Message: &basicRtMessage{}}, 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-11-09 01:48:05 +00:00
|
|
|
{
|
|
|
|
_, aerr := code[1](&Runtime{
|
2022-03-15 23:40:17 +00:00
|
|
|
vm: &LegacyVM{networkVersion: network.Version0},
|
2021-07-28 19:19:32 +00:00
|
|
|
Message: &basicRtMessage{},
|
2020-11-09 01:48:05 +00:00
|
|
|
}, []byte{99})
|
|
|
|
if aerrors.IsFatal(aerr) {
|
|
|
|
t.Fatal("err should not be fatal")
|
|
|
|
}
|
|
|
|
assert.Equal(t, exitcode.ExitCode(1), aerrors.RetCode(aerr), "return code should be 1")
|
2019-07-22 18:17:42 +00:00
|
|
|
}
|
2019-07-15 14:44:30 +00:00
|
|
|
|
2020-11-09 01:48:05 +00:00
|
|
|
{
|
|
|
|
_, aerr := code[1](&Runtime{
|
2022-03-15 23:40:17 +00:00
|
|
|
vm: &LegacyVM{networkVersion: network.Version7},
|
2021-07-28 19:19:32 +00:00
|
|
|
Message: &basicRtMessage{},
|
2020-11-09 01:48:05 +00:00
|
|
|
}, []byte{99})
|
|
|
|
if aerrors.IsFatal(aerr) {
|
|
|
|
t.Fatal("err should not be fatal")
|
|
|
|
}
|
|
|
|
assert.Equal(t, exitcode.ErrSerialization, aerrors.RetCode(aerr), "return code should be %s", 1)
|
|
|
|
}
|
2019-07-11 15:38:37 +00:00
|
|
|
}
|