Skip to content

Commit

Permalink
fix downtime recorded twice
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown unknown committed Jul 19, 2023
1 parent 0217aca commit 87a0fde
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 7 additions & 4 deletions x/downtime/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,19 @@ func (k Keeper) GarbageCollectDowntimes(ctx sdk.Context) {
}

func (k Keeper) BeginBlock(ctx sdk.Context) {
// this ensures that no matter the outcome, we will
// reset the last block time to the current block time.
defer func() {
k.SetLastBlockTime(ctx, ctx.BlockTime())
}()

k.GarbageCollectDowntimes(ctx)
// we fetch the last block time
lastBlockTime, ok := k.GetLastBlockTime(ctx)
// if no last block time is known then it means that
// this is the first time we're recording a block time,
// so we just store the current block time and exit.
if !ok {
k.SetLastBlockTime(ctx, ctx.BlockTime())
return
}

Expand All @@ -248,10 +253,8 @@ func (k Keeper) BeginBlock(ctx sdk.Context) {
params := k.GetParams(ctx)
maxDowntimeDuration := params.DowntimeDuration
elapsedDuration := ctx.BlockTime().Sub(lastBlockTime)
// we didn't find any downtimes. So we exit.
if elapsedDuration < maxDowntimeDuration {
// if the current block time is less than the max downtime duration
// then we just store the current block time and exit.
k.SetLastBlockTime(ctx, ctx.BlockTime())
return
}
// if the current block time is greater than the max downtime duration
Expand Down
12 changes: 12 additions & 0 deletions x/downtime/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ func TestBeginBlock(t *testing.T) {
require.True(t, hadDowntimes)
require.Equal(t, keeper.GetParams(ctx).DowntimeDuration, duration)

// move into next block, it shouldn't have downtimes.
ctx = nextBlock(ctx, 1*time.Second)
keeper.BeginBlock(ctx)
_, hadDowntimes = keeper.GetDowntime(ctx, uint64(ctx.BlockHeight()))
require.False(t, hadDowntimes)

// now check garbage collection
// we extend the downtime duration in order not to have another downtime
// since we're making time elapse by a lot in order to trigger garbage collection!
Expand All @@ -132,6 +138,12 @@ func TestBeginBlock(t *testing.T) {
require.False(t, iter.Valid())
}

func TestBeginBlock_DowntimeReset(t *testing.T) {
// we test that after a downtime the last block
// time is correctly reset.

}

func TestImportExportGenesis(t *testing.T) {
app, ctx := app.TestSetup()
keeper := app.DowntimeKeeper
Expand Down

0 comments on commit 87a0fde

Please sign in to comment.