2019-11-01 23:43:54 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2019-11-06 23:09:48 +00:00
|
|
|
// TODO: expected sector ID
|
|
|
|
func (m *Miner) storeGarbage(ctx context.Context, sizes ...uint64) ([]uint64, error) {
|
|
|
|
deals := make([]actors.StorageDeal, len(sizes))
|
|
|
|
for i, size := range sizes {
|
2019-11-01 23:43:54 +00:00
|
|
|
commP, err := sectorbuilder.GeneratePieceCommitment(io.LimitReader(rand.New(rand.NewSource(42)), int64(size)), size)
|
|
|
|
if err != nil {
|
2019-11-06 23:09:48 +00:00
|
|
|
return nil, err
|
2019-11-01 23:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sdp := actors.StorageDealProposal{
|
|
|
|
PieceRef: commP[:],
|
|
|
|
PieceSize: size,
|
|
|
|
PieceSerialization: actors.SerializationUnixFSv0,
|
|
|
|
Client: m.worker,
|
2019-11-02 14:13:21 +00:00
|
|
|
Provider: m.maddr,
|
2019-11-01 23:43:54 +00:00
|
|
|
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)
|
2019-11-01 23:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-01 23:43:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 23:09:48 +00:00
|
|
|
deals[i] = storageDeal
|
|
|
|
}
|
2019-11-01 23:43:54 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We may want this to happen after fetching data
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
sectorIDs := make([]uint64, len(sizes))
|
|
|
|
|
|
|
|
for i, size := range sizes {
|
|
|
|
name := fmt.Sprintf("fake-file-%d", rand.Intn(100000000))
|
|
|
|
sectorID, err := m.secst.AddPiece(name, size, io.LimitReader(rand.New(rand.NewSource(42)), int64(size)), resp.DealIDs[i])
|
2019-11-01 23:43:54 +00:00
|
|
|
if err != nil {
|
2019-11-06 23:09:48 +00:00
|
|
|
return nil, err
|
2019-11-01 23:43:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 23:09:48 +00:00
|
|
|
sectorIDs[i] = sectorID
|
|
|
|
}
|
|
|
|
|
|
|
|
return sectorIDs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Miner) StoreGarbageData(_ context.Context) error {
|
|
|
|
ctx := context.TODO()
|
|
|
|
ssize, err := m.SectorSize(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to get miner sector size: %w", err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
size := sectorbuilder.UserBytesForSectorSize(ssize)
|
|
|
|
|
|
|
|
sids, err := m.storeGarbage(ctx, size)
|
2019-11-01 23:43:54 +00:00
|
|
|
if err != nil {
|
2019-11-06 23:09:48 +00:00
|
|
|
log.Errorf("%+v", err)
|
2019-11-01 23:43:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-06 23:09:48 +00:00
|
|
|
if err := m.SealSector(context.TODO(), sids[0]); err != nil {
|
|
|
|
log.Errorf("%+v", err)
|
2019-11-01 23:43:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|