2020-03-26 02:50:56 +00:00
|
|
|
//+build cgo
|
|
|
|
|
|
|
|
package ffiwrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2020-05-18 22:08:11 +00:00
|
|
|
"io/ioutil"
|
2020-03-26 02:50:56 +00:00
|
|
|
"math/bits"
|
|
|
|
"os"
|
2020-05-14 15:35:38 +00:00
|
|
|
"path/filepath"
|
2020-05-18 22:08:11 +00:00
|
|
|
"syscall"
|
2020-03-26 02:50:56 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
ffi "github.com/filecoin-project/filecoin-ffi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-storage/storage"
|
|
|
|
|
2020-03-27 23:21:36 +00:00
|
|
|
"github.com/filecoin-project/sector-storage/stores"
|
2020-05-18 22:08:11 +00:00
|
|
|
"github.com/filecoin-project/sector-storage/storiface"
|
2020-03-27 23:21:36 +00:00
|
|
|
"github.com/filecoin-project/sector-storage/zerocomm"
|
2020-03-26 02:50:56 +00:00
|
|
|
)
|
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
var _ Storage = &Sealer{}
|
2020-03-26 02:50:56 +00:00
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
func New(sectors SectorProvider, cfg *Config) (*Sealer, error) {
|
2020-03-26 02:50:56 +00:00
|
|
|
sectorSize, err := sizeFromConfig(*cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
sb := &Sealer{
|
2020-03-26 02:50:56 +00:00
|
|
|
sealProofType: cfg.SealProofType,
|
|
|
|
ssize: sectorSize,
|
|
|
|
|
|
|
|
sectors: sectors,
|
|
|
|
|
|
|
|
stopping: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb, nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
func (sb *Sealer) NewSector(ctx context.Context, sector abi.SectorID) error {
|
2020-03-26 02:50:56 +00:00
|
|
|
// TODO: Allocate the sector here instead of in addpiece
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
func (sb *Sealer) AddPiece(ctx context.Context, sector abi.SectorID, existingPieceSizes []abi.UnpaddedPieceSize, pieceSize abi.UnpaddedPieceSize, file storage.Data) (abi.PieceInfo, error) {
|
2020-05-14 15:35:38 +00:00
|
|
|
var offset abi.UnpaddedPieceSize
|
|
|
|
for _, size := range existingPieceSizes {
|
|
|
|
offset += size
|
|
|
|
}
|
|
|
|
|
|
|
|
maxPieceSize := abi.PaddedPieceSize(sb.ssize).Unpadded()
|
|
|
|
|
2020-05-18 22:08:11 +00:00
|
|
|
if offset+pieceSize > maxPieceSize {
|
2020-05-14 15:35:38 +00:00
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("can't add %d byte piece to sector %v with %d bytes of existing pieces", pieceSize, sector, offset)
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 15:35:38 +00:00
|
|
|
var err error
|
2020-03-26 02:50:56 +00:00
|
|
|
var done func()
|
2020-05-14 15:35:38 +00:00
|
|
|
var stagedFile *partialFile
|
2020-03-26 02:50:56 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if done != nil {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
|
|
|
|
if stagedFile != nil {
|
|
|
|
if err := stagedFile.Close(); err != nil {
|
|
|
|
log.Errorf("closing staged file: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var stagedPath stores.SectorPaths
|
|
|
|
if len(existingPieceSizes) == 0 {
|
|
|
|
stagedPath, done, err = sb.sectors.AcquireSector(ctx, sector, 0, stores.FTUnsealed, true)
|
|
|
|
if err != nil {
|
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("acquire unsealed sector: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:35:38 +00:00
|
|
|
stagedFile, err = createPartialFile(maxPieceSize, stagedPath.Unsealed)
|
2020-03-26 02:50:56 +00:00
|
|
|
if err != nil {
|
2020-05-14 15:35:38 +00:00
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("creating unsealed sector file: %w", err)
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stagedPath, done, err = sb.sectors.AcquireSector(ctx, sector, stores.FTUnsealed, 0, true)
|
|
|
|
if err != nil {
|
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("acquire unsealed sector: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:35:38 +00:00
|
|
|
stagedFile, err = openPartialFile(maxPieceSize, stagedPath.Unsealed)
|
2020-03-26 02:50:56 +00:00
|
|
|
if err != nil {
|
2020-05-14 15:35:38 +00:00
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("opening unsealed sector file: %w", err)
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
2020-05-14 15:35:38 +00:00
|
|
|
}
|
2020-03-26 02:50:56 +00:00
|
|
|
|
2020-05-18 22:08:11 +00:00
|
|
|
w, err := stagedFile.Writer(UnpaddedByteIndex(offset), pieceSize)
|
2020-05-14 15:35:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("getting partial file writer: %w", err)
|
|
|
|
}
|
|
|
|
pr := io.TeeReader(io.LimitReader(file, int64(pieceSize)), w)
|
|
|
|
prf, werr, err := toReadableFile(pr, int64(pieceSize))
|
|
|
|
if err != nil {
|
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("getting tee reader pipe: %w", err)
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 15:35:38 +00:00
|
|
|
pieceCID, err := ffi.GeneratePieceCIDFromFile(sb.sealProofType, prf, pieceSize)
|
2020-03-26 02:50:56 +00:00
|
|
|
if err != nil {
|
2020-05-14 15:35:38 +00:00
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("generating piece commitment: %w", err)
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 22:08:11 +00:00
|
|
|
if err := stagedFile.MarkAllocated(UnpaddedByteIndex(offset), pieceSize); err != nil {
|
2020-05-14 15:35:38 +00:00
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("marking data range as allocated: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := stagedFile.Close(); err != nil {
|
2020-03-26 02:50:56 +00:00
|
|
|
return abi.PieceInfo{}, err
|
|
|
|
}
|
2020-05-14 15:35:38 +00:00
|
|
|
stagedFile = nil
|
2020-03-26 02:50:56 +00:00
|
|
|
|
|
|
|
return abi.PieceInfo{
|
|
|
|
Size: pieceSize.Padded(),
|
|
|
|
PieceCID: pieceCID,
|
|
|
|
}, werr()
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:22:58 +00:00
|
|
|
type closerFunc func() error
|
|
|
|
|
|
|
|
func (cf closerFunc) Close() error {
|
|
|
|
return cf()
|
|
|
|
}
|
|
|
|
|
2020-05-18 23:03:42 +00:00
|
|
|
func (sb *Sealer) UnsealPiece(ctx context.Context, sector abi.SectorID, offset UnpaddedByteIndex, size abi.UnpaddedPieceSize, randomness abi.SealRandomness, commd cid.Cid) error {
|
2020-05-18 22:08:11 +00:00
|
|
|
maxPieceSize := abi.PaddedPieceSize(sb.ssize).Unpadded()
|
|
|
|
|
|
|
|
// try finding existing
|
|
|
|
unsealedPath, done, err := sb.sectors.AcquireSector(ctx, sector, stores.FTUnsealed, stores.FTNone, false)
|
|
|
|
var pf *partialFile
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case xerrors.Is(err, storiface.ErrSectorNotFound):
|
|
|
|
unsealedPath, done, err = sb.sectors.AcquireSector(ctx, sector, stores.FTNone, stores.FTUnsealed, false)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("acquire unsealed sector path (allocate): %w", err)
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
pf, err = createPartialFile(maxPieceSize, unsealedPath.Unsealed)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("create unsealed file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case err == nil:
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
pf, err = openPartialFile(maxPieceSize, unsealedPath.Unsealed)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening partial file: %w", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return xerrors.Errorf("acquire unsealed sector path (existing): %w", err)
|
|
|
|
}
|
|
|
|
defer pf.Close()
|
|
|
|
|
|
|
|
allocated, err := pf.Allocated()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting bitruns of allocated data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
toUnseal, err := computeUnsealRanges(allocated, offset, size)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("computing unseal ranges: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !toUnseal.HasNext() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
srcPaths, srcDone, err := sb.sectors.AcquireSector(ctx, sector, stores.FTCache|stores.FTSealed, stores.FTNone, false)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("acquire sealed sector paths: %w", err)
|
|
|
|
}
|
|
|
|
defer srcDone()
|
|
|
|
|
|
|
|
var at, nextat uint64
|
|
|
|
for {
|
|
|
|
piece, err := toUnseal.NextRun()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting next range to unseal: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
at = nextat
|
|
|
|
nextat += piece.Len
|
|
|
|
|
|
|
|
if !piece.Val {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := pf.Writer(offset, size)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting partial file writer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// <eww>
|
|
|
|
outpipe, err := ioutil.TempFile(os.TempDir(), "sector-storage-unseal-")
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("creating temp pipe file: %w", err)
|
|
|
|
}
|
|
|
|
var outpath string
|
|
|
|
var perr error
|
|
|
|
outWait := make(chan struct{})
|
|
|
|
|
|
|
|
{
|
|
|
|
outpath = outpipe.Name()
|
|
|
|
if err := outpipe.Close(); err != nil {
|
|
|
|
return xerrors.Errorf("close pipe temp: %w", err)
|
|
|
|
}
|
|
|
|
if err := os.Remove(outpath); err != nil {
|
|
|
|
return xerrors.Errorf("rm pipe temp: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Make UnsealRange write to an FD
|
|
|
|
if err := syscall.Mkfifo(outpath, 0600); err != nil {
|
|
|
|
return xerrors.Errorf("mk temp fifo: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(outWait)
|
|
|
|
defer os.Remove(outpath)
|
2020-05-18 23:03:42 +00:00
|
|
|
|
|
|
|
outpipe, err = os.OpenFile(outpath, os.O_RDONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
perr = xerrors.Errorf("open temp pipe: %w", err)
|
|
|
|
return
|
|
|
|
}
|
2020-05-18 22:08:11 +00:00
|
|
|
defer outpipe.Close()
|
|
|
|
|
|
|
|
_, perr = io.CopyN(out, outpipe, int64(size))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
// </eww>
|
|
|
|
|
|
|
|
// TODO: This may be possible to do in parallel
|
|
|
|
err = ffi.UnsealRange(sb.sealProofType,
|
|
|
|
srcPaths.Cache,
|
|
|
|
srcPaths.Sealed,
|
|
|
|
outpath,
|
|
|
|
sector.Number,
|
|
|
|
sector.Miner,
|
|
|
|
randomness,
|
2020-05-18 23:03:42 +00:00
|
|
|
commd,
|
2020-05-18 22:08:11 +00:00
|
|
|
at,
|
|
|
|
piece.Len)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("unseal range: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-outWait:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
if perr != nil {
|
|
|
|
return xerrors.Errorf("piping output to unsealed file: %w", perr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pf.MarkAllocated(UnpaddedByteIndex(at), abi.UnpaddedPieceSize(piece.Len)); err != nil {
|
|
|
|
return xerrors.Errorf("marking unsealed range as allocated: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !toUnseal.HasNext() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *Sealer) ReadPiece(ctx context.Context, writer io.Writer, sector abi.SectorID, offset UnpaddedByteIndex, size abi.UnpaddedPieceSize) error {
|
|
|
|
path, done, err := sb.sectors.AcquireSector(ctx, sector, stores.FTUnsealed, stores.FTNone, false)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("acquire unsealed sector path: %w", err)
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
maxPieceSize := abi.PaddedPieceSize(sb.ssize).Unpadded()
|
|
|
|
|
|
|
|
pf, err := openPartialFile(maxPieceSize, path.Unsealed)
|
|
|
|
if xerrors.Is(err, os.ErrNotExist) {
|
|
|
|
return xerrors.Errorf("opening partial file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := pf.Reader(offset, size)
|
|
|
|
if err != nil {
|
|
|
|
pf.Close()
|
|
|
|
return xerrors.Errorf("getting partial file reader: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.CopyN(writer, f, int64(size)); err != nil {
|
|
|
|
pf.Close()
|
|
|
|
return xerrors.Errorf("reading unsealed file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pf.Close(); err != nil {
|
|
|
|
return xerrors.Errorf("closing partial file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
func (sb *Sealer) ReadPieceFromSealedSector(ctx context.Context, sector abi.SectorID, offset UnpaddedByteIndex, size abi.UnpaddedPieceSize, ticket abi.SealRandomness, unsealedCID cid.Cid) (io.ReadCloser, error) {
|
2020-05-07 22:22:58 +00:00
|
|
|
{
|
|
|
|
path, doneUnsealed, err := sb.sectors.AcquireSector(ctx, sector, stores.FTUnsealed, stores.FTNone, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("acquire unsealed sector path: %w", err)
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 22:22:58 +00:00
|
|
|
f, err := os.OpenFile(path.Unsealed, os.O_RDONLY, 0644)
|
|
|
|
if err == nil {
|
|
|
|
if _, err := f.Seek(int64(offset), io.SeekStart); err != nil {
|
|
|
|
doneUnsealed()
|
|
|
|
return nil, xerrors.Errorf("seek: %w", err)
|
|
|
|
}
|
2020-03-26 02:50:56 +00:00
|
|
|
|
2020-05-07 22:22:58 +00:00
|
|
|
lr := io.LimitReader(f, int64(size))
|
|
|
|
|
|
|
|
return &struct {
|
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
}{
|
|
|
|
Reader: lr,
|
|
|
|
Closer: closerFunc(func() error {
|
|
|
|
doneUnsealed()
|
|
|
|
return f.Close()
|
|
|
|
}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
doneUnsealed()
|
|
|
|
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 22:22:58 +00:00
|
|
|
paths, doneSealed, err := sb.sectors.AcquireSector(ctx, sector, stores.FTSealed|stores.FTCache, stores.FTUnsealed, false)
|
2020-03-26 02:50:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("acquire sealed/cache sector path: %w", err)
|
|
|
|
}
|
|
|
|
defer doneSealed()
|
|
|
|
|
|
|
|
// TODO: GC for those
|
|
|
|
// (Probably configurable count of sectors to be kept unsealed, and just
|
|
|
|
// remove last used one (or use whatever other cache policy makes sense))
|
|
|
|
err = ffi.Unseal(
|
|
|
|
sb.sealProofType,
|
2020-05-07 22:22:58 +00:00
|
|
|
paths.Cache,
|
|
|
|
paths.Sealed,
|
|
|
|
paths.Unsealed,
|
2020-03-26 02:50:56 +00:00
|
|
|
sector.Number,
|
|
|
|
sector.Miner,
|
|
|
|
ticket,
|
|
|
|
unsealedCID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("unseal failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:22:58 +00:00
|
|
|
f, err := os.OpenFile(paths.Unsealed, os.O_RDONLY, 0644)
|
2020-03-26 02:50:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := f.Seek(int64(offset), io.SeekStart); err != nil {
|
|
|
|
return nil, xerrors.Errorf("seek: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lr := io.LimitReader(f, int64(size))
|
|
|
|
|
|
|
|
return &struct {
|
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
}{
|
|
|
|
Reader: lr,
|
|
|
|
Closer: f,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
func (sb *Sealer) SealPreCommit1(ctx context.Context, sector abi.SectorID, ticket abi.SealRandomness, pieces []abi.PieceInfo) (out storage.PreCommit1Out, err error) {
|
2020-03-26 02:50:56 +00:00
|
|
|
paths, done, err := sb.sectors.AcquireSector(ctx, sector, stores.FTUnsealed, stores.FTSealed|stores.FTCache, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("acquiring sector paths: %w", err)
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
e, err := os.OpenFile(paths.Sealed, os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("ensuring sealed file exists: %w", err)
|
|
|
|
}
|
|
|
|
if err := e.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Mkdir(paths.Cache, 0755); err != nil {
|
|
|
|
if os.IsExist(err) {
|
|
|
|
log.Warnf("existing cache in %s; removing", paths.Cache)
|
|
|
|
|
|
|
|
if err := os.RemoveAll(paths.Cache); err != nil {
|
|
|
|
return nil, xerrors.Errorf("remove existing sector cache from %s (sector %d): %w", paths.Cache, sector, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Mkdir(paths.Cache, 0755); err != nil {
|
|
|
|
return nil, xerrors.Errorf("mkdir cache path after cleanup: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sum abi.UnpaddedPieceSize
|
|
|
|
for _, piece := range pieces {
|
|
|
|
sum += piece.Size.Unpadded()
|
|
|
|
}
|
|
|
|
ussize := abi.PaddedPieceSize(sb.ssize).Unpadded()
|
|
|
|
if sum != ussize {
|
|
|
|
return nil, xerrors.Errorf("aggregated piece sizes don't match sector size: %d != %d (%d)", sum, ussize, int64(ussize-sum))
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:35:38 +00:00
|
|
|
staged := filepath.Join(paths.Cache, "staged")
|
|
|
|
|
|
|
|
if err := sb.rewriteAsPadded(paths.Unsealed, staged); err != nil {
|
|
|
|
return nil, xerrors.Errorf("rewriting sector as padded: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := os.Remove(staged); err != nil {
|
|
|
|
log.Warnf("Removing staged sector file(%v): %s", sector, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-03-26 02:50:56 +00:00
|
|
|
// TODO: context cancellation respect
|
|
|
|
p1o, err := ffi.SealPreCommitPhase1(
|
|
|
|
sb.sealProofType,
|
|
|
|
paths.Cache,
|
2020-05-14 15:35:38 +00:00
|
|
|
staged,
|
2020-03-26 02:50:56 +00:00
|
|
|
paths.Sealed,
|
|
|
|
sector.Number,
|
|
|
|
sector.Miner,
|
|
|
|
ticket,
|
|
|
|
pieces,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("presealing sector %d (%s): %w", sector.Number, paths.Unsealed, err)
|
|
|
|
}
|
|
|
|
return p1o, nil
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:35:38 +00:00
|
|
|
func (sb *Sealer) rewriteAsPadded(unsealed string, staged string) error {
|
|
|
|
maxPieceSize := abi.PaddedPieceSize(sb.ssize).Unpadded()
|
|
|
|
|
|
|
|
pf, err := openPartialFile(maxPieceSize, unsealed)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening unsealed file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
upr, err := pf.Reader(0, maxPieceSize)
|
|
|
|
if err != nil {
|
|
|
|
pf.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := os.OpenFile(staged, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
pf.Close()
|
|
|
|
return xerrors.Errorf("openning staged file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OPTIMIZATION: upr is a file, so it could be passed straight to
|
|
|
|
// WriteWithAlignment IF it wouldn't care about the trailer
|
|
|
|
lupr, werr, err := toReadableFile(io.LimitReader(upr, int64(maxPieceSize)), int64(maxPieceSize))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, _, err = ffi.WriteWithAlignment(sb.sealProofType, lupr, maxPieceSize, st, nil)
|
|
|
|
if err != nil {
|
|
|
|
pf.Close()
|
|
|
|
st.Close()
|
|
|
|
return xerrors.Errorf("write with alignment: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := st.Close(); err != nil {
|
|
|
|
pf.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pf.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return werr()
|
|
|
|
}
|
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
func (sb *Sealer) SealPreCommit2(ctx context.Context, sector abi.SectorID, phase1Out storage.PreCommit1Out) (storage.SectorCids, error) {
|
2020-03-26 02:50:56 +00:00
|
|
|
paths, done, err := sb.sectors.AcquireSector(ctx, sector, stores.FTSealed|stores.FTCache, 0, true)
|
|
|
|
if err != nil {
|
|
|
|
return storage.SectorCids{}, xerrors.Errorf("acquiring sector paths: %w", err)
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
sealedCID, unsealedCID, err := ffi.SealPreCommitPhase2(phase1Out, paths.Cache, paths.Sealed)
|
|
|
|
if err != nil {
|
|
|
|
return storage.SectorCids{}, xerrors.Errorf("presealing sector %d (%s): %w", sector.Number, paths.Unsealed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage.SectorCids{
|
|
|
|
Unsealed: unsealedCID,
|
|
|
|
Sealed: sealedCID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
func (sb *Sealer) SealCommit1(ctx context.Context, sector abi.SectorID, ticket abi.SealRandomness, seed abi.InteractiveSealRandomness, pieces []abi.PieceInfo, cids storage.SectorCids) (storage.Commit1Out, error) {
|
2020-03-26 02:50:56 +00:00
|
|
|
paths, done, err := sb.sectors.AcquireSector(ctx, sector, stores.FTSealed|stores.FTCache, 0, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("acquire sector paths: %w", err)
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
output, err := ffi.SealCommitPhase1(
|
|
|
|
sb.sealProofType,
|
|
|
|
cids.Sealed,
|
|
|
|
cids.Unsealed,
|
|
|
|
paths.Cache,
|
|
|
|
paths.Sealed,
|
|
|
|
sector.Number,
|
|
|
|
sector.Miner,
|
|
|
|
ticket,
|
|
|
|
seed,
|
|
|
|
pieces,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("StandaloneSealCommit error: ", err)
|
|
|
|
log.Warnf("num:%d tkt:%v seed:%v, pi:%v sealedCID:%v, unsealedCID:%v", sector.Number, ticket, seed, pieces, cids.Sealed, cids.Unsealed)
|
|
|
|
|
|
|
|
return nil, xerrors.Errorf("StandaloneSealCommit: %w", err)
|
|
|
|
}
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
func (sb *Sealer) SealCommit2(ctx context.Context, sector abi.SectorID, phase1Out storage.Commit1Out) (storage.Proof, error) {
|
2020-03-26 02:50:56 +00:00
|
|
|
return ffi.SealCommitPhase2(phase1Out, sector.Number, sector.Miner)
|
|
|
|
}
|
|
|
|
|
2020-03-26 19:34:38 +00:00
|
|
|
func (sb *Sealer) FinalizeSector(ctx context.Context, sector abi.SectorID) error {
|
2020-03-26 02:50:56 +00:00
|
|
|
paths, done, err := sb.sectors.AcquireSector(ctx, sector, stores.FTCache, 0, false)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("acquiring sector cache path: %w", err)
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
2020-04-10 18:41:59 +00:00
|
|
|
return ffi.ClearCache(uint64(sb.ssize), paths.Cache)
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GeneratePieceCIDFromFile(proofType abi.RegisteredProof, piece io.Reader, pieceSize abi.UnpaddedPieceSize) (cid.Cid, error) {
|
|
|
|
f, werr, err := toReadableFile(piece, int64(pieceSize))
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pieceCID, err := ffi.GeneratePieceCIDFromFile(proofType, f, pieceSize)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pieceCID, werr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateUnsealedCID(proofType abi.RegisteredProof, pieces []abi.PieceInfo) (cid.Cid, error) {
|
|
|
|
var sum abi.PaddedPieceSize
|
|
|
|
for _, p := range pieces {
|
|
|
|
sum += p.Size
|
|
|
|
}
|
|
|
|
|
2020-04-10 21:01:35 +00:00
|
|
|
ssize, err := proofType.SectorSize()
|
2020-03-26 02:50:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// pad remaining space with 0 CommPs
|
|
|
|
toFill := uint64(abi.PaddedPieceSize(ssize) - sum)
|
|
|
|
n := bits.OnesCount64(toFill)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
next := bits.TrailingZeros64(toFill)
|
|
|
|
psize := uint64(1) << uint(next)
|
|
|
|
toFill ^= psize
|
|
|
|
|
|
|
|
unpadded := abi.PaddedPieceSize(psize).Unpadded()
|
|
|
|
pieces = append(pieces, abi.PieceInfo{
|
|
|
|
Size: unpadded.Padded(),
|
|
|
|
PieceCID: zerocomm.ZeroPieceCommitment(unpadded),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ffi.GenerateUnsealedCID(proofType, pieces)
|
|
|
|
}
|