2019-07-15 17:56:49 +00:00
|
|
|
package actors_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/filecoin-project/go-lotus/chain/actors"
|
2019-07-25 22:15:03 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/gen"
|
2019-07-26 04:54:22 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/vm"
|
2019-07-15 17:56:49 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
2019-07-25 22:15:03 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/state"
|
2019-07-15 17:56:49 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
|
|
|
|
|
|
|
dstore "github.com/ipfs/go-datastore"
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
|
|
|
bstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Harness struct {
|
|
|
|
Steps []Step
|
|
|
|
From address.Address
|
2019-07-16 11:35:56 +00:00
|
|
|
Third address.Address
|
2019-07-15 17:56:49 +00:00
|
|
|
|
|
|
|
currStep int
|
|
|
|
|
|
|
|
t *testing.T
|
|
|
|
actors []address.Address
|
2019-07-26 04:54:22 +00:00
|
|
|
vm *vm.VM
|
2019-07-15 17:56:49 +00:00
|
|
|
bs bstore.Blockstore
|
2019-07-26 04:54:22 +00:00
|
|
|
cs *store.ChainStore
|
2019-07-15 17:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Step struct {
|
|
|
|
M types.Message
|
|
|
|
Ret func(*testing.T, *types.MessageReceipt)
|
|
|
|
Err func(*testing.T, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHarness(t *testing.T) *Harness {
|
|
|
|
h := &Harness{t: t}
|
|
|
|
h.bs = bstore.NewBlockstore(dstore.NewMapDatastore())
|
|
|
|
|
|
|
|
from := blsaddr(0)
|
|
|
|
maddr := blsaddr(1)
|
2019-07-16 11:35:56 +00:00
|
|
|
third := blsaddr(1)
|
2019-07-15 17:56:49 +00:00
|
|
|
|
|
|
|
actors := map[address.Address]types.BigInt{
|
|
|
|
from: types.NewInt(1000000),
|
|
|
|
maddr: types.NewInt(0),
|
2019-07-16 11:35:56 +00:00
|
|
|
third: types.NewInt(1000),
|
2019-07-15 17:56:49 +00:00
|
|
|
}
|
2019-07-25 22:15:03 +00:00
|
|
|
st, err := gen.MakeInitialStateTree(h.bs, actors)
|
2019-07-15 17:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stateroot, err := st.Flush()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
h.cs = store.NewChainStore(h.bs, nil)
|
2019-07-15 17:56:49 +00:00
|
|
|
|
2019-07-26 21:42:38 +00:00
|
|
|
h.vm, err = vm.NewVM(stateroot, IAMethods.Exec, maddr, h.cs)
|
2019-07-15 17:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-07-16 11:35:56 +00:00
|
|
|
h.actors = []address.Address{from, maddr, third}
|
2019-07-15 17:56:49 +00:00
|
|
|
h.From = from
|
2019-07-16 11:35:56 +00:00
|
|
|
h.Third = third
|
2019-07-15 17:56:49 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (h *Harness) Execute() *state.StateTree {
|
2019-07-15 17:56:49 +00:00
|
|
|
for i, step := range h.Steps {
|
|
|
|
h.currStep = i
|
2019-07-26 19:07:19 +00:00
|
|
|
ret, err := h.vm.ApplyMessage(context.TODO(), &step.M)
|
2019-07-16 11:35:56 +00:00
|
|
|
if step.Err != nil {
|
|
|
|
step.Err(h.t, err)
|
|
|
|
} else {
|
|
|
|
h.NoError(h.t, err)
|
|
|
|
}
|
2019-07-15 20:27:45 +00:00
|
|
|
step.Ret(h.t, ret)
|
2019-07-15 17:56:49 +00:00
|
|
|
}
|
|
|
|
stateroot, err := h.vm.Flush(context.TODO())
|
|
|
|
if err != nil {
|
2019-07-15 20:27:45 +00:00
|
|
|
h.t.Fatalf("%+v", err)
|
2019-07-15 17:56:49 +00:00
|
|
|
}
|
|
|
|
cst := hamt.CSTFromBstore(h.bs)
|
2019-07-25 22:15:03 +00:00
|
|
|
state, err := state.LoadStateTree(cst, stateroot)
|
2019-07-15 17:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
h.t.Fatal(err)
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Harness) DumpObject(obj interface{}) []byte {
|
|
|
|
enc, err := cbor.DumpObject(obj)
|
|
|
|
if err != nil {
|
|
|
|
h.t.Fatal(err)
|
|
|
|
}
|
|
|
|
return enc
|
|
|
|
}
|
|
|
|
func (h *Harness) NoError(t *testing.T, err error) {
|
|
|
|
if err != nil {
|
2019-07-17 14:30:55 +00:00
|
|
|
t.Fatalf("Error in step %d: %+v", h.currStep, err)
|
2019-07-15 17:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVMInvokeHarness(t *testing.T) {
|
|
|
|
h := NewHarness(t)
|
|
|
|
var outaddr address.Address
|
|
|
|
h.Steps = []Step{
|
|
|
|
{
|
|
|
|
M: types.Message{
|
|
|
|
To: InitActorAddress,
|
|
|
|
From: h.From,
|
2019-07-26 21:42:38 +00:00
|
|
|
Method: IAMethods.Exec,
|
2019-07-15 17:56:49 +00:00
|
|
|
Params: h.DumpObject(
|
|
|
|
&ExecParams{
|
2019-07-16 16:40:25 +00:00
|
|
|
Code: StorageMinerCodeCid,
|
|
|
|
Params: h.DumpObject(&StorageMinerConstructorParams{
|
|
|
|
Owner: h.From,
|
|
|
|
}),
|
2019-07-15 17:56:49 +00:00
|
|
|
}),
|
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
GasLimit: types.NewInt(1),
|
|
|
|
Value: types.NewInt(0),
|
|
|
|
},
|
|
|
|
Ret: func(t *testing.T, ret *types.MessageReceipt) {
|
|
|
|
if ret.ExitCode != 0 {
|
|
|
|
t.Fatal("invocation failed: ", ret.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
outaddr, err = address.NewFromBytes(ret.Return)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if outaddr.String() != "t0102" {
|
|
|
|
t.Fatal("hold up")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err: h.NoError,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state := h.Execute()
|
2019-07-15 22:30:54 +00:00
|
|
|
act, err := state.GetActor(outaddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if act.Code != StorageMinerCodeCid {
|
|
|
|
t.Fatalf("Expected correct code, got %s, instead of %s", act.Code, StorageMinerCodeCid)
|
|
|
|
}
|
2019-07-15 22:42:37 +00:00
|
|
|
hblock, err := h.bs.Get(act.Head)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
smas := &StorageMinerActorState{}
|
2019-07-16 16:49:48 +00:00
|
|
|
err = cbor.DecodeInto(hblock.RawData(), smas)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-07-15 22:42:37 +00:00
|
|
|
if smas.Owner != h.From {
|
|
|
|
t.Fatalf("Owner should be %s, but is %s", h.From, smas.Owner)
|
|
|
|
}
|
2019-07-15 17:56:49 +00:00
|
|
|
}
|