Skip to content

Commit

Permalink
chore: warn on unused reporters and chains (#62)
Browse files Browse the repository at this point in the history
* chore: warn on unused reporters and chains

* chore: do not check for empty denoms
  • Loading branch information
freak12techno committed Feb 29, 2024
1 parent 30018a2 commit 141d7d2
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 29 deletions.
33 changes: 33 additions & 0 deletions assets/valid-unused-chain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
timezone = "Etc/UTC"

[[reporters]]
name = "reporter"
type = "telegram"
telegram-config = { token = "xxx:yyy", chat = 12345, admins = [67890] }

[[subscriptions]]
reporter = "reporter"
name = "subscription"
[[subscriptions.chains]]
name = "cosmos"
filters = ["message.action = '/cosmos.gov.v1beta1.MsgVote'", ]

[[chains]]
name = "cosmos"
chain-id = "cosmoshub-4"
tendermint-nodes = ["https://rpc.cosmos.quokkastake.io:443", ]
api-nodes = ["https://api.cosmos.quokkastake.io"]
mintscan-prefix = "cosmos"
denoms = [
{ denom = "uatom", display-denom = "atom", coingecko-currency = "cosmos" }
]

[[chains]]
name = "sentinel"
chain-id = "sentinelhub-2"
tendermint-nodes = ["https://rpc.sentine;.quokkastake.io:443", ]
api-nodes = ["https://api.sentine;.quokkastake.io"]
mintscan-prefix = "sent"
denoms = [
{ denom = "udvpn", display-denom = "dvpn", coingecko-currency = "sentinel" }
]
28 changes: 28 additions & 0 deletions assets/valid-unused-reporter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
timezone = "Etc/UTC"

[[reporters]]
name = "reporter"
type = "telegram"
telegram-config = { token = "xxx:yyy", chat = 12345, admins = [67890] }

[[reporters]]
name = "reporter-2"
type = "telegram"
telegram-config = { token = "xxx:yyy", chat = 12345, admins = [67890] }

[[subscriptions]]
reporter = "reporter"
name = "subscription"
[[subscriptions.chains]]
name = "cosmos"
filters = ["message.action = '/cosmos.gov.v1beta1.MsgVote'", ]

[[chains]]
name = "cosmos"
chain-id = "cosmoshub-4"
tendermint-nodes = ["https://rpc.cosmos.quokkastake.io:443", ]
api-nodes = ["https://api.cosmos.quokkastake.io"]
mintscan-prefix = "cosmos"
denoms = [
{ denom = "uatom", display-denom = "atom", coingecko-currency = "cosmos" }
]
29 changes: 29 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,35 @@ func (c *AppConfig) DisplayWarnings() []types.DisplayWarning {
warnings = append(warnings, chain.DisplayWarnings()...)
}

reportersUsed := map[string]bool{}
chainsUsed := map[string]bool{}

for _, subscription := range c.Subscriptions {
reportersUsed[subscription.Reporter] = true

for _, chainSubscription := range subscription.ChainSubscriptions {
chainsUsed[chainSubscription.Chain] = true
}
}

for _, chain := range c.Chains {
if _, ok := chainsUsed[chain.Name]; !ok {
warnings = append(warnings, types.DisplayWarning{
Keys: map[string]string{"chain": chain.Name},
Text: "Chain is not used in any subscriptions",
})
}
}

for _, reporter := range c.Reporters {
if _, ok := reportersUsed[reporter.Name]; !ok {
warnings = append(warnings, types.DisplayWarning{
Keys: map[string]string{"reporter": reporter.Name},
Text: "Reporter is not used in any subscriptions",
})
}
}

return warnings
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,27 @@ func TestGetConfigAsString(t *testing.T) {
configString := config.GetConfigAsString()
require.NotEmpty(t, configString)
}

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

config, err := configPkg.GetConfig("valid-unused-reporter.toml", assets.AssetsFS)

require.NoError(t, err)
require.NotNil(t, config)

warnings := config.DisplayWarnings()
require.Len(t, warnings, 1)
}

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

config, err := configPkg.GetConfig("valid-unused-chain.toml", assets.AssetsFS)

require.NoError(t, err)
require.NotNil(t, config)

warnings := config.DisplayWarnings()
require.Len(t, warnings, 1)
}
11 changes: 2 additions & 9 deletions pkg/config/types/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,8 @@ func (c Chain) GetBlockLink(height int64) Link {
func (c *Chain) DisplayWarnings() []DisplayWarning {
var warnings []DisplayWarning

if len(c.Denoms) == 0 {
warnings = append(warnings, DisplayWarning{
Keys: map[string]string{"chain": c.Name},
Text: "No denoms set, prices in USD won't be displayed.",
})
} else {
for _, denom := range c.Denoms {
warnings = append(warnings, denom.DisplayWarnings(c)...)
}
for _, denom := range c.Denoms {
warnings = append(warnings, denom.DisplayWarnings(c)...)
}

if c.ChainID == "" {
Expand Down
20 changes: 0 additions & 20 deletions pkg/config/types/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,26 +161,6 @@ func TestChainGetBlockLink(t *testing.T) {
require.Empty(t, link2.Title)
}

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

chain := types.Chain{
Name: "name",
ChainID: "chain-id",
Explorer: &types.Explorer{
TransactionLinkPattern: "test/%s",
BlockLinkPattern: "test/%s",
WalletLinkPattern: "test/%s",
ValidatorLinkPattern: "test/%s",
ProposalLinkPattern: "test/%s",
},
}

warnings := chain.DisplayWarnings()

require.Len(t, warnings, 1)
}

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

Expand Down

0 comments on commit 141d7d2

Please sign in to comment.