Skip to content

Commit

Permalink
feat: added /jailscount command
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jan 25, 2025
1 parent a926a34 commit 37e3539
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,44 @@ func (d *Database) FindLastEventsByValidator(

return events, nil
}

func (d *Database) FindAllJailsCount(chain string) ([]types.ValidatorWithJailsCount, error) {
d.MaybeMutexLock()
defer d.MaybeMutexUnlock()

jailsCount := []types.ValidatorWithJailsCount{}

rows, err := d.client.Query(
"SELECT validator, count(*) from events where chain = $1 AND event = $2 GROUP BY chain, validator ORDER BY count DESC",
chain,
constants.EventValidatorJailed,
)
if err != nil {
d.logger.Error().Err(err).Msg("Error getting events by operator address and type")
return jailsCount, err
}
defer func() {
_ = rows.Close()
_ = rows.Err()
}()

for rows.Next() {
var (
validator string
count int
)

err = rows.Scan(&validator, &count)
if err != nil {
d.logger.Error().Err(err).Msg("Error fetching jails count")
return jailsCount, err
}

jailsCount = append(jailsCount, types.ValidatorWithJailsCount{
Validator: validator,
JailsCount: count,
})
}

return jailsCount, nil
}
46 changes: 46 additions & 0 deletions pkg/reporters/telegram/jails_count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package telegram

import (
"main/pkg/constants"

tele "gopkg.in/telebot.v3"
)

func (reporter *Reporter) HandleJailsCount(c tele.Context) error {
reporter.Logger.Info().
Str("sender", c.Sender().Username).
Str("text", c.Text()).
Msg("Got jails count query")

reporter.MetricsManager.LogReporterQuery(reporter.Config.Name, constants.TelegramReporterName, "jailscount")

snapshot, found := reporter.SnapshotManager.GetNewerSnapshot()
if !found {
reporter.Logger.Info().
Str("sender", c.Sender().Username).
Str("text", c.Text()).
Msg("No older snapshot on telegram events query!")
return reporter.BotReply(c, "Error getting validator events!")
}

jailsCount, err := reporter.Manager.FindAllJailsCount()
if err != nil {
return reporter.BotReply(c, "Error searching for jails count!")
}

jailsCountRendered := make([]renderedJailsCount, len(jailsCount))

for index, validatorJailsCount := range jailsCount {
validatorEntries := snapshot.Entries.ByValidatorAddresses([]string{validatorJailsCount.Validator})
if len(validatorEntries) == 0 {
return reporter.BotReply(c, "Validator is not found!")
}

jailsCountRendered[index] = renderedJailsCount{
ValidatorLink: reporter.Config.ExplorerConfig.GetValidatorLink(validatorEntries[0].Validator),
JailsCount: validatorJailsCount.JailsCount,
}
}

return reporter.ReplyRender(c, "JailsCount", jailsCountRendered)
}
1 change: 1 addition & 0 deletions pkg/reporters/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (reporter *Reporter) Init() {
bot.Handle("/config", reporter.HandleParams)
bot.Handle("/jails", reporter.HandleJailsList)
bot.Handle("/events", reporter.HandleValidatorEventsList)
bot.Handle("/jailscount", reporter.HandleJailsCount)

reporter.TelegramBot = bot
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/reporters/telegram/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,8 @@ type eventsRender struct {
ValidatorLink types.Link
Events []renderedHistoricalEvent
}

type renderedJailsCount struct {
ValidatorLink types.Link
JailsCount int
}
4 changes: 4 additions & 0 deletions pkg/state/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ func (m *Manager) FindLastEventsByValidator(operatorAddress string) ([]types.His
)
}

func (m *Manager) FindAllJailsCount() ([]types.ValidatorWithJailsCount, error) {
return m.database.FindAllJailsCount(m.config.Name)
}

func (m *Manager) GetValidators() types.ValidatorsMap {
return m.state.GetValidators()
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ func (validatorsMap ValidatorsMap) ToSlice() Validators {

return validators
}

type ValidatorWithJailsCount struct {
Validator string
JailsCount int
}
8 changes: 8 additions & 0 deletions templates/telegram/JailsCount.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- if . }}
<strong>Validators jails count since the app was started:</strong>
{{- range . }}
{{ SerializeLink .ValidatorLink }}: {{ .JailsCount }}
{{- end }}
{{- else }}
Nobody has been jailed since the app launch.
{{- end }}

0 comments on commit 37e3539

Please sign in to comment.