From e763d05786d57b7ffeee3df39ae96f76a662590e Mon Sep 17 00:00:00 2001 From: Sergey <83376337+freak12techno@users.noreply.github.com> Date: Sat, 28 Sep 2024 13:57:37 +0300 Subject: [PATCH] feat: add blocks-behind flag (#39) --- cmd/tmtop.go | 1 + pkg/config/config.go | 7 +++++++ pkg/tendermint/tendermint.go | 4 +--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/tmtop.go b/cmd/tmtop.go index 39f6664..e6104a0 100644 --- a/cmd/tmtop.go +++ b/cmd/tmtop.go @@ -55,6 +55,7 @@ func main() { rootCmd.PersistentFlags().StringVar(&config.LCDHost, "lcd-host", "", "LCD API host URL") rootCmd.PersistentFlags().StringVar(&config.DebugFile, "debug-file", "", "Path to file to write debug info/logs to") rootCmd.PersistentFlags().Int64Var(&config.HaltHeight, "halt-height", 0, "Custom halt-height") + rootCmd.PersistentFlags().Uint64Var(&config.BlocksBehind, "blocks-behind", 1000, "How many blocks behind to check to calculate block time") rootCmd.PersistentFlags().StringVar(&config.Timezone, "timezone", "", "Timezone to display dates in") if err := rootCmd.Execute(); err != nil { diff --git a/pkg/config/config.go b/pkg/config/config.go index 8a8d085..8a7bcf5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -20,6 +20,7 @@ type InputConfig struct { DisableEmojis bool DebugFile string HaltHeight int64 + BlocksBehind uint64 LCDHost string Timezone string } @@ -66,6 +67,10 @@ func ParseAndValidateConfig(input InputConfig) (*Config, error) { return nil, errors.New("chain-type is 'cosmos-lcd', but lcd-host is not set") } + if input.BlocksBehind <= 0 { + return nil, errors.New("cannot run with a negative blocks-behind") + } + timezone := time.Local //nolint:gosmopolitan // local timezone is expected here if input.Timezone != "" { @@ -91,6 +96,7 @@ func ParseAndValidateConfig(input InputConfig) (*Config, error) { DisableEmojis: input.DisableEmojis, DebugFile: input.DebugFile, HaltHeight: input.HaltHeight, + BlocksBehind: input.BlocksBehind, LCDHost: input.LCDHost, Timezone: timezone, } @@ -112,6 +118,7 @@ type Config struct { DisableEmojis bool DebugFile string HaltHeight int64 + BlocksBehind uint64 LCDHost string Timezone *time.Location } diff --git a/pkg/tendermint/tendermint.go b/pkg/tendermint/tendermint.go index 98d2461..d58305d 100644 --- a/pkg/tendermint/tendermint.go +++ b/pkg/tendermint/tendermint.go @@ -127,8 +127,6 @@ func (rpc *RPC) Block(height int64) (types.TendermintBlockResponse, error) { } func (rpc *RPC) GetBlockTime() (time.Duration, error) { - var blocksBehind int64 = 1000 - latestBlock, err := rpc.Block(0) if err != nil { rpc.Logger.Error().Err(err).Msg("Could not fetch current block") @@ -146,7 +144,7 @@ func (rpc *RPC) GetBlockTime() (time.Duration, error) { Msg("Error converting latest block height to int64, which should never happen.") return 0, err } - olderBlockHeight := latestBlockHeight - blocksBehind + olderBlockHeight := latestBlockHeight - int64(rpc.Config.BlocksBehind) if olderBlockHeight <= 0 { olderBlockHeight = 1 }