2019-07-12 23:52:25 +00:00
|
|
|
package actors_test
|
2019-07-12 03:59:55 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-26 19:07:19 +00:00
|
|
|
"context"
|
2019-07-12 03:59:55 +00:00
|
|
|
"encoding/binary"
|
2019-09-13 01:06:06 +00:00
|
|
|
"fmt"
|
2019-07-12 03:59:55 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
|
|
|
|
. "github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"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"
|
2019-07-12 03:59:55 +00:00
|
|
|
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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2019-09-13 01:06:06 +00:00
|
|
|
func setupVMTestEnv(t *testing.T) (*vm.VM, []address.Address, bstore.Blockstore) {
|
2019-07-12 03:59:55 +00:00
|
|
|
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),
|
|
|
|
}
|
2019-07-25 22:15:03 +00:00
|
|
|
st, err := gen.MakeInitialStateTree(bs, actors)
|
2019-07-12 03:59:55 +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
|
|
|
cs := store.NewChainStore(bs, nil)
|
2019-07-12 03:59:55 +00:00
|
|
|
|
2019-09-20 02:54:52 +00:00
|
|
|
// TODO: should probabaly mock out the randomness bit, nil works for now
|
2019-10-15 04:33:29 +00:00
|
|
|
vm, err := vm.NewVM(stateroot, 1, nil, maddr, cs.Blockstore())
|
2019-07-12 03:59:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-13 01:06:06 +00:00
|
|
|
return vm, []address.Address{from, maddr}, bs
|
2019-07-12 21:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVMInvokeMethod(t *testing.T) {
|
2019-09-13 01:06:06 +00:00
|
|
|
vm, addrs, _ := setupVMTestEnv(t)
|
2019-07-12 21:57:49 +00:00
|
|
|
from := addrs[0]
|
2019-07-12 03:59:55 +00:00
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
var err error
|
2019-10-14 11:53:20 +00:00
|
|
|
cenc, err := SerializeParams(&StorageMinerConstructorParams{Owner: from, Worker: from})
|
2019-07-15 20:27:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-07-12 03:59:55 +00:00
|
|
|
execparams := &ExecParams{
|
|
|
|
Code: StorageMinerCodeCid,
|
2019-07-15 20:27:45 +00:00
|
|
|
Params: cenc,
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
2019-09-10 23:03:17 +00:00
|
|
|
enc, err := SerializeParams(execparams)
|
2019-07-12 03:59:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
2019-10-21 18:12:11 +00:00
|
|
|
To: InitAddress,
|
2019-07-12 03:59:55 +00:00
|
|
|
From: from,
|
2019-07-26 21:42:38 +00:00
|
|
|
Method: IAMethods.Exec,
|
2019-07-12 03:59:55 +00:00
|
|
|
Params: enc,
|
|
|
|
GasPrice: types.NewInt(1),
|
2019-08-29 18:20:59 +00:00
|
|
|
GasLimit: types.NewInt(10000),
|
2019-07-12 03:59:55 +00:00
|
|
|
Value: types.NewInt(0),
|
|
|
|
}
|
|
|
|
|
2019-07-26 19:07:19 +00:00
|
|
|
ret, err := vm.ApplyMessage(context.TODO(), msg)
|
2019-07-12 03:59:55 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 20:56:41 +00:00
|
|
|
|
|
|
|
func TestStorageMarketActorCreateMiner(t *testing.T) {
|
2019-09-13 01:06:06 +00:00
|
|
|
vm, addrs, bs := setupVMTestEnv(t)
|
2019-07-12 21:57:49 +00:00
|
|
|
from := addrs[0]
|
|
|
|
maddr := addrs[1]
|
2019-07-12 20:56:41 +00:00
|
|
|
|
2019-09-13 01:06:06 +00:00
|
|
|
cheatStorageMarketTotal(t, vm, bs)
|
|
|
|
|
2019-07-12 21:36:49 +00:00
|
|
|
params := &StorageMinerConstructorParams{
|
2019-10-14 11:53:20 +00:00
|
|
|
Owner: maddr,
|
2019-07-12 21:36:49 +00:00
|
|
|
Worker: maddr,
|
2019-08-29 19:01:29 +00:00
|
|
|
SectorSize: types.NewInt(build.SectorSize),
|
2019-07-12 21:36:49 +00:00
|
|
|
PeerID: "fakepeerid",
|
|
|
|
}
|
2019-09-10 23:03:17 +00:00
|
|
|
var err error
|
|
|
|
enc, err := SerializeParams(params)
|
2019-07-12 20:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
2019-10-19 08:06:41 +00:00
|
|
|
To: StoragePowerAddress,
|
2019-07-12 20:56:41 +00:00
|
|
|
From: from,
|
2019-10-11 22:59:36 +00:00
|
|
|
Method: SPAMethods.CreateStorageMiner,
|
2019-07-12 20:56:41 +00:00
|
|
|
Params: enc,
|
|
|
|
GasPrice: types.NewInt(1),
|
2019-08-29 18:20:59 +00:00
|
|
|
GasLimit: types.NewInt(10000),
|
2019-09-13 01:06:06 +00:00
|
|
|
Value: types.NewInt(50000),
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:07:19 +00:00
|
|
|
ret, err := vm.ApplyMessage(context.TODO(), msg)
|
2019-07-12 20:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret.ExitCode != 0 {
|
2019-09-13 01:06:06 +00:00
|
|
|
fmt.Println(ret.ActorErr)
|
2019-07-12 21:36:49 +00:00
|
|
|
t.Fatal("invocation failed: ", ret.ExitCode)
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
outaddr, err := address.NewFromBytes(ret.Return)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if outaddr.String() != "t0102" {
|
|
|
|
t.Fatal("hold up")
|
|
|
|
}
|
|
|
|
}
|