Skip to content

Commit

Permalink
adjust tests more
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown unknown committed Aug 2, 2023
1 parent 3a25740 commit 2471b1b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 18 deletions.
2 changes: 2 additions & 0 deletions proto/downtime/v1/downtime.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ message Params {
// downtime_duration defines the minimum time elapsed between blocks
// that we consider the chain to be down.
google.protobuf.Duration downtime_duration = 1 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
// epoch_duration defines an estimation of the time elapsed between epochs
google.protobuf.Duration epoch_duration = 2 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
}

// Downtime defines a single downtime record.
Expand Down
9 changes: 9 additions & 0 deletions x/downtime/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ func (k Keeper) GarbageCollectDowntimes(ctx sdk.Context) {
}
}

func (k Keeper) GetDowntimeFactor(ctx sdk.Context, epoch uint64) uint64 {
duration, exists := k.GetDowntime(ctx, epoch)
if !exists {
return 1
}
epochDuration := k.GetParams(ctx).EpochDuration
return uint64(duration/epochDuration) + 1
}

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.
Expand Down
10 changes: 10 additions & 0 deletions x/downtime/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ func TestBeginBlock(t *testing.T) {
require.True(t, hadDowntimes)
require.Equal(t, 2*keeper.GetParams(ctx).DowntimeDuration, duration)

// check downtime factors
factor := keeper.GetDowntimeFactor(ctx, epochStartBlock(ctx))
require.Equal(t, uint64(1), factor)

// now let's advance the block until we have 1 entire epoch of downtime
ctx = nextBlock(ctx, keeper.GetParams(ctx).EpochDuration-duration)
keeper.BeginBlock(ctx)
factor = keeper.GetDowntimeFactor(ctx, epochStartBlock(ctx))
require.Equal(t, uint64(2), factor)

// check garbage collection was done, after forcing an epoch to pass
for !app.EpochstorageKeeper.IsEpochStart(ctx) {
ctx = nextBlock(ctx, 1*time.Second)
Expand Down
87 changes: 70 additions & 17 deletions x/downtime/v1/downtime.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion x/downtime/v1/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import (

var (
ParamKeyDowntimeDuration = []byte("DowntimeDuration")
DefaultParamKeyDowntimeDuration = 30 * time.Minute
DefaultParamKeyDowntimeDuration = 5 * time.Minute
ParamKeyEpochDuration = []byte("EpochDuration")
DefaultParamKeyEpochDuration = 30 * time.Minute
)

func DefaultParams() Params {
return Params{
DowntimeDuration: DefaultParamKeyDowntimeDuration,
EpochDuration: DefaultParamKeyEpochDuration,
}
}

Expand All @@ -23,13 +26,17 @@ var _ paramtypes.ParamSet = (*Params)(nil)
func (m *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamKeyDowntimeDuration, &m.DowntimeDuration, validateDowntimeDuration),
paramtypes.NewParamSetPair(ParamKeyEpochDuration, &m.EpochDuration, validateDowntimeDuration),
}
}

func (m *Params) Validate() error {
if err := validateDowntimeDuration(m.DowntimeDuration); err != nil {
return err
}
if err := validateDowntimeDuration(m.EpochDuration); err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit 2471b1b

Please sign in to comment.