lotus/chain/actors/actor_storagepower_test.go

175 lines
4.2 KiB
Go
Raw Normal View History

package actors_test
import (
2019-09-12 04:12:35 +00:00
"context"
"fmt"
"testing"
"github.com/filecoin-project/lotus/build"
cbg "github.com/whyrusleeping/cbor-gen"
2019-09-10 02:05:24 +00:00
. "github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/address"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/chain/wallet"
2019-09-12 04:12:35 +00:00
cid "github.com/ipfs/go-cid"
hamt "github.com/ipfs/go-hamt-ipld"
bstore "github.com/ipfs/go-ipfs-blockstore"
cbor "github.com/ipfs/go-ipld-cbor"
2019-09-12 04:12:35 +00:00
mh "github.com/multiformats/go-multihash"
"github.com/stretchr/testify/assert"
)
2019-09-12 04:12:35 +00:00
func TestStorageMarketCreateAndSlashMiner(t *testing.T) {
var ownerAddr, workerAddr address.Address
opts := []HarnessOpt{
HarnessAddr(&ownerAddr, 1000000),
HarnessAddr(&workerAddr, 100000),
}
h := NewHarness(t, opts...)
var minerAddr address.Address
{
// cheating the bootstrapping problem
cheatStorageMarketTotal(t, h.vm, h.cs.Blockstore())
2019-10-19 08:06:41 +00:00
ret, _ := h.InvokeWithValue(t, ownerAddr, StoragePowerAddress, SPAMethods.CreateStorageMiner,
types.NewInt(500000),
&CreateStorageMinerParams{
Owner: ownerAddr,
Worker: workerAddr,
SectorSize: build.SectorSizes[0],
PeerID: "fakepeerid",
})
ApplyOK(t, ret)
var err error
minerAddr, err = address.NewFromBytes(ret.Return)
assert.NoError(t, err)
}
{
2019-10-19 08:06:41 +00:00
ret, _ := h.Invoke(t, ownerAddr, StoragePowerAddress, SPAMethods.IsMiner,
&IsMinerParam{Addr: minerAddr})
ApplyOK(t, ret)
var output bool
err := cbor.DecodeInto(ret.Return, &output)
if err != nil {
t.Fatalf("error decoding: %+v", err)
}
if !output {
t.Fatalf("%s is miner but IsMiner call returned false", minerAddr)
}
}
{
2019-10-19 08:06:41 +00:00
ret, _ := h.Invoke(t, ownerAddr, StoragePowerAddress, SPAMethods.PowerLookup,
&PowerLookupParams{Miner: minerAddr})
ApplyOK(t, ret)
power := types.BigFromBytes(ret.Return)
if types.BigCmp(power, types.NewInt(0)) != 0 {
t.Fatalf("power should be zero, is: %s", power)
}
}
{
ret, _ := h.Invoke(t, ownerAddr, minerAddr, MAMethods.GetOwner, nil)
ApplyOK(t, ret)
oA, err := address.NewFromBytes(ret.Return)
assert.NoError(t, err)
assert.Equal(t, ownerAddr, oA, "return from GetOwner should be equal to the owner")
}
2019-09-12 04:12:35 +00:00
{
b1 := fakeBlock(t, minerAddr, 100)
b2 := fakeBlock(t, minerAddr, 101)
signBlock(t, h.w, workerAddr, b1)
signBlock(t, h.w, workerAddr, b2)
2019-10-19 08:06:41 +00:00
ret, _ := h.Invoke(t, ownerAddr, StoragePowerAddress, SPAMethods.ArbitrateConsensusFault,
&ArbitrateConsensusFaultParams{
2019-09-12 04:12:35 +00:00
Block1: b1,
Block2: b2,
})
ApplyOK(t, ret)
}
{
2019-10-19 08:06:41 +00:00
ret, _ := h.Invoke(t, ownerAddr, StoragePowerAddress, SPAMethods.PowerLookup,
&PowerLookupParams{Miner: minerAddr})
assert.Equal(t, ret.ExitCode, byte(1))
}
{
2019-10-19 08:06:41 +00:00
ret, _ := h.Invoke(t, ownerAddr, StoragePowerAddress, SPAMethods.IsMiner, &IsMinerParam{minerAddr})
ApplyOK(t, ret)
assert.Equal(t, ret.Return, cbg.CborBoolFalse)
}
}
func cheatStorageMarketTotal(t *testing.T, vm *vm.VM, bs bstore.Blockstore) {
t.Helper()
2019-10-19 08:06:41 +00:00
sma, err := vm.StateTree().GetActor(StoragePowerAddress)
if err != nil {
t.Fatal(err)
}
cst := hamt.CSTFromBstore(bs)
var smastate StoragePowerState
if err := cst.Get(context.TODO(), sma.Head, &smastate); err != nil {
t.Fatal(err)
}
smastate.TotalStorage = types.NewInt(10000)
c, err := cst.Put(context.TODO(), &smastate)
if err != nil {
t.Fatal(err)
}
sma.Head = c
2019-10-19 08:06:41 +00:00
if err := vm.StateTree().SetActor(StoragePowerAddress, sma); err != nil {
t.Fatal(err)
}
2019-09-12 04:12:35 +00:00
}
func fakeBlock(t *testing.T, minerAddr address.Address, ts uint64) *types.BlockHeader {
c := fakeCid(t, 1)
2019-09-27 23:55:15 +00:00
return &types.BlockHeader{Height: 5, Miner: minerAddr, Timestamp: ts, ParentStateRoot: c, Messages: c, ParentMessageReceipts: c, BLSAggregate: types.Signature{Type: types.KTBLS}}
2019-09-12 04:12:35 +00:00
}
func fakeCid(t *testing.T, s int) cid.Cid {
t.Helper()
c, err := cid.NewPrefixV1(cid.Raw, mh.IDENTITY).Sum([]byte(fmt.Sprintf("%d", s)))
if err != nil {
t.Fatal(err)
}
return c
}
func signBlock(t *testing.T, w *wallet.Wallet, worker address.Address, blk *types.BlockHeader) {
t.Helper()
sb, err := blk.SigningBytes()
if err != nil {
t.Fatal(err)
}
sig, err := w.Sign(context.TODO(), worker, sb)
if err != nil {
t.Fatal(err)
}
blk.BlockSig = *sig
}