From 4ae3fe32c065dd232a6ec0b75600b714afb9f8f4 Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Sat, 28 May 2022 03:35:49 +0800 Subject: [PATCH 1/6] [ioctl] refactor nodereward command in new ioctl --- ioctl/newcmd/node/nodereward.go | 69 ++++++++++++--------------------- 1 file changed, 24 insertions(+), 45 deletions(-) diff --git a/ioctl/newcmd/node/nodereward.go b/ioctl/newcmd/node/nodereward.go index 51b672e2b1..a06e0ea0b1 100644 --- a/ioctl/newcmd/node/nodereward.go +++ b/ioctl/newcmd/node/nodereward.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019 IoTeX Foundation +// Copyright (c) 2022 IoTeX Foundation // This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no // warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent // permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache @@ -11,6 +11,7 @@ import ( "fmt" "math/big" + "github.com/pkg/errors" "github.com/spf13/cobra" "google.golang.org/grpc/status" @@ -18,7 +19,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl" "github.com/iotexproject/iotex-core/ioctl/config" - "github.com/iotexproject/iotex-core/ioctl/output" "github.com/iotexproject/iotex-core/ioctl/util" ) @@ -39,13 +39,13 @@ var ( ) // NewNodeRewardCmd represents the node reward command -func NewNodeRewardCmd(c ioctl.Client) *cobra.Command { +func NewNodeRewardCmd(client ioctl.Client) *cobra.Command { var endpoint string var insecure bool - use, _ := c.SelectTranslation(_rewardUses) - short, _ := c.SelectTranslation(_rewardShorts) - rewardPoolMessageTranslation, _ := c.SelectTranslation(_rewardPoolMessageTranslations) + use, _ := client.SelectTranslation(_rewardUses) + short, _ := client.SelectTranslation(_rewardShorts) + rewardPoolMessageTranslation, _ := client.SelectTranslation(_rewardPoolMessageTranslations) nc := &cobra.Command{ Use: use, Short: short, @@ -54,8 +54,7 @@ func NewNodeRewardCmd(c ioctl.Client) *cobra.Command { cmd.SilenceUsage = true var err error if len(args) == 0 { - - apiClient, err := c.APIServiceClient(ioctl.APIServiceConfig{Endpoint: endpoint, Insecure: insecure}) + apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{Endpoint: endpoint, Insecure: insecure}) if err != nil { return err } @@ -71,14 +70,14 @@ func NewNodeRewardCmd(c ioctl.Client) *cobra.Command { if err != nil { sta, ok := status.FromError(err) if ok { - return output.NewError(output.APIError, sta.Message(), nil) + return errors.New(sta.Message()) } - return output.NewError(output.NetworkError, "failed to invoke ReadState api", err) + return errors.New("failed to invoke ReadState api") } availableRewardRau, ok := new(big.Int).SetString(string(response.Data), 10) if !ok { - return output.NewError(output.ConvertError, "failed to convert string into big int", err) + return errors.New("failed to convert string into big int") } response, err = apiClient.ReadState( @@ -91,28 +90,28 @@ func NewNodeRewardCmd(c ioctl.Client) *cobra.Command { if err != nil { sta, ok := status.FromError(err) if ok { - return output.NewError(output.APIError, sta.Message(), nil) + return errors.New(sta.Message()) } - return output.NewError(output.NetworkError, "failed to invoke ReadState api", err) + return errors.New("failed to invoke ReadState api") } totalRewardRau, ok := new(big.Int).SetString(string(response.Data), 10) if !ok { - return output.NewError(output.ConvertError, "failed to convert string into big int", err) + return errors.New("failed to convert string into big int") } message := rewardPoolMessage{ AvailableReward: util.RauToString(availableRewardRau, util.IotxDecimalNum), TotalReward: util.RauToString(totalRewardRau, util.IotxDecimalNum), } - fmt.Println(message.String(rewardPoolMessageTranslation)) + cmd.Println(message.String(rewardPoolMessageTranslation)) } else { arg := args[0] - address, err := c.Address(arg) + address, err := client.Address(arg) if err != nil { - return output.NewError(output.AddressError, "failed to get address", err) + return errors.New("failed to get address") } - apiClient, err := c.APIServiceClient(ioctl.APIServiceConfig{ + apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{ Endpoint: endpoint, Insecure: insecure, }) @@ -131,19 +130,18 @@ func NewNodeRewardCmd(c ioctl.Client) *cobra.Command { if err != nil { sta, ok := status.FromError(err) if ok { - return output.NewError(output.APIError, sta.Message(), nil) + return errors.New(sta.Message()) } - return output.NewError(output.NetworkError, "failed to get version from server", err) + return errors.New("failed to get version from server") } rewardRau, ok := new(big.Int).SetString(string(response.Data), 10) if !ok { - return output.NewError(output.ConvertError, "failed to convert string into big int", err) + return errors.New("failed to convert string into big int") } - message := rewardMessage{Address: address, Reward: util.RauToString(rewardRau, util.IotxDecimalNum)} - fmt.Println(message.String()) - + cmd.Println(fmt.Sprintf("%s: %s IOTX", address, util.RauToString(rewardRau, util.IotxDecimalNum))) } - return output.PrintError(err) + cmd.Println(err) + return nil }, } return nc @@ -155,24 +153,5 @@ type rewardPoolMessage struct { } func (m *rewardPoolMessage) String(trans ...string) string { - - if output.Format == "" { - message := fmt.Sprintf(trans[0], - m.AvailableReward, m.TotalReward) - return message - } - return output.FormatStringWithTrans(output.Result, m) -} - -type rewardMessage struct { - Address string `json:"address"` - Reward string `json:"reward"` -} - -func (m *rewardMessage) String(trans ...string) string { - if output.Format == "" { - message := fmt.Sprintf("%s: %s IOTX", m.Address, m.Reward) - return message - } - return output.FormatStringWithTrans(output.Result, m) + return fmt.Sprintf(trans[0], m.AvailableReward, m.TotalReward) } From 9b7afdf6f8107dcf70c06a366609d66fa11ac28c Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Sat, 28 May 2022 04:56:20 +0800 Subject: [PATCH 2/6] refactor unit test to cover the modification --- ioctl/newcmd/node/nodereward.go | 9 +- ioctl/newcmd/node/nodereward_test.go | 155 ++++++++++++++++++++------- 2 files changed, 121 insertions(+), 43 deletions(-) diff --git a/ioctl/newcmd/node/nodereward.go b/ioctl/newcmd/node/nodereward.go index a06e0ea0b1..c823e59448 100644 --- a/ioctl/newcmd/node/nodereward.go +++ b/ioctl/newcmd/node/nodereward.go @@ -33,8 +33,8 @@ var ( config.Chinese: "查询奖励", } _rewardPoolMessageTranslations = map[config.Language]string{ - config.English: "Available Reward: %s IOTX Total Reward: %s IOTX", - config.Chinese: "可用奖金: %s IOTX 总奖金: %s IOTX", + config.English: "Available Reward: %s IOTX, Total Reward: %s IOTX", + config.Chinese: "可用奖金: %s IOTX,总奖金: %s IOTX", } ) @@ -46,7 +46,7 @@ func NewNodeRewardCmd(client ioctl.Client) *cobra.Command { use, _ := client.SelectTranslation(_rewardUses) short, _ := client.SelectTranslation(_rewardShorts) rewardPoolMessageTranslation, _ := client.SelectTranslation(_rewardPoolMessageTranslations) - nc := &cobra.Command{ + cmd := &cobra.Command{ Use: use, Short: short, Args: cobra.MaximumNArgs(1), @@ -104,7 +104,6 @@ func NewNodeRewardCmd(client ioctl.Client) *cobra.Command { TotalReward: util.RauToString(totalRewardRau, util.IotxDecimalNum), } cmd.Println(message.String(rewardPoolMessageTranslation)) - } else { arg := args[0] address, err := client.Address(arg) @@ -144,7 +143,7 @@ func NewNodeRewardCmd(client ioctl.Client) *cobra.Command { return nil }, } - return nc + return cmd } type rewardPoolMessage struct { diff --git a/ioctl/newcmd/node/nodereward_test.go b/ioctl/newcmd/node/nodereward_test.go index 65cf381e68..d0489e20fa 100644 --- a/ioctl/newcmd/node/nodereward_test.go +++ b/ioctl/newcmd/node/nodereward_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019 IoTeX Foundation +// Copyright (c) 2022 IoTeX Foundation // This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no // warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent // permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache @@ -12,6 +12,7 @@ import ( "github.com/golang/mock/gomock" "github.com/iotexproject/iotex-proto/golang/iotexapi" "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" + "github.com/pkg/errors" "github.com/stretchr/testify/require" "github.com/iotexproject/iotex-core/ioctl" @@ -21,53 +22,131 @@ import ( ) func TestNewNodeRewardCmd(t *testing.T) { + require := require.New(t) ctrl := gomock.NewController(t) - client := mock_ioctlclient.NewMockClient(ctrl) - client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() - client.EXPECT().Address(gomock.Any()).Return("test_address", nil).Times(1) + client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(21) apiClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("UnclaimedBalance"), - Arguments: [][]byte{[]byte("test_address")}, - }).Return(&iotexapi.ReadStateResponse{ - Data: []byte("0")}, - nil) - - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("AvailableBalance"), - }).Return(&iotexapi.ReadStateResponse{ - Data: []byte("24361490367906930338205776")}, - nil) - - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("TotalBalance"), - }).Return(&iotexapi.ReadStateResponse{ - Data: []byte("52331682309272536203174665")}, - nil) - var endpoint string var insecure bool client.EXPECT().APIServiceClient(ioctl.APIServiceConfig{ Endpoint: endpoint, Insecure: insecure, - }).Return(apiClient, nil).AnyTimes() - - cmd := NewNodeRewardCmd(client) - - result, err := util.ExecuteCmd(cmd, "test") - require.NotNil(t, result) - require.NoError(t, err) - - result, err = util.ExecuteCmd(cmd) - require.NotNil(t, result) - require.NoError(t, err) - + }).Return(apiClient, nil).Times(7) + + t.Run("get available reward & total reward", func(t *testing.T) { + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("AvailableBalance"), + }).Return(&iotexapi.ReadStateResponse{ + Data: []byte("24361490367906930338205776")}, + nil) + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("TotalBalance"), + }).Return(&iotexapi.ReadStateResponse{ + Data: []byte("52331682309272536203174665")}, + nil) + + cmd := NewNodeRewardCmd(client) + result, err := util.ExecuteCmd(cmd) + require.NoError(err) + require.Contains(result, "24361490.367906930338205776") + require.Contains(result, "52331682.309272536203174665") + }) + + t.Run("get balance by address", func(t *testing.T) { + client.EXPECT().Address(gomock.Any()).Return("test_address", nil).Times(1) + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("UnclaimedBalance"), + Arguments: [][]byte{[]byte("test_address")}, + }).Return(&iotexapi.ReadStateResponse{ + Data: []byte("0"), + }, nil) + + cmd := NewNodeRewardCmd(client) + result, err := util.ExecuteCmd(cmd, "test") + require.NoError(err) + require.Contains(result, "test_address: 0 IOTX") + }) + + t.Run("failed to invoke AvailableBalance api", func(t *testing.T) { + expectedErr := errors.New("failed to invoke ReadState api") + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("AvailableBalance"), + }).Return(nil, expectedErr) + + cmd := NewNodeRewardCmd(client) + _, err := util.ExecuteCmd(cmd) + require.Contains(err.Error(), expectedErr.Error()) + }) + + t.Run("failed to convert string into big int", func(t *testing.T) { + expectedErr := errors.New("failed to convert string into big int") + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("AvailableBalance"), + }).Return(&iotexapi.ReadStateResponse{ + Data: []byte("0x24361490367906930338205776")}, + nil) + + cmd := NewNodeRewardCmd(client) + _, err := util.ExecuteCmd(cmd) + require.Contains(err.Error(), expectedErr.Error()) + }) + + t.Run("failed to invoke TotalBalance api", func(t *testing.T) { + expectedErr := errors.New("failed to invoke ReadState api") + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("AvailableBalance"), + }).Return(&iotexapi.ReadStateResponse{ + Data: []byte("24361490367906930338205776")}, + nil) + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("TotalBalance"), + }).Return(nil, expectedErr) + + cmd := NewNodeRewardCmd(client) + _, err := util.ExecuteCmd(cmd) + require.Contains(err.Error(), expectedErr.Error()) + }) + + t.Run("failed to get address", func(t *testing.T) { + expectedErr := errors.New("failed to get address") + client.EXPECT().Address(gomock.Any()).Return("", expectedErr) + + cmd := NewNodeRewardCmd(client) + _, err := util.ExecuteCmd(cmd, "test") + require.Contains(err.Error(), expectedErr.Error()) + }) + + t.Run("failed to get version from server", func(t *testing.T) { + expectedErr := errors.New("failed to get version from server") + + client.EXPECT().Address(gomock.Any()).Return("test_address", nil).Times(1) + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("UnclaimedBalance"), + Arguments: [][]byte{[]byte("test_address")}, + }).Return(nil, expectedErr) + + cmd := NewNodeRewardCmd(client) + _, err := util.ExecuteCmd(cmd, "test") + require.Contains(err.Error(), expectedErr.Error()) + }) } From ed17455281ae2f74c07726b2a7f5b8701f6bcad2 Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Thu, 2 Jun 2022 10:09:24 +0800 Subject: [PATCH 3/6] refactor new nodereward as ioctl/cmd/nodereward structure --- ioctl/newcmd/node/nodereward.go | 227 ++++++++++++++++++-------------- 1 file changed, 125 insertions(+), 102 deletions(-) diff --git a/ioctl/newcmd/node/nodereward.go b/ioctl/newcmd/node/nodereward.go index c823e59448..565420f569 100644 --- a/ioctl/newcmd/node/nodereward.go +++ b/ioctl/newcmd/node/nodereward.go @@ -24,133 +24,156 @@ import ( // Multi-language support var ( - _rewardUses = map[config.Language]string{ - config.English: "reward [ALIAS|DELEGATE_ADDRESS]", - config.Chinese: "reward [别名|委托地址]", + _rewardCmdUses = map[config.Language]string{ + config.English: "reward unclaimed|pool [ALIAS|DELEGATE_ADDRESS]", + config.Chinese: "reward 未支取|奖金池 [别名|委托地址]", } - _rewardShorts = map[config.Language]string{ + _rewardCmdShorts = map[config.Language]string{ config.English: "Query rewards", config.Chinese: "查询奖励", } - _rewardPoolMessageTranslations = map[config.Language]string{ - config.English: "Available Reward: %s IOTX, Total Reward: %s IOTX", - config.Chinese: "可用奖金: %s IOTX,总奖金: %s IOTX", + _rewardPoolLong = map[config.Language]string{ + config.English: "ioctl node reward pool returns unclaimed and available Rewards in fund pool.\nTotalUnclaimed is the amount of all delegates that have been issued but are not claimed;\nTotalAvailable is the amount of balance that has not been issued to anyone.\n\nioctl node reward unclaimed [ALIAS|DELEGATE_ADDRESS] returns unclaimed rewards of a specific delegate.", + config.Chinese: "ioctl node reward 返回奖金池中的未支取奖励和可获取的奖励. TotalUnclaimed是所有代表已被发放但未支取的奖励的总和; TotalAvailable 是奖金池中未被发放的奖励的总和.\n\nioctl node [ALIAS|DELEGATE_ADDRESS] 返回特定代表的已被发放但未支取的奖励.", } ) // NewNodeRewardCmd represents the node reward command func NewNodeRewardCmd(client ioctl.Client) *cobra.Command { - var endpoint string - var insecure bool + use, _ := client.SelectTranslation(_rewardCmdUses) + short, _ := client.SelectTranslation(_rewardCmdShorts) + long, _ := client.SelectTranslation(_rewardPoolLong) - use, _ := client.SelectTranslation(_rewardUses) - short, _ := client.SelectTranslation(_rewardShorts) - rewardPoolMessageTranslation, _ := client.SelectTranslation(_rewardPoolMessageTranslations) - cmd := &cobra.Command{ + return &cobra.Command{ Use: use, Short: short, - Args: cobra.MaximumNArgs(1), + Args: cobra.RangeArgs(1, 2), + Long: long, RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true var err error - if len(args) == 0 { - apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{Endpoint: endpoint, Insecure: insecure}) - if err != nil { - return err - } - - response, err := apiClient.ReadState( - context.Background(), - &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("AvailableBalance"), - }, - ) - - if err != nil { - sta, ok := status.FromError(err) - if ok { - return errors.New(sta.Message()) - } - return errors.New("failed to invoke ReadState api") - } - - availableRewardRau, ok := new(big.Int).SetString(string(response.Data), 10) - if !ok { - return errors.New("failed to convert string into big int") - } - - response, err = apiClient.ReadState( - context.Background(), - &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("TotalBalance"), - }, - ) - if err != nil { - sta, ok := status.FromError(err) - if ok { - return errors.New(sta.Message()) - } - return errors.New("failed to invoke ReadState api") - } - totalRewardRau, ok := new(big.Int).SetString(string(response.Data), 10) - if !ok { - return errors.New("failed to convert string into big int") - } - - message := rewardPoolMessage{ - AvailableReward: util.RauToString(availableRewardRau, util.IotxDecimalNum), - TotalReward: util.RauToString(totalRewardRau, util.IotxDecimalNum), + switch args[0] { + case "pool": + if len(args) != 1 { + return errors.New("wrong number of arg(s) for ioctl node reward pool command. \nRun 'ioctl node reward --help' for usage.") } - cmd.Println(message.String(rewardPoolMessageTranslation)) - } else { - arg := args[0] - address, err := client.Address(arg) - if err != nil { - return errors.New("failed to get address") + err = rewardPool(client) + case "unclaimed": + if len(args) != 2 { + return errors.New("wrong number of arg(s) for ioctl node reward unclaimed [ALIAS|DELEGATE_ADDRESS] command. \nRun 'ioctl node reward --help' for usage.") } - apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{ - Endpoint: endpoint, - Insecure: insecure, - }) - if err != nil { - return err - } - - response, err := apiClient.ReadState( - context.Background(), - &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("UnclaimedBalance"), - Arguments: [][]byte{[]byte(address)}, - }, - ) - if err != nil { - sta, ok := status.FromError(err) - if ok { - return errors.New(sta.Message()) - } - return errors.New("failed to get version from server") - } - rewardRau, ok := new(big.Int).SetString(string(response.Data), 10) - if !ok { - return errors.New("failed to convert string into big int") - } - cmd.Println(fmt.Sprintf("%s: %s IOTX", address, util.RauToString(rewardRau, util.IotxDecimalNum))) + err = reward(client, args[1]) + default: + return errors.New("unknown command. \nRun 'ioctl node reward --help' for usage.") } cmd.Println(err) return nil }, } - return cmd } type rewardPoolMessage struct { - AvailableReward string `json:"availableReward"` - TotalReward string `json:"totalReward"` + TotalBalance string `json:"TotalBalance"` + TotalUnclaimed string `json:"TotalUnclaimed"` + TotalAvailable string `json:"TotalAvailable"` } -func (m *rewardPoolMessage) String(trans ...string) string { - return fmt.Sprintf(trans[0], m.AvailableReward, m.TotalReward) +func rewardPool(client ioctl.Client) error { + var endpoint string + var insecure bool + + apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{Endpoint: endpoint, Insecure: insecure}) + if err != nil { + return err + } + + response, err := apiClient.ReadState( + context.Background(), + &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("AvailableBalance"), + }, + ) + + if err != nil { + sta, ok := status.FromError(err) + if ok { + return errors.New(sta.Message()) + } + return errors.New("failed to invoke ReadState api") + } + + availableRewardRau, ok := new(big.Int).SetString(string(response.Data), 10) + if !ok { + return errors.New("failed to convert string into big int") + } + + response, err = apiClient.ReadState( + context.Background(), + &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("TotalBalance"), + }, + ) + if err != nil { + sta, ok := status.FromError(err) + if ok { + return errors.New(sta.Message()) + } + return errors.New("failed to invoke ReadState api") + } + totalRewardRau, ok := new(big.Int).SetString(string(response.Data), 10) + if !ok { + return errors.New("failed to convert string into big int") + } + + totalUnclaimedRewardRau := big.NewInt(0) + totalUnclaimedRewardRau.Sub(totalRewardRau, availableRewardRau) + message := rewardPoolMessage{ + TotalBalance: util.RauToString(totalRewardRau, util.IotxDecimalNum), + TotalUnclaimed: util.RauToString(totalUnclaimedRewardRau, util.IotxDecimalNum), + TotalAvailable: util.RauToString(availableRewardRau, util.IotxDecimalNum), + } + fmt.Printf("Total Unclaimed:\t %s IOTX\nTotal Available:\t %s IOTX\nTotal Balance:\t\t %s IOTX", + message.TotalUnclaimed, message.TotalAvailable, message.TotalBalance) + return err +} + +func reward(client ioctl.Client, arg string) error { + var endpoint string + var insecure bool + + address, err := client.Address(arg) + if err != nil { + return errors.New("failed to get address") + } + apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{ + Endpoint: endpoint, + Insecure: insecure, + }) + if err != nil { + return err + } + + response, err := apiClient.ReadState( + context.Background(), + &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("UnclaimedBalance"), + Arguments: [][]byte{[]byte(address)}, + }, + ) + if err != nil { + sta, ok := status.FromError(err) + if ok { + return errors.New(sta.Message()) + } + return errors.New("failed to get version from server") + } + rewardRau, ok := new(big.Int).SetString(string(response.Data), 10) + if !ok { + return errors.New("failed to convert string into big int") + } + fmt.Printf("%s: %s IOTX", address, util.RauToString(rewardRau, util.IotxDecimalNum)) + return err } From 1c7afcb62ba525e1dc4f101ec3bf56203f40c23c Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Thu, 2 Jun 2022 10:59:34 +0800 Subject: [PATCH 4/6] refactor unit test to cover the modification --- ioctl/newcmd/node/nodereward.go | 64 ++++---- ioctl/newcmd/node/nodereward_test.go | 220 ++++++++++++++------------- 2 files changed, 153 insertions(+), 131 deletions(-) diff --git a/ioctl/newcmd/node/nodereward.go b/ioctl/newcmd/node/nodereward.go index 565420f569..228505d1d1 100644 --- a/ioctl/newcmd/node/nodereward.go +++ b/ioctl/newcmd/node/nodereward.go @@ -8,7 +8,6 @@ package node import ( "context" - "fmt" "math/big" "github.com/pkg/errors" @@ -51,22 +50,29 @@ func NewNodeRewardCmd(client ioctl.Client) *cobra.Command { Long: long, RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true - var err error switch args[0] { case "pool": if len(args) != 1 { - return errors.New("wrong number of arg(s) for ioctl node reward pool command. \nRun 'ioctl node reward --help' for usage.") + return errors.New("wrong number of arg(s) for ioctl node reward pool command. \nRun 'ioctl node reward --help' for usage") } - err = rewardPool(client) + message, err := rewardPool(client) + if err != nil { + return err + } + cmd.Printf("Total Unclaimed:\t %s IOTX\nTotal Available:\t %s IOTX\nTotal Balance:\t\t %s IOTX", + message.TotalUnclaimed, message.TotalAvailable, message.TotalBalance) case "unclaimed": if len(args) != 2 { - return errors.New("wrong number of arg(s) for ioctl node reward unclaimed [ALIAS|DELEGATE_ADDRESS] command. \nRun 'ioctl node reward --help' for usage.") + return errors.New("wrong number of arg(s) for ioctl node reward unclaimed [ALIAS|DELEGATE_ADDRESS] command. \nRun 'ioctl node reward --help' for usage") + } + message, err := reward(client, args[1]) + if err != nil { + return err } - err = reward(client, args[1]) + cmd.Printf("%s: %s IOTX", message.Address, message.Reward) default: - return errors.New("unknown command. \nRun 'ioctl node reward --help' for usage.") + return errors.New("unknown command. \nRun 'ioctl node reward --help' for usage") } - cmd.Println(err) return nil }, } @@ -78,13 +84,18 @@ type rewardPoolMessage struct { TotalAvailable string `json:"TotalAvailable"` } -func rewardPool(client ioctl.Client) error { +type rewardMessage struct { + Address string `json:"address"` + Reward string `json:"reward"` +} + +func rewardPool(client ioctl.Client) (rewardPoolMessage, error) { var endpoint string var insecure bool apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{Endpoint: endpoint, Insecure: insecure}) if err != nil { - return err + return rewardPoolMessage{}, err } response, err := apiClient.ReadState( @@ -98,14 +109,14 @@ func rewardPool(client ioctl.Client) error { if err != nil { sta, ok := status.FromError(err) if ok { - return errors.New(sta.Message()) + return rewardPoolMessage{}, errors.New(sta.Message()) } - return errors.New("failed to invoke ReadState api") + return rewardPoolMessage{}, errors.New("failed to invoke ReadState api") } availableRewardRau, ok := new(big.Int).SetString(string(response.Data), 10) if !ok { - return errors.New("failed to convert string into big int") + return rewardPoolMessage{}, errors.New("failed to convert string into big int") } response, err = apiClient.ReadState( @@ -118,13 +129,13 @@ func rewardPool(client ioctl.Client) error { if err != nil { sta, ok := status.FromError(err) if ok { - return errors.New(sta.Message()) + return rewardPoolMessage{}, errors.New(sta.Message()) } - return errors.New("failed to invoke ReadState api") + return rewardPoolMessage{}, errors.New("failed to invoke ReadState api") } totalRewardRau, ok := new(big.Int).SetString(string(response.Data), 10) if !ok { - return errors.New("failed to convert string into big int") + return rewardPoolMessage{}, errors.New("failed to convert string into big int") } totalUnclaimedRewardRau := big.NewInt(0) @@ -134,25 +145,24 @@ func rewardPool(client ioctl.Client) error { TotalUnclaimed: util.RauToString(totalUnclaimedRewardRau, util.IotxDecimalNum), TotalAvailable: util.RauToString(availableRewardRau, util.IotxDecimalNum), } - fmt.Printf("Total Unclaimed:\t %s IOTX\nTotal Available:\t %s IOTX\nTotal Balance:\t\t %s IOTX", - message.TotalUnclaimed, message.TotalAvailable, message.TotalBalance) - return err + + return message, err } -func reward(client ioctl.Client, arg string) error { +func reward(client ioctl.Client, arg string) (rewardMessage, error) { var endpoint string var insecure bool address, err := client.Address(arg) if err != nil { - return errors.New("failed to get address") + return rewardMessage{}, errors.New("failed to get address") } apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{ Endpoint: endpoint, Insecure: insecure, }) if err != nil { - return err + return rewardMessage{}, err } response, err := apiClient.ReadState( @@ -166,14 +176,14 @@ func reward(client ioctl.Client, arg string) error { if err != nil { sta, ok := status.FromError(err) if ok { - return errors.New(sta.Message()) + return rewardMessage{}, errors.New(sta.Message()) } - return errors.New("failed to get version from server") + return rewardMessage{}, errors.New("failed to get version from server") } rewardRau, ok := new(big.Int).SetString(string(response.Data), 10) if !ok { - return errors.New("failed to convert string into big int") + return rewardMessage{}, errors.New("failed to convert string into big int") } - fmt.Printf("%s: %s IOTX", address, util.RauToString(rewardRau, util.IotxDecimalNum)) - return err + message := rewardMessage{Address: address, Reward: util.RauToString(rewardRau, util.IotxDecimalNum)} + return message, err } diff --git a/ioctl/newcmd/node/nodereward_test.go b/ioctl/newcmd/node/nodereward_test.go index d0489e20fa..cf472ab1fc 100644 --- a/ioctl/newcmd/node/nodereward_test.go +++ b/ioctl/newcmd/node/nodereward_test.go @@ -26,7 +26,7 @@ func TestNewNodeRewardCmd(t *testing.T) { ctrl := gomock.NewController(t) client := mock_ioctlclient.NewMockClient(ctrl) - client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(21) + client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(24) apiClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) @@ -38,115 +38,127 @@ func TestNewNodeRewardCmd(t *testing.T) { Insecure: insecure, }).Return(apiClient, nil).Times(7) - t.Run("get available reward & total reward", func(t *testing.T) { - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("AvailableBalance"), - }).Return(&iotexapi.ReadStateResponse{ - Data: []byte("24361490367906930338205776")}, - nil) - - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("TotalBalance"), - }).Return(&iotexapi.ReadStateResponse{ - Data: []byte("52331682309272536203174665")}, - nil) - - cmd := NewNodeRewardCmd(client) - result, err := util.ExecuteCmd(cmd) - require.NoError(err) - require.Contains(result, "24361490.367906930338205776") - require.Contains(result, "52331682.309272536203174665") - }) - - t.Run("get balance by address", func(t *testing.T) { - client.EXPECT().Address(gomock.Any()).Return("test_address", nil).Times(1) - - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("UnclaimedBalance"), - Arguments: [][]byte{[]byte("test_address")}, - }).Return(&iotexapi.ReadStateResponse{ - Data: []byte("0"), - }, nil) - - cmd := NewNodeRewardCmd(client) - result, err := util.ExecuteCmd(cmd, "test") - require.NoError(err) - require.Contains(result, "test_address: 0 IOTX") + t.Run("get node reward pool", func(t *testing.T) { + t.Run("get available reward & total reward", func(t *testing.T) { + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("AvailableBalance"), + }).Return(&iotexapi.ReadStateResponse{ + Data: []byte("24361490367906930338205776")}, + nil) + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("TotalBalance"), + }).Return(&iotexapi.ReadStateResponse{ + Data: []byte("52331682309272536203174665")}, + nil) + + cmd := NewNodeRewardCmd(client) + result, err := util.ExecuteCmd(cmd, "pool") + require.NoError(err) + require.Contains(result, "24361490.367906930338205776") + require.Contains(result, "52331682.309272536203174665") + }) + + t.Run("failed to invoke AvailableBalance api", func(t *testing.T) { + expectedErr := errors.New("failed to invoke ReadState api") + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("AvailableBalance"), + }).Return(nil, expectedErr) + + cmd := NewNodeRewardCmd(client) + _, err := util.ExecuteCmd(cmd, "pool") + require.Contains(err.Error(), expectedErr.Error()) + }) + + t.Run("failed to convert string into big int", func(t *testing.T) { + expectedErr := errors.New("failed to convert string into big int") + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("AvailableBalance"), + }).Return(&iotexapi.ReadStateResponse{ + Data: []byte("0x24361490367906930338205776")}, + nil) + + cmd := NewNodeRewardCmd(client) + _, err := util.ExecuteCmd(cmd, "pool") + require.Contains(err.Error(), expectedErr.Error()) + }) + + t.Run("failed to invoke TotalBalance api", func(t *testing.T) { + expectedErr := errors.New("failed to invoke ReadState api") + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("AvailableBalance"), + }).Return(&iotexapi.ReadStateResponse{ + Data: []byte("24361490367906930338205776")}, + nil) + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("TotalBalance"), + }).Return(nil, expectedErr) + + cmd := NewNodeRewardCmd(client) + _, err := util.ExecuteCmd(cmd, "pool") + require.Contains(err.Error(), expectedErr.Error()) + }) }) - t.Run("failed to invoke AvailableBalance api", func(t *testing.T) { - expectedErr := errors.New("failed to invoke ReadState api") - - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("AvailableBalance"), - }).Return(nil, expectedErr) - - cmd := NewNodeRewardCmd(client) - _, err := util.ExecuteCmd(cmd) - require.Contains(err.Error(), expectedErr.Error()) + t.Run("get unclaimed node reward", func(t *testing.T) { + t.Run("get balance by address", func(t *testing.T) { + client.EXPECT().Address(gomock.Any()).Return("test_address", nil).Times(1) + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("UnclaimedBalance"), + Arguments: [][]byte{[]byte("test_address")}, + }).Return(&iotexapi.ReadStateResponse{ + Data: []byte("0"), + }, nil) + + cmd := NewNodeRewardCmd(client) + result, err := util.ExecuteCmd(cmd, "unclaimed", "test") + require.NoError(err) + require.Contains(result, "test_address: 0 IOTX") + }) + + t.Run("failed to get address", func(t *testing.T) { + expectedErr := errors.New("failed to get address") + client.EXPECT().Address(gomock.Any()).Return("", expectedErr) + + cmd := NewNodeRewardCmd(client) + _, err := util.ExecuteCmd(cmd, "unclaimed", "test") + require.Contains(err.Error(), expectedErr.Error()) + }) + + t.Run("failed to get version from server", func(t *testing.T) { + expectedErr := errors.New("failed to get version from server") + + client.EXPECT().Address(gomock.Any()).Return("test_address", nil).Times(1) + + apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ + ProtocolID: []byte("rewarding"), + MethodName: []byte("UnclaimedBalance"), + Arguments: [][]byte{[]byte("test_address")}, + }).Return(nil, expectedErr) + + cmd := NewNodeRewardCmd(client) + _, err := util.ExecuteCmd(cmd, "unclaimed", "test") + require.Contains(err.Error(), expectedErr.Error()) + }) }) - t.Run("failed to convert string into big int", func(t *testing.T) { - expectedErr := errors.New("failed to convert string into big int") - - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("AvailableBalance"), - }).Return(&iotexapi.ReadStateResponse{ - Data: []byte("0x24361490367906930338205776")}, - nil) - - cmd := NewNodeRewardCmd(client) - _, err := util.ExecuteCmd(cmd) - require.Contains(err.Error(), expectedErr.Error()) - }) - - t.Run("failed to invoke TotalBalance api", func(t *testing.T) { - expectedErr := errors.New("failed to invoke ReadState api") - - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("AvailableBalance"), - }).Return(&iotexapi.ReadStateResponse{ - Data: []byte("24361490367906930338205776")}, - nil) - - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("TotalBalance"), - }).Return(nil, expectedErr) - - cmd := NewNodeRewardCmd(client) - _, err := util.ExecuteCmd(cmd) - require.Contains(err.Error(), expectedErr.Error()) - }) - - t.Run("failed to get address", func(t *testing.T) { - expectedErr := errors.New("failed to get address") - client.EXPECT().Address(gomock.Any()).Return("", expectedErr) - - cmd := NewNodeRewardCmd(client) - _, err := util.ExecuteCmd(cmd, "test") - require.Contains(err.Error(), expectedErr.Error()) - }) - - t.Run("failed to get version from server", func(t *testing.T) { - expectedErr := errors.New("failed to get version from server") - - client.EXPECT().Address(gomock.Any()).Return("test_address", nil).Times(1) - - apiClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ - ProtocolID: []byte("rewarding"), - MethodName: []byte("UnclaimedBalance"), - Arguments: [][]byte{[]byte("test_address")}, - }).Return(nil, expectedErr) + t.Run("unknown command", func(t *testing.T) { + expectedErr := errors.New("unknown command. \nRun 'ioctl node reward --help' for usage") cmd := NewNodeRewardCmd(client) - _, err := util.ExecuteCmd(cmd, "test") + _, err := util.ExecuteCmd(cmd, "") require.Contains(err.Error(), expectedErr.Error()) }) } From 2638aceb88f0074606c3396be82f2c27cf2855e1 Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Fri, 10 Jun 2022 19:10:40 +0800 Subject: [PATCH 5/6] delete useless message --- ioctl/newcmd/node/nodereward.go | 83 +++++++++++++++------------------ 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/ioctl/newcmd/node/nodereward.go b/ioctl/newcmd/node/nodereward.go index 228505d1d1..fd0adeac03 100644 --- a/ioctl/newcmd/node/nodereward.go +++ b/ioctl/newcmd/node/nodereward.go @@ -10,12 +10,12 @@ import ( "context" "math/big" + "github.com/grpc-ecosystem/go-grpc-middleware/util/metautils" + "github.com/iotexproject/iotex-proto/golang/iotexapi" "github.com/pkg/errors" "github.com/spf13/cobra" "google.golang.org/grpc/status" - "github.com/iotexproject/iotex-proto/golang/iotexapi" - "github.com/iotexproject/iotex-core/ioctl" "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" @@ -55,21 +55,21 @@ func NewNodeRewardCmd(client ioctl.Client) *cobra.Command { if len(args) != 1 { return errors.New("wrong number of arg(s) for ioctl node reward pool command. \nRun 'ioctl node reward --help' for usage") } - message, err := rewardPool(client) + totalUnclaimed, totalAvailable, totalBalance, err := rewardPool(client) if err != nil { return err } - cmd.Printf("Total Unclaimed:\t %s IOTX\nTotal Available:\t %s IOTX\nTotal Balance:\t\t %s IOTX", - message.TotalUnclaimed, message.TotalAvailable, message.TotalBalance) + cmd.Printf("Total Unclaimed:\t %s IOTX\nTotal Available:\t %s IOTX\nTotal Balance:\t\t %s IOTX\n", + totalUnclaimed, totalAvailable, totalBalance) case "unclaimed": if len(args) != 2 { return errors.New("wrong number of arg(s) for ioctl node reward unclaimed [ALIAS|DELEGATE_ADDRESS] command. \nRun 'ioctl node reward --help' for usage") } - message, err := reward(client, args[1]) + address, reward, err := reward(client, args[1]) if err != nil { return err } - cmd.Printf("%s: %s IOTX", message.Address, message.Reward) + cmd.Printf("%s: %s IOTX\n", address, reward) default: return errors.New("unknown command. \nRun 'ioctl node reward --help' for usage") } @@ -78,45 +78,37 @@ func NewNodeRewardCmd(client ioctl.Client) *cobra.Command { } } -type rewardPoolMessage struct { - TotalBalance string `json:"TotalBalance"` - TotalUnclaimed string `json:"TotalUnclaimed"` - TotalAvailable string `json:"TotalAvailable"` -} - -type rewardMessage struct { - Address string `json:"address"` - Reward string `json:"reward"` -} - -func rewardPool(client ioctl.Client) (rewardPoolMessage, error) { +func rewardPool(client ioctl.Client) (string, string, string, error) { var endpoint string var insecure bool apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{Endpoint: endpoint, Insecure: insecure}) if err != nil { - return rewardPoolMessage{}, err + return "", "", "", err + } + ctx := context.Background() + jwtMD, err := util.JwtAuth() + if err == nil { + ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx) } - response, err := apiClient.ReadState( - context.Background(), + ctx, &iotexapi.ReadStateRequest{ ProtocolID: []byte("rewarding"), MethodName: []byte("AvailableBalance"), }, ) - if err != nil { sta, ok := status.FromError(err) if ok { - return rewardPoolMessage{}, errors.New(sta.Message()) + return "", "", "", errors.New(sta.Message()) } - return rewardPoolMessage{}, errors.New("failed to invoke ReadState api") + return "", "", "", errors.Wrap(err, "failed to invoke ReadState api") } availableRewardRau, ok := new(big.Int).SetString(string(response.Data), 10) if !ok { - return rewardPoolMessage{}, errors.New("failed to convert string into big int") + return "", "", "", errors.New("failed to convert string into big int") } response, err = apiClient.ReadState( @@ -129,44 +121,46 @@ func rewardPool(client ioctl.Client) (rewardPoolMessage, error) { if err != nil { sta, ok := status.FromError(err) if ok { - return rewardPoolMessage{}, errors.New(sta.Message()) + return "", "", "", errors.New(sta.Message()) } - return rewardPoolMessage{}, errors.New("failed to invoke ReadState api") + return "", "", "", errors.Wrap(err, "failed to invoke ReadState api") } totalRewardRau, ok := new(big.Int).SetString(string(response.Data), 10) if !ok { - return rewardPoolMessage{}, errors.New("failed to convert string into big int") + return "", "", "", errors.New("failed to convert string into big int") } totalUnclaimedRewardRau := big.NewInt(0) totalUnclaimedRewardRau.Sub(totalRewardRau, availableRewardRau) - message := rewardPoolMessage{ - TotalBalance: util.RauToString(totalRewardRau, util.IotxDecimalNum), - TotalUnclaimed: util.RauToString(totalUnclaimedRewardRau, util.IotxDecimalNum), - TotalAvailable: util.RauToString(availableRewardRau, util.IotxDecimalNum), - } - return message, err + return util.RauToString(totalUnclaimedRewardRau, util.IotxDecimalNum), + util.RauToString(availableRewardRau, util.IotxDecimalNum), + util.RauToString(totalRewardRau, util.IotxDecimalNum), + err } -func reward(client ioctl.Client, arg string) (rewardMessage, error) { +func reward(client ioctl.Client, arg string) (string, string, error) { var endpoint string var insecure bool address, err := client.Address(arg) if err != nil { - return rewardMessage{}, errors.New("failed to get address") + return "", "", errors.Wrap(err, "failed to get address") } apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{ Endpoint: endpoint, Insecure: insecure, }) if err != nil { - return rewardMessage{}, err + return "", "", errors.Wrap(err, "failed to connect to endpoint") + } + ctx := context.Background() + jwtMD, err := util.JwtAuth() + if err == nil { + ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx) } - response, err := apiClient.ReadState( - context.Background(), + ctx, &iotexapi.ReadStateRequest{ ProtocolID: []byte("rewarding"), MethodName: []byte("UnclaimedBalance"), @@ -176,14 +170,13 @@ func reward(client ioctl.Client, arg string) (rewardMessage, error) { if err != nil { sta, ok := status.FromError(err) if ok { - return rewardMessage{}, errors.New(sta.Message()) + return "", "", errors.New(sta.Message()) } - return rewardMessage{}, errors.New("failed to get version from server") + return "", "", errors.Wrap(err, "failed to get version from server") } rewardRau, ok := new(big.Int).SetString(string(response.Data), 10) if !ok { - return rewardMessage{}, errors.New("failed to convert string into big int") + return "", "", errors.New("failed to convert string into big int") } - message := rewardMessage{Address: address, Reward: util.RauToString(rewardRau, util.IotxDecimalNum)} - return message, err + return address, util.RauToString(rewardRau, util.IotxDecimalNum), err } From 226360fe2ecd642a8d68cb2f5948a5bb8e3eef7c Mon Sep 17 00:00:00 2001 From: LuckyPigeon Date: Sat, 18 Jun 2022 22:24:58 +0800 Subject: [PATCH 6/6] fix client.APIServiceClient() --- ioctl/newcmd/node/nodereward.go | 13 ++----------- ioctl/newcmd/node/nodereward_test.go | 12 ++---------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/ioctl/newcmd/node/nodereward.go b/ioctl/newcmd/node/nodereward.go index fd0adeac03..3ab44fc421 100644 --- a/ioctl/newcmd/node/nodereward.go +++ b/ioctl/newcmd/node/nodereward.go @@ -79,10 +79,7 @@ func NewNodeRewardCmd(client ioctl.Client) *cobra.Command { } func rewardPool(client ioctl.Client) (string, string, string, error) { - var endpoint string - var insecure bool - - apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{Endpoint: endpoint, Insecure: insecure}) + apiClient, err := client.APIServiceClient() if err != nil { return "", "", "", err } @@ -140,17 +137,11 @@ func rewardPool(client ioctl.Client) (string, string, string, error) { } func reward(client ioctl.Client, arg string) (string, string, error) { - var endpoint string - var insecure bool - address, err := client.Address(arg) if err != nil { return "", "", errors.Wrap(err, "failed to get address") } - apiClient, err := client.APIServiceClient(ioctl.APIServiceConfig{ - Endpoint: endpoint, - Insecure: insecure, - }) + apiClient, err := client.APIServiceClient() if err != nil { return "", "", errors.Wrap(err, "failed to connect to endpoint") } diff --git a/ioctl/newcmd/node/nodereward_test.go b/ioctl/newcmd/node/nodereward_test.go index 0d2d69cde4..eed0d3c6b0 100644 --- a/ioctl/newcmd/node/nodereward_test.go +++ b/ioctl/newcmd/node/nodereward_test.go @@ -24,18 +24,10 @@ func TestNewNodeRewardCmd(t *testing.T) { require := require.New(t) ctrl := gomock.NewController(t) client := mock_ioctlclient.NewMockClient(ctrl) - - client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(24) - apiClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) - var endpoint string - var insecure bool - - client.EXPECT().APIServiceClient(ioctl.APIServiceConfig{ - Endpoint: endpoint, - Insecure: insecure, - }).Return(apiClient, nil).Times(7) + client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(24) + client.EXPECT().APIServiceClient().Return(apiClient, nil).Times(7) t.Run("get node reward pool", func(t *testing.T) { t.Run("get available reward & total reward", func(t *testing.T) {