-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.go
160 lines (127 loc) · 3.53 KB
/
hash.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package node
import (
"fmt"
"github.com/gosuri/uitable"
"hash"
"hash/fnv"
"sort"
"strings"
)
// MethodsResult contains all available rpc methods of node
type MethodsResult struct {
Version int `json:"version"`
Methods []string `json:"methods"`
}
// Properties are node properties
type Properties struct {
SS58Format int `json:"ss58Format"`
TokenDecimals int `json:"tokenDecimals"`
TokenSymbol string `json:"tokenSymbol"`
}
// GetConfigHash hashes sorted node configuration
func (client *client) GetConfigHash() (hash.Hash32, error) {
methods, err := client.getNodeRPCMethods()
if err != nil {
return nil, err
}
nodeRoles, err := client.getNodeRoles()
if err != nil {
return nil, err
}
chain, err := client.getChain()
if err != nil {
return nil, err
}
chainType, err := client.getChainType()
if err != nil {
return nil, err
}
properties, err := client.getNodeProperties()
if err != nil {
return nil, err
}
sort.Strings(methods)
sort.Strings(nodeRoles)
displayConfiguration(chain, chainType, nodeRoles, properties, methods)
hash := fnv.New32()
_, _ = hash.Write([]byte(chain))
_, _ = hash.Write([]byte(chainType))
_, _ = hash.Write([]byte(strings.Join(methods, "")))
_, _ = hash.Write([]byte(strings.Join(nodeRoles, "")))
_, _ = hash.Write([]byte(fmt.Sprint(properties.SS58Format)))
_, _ = hash.Write([]byte(fmt.Sprint(properties.TokenDecimals)))
_, _ = hash.Write([]byte(properties.TokenSymbol))
return hash, nil
}
func (client *client) getChainType() (string, error) {
var chainType string
_, err := client.sendRPCRequest("system_chainType", []string{}, &chainType)
if err != nil {
return "", err
}
return chainType, nil
}
func (client *client) getChain() (string, error) {
var chain string
_, err := client.sendRPCRequest("system_chain", []string{}, &chain)
if err != nil {
return "", err
}
return chain, nil
}
func (client *client) getNodeRoles() ([]string, error) {
var nodeRoles []string
_, err := client.sendRPCRequest("system_nodeRoles", []string{}, &nodeRoles)
if err != nil {
return nil, err
}
return nodeRoles, nil
}
func (client *client) getNodeProperties() (Properties, error) {
var properties Properties
_, err := client.sendRPCRequest("system_properties", []string{}, &properties)
if err != nil {
return Properties{}, err
}
return properties, nil
}
func (client *client) getNodeRPCMethods() ([]string, error) {
var methods MethodsResult
_, err := client.sendRPCRequest("rpc_methods", []string{}, &methods)
if err != nil {
return nil, err
}
return methods.Methods, nil
}
func displayConfiguration(chain string, chainType string, nodeRoles []string, properties Properties, methods []string) {
var sortedMethods = map[string][]string{}
for _, m := range methods {
methodParts := strings.Split(m, "_")
methodsInCategory := sortedMethods[methodParts[0]]
if len(methodsInCategory) == 0 {
methodsInCategory = []string{m}
} else {
methodsInCategory = append(methodsInCategory, m)
}
sortedMethods[methodParts[0]] = methodsInCategory
}
methodsTable := uitable.New()
methodsTable.MaxColWidth = 80
methodsTable.Wrap = true
methodsTable.AddRow("", "")
for category, cMethods := range sortedMethods {
methodsTable.AddRow(category, strings.Join(cMethods, ", "))
}
fmt.Printf(`Created config hash with:
Chain: %s
Chain Type: %s
Node Roles: %v
SS58Format: %d
Token Decimals: %d
Token Symbol: %s
Methods:
`,
chain, chainType, nodeRoles, properties.SS58Format,
properties.TokenDecimals, properties.TokenSymbol)
fmt.Println(methodsTable)
}