Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add blacklist column when reading delegate in ioctl #1924

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions ioctl/cmd/bc/bc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)

// Multi-language support
Expand Down Expand Up @@ -109,3 +110,37 @@ func GetEpochMeta(epochNum uint64) (*iotexapi.GetEpochMetaResponse, error) {
}
return response, nil
}


// GetKickoutList gets kickout list
func GetKickoutList(epochNum uint64) (*iotexapi.ReadStateResponse, error) {
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
if err != nil {
return nil, output.NewError(output.NetworkError, "failed to connect to endpoint", err)
}
defer conn.Close()
cli := iotexapi.NewAPIServiceClient(conn)

request := &iotexapi.ReadStateRequest{
ProtocolID: []byte("poll"),
MethodName: []byte("KickoutListByEpoch"),
Arguments: [][]byte{byteutil.Uint64ToBytes(epochNum)},
}
ctx := context.Background()

jwtMD, err := util.JwtAuth()
if err == nil {
ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
}

response, err := cli.ReadState(ctx, request)
if err != nil {
sta, ok := status.FromError(err)
if ok {
return nil, output.NewError(output.APIError, sta.Message(), nil)
}
return nil, output.NewError(output.NetworkError, "failed to invoke ReadState api", err)
}
return response, nil
}

57 changes: 40 additions & 17 deletions ioctl/cmd/node/nodedelegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/iotexproject/iotex-proto/golang/iotexapi"

"github.com/iotexproject/iotex-proto/golang/iotexapi"

"github.com/iotexproject/iotex-core/action/protocol/vote"
"github.com/iotexproject/iotex-core/ioctl/cmd/alias"
"github.com/iotexproject/iotex-core/ioctl/cmd/bc"
"github.com/iotexproject/iotex-core/ioctl/config"
Expand Down Expand Up @@ -53,6 +55,7 @@ var (
epochNum uint64
nextEpoch bool
nodeStatus map[bool]string
kickoutStatus map[bool]string
)

// nodeDelegateCmd represents the node delegate command
Expand All @@ -74,12 +77,13 @@ var nodeDelegateCmd = &cobra.Command{
}

type delegate struct {
Address string `json:"address"`
Rank int `json:"rank"`
Alias string `json:"alias"`
Active bool `json:"active"`
Production int `json:"production"`
Votes string `json:"votes"`
Address string `json:"address"`
Rank int `json:"rank"`
Alias string `json:"alias"`
Active bool `json:"active"`
Production int `json:"production"`
Votes string `json:"votes"`
KickoutStatus bool `json:"kickoutStatus"`
}

type delegatesMessage struct {
Expand All @@ -99,13 +103,13 @@ func (m *delegatesMessage) String() string {
}
lines := []string{fmt.Sprintf("Epoch: %d, Start block height: %d,Total blocks in epoch: %d\n",
m.Epoch, m.StartBlock, m.TotalBlocks)}
formatTitleString := "%-41s %-4s %-" + strconv.Itoa(aliasLen) + "s %-6s %-6s %s"
formatDataString := "%-41s %4d %-" + strconv.Itoa(aliasLen) + "s %-6s %-6d %s"
formatTitleString := "%-41s %-4s %-" + strconv.Itoa(aliasLen) + "s %-6s %-6s %-12s %s"
formatDataString := "%-41s %4d %-" + strconv.Itoa(aliasLen) + "s %-6s %-6d %-12s %s"
lines = append(lines, fmt.Sprintf(formatTitleString,
"Address", "Rank", "Alias", "Status", "Blocks", "Votes"))
"Address", "Rank", "Alias", "Status", "Blocks", "KickoutStatus", "Votes"))
for _, bp := range m.Delegates {
lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, bp.Rank,
bp.Alias, nodeStatus[bp.Active], bp.Production, bp.Votes))
bp.Alias, nodeStatus[bp.Active], bp.Production, kickoutStatus[bp.KickoutStatus], bp.Votes))
}
return strings.Join(lines, "\n")
}
Expand Down Expand Up @@ -148,6 +152,7 @@ func init() {
nodeDelegateCmd.Flags().BoolVarP(&nextEpoch, "next-epoch", "n", false,
config.TranslateInLang(flagNextEpochUsages, config.UILanguage))
nodeStatus = map[bool]string{true: "active", false: ""}
kickoutStatus = map[bool]string{true: "kicked-out", false: ""}
}

func delegates() error {
Expand All @@ -169,25 +174,43 @@ func delegates() error {
StartBlock: int(epochData.Height),
TotalBlocks: int(response.TotalBlocks),
}
kickoutListRes, err := bc.GetKickoutList(epochNum)
if err != nil {
return output.NewError(0, "failed to get kickout list", err)
}
blacklist := &vote.Blacklist{}
if err := blacklist.Deserialize(kickoutListRes.Data); err != nil {
return output.NewError(output.SerializationError, "failed to deserialize kickout blacklist", err)
}
kickoutIntensityRate := float64(blacklist.IntensityRate) / float64(100)
for rank, bp := range response.BlockProducersInfo {
votes, ok := big.NewInt(0).SetString(bp.Votes, 10)
if !ok {
return output.NewError(output.ConvertError, "failed to convert votes into big int", nil)
}
isBlacklist := false
if _, ok := blacklist.BlacklistInfos[bp.Address]; ok {
// if it exists in blacklist
isBlacklist = true
votingPower := new(big.Float).SetInt(votes)
votes, _ = votingPower.Mul(votingPower, big.NewFloat(kickoutIntensityRate)).Int(nil)
}
delegate := delegate{
Address: bp.Address,
Rank: rank + 1,
Alias: aliases[bp.Address],
Active: bp.Active,
Production: int(bp.Production),
Votes: util.RauToString(votes, util.IotxDecimalNum),
Address: bp.Address,
Rank: rank + 1,
Alias: aliases[bp.Address],
Active: bp.Active,
Production: int(bp.Production),
Votes: util.RauToString(votes, util.IotxDecimalNum),
KickoutStatus: isBlacklist,
}
message.Delegates = append(message.Delegates, delegate)
}
fmt.Println(message.String())
return nil
}

// deprecated: It won't be able to query next delegate after Easter height, because it will be determined at the end of the epoch.
func nextDelegates() error {
chainMeta, err := bc.GetChainMeta()
if err != nil {
Expand Down