lotus/storage/garbage.go

144 lines
3.7 KiB
Go
Raw Normal View History

package storage
import (
"bytes"
"context"
"fmt"
"io"
"math"
"math/rand"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/sectorbuilder"
)
func (m *Miner) storeGarbage(ctx context.Context, sectorID uint64, existingPieceSizes []uint64, sizes ...uint64) ([]Piece, error) {
2019-11-07 19:54:24 +00:00
if len(sizes) == 0 {
return nil, nil
}
2019-11-06 23:09:48 +00:00
deals := make([]actors.StorageDeal, len(sizes))
for i, size := range sizes {
release := m.sb.RateLimit()
commP, err := sectorbuilder.GeneratePieceCommitment(io.LimitReader(rand.New(rand.NewSource(42)), int64(size)), size)
release()
if err != nil {
2019-11-06 23:09:48 +00:00
return nil, err
}
sdp := actors.StorageDealProposal{
PieceRef: commP[:],
PieceSize: size,
PieceSerialization: actors.SerializationUnixFSv0,
Client: m.worker,
Provider: m.maddr,
ProposalExpiration: math.MaxUint64,
Duration: math.MaxUint64 / 2, // /2 because overflows
StoragePricePerEpoch: types.NewInt(0),
StorageCollateral: types.NewInt(0),
ProposerSignature: nil,
}
if err := api.SignWith(ctx, m.api.WalletSign, m.worker, &sdp); err != nil {
2019-11-06 23:09:48 +00:00
return nil, xerrors.Errorf("signing storage deal failed: ", err)
}
storageDeal := actors.StorageDeal{
Proposal: sdp,
}
if err := api.SignWith(ctx, m.api.WalletSign, m.worker, &storageDeal); err != nil {
2019-11-06 23:09:48 +00:00
return nil, xerrors.Errorf("signing storage deal failed: ", err)
}
2019-11-06 23:09:48 +00:00
deals[i] = storageDeal
}
2019-11-06 23:09:48 +00:00
params, aerr := actors.SerializeParams(&actors.PublishStorageDealsParams{
Deals: deals,
})
if aerr != nil {
return nil, xerrors.Errorf("serializing PublishStorageDeals params failed: ", aerr)
}
smsg, err := m.api.MpoolPushMessage(ctx, &types.Message{
To: actors.StorageMarketAddress,
From: m.worker,
Value: types.NewInt(0),
GasPrice: types.NewInt(0),
GasLimit: types.NewInt(1000000),
Method: actors.SMAMethods.PublishStorageDeals,
Params: params,
})
if err != nil {
return nil, err
}
r, err := m.api.StateWaitMsg(ctx, smsg.Cid())
if err != nil {
return nil, err
}
if r.Receipt.ExitCode != 0 {
log.Error(xerrors.Errorf("publishing deal failed: exit %d", r.Receipt.ExitCode))
}
var resp actors.PublishStorageDealResponse
if err := resp.UnmarshalCBOR(bytes.NewReader(r.Receipt.Return)); err != nil {
return nil, err
}
if len(resp.DealIDs) != len(sizes) {
return nil, xerrors.New("got unexpected number of DealIDs from PublishStorageDeals")
}
2019-11-07 18:22:59 +00:00
out := make([]Piece, len(sizes))
2019-11-06 23:09:48 +00:00
for i, size := range sizes {
name := fmt.Sprintf("fake-file-%d", rand.Intn(100000000))
ppi, err := m.sb.AddPiece(size, sectorID, io.LimitReader(rand.New(rand.NewSource(42)), int64(size)), existingPieceSizes)
if err != nil {
2019-11-06 23:09:48 +00:00
return nil, err
}
existingPieceSizes = append(existingPieceSizes, size)
2019-11-07 18:22:59 +00:00
out[i] = Piece{
DealID: resp.DealIDs[i],
Ref: name,
Size: ppi.Size,
CommP: ppi.CommP[:],
}
2019-11-06 23:09:48 +00:00
}
2019-11-07 18:22:59 +00:00
return out, nil
2019-11-06 23:09:48 +00:00
}
2019-11-08 23:06:07 +00:00
func (m *Miner) StoreGarbageData() error {
2019-11-06 23:09:48 +00:00
go func() {
2019-11-08 23:06:07 +00:00
ctx := context.TODO() // we can't use the context from command which invokes
// this, as we run everything here async, and it's cancelled when the
// command exits
2019-11-07 18:22:59 +00:00
size := sectorbuilder.UserBytesForSectorSize(m.sb.SectorSize())
2019-11-06 23:09:48 +00:00
2019-11-07 18:22:59 +00:00
sid, err := m.sb.AcquireSectorId()
if err != nil {
2019-11-06 23:09:48 +00:00
log.Errorf("%+v", err)
return
}
pieces, err := m.storeGarbage(ctx, sid, []uint64{}, size)
2019-11-07 18:22:59 +00:00
if err != nil {
2019-11-06 23:09:48 +00:00
log.Errorf("%+v", err)
return
}
2019-11-07 18:22:59 +00:00
if err := m.newSector(context.TODO(), sid, pieces[0].DealID, pieces[0].Ref, pieces[0].ppi()); err != nil {
log.Errorf("%+v", err)
return
}
}()
return nil
}