-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into issue3502
- Loading branch information
Showing
5 changed files
with
216 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// 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 | ||
// License 2.0 that can be found in the LICENSE file. | ||
|
||
package node | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/iotexproject/iotex-core/action/protocol/vote" | ||
"github.com/iotexproject/iotex-core/ioctl" | ||
"github.com/iotexproject/iotex-core/ioctl/config" | ||
"github.com/iotexproject/iotex-core/ioctl/newcmd/bc" | ||
) | ||
|
||
// Multi-language support | ||
var ( | ||
_probationlistCmdUses = map[config.Language]string{ | ||
config.English: "probationlist [-e epoch-num]", | ||
config.Chinese: "probationlist [-e epoch数]", | ||
} | ||
_probationlistCmdShorts = map[config.Language]string{ | ||
config.English: "Print probation list at given epoch", | ||
config.Chinese: "打印给定epoch内的试用名单", | ||
} | ||
) | ||
|
||
// NewNodeProbationlistCmd represents querying probation list command | ||
func NewNodeProbationlistCmd(client ioctl.Client) *cobra.Command { | ||
var epochNum uint64 | ||
use, _ := client.SelectTranslation(_probationlistCmdUses) | ||
short, _ := client.SelectTranslation(_probationlistCmdShorts) | ||
flagEpochNumUsages, _ := client.SelectTranslation(_flagEpochNumUsages) | ||
|
||
cmd := &cobra.Command{ | ||
Use: use, | ||
Short: short, | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.SilenceUsage = true | ||
if epochNum == 0 { | ||
chainMeta, err := bc.GetChainMeta(client) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get chain meta") | ||
} | ||
epochNum = chainMeta.GetEpoch().GetNum() | ||
} | ||
response, err := bc.GetEpochMeta(client, epochNum) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get epoch meta") | ||
} | ||
probationlist, err := getProbationList(client, epochNum, response.EpochData.Height) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get probation list") | ||
} | ||
delegateList := make([]string, 0) | ||
for addr := range probationlist.ProbationInfo { | ||
delegateList = append(delegateList, addr) | ||
} | ||
byteAsJSON, err := json.MarshalIndent(delegateList, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
cmd.Printf("EpochNumber : %d, IntensityRate : %d%%\nProbationList : %s", | ||
epochNum, | ||
probationlist.IntensityRate, | ||
fmt.Sprint(string(byteAsJSON)), | ||
) | ||
return nil | ||
}, | ||
} | ||
cmd.PersistentFlags().Uint64VarP(&epochNum, "epoch-num", "e", 0, flagEpochNumUsages) | ||
return cmd | ||
} | ||
|
||
func getProbationList(client ioctl.Client, epochNum uint64, epochStartHeight uint64) (*vote.ProbationList, error) { | ||
probationListRes, err := bc.GetProbationList(client, epochNum, epochStartHeight) | ||
if err != nil { | ||
return nil, err | ||
} | ||
probationList := &vote.ProbationList{} | ||
if probationListRes != nil { | ||
if err := probationList.Deserialize(probationListRes.Data); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return probationList, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// 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 | ||
// License 2.0 that can be found in the LICENSE file. | ||
|
||
package node | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/iotexproject/iotex-proto/golang/iotexapi" | ||
"github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" | ||
"github.com/iotexproject/iotex-proto/golang/iotextypes" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/iotexproject/iotex-core/action/protocol/vote" | ||
"github.com/iotexproject/iotex-core/ioctl/config" | ||
"github.com/iotexproject/iotex-core/ioctl/util" | ||
"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" | ||
) | ||
|
||
func TestNewNodeProbationlistCmd(t *testing.T) { | ||
require := require.New(t) | ||
ctrl := gomock.NewController(t) | ||
client := mock_ioctlclient.NewMockClient(ctrl) | ||
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) | ||
|
||
client.EXPECT().SelectTranslation(gomock.Any()).Return("", config.English).Times(12) | ||
client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(10) | ||
|
||
t.Run("failed to get chain meta", func(t *testing.T) { | ||
expectedErr := errors.New("failed to get chain meta") | ||
apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(nil, expectedErr).Times(1) | ||
|
||
cmd := NewNodeProbationlistCmd(client) | ||
_, err := util.ExecuteCmd(cmd) | ||
require.Contains(err.Error(), "failed to get chain meta") | ||
}) | ||
|
||
var testBlockProducersInfo = []*iotexapi.BlockProducerInfo{ | ||
{Address: "io1kr8c6krd7dhxaaqwdkr6erqgu4z0scug3drgja", Votes: "109510794521770016955545668", Active: true, Production: 30}, | ||
{Address: "io13q2am9nedrd3n746lsj6qan4pymcpgm94vvx2c", Votes: "81497052527306018062463878", Active: false, Production: 0}, | ||
} | ||
|
||
chainMetaResponse := &iotexapi.GetChainMetaResponse{ChainMeta: &iotextypes.ChainMeta{Epoch: &iotextypes.EpochData{Num: 7000}}} | ||
epochMetaResponse := &iotexapi.GetEpochMetaResponse{EpochData: &iotextypes.EpochData{Num: 7000, Height: 3223081}, TotalBlocks: 720, BlockProducersInfo: testBlockProducersInfo} | ||
|
||
apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResponse, nil).Times(3) | ||
apiServiceClient.EXPECT().GetEpochMeta(gomock.Any(), gomock.Any()).Return(epochMetaResponse, nil).Times(3) | ||
|
||
t.Run("query probation list", func(t *testing.T) { | ||
probationList := &iotexapi.ReadStateResponse{} | ||
|
||
apiServiceClient.EXPECT().ReadState(gomock.Any(), gomock.Any()).Return(probationList, nil).Times(1) | ||
|
||
cmd := NewNodeProbationlistCmd(client) | ||
result, err := util.ExecuteCmd(cmd) | ||
require.NoError(err) | ||
require.Contains(result, "ProbationList : []") | ||
}) | ||
|
||
t.Run("epochNum > 0", func(t *testing.T) { | ||
d, _ := vote.NewProbationList(1).Serialize() | ||
probationList := &iotexapi.ReadStateResponse{ | ||
Data: d, | ||
} | ||
|
||
apiServiceClient.EXPECT().ReadState(gomock.Any(), gomock.Any()).Return(probationList, nil).Times(1) | ||
|
||
cmd := NewNodeProbationlistCmd(client) | ||
result, err := util.ExecuteCmd(cmd, "-e", "1") | ||
require.NoError(err) | ||
require.Contains(result, "ProbationList : []") | ||
}) | ||
|
||
t.Run("failed to get probation list", func(t *testing.T) { | ||
apiServiceClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ | ||
ProtocolID: []byte("poll"), | ||
MethodName: []byte("ProbationListByEpoch"), | ||
Arguments: [][]byte{[]byte("7000")}, | ||
Height: strconv.FormatUint(3223081, 10), | ||
}).Return(&iotexapi.ReadStateResponse{ | ||
Data: []byte("0")}, | ||
nil) | ||
|
||
cmd := NewNodeProbationlistCmd(client) | ||
_, err := util.ExecuteCmd(cmd) | ||
require.Contains(err.Error(), "failed to get probation list") | ||
}) | ||
} |