From c6afbddb8b7353e660c08a05a0dc13673c2b0dbe Mon Sep 17 00:00:00 2001 From: frankonly Date: Tue, 26 Mar 2019 15:41:06 -0700 Subject: [PATCH 1/2] Update node delegate cmd for ioctl --- cli/ioctl/cmd/account/accountbalance.go | 2 +- cli/ioctl/cmd/node/node.go | 4 +- cli/ioctl/cmd/node/nodedelegate.go | 152 ++++++++++++------------ cli/ioctl/cmd/version/version.go | 8 +- 4 files changed, 85 insertions(+), 81 deletions(-) diff --git a/cli/ioctl/cmd/account/accountbalance.go b/cli/ioctl/cmd/account/accountbalance.go index 32768c10e4..b2941e82f0 100644 --- a/cli/ioctl/cmd/account/accountbalance.go +++ b/cli/ioctl/cmd/account/accountbalance.go @@ -38,7 +38,7 @@ func balance(args []string) string { } balance, ok := big.NewInt(0).SetString(accountMeta.Balance, 10) if !ok { - return "failed to convert balance to big int" + return "failed to convert balance into big int" } return fmt.Sprintf("%s: %s IOTX", address, util.RauToString(balance, util.IotxDecimalNum)) diff --git a/cli/ioctl/cmd/node/node.go b/cli/ioctl/cmd/node/node.go index ec1f4b05f5..af4a81eb10 100644 --- a/cli/ioctl/cmd/node/node.go +++ b/cli/ioctl/cmd/node/node.go @@ -23,7 +23,7 @@ var NodeCmd = &cobra.Command{ } func init() { - //NodeCmd.AddCommand(nodeDelegateCmd) + NodeCmd.AddCommand(nodeDelegateCmd) NodeCmd.AddCommand(nodeRewardCmd) - //nodeDelegateCmd.Flags().Uint64VarP(&epochNum, "epoch-num", "e", 0, "query specific epoch") + nodeDelegateCmd.Flags().Uint64VarP(&epochNum, "epoch-num", "e", 0, "specify specific epoch") } diff --git a/cli/ioctl/cmd/node/nodedelegate.go b/cli/ioctl/cmd/node/nodedelegate.go index 550d2edb47..87b498952b 100644 --- a/cli/ioctl/cmd/node/nodedelegate.go +++ b/cli/ioctl/cmd/node/nodedelegate.go @@ -6,76 +6,82 @@ package node -//// nodeDelegateCmd represents the node delegate command -//var nodeDelegateCmd = &cobra.Command{ -// Use: "delegate [ALIAS|DELEGATE_ADDRESS]", -// Short: "list delegates and number of blocks produced", -// Args: cobra.MaximumNArgs(1), -// Run: func(cmd *cobra.Command, args []string) { -// fmt.Println(delegate(args)) -// }, -//} -// -//func delegate(args []string) string { -// delegate := "" -// var err error -// if len(args) != 0 { -// delegate, err = alias.Address(args[0]) -// if err != nil { -// return err.Error() -// } -// } -// if epochNum == 0 { -// chainMeta, err := bc.GetChainMeta() -// if err != nil { -// return err.Error() -// } -// epochNum = chainMeta.Epoch.Num -// } -// conn, err := util.ConnectToEndpoint() -// if err != nil { -// return err.Error() -// } -// defer conn.Close() -// cli := iotexapi.NewAPIServiceClient(conn) -// request := &iotexapi.GetEpochMetaRequest{EpochNumber: epochNum} -// ctx := context.Background() -// epochResponse, err := cli.GetEpochMeta(ctx, request) -// if err != nil { -// return err.Error() -// } -// response := epochResponse.Productivity -// if len(delegate) != 0 { -// delegateAlias, err := alias.Alias(delegate) -// if err != nil && err != alias.ErrNoAliasFound { -// return err.Error() -// } -// return fmt.Sprintf("Epoch: %d, Total blocks: %d\n", epochNum, response.TotalBlks) + -// fmt.Sprintf("%s %s %d", delegate, delegateAlias, response.BlksPerDelegate[delegate]) -// } -// -// aliases := alias.GetAliasMap() -// formataliasLen := 0 -// for delegate := range response.BlksPerDelegate { -// if len(aliases[delegate]) > formataliasLen { -// formataliasLen = len(aliases[delegate]) -// } -// } -// lines := make([]string, 0) -// lines = append(lines, fmt.Sprintf("Epoch: %d, Total blocks: %d\n", -// epochNum, response.TotalBlks)) -// if formataliasLen == 0 { -// lines = append(lines, fmt.Sprintf("%-41s %s", "Address", "Blocks")) -// for delegate, productivity := range response.BlksPerDelegate { -// lines = append(lines, fmt.Sprintf("%-41s %d", delegate, productivity)) -// } -// } else { -// formatTitleString := "%-41s %-" + strconv.Itoa(formataliasLen) + "s %s" -// formatDataString := "%-41s %-" + strconv.Itoa(formataliasLen) + "s %d" -// lines = append(lines, fmt.Sprintf(formatTitleString, "Address", "Alias", "Blocks")) -// for delegate, productivity := range response.BlksPerDelegate { -// lines = append(lines, fmt.Sprintf(formatDataString, delegate, aliases[delegate], productivity)) -// } -// } -// return strings.Join(lines, "\n") -//} +import ( + "context" + "fmt" + "math/big" + "strconv" + "strings" + + "github.com/spf13/cobra" + + "github.com/iotexproject/iotex-core/cli/ioctl/cmd/alias" + "github.com/iotexproject/iotex-core/cli/ioctl/cmd/bc" + "github.com/iotexproject/iotex-core/cli/ioctl/util" + "github.com/iotexproject/iotex-core/protogen/iotexapi" +) + +// nodeDelegateCmd represents the node delegate command +var nodeDelegateCmd = &cobra.Command{ + Use: "delegate", + Short: "print consensus delegates information in certain epoch", + Args: cobra.MaximumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(delegate(args)) + }, +} + +func delegate(args []string) string { + status := map[bool]string{true: "active", false: ""} + if epochNum == 0 { + chainMeta, err := bc.GetChainMeta() + if err != nil { + return err.Error() + } + epochNum = chainMeta.Epoch.Num + } + conn, err := util.ConnectToEndpoint() + if err != nil { + return err.Error() + } + defer conn.Close() + cli := iotexapi.NewAPIServiceClient(conn) + request := &iotexapi.GetEpochMetaRequest{EpochNumber: epochNum} + ctx := context.Background() + response, err := cli.GetEpochMeta(ctx, request) + if err != nil { + return err.Error() + } + + epockData := response.EpochData + aliases := alias.GetAliasMap() + formataliasLen := 0 + for _, delegateInfo := range response.BlockProducersInfo { + if len(aliases[delegateInfo.Address]) > formataliasLen { + formataliasLen = len(aliases[delegateInfo.Address]) + } + } + lines := make([]string, 0) + lines = append(lines, fmt.Sprintf("Epoch: %d, Start block height: %d, Total blocks: %d\n", + epockData.Num, epockData.Height, response.TotalBlocks)) + formatTitleString := "%-41s %-5s %-" + strconv.Itoa(formataliasLen) + + "s %-6s %-6s %s" + formatDataString := "%-41s %5d %-" + strconv.Itoa(formataliasLen) + + "s %-6s %-6s %s" + lines = append(lines, fmt.Sprintf(formatTitleString, + "Address", "Index", "Alias", "Status", "Blocks", "Votes")) + for index, bp := range response.BlockProducersInfo { + votes, ok := big.NewInt(0).SetString(bp.Votes, 10) + if !ok { + return "failed to convert votes into big int" + } + production := "" + if bp.Active == true { + production = strconv.Itoa(int(bp.Production)) + } + lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, index+1, + aliases[bp.Address], status[bp.Active], production, + util.RauToString(votes, util.IotxDecimalNum))) + } + return strings.Join(lines, "\n") +} diff --git a/cli/ioctl/cmd/version/version.go b/cli/ioctl/cmd/version/version.go index 434ef1d6e1..ff4ab63397 100644 --- a/cli/ioctl/cmd/version/version.go +++ b/cli/ioctl/cmd/version/version.go @@ -37,7 +37,7 @@ func version() string { GoVersion: ver.GoVersion, BuidTime: ver.BuildTime, } - res := fmt.Sprintf("Client:\n%+v\n\n", versionInfo) + fmt.Printf("Client:\n%+v\n\n", versionInfo) conn, err := util.ConnectToEndpoint() if err != nil { return err.Error() @@ -48,9 +48,7 @@ func version() string { ctx := context.Background() response, err := cli.GetServerMeta(ctx, request) if err != nil { - res += "failed to get version from server: " + err.Error() - } else { - res += fmt.Sprintf("Server: %s\n%+v", config.ReadConfig.Endpoint, response.ServerMeta) + return "failed to get version from server: " + err.Error() } - return res + return fmt.Sprintf("Server: %s\n%+v", config.ReadConfig.Endpoint, response.ServerMeta) } From c84b30eed347cb3714fe34e00de2ef9cf56b688a Mon Sep 17 00:00:00 2001 From: frankonly Date: Tue, 26 Mar 2019 16:36:01 -0700 Subject: [PATCH 2/2] resolve --- cli/ioctl/cmd/node/nodedelegate.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/ioctl/cmd/node/nodedelegate.go b/cli/ioctl/cmd/node/nodedelegate.go index 87b498952b..e8979e222d 100644 --- a/cli/ioctl/cmd/node/nodedelegate.go +++ b/cli/ioctl/cmd/node/nodedelegate.go @@ -27,11 +27,11 @@ var nodeDelegateCmd = &cobra.Command{ Short: "print consensus delegates information in certain epoch", Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - fmt.Println(delegate(args)) + fmt.Println(delegate()) }, } -func delegate(args []string) string { +func delegate() string { status := map[bool]string{true: "active", false: ""} if epochNum == 0 { chainMeta, err := bc.GetChainMeta() @@ -76,7 +76,7 @@ func delegate(args []string) string { return "failed to convert votes into big int" } production := "" - if bp.Active == true { + if bp.Active { production = strconv.Itoa(int(bp.Production)) } lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, index+1,