Skip to content

Commit

Permalink
feat: add events priority
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Aug 10, 2023
1 parent 4e351b9 commit dd7581e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ const (

func GetEventNames() []EventName {
return []EventName{
EventValidatorActive,
EventValidatorGroupChanged,
EventValidatorInactive,
EventValidatorTombstoned,
EventValidatorJailed,
EventValidatorInactive,
EventValidatorUnjailed,
EventValidatorTombstoned,
EventValidatorActive,
EventValidatorCreated,
EventValidatorGroupChanged,
}
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package snapshot

import (
"golang.org/x/exp/slices"

Check failure on line 4 in pkg/snapshot/snapshot.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"main/pkg/config"
"main/pkg/constants"
"main/pkg/events"
"main/pkg/report"
"main/pkg/types"
"math"
"sort"
)

type Entry struct {
Expand Down Expand Up @@ -118,6 +121,13 @@ func (snapshot *Snapshot) GetReport(
}
}

sort.Slice(entries, func(first, second int) bool {
firstPriority := slices.Index(constants.GetEventNames(), entries[first].Type())
secondPriority := slices.Index(constants.GetEventNames(), entries[second].Type())

return firstPriority < secondPriority
})

return &report.Report{Entries: entries}, nil
}

Expand Down
48 changes: 48 additions & 0 deletions pkg/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,51 @@ func TestOldMissedBlocksGroupNotPresent(t *testing.T) {
assert.NotNil(t, err, "Error should be present!")
assert.Nil(t, report, "Report should not be present!")
}

func TestSorting(t *testing.T) {
t.Parallel()

config := &configPkg.ChainConfig{
MissedBlocksGroups: []*configPkg.MissedBlocksGroup{
{Start: 0, End: 49},
{Start: 50, End: 99},
},
}

olderSnapshot := Snapshot{Entries: map[string]Entry{
"validator1": {
Validator: &types.Validator{Jailed: false, Status: 3},
SignatureInfo: types.SignatureInto{NotSigned: 25},
},
"validator2": {
Validator: &types.Validator{Jailed: false, Status: 3},
SignatureInfo: types.SignatureInto{NotSigned: 25},
},
"validator3": {
Validator: &types.Validator{Jailed: false, Status: 3, SigningInfo: &types.SigningInfo{Tombstoned: false}},
SignatureInfo: types.SignatureInto{NotSigned: 25},
},
}}
newerSnapshot := Snapshot{Entries: map[string]Entry{
"validator1": {
Validator: &types.Validator{Jailed: true, Status: 3},
SignatureInfo: types.SignatureInto{NotSigned: 25},
},
"validator2": {
Validator: &types.Validator{Jailed: false, Status: 3},
SignatureInfo: types.SignatureInto{NotSigned: 75},
},
"validator3": {
Validator: &types.Validator{Jailed: false, Status: 3, SigningInfo: &types.SigningInfo{Tombstoned: true}},
SignatureInfo: types.SignatureInto{NotSigned: 25},
},
}}

report, err := newerSnapshot.GetReport(olderSnapshot, config)
assert.Nil(t, err, "Error should not be present!")
assert.NotNil(t, report, "Report should be present!")
assert.Len(t, report.Entries, 3, "Slice should have exactly 3 entries!")
assert.Equal(t, report.Entries[0].Type(), constants.EventValidatorTombstoned, "Entry type mismatch!")
assert.Equal(t, report.Entries[1].Type(), constants.EventValidatorJailed, "Entry type mismatch!")
assert.Equal(t, report.Entries[2].Type(), constants.EventValidatorGroupChanged, "Entry type mismatch!")
}

0 comments on commit dd7581e

Please sign in to comment.