2020-01-15 20:49:11 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-02-23 00:47:47 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-01-15 20:49:11 +00:00
|
|
|
)
|
|
|
|
|
2020-02-23 15:50:36 +00:00
|
|
|
type nullReader struct{}
|
2020-01-25 11:15:28 +00:00
|
|
|
|
2020-02-23 00:47:47 +00:00
|
|
|
func (nullReader) Read(out []byte) (int, error) {
|
|
|
|
for i := range out {
|
|
|
|
out[i] = 0
|
2020-01-25 11:15:28 +00:00
|
|
|
}
|
2020-02-23 00:47:47 +00:00
|
|
|
return len(out), nil
|
|
|
|
}
|
2020-01-25 11:15:28 +00:00
|
|
|
|
2020-02-23 00:47:47 +00:00
|
|
|
func (m *Sealing) pledgeReader(size abi.UnpaddedPieceSize) io.Reader {
|
|
|
|
return io.LimitReader(&nullReader{}, int64(size))
|
2020-01-25 11:15:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func (m *Sealing) pledgeSector(ctx context.Context, sectorID abi.SectorNumber, existingPieceSizes []abi.UnpaddedPieceSize, sizes ...abi.UnpaddedPieceSize) ([]Piece, error) {
|
2020-01-15 20:49:11 +00:00
|
|
|
if len(sizes) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-01-31 19:22:31 +00:00
|
|
|
log.Infof("Pledge %d, contains %+v", sectorID, existingPieceSizes)
|
|
|
|
|
|
|
|
out := make([]Piece, len(sizes))
|
2020-01-15 20:49:11 +00:00
|
|
|
for i, size := range sizes {
|
2020-02-23 00:47:47 +00:00
|
|
|
ppi, err := m.sb.AddPiece(ctx, size, sectorID, m.pledgeReader(size), existingPieceSizes)
|
2020-01-15 20:49:11 +00:00
|
|
|
if err != nil {
|
2020-01-28 23:08:02 +00:00
|
|
|
return nil, xerrors.Errorf("add piece: %w", err)
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
existingPieceSizes = append(existingPieceSizes, size)
|
|
|
|
|
|
|
|
out[i] = Piece{
|
2020-02-27 00:42:39 +00:00
|
|
|
Size: ppi.Size.Unpadded(),
|
|
|
|
CommP: ppi.PieceCID,
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) PledgeSector() error {
|
|
|
|
go func() {
|
|
|
|
ctx := context.TODO() // we can't use the context from command which invokes
|
|
|
|
// this, as we run everything here async, and it's cancelled when the
|
|
|
|
// command exits
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
size := abi.PaddedPieceSize(m.sb.SectorSize()).Unpadded()
|
2020-01-15 20:49:11 +00:00
|
|
|
|
2020-02-11 01:10:50 +00:00
|
|
|
sid, err := m.sb.AcquireSectorNumber()
|
2020-01-15 20:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-22 13:10:46 +00:00
|
|
|
pieces, err := m.pledgeSector(ctx, sid, []abi.UnpaddedPieceSize{}, size)
|
2020-01-15 20:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-23 00:47:47 +00:00
|
|
|
if err := m.newSector(sid, pieces); err != nil {
|
2020-01-15 20:49:11 +00:00
|
|
|
log.Errorf("%+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|