package sectorbuilder import ( "fmt" "io/ioutil" "os" "path/filepath" "strconv" "sync/atomic" ffi "github.com/filecoin-project/filecoin-ffi" "github.com/filecoin-project/go-address" datastore "github.com/ipfs/go-datastore" logging "github.com/ipfs/go-log" dcopy "github.com/otiai10/copy" "golang.org/x/xerrors" ) const PoStReservedWorkers = 1 const PoRepProofPartitions = 10 var lastSectorIdKey = datastore.NewKey("/last") var log = logging.Logger("sectorbuilder") func (rspco *RawSealPreCommitOutput) ToJson() JsonRSPCO { return JsonRSPCO{ CommD: rspco.CommD[:], CommR: rspco.CommR[:], } } func (rspco *JsonRSPCO) rspco() RawSealPreCommitOutput { var out RawSealPreCommitOutput copy(out.CommD[:], rspco.CommD) copy(out.CommR[:], rspco.CommR) return out } type Config struct { SectorSize uint64 Miner address.Address WorkerThreads uint8 FallbackLastID uint64 NoCommit bool NoPreCommit bool Dir string _ struct{} // guard against nameless init } func New(cfg *Config, ds datastore.Batching) (*SectorBuilder, error) { if cfg.WorkerThreads < PoStReservedWorkers { return nil, xerrors.Errorf("minimum worker threads is %d, specified %d", PoStReservedWorkers, cfg.WorkerThreads) } var lastUsedID uint64 b, err := ds.Get(lastSectorIdKey) switch err { case nil: i, err := strconv.ParseInt(string(b), 10, 64) if err != nil { return nil, err } lastUsedID = uint64(i) case datastore.ErrNotFound: lastUsedID = cfg.FallbackLastID default: return nil, err } rlimit := cfg.WorkerThreads - PoStReservedWorkers sealLocal := rlimit > 0 if rlimit == 0 { rlimit = 1 } sb := &SectorBuilder{ ds: ds, ssize: cfg.SectorSize, lastID: lastUsedID, filesystem: openFs(cfg.Dir), Miner: cfg.Miner, noPreCommit: cfg.NoPreCommit || !sealLocal, noCommit: cfg.NoCommit || !sealLocal, rateLimit: make(chan struct{}, rlimit), taskCtr: 1, precommitTasks: make(chan workerCall), commitTasks: make(chan workerCall), remoteResults: map[uint64]chan<- SealRes{}, remotes: map[int]*remote{}, stopping: make(chan struct{}), } if err := sb.filesystem.init(); err != nil { return nil, xerrors.Errorf("initializing sectorbuilder filesystem: %w", err) } return sb, nil } func NewStandalone(cfg *Config) (*SectorBuilder, error) { sb := &SectorBuilder{ ds: nil, ssize: cfg.SectorSize, Miner: cfg.Miner, filesystem: openFs(cfg.Dir), taskCtr: 1, remotes: map[int]*remote{}, rateLimit: make(chan struct{}, cfg.WorkerThreads), stopping: make(chan struct{}), } if err := sb.filesystem.init(); err != nil { return nil, xerrors.Errorf("initializing sectorbuilder filesystem: %w", err) } return sb, nil } func (sb *SectorBuilder) checkRateLimit() { if cap(sb.rateLimit) == len(sb.rateLimit) { log.Warn("rate-limiting local sectorbuilder call") } } func (sb *SectorBuilder) RateLimit() func() { sb.checkRateLimit() sb.rateLimit <- struct{}{} return func() { <-sb.rateLimit } } type WorkerStats struct { LocalFree int LocalReserved int LocalTotal int // todo: post in progress RemotesTotal int RemotesFree int AddPieceWait int PreCommitWait int CommitWait int UnsealWait int } func (sb *SectorBuilder) WorkerStats() WorkerStats { sb.remoteLk.Lock() defer sb.remoteLk.Unlock() remoteFree := len(sb.remotes) for _, r := range sb.remotes { if r.busy > 0 { remoteFree-- } } return WorkerStats{ LocalFree: cap(sb.rateLimit) - len(sb.rateLimit), LocalReserved: PoStReservedWorkers, LocalTotal: cap(sb.rateLimit) + PoStReservedWorkers, RemotesTotal: len(sb.remotes), RemotesFree: remoteFree, AddPieceWait: int(atomic.LoadInt32(&sb.addPieceWait)), PreCommitWait: int(atomic.LoadInt32(&sb.preCommitWait)), CommitWait: int(atomic.LoadInt32(&sb.commitWait)), UnsealWait: int(atomic.LoadInt32(&sb.unsealWait)), } } func addressToProverID(a address.Address) [32]byte { var proverId [32]byte copy(proverId[:], a.Payload()) return proverId } func (sb *SectorBuilder) AcquireSectorId() (uint64, error) { sb.idLk.Lock() defer sb.idLk.Unlock() sb.lastID++ id := sb.lastID err := sb.ds.Put(lastSectorIdKey, []byte(fmt.Sprint(id))) if err != nil { return 0, err } return id, nil } func (sb *SectorBuilder) sealPreCommitRemote(call workerCall) (RawSealPreCommitOutput, error) { atomic.AddInt32(&sb.preCommitWait, -1) select { case ret := <-call.ret: var err error if ret.Err != "" { err = xerrors.New(ret.Err) } return ret.Rspco.rspco(), err case <-sb.stopping: return RawSealPreCommitOutput{}, xerrors.New("sectorbuilder stopped") } } func (sb *SectorBuilder) sealCommitRemote(call workerCall) (proof []byte, err error) { atomic.AddInt32(&sb.commitWait, -1) select { case ret := <-call.ret: if ret.Err != "" { err = xerrors.New(ret.Err) } return ret.Proof, err case <-sb.stopping: return nil, xerrors.New("sectorbuilder stopped") } } func (sb *SectorBuilder) pubSectorToPriv(sectorInfo SortedPublicSectorInfo, faults []uint64) (SortedPrivateSectorInfo, error) { fmap := map[uint64]struct{}{} for _, fault := range faults { fmap[fault] = struct{}{} } var out []ffi.PrivateSectorInfo for _, s := range sectorInfo.Values() { if _, faulty := fmap[s.SectorID]; faulty { continue } cachePath, err := sb.sectorCacheDir(s.SectorID) if err != nil { return SortedPrivateSectorInfo{}, xerrors.Errorf("getting cache path for sector %d: %w", s.SectorID, err) } sealedPath, err := sb.SealedSectorPath(s.SectorID) if err != nil { return SortedPrivateSectorInfo{}, xerrors.Errorf("getting sealed path for sector %d: %w", s.SectorID, err) } out = append(out, ffi.PrivateSectorInfo{ SectorID: s.SectorID, CommR: s.CommR, CacheDirPath: cachePath, SealedSectorPath: sealedPath, }) } return ffi.NewSortedPrivateSectorInfo(out...), nil } func fallbackPostChallengeCount(sectors uint64, faults uint64) uint64 { challengeCount := ElectionPostChallengeCount(sectors, faults) if challengeCount > MaxFallbackPostChallengeCount { return MaxFallbackPostChallengeCount } return challengeCount } func (sb *SectorBuilder) ImportFrom(osb *SectorBuilder, symlink bool) error { if err := migrate(osb.filesystem.pathFor(dataCache), sb.filesystem.pathFor(dataCache), symlink); err != nil { return err } if err := migrate(osb.filesystem.pathFor(dataStaging), sb.filesystem.pathFor(dataStaging), symlink); err != nil { return err } if err := migrate(osb.filesystem.pathFor(dataSealed), sb.filesystem.pathFor(dataSealed), symlink); err != nil { return err } val, err := osb.ds.Get(lastSectorIdKey) if err != nil { if err == datastore.ErrNotFound { log.Warnf("CAUTION: last sector ID not found in previous datastore") return nil } return err } if err := sb.ds.Put(lastSectorIdKey, val); err != nil { return err } sb.lastID = osb.lastID return nil } func (sb *SectorBuilder) SetLastSectorID(id uint64) error { if err := sb.ds.Put(lastSectorIdKey, []byte(fmt.Sprint(id))); err != nil { return err } sb.lastID = id return nil } func migrate(from, to string, symlink bool) error { st, err := os.Stat(from) if err != nil { return err } if st.IsDir() { return migrateDir(from, to, symlink) } return migrateFile(from, to, symlink) } func migrateDir(from, to string, symlink bool) error { tost, err := os.Stat(to) if err != nil { if !os.IsNotExist(err) { return err } if err := os.MkdirAll(to, 0755); err != nil { return err } } else if !tost.IsDir() { return xerrors.Errorf("target %q already exists and is a file (expected directory)") } dirents, err := ioutil.ReadDir(from) if err != nil { return err } for _, inf := range dirents { n := inf.Name() if inf.IsDir() { if err := migrate(filepath.Join(from, n), filepath.Join(to, n), symlink); err != nil { return err } } else { if err := migrate(filepath.Join(from, n), filepath.Join(to, n), symlink); err != nil { return err } } } return nil } func migrateFile(from, to string, symlink bool) error { if symlink { return os.Symlink(from, to) } return dcopy.Copy(from, to) } func (sb *SectorBuilder) Stop() { close(sb.stopping) }