This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selected.go
82 lines (67 loc) · 1.89 KB
/
selected.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cmd
import (
"log"
"github.com/davecgh/go-spew/spew"
"github.com/jedib0t/go-pretty/table"
"github.com/spf13/cobra"
"github.com/ethereum/go-ethereum/common"
"github.com/linki/wanchain-cli/client"
"github.com/linki/wanchain-cli/util"
)
var (
selectedCmd = &cobra.Command{
Use: "selected",
Run: listSelected,
}
selectedParams = struct {
validatorAddress string
epochID uint64
}{}
)
func init() {
selectedCmd.PersistentFlags().StringVar(&selectedParams.validatorAddress, "validator-address", "", "Address of the validator by which to filter")
selectedCmd.PersistentFlags().Uint64Var(&selectedParams.epochID, "epoch-id", 0, "Epoch ID to query for")
rootCmd.AddCommand(selectedCmd)
}
func listSelected(cmd *cobra.Command, _ []string) {
client, err := client.NewClient(rpcURL)
if err != nil {
log.Fatal(err)
}
defer client.Close()
if selectedParams.epochID == 0 {
selectedParams.epochID, err = client.GetCurrentEpochID()
if err != nil {
log.Fatal(err)
}
selectedParams.epochID++
}
t := table.NewWriter()
t.SetOutputMirror(cmd.OutOrStdout())
t.AppendHeader(table.Row{"Epoch ID", "Role", "Address"})
EPAddrs, err := client.GetEpochLeaders(selectedParams.epochID)
if err != nil {
log.Fatal(err)
}
if debug {
spew.Dump(EPAddrs)
}
RPAddrs, err := client.GetRandomProposers(selectedParams.epochID)
if err != nil {
log.Fatal(err)
}
if debug {
spew.Dump(RPAddrs)
}
for _, addr := range EPAddrs {
if selectedParams.validatorAddress == "" || common.HexToAddress(addr) == common.HexToAddress(selectedParams.validatorAddress) {
t.AppendRow(table.Row{selectedParams.epochID, "EP", addr})
}
}
for _, addr := range RPAddrs {
if selectedParams.validatorAddress == "" || common.HexToAddress(addr) == common.HexToAddress(selectedParams.validatorAddress) {
t.AppendRow(table.Row{selectedParams.epochID, "RP", addr})
}
}
util.RenderTable(t, format)
}