This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
chains_info.go
134 lines (118 loc) · 3.09 KB
/
chains_info.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package util
import (
"fmt"
"strconv"
"strings"
"github.com/hyperledger/burrow/deploy/def"
"encoding/json"
"github.com/elgs/gojq"
"github.com/hyperledger/burrow/acm/validator"
log "github.com/sirupsen/logrus"
)
func GetBlockHeight(client *def.Client) (latestBlockHeight uint64, err error) {
stat, err := client.Status()
if err != nil {
return 0, err
}
return stat.SyncInfo.LatestBlockHeight, nil
}
func AccountsInfo(account, field string, client *def.Client) (string, error) {
address, err := client.GetKeyAddress(account)
if err != nil {
return "", err
}
acc, err := client.GetAccount(address)
if err != nil {
return "", err
}
if acc == nil {
return "", fmt.Errorf("Account %s does not exist", account)
}
bs, err := json.Marshal(acc)
if err != nil {
return "", err
}
jq, err := gojq.NewStringQuery(string(bs))
if err == nil {
log.Warn("Attempting jq query")
res, err := jq.Query(field)
if err == nil {
return fmt.Sprintf("%v", res), nil
} else {
log.Debugf("Got error from jq query: %v trying legacy query (probably fine)...", err)
}
}
var s string
if strings.Contains(field, "permissions") {
fields := strings.Split(field, ".")
if len(fields) > 1 {
switch fields[1] {
case "roles":
s = strings.Join(acc.Permissions.Roles, ",")
case "base", "perms":
s = strconv.Itoa(int(acc.Permissions.Base.Perms))
case "set":
s = strconv.Itoa(int(acc.Permissions.Base.SetBit))
}
}
} else if field == "balance" {
s = itoaU64(acc.Balance)
}
if err != nil {
return "", err
}
return s, nil
}
func NamesInfo(name, field string, client *def.Client) (string, error) {
entry, err := client.GetName(name)
if err != nil {
return "", err
}
switch strings.ToLower(field) {
case "name":
return name, nil
case "owner":
return entry.Owner.String(), nil
case "data":
return entry.Data, nil
case "expires":
return itoaU64(entry.Expires), nil
default:
return "", fmt.Errorf("Field %s not recognized", field)
}
}
func ValidatorsInfo(query string, client *def.Client) (interface{}, error) {
// Currently there is no notion of 'unbonding validators' we can revisit what should go here or whether this deserves
// to exist as a job
validatorSet, err := client.GetValidatorSet()
if err != nil {
return nil, err
}
// Yes, this feels a bit silly, but it is the easiest way to get the generic map of slice object that gojq needs
// mapstructure is not able to do this it would seem.
bs, err := json.Marshal(map[string]interface{}{
"Height": validatorSet.Height,
"Set": validatorMap(validatorSet.Set),
})
if err != nil {
return nil, err
}
jq, err := gojq.NewStringQuery(string(bs))
if err != nil {
return nil, err
}
return jq.Query(query)
}
func validatorMap(vs []*validator.Validator) map[string]interface{} {
set := validator.UnpersistSet(vs)
vsMap := make(map[string]interface{}, len(vs))
vsMap["TotalPower"] = set.TotalPower().String()
vsMap["String"] = set.String()
for _, v := range vs {
vsMap[v.GetPublicKey().GetAddress().String()] = v
}
return vsMap
}
func itoaU64(i uint64) string {
return strconv.FormatUint(i, 10)
}