diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index fc707bc..2959721 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -45,13 +45,13 @@ const ( func GetEventNames() []EventName { return []EventName{ - EventValidatorActive, - EventValidatorGroupChanged, - EventValidatorInactive, + EventValidatorTombstoned, EventValidatorJailed, + EventValidatorInactive, EventValidatorUnjailed, - EventValidatorTombstoned, + EventValidatorActive, EventValidatorCreated, + EventValidatorGroupChanged, } } diff --git a/pkg/snapshot/snapshot.go b/pkg/snapshot/snapshot.go index 55c567b..1fe1d05 100644 --- a/pkg/snapshot/snapshot.go +++ b/pkg/snapshot/snapshot.go @@ -1,11 +1,14 @@ package snapshot import ( + "golang.org/x/exp/slices" "main/pkg/config" + "main/pkg/constants" "main/pkg/events" "main/pkg/report" "main/pkg/types" "math" + "sort" ) type Entry struct { @@ -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 } diff --git a/pkg/snapshot/snapshot_test.go b/pkg/snapshot/snapshot_test.go index 49a9ab7..0b0c3cf 100644 --- a/pkg/snapshot/snapshot_test.go +++ b/pkg/snapshot/snapshot_test.go @@ -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!") +}