Skip to content

Commit

Permalink
fix: add config-host-enable + config-sync-chaininfo cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
laocheng.cheng committed Mar 10, 2022
1 parent f76f7e0 commit 55484c1
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
2 changes: 2 additions & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func TestCommands(t *testing.T) {
"/config/show",
//"/config/profile",
//"/config/profile/apply",
"/config/storage-host-enable",
"/config/sync-chain-info",
"/config/optin",
"/config/optout",
"/dag",
Expand Down
103 changes: 101 additions & 2 deletions core/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"

"github.com/bittorrent/go-btfs/chain"
"github.com/bittorrent/go-btfs/core/commands/cmdenv"
"github.com/bittorrent/go-btfs/repo"
"github.com/bittorrent/go-btfs/repo/fsrepo"
Expand Down Expand Up @@ -64,8 +66,10 @@ Set the value of the 'Datastore.Path' key:
"edit": configEditCmd,
"replace": configReplaceCmd,
//"profile": configProfileCmd,
"optin": optInCmd,
"optout": optOutCmd,
"storage-host-enable": storageHostEnableCmd,
"sync-chain-info": SyncChainInfoCmd,
"optin": optInCmd,
"optout": optOutCmd,
},
Arguments: []cmds.Argument{
cmds.StringArg("key", true, false, "The key of the config entry (e.g. \"Addresses.API\")."),
Expand Down Expand Up @@ -303,6 +307,54 @@ can't be undone.
},
}

var storageHostEnableCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "host is or not.",
},
Arguments: []cmds.Argument{
cmds.StringArg("enable", true, false, "host is or not."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
enable, err := strconv.ParseBool(req.Arguments[0])
if err != nil {
return err
}

cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}

err = SetConfigStorageHostEnable(cfgRoot, enable)
if err != nil {
return err
}

fmt.Println("set storage-host-enable =", enable)
return nil
},
}

var SyncChainInfoCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "sync chain info.",
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}
chainInfo := chain.ChainObject
err = SyncConfigChainInfo(cfgRoot, &chainInfo)
if err != nil {
return err
}

fmt.Println("sync chain info ok.")
return nil
},
}

var configProfileCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Apply profiles to config.",
Expand Down Expand Up @@ -542,6 +594,53 @@ func transformConfig(configRoot string, configName string, transformer config.Tr
return oldCfg, newCfg, nil
}

func SyncConfigChainInfo(configRoot string, chainInfo *chain.ChainInfo) error {
r, err := fsrepo.Open(configRoot)
if err != nil {
return err
}
defer r.Close()

cfg, err := r.Config()
if err != nil {
return err
}
cfg.ChainInfo.ChainId = chainInfo.ChainID
cfg.ChainInfo.CurrentFactory = chainInfo.Chainconfig.CurrentFactory.Hex()
cfg.ChainInfo.PriceOracleAddress = chainInfo.Chainconfig.PriceOracleAddress.Hex()
cfg.ChainInfo.Endpoint = chainInfo.Chainconfig.Endpoint

cfg.Identity.BttcAddr = chainInfo.OverlayAddress.Hex()

err = r.SetConfig(cfg)
if err != nil {
return err
}

return nil
}

func SetConfigStorageHostEnable(configRoot string, enable bool) error {
r, err := fsrepo.Open(configRoot)
if err != nil {
return err
}
defer r.Close()

cfg, err := r.Config()
if err != nil {
return err
}
cfg.Experimental.StorageHostEnabled = enable

err = r.SetConfig(cfg)
if err != nil {
return err
}

return nil
}

func getConfig(r repo.Repo, key string) (*ConfigField, error) {
value, err := r.GetConfigKey(key)
if err != nil {
Expand Down

0 comments on commit 55484c1

Please sign in to comment.