lotus/chain/actors/actors_test.go

159 lines
3.2 KiB
Go
Raw Normal View History

2019-07-12 23:52:25 +00:00
package actors_test
import (
"context"
"encoding/binary"
"fmt"
"testing"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/go-address"
. "github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/gen"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
dstore "github.com/ipfs/go-datastore"
bstore "github.com/ipfs/go-ipfs-blockstore"
)
func blsaddr(n uint64) address.Address {
buf := make([]byte, 48)
binary.PutUvarint(buf, n)
addr, err := address.NewBLSAddress(buf)
if err != nil {
2019-11-16 23:41:14 +00:00
panic(err) // ok
}
return addr
}
func setupVMTestEnv(t *testing.T) (*vm.VM, []address.Address, bstore.Blockstore) {
bs := bstore.NewBlockstore(dstore.NewMapDatastore())
from := blsaddr(0)
maddr := blsaddr(1)
actors := map[address.Address]types.BigInt{
from: types.NewInt(1000000),
maddr: types.NewInt(0),
}
st, err := gen.MakeInitialStateTree(bs, actors)
if err != nil {
t.Fatal(err)
}
stateroot, err := st.Flush()
if err != nil {
t.Fatal(err)
}
2020-01-13 20:47:27 +00:00
cs := store.NewChainStore(bs, nil, nil)
// TODO: should probabaly mock out the randomness bit, nil works for now
2020-01-13 20:47:27 +00:00
vm, err := vm.NewVM(stateroot, 1, nil, maddr, cs.Blockstore(), cs.VMSys())
if err != nil {
t.Fatal(err)
}
return vm, []address.Address{from, maddr}, bs
2019-07-12 21:57:49 +00:00
}
func TestVMInvokeMethod(t *testing.T) {
vm, addrs, _ := setupVMTestEnv(t)
2019-07-12 21:57:49 +00:00
from := addrs[0]
var err error
cenc, err := SerializeParams(&StorageMinerConstructorParams{Owner: from, Worker: from})
if err != nil {
t.Fatal(err)
}
execparams := &ExecParams{
Code: StorageMinerCodeCid,
Params: cenc,
}
enc, err := SerializeParams(execparams)
if err != nil {
t.Fatal(err)
}
msg := &types.Message{
To: InitAddress,
From: from,
Method: IAMethods.Exec,
Params: enc,
GasPrice: types.NewInt(1),
GasLimit: types.NewInt(10000),
Value: types.NewInt(0),
}
ret, err := vm.ApplyMessage(context.TODO(), msg)
if err != nil {
t.Fatal(err)
}
if ret.ExitCode != 0 {
t.Fatal("invocation failed")
}
outaddr, err := address.NewFromBytes(ret.Return)
if err != nil {
t.Fatal(err)
}
if outaddr.String() != "t0102" {
t.Fatal("hold up")
}
}
func TestStorageMarketActorCreateMiner(t *testing.T) {
vm, addrs, bs := setupVMTestEnv(t)
2019-07-12 21:57:49 +00:00
from := addrs[0]
maddr := addrs[1]
cheatStorageMarketTotal(t, vm, bs)
2019-07-12 21:36:49 +00:00
params := &StorageMinerConstructorParams{
Owner: maddr,
2019-07-12 21:36:49 +00:00
Worker: maddr,
SectorSize: build.SectorSizes[0],
2019-07-12 21:36:49 +00:00
PeerID: "fakepeerid",
}
var err error
enc, err := SerializeParams(params)
if err != nil {
t.Fatal(err)
}
msg := &types.Message{
2019-10-19 08:06:41 +00:00
To: StoragePowerAddress,
From: from,
Method: SPAMethods.CreateStorageMiner,
Params: enc,
GasPrice: types.NewInt(1),
GasLimit: types.NewInt(10000),
Value: types.NewInt(50000),
}
ret, err := vm.ApplyMessage(context.TODO(), msg)
if err != nil {
t.Fatal(err)
}
if ret.ExitCode != 0 {
fmt.Println(ret.ActorErr)
2019-07-12 21:36:49 +00:00
t.Fatal("invocation failed: ", ret.ExitCode)
}
outaddr, err := address.NewFromBytes(ret.Return)
if err != nil {
t.Fatal(err)
}
if outaddr.String() != "t0102" {
t.Fatal("hold up")
}
}