Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: curio: add miner init #11775

Merged
merged 26 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cli/spcli/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
power2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/power"
power6 "github.com/filecoin-project/specs-actors/v6/actors/builtin/power"

"github.com/filecoin-project/lotus/api"
lapi "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/blockstore"
Expand Down Expand Up @@ -1232,7 +1231,7 @@ func ActorCompactAllocatedCmd(getActor ActorAddressGetter) *cli.Command {
}
}

func isController(mi api.MinerInfo, addr address.Address) bool {
func isController(mi lapi.MinerInfo, addr address.Address) bool {
if addr == mi.Owner || addr == mi.Worker {
return true
}
Expand Down
9 changes: 0 additions & 9 deletions cmd/curio/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,6 @@ var configEditCmd = &cli.Command{
},
}

func getDefaultConfig(comment bool) (string, error) {
c := config.DefaultCurioConfig()
cb, err := config.ConfigUpdate(c, nil, config.Commented(comment), config.DefaultKeepUncommented(), config.NoEnv())
if err != nil {
return "", err
}
return string(cb), nil
}

func diff(sourceConf, newConf string) (string, error) {
lpSrc := config.DefaultCurioConfig()
lpNew := config.DefaultCurioConfig()
Expand Down
77 changes: 41 additions & 36 deletions cmd/curio/config_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"

"github.com/BurntSushi/toml"
"github.com/fatih/color"
"github.com/samber/lo"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/filecoin-project/lotus/chain/types"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/lotus/cmd/curio/deps"
"github.com/filecoin-project/lotus/cmd/curio/guidedsetup"
"github.com/filecoin-project/lotus/node/config"
"github.com/filecoin-project/lotus/node/repo"
)
Expand All @@ -36,10 +36,8 @@ var configNewCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
configColor := color.New(color.FgHiGreen).SprintFunc()

if cctx.Args().Len() < 1 {
return xerrors.New("must specify at least one SP actor address. Use 'lotus-shed miner create'")
return xerrors.New("must specify at least one SP actor address. Use 'lotus-shed miner create' or use 'curio guided-setup'")
}

ctx := cctx.Context
Expand All @@ -58,22 +56,12 @@ var configNewCmd = &cli.Command{
var titles []string
err = db.Select(ctx, &titles, `SELECT title FROM harmony_config WHERE LENGTH(config) > 0`)
if err != nil {
return fmt.Errorf("miner cannot reach the db. Ensure the config toml's HarmonyDB entry"+
" is setup to reach Yugabyte correctly: %s", err.Error())
return fmt.Errorf("cannot reach the db. Ensure that Yugabyte flags are set correctly to"+
" reach Yugabyte: %s", err.Error())
}

name := cctx.String("to-layer")
if name == "" {
name = fmt.Sprintf("cluster%d", len(titles))
} else {
if lo.Contains(titles, name) && !cctx.Bool("overwrite") {
return xerrors.New("the overwrite flag is needed to replace existing layer: " + name)
}
}
msg := "Layer " + configColor(name) + ` created. `

// setup config
lpCfg := config.DefaultCurioConfig()
curioConfig := config.DefaultCurioConfig()

for _, addr := range cctx.Args().Slice() {
maddr, err := address.NewFromString(addr)
Expand All @@ -86,7 +74,7 @@ var configNewCmd = &cli.Command{
return xerrors.Errorf("Failed to get miner info: %w", err)
}

lpCfg.Addresses = append(lpCfg.Addresses, config.CurioAddresses{
curioConfig.Addresses = append(curioConfig.Addresses, config.CurioAddresses{
PreCommitControl: nil,
CommitControl: nil,
TerminateControl: nil,
Expand All @@ -102,7 +90,7 @@ var configNewCmd = &cli.Command{
return err
}

lpCfg.Apis.StorageRPCSecret = base64.StdEncoding.EncodeToString(sk)
curioConfig.Apis.StorageRPCSecret = base64.StdEncoding.EncodeToString(sk)
}

{
Expand All @@ -116,44 +104,61 @@ var configNewCmd = &cli.Command{
return err
}

lpCfg.Apis.ChainApiInfo = append(lpCfg.Apis.ChainApiInfo, fmt.Sprintf("%s:%s", string(token), ainfo.Addr))
curioConfig.Apis.ChainApiInfo = append(curioConfig.Apis.ChainApiInfo, fmt.Sprintf("%s:%s", string(token), ainfo.Addr))
}

// write config

configTOML := &bytes.Buffer{}
if err = toml.NewEncoder(configTOML).Encode(lpCfg); err != nil {
if err = toml.NewEncoder(configTOML).Encode(curioConfig); err != nil {
return err
}

// If no base layer is present
if !lo.Contains(titles, "base") {
cfg, err := getDefaultConfig(true)
cb, err := config.ConfigUpdate(curioConfig, nil, config.Commented(true), config.DefaultKeepUncommented(), config.NoEnv())
if err != nil {
return xerrors.Errorf("Cannot get default config: %w", err)
return xerrors.Errorf("Failed to generate default config: %w", err)
}
cfg := string(cb)
_, err = db.Exec(ctx, "INSERT INTO harmony_config (title, config) VALUES ('base', $1)", cfg)

if err != nil {
return err
return xerrors.Errorf("failed to insert the 'base' into the database: %w", err)
}
fmt.Printf("The base layer has been updated with miner[s] %s\n", cctx.Args().Slice())
return nil
}

if cctx.Bool("overwrite") {
i, err := db.Exec(ctx, "DELETE FROM harmony_config WHERE title=$1", name)
if i != 0 {
fmt.Println("Overwriting existing layer")
}
if err != nil {
fmt.Println("Got error while deleting existing layer: " + err.Error())
}
// if base layer is present
var baseCfg = config.DefaultCurioConfig()
var baseText string
err = db.QueryRow(ctx, "SELECT config FROM harmony_config WHERE title='base'").Scan(&baseText)
if err != nil {
return xerrors.Errorf("Cannot load base config: %w", err)
}
guidedsetup.EnsureEmptyArrays(baseCfg)
_, err = deps.LoadConfigWithUpgrades(baseText, baseCfg)
if err != nil {
return xerrors.Errorf("Cannot load base config: %w", err)
}

_, err = db.Exec(ctx, "INSERT INTO harmony_config (title, config) VALUES ($1, $2)", name, configTOML.String())
for i := range curioConfig.Addresses {
baseCfg.Addresses = append(baseCfg.Addresses, curioConfig.Addresses[i])
baseCfg.Addresses = lo.Filter(baseCfg.Addresses, func(a config.CurioAddresses, _ int) bool {
return len(a.MinerAddresses) > 0
})
}

cb, err := config.ConfigUpdate(baseCfg, config.DefaultCurioConfig(), config.Commented(true), config.DefaultKeepUncommented(), config.NoEnv())
if err != nil {
return err
return xerrors.Errorf("cannot interpret config: %w", err)
}
_, err = db.Exec(ctx, "UPDATE harmony_config SET config=$1 WHERE title='base'", string(cb))
if err != nil {
return xerrors.Errorf("cannot update base config: %w", err)
}

fmt.Println(msg)
fmt.Printf("The base layer has been updated with miner[s] %s\n", cctx.Args().Slice())
return nil
},
}
40 changes: 36 additions & 4 deletions cmd/curio/guidedsetup/guidedsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/cli/spcli"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/lotus/cmd/curio/deps"
_ "github.com/filecoin-project/lotus/cmd/curio/internal/translations"
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
"github.com/filecoin-project/lotus/node/config"
Expand Down Expand Up @@ -183,7 +184,7 @@ func SetupLanguage() (func(key message.Reference, a ...interface{}) string, func

func newOrMigrate(d *MigrationData) {
i, _, err := (&promptui.Select{
Label: d.T("Choose if you with to create a new miner or migrate from existing Lotus-Miner"),
Label: d.T("I want to:"),
Items: []string{
d.T("Migrate from existing Lotus-Miner"),
d.T("Create a new miner")},
LexLuthr marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -770,6 +771,7 @@ func stepNewMinerConfig(d *MigrationData) {

if !lo.Contains(titles, "base") {
c := config.DefaultCurioConfig()
c.Addresses[0].MinerAddresses = []string{d.MinerID.String()}
cb, err := config.ConfigUpdate(c, nil, config.Commented(true), config.DefaultKeepUncommented(), config.NoEnv())
if err != nil {
d.say(notice, "Failed to generate default config: %s", err.Error())
Expand All @@ -783,16 +785,46 @@ func stepNewMinerConfig(d *MigrationData) {
d.say(notice, "Please do not run guided-setup again as miner creation is not idempotent. You need to run 'curio config new-cluster %s' to finish the configuration", d.MinerID.String())
os.Exit(1)
}
stepCompleted(d, d.T("Configuration 'base' was updated to include this miner's address"))
return
}

_, err = d.DB.Exec(d.ctx, "INSERT INTO harmony_config (title, config) VALUES ($1, $2)", d.MinerID.String(), configTOML.String())
baseCfg := &config.CurioConfig{}
baseText := ""

err = d.DB.QueryRow(d.ctx, "SELECT config FROM harmony_config WHERE title='base'").Scan(&baseText)
if err != nil {
d.say(notice, "Cannot load base config: %s", err.Error())
d.say(notice, "Please do not run guided-setup again as miner creation is not idempotent. You need to run 'curio config new-cluster %s' to finish the configuration", d.MinerID.String())
os.Exit(1)
}
EnsureEmptyArrays(baseCfg)
LexLuthr marked this conversation as resolved.
Show resolved Hide resolved
_, err = deps.LoadConfigWithUpgrades(baseText, baseCfg)
if err != nil {
d.say(notice, "Failed to load base config: %s", err.Error())
d.say(notice, "Please do not run guided-setup again as miner creation is not idempotent. You need to run 'curio config new-cluster %s' to finish the configuration", d.MinerID.String())
os.Exit(1)
}

baseCfg.Addresses = append(baseCfg.Addresses, curioCfg.Addresses[0])
baseCfg.Addresses = lo.Filter(baseCfg.Addresses, func(a config.CurioAddresses, _ int) bool {
return len(a.MinerAddresses) > 0
})

cb, err := config.ConfigUpdate(baseCfg, config.DefaultCurioConfig(), config.Commented(true), config.DefaultKeepUncommented(), config.NoEnv())
if err != nil {
d.say(notice, "Failed to regenerate base config: %s", err.Error())
d.say(notice, "Please do not run guided-setup again as miner creation is not idempotent. You need to run 'curio config new-cluster %s' to finish the configuration", d.MinerID.String())
os.Exit(1)
}
_, err = d.DB.Exec(d.ctx, "UPDATE harmony_config SET config=$1 WHERE title='base'", string(cb))
if err != nil {
d.say(notice, "Failed to insert '%s' config layer in database: %s", d.MinerID.String(), err.Error())
d.say(notice, "Failed to insert 'base' config layer in database: %s", err.Error())
d.say(notice, "Please do not run guided-setup again as miner creation is not idempotent. You need to run 'curio config new-cluster %s' to finish the configuration", d.MinerID.String())
os.Exit(1)
}

stepCompleted(d, d.T("New Curio configuration layer '%s' created", d.MinerID.String()))
stepCompleted(d, d.T("Configuration 'base' was updated to include this miner's address"))
}

func getDBDetails(d *MigrationData) *config.HarmonyDB {
Expand Down
6 changes: 3 additions & 3 deletions cmd/curio/guidedsetup/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func SaveConfigToLayer(minerRepoPath, layerName string, overwrite bool, chainApi
}
curioCfg := config.DefaultCurioConfig()

ensureEmptyArrays(curioCfg)
EnsureEmptyArrays(curioCfg)
_, err = deps.LoadConfigWithUpgrades(string(buf), curioCfg)

if err != nil {
Expand Down Expand Up @@ -149,7 +149,7 @@ func SaveConfigToLayer(minerRepoPath, layerName string, overwrite bool, chainApi
if err != nil {
return minerAddress, xerrors.Errorf("Cannot load base config: %w", err)
}
ensureEmptyArrays(baseCfg)
EnsureEmptyArrays(baseCfg)
_, err := deps.LoadConfigWithUpgrades(baseText, baseCfg)
if err != nil {
return minerAddress, xerrors.Errorf("Cannot load base config: %w", err)
Expand Down Expand Up @@ -242,7 +242,7 @@ func getDBSettings(smCfg config.StorageMiner) string {
return dbSettings
}

func ensureEmptyArrays(cfg *config.CurioConfig) {
func EnsureEmptyArrays(cfg *config.CurioConfig) {
if cfg.Addresses == nil {
cfg.Addresses = []config.CurioAddresses{}
} else {
snadrus marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading
Loading