Skip to content

Commit

Permalink
[ioctl/newcmd] fix node delegate run failed (#3729)
Browse files Browse the repository at this point in the history
* fix grpc error and show right datas

* check chainMeta.Epoch

* modify as currEpochNum
  • Loading branch information
huof6829 committed Dec 30, 2022
1 parent 6a93361 commit b56a665
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
5 changes: 1 addition & 4 deletions ioctl/newcmd/bc/bc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
65 changes: 42 additions & 23 deletions ioctl/newcmd/node/nodedelegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -169,20 +168,21 @@ 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)
if err != nil {
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()
Expand Down Expand Up @@ -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,
Expand All @@ -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")
}
Expand All @@ -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
}
2 changes: 0 additions & 2 deletions ioctl/newcmd/node/nodedelegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package node

import (
"strings"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -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)
})
}

0 comments on commit b56a665

Please sign in to comment.