Skip to content

Commit

Permalink
feat(ioctl): add znode commands configs
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun committed Nov 21, 2023
1 parent e89262e commit 27f9da0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
4 changes: 4 additions & 0 deletions ioctl/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type Config struct {
Language string `json:"language" yaml:"language"`
Nsv2height uint64 `json:"nsv2height" yaml:"nsv2height"`
AnalyserEndpoint string `json:"analyserEndpoint" yaml:"analyserEndpoint"`
// ZnodeEndpint IoTeX Zero-node endpint
ZnodeEndpoint string `json:"znodeEndpoint" yaml:"znodeEndpoint"`
// ZnodeContractAddress znode contract address
ZnodeContractAddress string `json:"znodeContractAddress" yaml:"znodeContractAddress"`
}

var (
Expand Down
12 changes: 12 additions & 0 deletions ioctl/config/configsetget.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
_localPattern = "localhost"
_endpointPattern = "(" + _ipPattern + "|(" + _domainPattern + ")" + "|(" + _localPattern + "))" + `(:\d{1,5})?`
_defaultAnalyserEndpoint = "https://iotex-analyser-api-mainnet.chainanalytics.org"
// _defaultZnodeContractAddress default znode contract address
_defaultZnodeContractAddress = "0x190Cc9af23504ac5Dc461376C1e2319bc3B9cD29"
)

var (
Expand Down Expand Up @@ -148,6 +150,10 @@ func Get(arg string) error {
fmt.Println(ReadConfig.Nsv2height)
case "analyserEndpoint":
fmt.Println(ReadConfig.AnalyserEndpoint)
case "znodeEndpoint":
fmt.Println(ReadConfig.ZnodeEndpoint)
case "znodeContractAddress":
fmt.Println(ReadConfig.ZnodeContractAddress)
case "all":
fmt.Println(ReadConfig.String())
}
Expand Down Expand Up @@ -276,6 +282,10 @@ func set(args []string) error {
return output.NewError(output.ValidationError, "invalid height", nil)
}
ReadConfig.Nsv2height = height
case "znodeEndpoint":
ReadConfig.ZnodeEndpoint = args[1]
case "znodeContractAddress":
ReadConfig.ZnodeContractAddress = args[1]
}
err := writeConfig()
if err != nil {
Expand All @@ -294,6 +304,8 @@ func reset() error {
ReadConfig.Explorer = "iotexscan"
ReadConfig.Language = "English"
ReadConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
ReadConfig.ZnodeEndpoint = ""
ReadConfig.ZnodeContractAddress = _defaultZnodeContractAddress

err := writeConfig()
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions ioctl/newcmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (
_endpointPattern = "(" + _ipPattern + "|(" + _domainPattern + ")" + "|(" + _localPattern + "))" + `(:\d{1,5})?`
_defaultAnalyserEndpoint = "https://iotex-analyser-api-mainnet.chainanalytics.org"
_defaultConfigFileName = "config.default"
// _defaultZnodeContractAddress default znode contract address
_defaultZnodeContractAddress = "0x190Cc9af23504ac5Dc461376C1e2319bc3B9cD29"
)

var (
Expand Down Expand Up @@ -115,6 +117,10 @@ func InitConfig() (config.Config, string, error) {
info.readConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
completeness = false
}
if info.readConfig.ZnodeContractAddress == "" {
info.readConfig.ZnodeContractAddress = _defaultZnodeContractAddress
completeness = false
}
if !completeness {
if err = info.writeConfig(); err != nil {
return info.readConfig, info.defaultConfigFile, err
Expand Down Expand Up @@ -144,6 +150,8 @@ func (c *info) reset() error {
c.readConfig.Explorer = _validExpl[0]
c.readConfig.Language = _supportedLanguage[0]
c.readConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
c.readConfig.ZnodeEndpoint = ""
c.readConfig.ZnodeContractAddress = _defaultZnodeContractAddress

err := c.writeConfig()
if err != nil {
Expand Down Expand Up @@ -202,6 +210,10 @@ func (c *info) set(args []string, insecure bool, client ioctl.Client) (string, e
return "", errors.Wrapf(err, "invalid height %d", height)
}
c.readConfig.Nsv2height = height
case "znodeEndpoint":
c.readConfig.ZnodeEndpoint = args[1]
case "znodeContractAddress":
c.readConfig.ZnodeContractAddress = args[1]
default:
return "", config.ErrConfigNotMatch
}
Expand Down Expand Up @@ -237,6 +249,10 @@ func (c *info) get(arg string) (string, error) {
return strconv.FormatUint(c.readConfig.Nsv2height, 10), nil
case "analyserEndpoint":
return c.readConfig.AnalyserEndpoint, nil
case "znodeEndpoint":
return c.readConfig.ZnodeEndpoint, nil
case "znodeContractAddress":
return c.readConfig.ZnodeContractAddress, nil
case "all":
return jsonString(c.readConfig)
default:
Expand Down
54 changes: 39 additions & 15 deletions ioctl/newcmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ func TestConfigGet(t *testing.T) {
require := require.New(t)
testPath := t.TempDir()
info := newInfo(config.Config{
Wallet: testPath,
SecureConnect: true,
Aliases: make(map[string]string),
DefaultAccount: config.Context{AddressOrAlias: "test"},
Explorer: "iotexscan",
Language: "English",
AnalyserEndpoint: "testAnalyser",
Wallet: testPath,
SecureConnect: true,
Aliases: make(map[string]string),
DefaultAccount: config.Context{AddressOrAlias: "test"},
Explorer: "iotexscan",
Language: "English",
AnalyserEndpoint: "testAnalyser",
ZnodeEndpoint: "testZnodeEndpoint",
ZnodeContractAddress: "testZnodeContractAddress",
}, testPath)

tcs := []struct {
Expand Down Expand Up @@ -88,9 +90,17 @@ func TestConfigGet(t *testing.T) {
"analyserEndpoint",
"testAnalyser",
},
{
"znodeEndpoint",
"testZnodeEndpoint",
},
{
"znodeContractAddress",
"testZnodeContractAddress",
},
{
"all",
"\"endpoint\": \"\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 0,\n \"analyserEndpoint\": \"testAnalyser\"\n}",
"\"endpoint\": \"\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 0,\n \"analyserEndpoint\": \"testAnalyser\",\n \"znodeEndpoint\": \"testZnodeEndpoint\",\n \"znodeContractAddress\": \"testZnodeContractAddress\"\n}",
},
}

Expand All @@ -110,13 +120,15 @@ func TestConfigReset(t *testing.T) {
cfgFile := fmt.Sprintf("%s/%s", cfgDir, "config.test")

info := newInfo(config.Config{
Wallet: "wallet",
Endpoint: "testEndpoint",
SecureConnect: false,
DefaultAccount: config.Context{AddressOrAlias: ""},
Explorer: "explorer",
Language: "Croatian",
AnalyserEndpoint: "testAnalyser",
Wallet: "wallet",
Endpoint: "testEndpoint",
SecureConnect: false,
DefaultAccount: config.Context{AddressOrAlias: ""},
Explorer: "explorer",
Language: "Croatian",
AnalyserEndpoint: "testAnalyser",
ZnodeEndpoint: "testZnodeEndpoint",
ZnodeContractAddress: "testZnodeContractAddress",
}, cfgFile)

// write the config to the temp dir and then reset
Expand All @@ -131,6 +143,8 @@ func TestConfigReset(t *testing.T) {
require.Equal("testAnalyser", cfg.AnalyserEndpoint)
require.Equal("explorer", cfg.Explorer)
require.Equal(config.Context{AddressOrAlias: ""}, cfg.DefaultAccount)
require.Equal("testZnodeEndpoint", cfg.ZnodeEndpoint)
require.Equal("testZnodeContractAddress", cfg.ZnodeContractAddress)

require.NoError(info.reset())
require.NoError(info.loadConfig())
Expand All @@ -144,6 +158,8 @@ func TestConfigReset(t *testing.T) {
require.Equal(_defaultAnalyserEndpoint, resetCfg.AnalyserEndpoint)
require.Equal("iotexscan", resetCfg.Explorer)
require.Equal(*new(config.Context), resetCfg.DefaultAccount)
require.Equal("", resetCfg.ZnodeEndpoint)
require.Equal(_defaultZnodeContractAddress, resetCfg.ZnodeContractAddress)
}

func TestConfigSet(t *testing.T) {
Expand Down Expand Up @@ -209,6 +225,14 @@ func TestConfigSet(t *testing.T) {
[]string{"unknownField", ""},
"no matching config",
},
{
[]string{"znodeEndpoint", "testZnodeEndpoint"},
"testZnodeEndpoint",
},
{
[]string{"znodeContractAddress", "testZnodeContractAddress"},
"testZnodeContractAddress",
},
}

for _, tc := range tcs {
Expand Down

0 comments on commit 27f9da0

Please sign in to comment.