lotus/curiosrc/web/hapi/watch_actor.go

287 lines
6.6 KiB
Go
Raw Normal View History

2024-01-12 13:09:12 +00:00
package hapi
import (
"context"
2024-01-15 16:34:10 +00:00
"sort"
"time"
2024-01-12 13:09:12 +00:00
"github.com/BurntSushi/toml"
2024-01-15 16:34:10 +00:00
"golang.org/x/xerrors"
2024-01-12 13:09:12 +00:00
"github.com/filecoin-project/go-address"
2024-01-15 16:34:10 +00:00
"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/store"
2024-01-12 13:09:12 +00:00
"github.com/filecoin-project/lotus/chain/types"
)
const watchInterval = time.Second * 10
func (a *app) watchActor() {
ticker := time.NewTicker(watchInterval)
for {
err := a.updateActor(context.TODO())
if err != nil {
log.Errorw("updating rpc info", "error", err)
}
select {
case <-ticker.C:
}
}
}
type minimalActorInfo struct {
2024-02-17 17:05:54 +00:00
Addresses []struct {
2024-01-12 13:09:12 +00:00
MinerAddresses []string
}
}
feat: curio: sectors UI (#11869) * cfg edit 1 * jsonschema deps * feat: lp mig - first few steps * lp mig: default tasks * code comments * docs * lp-mig-progress * shared * comments and todos * fix: curio: rename lotus-provider to curio (#11645) * rename provider to curio * install gotext * fix lint errors, mod tidy * fix typo * fix API_INFO and add gotext to circleCI * add back gotext * add gotext after remerge * lp: channels doc * finish easy-migration TODOs * out generate * merging and more renames * avoid make-all * minor doc stuff * cu: make gen * make gen fix * make gen * tryfix * go mod tidy * minor ez migration fixes * ez setup - ui cleanups * better error message * guided setup colors * better path to saveconfigtolayer * loadconfigwithupgrades fix * readMiner oops * guided - homedir * err if miner is running * prompt error should exit * process already running, miner_id sectors in migration * dont prompt for language a second time * check miner stopped * unlock repo * render and sql oops * curio easyMig - some fixes * easyMigration runs successfully * lint * part 2 of last * message * merge addtl * fixing guided setup for myself * warn-on-no-post * EditorLoads * cleanups and styles * create info * fix tests * make gen * sector early bird * sectors v2 * sector termination v1 * terminate2 * mjs * minor things * flag bad sectors * fix errors * add dealweight and deals * change column width * refactor sql, handle sealing sectors * fix estimates --------- Co-authored-by: LexLuthr <88259624+LexLuthr@users.noreply.github.com> Co-authored-by: LexLuthr <lexluthr@protocol.ai> Co-authored-by: LexLuthr <lexluthr@curiostorage.org>
2024-04-18 19:57:29 +00:00
var startedAt = time.Now()
2024-01-12 13:09:12 +00:00
func (a *app) updateActor(ctx context.Context) error {
a.rpcInfoLk.Lock()
api := a.workingApi
a.rpcInfoLk.Unlock()
stor := store.ActorStore(ctx, blockstore.NewReadCachedBlockstore(blockstore.NewAPIBlockstore(a.workingApi), ChainBlockCache))
2024-01-12 13:09:12 +00:00
if api == nil {
feat: curio: sectors UI (#11869) * cfg edit 1 * jsonschema deps * feat: lp mig - first few steps * lp mig: default tasks * code comments * docs * lp-mig-progress * shared * comments and todos * fix: curio: rename lotus-provider to curio (#11645) * rename provider to curio * install gotext * fix lint errors, mod tidy * fix typo * fix API_INFO and add gotext to circleCI * add back gotext * add gotext after remerge * lp: channels doc * finish easy-migration TODOs * out generate * merging and more renames * avoid make-all * minor doc stuff * cu: make gen * make gen fix * make gen * tryfix * go mod tidy * minor ez migration fixes * ez setup - ui cleanups * better error message * guided setup colors * better path to saveconfigtolayer * loadconfigwithupgrades fix * readMiner oops * guided - homedir * err if miner is running * prompt error should exit * process already running, miner_id sectors in migration * dont prompt for language a second time * check miner stopped * unlock repo * render and sql oops * curio easyMig - some fixes * easyMigration runs successfully * lint * part 2 of last * message * merge addtl * fixing guided setup for myself * warn-on-no-post * EditorLoads * cleanups and styles * create info * fix tests * make gen * sector early bird * sectors v2 * sector termination v1 * terminate2 * mjs * minor things * flag bad sectors * fix errors * add dealweight and deals * change column width * refactor sql, handle sealing sectors * fix estimates --------- Co-authored-by: LexLuthr <88259624+LexLuthr@users.noreply.github.com> Co-authored-by: LexLuthr <lexluthr@protocol.ai> Co-authored-by: LexLuthr <lexluthr@curiostorage.org>
2024-04-18 19:57:29 +00:00
if time.Since(startedAt) > time.Second*10 {
log.Warnw("no working api yet")
}
2024-01-12 13:09:12 +00:00
return nil
}
var actorInfos []actorInfo
confNameToAddr := map[address.Address][]string{} // address -> config names
err := forEachConfig[minimalActorInfo](a, func(name string, info minimalActorInfo) error {
2024-02-17 17:05:54 +00:00
for _, aset := range info.Addresses {
for _, addr := range aset.MinerAddresses {
a, err := address.NewFromString(addr)
if err != nil {
return xerrors.Errorf("parsing address: %w", err)
}
confNameToAddr[a] = append(confNameToAddr[a], name)
2024-01-12 13:09:12 +00:00
}
}
return nil
})
if err != nil {
return err
}
wins, err := a.spWins(ctx)
if err != nil {
return xerrors.Errorf("getting sp wins: %w", err)
}
2024-01-12 13:09:12 +00:00
for addr, cnames := range confNameToAddr {
p, err := api.StateMinerPower(ctx, addr, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("getting miner power: %w", err)
}
dls, err := api.StateMinerDeadlines(ctx, addr, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("getting deadlines: %w", err)
}
mact, err := api.StateGetActor(ctx, addr, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("getting actor: %w", err)
}
mas, err := miner.Load(stor, mact)
if err != nil {
return err
}
2024-01-12 13:09:12 +00:00
outDls := []actorDeadline{}
for dlidx := range dls {
p, err := api.StateMinerPartitions(ctx, addr, uint64(dlidx), types.EmptyTSK)
if err != nil {
return xerrors.Errorf("getting partition: %w", err)
}
dl := actorDeadline{
Empty: false,
Current: false, // todo
Proven: false,
PartFaulty: false,
Faulty: false,
}
var live, faulty uint64
for _, part := range p {
l, err := part.LiveSectors.Count()
if err != nil {
return xerrors.Errorf("getting live sectors: %w", err)
}
live += l
f, err := part.FaultySectors.Count()
if err != nil {
return xerrors.Errorf("getting faulty sectors: %w", err)
}
faulty += f
}
dl.Empty = live == 0
dl.Proven = live > 0 && faulty == 0
dl.PartFaulty = faulty > 0
dl.Faulty = faulty > 0 && faulty == live
outDls = append(outDls, dl)
}
pd, err := api.StateMinerProvingDeadline(ctx, addr, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("getting proving deadline: %w", err)
}
if len(outDls) != 48 {
return xerrors.Errorf("expected 48 deadlines, got %d", len(outDls))
}
outDls[pd.Index].Current = true
avail, err := mas.AvailableBalance(mact.Balance)
if err != nil {
return xerrors.Errorf("getting available balance: %w", err)
}
mi, err := mas.Info()
if err != nil {
return xerrors.Errorf("getting miner info: %w", err)
}
wbal, err := api.WalletBalance(ctx, mi.Worker)
if err != nil {
return xerrors.Errorf("getting worker balance: %w", err)
}
feat: curio: web based config edit (#11822) * cfg edit 1 * jsonschema deps * feat: lp mig - first few steps * lp mig: default tasks * code comments * docs * lp-mig-progress * shared * comments and todos * fix: curio: rename lotus-provider to curio (#11645) * rename provider to curio * install gotext * fix lint errors, mod tidy * fix typo * fix API_INFO and add gotext to circleCI * add back gotext * add gotext after remerge * lp: channels doc * finish easy-migration TODOs * out generate * merging and more renames * avoid make-all * minor doc stuff * cu: make gen * make gen fix * make gen * tryfix * go mod tidy * minor ez migration fixes * ez setup - ui cleanups * better error message * guided setup colors * better path to saveconfigtolayer * loadconfigwithupgrades fix * readMiner oops * guided - homedir * err if miner is running * prompt error should exit * process already running, miner_id sectors in migration * dont prompt for language a second time * check miner stopped * unlock repo * render and sql oops * curio easyMig - some fixes * easyMigration runs successfully * lint * part 2 of last * message * merge addtl * fixing guided setup for myself * warn-on-no-post * EditorLoads * cleanups and styles * create info * fix tests * make gen * change layout, add help button * Duration custom json * mjs naming --------- Co-authored-by: LexLuthr <88259624+LexLuthr@users.noreply.github.com> Co-authored-by: LexLuthr <lexluthr@protocol.ai> Co-authored-by: LexLuthr <lexluthr@curiostorage.org>
2024-04-16 14:30:27 +00:00
sort.Strings(cnames)
2024-01-12 13:09:12 +00:00
actorInfos = append(actorInfos, actorInfo{
Address: addr.String(),
CLayers: cnames,
QualityAdjustedPower: types.DeciStr(p.MinerPower.QualityAdjPower),
RawBytePower: types.DeciStr(p.MinerPower.RawBytePower),
Deadlines: outDls,
ActorBalance: types.FIL(mact.Balance).Short(),
ActorAvailable: types.FIL(avail).Short(),
WorkerBalance: types.FIL(wbal).Short(),
Win1: wins[addr].Win1, // note: zero values are fine here
Win7: wins[addr].Win7,
Win30: wins[addr].Win30,
2024-01-12 13:09:12 +00:00
})
}
sort.Slice(actorInfos, func(i, j int) bool {
return actorInfos[i].Address < actorInfos[j].Address
})
a.actorInfoLk.Lock()
a.actorInfos = actorInfos
a.actorInfoLk.Unlock()
return nil
}
func (a *app) loadConfigs(ctx context.Context) (map[string]string, error) {
rows, err := a.db.Query(ctx, `SELECT title, config FROM harmony_config`)
if err != nil {
return nil, xerrors.Errorf("getting db configs: %w", err)
}
configs := make(map[string]string)
for rows.Next() {
var title, config string
if err := rows.Scan(&title, &config); err != nil {
return nil, xerrors.Errorf("scanning db configs: %w", err)
}
configs[title] = config
}
return configs, nil
}
type wins struct {
SpID int64 `db:"sp_id"`
Win1 int64 `db:"win1"`
Win7 int64 `db:"win7"`
Win30 int64 `db:"win30"`
}
func (a *app) spWins(ctx context.Context) (map[address.Address]wins, error) {
var w []wins
// note: this query uses mining_tasks_won_sp_id_base_compute_time_index
err := a.db.Select(ctx, &w, `WITH wins AS (
SELECT
sp_id,
base_compute_time,
won
FROM
mining_tasks
WHERE
won = true
AND base_compute_time > NOW() - INTERVAL '30 days'
)
SELECT
sp_id,
COUNT(*) FILTER (WHERE base_compute_time > NOW() - INTERVAL '1 day') AS "win1",
COUNT(*) FILTER (WHERE base_compute_time > NOW() - INTERVAL '7 days') AS "win7",
COUNT(*) FILTER (WHERE base_compute_time > NOW() - INTERVAL '30 days') AS "win30"
FROM
wins
GROUP BY
sp_id
ORDER BY
sp_id`)
if err != nil {
return nil, xerrors.Errorf("query win counts: %w", err)
}
wm := make(map[address.Address]wins)
for _, wi := range w {
ma, err := address.NewIDAddress(uint64(wi.SpID))
if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err)
}
wm[ma] = wi
}
return wm, nil
}
2024-01-12 13:09:12 +00:00
func forEachConfig[T any](a *app, cb func(name string, v T) error) error {
confs, err := a.loadConfigs(context.Background())
if err != nil {
return err
}
for name, tomlStr := range confs {
var info T
if err := toml.Unmarshal([]byte(tomlStr), &info); err != nil {
return xerrors.Errorf("unmarshaling %s config: %w", name, err)
}
if err := cb(name, info); err != nil {
return xerrors.Errorf("cb: %w", err)
}
}
return nil
}