diff --git a/ioctl/newcmd/bc/bc.go b/ioctl/newcmd/bc/bc.go index e36922af66..333a6f7c03 100644 --- a/ioctl/newcmd/bc/bc.go +++ b/ioctl/newcmd/bc/bc.go @@ -58,9 +58,7 @@ func GetChainMeta(client ioctl.Client) (*iotextypes.ChainMeta, error) { if err == nil { ctx = metautils.NiceMD(jwtMD).ToOutgoing(context.Background()) } - chainMetaResponse, err := apiServiceClient.GetChainMeta(ctx, &iotexapi.GetChainMetaRequest{}) - if err != nil { sta, ok := status.FromError(err) if ok { @@ -77,13 +75,12 @@ func GetEpochMeta(client ioctl.Client, epochNum uint64) (*iotexapi.GetEpochMetaR if err != nil { return nil, err } - ctx := context.Background() jwtMD, err := util.JwtAuth() if err == nil { ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx) } - epochMetaresponse, err := apiServiceClient.GetEpochMeta(ctx, &iotexapi.GetEpochMetaRequest{}) + epochMetaresponse, err := apiServiceClient.GetEpochMeta(ctx, &iotexapi.GetEpochMetaRequest{EpochNumber: epochNum}) if err != nil { sta, ok := status.FromError(err) if ok { diff --git a/ioctl/newcmd/node/nodedelegate.go b/ioctl/newcmd/node/nodedelegate.go index 68473452a8..a2e3cde2af 100644 --- a/ioctl/newcmd/node/nodedelegate.go +++ b/ioctl/newcmd/node/nodedelegate.go @@ -91,22 +91,21 @@ func NewNodeDelegateCmd(client ioctl.Client) *cobra.Command { if nextEpoch { //nextDelegates //deprecated: It won't be able to query next delegate after Easter height, because it will be determined at the end of the epoch. - apiServiceClient, err := client.APIServiceClient() + currEpochNum, err := currEpochNum(client) if err != nil { return err } - chainMeta, err := bc.GetChainMeta(client) - if err != nil { - return errors.Wrap(err, "failed to get chain meta") - } - epochNum = chainMeta.GetEpoch().GetNum() + 1 + epochNum = currEpochNum + 1 message := nextDelegatesMessage{Epoch: int(epochNum)} ctx := context.Background() - jwtMD, err := util.JwtAuth() - if err == nil { + if jwtMD, err := util.JwtAuth(); err == nil { ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx) } + apiServiceClient, err := client.APIServiceClient() + if err != nil { + return err + } abpResponse, err := apiServiceClient.ReadState( ctx, &iotexapi.ReadStateRequest{ @@ -140,7 +139,6 @@ func NewNodeDelegateCmd(client ioctl.Client) *cobra.Command { Arguments: [][]byte{[]byte(strconv.FormatUint(epochNum, 10))}, }, ) - if err != nil { sta, ok := status.FromError(err) if ok { @@ -152,6 +150,7 @@ func NewNodeDelegateCmd(client ioctl.Client) *cobra.Command { if err := bps.Deserialize(bpResponse.Data); err != nil { return errors.Wrap(err, "failed to deserialize bps") } + isActive := make(map[string]bool) for _, abp := range abps { isActive[abp.Address] = true @@ -169,12 +168,13 @@ func NewNodeDelegateCmd(client ioctl.Client) *cobra.Command { } cmd.Println(message.String(epochNum)) } else { + // specfic epoch-num if epochNum == 0 { - chainMeta, err := bc.GetChainMeta(client) + currEpochNum, err := currEpochNum(client) if err != nil { - return errors.Wrap(err, "failed to get chain meta") + return err } - epochNum = chainMeta.GetEpoch().GetNum() + epochNum = currEpochNum } response, err := bc.GetEpochMeta(client, epochNum) @@ -182,7 +182,7 @@ func NewNodeDelegateCmd(client ioctl.Client) *cobra.Command { return errors.Wrap(err, "failed to get epoch meta") } if response.EpochData == nil { - return errors.New("ROLLDPOS is not registered") + return errors.New("rolldpos is not registered") } epochData := response.EpochData aliases := client.AliasMap() @@ -221,6 +221,7 @@ func NewNodeDelegateCmd(client ioctl.Client) *cobra.Command { return nil }, } + cmd.Flags().Uint64VarP(&epochNum, "epoch-num", "e", 0, flagEpochNumUsage) cmd.Flags().BoolVarP(&nextEpoch, "next-epoch", "n", false, @@ -242,14 +243,14 @@ func (m *nextDelegatesMessage) String(epochNum uint64) string { formatTitleString := "%-41s %-4s %-" + strconv.Itoa(aliasLen) + "s %-6s %s" formatDataString := "%-41s %4d %-" + strconv.Itoa(aliasLen) + "s %-6s %s" lines = append(lines, fmt.Sprintf(formatTitleString, "Address", "Rank", "Alias", "Status", "Votes")) + + var status string for _, bp := range m.Delegates { if bp.Active { - lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, bp.Rank, - bp.Alias, "active", bp.Votes)) - } else { - lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, bp.Rank, - bp.Alias, "false", bp.Votes)) + status = "active" } + lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, bp.Rank, + bp.Alias, status, bp.Votes)) } return strings.Join(lines, "\n") } @@ -267,14 +268,32 @@ func (m *delegatesMessage) String() string { formatDataString := "%-41s %4d %-" + strconv.Itoa(aliasLen) + "s %-6s %-6d %-12s %s" lines = append(lines, fmt.Sprintf(formatTitleString, "Address", "Rank", "Alias", "Status", "Blocks", "ProbatedStatus", "Votes")) + + var ( + status string + probatedStatus string + ) for _, bp := range m.Delegates { if bp.Active { - lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, bp.Rank, - bp.Alias, "active", bp.Production, "probated", bp.Votes)) - } else { - lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, bp.Rank, - bp.Alias, "false", bp.Production, "", bp.Votes)) + status = "active" + } + if bp.ProbatedStatus { + probatedStatus = "probated" } + lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, bp.Rank, + bp.Alias, status, bp.Production, probatedStatus, bp.Votes)) } return strings.Join(lines, "\n") } + +func currEpochNum(client ioctl.Client) (uint64, error) { + chainMeta, err := bc.GetChainMeta(client) + if err != nil { + return 0, errors.Wrap(err, "failed to get chain meta") + } + epoch := chainMeta.Epoch + if epoch == nil { + return 0, errors.Wrap(err, "rolldpos is not registered") + } + return epoch.Num, nil +} diff --git a/ioctl/newcmd/node/nodedelegate_test.go b/ioctl/newcmd/node/nodedelegate_test.go index 5fc7c847a8..37f9aee22d 100644 --- a/ioctl/newcmd/node/nodedelegate_test.go +++ b/ioctl/newcmd/node/nodedelegate_test.go @@ -5,7 +5,6 @@ package node import ( - "strings" "testing" "github.com/golang/mock/gomock" @@ -95,6 +94,5 @@ func TestNewNodeDelegateCmd(t *testing.T) { require.NoError(err) require.Contains(result, "io13q2am9nedrd3n746lsj6qan4pymcpgm94vvx2c") require.Contains(result, "81497052.527306018062463878") - require.Equal(12, len(strings.Split(result, "false"))-1) }) }