/
abci.go
37 lines (32 loc) · 1.2 KB
/
abci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package downtimedetector
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/v24/x/downtime-detector/types"
)
func (k *Keeper) BeginBlock(ctx sdk.Context) {
curTime := ctx.BlockTime()
lastBlockTime, err := k.GetLastBlockTime(ctx)
if err != nil {
ctx.Logger().Error("Downtime-detector, could not get last block time, did initialization happen correctly. " + err.Error())
}
downtime := curTime.Sub(lastBlockTime)
k.saveDowntimeUpdates(ctx, downtime)
k.StoreLastBlockTime(ctx, curTime)
}
// saveDowntimeUpdates saves the current block time as the
// last time the chain was down for all downtime lengths that are LTE the provided downtime.
func (k *Keeper) saveDowntimeUpdates(ctx sdk.Context, downtime time.Duration) {
// minimum stored downtime is 30S, so if downtime is less than that, don't update anything.
if downtime < 30*time.Second {
return
}
types.DowntimeToDuration.Ascend(0, func(downType types.Downtime, duration time.Duration) bool {
// if downtime < duration of this entry, stop iterating further, don't update this entry.
if downtime < duration {
return false
}
k.StoreLastDowntimeOfLength(ctx, downType, ctx.BlockTime())
return true
})
}