github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/node/nodeprobationlist.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package node
     7  
     8  import (
     9  	"encoding/json"
    10  	"fmt"
    11  
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  
    15  	"github.com/iotexproject/iotex-core/action/protocol/vote"
    16  	"github.com/iotexproject/iotex-core/ioctl"
    17  	"github.com/iotexproject/iotex-core/ioctl/config"
    18  	"github.com/iotexproject/iotex-core/ioctl/newcmd/bc"
    19  )
    20  
    21  // Multi-language support
    22  var (
    23  	_probationlistCmdUses = map[config.Language]string{
    24  		config.English: "probationlist [-e epoch-num]",
    25  		config.Chinese: "probationlist [-e epoch数]",
    26  	}
    27  	_probationlistCmdShorts = map[config.Language]string{
    28  		config.English: "Print probation list at given epoch",
    29  		config.Chinese: "打印给定epoch内的试用名单",
    30  	}
    31  )
    32  
    33  // NewNodeProbationlistCmd represents querying probation list command
    34  func NewNodeProbationlistCmd(client ioctl.Client) *cobra.Command {
    35  	var epochNum uint64
    36  	use, _ := client.SelectTranslation(_probationlistCmdUses)
    37  	short, _ := client.SelectTranslation(_probationlistCmdShorts)
    38  	flagEpochNumUsages, _ := client.SelectTranslation(_flagEpochNumUsages)
    39  
    40  	cmd := &cobra.Command{
    41  		Use:   use,
    42  		Short: short,
    43  		Args:  cobra.ExactArgs(0),
    44  		RunE: func(cmd *cobra.Command, args []string) error {
    45  			cmd.SilenceUsage = true
    46  			if epochNum == 0 {
    47  				chainMeta, err := bc.GetChainMeta(client)
    48  				if err != nil {
    49  					return errors.Wrap(err, "failed to get chain meta")
    50  				}
    51  				epochNum = chainMeta.GetEpoch().GetNum()
    52  			}
    53  			response, err := bc.GetEpochMeta(client, epochNum)
    54  			if err != nil {
    55  				return errors.Wrap(err, "failed to get epoch meta")
    56  			}
    57  			if response.EpochData == nil {
    58  				return errors.New("ROLLDPOS is not registered")
    59  			}
    60  			probationlist, err := getProbationList(client, epochNum, response.EpochData.Height)
    61  			if err != nil {
    62  				return errors.Wrap(err, "failed to get probation list")
    63  			}
    64  			delegateList := make([]string, 0)
    65  			for addr := range probationlist.ProbationInfo {
    66  				delegateList = append(delegateList, addr)
    67  			}
    68  			byteAsJSON, err := json.MarshalIndent(delegateList, "", "  ")
    69  			if err != nil {
    70  				return err
    71  			}
    72  			cmd.Printf("EpochNumber : %d, IntensityRate : %d%%\nProbationList : %s",
    73  				epochNum,
    74  				probationlist.IntensityRate,
    75  				fmt.Sprint(string(byteAsJSON)),
    76  			)
    77  			return nil
    78  		},
    79  	}
    80  	cmd.PersistentFlags().Uint64VarP(&epochNum, "epoch-num", "e", 0, flagEpochNumUsages)
    81  	return cmd
    82  }
    83  
    84  func getProbationList(client ioctl.Client, epochNum uint64, epochStartHeight uint64) (*vote.ProbationList, error) {
    85  	probationListRes, err := bc.GetProbationList(client, epochNum, epochStartHeight)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	probationList := &vote.ProbationList{}
    90  	if probationListRes != nil {
    91  		if err := probationList.Deserialize(probationListRes.Data); err != nil {
    92  			return nil, err
    93  		}
    94  	}
    95  	return probationList, nil
    96  }