2019-07-24 01:13:56 +00:00
|
|
|
package impl
|
2019-07-24 00:58:31 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-27 01:54:03 +00:00
|
|
|
"context"
|
2019-07-27 21:08:10 +00:00
|
|
|
"fmt"
|
2019-08-14 20:27:10 +00:00
|
|
|
"io"
|
2019-07-27 21:08:10 +00:00
|
|
|
"math/rand"
|
2019-07-27 01:54:03 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/lotus/lib/sectorbuilder"
|
|
|
|
"github.com/filecoin-project/lotus/storage"
|
2019-10-29 20:41:30 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/commitment"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sector"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sectorblocks"
|
2019-10-16 07:07:16 +00:00
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
2019-07-24 00:58:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StorageMinerAPI struct {
|
|
|
|
CommonAPI
|
2019-07-27 00:45:27 +00:00
|
|
|
|
2019-08-10 01:54:45 +00:00
|
|
|
SectorBuilderConfig *sectorbuilder.SectorBuilderConfig
|
|
|
|
SectorBuilder *sectorbuilder.SectorBuilder
|
2019-08-14 20:27:10 +00:00
|
|
|
Sectors *sector.Store
|
2019-08-26 10:04:57 +00:00
|
|
|
SectorBlocks *sectorblocks.SectorBlocks
|
2019-10-29 20:41:30 +00:00
|
|
|
CommitmentTracker *commitment.Tracker
|
2019-07-30 00:46:56 +00:00
|
|
|
|
|
|
|
Miner *storage.Miner
|
2019-07-24 00:58:31 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 02:32:32 +00:00
|
|
|
func (sm *StorageMinerAPI) ActorAddress(context.Context) (address.Address, error) {
|
|
|
|
return sm.SectorBuilderConfig.Miner, nil
|
2019-08-10 01:54:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 17:52:07 +00:00
|
|
|
func (sm *StorageMinerAPI) StoreGarbageData(ctx context.Context) error {
|
2019-10-16 07:07:16 +00:00
|
|
|
ssize, err := sm.Miner.SectorSize(ctx)
|
|
|
|
if err != nil {
|
2019-10-29 17:52:07 +00:00
|
|
|
return xerrors.Errorf("failed to get miner sector size: %w", err)
|
2019-10-16 07:07:16 +00:00
|
|
|
}
|
2019-10-29 17:52:07 +00:00
|
|
|
go func() {
|
|
|
|
size := sectorbuilder.UserBytesForSectorSize(ssize)
|
|
|
|
|
|
|
|
// TODO: create a deal
|
|
|
|
name := fmt.Sprintf("fake-file-%d", rand.Intn(100000000))
|
|
|
|
sectorId, err := sm.Sectors.AddPiece(name, size, io.LimitReader(rand.New(rand.NewSource(42)), int64(size)))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sm.Sectors.SealSector(ctx, sectorId); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return err
|
2019-07-27 01:54:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 21:08:10 +00:00
|
|
|
func (sm *StorageMinerAPI) SectorsStatus(ctx context.Context, sid uint64) (sectorbuilder.SectorSealingStatus, error) {
|
|
|
|
return sm.SectorBuilder.SealStatus(sid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List all staged sectors
|
2019-10-14 02:32:32 +00:00
|
|
|
func (sm *StorageMinerAPI) SectorsList(context.Context) ([]uint64, error) {
|
2019-07-27 21:08:10 +00:00
|
|
|
return sm.SectorBuilder.GetAllStagedSectors()
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:04:57 +00:00
|
|
|
func (sm *StorageMinerAPI) SectorsRefs(context.Context) (map[string][]api.SealedRef, error) {
|
|
|
|
// json can't handle cids as map keys
|
|
|
|
out := map[string][]api.SealedRef{}
|
|
|
|
|
|
|
|
refs, err := sm.SectorBlocks.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range refs {
|
|
|
|
out[k.String()] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2019-10-29 20:41:30 +00:00
|
|
|
func (sm *StorageMinerAPI) CommitmentsList(ctx context.Context) ([]api.SectorCommitment, error) {
|
|
|
|
return sm.CommitmentTracker.List()
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:58:31 +00:00
|
|
|
var _ api.StorageMiner = &StorageMinerAPI{}
|