2019-12-06 14:06:42 +00:00
|
|
|
package actors_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-01-07 18:23:49 +00:00
|
|
|
"math"
|
2019-12-06 14:06:42 +00:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-10 19:16:36 +00:00
|
|
|
commcid "github.com/filecoin-project/go-fil-commcid"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/market"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
|
2020-01-07 16:18:35 +00:00
|
|
|
"github.com/filecoin-project/go-sectorbuilder"
|
2019-12-06 14:06:42 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-01-07 21:51:33 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-12-06 14:06:42 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-01-07 18:23:49 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/rlepluslazy"
|
2019-12-06 14:06:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMinerCommitSectors(t *testing.T) {
|
|
|
|
var worker, client address.Address
|
|
|
|
var minerAddr address.Address
|
|
|
|
opts := []HarnessOpt{
|
|
|
|
HarnessAddr(&worker, 1000000),
|
|
|
|
HarnessAddr(&client, 1000000),
|
|
|
|
HarnessActor(&minerAddr, &worker, actors.StorageMinerCodeCid,
|
|
|
|
func() cbg.CBORMarshaler {
|
|
|
|
return &actors.StorageMinerConstructorParams{
|
|
|
|
Owner: worker,
|
|
|
|
Worker: worker,
|
|
|
|
SectorSize: 1024,
|
|
|
|
PeerID: "fakepeerid",
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
h := NewHarness(t, opts...)
|
2020-02-10 19:16:36 +00:00
|
|
|
h.vm.Syscalls.ValidatePoRep = func(ctx context.Context, maddr address.Address, ssize abi.SectorSize, commD, commR, ticket, proof, seed []byte, sectorID abi.SectorNumber) (bool, aerrors.ActorError) {
|
2019-12-06 14:06:42 +00:00
|
|
|
// all proofs are valid
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, _ := h.SendFunds(t, worker, minerAddr, types.NewInt(100000))
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
ret, _ = h.InvokeWithValue(t, client, actors.StorageMarketAddress, actors.SMAMethods.AddBalance, types.NewInt(2000), nil)
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
addSectorToMiner(h, t, minerAddr, worker, client, 1)
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
assertSectorIDs(h, t, minerAddr, []abi.SectorNumber{1})
|
2020-01-07 18:23:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type badRuns struct {
|
|
|
|
done bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *badRuns) HasNext() bool {
|
|
|
|
return !br.done
|
2019-12-06 14:06:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 18:23:49 +00:00
|
|
|
func (br *badRuns) NextRun() (rlepluslazy.Run, error) {
|
|
|
|
br.done = true
|
|
|
|
return rlepluslazy.Run{true, math.MaxInt64}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ rlepluslazy.RunIterator = (*badRuns)(nil)
|
|
|
|
|
2019-12-21 04:02:18 +00:00
|
|
|
func TestMinerSubmitBadFault(t *testing.T) {
|
2020-01-07 21:51:33 +00:00
|
|
|
oldSS, oldMin := build.SectorSizes, build.MinimumMinerPower
|
2020-02-10 19:16:36 +00:00
|
|
|
build.SectorSizes, build.MinimumMinerPower = []abi.SectorSize{1024}, 1024
|
2020-01-07 21:51:33 +00:00
|
|
|
defer func() {
|
|
|
|
build.SectorSizes, build.MinimumMinerPower = oldSS, oldMin
|
|
|
|
}()
|
|
|
|
|
2019-12-21 04:02:18 +00:00
|
|
|
var worker, client address.Address
|
|
|
|
var minerAddr address.Address
|
|
|
|
opts := []HarnessOpt{
|
|
|
|
HarnessAddr(&worker, 1000000),
|
|
|
|
HarnessAddr(&client, 1000000),
|
2020-01-07 21:51:33 +00:00
|
|
|
HarnessAddMiner(&minerAddr, &worker),
|
2019-12-21 04:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h := NewHarness(t, opts...)
|
2020-02-10 19:16:36 +00:00
|
|
|
h.vm.Syscalls.ValidatePoRep = func(ctx context.Context, maddr address.Address, ssize abi.SectorSize, commD, commR, ticket, proof, seed []byte, sectorID abi.SectorNumber) (bool, aerrors.ActorError) {
|
2019-12-21 04:02:18 +00:00
|
|
|
// all proofs are valid
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, _ := h.SendFunds(t, worker, minerAddr, types.NewInt(100000))
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
ret, _ = h.InvokeWithValue(t, client, actors.StorageMarketAddress, actors.SMAMethods.AddBalance, types.NewInt(2000), nil)
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
addSectorToMiner(h, t, minerAddr, worker, client, 1)
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
assertSectorIDs(h, t, minerAddr, []abi.SectorNumber{1})
|
2019-12-21 04:02:18 +00:00
|
|
|
|
|
|
|
bf := types.NewBitField()
|
|
|
|
bf.Set(6)
|
|
|
|
ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.DeclareFaults, &actors.DeclareFaultsParams{bf})
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
ret, _ = h.Invoke(t, actors.NetworkAddress, minerAddr, actors.MAMethods.SubmitElectionPoSt, nil)
|
|
|
|
ApplyOK(t, ret)
|
2020-02-10 19:16:36 +00:00
|
|
|
assertSectorIDs(h, t, minerAddr, []abi.SectorNumber{1})
|
2019-12-21 04:02:18 +00:00
|
|
|
|
2020-01-07 21:51:33 +00:00
|
|
|
st, err := getMinerState(context.TODO(), h.vm.StateTree(), h.bs, minerAddr)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
expectedPower := st.Power
|
|
|
|
if types.BigCmp(expectedPower, types.NewInt(1024)) != 0 {
|
|
|
|
t.Errorf("Expected power of 1024, got %s", expectedPower)
|
|
|
|
}
|
|
|
|
|
2019-12-21 04:04:38 +00:00
|
|
|
badnum := uint64(0)
|
|
|
|
badnum--
|
|
|
|
bf = types.NewBitField()
|
|
|
|
bf.Set(badnum)
|
2020-01-07 18:23:49 +00:00
|
|
|
bf.Set(badnum - 1)
|
2019-12-21 04:04:38 +00:00
|
|
|
ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.DeclareFaults, &actors.DeclareFaultsParams{bf})
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
ret, _ = h.Invoke(t, actors.NetworkAddress, minerAddr, actors.MAMethods.SubmitElectionPoSt, nil)
|
2020-01-07 18:23:49 +00:00
|
|
|
|
2019-12-21 04:04:38 +00:00
|
|
|
ApplyOK(t, ret)
|
2020-02-10 19:16:36 +00:00
|
|
|
assertSectorIDs(h, t, minerAddr, []abi.SectorNumber{1})
|
2020-01-07 18:23:49 +00:00
|
|
|
|
2020-01-07 18:59:31 +00:00
|
|
|
st, err = getMinerState(context.TODO(), h.vm.StateTree(), h.bs, minerAddr)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
currentPower := st.Power
|
|
|
|
if types.BigCmp(expectedPower, currentPower) != 0 {
|
|
|
|
t.Errorf("power changed and shouldn't have: %s != %s", expectedPower, currentPower)
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:23:49 +00:00
|
|
|
bf.Set(badnum - 2)
|
|
|
|
ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.DeclareFaults, &actors.DeclareFaultsParams{bf})
|
|
|
|
if ret.ExitCode != 3 {
|
|
|
|
t.Errorf("expected exit code 3, got %d: %+v", ret.ExitCode, ret.ActorErr)
|
|
|
|
}
|
2020-02-10 19:16:36 +00:00
|
|
|
assertSectorIDs(h, t, minerAddr, []abi.SectorNumber{1})
|
2020-01-07 18:23:49 +00:00
|
|
|
|
|
|
|
rle, err := rlepluslazy.EncodeRuns(&badRuns{}, []byte{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
bf, err = types.NewBitFieldFromBytes(rle)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.DeclareFaults, &actors.DeclareFaultsParams{bf})
|
|
|
|
if ret.ExitCode != 3 {
|
|
|
|
t.Errorf("expected exit code 3, got %d: %+v", ret.ExitCode, ret.ActorErr)
|
|
|
|
}
|
2020-02-10 19:16:36 +00:00
|
|
|
assertSectorIDs(h, t, minerAddr, []abi.SectorNumber{1})
|
2019-12-21 04:04:38 +00:00
|
|
|
|
2019-12-21 04:02:18 +00:00
|
|
|
bf = types.NewBitField()
|
|
|
|
bf.Set(1)
|
|
|
|
ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.DeclareFaults, &actors.DeclareFaultsParams{bf})
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
ret, _ = h.Invoke(t, actors.NetworkAddress, minerAddr, actors.MAMethods.SubmitElectionPoSt, nil)
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
assertSectorIDs(h, t, minerAddr, []abi.SectorNumber{})
|
2019-12-21 04:04:38 +00:00
|
|
|
|
2019-12-21 04:02:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
func addSectorToMiner(h *Harness, t *testing.T, minerAddr, worker, client address.Address, sid abi.SectorNumber) {
|
2019-12-06 14:06:42 +00:00
|
|
|
t.Helper()
|
2020-02-10 19:16:36 +00:00
|
|
|
s := abi.PaddedPieceSize(1024).Unpadded()
|
2019-12-06 14:06:42 +00:00
|
|
|
deal := h.makeFakeDeal(t, minerAddr, worker, client, s)
|
|
|
|
ret, _ := h.Invoke(t, worker, actors.StorageMarketAddress, actors.SMAMethods.PublishStorageDeals,
|
|
|
|
&actors.PublishStorageDealsParams{
|
2020-02-10 19:16:36 +00:00
|
|
|
Deals: []market.ClientDealProposal{*deal},
|
2019-12-06 14:06:42 +00:00
|
|
|
})
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
var dealIds actors.PublishStorageDealResponse
|
|
|
|
if err := dealIds.UnmarshalCBOR(bytes.NewReader(ret.Return)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
dealid := dealIds.IDs[0]
|
2019-12-06 14:06:42 +00:00
|
|
|
|
|
|
|
ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.PreCommitSector,
|
2020-02-10 19:16:36 +00:00
|
|
|
&miner.SectorPreCommitInfo{
|
2019-12-06 14:06:42 +00:00
|
|
|
SectorNumber: sid,
|
2020-02-10 19:16:36 +00:00
|
|
|
SealedCID: commcid.ReplicaCommitmentV1ToCID([]byte("cats")),
|
2019-12-06 14:06:42 +00:00
|
|
|
SealEpoch: 10,
|
2020-02-10 19:16:36 +00:00
|
|
|
DealIDs: []abi.DealID{dealid},
|
2019-12-06 14:06:42 +00:00
|
|
|
})
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
h.BlockHeight += 100
|
|
|
|
ret, _ = h.Invoke(t, worker, minerAddr, actors.MAMethods.ProveCommitSector,
|
2020-02-10 19:16:36 +00:00
|
|
|
&miner.ProveCommitSectorParams{
|
|
|
|
SectorNumber: sid,
|
|
|
|
Proof: abi.SealProof{ProofBytes: []byte("prooofy")},
|
2019-12-06 14:06:42 +00:00
|
|
|
})
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
func assertSectorIDs(h *Harness, t *testing.T, maddr address.Address, ids []abi.SectorNumber) {
|
2019-12-06 14:06:42 +00:00
|
|
|
t.Helper()
|
|
|
|
sectors, err := getMinerSectorSet(context.TODO(), h.vm.StateTree(), h.bs, maddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sectors) != len(ids) {
|
|
|
|
t.Fatal("miner has wrong number of sectors in their sector set")
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
all := make(map[abi.SectorNumber]bool)
|
2019-12-06 14:06:42 +00:00
|
|
|
for _, s := range sectors {
|
|
|
|
all[s.SectorID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
if !all[id] {
|
|
|
|
t.Fatal("expected to find sector ID: ", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:59:31 +00:00
|
|
|
func getMinerState(ctx context.Context, st types.StateTree, bs blockstore.Blockstore, maddr address.Address) (*actors.StorageMinerActorState, error) {
|
2019-12-06 14:06:42 +00:00
|
|
|
mact, err := st.GetActor(maddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:19:05 +00:00
|
|
|
cst := cbor.NewCborStore(bs)
|
2019-12-06 14:06:42 +00:00
|
|
|
|
|
|
|
var mstate actors.StorageMinerActorState
|
|
|
|
if err := cst.Get(ctx, mact.Head, &mstate); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-07 18:59:31 +00:00
|
|
|
return &mstate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMinerSectorSet(ctx context.Context, st types.StateTree, bs blockstore.Blockstore, maddr address.Address) ([]*api.ChainSectorInfo, error) {
|
|
|
|
mstate, err := getMinerState(ctx, st, bs, maddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-06 14:06:42 +00:00
|
|
|
|
|
|
|
return stmgr.LoadSectorsFromSet(ctx, bs, mstate.Sectors)
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
func (h *Harness) makeFakeDeal(t *testing.T, miner, worker, client address.Address, size abi.UnpaddedPieceSize) *market.ClientDealProposal {
|
2019-12-06 14:06:42 +00:00
|
|
|
data := make([]byte, size)
|
|
|
|
rand.Read(data)
|
2020-01-14 02:04:33 +00:00
|
|
|
commP, err := sectorbuilder.GeneratePieceCommitment(bytes.NewReader(data), size)
|
2019-12-06 14:06:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
prop := market.DealProposal{
|
|
|
|
PieceCID: commcid.PieceCommitmentV1ToCID(commP[:]),
|
|
|
|
PieceSize: size.Padded(),
|
2019-12-06 14:06:42 +00:00
|
|
|
|
|
|
|
Client: client,
|
|
|
|
Provider: miner,
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
StartEpoch: 10000,
|
|
|
|
EndEpoch: 10150,
|
2019-12-06 14:06:42 +00:00
|
|
|
|
|
|
|
StoragePricePerEpoch: types.NewInt(1),
|
2020-02-10 19:16:36 +00:00
|
|
|
ProviderCollateral: types.NewInt(0),
|
2019-12-06 14:06:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
return &market.ClientDealProposal{
|
|
|
|
Proposal: prop,
|
|
|
|
ClientSignature: crypto.Signature{}, // TODO: not quite correct
|
2019-12-06 14:06:42 +00:00
|
|
|
}
|
|
|
|
}
|