Skip to content

Commit

Permalink
feat: add blocks-behind flag (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Sep 28, 2024
1 parent e450dd3 commit e763d05
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/tmtop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type InputConfig struct {
DisableEmojis bool
DebugFile string
HaltHeight int64
BlocksBehind uint64
LCDHost string
Timezone string
}
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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,
}
Expand All @@ -112,6 +118,7 @@ type Config struct {
DisableEmojis bool
DebugFile string
HaltHeight int64
BlocksBehind uint64
LCDHost string
Timezone *time.Location
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
Expand Down

0 comments on commit e763d05

Please sign in to comment.