2023-10-25 12:23:25 +00:00
|
|
|
package lpwindow
|
2023-08-30 16:34:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-25 14:00:49 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2023-10-25 15:01:24 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin/v9/miner"
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/storage/wdpost"
|
2023-10-25 12:23:25 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2023-10-25 15:01:24 +00:00
|
|
|
"golang.org/x/xerrors"
|
2023-10-11 22:51:46 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2023-08-30 16:34:11 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/dline"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
|
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/harmonytask"
|
2023-10-11 22:51:46 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/resources"
|
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/taskhelp"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/sealtasks"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
|
|
|
"github.com/samber/lo"
|
2023-08-30 16:34:11 +00:00
|
|
|
)
|
|
|
|
|
2023-10-25 12:23:25 +00:00
|
|
|
var log = logging.Logger("lpwindow")
|
|
|
|
|
2023-10-25 15:01:24 +00:00
|
|
|
var EpochsPerDeadline = miner.WPoStProvingPeriod / abi.ChainEpoch(miner.WPoStPeriodDeadlines)
|
|
|
|
|
2023-08-30 16:34:11 +00:00
|
|
|
type WdPostTaskDetails struct {
|
|
|
|
Ts *types.TipSet
|
|
|
|
Deadline *dline.Info
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:23:25 +00:00
|
|
|
type WDPoStAPI interface {
|
|
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
|
|
|
ChainGetTipSet(context.Context, types.TipSetKey) (*types.TipSet, error)
|
2023-10-25 14:00:49 +00:00
|
|
|
StateMinerProvingDeadline(context.Context, address.Address, types.TipSetKey) (*dline.Info, error)
|
2023-10-25 15:01:24 +00:00
|
|
|
StateMinerInfo(context.Context, address.Address, types.TipSetKey) (api.MinerInfo, error)
|
|
|
|
ChainGetTipSetAfterHeight(context.Context, abi.ChainEpoch, types.TipSetKey) (*types.TipSet, error)
|
2023-10-25 12:23:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 16:34:11 +00:00
|
|
|
type WdPostTask struct {
|
2023-10-25 12:23:25 +00:00
|
|
|
api WDPoStAPI
|
|
|
|
|
2023-10-25 14:00:49 +00:00
|
|
|
db *harmonydb.DB
|
|
|
|
max int
|
2023-08-30 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WdPostTask) Do(taskID harmonytask.TaskID, stillOwned func() bool) (done bool, err error) {
|
|
|
|
|
2023-10-05 15:52:22 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2023-08-30 16:34:11 +00:00
|
|
|
log.Errorf("WdPostTask.Do() called with taskID: %v", taskID)
|
2023-10-05 15:52:22 +00:00
|
|
|
|
|
|
|
var deadline dline.Info
|
|
|
|
|
2023-10-25 15:01:24 +00:00
|
|
|
var spID, pps, dlIdx, partIdx uint64
|
|
|
|
|
2023-10-05 15:52:22 +00:00
|
|
|
err = t.db.QueryRow(context.Background(),
|
2023-10-25 15:01:24 +00:00
|
|
|
`Select sp_id, proving_period_start, deadline_index, partition_index
|
2023-10-05 15:52:22 +00:00
|
|
|
from wdpost_tasks
|
|
|
|
where task_id = $1`, taskID).Scan(
|
2023-10-25 15:01:24 +00:00
|
|
|
&spID, &pps, &dlIdx, &partIdx,
|
2023-10-05 15:52:22 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("WdPostTask.Do() failed to queryRow: %v", err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:23:25 +00:00
|
|
|
head, err := t.api.ChainHead(context.Background())
|
2023-10-11 22:51:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("WdPostTask.Do() failed to get chain head: %v", err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-10-25 15:01:24 +00:00
|
|
|
wdpost.NewDeadlineInfo(abi.ChainEpoch(pps), dlIdx, head.Height())
|
2023-10-11 22:51:46 +00:00
|
|
|
|
2023-10-25 15:01:24 +00:00
|
|
|
if deadline.PeriodElapsed() {
|
|
|
|
log.Errorf("WdPost removed stale task: %v %v", taskID, deadline)
|
|
|
|
return true, nil
|
2023-10-05 15:52:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 12:23:25 +00:00
|
|
|
panic("todo")
|
2023-10-12 17:35:10 +00:00
|
|
|
|
2023-10-25 12:23:25 +00:00
|
|
|
/*submitWdPostParams, err := t.Scheduler.runPoStCycle(context.Background(), false, deadline, ts)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("WdPostTask.Do() failed to runPoStCycle: %v", err)
|
2023-10-12 17:35:10 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2023-10-25 12:23:25 +00:00
|
|
|
|
|
|
|
log.Errorf("WdPostTask.Do() called with taskID: %v, submitWdPostParams: %v", taskID, submitWdPostParams)
|
|
|
|
|
|
|
|
// Enter an entry for each wdpost message proof into the wdpost_proofs table
|
|
|
|
for _, params := range submitWdPostParams {
|
|
|
|
|
|
|
|
// Convert submitWdPostParams.Partitions to a byte array using CBOR
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
scratch := make([]byte, 9)
|
|
|
|
if err := cbg.WriteMajorTypeHeaderBuf(scratch, buf, cbg.MajArray, uint64(len(params.Partitions))); err != nil {
|
2023-10-12 17:35:10 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2023-10-25 12:23:25 +00:00
|
|
|
for _, v := range params.Partitions {
|
|
|
|
if err := v.MarshalCBOR(buf); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
2023-10-12 17:35:10 +00:00
|
|
|
|
2023-10-25 12:23:25 +00:00
|
|
|
// Insert into wdpost_proofs table
|
|
|
|
_, err = t.db.Exec(context.Background(),
|
|
|
|
`INSERT INTO wdpost_proofs (
|
|
|
|
deadline,
|
|
|
|
partitions,
|
|
|
|
proof_type,
|
|
|
|
proof_bytes)
|
|
|
|
VALUES ($1, $2, $3, $4)`,
|
|
|
|
params.Deadline,
|
|
|
|
buf.Bytes(),
|
|
|
|
params.Proofs[0].PoStProof,
|
|
|
|
params.Proofs[0].ProofBytes)
|
|
|
|
}*/
|
2023-10-12 17:35:10 +00:00
|
|
|
|
2023-08-30 16:34:11 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-10-11 22:51:46 +00:00
|
|
|
func (t *WdPostTask) CanAccept(ids []harmonytask.TaskID, te *harmonytask.TaskEngine) (*harmonytask.TaskID, error) {
|
|
|
|
// GetEpoch
|
2023-10-25 12:23:25 +00:00
|
|
|
ts, err := t.api.ChainHead(context.Background())
|
2023-10-11 22:51:46 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetData for tasks
|
|
|
|
type wdTaskDef struct {
|
2023-10-25 15:01:24 +00:00
|
|
|
Task_id harmonytask.TaskID
|
|
|
|
Sp_id uint64
|
|
|
|
Proving_period_start abi.ChainEpoch
|
|
|
|
Deadline_index uint64
|
|
|
|
Partition_index uint64
|
|
|
|
|
|
|
|
dlInfo *dline.Info `pgx:"-"`
|
|
|
|
openTs *types.TipSet
|
2023-10-11 22:51:46 +00:00
|
|
|
}
|
|
|
|
var tasks []wdTaskDef
|
2023-10-25 15:01:24 +00:00
|
|
|
|
2023-10-23 04:03:06 +00:00
|
|
|
err = t.db.Select(context.Background(), &tasks,
|
2023-10-25 15:01:24 +00:00
|
|
|
`Select
|
2023-10-11 22:51:46 +00:00
|
|
|
task_id,
|
2023-10-25 15:01:24 +00:00
|
|
|
sp_id,
|
|
|
|
proving_period_start,
|
|
|
|
deadline_index,
|
|
|
|
partition_index
|
2023-10-11 22:51:46 +00:00
|
|
|
from wdpost_tasks
|
|
|
|
where task_id IN $1`, ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept those past deadline, then delete them in Do().
|
2023-10-25 15:01:24 +00:00
|
|
|
for i := range tasks {
|
|
|
|
tasks[i].dlInfo = wdpost.NewDeadlineInfo(tasks[i].Proving_period_start, tasks[i].Deadline_index, ts.Height())
|
|
|
|
|
|
|
|
if tasks[i].dlInfo.PeriodElapsed() {
|
|
|
|
return &tasks[i].Task_id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks[i].openTs, err = t.api.ChainGetTipSetAfterHeight(context.Background(), tasks[i].dlInfo.Open, ts.Key())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting task open tipset: %w", err)
|
2023-10-11 22:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard those too big for our free RAM
|
|
|
|
freeRAM := te.ResourcesAvailable().Ram
|
|
|
|
tasks = lo.Filter(tasks, func(d wdTaskDef, _ int) bool {
|
2023-10-25 15:01:24 +00:00
|
|
|
maddr, err := address.NewIDAddress(tasks[0].Sp_id)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("WdPostTask.CanAccept() failed to NewIDAddress: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
mi, err := t.api.StateMinerInfo(context.Background(), maddr, ts.Key())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("WdPostTask.CanAccept() failed to StateMinerInfo: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return res[mi.WindowPoStProofType].MaxMemory <= freeRAM
|
2023-10-11 22:51:46 +00:00
|
|
|
})
|
|
|
|
if len(tasks) == 0 {
|
|
|
|
log.Infof("RAM too small for any WDPost task")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore those with too many failures unless they are the only ones left.
|
|
|
|
tasks, _ = taskhelp.SliceIfFound(tasks, func(d wdTaskDef) bool {
|
|
|
|
var r int
|
|
|
|
err := t.db.QueryRow(context.Background(), `SELECT COUNT(*)
|
|
|
|
FROM harmony_task_history
|
|
|
|
WHERE task_id = $1 AND success = false`, d.Task_id).Scan(&r)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("WdPostTask.CanAccept() failed to queryRow: %v", err)
|
|
|
|
}
|
|
|
|
return r < 2
|
|
|
|
})
|
|
|
|
|
|
|
|
// Select the one closest to the deadline
|
|
|
|
sort.Slice(tasks, func(i, j int) bool {
|
2023-10-25 15:01:24 +00:00
|
|
|
return tasks[i].dlInfo.Open < tasks[j].dlInfo.Open
|
2023-10-11 22:51:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return &tasks[0].Task_id, nil
|
2023-08-30 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 22:51:46 +00:00
|
|
|
var res = storiface.ResourceTable[sealtasks.TTGenerateWindowPoSt]
|
|
|
|
|
2023-08-30 16:34:11 +00:00
|
|
|
func (t *WdPostTask) TypeDetails() harmonytask.TaskTypeDetails {
|
|
|
|
return harmonytask.TaskTypeDetails{
|
2023-10-11 22:51:46 +00:00
|
|
|
Name: "WdPost",
|
|
|
|
Max: t.max,
|
|
|
|
MaxFailures: 3,
|
|
|
|
Follows: nil,
|
|
|
|
Cost: resources.Resources{
|
|
|
|
Cpu: 1,
|
|
|
|
Gpu: 1,
|
|
|
|
// RAM of smallest proof's max is listed here
|
|
|
|
Ram: lo.Reduce(lo.Keys(res), func(i uint64, k abi.RegisteredSealProof, _ int) uint64 {
|
|
|
|
if res[k].MaxMemory < i {
|
|
|
|
return res[k].MaxMemory
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}, 1<<63),
|
|
|
|
},
|
2023-08-30 16:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WdPostTask) Adder(taskFunc harmonytask.AddTaskFunc) {
|
|
|
|
|
|
|
|
// wait for any channels on t.tasks and call taskFunc on them
|
|
|
|
for taskDetails := range t.tasks {
|
|
|
|
|
2023-10-12 17:35:10 +00:00
|
|
|
//log.Errorf("WdPostTask.Adder() received taskDetails: %v", taskDetails)
|
2023-08-30 16:34:11 +00:00
|
|
|
|
|
|
|
taskFunc(func(tID harmonytask.TaskID, tx *harmonydb.Tx) (bool, error) {
|
|
|
|
return t.addTaskToDB(taskDetails.Ts, taskDetails.Deadline, tID, tx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:23:25 +00:00
|
|
|
func NewWdPostTask(db *harmonydb.DB, api WDPoStAPI, max int) *WdPostTask {
|
2023-08-30 16:34:11 +00:00
|
|
|
return &WdPostTask{
|
2023-10-25 14:00:49 +00:00
|
|
|
db: db,
|
|
|
|
api: api,
|
|
|
|
max: max,
|
2023-08-30 16:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 15:01:24 +00:00
|
|
|
func (t *WdPostTask) addTaskToDB(deadline *dline.Info, taskId harmonytask.TaskID, tx *harmonydb.Tx) (bool, error) {
|
2023-09-05 16:29:39 +00:00
|
|
|
|
2023-08-30 16:34:11 +00:00
|
|
|
_, err := tx.Exec(
|
|
|
|
`INSERT INTO wdpost_tasks (
|
|
|
|
task_id,
|
2023-10-25 15:01:24 +00:00
|
|
|
sp_id,
|
|
|
|
proving_period_start,
|
|
|
|
deadline_index,
|
|
|
|
partition_index,
|
|
|
|
|
2023-08-30 16:34:11 +00:00
|
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10 , $11, $12, $13, $14)`,
|
|
|
|
taskId,
|
2023-10-25 15:01:24 +00:00
|
|
|
spID,
|
2023-08-30 16:34:11 +00:00
|
|
|
deadline.PeriodStart,
|
|
|
|
deadline.Index,
|
2023-10-25 15:01:24 +00:00
|
|
|
partID,
|
2023-08-30 16:34:11 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ harmonytask.TaskInterface = &WdPostTask{}
|