From 35575263cb68e43481192ed9baba9ee935950243 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 16 Jul 2019 13:35:56 +0200 Subject: [PATCH] Add more tests License: MIT Signed-off-by: Jakub Sztandera --- chain/actors/actor_init.go | 1 - chain/actors/actor_storagemarket_test.go | 65 ++++++++++++++++++++++++ chain/actors/harness_test.go | 15 ++++-- chain/statetree.go | 1 - chain/vm.go | 10 +++- 5 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 chain/actors/actor_storagemarket_test.go diff --git a/chain/actors/actor_init.go b/chain/actors/actor_init.go index c6f086d7f..1cbc4ebb9 100644 --- a/chain/actors/actor_init.go +++ b/chain/actors/actor_init.go @@ -143,7 +143,6 @@ func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams) return types.InvokeRet{}, errors.Wrap(err, "inserting new actor into state tree") } - fmt.Println("PARAMS: ", string(p.Params)) _, _, err = vmctx.Send(idAddr, 0, vmctx.Message().Value, p.Params) if err != nil { return types.InvokeRet{}, err diff --git a/chain/actors/actor_storagemarket_test.go b/chain/actors/actor_storagemarket_test.go new file mode 100644 index 000000000..dfd8b91f0 --- /dev/null +++ b/chain/actors/actor_storagemarket_test.go @@ -0,0 +1,65 @@ +package actors_test + +import ( + "testing" + + . "github.com/filecoin-project/go-lotus/chain/actors" + "github.com/filecoin-project/go-lotus/chain/address" + "github.com/filecoin-project/go-lotus/chain/types" + cbor "github.com/ipfs/go-ipld-cbor" +) + +func TestStorageMarketCreateMiner(t *testing.T) { + h := NewHarness(t) + var outaddr address.Address + h.Steps = []Step{ + { + M: types.Message{ + To: StorageMarketAddress, + From: h.From, + Method: 1, + GasPrice: types.NewInt(1), + GasLimit: types.NewInt(1), + Value: types.NewInt(0), + Params: h.DumpObject(&StorageMinerConstructorParams{ + Worker: h.Third, + SectorSize: types.NewInt(SectorSize), + PeerID: "fakepeerid", + }), + }, + Ret: func(t *testing.T, ret *types.MessageReceipt) { + if ret.ExitCode != 0 { + t.Fatal("invokation 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") + } + }, + }, + } + state := h.Execute() + 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) + } + hblock, err := h.bs.Get(act.Head) + if err != nil { + t.Fatal(err) + } + + smas := &StorageMinerActorState{} + cbor.DecodeInto(hblock.RawData(), smas) + if smas.Owner != h.From { + t.Fatalf("Owner should be %s, but is %s", h.From, smas.Owner) + } +} diff --git a/chain/actors/harness_test.go b/chain/actors/harness_test.go index 8855822e2..a2e9853b5 100644 --- a/chain/actors/harness_test.go +++ b/chain/actors/harness_test.go @@ -19,6 +19,7 @@ import ( type Harness struct { Steps []Step From address.Address + Third address.Address currStep int @@ -41,10 +42,12 @@ func NewHarness(t *testing.T) *Harness { from := blsaddr(0) maddr := blsaddr(1) + third := blsaddr(1) actors := map[address.Address]types.BigInt{ from: types.NewInt(1000000), maddr: types.NewInt(0), + third: types.NewInt(1000), } st, err := chain.MakeInitialStateTree(h.bs, actors) if err != nil { @@ -62,8 +65,9 @@ func NewHarness(t *testing.T) *Harness { if err != nil { t.Fatal(err) } - h.actors = []address.Address{from, maddr} + h.actors = []address.Address{from, maddr, third} h.From = from + h.Third = third return h } @@ -71,7 +75,11 @@ func (h *Harness) Execute() *chain.StateTree { for i, step := range h.Steps { h.currStep = i ret, err := h.vm.ApplyMessage(&step.M) - step.Err(h.t, err) + if step.Err != nil { + step.Err(h.t, err) + } else { + h.NoError(h.t, err) + } step.Ret(h.t, ret) } stateroot, err := h.vm.Flush(context.TODO()) @@ -118,9 +126,6 @@ func TestVMInvokeHarness(t *testing.T) { Value: types.NewInt(0), }, Ret: func(t *testing.T, ret *types.MessageReceipt) { - if ret == nil { - t.Fatal("ret is nil") - } if ret.ExitCode != 0 { t.Fatal("invocation failed: ", ret.ExitCode) } diff --git a/chain/statetree.go b/chain/statetree.go index b90d8b6d8..a19397141 100644 --- a/chain/statetree.go +++ b/chain/statetree.go @@ -120,7 +120,6 @@ func (st *StateTree) GetActor(addr address.Address) (*types.Actor, error) { func (st *StateTree) Flush() (cid.Cid, error) { for addr, act := range st.actorcache { - fmt.Println("FLUSHING ACTOR CACHE: ", addr.String(), act.Head) if err := st.root.Set(context.TODO(), string(addr.Bytes()), act); err != nil { return cid.Undef, err } diff --git a/chain/vm.go b/chain/vm.go index ad9953b28..ec1c9fc7b 100644 --- a/chain/vm.go +++ b/chain/vm.go @@ -75,8 +75,16 @@ func (vmc *VMContext) Ipld() *hamt.CborIpldStore { // Send allows the current execution context to invoke methods on other actors in the system func (vmc *VMContext) Send(to address.Address, method uint64, value types.BigInt, params []byte) ([]byte, uint8, error) { + + var from address.Address + if method == 0 { + // is constructor + from = vmc.msg.From + } else { + from = vmc.msg.To + } msg := &types.Message{ - From: vmc.msg.From, + From: from, To: to, Method: method, Value: value,