2023-11-15 01:00:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-02 22:32:13 +00:00
|
|
|
"database/sql"
|
2023-11-15 01:00:23 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-11-22 04:10:25 +00:00
|
|
|
"time"
|
2023-11-15 01:00:23 +00:00
|
|
|
|
2023-11-15 01:53:00 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2023-11-15 01:00:23 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/dline"
|
2023-11-15 01:53:00 +00:00
|
|
|
|
2023-12-05 04:30:40 +00:00
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-provider/deps"
|
2023-11-17 00:22:03 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
|
2023-11-15 01:00:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/provider"
|
|
|
|
)
|
|
|
|
|
2023-11-21 00:05:59 +00:00
|
|
|
var testCmd = &cli.Command{
|
|
|
|
Name: "test",
|
|
|
|
Usage: "Utility functions for testing",
|
2023-11-15 01:00:23 +00:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
//provingInfoCmd,
|
2023-11-21 00:05:59 +00:00
|
|
|
wdPostCmd,
|
2023-11-15 01:00:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:05:59 +00:00
|
|
|
var wdPostCmd = &cli.Command{
|
|
|
|
Name: "window-post",
|
|
|
|
Aliases: []string{"wd", "windowpost", "wdpost"},
|
|
|
|
Usage: "Compute a proof-of-spacetime for a sector (requires the sector to be pre-sealed). These will not send to the chain.",
|
2023-11-15 01:00:23 +00:00
|
|
|
Subcommands: []*cli.Command{
|
2023-11-21 00:05:59 +00:00
|
|
|
wdPostHereCmd,
|
|
|
|
wdPostTaskCmd,
|
2023-11-17 00:22:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-22 04:43:13 +00:00
|
|
|
// wdPostTaskCmd writes to harmony_task and wdpost_partition_tasks, then waits for the result.
|
|
|
|
// It is intended to be used to test the windowpost scheduler.
|
|
|
|
// The end of the compute task puts the task_id onto wdpost_proofs, which is read by the submit task.
|
|
|
|
// The submit task will not send test tasks to the chain, and instead will write the result to harmony_test.
|
|
|
|
// The result is read by this command, and printed to stdout.
|
2023-11-21 00:05:59 +00:00
|
|
|
var wdPostTaskCmd = &cli.Command{
|
|
|
|
Name: "task",
|
|
|
|
Aliases: []string{"scheduled", "schedule", "async", "asynchronous"},
|
|
|
|
Usage: "Test the windowpost scheduler by running it on the next available lotus-provider. ",
|
2023-11-17 00:22:03 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: "deadline",
|
|
|
|
Usage: "deadline to compute WindowPoSt for ",
|
|
|
|
Value: 0,
|
|
|
|
},
|
2024-03-06 09:41:37 +00:00
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "layers",
|
|
|
|
Usage: "list of layers to be interpreted (atop defaults). Default: base",
|
|
|
|
},
|
2023-11-17 00:22:03 +00:00
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-12-05 04:30:40 +00:00
|
|
|
deps, err := deps.GetDeps(ctx, cctx)
|
2023-11-17 00:22:03 +00:00
|
|
|
if err != nil {
|
2024-02-17 12:01:19 +00:00
|
|
|
return xerrors.Errorf("get config: %w", err)
|
2023-11-17 00:22:03 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 04:30:40 +00:00
|
|
|
ts, err := deps.Full.ChainHead(ctx)
|
2023-11-17 00:22:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("cannot get chainhead %w", err)
|
|
|
|
}
|
|
|
|
ht := ts.Height()
|
2023-11-17 13:43:58 +00:00
|
|
|
|
2024-01-20 00:48:29 +00:00
|
|
|
// It's not important to be super-accurate as it's only for basic testing.
|
|
|
|
addr, err := address.NewFromString(deps.Cfg.Addresses[0].MinerAddresses[0])
|
2023-11-17 13:43:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("cannot get miner address %w", err)
|
|
|
|
}
|
|
|
|
maddr, err := address.IDFromAddress(addr)
|
2023-11-17 00:22:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("cannot get miner id %w", err)
|
|
|
|
}
|
2024-02-17 12:01:19 +00:00
|
|
|
var taskId int64
|
2023-12-11 23:47:06 +00:00
|
|
|
|
2023-12-12 00:30:39 +00:00
|
|
|
_, err = deps.DB.BeginTransaction(ctx, func(tx *harmonydb.Tx) (commit bool, err error) {
|
2024-02-17 12:01:19 +00:00
|
|
|
err = tx.QueryRow(`INSERT INTO harmony_task (name, posted_time, added_by) VALUES ('WdPost', CURRENT_TIMESTAMP, 123) RETURNING id`).Scan(&taskId)
|
2023-11-17 00:22:03 +00:00
|
|
|
if err != nil {
|
2023-11-17 13:43:58 +00:00
|
|
|
log.Error("inserting harmony_task: ", err)
|
2023-11-17 00:22:03 +00:00
|
|
|
return false, xerrors.Errorf("inserting harmony_task: %w", err)
|
|
|
|
}
|
|
|
|
_, err = tx.Exec(`INSERT INTO wdpost_partition_tasks
|
|
|
|
(task_id, sp_id, proving_period_start, deadline_index, partition_index) VALUES ($1, $2, $3, $4, $5)`,
|
2024-02-17 12:01:19 +00:00
|
|
|
taskId, maddr, ht, cctx.Uint64("deadline"), 0)
|
2023-11-17 00:22:03 +00:00
|
|
|
if err != nil {
|
2023-11-17 13:43:58 +00:00
|
|
|
log.Error("inserting wdpost_partition_tasks: ", err)
|
2023-11-17 00:22:03 +00:00
|
|
|
return false, xerrors.Errorf("inserting wdpost_partition_tasks: %w", err)
|
|
|
|
}
|
2024-02-17 12:01:19 +00:00
|
|
|
_, err = tx.Exec("INSERT INTO harmony_test (task_id) VALUES ($1)", taskId)
|
2023-11-21 00:05:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("inserting into harmony_tests: %w", err)
|
|
|
|
}
|
2023-11-17 00:22:03 +00:00
|
|
|
return true, nil
|
2024-02-21 13:19:48 +00:00
|
|
|
}, harmonydb.OptionRetry())
|
2023-11-17 13:43:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("writing SQL transaction: %w", err)
|
|
|
|
}
|
2024-02-17 12:01:19 +00:00
|
|
|
fmt.Printf("Inserted task %v. Waiting for success ", taskId)
|
2023-12-02 22:32:13 +00:00
|
|
|
var result sql.NullString
|
2023-11-22 04:10:25 +00:00
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
2024-02-17 12:01:19 +00:00
|
|
|
err = deps.DB.QueryRow(ctx, `SELECT result FROM harmony_test WHERE task_id=$1`, taskId).Scan(&result)
|
2023-11-25 15:15:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("reading result from harmony_test: %w", err)
|
|
|
|
}
|
2023-12-02 22:32:13 +00:00
|
|
|
if result.Valid {
|
2023-11-22 04:10:25 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
fmt.Print(".")
|
|
|
|
}
|
2024-02-20 16:35:52 +00:00
|
|
|
fmt.Println()
|
2023-12-08 08:21:07 +00:00
|
|
|
log.Infof("Result: %s", result.String)
|
2023-11-17 00:22:03 +00:00
|
|
|
return nil
|
2023-11-15 01:00:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-22 04:43:13 +00:00
|
|
|
// This command is intended to be used to verify PoSt compute performance.
|
|
|
|
// It will not send any messages to the chain. Since it can compute any deadline, output may be incorrectly timed for the chain.
|
|
|
|
// The entire processing happens in this process while you wait. It does not use the scheduler.
|
2023-11-21 00:05:59 +00:00
|
|
|
var wdPostHereCmd = &cli.Command{
|
|
|
|
Name: "here",
|
|
|
|
Aliases: []string{"cli"},
|
2023-11-15 01:00:23 +00:00
|
|
|
Usage: "Compute WindowPoSt for performance and configuration testing.",
|
|
|
|
Description: `Note: This command is intended to be used to verify PoSt compute performance.
|
|
|
|
It will not send any messages to the chain. Since it can compute any deadline, output may be incorrectly timed for the chain.`,
|
|
|
|
ArgsUsage: "[deadline index]",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: "deadline",
|
|
|
|
Usage: "deadline to compute WindowPoSt for ",
|
|
|
|
Value: 0,
|
|
|
|
},
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "layers",
|
|
|
|
Usage: "list of layers to be interpreted (atop defaults). Default: base",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "storage-json",
|
|
|
|
Usage: "path to json file containing storage config",
|
|
|
|
Value: "~/.lotus-provider/storage.json",
|
|
|
|
},
|
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: "partition",
|
|
|
|
Usage: "partition to compute WindowPoSt for",
|
|
|
|
Value: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2023-12-05 04:30:40 +00:00
|
|
|
deps, err := deps.GetDeps(ctx, cctx)
|
2023-11-15 01:00:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-05 15:10:34 +00:00
|
|
|
wdPostTask, wdPoStSubmitTask, derlareRecoverTask, err := provider.WindowPostScheduler(ctx, deps.Cfg.Fees, deps.Cfg.Proving, deps.Full, deps.Verif, deps.LW, nil, nil,
|
2023-12-05 04:30:40 +00:00
|
|
|
deps.As, deps.Maddrs, deps.DB, deps.Stor, deps.Si, deps.Cfg.Subsystems.WindowPostMaxTasks)
|
2023-11-15 01:00:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, _ = wdPoStSubmitTask, derlareRecoverTask
|
|
|
|
|
2023-12-05 04:30:40 +00:00
|
|
|
if len(deps.Maddrs) == 0 {
|
2023-11-15 01:00:23 +00:00
|
|
|
return errors.New("no miners to compute WindowPoSt for")
|
|
|
|
}
|
2023-12-05 04:30:40 +00:00
|
|
|
head, err := deps.Full.ChainHead(ctx)
|
2023-11-15 01:00:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to get chain head: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
di := dline.NewInfo(head.Height(), cctx.Uint64("deadline"), 0, 0, 0, 10 /*challenge window*/, 0, 0)
|
|
|
|
|
2024-02-13 01:03:45 +00:00
|
|
|
for maddr := range deps.Maddrs {
|
2023-11-15 01:00:23 +00:00
|
|
|
out, err := wdPostTask.DoPartition(ctx, head, address.Address(maddr), di, cctx.Uint64("partition"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error computing WindowPoSt for miner", maddr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Println("Computed WindowPoSt for miner", maddr, ":")
|
|
|
|
err = json.NewEncoder(os.Stdout).Encode(out)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Could not encode WindowPoSt output for miner", maddr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|