2020-03-03 22:19:22 +00:00
|
|
|
package advmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
2020-03-05 22:02:01 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2020-03-04 02:24:08 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-03-03 22:19:22 +00:00
|
|
|
|
2020-03-05 00:38:07 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-03-03 22:19:22 +00:00
|
|
|
"github.com/filecoin-project/go-sectorbuilder"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-03-06 05:30:47 +00:00
|
|
|
storage2 "github.com/filecoin-project/specs-storage/storage"
|
2020-03-03 22:19:22 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sealmgr"
|
|
|
|
)
|
|
|
|
|
2020-03-05 00:38:07 +00:00
|
|
|
type SectorIDCounter interface {
|
|
|
|
Next() (abi.SectorNumber, error)
|
|
|
|
}
|
|
|
|
|
2020-03-03 22:19:22 +00:00
|
|
|
type LocalStorage interface {
|
|
|
|
GetStorage() (config.StorageConfig, error)
|
|
|
|
SetStorage(config.StorageConfig) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Path struct {
|
2020-03-05 19:21:06 +00:00
|
|
|
ID string
|
2020-03-03 22:19:22 +00:00
|
|
|
Weight uint64
|
|
|
|
|
|
|
|
LocalPath string
|
|
|
|
|
2020-03-05 19:21:06 +00:00
|
|
|
CanSeal bool
|
2020-03-03 22:19:22 +00:00
|
|
|
CanStore bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Worker interface {
|
2020-03-05 00:38:07 +00:00
|
|
|
sectorbuilder.Sealer
|
2020-03-03 22:19:22 +00:00
|
|
|
|
2020-03-05 00:38:07 +00:00
|
|
|
TaskTypes() map[sealmgr.TaskType]struct{}
|
2020-03-03 22:19:22 +00:00
|
|
|
Paths() []Path
|
|
|
|
}
|
|
|
|
|
|
|
|
type Manager struct {
|
2020-03-05 00:38:07 +00:00
|
|
|
workers []Worker
|
2020-03-05 19:21:06 +00:00
|
|
|
scfg *sectorbuilder.Config
|
|
|
|
sc SectorIDCounter
|
2020-03-05 00:38:07 +00:00
|
|
|
|
|
|
|
storage *storage
|
2020-03-03 22:19:22 +00:00
|
|
|
|
2020-03-06 05:30:47 +00:00
|
|
|
storage2.Prover
|
2020-03-04 02:24:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 00:38:07 +00:00
|
|
|
func New(ls LocalStorage, cfg *sectorbuilder.Config, sc SectorIDCounter) (*Manager, error) {
|
2020-03-04 02:24:08 +00:00
|
|
|
stor := &storage{
|
|
|
|
localStorage: ls,
|
|
|
|
}
|
|
|
|
if err := stor.open(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mid, err := address.IDFromAddress(cfg.Miner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting miner id: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
prover, err := sectorbuilder.New(&readonlyProvider{stor: stor, miner: abi.ActorID(mid)}, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("creating prover instance: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &Manager{
|
2020-03-05 19:21:06 +00:00
|
|
|
workers: []Worker{
|
2020-03-05 02:18:22 +00:00
|
|
|
&localWorker{scfg: cfg, storage: stor},
|
|
|
|
},
|
2020-03-05 00:38:07 +00:00
|
|
|
scfg: cfg,
|
2020-03-05 19:21:06 +00:00
|
|
|
sc: sc,
|
2020-03-05 00:38:07 +00:00
|
|
|
|
|
|
|
storage: stor,
|
2020-03-04 02:24:08 +00:00
|
|
|
|
|
|
|
Prover: prover,
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 22:02:01 +00:00
|
|
|
func (m *Manager) AddLocalStorage(path string) error {
|
|
|
|
path, err := homedir.Expand(path)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("expanding local path: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.storage.openPath(path); err != nil {
|
|
|
|
return xerrors.Errorf("opening local path: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sc, err := m.storage.localStorage.GetStorage()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("get storage config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sc.StoragePaths = append(sc.StoragePaths, config.LocalPath{Path: path})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-05 00:38:07 +00:00
|
|
|
func (m *Manager) SectorSize() abi.SectorSize {
|
|
|
|
sz, _ := m.scfg.SealProofType.SectorSize()
|
|
|
|
return sz
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 00:38:07 +00:00
|
|
|
func (m *Manager) NewSector() (abi.SectorNumber, error) {
|
|
|
|
return m.sc.Next()
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 00:38:07 +00:00
|
|
|
func (m *Manager) ReadPieceFromSealedSector(context.Context, abi.SectorNumber, sectorbuilder.UnpaddedByteIndex, abi.UnpaddedPieceSize, abi.SealRandomness, cid.Cid) (io.ReadCloser, error) {
|
2020-03-03 22:19:22 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:18:33 +00:00
|
|
|
func (m *Manager) getWorkersByPaths(task sealmgr.TaskType, inPaths []config.StorageMeta) ([]Worker, map[int]config.StorageMeta) {
|
|
|
|
var workers []Worker
|
|
|
|
paths := map[int]config.StorageMeta{}
|
2020-03-05 00:38:07 +00:00
|
|
|
|
|
|
|
for i, worker := range m.workers {
|
2020-03-05 18:18:33 +00:00
|
|
|
if _, ok := worker.TaskTypes()[task]; !ok {
|
2020-03-05 00:38:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the worker has access to the path we selected
|
|
|
|
var st *config.StorageMeta
|
|
|
|
for _, p := range worker.Paths() {
|
2020-03-05 18:18:33 +00:00
|
|
|
for _, m := range inPaths {
|
2020-03-05 00:38:07 +00:00
|
|
|
if p.ID == m.ID {
|
|
|
|
if st != nil && st.Weight > p.Weight {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
p := m // copy
|
|
|
|
st = &p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if st == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:18:33 +00:00
|
|
|
paths[i] = *st
|
|
|
|
workers = append(workers, worker)
|
|
|
|
}
|
|
|
|
|
|
|
|
return workers, paths
|
|
|
|
}
|
|
|
|
|
2020-03-06 05:30:47 +00:00
|
|
|
func (m *Manager) AddPiece(ctx context.Context, sn abi.SectorNumber, existingPieces []abi.UnpaddedPieceSize, sz abi.UnpaddedPieceSize, r io.Reader) (abi.PieceInfo, error) {
|
2020-03-05 18:18:33 +00:00
|
|
|
// TODO: consider multiple paths vs workers when initially allocating
|
|
|
|
|
|
|
|
var best []config.StorageMeta
|
|
|
|
var err error
|
|
|
|
if len(existingPieces) == 0 { // new
|
|
|
|
best, err = m.storage.findBestAllocStorage(sectorbuilder.FTUnsealed, true)
|
|
|
|
} else { // append to existing
|
|
|
|
best, err = m.storage.findSector(m.minerID(), sn, sectorbuilder.FTUnsealed)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("finding sector path: %w", err)
|
2020-03-05 00:38:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:18:33 +00:00
|
|
|
candidateWorkers, _ := m.getWorkersByPaths(sealmgr.TTAddPiece, best)
|
|
|
|
|
2020-03-05 00:38:07 +00:00
|
|
|
if len(candidateWorkers) == 0 {
|
2020-03-05 01:12:52 +00:00
|
|
|
return abi.PieceInfo{}, xerrors.New("no worker found")
|
2020-03-05 00:38:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:18:33 +00:00
|
|
|
// TODO: select(candidateWorkers, ...)
|
|
|
|
// TODO: remove the sectorbuilder abstraction, pass path directly
|
2020-03-06 05:30:47 +00:00
|
|
|
return candidateWorkers[0].AddPiece(ctx, sn, existingPieces, sz, r)
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 05:30:47 +00:00
|
|
|
func (m *Manager) SealPreCommit1(ctx context.Context, sectorNum abi.SectorNumber, ticket abi.SealRandomness, pieces []abi.PieceInfo) (out storage2.PreCommit1Out, err error) {
|
2020-03-05 18:18:33 +00:00
|
|
|
// TODO: also consider where the unsealed data sits
|
|
|
|
|
2020-03-05 19:21:06 +00:00
|
|
|
best, err := m.storage.findBestAllocStorage(sectorbuilder.FTCache|sectorbuilder.FTSealed, true)
|
2020-03-05 18:18:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("finding path for sector sealing: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
candidateWorkers, _ := m.getWorkersByPaths(sealmgr.TTPreCommit1, best)
|
|
|
|
|
|
|
|
// TODO: select(candidateWorkers, ...)
|
|
|
|
// TODO: remove the sectorbuilder abstraction, pass path directly
|
|
|
|
return candidateWorkers[0].SealPreCommit1(ctx, sectorNum, ticket, pieces)
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 05:30:47 +00:00
|
|
|
func (m *Manager) SealPreCommit2(ctx context.Context, sectorNum abi.SectorNumber, phase1Out storage2.PreCommit1Out) (sealedCID cid.Cid, unsealedCID cid.Cid, err error) {
|
2020-03-05 18:18:33 +00:00
|
|
|
// TODO: allow workers to fetch the sectors
|
|
|
|
|
2020-03-05 19:21:06 +00:00
|
|
|
best, err := m.storage.findSector(m.minerID(), sectorNum, sectorbuilder.FTCache|sectorbuilder.FTSealed)
|
2020-03-05 18:18:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("finding path for sector sealing: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
candidateWorkers, _ := m.getWorkersByPaths(sealmgr.TTPreCommit2, best)
|
|
|
|
|
|
|
|
// TODO: select(candidateWorkers, ...)
|
|
|
|
// TODO: remove the sectorbuilder abstraction, pass path directly
|
|
|
|
return candidateWorkers[0].SealPreCommit2(ctx, sectorNum, phase1Out)
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 05:30:47 +00:00
|
|
|
func (m *Manager) SealCommit1(ctx context.Context, sectorNum abi.SectorNumber, ticket abi.SealRandomness, seed abi.InteractiveSealRandomness, pieces []abi.PieceInfo, sealedCID cid.Cid, unsealedCID cid.Cid) (output storage2.Commit1Out, err error) {
|
2020-03-05 19:21:06 +00:00
|
|
|
best, err := m.storage.findSector(m.minerID(), sectorNum, sectorbuilder.FTCache|sectorbuilder.FTSealed)
|
2020-03-05 18:18:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("finding path for sector sealing: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
candidateWorkers, _ := m.getWorkersByPaths(sealmgr.TTPreCommit2, best)
|
|
|
|
|
|
|
|
// TODO: select(candidateWorkers, ...)
|
|
|
|
// TODO: remove the sectorbuilder abstraction, pass path directly
|
|
|
|
return candidateWorkers[0].SealCommit1(ctx, sectorNum, ticket, seed, pieces, sealedCID, unsealedCID)
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 05:30:47 +00:00
|
|
|
func (m *Manager) SealCommit2(ctx context.Context, sectorNum abi.SectorNumber, phase1Out storage2.Commit1Out) (proof storage2.Proof, err error) {
|
2020-03-05 18:18:33 +00:00
|
|
|
for _, worker := range m.workers {
|
|
|
|
if _, ok := worker.TaskTypes()[sealmgr.TTCommit2]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return worker.SealCommit2(ctx, sectorNum, phase1Out)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, xerrors.New("no worker found")
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:18:33 +00:00
|
|
|
func (m *Manager) FinalizeSector(ctx context.Context, sectorNum abi.SectorNumber) error {
|
2020-03-05 19:21:06 +00:00
|
|
|
best, err := m.storage.findSector(m.minerID(), sectorNum, sectorbuilder.FTCache|sectorbuilder.FTSealed|sectorbuilder.FTUnsealed)
|
2020-03-05 18:18:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("finding sealed sector: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
candidateWorkers, _ := m.getWorkersByPaths(sealmgr.TTPreCommit2, best) // find last worker with the sector
|
|
|
|
|
|
|
|
// TODO: Move the sector to long-term storage
|
|
|
|
return candidateWorkers[0].FinalizeSector(ctx, sectorNum)
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 00:38:07 +00:00
|
|
|
func (m *Manager) minerID() abi.ActorID {
|
|
|
|
mid, err := address.IDFromAddress(m.scfg.Miner)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return abi.ActorID(mid)
|
|
|
|
}
|
|
|
|
|
2020-03-03 22:19:22 +00:00
|
|
|
var _ sealmgr.Manager = &Manager{}
|