/
getconsensusinfo.go
61 lines (50 loc) · 1.25 KB
/
getconsensusinfo.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package cmd
import (
"context"
"encoding/json"
aergorpc "github.com/aergoio/aergo/types"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(getConsensusInfoCmd)
}
var getConsensusInfoCmd = &cobra.Command{
Use: "getconsensusinfo",
Short: "Print consensus info",
Run: func(cmd *cobra.Command, args []string) {
msg, err := client.GetConsensusInfo(context.Background(), &aergorpc.Empty{})
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
return
}
type outInfo struct {
Type string `json:",omitempty"`
Info *json.RawMessage `json:",omitempty"`
Bps []*json.RawMessage `json:",omitempty"`
}
var out = &outInfo{}
out.Type = msg.Type
//cmd.Println(fmt.Sprintf("consensus:%s, leader:%d", msg.Type, uinfo.Leader))
if len(msg.Info) > 0 {
infoB := json.RawMessage(msg.Info)
out.Info = &infoB
}
if len(msg.Bps) > 0 {
out.Bps = make([]*json.RawMessage, len(msg.Bps))
for i, bpstr := range msg.Bps {
b := json.RawMessage([]byte(bpstr))
out.Bps[i] = &b
}
}
jsonout, err := json.Marshal(out)
if err != nil {
cmd.Printf("failed decode consensus info: %v\n", err)
return
}
cmd.Println(string(jsonout))
},
}