Skip to content

Commit

Permalink
feat: add version to /help (#30)
Browse files Browse the repository at this point in the history
* feat: add version to /help

* chore: fix linting
  • Loading branch information
freak12techno committed Nov 3, 2023
1 parent a0304e4 commit 0e74504
Show file tree
Hide file tree
Showing 26 changed files with 194 additions and 157 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ main
cover.out
missed-blocks-checker
dist
*.sqlite
*.sqlite
**/.DS_Store
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module main

go 1.19
go 1.18

require (
github.com/BurntSushi/toml v1.2.1
Expand Down
1 change: 1 addition & 0 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewApp(configPath string, version string) *App {
appManagers[index] = NewAppManager(
logger,
chainConfig,
version,
metricsManager,
database,
)
Expand Down
5 changes: 3 additions & 2 deletions pkg/app_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type AppManager struct {
func NewAppManager(
logger zerolog.Logger,
config *configPkg.ChainConfig,
version string,
metricsManager *metrics.Manager,
database *databasePkg.Database,
) *AppManager {
Expand All @@ -54,8 +55,8 @@ func NewAppManager(
websocketManager := tendermint.NewWebsocketManager(managerLogger, config, metricsManager)

reporters := []reportersPkg.Reporter{
telegram.NewReporter(config, managerLogger, stateManager, metricsManager, snapshotManager),
discord.NewReporter(config, managerLogger, stateManager, metricsManager, snapshotManager),
telegram.NewReporter(config, version, managerLogger, stateManager, metricsManager, snapshotManager),
discord.NewReporter(config, version, managerLogger, stateManager, metricsManager, snapshotManager),
}

return &AppManager{
Expand Down
38 changes: 20 additions & 18 deletions pkg/config/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"testing"

"github.com/stretchr/testify/require"

"gopkg.in/guregu/null.v4"

"github.com/stretchr/testify/assert"
Expand All @@ -12,44 +14,44 @@ func TestGetChainNameWithoutPrettyName(t *testing.T) {
t.Parallel()

config := &ChainConfig{Name: "test"}
assert.Equal(t, config.GetName(), "test", "Names do not match!")
assert.Equal(t, "test", config.GetName(), "Names do not match!")
}

func TestGetChainNameWithPrettyName(t *testing.T) {
t.Parallel()

config := &ChainConfig{Name: "test", PrettyName: "Test"}
assert.Equal(t, config.GetName(), "Test", "Names do not match!")
assert.Equal(t, "Test", config.GetName(), "Names do not match!")
}

func TestGetChainBlocksSignCount(t *testing.T) {
t.Parallel()

config := &ChainConfig{BlocksWindow: 10000, MinSignedPerWindow: 0.05}
assert.Equal(t, config.GetBlocksSignCount(), int64(9500), "Blocks sign count does not match!")
assert.Equal(t, int64(9500), config.GetBlocksSignCount(), "Blocks sign count does not match!")
}

func TestGetChainBlocksMissCount(t *testing.T) {
t.Parallel()

config := &ChainConfig{BlocksWindow: 10000, MinSignedPerWindow: 0.05}
assert.Equal(t, config.GetBlocksMissCount(), int64(500), "Blocks miss count does not match!")
assert.Equal(t, int64(500), config.GetBlocksMissCount(), "Blocks miss count does not match!")
}

func TestValidateChainWithoutName(t *testing.T) {
t.Parallel()

config := &ChainConfig{}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateChainWithoutRPCEndpoints(t *testing.T) {
t.Parallel()

config := &ChainConfig{Name: "chain"}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateConsumerChainWithoutProviderEndpoints(t *testing.T) {
Expand All @@ -61,7 +63,7 @@ func TestValidateConsumerChainWithoutProviderEndpoints(t *testing.T) {
IsConsumer: null.BoolFrom(true),
}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateConsumerChainWithoutChainID(t *testing.T) {
Expand All @@ -74,7 +76,7 @@ func TestValidateConsumerChainWithoutChainID(t *testing.T) {
ProviderRPCEndpoints: []string{"endpoint"},
}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateNotEnoughThresholds(t *testing.T) {
Expand All @@ -87,7 +89,7 @@ func TestValidateNotEnoughThresholds(t *testing.T) {
EmojisEnd: []string{"x"},
}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateNotEnoughStartEmojis(t *testing.T) {
Expand All @@ -100,7 +102,7 @@ func TestValidateNotEnoughStartEmojis(t *testing.T) {
EmojisEnd: []string{"x", "y"},
}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateNotEnoughEndEmojis(t *testing.T) {
Expand All @@ -113,7 +115,7 @@ func TestValidateNotEnoughEndEmojis(t *testing.T) {
EmojisEnd: []string{"x"},
}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateFirstThresholdNotZero(t *testing.T) {
Expand All @@ -126,7 +128,7 @@ func TestValidateFirstThresholdNotZero(t *testing.T) {
EmojisEnd: []string{"x", "y"},
}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateLastThresholdNotHundred(t *testing.T) {
Expand All @@ -139,7 +141,7 @@ func TestValidateLastThresholdNotHundred(t *testing.T) {
EmojisEnd: []string{"x", "y"},
}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateThresholdsInconsistent(t *testing.T) {
Expand All @@ -152,7 +154,7 @@ func TestValidateThresholdsInconsistent(t *testing.T) {
EmojisEnd: []string{"x", "y", "z"},
}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateChainValid(t *testing.T) {
Expand All @@ -165,7 +167,7 @@ func TestValidateChainValid(t *testing.T) {
EmojisEnd: []string{"x", "y"},
}
err := config.Validate()
assert.Nil(t, err, "Error should not be present!")
require.NoError(t, err, "Error should not be present!")
}

func TestValidateConsumerChainWithoutEndpoints(t *testing.T) {
Expand All @@ -182,7 +184,7 @@ func TestValidateConsumerChainWithoutEndpoints(t *testing.T) {
EmojisEnd: []string{"x", "y"},
}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateConsumerChainWithoutChainId(t *testing.T) {
Expand All @@ -199,7 +201,7 @@ func TestValidateConsumerChainWithoutChainId(t *testing.T) {
EmojisEnd: []string{"x", "y"},
}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateConsumerChainValid(t *testing.T) {
Expand All @@ -216,5 +218,5 @@ func TestValidateConsumerChainValid(t *testing.T) {
EmojisEnd: []string{"x", "y"},
}
err := config.Validate()
assert.Nil(t, err, "Error should not be present!")
require.NoError(t, err, "Error should not be present!")
}
8 changes: 4 additions & 4 deletions pkg/config/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ package config
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestValidateDatabaseConfigWithoutPath(t *testing.T) {
t.Parallel()

config := &DatabaseConfig{Type: "sqlite"}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateDatabaseConfigWithoutType(t *testing.T) {
t.Parallel()

config := &DatabaseConfig{Path: "path"}
err := config.Validate()
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestValidateDatabaseConfigOk(t *testing.T) {
t.Parallel()

config := &DatabaseConfig{Path: "path", Type: "sqlite"}
err := config.Validate()
assert.Nil(t, err, "Error should not be present!")
require.NoError(t, err, "Error should not be present!")
}
12 changes: 6 additions & 6 deletions pkg/config/missed_blocks_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package config
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMissedBlocksGroupEmpty(t *testing.T) {
t.Parallel()

groups := MissedBlocksGroups{}
err := groups.Validate(10000)
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestMissedBlocksGroupMissingStart(t *testing.T) {
Expand All @@ -21,7 +21,7 @@ func TestMissedBlocksGroupMissingStart(t *testing.T) {
{Start: 5000, End: 10000},
}
err := groups.Validate(10000)
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestMissedBlocksGroupMissingEnd(t *testing.T) {
Expand All @@ -31,7 +31,7 @@ func TestMissedBlocksGroupMissingEnd(t *testing.T) {
{Start: 0, End: 5000},
}
err := groups.Validate(10000)
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestMissedBlocksGroupGaps(t *testing.T) {
Expand All @@ -42,7 +42,7 @@ func TestMissedBlocksGroupGaps(t *testing.T) {
{Start: 9000, End: 10000},
}
err := groups.Validate(10000)
assert.NotNil(t, err, "Error should be present!")
require.Error(t, err, "Error should be present!")
}

func TestMissedBlocksValid(t *testing.T) {
Expand All @@ -53,5 +53,5 @@ func TestMissedBlocksValid(t *testing.T) {
{Start: 5000, End: 10000},
}
err := groups.Validate(10000)
assert.Nil(t, err, "Error should not be present!")
require.NoError(t, err, "Error should not be present!")
}
4 changes: 4 additions & 0 deletions pkg/reporters/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Reporter struct {
Guild string
Channel string

Version string

DiscordSession *discordgo.Session
Logger zerolog.Logger
Config *config.ChainConfig
Expand All @@ -34,6 +36,7 @@ type Reporter struct {

func NewReporter(
chainConfig *config.ChainConfig,
version string,
logger zerolog.Logger,
manager *statePkg.Manager,
metricsManager *metrics.Manager,
Expand All @@ -50,6 +53,7 @@ func NewReporter(
SnapshotManager: snapshotManager,
TemplatesManager: templatesPkg.NewManager(logger, constants.DiscordReporterName),
Commands: make(map[string]*Command, 0),
Version: version,
}
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/reporters/discord/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ func (reporter *Reporter) GetHelpCommand() *Command {
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
reporter.MetricsManager.LogReporterQuery(reporter.Config.Name, constants.DiscordReporterName, "help")

template, err := reporter.TemplatesManager.Render("Help", reporter.Commands)
template, err := reporter.TemplatesManager.Render("Help", helpRender{
Version: reporter.Version,
Commands: reporter.Commands,
})
if err != nil {
reporter.Logger.Error().Err(err).Str("template", "help").Msg("Error rendering template")
return
Expand Down
5 changes: 5 additions & 0 deletions pkg/reporters/discord/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,8 @@ type statusRender struct {
func (s statusRender) FormatNotSignedPercent(entry statusEntry) string {
return fmt.Sprintf("%.2f", float64(entry.SigningInfo.GetNotSigned())/float64(s.ChainConfig.BlocksWindow)*100)
}

type helpRender struct {
Version string
Commands map[string]*Command
}
2 changes: 1 addition & 1 deletion pkg/reporters/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ type Reporter interface {
Name() constants.ReporterName
Enabled() bool
SerializeEvent(event types.ReportEvent) types.RenderEventItem
Send(*types.Report) error
Send(report *types.Report) error
}
2 changes: 1 addition & 1 deletion pkg/reporters/telegram/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (reporter *Reporter) HandleHelp(c tele.Context) error {

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

template, err := reporter.TemplatesManager.Render("Help", nil)
template, err := reporter.TemplatesManager.Render("Help", reporter.Version)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/reporters/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Reporter struct {
Chat int64
Admins []int64

Version string

TelegramBot *tele.Bot
Logger zerolog.Logger
Config *config.ChainConfig
Expand All @@ -39,6 +41,7 @@ const (

func NewReporter(
chainConfig *config.ChainConfig,
version string,
logger zerolog.Logger,
manager *statePkg.Manager,
metricsManager *metrics.Manager,
Expand All @@ -54,6 +57,7 @@ func NewReporter(
MetricsManager: metricsManager,
SnapshotManager: snapshotManager,
TemplatesManager: templatesPkg.NewManager(logger, constants.TelegramReporterName),
Version: version,
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/reporters/telegram/unsubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"html"
"main/pkg/constants"
"strconv"
"strings"

tele "gopkg.in/telebot.v3"
Expand Down Expand Up @@ -35,7 +36,7 @@ func (reporter *Reporter) HandleUnsubscribe(c tele.Context) error {
))
}

removed := reporter.Manager.RemoveNotifier(address, reporter.Name(), fmt.Sprintf("%d", c.Sender().ID))
removed := reporter.Manager.RemoveNotifier(address, reporter.Name(), strconv.FormatInt(c.Sender().ID, 10))

if !removed {
return reporter.BotReply(c, "You are not subscribed to this validator's notifications")
Expand Down
Loading

0 comments on commit 0e74504

Please sign in to comment.