Skip to content

Commit

Permalink
feat(liveness): improve tests (#1623)
Browse files Browse the repository at this point in the history
  • Loading branch information
danwt authored Dec 8, 2024
1 parent 00799a0 commit de9f463
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
15 changes: 13 additions & 2 deletions x/rollapp/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,22 @@ func LivenessEventInvariant(k Keeper) sdk.Invariant {
msg += fmt.Sprintf("| rollapp stored event but wrong number found in queue: rollapp: %s: event height: %d: found: %d", ra.RollappId, ra.LivenessEventHeight, cnt)
}
}
for _, e := range k.GetLivenessEvents(ctx, nil) {
evts := k.GetLivenessEvents(ctx, nil)
seen := make(map[string]struct{})
for i, e := range evts {
if 0 < i && e.HubHeight < evts[i-1].HubHeight {
broken = true
msg += fmt.Sprintf("| events not sorted by height: event: %v\n", e)
}
if _, ok := seen[e.RollappId]; ok {
broken = true
msg += fmt.Sprintf("| more than one rollapp event: %v\n", e)
}
seen[e.RollappId] = struct{}{}
ra, ok := k.GetRollapp(ctx, e.RollappId)
if !ok {
broken = true
msg += fmt.Sprintf("| event stored but rollapp not found: rollapp id: %s", e.RollappId)
msg += fmt.Sprintf("| event stored but rollapp not found: rollapp id: %s\n", e.RollappId)
continue
}
if ra.LivenessEventHeight != e.HubHeight {
Expand Down
26 changes: 24 additions & 2 deletions x/rollapp/keeper/liveness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestLivenessArithmetic(t *testing.T) {
)
require.Equal(t, 8, int(hEvent))
})
t.Run("do not schedule for next height", func(t *testing.T) {
t.Run("do not schedule for next height (1)", func(t *testing.T) {
hEvent := keeper.NextSlashHeight(
8,
4,
Expand All @@ -45,7 +45,7 @@ func TestLivenessArithmetic(t *testing.T) {
)
require.Equal(t, 12, int(hEvent))
})
t.Run("do not schedule for next height", func(t *testing.T) {
t.Run("do not schedule for next height (2)", func(t *testing.T) {
hEvent := keeper.NextSlashHeight(
8,
4,
Expand All @@ -56,6 +56,24 @@ func TestLivenessArithmetic(t *testing.T) {
})
}

func TestCannotScheduleForPast(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
var (
noUpdate = rapid.Uint64Range(1, 100).Draw(t, "noUpdate")
slashInterval = rapid.Uint64Range(1, 100).Draw(t, "slashInterval")
heightHub = rapid.Int64Range(0, 100).Draw(t, "heightHub")
lastRollappUpdate = rapid.Int64Range(0, 100).Draw(t, "lastRollappUpdate")
)
res := keeper.NextSlashHeight(noUpdate, slashInterval, heightHub, lastRollappUpdate)
if res <= heightHub {
t.Fatalf(
"no update %d, interval %d, hub %d, last update %d, res %d",
noUpdate, slashInterval, heightHub, lastRollappUpdate, res,
)
}
})
}

// Storage and query operations work for the event queue
func TestLivenessEventsStorage(t *testing.T) {
_ = flag.Set("rapid.checks", "50")
Expand Down Expand Up @@ -100,6 +118,10 @@ func TestLivenessEventsStorage(t *testing.T) {
},
"iterAll": func(r *rapid.T) {
events := k.GetLivenessEvents(ctx, nil)
for i := 1; i < len(events); i++ {
require.LessOrEqual(r, events[i-1].HubHeight, events[i].HubHeight, "events not sorted")
i++
}
for _, modelE := range model {
require.Contains(r, events, modelE, "event in model but not store")
}
Expand Down

0 comments on commit de9f463

Please sign in to comment.