Skip to content

Commit

Permalink
chore: display proper block time on genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Aug 28, 2024
1 parent 5cb8b9b commit c3dab50
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,17 @@ func (rpc *RPC) GetBlockTime() (time.Duration, error) {
Msg("Error converting latest block height to int64, which should never happen.")
return 0, err
}
blockToCheck := latestBlockHeight - blocksBehind
olderBlockHeight := latestBlockHeight - blocksBehind
if olderBlockHeight <= 0 {
olderBlockHeight = 1
}

blocksDiff := latestBlockHeight - olderBlockHeight
if blocksDiff <= 0 {
return 0, fmt.Errorf("cannot calculate block time with the negative blocks counter")
}

olderBlock, err := rpc.Block(blockToCheck)
olderBlock, err := rpc.Block(olderBlockHeight)
if err != nil {
rpc.Logger.Error().Err(err).Msg("Could not fetch older block")
return 0, err
Expand All @@ -159,7 +167,7 @@ func (rpc *RPC) GetBlockTime() (time.Duration, error) {
}

blocksDiffTime := latestBlock.Result.Block.Header.Time.Sub(olderBlock.Result.Block.Header.Time)
blockTime := blocksDiffTime.Seconds() / float64(blocksBehind)
blockTime := blocksDiffTime.Seconds() / float64(blocksDiff)

duration := time.Duration(int64(blockTime * float64(time.Second)))
return duration, nil
Expand Down

0 comments on commit c3dab50

Please sign in to comment.