2024-03-15 21:38:13 +00:00
|
|
|
package seal
|
2023-12-18 17:29:10 +00:00
|
|
|
|
|
|
|
import (
|
2023-12-18 22:48:49 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-01-12 10:03:37 +00:00
|
|
|
|
2024-01-22 16:42:29 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2024-01-12 10:03:37 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2023-12-18 22:48:49 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2024-01-22 16:42:29 +00:00
|
|
|
"github.com/filecoin-project/go-commp-utils/nonffi"
|
2023-12-18 22:48:49 +00:00
|
|
|
"github.com/filecoin-project/go-commp-utils/zerocomm"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2024-01-12 10:03:37 +00:00
|
|
|
|
2023-12-19 11:50:27 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2023-12-18 22:48:49 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/policy"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2024-03-15 21:38:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/curiosrc/ffi"
|
2023-12-18 17:29:10 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
|
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/harmonytask"
|
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/resources"
|
2023-12-18 22:48:49 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2023-12-18 17:29:10 +00:00
|
|
|
)
|
|
|
|
|
2023-12-19 11:50:27 +00:00
|
|
|
var isDevnet = build.BlockDelaySecs < 30
|
|
|
|
|
2023-12-18 17:29:10 +00:00
|
|
|
type SDRAPI interface {
|
2023-12-18 22:48:49 +00:00
|
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
|
|
|
StateGetRandomnessFromTickets(context.Context, crypto.DomainSeparationTag, abi.ChainEpoch, []byte, types.TipSetKey) (abi.Randomness, error)
|
2023-12-18 17:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SDRTask struct {
|
|
|
|
api SDRAPI
|
|
|
|
db *harmonydb.DB
|
2023-12-18 22:48:49 +00:00
|
|
|
sp *SealPoller
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
sc *ffi.SealCalls
|
2023-12-18 17:29:10 +00:00
|
|
|
|
2023-12-20 10:28:06 +00:00
|
|
|
max int
|
2023-12-18 17:29:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
func NewSDRTask(api SDRAPI, db *harmonydb.DB, sp *SealPoller, sc *ffi.SealCalls, maxSDR int) *SDRTask {
|
2023-12-19 11:16:38 +00:00
|
|
|
return &SDRTask{
|
2023-12-20 10:28:06 +00:00
|
|
|
api: api,
|
|
|
|
db: db,
|
|
|
|
sp: sp,
|
|
|
|
sc: sc,
|
|
|
|
max: maxSDR,
|
2023-12-19 11:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 17:29:10 +00:00
|
|
|
func (s *SDRTask) Do(taskID harmonytask.TaskID, stillOwned func() bool) (done bool, err error) {
|
2023-12-18 22:48:49 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-12-19 11:50:27 +00:00
|
|
|
var sectorParamsArr []struct {
|
2023-12-18 22:48:49 +00:00
|
|
|
SpID int64 `db:"sp_id"`
|
|
|
|
SectorNumber int64 `db:"sector_number"`
|
|
|
|
RegSealProof abi.RegisteredSealProof `db:"reg_seal_proof"`
|
|
|
|
}
|
|
|
|
|
2023-12-19 11:50:27 +00:00
|
|
|
err = s.db.Select(ctx, §orParamsArr, `
|
2023-12-18 22:48:49 +00:00
|
|
|
SELECT sp_id, sector_number, reg_seal_proof
|
2023-12-19 10:20:18 +00:00
|
|
|
FROM sectors_sdr_pipeline
|
|
|
|
WHERE task_id_sdr = $1`, taskID)
|
2023-12-18 22:48:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("getting sector params: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-19 11:50:27 +00:00
|
|
|
if len(sectorParamsArr) != 1 {
|
|
|
|
return false, xerrors.Errorf("expected 1 sector params, got %d", len(sectorParamsArr))
|
|
|
|
}
|
|
|
|
sectorParams := sectorParamsArr[0]
|
|
|
|
|
2023-12-18 22:48:49 +00:00
|
|
|
var pieces []struct {
|
|
|
|
PieceIndex int64 `db:"piece_index"`
|
|
|
|
PieceCID string `db:"piece_cid"`
|
|
|
|
PieceSize int64 `db:"piece_size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.db.Select(ctx, &pieces, `
|
|
|
|
SELECT piece_index, piece_cid, piece_size
|
|
|
|
FROM sectors_sdr_initial_pieces
|
2024-02-21 13:19:48 +00:00
|
|
|
WHERE sp_id = $1 AND sector_number = $2 ORDER BY piece_index ASC`, sectorParams.SpID, sectorParams.SectorNumber)
|
2023-12-18 22:48:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("getting pieces: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize, err := sectorParams.RegSealProof.SectorSize()
|
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("getting sector size: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-01-22 16:33:59 +00:00
|
|
|
var commd cid.Cid
|
|
|
|
|
|
|
|
if len(pieces) > 0 {
|
|
|
|
pieceInfos := make([]abi.PieceInfo, len(pieces))
|
|
|
|
for i, p := range pieces {
|
|
|
|
c, err := cid.Parse(p.PieceCID)
|
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("parsing piece cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pieceInfos[i] = abi.PieceInfo{
|
|
|
|
Size: abi.PaddedPieceSize(p.PieceSize),
|
|
|
|
PieceCID: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commd, err = nonffi.GenerateUnsealedCID(sectorParams.RegSealProof, pieceInfos)
|
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("computing CommD: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
commd = zerocomm.ZeroPieceCommitment(abi.PaddedPieceSize(ssize).Unpadded())
|
|
|
|
}
|
2023-12-18 22:48:49 +00:00
|
|
|
|
|
|
|
sref := storiface.SectorRef{
|
|
|
|
ID: abi.SectorID{
|
|
|
|
Miner: abi.ActorID(sectorParams.SpID),
|
|
|
|
Number: abi.SectorNumber(sectorParams.SectorNumber),
|
|
|
|
},
|
|
|
|
ProofType: sectorParams.RegSealProof,
|
|
|
|
}
|
|
|
|
|
|
|
|
// get ticket
|
|
|
|
maddr, err := address.NewIDAddress(uint64(sectorParams.SpID))
|
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("getting miner address: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-01-19 15:07:06 +00:00
|
|
|
// FAIL: api may be down
|
|
|
|
// FAIL-RESP: rely on harmony retry
|
2023-12-20 09:41:00 +00:00
|
|
|
ticket, ticketEpoch, err := s.getTicket(ctx, maddr)
|
2023-12-18 22:48:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("getting ticket: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// do the SDR!!
|
|
|
|
|
2024-01-19 15:07:06 +00:00
|
|
|
// FAIL: storage may not have enough space
|
|
|
|
// FAIL-RESP: rely on harmony retry
|
|
|
|
|
|
|
|
// LATEFAIL: compute error in sdr
|
|
|
|
// LATEFAIL-RESP: Check in Trees task should catch this; Will retry computing
|
|
|
|
// Trees; After one retry, it should return the sector to the
|
|
|
|
// SDR stage; max number of retries should be configurable
|
|
|
|
|
2024-02-27 19:23:42 +00:00
|
|
|
err = s.sc.GenerateSDR(ctx, taskID, sref, ticket, commd)
|
2023-12-18 22:48:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("generating sdr: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// store success!
|
2023-12-20 09:41:00 +00:00
|
|
|
n, err := s.db.Exec(ctx, `UPDATE sectors_sdr_pipeline
|
|
|
|
SET after_sdr = true, ticket_epoch = $3, ticket_value = $4
|
|
|
|
WHERE sp_id = $1 AND sector_number = $2`,
|
2023-12-23 17:49:11 +00:00
|
|
|
sectorParams.SpID, sectorParams.SectorNumber, ticketEpoch, []byte(ticket))
|
2023-12-18 22:48:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("store sdr success: updating pipeline: %w", err)
|
|
|
|
}
|
|
|
|
if n != 1 {
|
|
|
|
return false, xerrors.Errorf("store sdr success: updated %d rows", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-12-20 09:41:00 +00:00
|
|
|
func (s *SDRTask) getTicket(ctx context.Context, maddr address.Address) (abi.SealRandomness, abi.ChainEpoch, error) {
|
2023-12-18 22:48:49 +00:00
|
|
|
ts, err := s.api.ChainHead(ctx)
|
|
|
|
if err != nil {
|
2023-12-20 09:41:00 +00:00
|
|
|
return nil, 0, xerrors.Errorf("getting chain head: %w", err)
|
2023-12-18 22:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ticketEpoch := ts.Height() - policy.SealRandomnessLookback
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := maddr.MarshalCBOR(buf); err != nil {
|
2023-12-20 09:41:00 +00:00
|
|
|
return nil, 0, xerrors.Errorf("marshaling miner address: %w", err)
|
2023-12-18 22:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rand, err := s.api.StateGetRandomnessFromTickets(ctx, crypto.DomainSeparationTag_SealRandomness, ticketEpoch, buf.Bytes(), ts.Key())
|
|
|
|
if err != nil {
|
2023-12-20 09:41:00 +00:00
|
|
|
return nil, 0, xerrors.Errorf("getting randomness from tickets: %w", err)
|
2023-12-18 22:48:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 09:41:00 +00:00
|
|
|
return abi.SealRandomness(rand), ticketEpoch, nil
|
2023-12-18 17:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SDRTask) CanAccept(ids []harmonytask.TaskID, engine *harmonytask.TaskEngine) (*harmonytask.TaskID, error) {
|
2023-12-18 22:48:49 +00:00
|
|
|
id := ids[0]
|
|
|
|
return &id, nil
|
2023-12-18 17:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SDRTask) TypeDetails() harmonytask.TaskTypeDetails {
|
2024-02-27 19:23:42 +00:00
|
|
|
ssize := abi.SectorSize(32 << 30) // todo task details needs taskID to get correct sector size
|
|
|
|
if isDevnet {
|
|
|
|
ssize = abi.SectorSize(2 << 20)
|
|
|
|
}
|
|
|
|
|
2023-12-19 11:50:27 +00:00
|
|
|
res := harmonytask.TaskTypeDetails{
|
2023-12-20 10:28:06 +00:00
|
|
|
Max: s.max,
|
2023-12-18 22:48:49 +00:00
|
|
|
Name: "SDR",
|
|
|
|
Cost: resources.Resources{ // todo offset for prefetch?
|
2024-02-27 19:23:42 +00:00
|
|
|
Cpu: 4, // todo multicore sdr
|
|
|
|
Gpu: 0,
|
|
|
|
Ram: 54 << 30,
|
|
|
|
Storage: s.sc.Storage(s.taskToSector, storiface.FTCache, storiface.FTNone, ssize, storiface.PathSealing),
|
2023-12-18 22:48:49 +00:00
|
|
|
},
|
2023-12-20 10:28:06 +00:00
|
|
|
MaxFailures: 2,
|
2023-12-18 17:29:10 +00:00
|
|
|
Follows: nil,
|
|
|
|
}
|
2023-12-19 11:50:27 +00:00
|
|
|
|
|
|
|
if isDevnet {
|
|
|
|
res.Cost.Ram = 1 << 30
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
2023-12-18 17:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SDRTask) Adder(taskFunc harmonytask.AddTaskFunc) {
|
2023-12-18 22:48:49 +00:00
|
|
|
s.sp.pollers[pollerSDR].Set(taskFunc)
|
2023-12-18 17:29:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
func (s *SDRTask) taskToSector(id harmonytask.TaskID) (ffi.SectorRef, error) {
|
|
|
|
var refs []ffi.SectorRef
|
2024-02-27 19:23:42 +00:00
|
|
|
|
|
|
|
err := s.db.Select(context.Background(), &refs, `SELECT sp_id, sector_number, reg_seal_proof FROM sectors_sdr_pipeline WHERE task_id_sdr = $1`, id)
|
|
|
|
if err != nil {
|
2024-03-15 21:38:13 +00:00
|
|
|
return ffi.SectorRef{}, xerrors.Errorf("getting sector ref: %w", err)
|
2024-02-27 19:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(refs) != 1 {
|
2024-03-15 21:38:13 +00:00
|
|
|
return ffi.SectorRef{}, xerrors.Errorf("expected 1 sector ref, got %d", len(refs))
|
2024-02-27 19:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return refs[0], nil
|
|
|
|
}
|
|
|
|
|
2023-12-18 17:29:10 +00:00
|
|
|
var _ harmonytask.TaskInterface = &SDRTask{}
|