lotus/curiosrc/web/api/config/config.go
Andrew Jackson (Ajax) 441f5995d3
fix: curio: node UI & darwin gpu count (#11950)
* fix: curio: Update pgx imports, fix db_storage alloc

* feat: curioweb: Improve task_history indexes (#11911)

* 1

* relatable

* add and delete layer

* chore: curio: merge master (#11956)

* Fixing dead links (#11907)

* ci: ci: create gh workflow that updates sorted pr checks (#11861)

* ci: create gh workflow that updates sorted pr checks

* ci: use grouped_by_result template for pr checks sticky comment

* chore: apply pr review suggestion

* Avoid cfg lookup on chain remove since unenabled splitstore delete is noop anyway (#11916)

Co-authored-by: zenground0 <ZenGround0@users.noreply.github.com>

* Fix mismatched method names in comments (#11913)

Signed-off-by: forcedebug <forcedebug@outlook.com>

* release: v1.26.3 (#11908) (#11915) (#11922)

* deps: update dependencies to address migration memory bloat

to address memory concerns during a heavy migration

Ref: https://github.com/filecoin-project/go-state-types/pull/260
Ref: https://github.com/whyrusleeping/cbor-gen/pull/96
Ref: https://github.com/filecoin-project/go-amt-ipld/pull/90

* release: prep v1.26.3 patch

Prep v1.26.3 patch release:
- Update changelog, version and make gen + make docsgen-cli

* deps: update cbor-gen to tagged version

deps: update cbor-gen to tagged version

* deps: update go-state-types to tagged version

deps: update go-state-types to tagged version v0.13.2

* chore: deps: update go-state-types to v0.13.3

Fixes a panic when we have fewer than 1k proposals.

---------

Co-authored-by: Phi-rjan <orjan.roren@gmail.com>
Co-authored-by: Rod Vagg <rod@vagg.org>
Co-authored-by: Steven Allen <steven@stebalien.com>

* Refactor `LookupID*` APIs in `StateManager` and `StateTree`

The naming of `LookupID` can cause confusion when resolving actor IDs vs
 ID addresses. To avoid this:

* Refactor `StateTree` `LookupID` to `LookupIDAddress`, because it
returns ID address.
* Refactor `StateManager` `LookupID` to
`LookupIDAddress` because it also returns ID address via a chain call to
`StateTree`.
* Introduce a new API `StateManager` dedicated to resolving address to
actor ID, called `LookupID` which returns `abi.ActorID`.

For context, see:
 * https://github.com/filecoin-project/lotus/pull/11723#discussion_r1534728607

* Add v13 support to invariants-checker (#11931)

Add v13 support to invariants-checker

* chore: docs: nv-skeleton documentation (#11065)

* nv-skeleton documentation

Add a tutorial for how one can create a nv-skeleton in Lotus

* Add footnote for `Add migration` step

Add footnote for `Add migration` step

* Indent migration-code

Indent migration-code to make it show properly as a footnote.

* Add ref-fvm and filecoin-ffi checklist

Add ref-fvm and filecoin-ffi checklist

* Add Filecoin-FFI steps

Add Filecoin-FFI steps

* Add step to params_butterfly.go

Add step to params_butterfly.go

* Fix typo

Fix typo

* Add links to reference PRs

Add links to reference PRs

* Update ref-fvm list

Update ref-fvm list

* feat: curio: add StorageInit api (#11918)

* feat: add StorageInit api

* remove unused variables

* fix gen check

* feat: curio: simpler reservation release logic (#11900)

* simpler release logic

* oops, plus simpler

* simpler

* fix NewLine (#11893)

* fix(events): check for sync-in-progress (#11932)

* feat(events): adjust indexes in event index db to match query patterns

Introduces a v4 migration that just adjusts indexes.

Copies some improvements from https://github.com/filecoin-project/lotus/pull/11723

Closes: https://github.com/filecoin-project/lotus/issues/11909

* fix(pipeline): should return if error occurred when get network version (#11902)

* fix(events): correct log msg for v4 events index db migration

* chore: remove duplicate words in strings and comments

* fix(events): register events index db migration v4

* fix: curio seal: Failed commit retry strategy (#11870)

* ffi: improved-error-handling

* curio seal: Failed commit retry strategy

* use master ffi

* mod tidy

* fix: curio: Update pgx imports, fix db_storage alloc

* feat: curioweb: Improve task_history indexes (#11911)

* mod tidy

* Event index should be unique for tipsets (#11952)

* event index should be unique for tipsets

* fix formatting

* migrate to version 5

* chore: bump build version in master (#11946)

* Bump version

Bump version in master branch in preperation for cutting v1.27.0-rc1

* chore: bump build-version

chore: bump build-version

* feat: curioweb: Show piece info on the sector page (#11955)

* curio: feat: break trees task into TreeD(prefetch) and TreeRC (#11895)

* break trees task

* fix TreeD reservation

* fix nil pointer err

* apply suggestions

* fix allocate file types

* fix dbIndex inserts

* set resource, move release func

* refactor func(), update memory

* remove extra release

---------

Signed-off-by: forcedebug <forcedebug@outlook.com>
Co-authored-by: parthshah1 <parthhshah171@gmail.com>
Co-authored-by: Piotr Galar <piotr.galar@gmail.com>
Co-authored-by: ZenGround0 <5515260+ZenGround0@users.noreply.github.com>
Co-authored-by: zenground0 <ZenGround0@users.noreply.github.com>
Co-authored-by: forcedebug <167591285+forcedebug@users.noreply.github.com>
Co-authored-by: Jiaying Wang <42981373+jennijuju@users.noreply.github.com>
Co-authored-by: Phi-rjan <orjan.roren@gmail.com>
Co-authored-by: Rod Vagg <rod@vagg.org>
Co-authored-by: Steven Allen <steven@stebalien.com>
Co-authored-by: Masih H. Derkani <m@derkani.org>
Co-authored-by: Lee <octalgah@gmail.com>
Co-authored-by: Andrew Jackson (Ajax) <snadrus@gmail.com>
Co-authored-by: beck <34204218+beck-8@users.noreply.github.com>
Co-authored-by: 0x5459 <0x5459@protonmail.com>
Co-authored-by: Łukasz Magiera <magik6k@users.noreply.github.com>
Co-authored-by: Łukasz Magiera <magik6k@gmail.com>
Co-authored-by: Aarsh Shah <aarshkshah1992@gmail.com>

* linter oops

* gen cleanup

* fix

* named returns are confusing

---------

Signed-off-by: forcedebug <forcedebug@outlook.com>
Co-authored-by: Łukasz Magiera <magik6k@gmail.com>
Co-authored-by: Łukasz Magiera <magik6k@users.noreply.github.com>
Co-authored-by: LexLuthr <88259624+LexLuthr@users.noreply.github.com>
Co-authored-by: parthshah1 <parthhshah171@gmail.com>
Co-authored-by: Piotr Galar <piotr.galar@gmail.com>
Co-authored-by: ZenGround0 <5515260+ZenGround0@users.noreply.github.com>
Co-authored-by: zenground0 <ZenGround0@users.noreply.github.com>
Co-authored-by: forcedebug <167591285+forcedebug@users.noreply.github.com>
Co-authored-by: Jiaying Wang <42981373+jennijuju@users.noreply.github.com>
Co-authored-by: Phi-rjan <orjan.roren@gmail.com>
Co-authored-by: Rod Vagg <rod@vagg.org>
Co-authored-by: Steven Allen <steven@stebalien.com>
Co-authored-by: Masih H. Derkani <m@derkani.org>
Co-authored-by: Lee <octalgah@gmail.com>
Co-authored-by: beck <34204218+beck-8@users.noreply.github.com>
Co-authored-by: 0x5459 <0x5459@protonmail.com>
Co-authored-by: Aarsh Shah <aarshkshah1992@gmail.com>
2024-05-04 10:35:30 +02:00

203 lines
5.9 KiB
Go

package config
import (
"bytes"
"context"
"encoding/json"
"net/http"
"reflect"
"time"
"github.com/BurntSushi/toml"
"github.com/gorilla/mux"
"github.com/invopop/jsonschema"
logging "github.com/ipfs/go-log/v2"
"github.com/filecoin-project/lotus/cmd/curio/deps"
"github.com/filecoin-project/lotus/curiosrc/web/api/apihelper"
"github.com/filecoin-project/lotus/node/config"
)
var log = logging.Logger("curio/web/config")
type cfg struct {
*deps.Deps
}
func Routes(r *mux.Router, deps *deps.Deps) {
c := &cfg{deps}
// At menu.html:
r.Methods("GET").Path("/layers").HandlerFunc(c.getLayers)
r.Methods("GET").Path("/topo").HandlerFunc(c.topo)
// At edit.html:
r.Methods("GET").Path("/schema").HandlerFunc(getSch)
r.Methods("GET").Path("/layers/{layer}").HandlerFunc(c.getLayer)
r.Methods("POST").Path("/addlayer").HandlerFunc(c.addLayer)
r.Methods("POST").Path("/layers/{layer}").HandlerFunc(c.setLayer)
r.Methods("GET").Path("/default").HandlerFunc(c.def)
}
func (c *cfg) addLayer(w http.ResponseWriter, r *http.Request) {
var layer struct {
Name string
}
apihelper.OrHTTPFail(w, json.NewDecoder(r.Body).Decode(&layer))
ct, err := c.DB.Exec(context.Background(), `INSERT INTO harmony_config (title, config) VALUES ($1, $2)`, layer.Name, "")
apihelper.OrHTTPFail(w, err)
if ct != 1 {
w.WriteHeader(http.StatusConflict)
_, err = w.Write([]byte("Layer already exists"))
if err != nil {
log.Errorf("Failed to write response: %s", err)
}
return
}
w.WriteHeader(200)
}
func getSch(w http.ResponseWriter, r *http.Request) {
ref := jsonschema.Reflector{
Mapper: func(i reflect.Type) *jsonschema.Schema {
if i == reflect.TypeOf(config.Duration(time.Second)) {
return &jsonschema.Schema{
Type: "string",
Format: "duration",
}
}
return nil
},
}
sch := ref.Reflect(config.CurioConfig{})
// add comments
for k, doc := range config.Doc {
item, ok := sch.Definitions[k]
if !ok {
continue
}
for _, line := range doc {
item, ok := item.Properties.Get(line.Name)
if !ok {
continue
}
if line.Comment != "" {
extra := make(map[string]any)
type options struct {
InfoText string `json:"infoText"`
}
opt := options{
InfoText: line.Comment,
}
extra["options"] = opt
item.Extras = extra
}
}
}
var allOpt func(s *jsonschema.Schema)
allOpt = func(s *jsonschema.Schema) {
s.Required = []string{}
for _, v := range s.Definitions {
v.Required = []string{}
allOpt(v)
}
}
allOpt(sch)
apihelper.OrHTTPFail(w, json.NewEncoder(w).Encode(sch))
}
func (c *cfg) getLayers(w http.ResponseWriter, r *http.Request) {
var layers []string
apihelper.OrHTTPFail(w, c.DB.Select(context.Background(), &layers, `SELECT title FROM harmony_config ORDER BY title`))
apihelper.OrHTTPFail(w, json.NewEncoder(w).Encode(layers))
}
func (c *cfg) getLayer(w http.ResponseWriter, r *http.Request) {
var layer string
apihelper.OrHTTPFail(w, c.DB.QueryRow(context.Background(), `SELECT config FROM harmony_config WHERE title = $1`, mux.Vars(r)["layer"]).Scan(&layer))
// Read the TOML into a struct
configStruct := map[string]any{} // NOT CurioConfig b/c we want to preserve unsets
_, err := toml.Decode(layer, &configStruct)
apihelper.OrHTTPFail(w, err)
// Encode the struct as JSON
jsonData, err := json.Marshal(configStruct)
apihelper.OrHTTPFail(w, err)
// Write the JSON response
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonData)
apihelper.OrHTTPFail(w, err)
}
func (c *cfg) setLayer(w http.ResponseWriter, r *http.Request) {
layer := mux.Vars(r)["layer"]
var configStruct map[string]any
dec := json.NewDecoder(r.Body)
dec.UseNumber() // JSON lib by default treats number is float64()
apihelper.OrHTTPFail(w, dec.Decode(&configStruct))
//Encode the struct as TOML
var tomlData bytes.Buffer
err := toml.NewEncoder(&tomlData).Encode(configStruct)
apihelper.OrHTTPFail(w, err)
configStr := tomlData.String()
// Generate a full commented string if this is base layer
if layer == "base" {
// Parse the into CurioConfig TOML
curioCfg := config.DefaultCurioConfig()
_, err = deps.LoadConfigWithUpgrades(tomlData.String(), curioCfg)
apihelper.OrHTTPFail(w, err)
cb, err := config.ConfigUpdate(curioCfg, config.DefaultCurioConfig(), config.Commented(true), config.DefaultKeepUncommented(), config.NoEnv())
apihelper.OrHTTPFail(w, err)
configStr = string(cb)
}
//Write the TOML to the database
_, err = c.DB.Exec(context.Background(), `INSERT INTO harmony_config (title, config) VALUES ($1, $2) ON CONFLICT (title) DO UPDATE SET config = $2`, layer, configStr)
apihelper.OrHTTPFail(w, err)
}
func (c *cfg) topo(w http.ResponseWriter, r *http.Request) {
var topology []struct {
Server string `db:"server"`
CPU int `db:"cpu"`
GPU int `db:"gpu"`
RAM int `db:"ram"`
LayersCSV string `db:"layers"`
TasksCSV string `db:"tasks"`
}
apihelper.OrHTTPFail(w, c.DB.Select(context.Background(), &topology, `
SELECT
m.host_and_port as server,
cpu, gpu, ram, layers, tasks
FROM harmony_machines m JOIN harmony_machine_details d ON m.id=d.machine_id
ORDER BY server`))
w.Header().Set("Content-Type", "application/json")
apihelper.OrHTTPFail(w, json.NewEncoder(w).Encode(topology))
}
func (c *cfg) def(w http.ResponseWriter, r *http.Request) {
cb, err := config.ConfigUpdate(config.DefaultCurioConfig(), nil, config.Commented(false), config.DefaultKeepUncommented(), config.NoEnv())
apihelper.OrHTTPFail(w, err)
// Read the TOML into a struct
configStruct := map[string]any{} // NOT CurioConfig b/c we want to preserve unsets
_, err = toml.Decode(string(cb), &configStruct)
apihelper.OrHTTPFail(w, err)
// Encode the struct as JSON
jsonData, err := json.Marshal(configStruct)
apihelper.OrHTTPFail(w, err)
// Write the JSON response
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonData)
apihelper.OrHTTPFail(w, err)
}