Skip to content

Commit

Permalink
Add suggested fixes (#2)
Browse files Browse the repository at this point in the history
* add additional helper function to makes switch conditional re-usable

Signed-off-by: Simon Lichtenauer <[email protected]>

* change to camelCase

Signed-off-by: Simon Lichtenauer <[email protected]>

* fix: remove not needed redundant variable asignments and log.Info messages

Signed-off-by: Simon Lichtenauer <[email protected]>

* fix: ensure slashing module is also not used in watcher.validator module

Signed-off-by: Simon Lichtenauer <[email protected]>

* fix: use camelCase

Signed-off-by: Simon Lichtenauer <[email protected]>

---------

Signed-off-by: Simon Lichtenauer <[email protected]>
  • Loading branch information
qwertzlbert authored Nov 12, 2024
1 parent b9c1169 commit d32ac03
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 46 deletions.
1 change: 1 addition & 0 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func RunFunc(cCtx *cli.Context) error {
validatorsWatcher := watcher.NewValidatorsWatcher(trackedValidators, metrics, pool, watcher.ValidatorsWatcherOptions{
Denom: denom,
DenomExponent: denomExpon,
NoSlashing: noSlashing,
})
errg.Go(func() error {
return validatorsWatcher.Start(ctx)
Expand Down
18 changes: 11 additions & 7 deletions pkg/crypto/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,34 @@ package crypto
import (
"strings"

"github.com/cometbft/cometbft/libs/bytes"
types1 "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/types/bech32"
)

func PubKeyAddress(consensusPubkey *types1.Any) string {
func PubKeyAddressHelper(consensusPubkey *types1.Any) bytes.HexBytes {
switch consensusPubkey.TypeUrl {
case "/cosmos.crypto.ed25519.PubKey":
key := ed25519.PubKey{Key: consensusPubkey.Value[2:]}
return key.Address().String()
return key.Address()

case "/cosmos.crypto.secp256k1.PubKey":
key := secp256k1.PubKey{Key: consensusPubkey.Value[2:]}
return key.Address().String()
return key.Address()
}

panic("unknown pubkey type: " + consensusPubkey.TypeUrl)
}

func PubKeyAddress(consensusPubkey *types1.Any) string {
key := PubKeyAddressHelper(consensusPubkey)
return key.String()
}

func PubKeyBech32Address(consensusPubkey *types1.Any, prefix string) string {
key := PubKeyAddress(consensusPubkey)
address, _ := bech32.ConvertAndEncode(prefix, key.Address())

key := PubKeyAddressHelper(consensusPubkey)
address, _ := bech32.ConvertAndEncode(prefix, key)
return address
}

Expand Down
28 changes: 14 additions & 14 deletions pkg/watcher/slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type SlashingWatcher struct {
metrics *metrics.Metrics
pool *rpc.Pool

signedBlocksWindow int64
min_signed_per_window float64
downtime_jail_duration float64
slash_fraction_double_sign float64
slash_fraction_downtime float64
signedBlocksWindow int64
minSignedPerWindow float64
downtimeJailDuration float64
slashFractionDoubleSign float64
slashFractionDowntime float64
}

func NewSlashingWatcher(metrics *metrics.Metrics, pool *rpc.Pool) *SlashingWatcher {
Expand Down Expand Up @@ -57,7 +57,7 @@ func (w *SlashingWatcher) fetchSlashingParameters(ctx context.Context, node *rpc
queryClient := slashing.NewQueryClient(clientCtx)
sigininParams, err := queryClient.Params(ctx, &slashing.QueryParamsRequest{})
if err != nil {
return fmt.Errorf("failed to get signing infos: %w", err)
return fmt.Errorf("failed to get slashing parameters: %w", err)
}

w.handleSlashingParams(node.ChainID(), sigininParams.Params)
Expand All @@ -77,14 +77,14 @@ func (w *SlashingWatcher) handleSlashingParams(chainID string, params slashing.P
Msgf("updating slashing metrics")

w.signedBlocksWindow = params.SignedBlocksWindow
w.min_signed_per_window, _ = params.MinSignedPerWindow.Float64()
w.downtime_jail_duration = params.DowntimeJailDuration.Seconds()
w.slash_fraction_double_sign, _ = params.SlashFractionDoubleSign.Float64()
w.slash_fraction_downtime, _ = params.SlashFractionDowntime.Float64()
w.minSignedPerWindow, _ = params.MinSignedPerWindow.Float64()
w.downtimeJailDuration = params.DowntimeJailDuration.Seconds()
w.slashFractionDoubleSign, _ = params.SlashFractionDoubleSign.Float64()
w.slashFractionDowntime, _ = params.SlashFractionDowntime.Float64()

w.metrics.SignedBlocksWindow.WithLabelValues(chainID).Set(float64(w.signedBlocksWindow))
w.metrics.MinSignedBlocksPerWindow.WithLabelValues(chainID).Set(w.min_signed_per_window)
w.metrics.DowntimeJailDuration.WithLabelValues(chainID).Set(w.downtime_jail_duration)
w.metrics.SlashFractionDoubleSign.WithLabelValues(chainID).Set(w.slash_fraction_double_sign)
w.metrics.SlashFractionDowntime.WithLabelValues(chainID).Set(w.slash_fraction_downtime)
w.metrics.MinSignedBlocksPerWindow.WithLabelValues(chainID).Set(w.minSignedPerWindow)
w.metrics.DowntimeJailDuration.WithLabelValues(chainID).Set(w.downtimeJailDuration)
w.metrics.SlashFractionDoubleSign.WithLabelValues(chainID).Set(w.slashFractionDoubleSign)
w.metrics.SlashFractionDowntime.WithLabelValues(chainID).Set(w.slashFractionDowntime)
}
12 changes: 6 additions & 6 deletions pkg/watcher/slashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ func TestSlashingWatcher(t *testing.T) {

t.Run("Handle Slashing Parameters", func(t *testing.T) {

min_signed_per_window := cosmossdk_io_math.LegacyMustNewDecFromStr("0.1")
slash_fraction_double_sign := cosmossdk_io_math.LegacyMustNewDecFromStr("0.01")
slash_fraction_downtime := cosmossdk_io_math.LegacyMustNewDecFromStr("0.001")
minSignedPerWindow := cosmossdk_io_math.LegacyMustNewDecFromStr("0.1")
slashFractionDoubleSign := cosmossdk_io_math.LegacyMustNewDecFromStr("0.01")
slashFractionDowntime := cosmossdk_io_math.LegacyMustNewDecFromStr("0.001")

params := slashing.Params{
SignedBlocksWindow: int64(1000),
MinSignedPerWindow: min_signed_per_window,
MinSignedPerWindow: minSignedPerWindow,
DowntimeJailDuration: time.Duration(10) * time.Second,
SlashFractionDoubleSign: slash_fraction_double_sign,
SlashFractionDowntime: slash_fraction_downtime,
SlashFractionDoubleSign: slashFractionDoubleSign,
SlashFractionDowntime: slashFractionDowntime,
}

watcher.handleSlashingParams(chainID, params)
Expand Down
37 changes: 18 additions & 19 deletions pkg/watcher/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ValidatorsWatcher struct {
type ValidatorsWatcherOptions struct {
Denom string
DenomExponent uint
NoSlashing bool
}

func NewValidatorsWatcher(validators []TrackedValidator, metrics *metrics.Metrics, pool *rpc.Pool, opts ValidatorsWatcherOptions) *ValidatorsWatcher {
Expand Down Expand Up @@ -54,7 +55,6 @@ func (w *ValidatorsWatcher) Start(ctx context.Context) error {
Str("node", node.Redacted()).
Msg("failed to fetch signing infos")
}
log.Info().Msg("fetched staking validators and signing infos")
select {
case <-ctx.Done():
return nil
Expand All @@ -64,20 +64,24 @@ func (w *ValidatorsWatcher) Start(ctx context.Context) error {
}

func (w *ValidatorsWatcher) fetchSigningInfos(ctx context.Context, node *rpc.Node) error {
clientCtx := (client.Context{}).WithClient(node.Client)
queryClient := slashing.NewQueryClient(clientCtx)
signingInfos, err := queryClient.SigningInfos(ctx, &slashing.QuerySigningInfosRequest{
Pagination: &query.PageRequest{
Limit: 3000,
},
})
if err != nil {
return fmt.Errorf("failed to get signing infos: %w", err)
}
if !w.opts.NoSlashing {
clientCtx := (client.Context{}).WithClient(node.Client)
queryClient := slashing.NewQueryClient(clientCtx)
signingInfos, err := queryClient.SigningInfos(ctx, &slashing.QuerySigningInfosRequest{
Pagination: &query.PageRequest{
Limit: 3000,
},
})
if err != nil {
return fmt.Errorf("failed to get signing infos: %w", err)
}

w.handleSigningInfos(node.ChainID(), signingInfos.Info)
w.handleSigningInfos(node.ChainID(), signingInfos.Info)

return nil
return nil
} else {
return nil
}

}

Expand All @@ -101,16 +105,11 @@ func (w *ValidatorsWatcher) fetchValidators(ctx context.Context, node *rpc.Node)

func (w *ValidatorsWatcher) handleSigningInfos(chainID string, signingInfos []slashing.ValidatorSigningInfo) {
for _, tracked := range w.validators {
name := tracked.Name

for _, val := range signingInfos {

if tracked.ConsensusAddress == val.Address {
var (
missedBlocksWindow = val.MissedBlocksCounter
)
log.Info().Msgf("Tracked validator missed blocks: %d", missedBlocksWindow)
w.metrics.MissedBlocksWindow.WithLabelValues(chainID, tracked.Address, name).Set(float64(missedBlocksWindow))
w.metrics.MissedBlocksWindow.WithLabelValues(chainID, tracked.Address, tracked.Name).Set(float64(val.MissedBlocksCounter))
break
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/watcher/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestValidatorsWatcher(t *testing.T) {
ValidatorsWatcherOptions{
Denom: "denom",
DenomExponent: 6,
NoSlashing: false,
},
)

Expand Down

0 comments on commit d32ac03

Please sign in to comment.