forked from solarlabsteam/cosmos-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
307 lines (257 loc) · 9.58 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"context"
"fmt"
"math"
"net/http"
"os"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
tmrpc "github.com/tendermint/tendermint/rpc/client/http"
"google.golang.org/grpc"
)
var (
ConfigPath string
Denom string
ListenAddress string
NodeAddress string
TendermintRPC string
LogLevel string
JsonOutput bool
Limit uint64
Prefix string
AccountPrefix string
AccountPubkeyPrefix string
ValidatorPrefix string
ValidatorPubkeyPrefix string
ConsensusNodePrefix string
ConsensusNodePubkeyPrefix string
ChainID string
ConstLabels map[string]string
DenomCoefficient float64
DenomExponent uint64
)
var log = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger()
var rootCmd = &cobra.Command{
Use: "cosmos-exporter",
Long: "Scrape the data about the validators set, specific validators or wallets in the Cosmos network.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if ConfigPath == "" {
setBechPrefixes(cmd)
return nil
}
viper.SetConfigFile(ConfigPath)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Info().Err(err).Msg("Error reading config file")
return err
}
}
// Credits to https://carolynvanslyck.com/blog/2020/08/sting-of-the-viper/
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if !f.Changed && viper.IsSet(f.Name) {
val := viper.Get(f.Name)
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil {
log.Fatal().Err(err).Msg("Could not set flag")
}
}
})
setBechPrefixes(cmd)
return nil
},
Run: Execute,
}
func setBechPrefixes(cmd *cobra.Command) {
if flag, err := cmd.Flags().GetString("bech-account-prefix"); flag != "" && err == nil {
AccountPrefix = flag
} else {
AccountPrefix = Prefix
}
if flag, err := cmd.Flags().GetString("bech-account-pubkey-prefix"); flag != "" && err == nil {
AccountPubkeyPrefix = flag
} else {
AccountPubkeyPrefix = Prefix + "pub"
}
if flag, err := cmd.Flags().GetString("bech-validator-prefix"); flag != "" && err == nil {
ValidatorPrefix = flag
} else {
ValidatorPrefix = Prefix + "valoper"
}
if flag, err := cmd.Flags().GetString("bech-validator-pubkey-prefix"); flag != "" && err == nil {
ValidatorPubkeyPrefix = flag
} else {
ValidatorPubkeyPrefix = Prefix + "valoperpub"
}
if flag, err := cmd.Flags().GetString("bech-consensus-node-prefix"); flag != "" && err == nil {
ConsensusNodePrefix = flag
} else {
ConsensusNodePrefix = Prefix + "valcons"
}
if flag, err := cmd.Flags().GetString("bech-consensus-node-pubkey-prefix"); flag != "" && err == nil {
ConsensusNodePubkeyPrefix = flag
} else {
ConsensusNodePubkeyPrefix = Prefix + "valconspub"
}
}
func Execute(cmd *cobra.Command, args []string) {
logLevel, err := zerolog.ParseLevel(LogLevel)
if err != nil {
log.Fatal().Err(err).Msg("Could not parse log level")
}
if JsonOutput {
log = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
zerolog.SetGlobalLevel(logLevel)
log.Info().
Str("--bech-account-prefix", AccountPrefix).
Str("--bech-account-pubkey-prefix", AccountPubkeyPrefix).
Str("--bech-validator-prefix", ValidatorPrefix).
Str("--bech-validator-pubkey-prefix", ValidatorPubkeyPrefix).
Str("--bech-consensus-node-prefix", ConsensusNodePrefix).
Str("--bech-consensus-node-pubkey-prefix", ConsensusNodePubkeyPrefix).
Str("--denom", Denom).
Str("--denom-cofficient", fmt.Sprintf("%f", DenomCoefficient)).
Str("--denom-exponent", fmt.Sprintf("%d", DenomExponent)).
Str("--listen-address", ListenAddress).
Str("--node", NodeAddress).
Str("--log-level", LogLevel).
Msg("Started with following parameters")
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(AccountPrefix, AccountPubkeyPrefix)
config.SetBech32PrefixForValidator(ValidatorPrefix, ValidatorPubkeyPrefix)
config.SetBech32PrefixForConsensusNode(ConsensusNodePrefix, ConsensusNodePubkeyPrefix)
config.Seal()
grpcConn, err := grpc.Dial(
NodeAddress,
grpc.WithInsecure(),
)
if err != nil {
log.Fatal().Err(err).Msg("Could not connect to gRPC node")
}
setChainID()
setDenom(grpcConn)
http.HandleFunc("/metrics/wallet", func(w http.ResponseWriter, r *http.Request) {
WalletHandler(w, r, grpcConn)
})
http.HandleFunc("/metrics/validator", func(w http.ResponseWriter, r *http.Request) {
ValidatorHandler(w, r, grpcConn)
})
http.HandleFunc("/metrics/validators", func(w http.ResponseWriter, r *http.Request) {
ValidatorsHandler(w, r, grpcConn)
})
http.HandleFunc("/metrics/params", func(w http.ResponseWriter, r *http.Request) {
ParamsHandler(w, r, grpcConn)
})
http.HandleFunc("/metrics/general", func(w http.ResponseWriter, r *http.Request) {
GeneralHandler(w, r, grpcConn)
})
log.Info().Str("address", ListenAddress).Msg("Listening")
err = http.ListenAndServe(ListenAddress, nil)
if err != nil {
log.Fatal().Err(err).Msg("Could not start application")
}
}
func setChainID() {
client, err := tmrpc.New(TendermintRPC, "/websocket")
if err != nil {
log.Fatal().Err(err).Msg("Could not create Tendermint client")
}
status, err := client.Status(context.Background())
if err != nil {
log.Fatal().Err(err).Msg("Could not query Tendermint status")
}
log.Info().Str("network", status.NodeInfo.Network).Msg("Got network status from Tendermint")
ChainID = status.NodeInfo.Network
ConstLabels = map[string]string{
"chain_id": ChainID,
}
}
func setDenom(grpcConn *grpc.ClientConn) {
// if --denom and (--denom-coefficient or --denom-exponent) are provided, use them
// instead of fetching them via gRPC. Can be useful for networks like osmosis.
if isUserProvidedAndHandled := checkAndHandleDenomInfoProvidedByUser(); isUserProvidedAndHandled {
return
}
bankClient := banktypes.NewQueryClient(grpcConn)
denoms, err := bankClient.DenomsMetadata(
context.Background(),
&banktypes.QueryDenomsMetadataRequest{},
)
if err != nil {
log.Fatal().Err(err).Msg("Error querying denom")
}
if len(denoms.Metadatas) == 0 {
log.Fatal().Msg("No denom infos. Try running the binary with --denom and --denom-coefficient to set them manually.")
}
metadata := denoms.Metadatas[0] // always using the first one
if Denom == "" { // using display currency
Denom = metadata.Display
}
for _, unit := range metadata.DenomUnits {
log.Debug().
Str("denom", unit.Denom).
Uint32("exponent", unit.Exponent).
Msg("Denom info")
if unit.Denom == Denom {
DenomCoefficient = math.Pow10(int(unit.Exponent))
log.Info().
Str("denom", Denom).
Float64("coefficient", DenomCoefficient).
Msg("Got denom info")
return
}
}
log.Fatal().Msg("Could not find the denom info")
}
func checkAndHandleDenomInfoProvidedByUser() bool {
if Denom != "" {
if DenomCoefficient != 1 && DenomExponent != 0 {
log.Fatal().Msg("denom-coefficient and denom-exponent are both provided. Must provide only one")
}
if DenomCoefficient != 1 {
log.Info().
Str("denom", Denom).
Float64("coefficient", DenomCoefficient).
Msg("Using provided denom and coefficient.")
return true
}
if DenomExponent != 0 {
DenomCoefficient = math.Pow10(int(DenomExponent))
log.Info().
Str("denom", Denom).
Uint64("exponent", DenomExponent).
Float64("calculated coefficient", DenomCoefficient).
Msg("Using provided denom and denom exponent and calculating coefficient.")
return true
}
return false
}
return false
}
func main() {
rootCmd.PersistentFlags().StringVar(&ConfigPath, "config", "", "Config file path")
rootCmd.PersistentFlags().StringVar(&Denom, "denom", "", "Cosmos coin denom")
rootCmd.PersistentFlags().Float64Var(&DenomCoefficient, "denom-coefficient", 1, "Denom coefficient")
rootCmd.PersistentFlags().Uint64Var(&DenomExponent, "denom-exponent", 0, "Denom exponent")
rootCmd.PersistentFlags().StringVar(&ListenAddress, "listen-address", ":9300", "The address this exporter would listen on")
rootCmd.PersistentFlags().StringVar(&NodeAddress, "node", "localhost:9090", "RPC node address")
rootCmd.PersistentFlags().StringVar(&LogLevel, "log-level", "info", "Logging level")
rootCmd.PersistentFlags().Uint64Var(&Limit, "limit", 1000, "Pagination limit for gRPC requests")
rootCmd.PersistentFlags().StringVar(&TendermintRPC, "tendermint-rpc", "http://localhost:26657", "Tendermint RPC address")
rootCmd.PersistentFlags().BoolVar(&JsonOutput, "json", false, "Output logs as JSON")
// some networks, like Iris, have the different prefixes for address, validator and consensus node
rootCmd.PersistentFlags().StringVar(&Prefix, "bech-prefix", "persistence", "Bech32 global prefix")
rootCmd.PersistentFlags().StringVar(&AccountPrefix, "bech-account-prefix", "", "Bech32 account prefix")
rootCmd.PersistentFlags().StringVar(&AccountPubkeyPrefix, "bech-account-pubkey-prefix", "", "Bech32 pubkey account prefix")
rootCmd.PersistentFlags().StringVar(&ValidatorPrefix, "bech-validator-prefix", "", "Bech32 validator prefix")
rootCmd.PersistentFlags().StringVar(&ValidatorPubkeyPrefix, "bech-validator-pubkey-prefix", "", "Bech32 pubkey validator prefix")
rootCmd.PersistentFlags().StringVar(&ConsensusNodePrefix, "bech-consensus-node-prefix", "", "Bech32 consensus node prefix")
rootCmd.PersistentFlags().StringVar(&ConsensusNodePubkeyPrefix, "bech-consensus-node-pubkey-prefix", "", "Bech32 pubkey consensus node prefix")
if err := rootCmd.Execute(); err != nil {
log.Fatal().Err(err).Msg("Could not start application")
}
}