Skip to content

Commit

Permalink
feat: add /missing command (#15)
Browse files Browse the repository at this point in the history
* feat: add /missing command

* chore: fixed linting
  • Loading branch information
freak12techno authored Sep 2, 2022
1 parent 372039b commit 3bb911d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
25 changes: 21 additions & 4 deletions telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"html"
"io/ioutil"
"math"
"os"
"sort"
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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 <a href=\"https://freak12techno.github.io\">freak12techno</a> at <a href=\"https://validator.solar\">SOLAR Labs</a> 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")
Expand Down Expand Up @@ -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().
Expand All @@ -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
})

Expand Down Expand Up @@ -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!")
}
Expand Down

0 comments on commit 3bb911d

Please sign in to comment.