Skip to content

Commit

Permalink
Merge pull request #253 from 0chain/feat/update_validator_settings
Browse files Browse the repository at this point in the history
Feat/update validator settings to access new endpoint
  • Loading branch information
Kishan-Dhakan authored Jul 4, 2022
2 parents 6d483d8 + 132278a commit 26753e2
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ When you run the `zbox` command in terminal with no arguments, it will list all
[alloc-cancel](#cancel-allocation)|Cancel an allocation
[alloc-fini](#finalise-allocation)|Finalize an expired allocation
[bl-info](#detailed-blobber-information)|Get blobber info
[validator-info](#detailed-validator-information)|Get validator info
[bl-update](#update-blobber-settings)|Update blobber settings by its delegate\_wallet owner
[validator-update](#update-validator-settings)|Update validator settings by its delegate\_wallet owner
[commit](#commit)| commit file changes to chain
[collect-reward](#collect-rewards)|transfer reward tokens from a stake pool to your wallet.
[copy](#copy)|copy an object(file/folder) to another folder on blobbers
Expand Down
182 changes: 182 additions & 0 deletions cmd/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package cmd

import (
"fmt"
"log"

"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/zboxcli/util"

"github.com/spf13/cobra"
)

func printValidators(nodes []*sdk.Validator) {
if len(nodes) == 0 {
fmt.Println("no validators registered yet")
return
}
for _, validator := range nodes {
fmt.Println("id: ", validator.ID)
fmt.Println("url: ", validator.BaseURL)
fmt.Println("settings:")
fmt.Println(" delegate_wallet:", validator.DelegateWallet)
fmt.Println(" min_stake: ", validator.MinStake)
fmt.Println(" max_stake: ", validator.MaxStake)
fmt.Println(" num_delegates: ", validator.NumDelegates)
fmt.Println(" service_charge: ", validator.ServiceCharge*100, "%")
}
}

// lsBlobers shows active blobbers
var lsValidators = &cobra.Command{
Use: "ls-validators",
Short: "Show active Validators.",
Long: `Show active Validators in the network.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
doJSON, _ := cmd.Flags().GetBool("json")

var list, err = sdk.GetValidators()

if err != nil {
log.Fatalf("Failed to get storage SC configurations: %v", err)
}

if doJSON {
util.PrintJSON(list)
} else {
printValidators(list)
}
},
}

var validatorInfoCmd = &cobra.Command{
Use: "validator-info",
Short: "Get validator info",
Long: `Get validator info`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {

var (
flags = cmd.Flags()

json bool
validatorID string
err error
)

if flags.Changed("json") {
if json, err = flags.GetBool("json"); err != nil {
log.Fatal("invalid 'json' flag: ", err)
}
}

if !flags.Changed("validator_id") {
log.Fatal("missing required 'validator_id' flag")
}

if validatorID, err = flags.GetString("validator_id"); err != nil {
log.Fatal("error in 'validator_id' flag: ", err)
}

var validator *sdk.Validator
if validator, err = sdk.GetValidator(validatorID); err != nil {
log.Fatal(err)
}

if json {
util.PrintJSON(validator)
} else {
printValidators([]*sdk.Validator{validator})
}

},
}

var validatorUpdateCmd = &cobra.Command{
Use: "validator-update",
Short: "Update validator settings by its delegate_wallet owner",
Long: `Update validator settings by its delegate_wallet owner`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()

validatorID string
err error
)

if !flags.Changed("validator_id") {
log.Fatal("missing required 'validator_id' flag")
}

if validatorID, err = flags.GetString("validator_id"); err != nil {
log.Fatal("error in 'validator_id' flag: ", err)
}

var validator *sdk.Validator
if validator, err = sdk.GetValidator(validatorID); err != nil {
log.Fatal(err)
}

if flags.Changed("min_stake") {
var minStake float64
if minStake, err = flags.GetFloat64("min_stake"); err != nil {
log.Fatal(err)
}
validator.MinStake = common.ToBalance(minStake)
}

if flags.Changed("max_stake") {
var maxStake float64
if maxStake, err = flags.GetFloat64("max_stake"); err != nil {
log.Fatal(err)
}
validator.MaxStake = common.ToBalance(maxStake)
}

if flags.Changed("num_delegates") {
var nd int
if nd, err = flags.GetInt("num_delegates"); err != nil {
log.Fatal(err)
}
validator.NumDelegates = nd
}

if flags.Changed("service_charge") {
var sc float64
if sc, err = flags.GetFloat64("service_charge"); err != nil {
log.Fatal(err)
}
validator.ServiceCharge = sc
}

if _, _, err = sdk.UpdateValidatorSettings(validator); err != nil {
log.Fatal(err)
}

fmt.Println("validator settings updated successfully")
},
}

func init() {
rootCmd.AddCommand(validatorUpdateCmd)
rootCmd.AddCommand(validatorInfoCmd)
rootCmd.AddCommand(lsValidators)

validatorInfoCmd.Flags().String("validator_id", "", "validator ID, required")
validatorInfoCmd.Flags().Bool("json", false,
"pass this option to print response as json data")
validatorInfoCmd.MarkFlagRequired("validator_id")

buf := validatorUpdateCmd.Flags()
buf.String("validator_id", "", "validator ID, required")
buf.Float64("min_stake", 0.0, "update min_stake, optional")
buf.Float64("max_stake", 0.0, "update max_stake, optional")
buf.Int("num_delegates", 0, "update num_delegates, optional")
buf.Float64("service_charge", 0.0, "update service_charge, optional")
validatorUpdateCmd.MarkFlagRequired("validator_id")

lsValidators.Flags().Bool("json", false, "pass this flag to get response as json object")
}

0 comments on commit 26753e2

Please sign in to comment.