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

Update node delegate cmd for ioctl #852

Merged
merged 2 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cli/ioctl/cmd/account/accountbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions cli/ioctl/cmd/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
152 changes: 79 additions & 73 deletions cli/ioctl/cmd/node/nodedelegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Frankonly marked this conversation as resolved.
Show resolved Hide resolved
Use: "delegate",
Short: "print consensus delegates information in certain epoch",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(delegate())
},
}

func delegate() 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 {
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")
}
8 changes: 3 additions & 5 deletions cli/ioctl/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}