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"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-12 23:52:25 +00:00
|
|
|
. "github.com/filecoin-project/go-lotus/chain/actors"
|
2019-07-12 03:59:55 +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/gen"
|
2019-07-26 04:54:22 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/store"
|
2019-07-12 03:59:55 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-26 04:54:22 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/vm"
|
2019-07-12 03:59:55 +00:00
|
|
|
dstore "github.com/ipfs/go-datastore"
|
|
|
|
bstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-07-26 04:54:22 +00:00
|
|
|
func setupVMTestEnv(t *testing.T) (*vm.VM, []address.Address) {
|
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-07-26 04:54:22 +00:00
|
|
|
vm, err := vm.NewVM(stateroot, 1, maddr, cs)
|
2019-07-12 03:59:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-07-12 21:57:49 +00:00
|
|
|
return vm, []address.Address{from, maddr}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVMInvokeMethod(t *testing.T) {
|
|
|
|
vm, addrs := setupVMTestEnv(t)
|
|
|
|
from := addrs[0]
|
2019-07-12 03:59:55 +00:00
|
|
|
|
2019-07-15 20:27:45 +00:00
|
|
|
cenc, err := cbor.DumpObject(StorageMinerConstructorParams{})
|
|
|
|
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
|
|
|
}
|
|
|
|
enc, err := cbor.DumpObject(execparams)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
|
|
|
To: InitActorAddress,
|
|
|
|
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),
|
|
|
|
GasLimit: types.NewInt(1),
|
|
|
|
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-07-12 21:57:49 +00:00
|
|
|
vm, addrs := setupVMTestEnv(t)
|
|
|
|
from := addrs[0]
|
|
|
|
maddr := addrs[1]
|
2019-07-12 20:56:41 +00:00
|
|
|
|
2019-07-12 21:36:49 +00:00
|
|
|
params := &StorageMinerConstructorParams{
|
|
|
|
Worker: maddr,
|
2019-07-15 17:48:59 +00:00
|
|
|
SectorSize: types.NewInt(SectorSize),
|
2019-07-12 21:36:49 +00:00
|
|
|
PeerID: "fakepeerid",
|
|
|
|
}
|
2019-07-12 20:56:41 +00:00
|
|
|
enc, err := cbor.DumpObject(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
|
|
|
To: StorageMarketAddress,
|
|
|
|
From: from,
|
2019-07-26 21:42:38 +00:00
|
|
|
Method: SMAMethods.CreateStorageMiner,
|
2019-07-12 20:56:41 +00:00
|
|
|
Params: enc,
|
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
GasLimit: types.NewInt(1),
|
|
|
|
Value: types.NewInt(0),
|
|
|
|
}
|
|
|
|
|
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-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")
|
|
|
|
}
|
|
|
|
}
|