From 3bb911db9a4967687aa102ce29de531effdefde3 Mon Sep 17 00:00:00 2001 From: Techno Freak <83376337+freak12techno@users.noreply.github.com> Date: Fri, 2 Sep 2022 11:10:08 +0300 Subject: [PATCH] feat: add /missing command (#15) * feat: add /missing command * chore: fixed linting --- README.md | 1 + telegram.go | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7a925d1..982a4bd 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ status - Get missed blocks info from validators you are subscribed to config - Display bot config params - Display chain slashing params validators - Get the list of all active validators and their missed blocks +missing - Get the list of validators who have missed blocks counter above threshold and their missed blocks ``` diff --git a/telegram.go b/telegram.go index 8ba2865..1702c94 100644 --- a/telegram.go +++ b/telegram.go @@ -3,7 +3,6 @@ package main import ( "fmt" "html" - "io/ioutil" "math" "os" "sort" @@ -173,7 +172,12 @@ func (r *TelegramReporter) Init() { r.TelegramBot.Handle("/subscribe", r.subscribeToValidatorUpdates) r.TelegramBot.Handle("/unsubscribe", r.unsubscribeFromValidatorUpdates) r.TelegramBot.Handle("/config", r.displayConfig) - r.TelegramBot.Handle("/validators", r.getValidatorsStatus) + r.TelegramBot.Handle("/validators", func(message *tb.Message) { + r.getValidatorsStatus(message, false) + }) + r.TelegramBot.Handle("/missing", func(message *tb.Message) { + r.getValidatorsStatus(message, true) + }) r.TelegramBot.Handle("/params", r.getChainParams) go r.TelegramBot.Start() @@ -257,6 +261,7 @@ func (r TelegramReporter) getHelp(message *tb.Message) { sb.WriteString("- /config - display bot config\n") sb.WriteString("- /params - display chain slashing params\n") sb.WriteString("- /validators - display all active validators and their missed blocks\n") + sb.WriteString("- /missing - display only validators missing blocks above threshold and their missing blocks\n") sb.WriteString("Created by freak12techno at SOLAR Labs with ❤️.\n") sb.WriteString("This bot is open-sourced, you can get the source code at https://github.com/solarlabsteam/missed-blocks-checker.\n\n") sb.WriteString("We also maintain the following tools for Cosmos ecosystem:\n") @@ -299,7 +304,7 @@ func (r *TelegramReporter) getValidatorStatus(message *tb.Message) { Msg("Successfully returned validator status") } -func (r *TelegramReporter) getValidatorsStatus(message *tb.Message) { +func (r *TelegramReporter) getValidatorsStatus(message *tb.Message, getOnlyMissing bool) { state, err := r.Client.GetValidatorsState() if err != nil { r.Logger.Error(). @@ -310,6 +315,18 @@ func (r *TelegramReporter) getValidatorsStatus(message *tb.Message) { } state = FilterMap(state, func(s ValidatorState) bool { + if getOnlyMissing { + group, err := r.AppConfig.MissedBlocksGroups.GetGroup(s.MissedBlocks) + if err != nil { + r.Logger.Error(). + Err(err). + Msg("Could not get validator missed block group") + return !s.Jailed + } + + return !s.Jailed && group.Start != 0 + } + return !s.Jailed }) @@ -570,7 +587,7 @@ func (r *TelegramReporter) loadConfigFromYaml() { r.Logger.Fatal().Err(err).Msg("Could not fetch Telegram config!") } - bytes, err := ioutil.ReadFile(r.TelegramAppConfig.ConfigPath) + bytes, err := os.ReadFile(r.TelegramAppConfig.ConfigPath) if err != nil { r.Logger.Fatal().Err(err).Msg("Could not read Telegram config!") }