diff --git a/pkg/converter/converter.go b/pkg/converter/converter.go index 4808bb4..78fdd2b 100644 --- a/pkg/converter/converter.go +++ b/pkg/converter/converter.go @@ -79,7 +79,6 @@ func (c *Converter) ValidatorFromCosmosValidator( ConsensusAddressHex: fmt.Sprintf("%x", addr), ConsensusAddressValcons: addr.String(), OperatorAddress: validator.OperatorAddress, - Status: int32(validator.Status), Jailed: validator.Jailed, SigningInfo: valSigningInfo, VotingPower: big.NewFloat(0).SetInt(validator.DelegatorShares.BigInt()), diff --git a/pkg/data/manager.go b/pkg/data/manager.go index be7879a..b8c7881 100644 --- a/pkg/data/manager.go +++ b/pkg/data/manager.go @@ -106,8 +106,6 @@ func (manager *Manager) GetValidators(height int64) (types.Validators, []error) validators[index] = validator } - validators.SetVotingPowerPercent() - return validators, nil } @@ -224,8 +222,6 @@ func (manager *Manager) GetValidatorsAndSigningInfoForConsumerChain(height int64 mutex.Unlock() } - validators.SetVotingPowerPercent() - return validators, errs } diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 9a43454..241ae1f 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -374,7 +374,7 @@ func (m *Manager) LogNodeReconnect(chain string, node string) { func (m *Manager) LogValidatorStats( chain *configPkg.ChainConfig, - entry types.Entry, + entry *types.Entry, ) { m.missingBlocksGauge. With(prometheus.Labels{ diff --git a/pkg/reporters/discord/missing.go b/pkg/reporters/discord/missing.go index a226b60..a6a090f 100644 --- a/pkg/reporters/discord/missing.go +++ b/pkg/reporters/discord/missing.go @@ -28,7 +28,7 @@ func (reporter *Reporter) GetMissingCommand() *Command { } validatorEntries := snapshot.Entries.ToSlice() - activeValidatorsEntries := utils.Filter(validatorEntries, func(v types.Entry) bool { + activeValidatorsEntries := utils.Filter(validatorEntries, func(v *types.Entry) bool { if !v.IsActive { return false } @@ -46,7 +46,7 @@ func (reporter *Reporter) GetMissingCommand() *Command { render := missingValidatorsRender{ Config: reporter.Config, - Validators: utils.Map(activeValidatorsEntries, func(v types.Entry) missingValidatorsEntry { + Validators: utils.Map(activeValidatorsEntries, func(v *types.Entry) missingValidatorsEntry { link := reporter.Config.ExplorerConfig.GetValidatorLink(v.Validator) group, _, _ := reporter.Config.MissedBlocksGroups.GetGroup(v.SignatureInfo.GetNotSigned()) link.Text = fmt.Sprintf("%s %s", group.EmojiEnd, v.Validator.Moniker) diff --git a/pkg/reporters/discord/validators.go b/pkg/reporters/discord/validators.go index 1048028..b4d1aab 100644 --- a/pkg/reporters/discord/validators.go +++ b/pkg/reporters/discord/validators.go @@ -28,7 +28,7 @@ func (reporter *Reporter) GetValidatorsCommand() *Command { } validatorEntries := snapshot.Entries.ToSlice() - activeValidatorsEntries := utils.Filter(validatorEntries, func(v types.Entry) bool { + activeValidatorsEntries := utils.Filter(validatorEntries, func(v *types.Entry) bool { return v.IsActive }) @@ -41,7 +41,7 @@ func (reporter *Reporter) GetValidatorsCommand() *Command { render := missingValidatorsRender{ Config: reporter.Config, - Validators: utils.Map(activeValidatorsEntries, func(v types.Entry) missingValidatorsEntry { + Validators: utils.Map(activeValidatorsEntries, func(v *types.Entry) missingValidatorsEntry { link := reporter.Config.ExplorerConfig.GetValidatorLink(v.Validator) group, _, _ := reporter.Config.MissedBlocksGroups.GetGroup(v.SignatureInfo.GetNotSigned()) link.Text = fmt.Sprintf("%s %s", group.EmojiEnd, v.Validator.Moniker) diff --git a/pkg/reporters/telegram/missing.go b/pkg/reporters/telegram/missing.go index d9ad18d..7392448 100644 --- a/pkg/reporters/telegram/missing.go +++ b/pkg/reporters/telegram/missing.go @@ -28,7 +28,7 @@ func (reporter *Reporter) HandleMissingValidators(c tele.Context) error { } validatorEntries := snapshot.Entries.ToSlice() - activeValidatorsEntries := utils.Filter(validatorEntries, func(v types.Entry) bool { + activeValidatorsEntries := utils.Filter(validatorEntries, func(v *types.Entry) bool { if !v.IsActive { return false } @@ -46,7 +46,7 @@ func (reporter *Reporter) HandleMissingValidators(c tele.Context) error { render := missingValidatorsRender{ Config: reporter.Config, - Validators: utils.Map(activeValidatorsEntries, func(v types.Entry) missingValidatorsEntry { + Validators: utils.Map(activeValidatorsEntries, func(v *types.Entry) missingValidatorsEntry { link := reporter.Config.ExplorerConfig.GetValidatorLink(v.Validator) group, _, _ := reporter.Config.MissedBlocksGroups.GetGroup(v.SignatureInfo.GetNotSigned()) link.Text = fmt.Sprintf("%s %s", group.EmojiEnd, v.Validator.Moniker) diff --git a/pkg/reporters/telegram/validators.go b/pkg/reporters/telegram/validators.go index a943af8..b152ec7 100644 --- a/pkg/reporters/telegram/validators.go +++ b/pkg/reporters/telegram/validators.go @@ -28,7 +28,7 @@ func (reporter *Reporter) HandleListValidators(c tele.Context) error { } validatorEntries := snapshot.Entries.ToSlice() - activeValidatorsEntries := utils.Filter(validatorEntries, func(v types.Entry) bool { + activeValidatorsEntries := utils.Filter(validatorEntries, func(v *types.Entry) bool { return v.IsActive }) @@ -41,7 +41,7 @@ func (reporter *Reporter) HandleListValidators(c tele.Context) error { render := missingValidatorsRender{ Config: reporter.Config, - Validators: utils.Map(activeValidatorsEntries, func(v types.Entry) missingValidatorsEntry { + Validators: utils.Map(activeValidatorsEntries, func(v *types.Entry) missingValidatorsEntry { link := reporter.Config.ExplorerConfig.GetValidatorLink(v.Validator) group, _, _ := reporter.Config.MissedBlocksGroups.GetGroup(v.SignatureInfo.GetNotSigned()) link.Text = fmt.Sprintf("%s %s", group.EmojiEnd, v.Validator.Moniker) diff --git a/pkg/snapshot/manager_test.go b/pkg/snapshot/manager_test.go index b995f20..0112090 100644 --- a/pkg/snapshot/manager_test.go +++ b/pkg/snapshot/manager_test.go @@ -38,12 +38,12 @@ func TestManagerCommitNewSnapshot(t *testing.T) { manager := NewManager(*log, config, metricsManager) manager.CommitNewSnapshot(10, Snapshot{ - Entries: map[string]types.Entry{ + Entries: types.Entries{ "validator": {Validator: &types.Validator{}}, }, }) manager.CommitNewSnapshot(20, Snapshot{ - Entries: map[string]types.Entry{ + Entries: types.Entries{ "validator": {Validator: &types.Validator{}}, }, }) @@ -77,7 +77,7 @@ func TestManagerGetNewerSnapshot(t *testing.T) { assert.Nil(t, firstSnapshot, "Snapshot should not be presented!") manager.CommitNewSnapshot(20, Snapshot{ - Entries: map[string]types.Entry{ + Entries: types.Entries{ "validator": {Validator: &types.Validator{}}, }, }) diff --git a/pkg/snapshot/snapshot_test.go b/pkg/snapshot/snapshot_test.go index 6c1b780..b19d1c7 100644 --- a/pkg/snapshot/snapshot_test.go +++ b/pkg/snapshot/snapshot_test.go @@ -15,8 +15,8 @@ import ( func TestValidatorCreated(t *testing.T) { t.Parallel() - olderSnapshot := Snapshot{Entries: map[string]types.Entry{}} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{}} + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": {Validator: &types.Validator{}}, }} @@ -36,17 +36,17 @@ func TestValidatorGroupChanged(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 50}, }, }} @@ -69,17 +69,17 @@ func TestValidatorGroupChangedAnomaly(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 125}, }, }} @@ -99,13 +99,13 @@ func TestValidatorTombstoned(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { Validator: &types.Validator{SigningInfo: &types.SigningInfo{Tombstoned: false}}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { Validator: &types.Validator{SigningInfo: &types.SigningInfo{Tombstoned: true}}, SignatureInfo: types.SignatureInto{NotSigned: 0}, @@ -130,17 +130,17 @@ func TestValidatorJailed(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: false, - Validator: &types.Validator{Jailed: true, Status: 1}, + Validator: &types.Validator{Jailed: true}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} @@ -170,13 +170,13 @@ func TestValidatorUnjailed(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { Validator: &types.Validator{Jailed: true}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 0}, @@ -201,19 +201,19 @@ func TestValidatorJoinedSignatory(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, NeedsToSign: false, - Validator: &types.Validator{Status: 3}, + Validator: &types.Validator{}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, NeedsToSign: true, - Validator: &types.Validator{Status: 3}, + Validator: &types.Validator{}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} @@ -236,19 +236,19 @@ func TestValidatorLeftSignatory(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, NeedsToSign: true, - Validator: &types.Validator{Status: 3}, + Validator: &types.Validator{}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, NeedsToSign: false, - Validator: &types.Validator{Status: 3}, + Validator: &types.Validator{}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} @@ -271,17 +271,17 @@ func TestValidatorInactive(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: false, - Validator: &types.Validator{Jailed: false, Status: 1}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} @@ -296,10 +296,10 @@ func TestValidatorInactive(t *testing.T) { func TestValidatorChangedKey(t *testing.T) { t.Parallel() - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": {Validator: &types.Validator{ConsensusAddressValcons: "key1"}}, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": {Validator: &types.Validator{ConsensusAddressValcons: "key2"}}, }} @@ -312,10 +312,10 @@ func TestValidatorChangedKey(t *testing.T) { func TestValidatorChangedMoniker(t *testing.T) { t.Parallel() - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": {Validator: &types.Validator{Moniker: "moniker1"}}, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": {Validator: &types.Validator{Moniker: "moniker2"}}, }} @@ -328,10 +328,10 @@ func TestValidatorChangedMoniker(t *testing.T) { func TestValidatorChangedCommission(t *testing.T) { t.Parallel() - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": {Validator: &types.Validator{Commission: 0.01}}, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": {Validator: &types.Validator{Commission: 0.02}}, }} @@ -351,17 +351,17 @@ func TestValidatorActive(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: false, - Validator: &types.Validator{Jailed: false, Status: 1}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} @@ -383,15 +383,15 @@ func TestValidatorJailedAndChangedGroup(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { - Validator: &types.Validator{Jailed: true, Status: 3}, + Validator: &types.Validator{Jailed: true}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { - Validator: &types.Validator{Jailed: true, Status: 3}, + Validator: &types.Validator{Jailed: true}, SignatureInfo: types.SignatureInto{NotSigned: 50}, }, }} @@ -411,17 +411,16 @@ func TestTombstonedAndNoPreviousSigningInfo(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { - Validator: &types.Validator{Jailed: true, Status: 3}, + Validator: &types.Validator{Jailed: true}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { Validator: &types.Validator{ Jailed: true, - Status: 3, SigningInfo: &types.SigningInfo{Tombstoned: true}, }, SignatureInfo: types.SignatureInto{NotSigned: 50}, @@ -443,17 +442,17 @@ func TestNewMissedBlocksGroupNotPresent(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 150}, }, }} @@ -473,17 +472,17 @@ func TestOldMissedBlocksGroupNotPresent(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 150}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 0}, }, }} @@ -503,37 +502,37 @@ func TestSorting(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator1": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 25}, }, "validator2": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 25}, }, "validator3": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3, SigningInfo: &types.SigningInfo{Tombstoned: false}}, + Validator: &types.Validator{Jailed: false, SigningInfo: &types.SigningInfo{Tombstoned: false}}, SignatureInfo: types.SignatureInto{NotSigned: 25}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ "validator1": { IsActive: true, - Validator: &types.Validator{Jailed: true, Status: 3}, + Validator: &types.Validator{Jailed: true}, SignatureInfo: types.SignatureInto{NotSigned: 25}, }, "validator2": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3}, + Validator: &types.Validator{Jailed: false}, SignatureInfo: types.SignatureInto{NotSigned: 75}, }, "validator3": { IsActive: true, - Validator: &types.Validator{Jailed: false, Status: 3, SigningInfo: &types.SigningInfo{Tombstoned: true}}, + Validator: &types.Validator{Jailed: false, SigningInfo: &types.SigningInfo{Tombstoned: true}}, SignatureInfo: types.SignatureInto{NotSigned: 25}, }, }} @@ -558,51 +557,51 @@ func TestSortingMissedBlocksGroups(t *testing.T) { }, } - olderSnapshot := Snapshot{Entries: map[string]types.Entry{ + olderSnapshot := Snapshot{Entries: types.Entries{ "validator1": { IsActive: true, - Validator: &types.Validator{OperatorAddress: "validator1", Status: 3}, + Validator: &types.Validator{OperatorAddress: "validator1"}, SignatureInfo: types.SignatureInto{NotSigned: 25}, }, "validator2": { IsActive: true, - Validator: &types.Validator{OperatorAddress: "validator2", Status: 3}, + Validator: &types.Validator{OperatorAddress: "validator2"}, SignatureInfo: types.SignatureInto{NotSigned: 75}, }, "validator3": { IsActive: true, - Validator: &types.Validator{OperatorAddress: "validator3", Status: 3}, + Validator: &types.Validator{OperatorAddress: "validator3"}, SignatureInfo: types.SignatureInto{NotSigned: 125}, }, "validator4": { IsActive: true, - Validator: &types.Validator{OperatorAddress: "validator4", Status: 3}, + Validator: &types.Validator{OperatorAddress: "validator4"}, SignatureInfo: types.SignatureInto{NotSigned: 75}, }, }} - newerSnapshot := Snapshot{Entries: map[string]types.Entry{ + newerSnapshot := Snapshot{Entries: types.Entries{ // skipping blocks: 25 -> 75 "validator1": { IsActive: true, - Validator: &types.Validator{OperatorAddress: "validator1", Status: 3}, + Validator: &types.Validator{OperatorAddress: "validator1"}, SignatureInfo: types.SignatureInto{NotSigned: 75}, }, // skipping blocks: 75 -> 125 "validator2": { IsActive: true, - Validator: &types.Validator{OperatorAddress: "validator2", Status: 3}, + Validator: &types.Validator{OperatorAddress: "validator2"}, SignatureInfo: types.SignatureInto{NotSigned: 125}, }, // recovering: 125 -> 75 "validator3": { IsActive: true, - Validator: &types.Validator{OperatorAddress: "validator3", Status: 3}, + Validator: &types.Validator{OperatorAddress: "validator3"}, SignatureInfo: types.SignatureInto{NotSigned: 75}, }, // recovering: 75 -> 25 "validator4": { IsActive: true, - Validator: &types.Validator{OperatorAddress: "validator4", Status: 3}, + Validator: &types.Validator{OperatorAddress: "validator4"}, SignatureInfo: types.SignatureInto{NotSigned: 25}, }, }} diff --git a/pkg/state/manager.go b/pkg/state/manager.go index fd73ace..a791da1 100644 --- a/pkg/state/manager.go +++ b/pkg/state/manager.go @@ -156,7 +156,7 @@ func (m *Manager) GetSnapshot() (snapshotPkg.Snapshot, error) { return snapshotPkg.Snapshot{}, err } - entries[validator.OperatorAddress] = types.Entry{ + entries[validator.OperatorAddress] = &types.Entry{ IsActive: isActiveAtLastBlock, Validator: validator, SignatureInfo: signatureInfo, @@ -171,6 +171,8 @@ func (m *Manager) GetSnapshot() (snapshotPkg.Snapshot, error) { } } + entries.SetVotingPowerPercent() + return snapshotPkg.Snapshot{Entries: entries}, nil } diff --git a/pkg/types/entry.go b/pkg/types/entry.go index 5835f5a..a8238cd 100644 --- a/pkg/types/entry.go +++ b/pkg/types/entry.go @@ -11,12 +11,16 @@ type Entry struct { NeedsToSign bool Validator *Validator SignatureInfo SignatureInto + + VotingPowerPercent float64 + CumulativeVotingPowerPercent float64 + Rank int } -type Entries map[string]Entry +type Entries map[string]*Entry -func (e Entries) ToSlice() []Entry { - entries := make([]Entry, len(e)) +func (e Entries) ToSlice() []*Entry { + entries := make([]*Entry, len(e)) index := 0 for _, entry := range e { @@ -27,8 +31,8 @@ func (e Entries) ToSlice() []Entry { return entries } -func (e Entries) ByValidatorAddresses(addresses []string) []Entry { - entries := make([]Entry, 0) +func (e Entries) ByValidatorAddresses(addresses []string) []*Entry { + entries := make([]*Entry, 0) for _, entry := range e { if utils.Contains(addresses, entry.Validator.OperatorAddress) { @@ -39,8 +43,8 @@ func (e Entries) ByValidatorAddresses(addresses []string) []Entry { return entries } -func (e Entries) GetActive() []Entry { - activeValidators := make([]Entry, 0) +func (e Entries) GetActive() []*Entry { + activeValidators := make([]*Entry, 0) for _, entry := range e { if entry.IsActive { activeValidators = append(activeValidators, entry) @@ -89,3 +93,27 @@ func (e Entries) GetSoftOutOutThreshold(softOptOut float64) (*big.Float, int) { // should've never reached here return sortedEntries[0].Validator.VotingPower, len(sortedEntries) } + +func (e Entries) SetVotingPowerPercent() { + totalVP := e.GetTotalVotingPower() + + activeAndSortedEntries := e.GetActive() + + // sorting by voting power desc + sort.Slice(activeAndSortedEntries, func(first, second int) bool { + return activeAndSortedEntries[first].Validator.VotingPower.Cmp(activeAndSortedEntries[second].Validator.VotingPower) > 0 + }) + + var cumulativeVotingPowerPercent float64 = 0 + for index, sortedEntry := range activeAndSortedEntries { + percent, _ := new(big.Float).Quo(sortedEntry.Validator.VotingPower, totalVP).Float64() + + entry := e[sortedEntry.Validator.OperatorAddress] + + entry.VotingPowerPercent = percent + entry.Rank = index + 1 + + cumulativeVotingPowerPercent += percent + entry.CumulativeVotingPowerPercent = cumulativeVotingPowerPercent + } +} diff --git a/pkg/types/entry_test.go b/pkg/types/entry_test.go index 770900b..9d9b1b3 100644 --- a/pkg/types/entry_test.go +++ b/pkg/types/entry_test.go @@ -11,8 +11,8 @@ func TestEntriesToSlice(t *testing.T) { t.Parallel() entries := Entries{ - "validator": Entry{ - Validator: &Validator{Moniker: "test", Jailed: false, Status: 1}, + "validator": &Entry{ + Validator: &Validator{Moniker: "test", Jailed: false}, SignatureInfo: SignatureInto{NotSigned: 0}, }, } @@ -29,11 +29,11 @@ func TestEntriesGetActive(t *testing.T) { entries := Entries{ "firstaddr": { IsActive: true, - Validator: &Validator{Moniker: "first", OperatorAddress: "firstaddr", Status: 3}, + Validator: &Validator{Moniker: "first", OperatorAddress: "firstaddr"}, }, "secondaddr": { IsActive: false, - Validator: &Validator{Moniker: "second", OperatorAddress: "secondaddr", Status: 1}, + Validator: &Validator{Moniker: "second", OperatorAddress: "secondaddr"}, }, } @@ -47,15 +47,15 @@ func TestValidatorsGetTotalVotingPower(t *testing.T) { entries := Entries{ "firstaddr": { IsActive: true, - Validator: &Validator{Moniker: "first", OperatorAddress: "firstaddr", Status: 3, VotingPower: big.NewFloat(1)}, + Validator: &Validator{Moniker: "first", OperatorAddress: "firstaddr", VotingPower: big.NewFloat(1)}, }, "secondaddr": { IsActive: true, - Validator: &Validator{Moniker: "second", OperatorAddress: "secondaddr", Status: 3, VotingPower: big.NewFloat(2)}, + Validator: &Validator{Moniker: "second", OperatorAddress: "secondaddr", VotingPower: big.NewFloat(2)}, }, "thirdaddr": { IsActive: false, - Validator: &Validator{Moniker: "third", OperatorAddress: "thirdaddr", Status: 1, VotingPower: big.NewFloat(3)}, + Validator: &Validator{Moniker: "third", OperatorAddress: "thirdaddr", VotingPower: big.NewFloat(3)}, }, } @@ -74,7 +74,6 @@ func TestEntriesGetSoftOptOutThresholdAchievable(t *testing.T) { Validator: &Validator{ Moniker: "first", OperatorAddress: "firstaddr", - Status: 3, VotingPower: big.NewFloat(80), }, }, @@ -83,7 +82,6 @@ func TestEntriesGetSoftOptOutThresholdAchievable(t *testing.T) { Validator: &Validator{ Moniker: "second", OperatorAddress: "secondaddr", - Status: 3, VotingPower: big.NewFloat(15), }, }, @@ -92,7 +90,6 @@ func TestEntriesGetSoftOptOutThresholdAchievable(t *testing.T) { Validator: &Validator{ Moniker: "third", OperatorAddress: "thirdaddr", - Status: 1, VotingPower: big.NewFloat(2), }, }, @@ -101,7 +98,6 @@ func TestEntriesGetSoftOptOutThresholdAchievable(t *testing.T) { Validator: &Validator{ Moniker: "fourth", OperatorAddress: "fourthaddr", - Status: 3, VotingPower: big.NewFloat(5), }, }, @@ -123,7 +119,6 @@ func TestEntriesGetSoftOptOutThresholdNotAchievable(t *testing.T) { Validator: &Validator{ Moniker: "first", OperatorAddress: "firstaddr", - Status: 3, VotingPower: big.NewFloat(80), }, }, @@ -132,7 +127,6 @@ func TestEntriesGetSoftOptOutThresholdNotAchievable(t *testing.T) { Validator: &Validator{ Moniker: "second", OperatorAddress: "secondaddr", - Status: 3, VotingPower: big.NewFloat(15), }, }, @@ -141,7 +135,6 @@ func TestEntriesGetSoftOptOutThresholdNotAchievable(t *testing.T) { Validator: &Validator{ Moniker: "third", OperatorAddress: "thirdaddr", - Status: 1, VotingPower: big.NewFloat(2), }, }, @@ -150,7 +143,6 @@ func TestEntriesGetSoftOptOutThresholdNotAchievable(t *testing.T) { Validator: &Validator{ Moniker: "fourth", OperatorAddress: "fourthaddr", - Status: 3, VotingPower: big.NewFloat(5), }, }, @@ -170,7 +162,6 @@ func TestEntriesGetSoftOptOutThresholdEmpty(t *testing.T) { Validator: &Validator{ Moniker: "third", OperatorAddress: "thirdaddr", - Status: 1, VotingPower: big.NewFloat(2), }, }, @@ -180,3 +171,43 @@ func TestEntriesGetSoftOptOutThresholdEmpty(t *testing.T) { assert.Equal(t, big.NewFloat(0), threshold) assert.Equal(t, 0, count) } + +func TestEntriesSetTotalVotingPower(t *testing.T) { + t.Parallel() + + entries := Entries{ + "firstaddr": { + IsActive: true, + Validator: &Validator{ + Moniker: "first", + OperatorAddress: "firstaddr", + VotingPower: big.NewFloat(1), + }, + }, + "secondaddr": { + IsActive: true, + Validator: &Validator{ + Moniker: "second", + OperatorAddress: "secondaddr", + VotingPower: big.NewFloat(3), + }, + }, + "thirdaddr": { + IsActive: false, + Validator: &Validator{ + Moniker: "third", + OperatorAddress: "thirdaddr", + VotingPower: big.NewFloat(2), + }, + }, + } + + entries.SetVotingPowerPercent() + assert.Len(t, entries, 3) + assert.InDelta(t, 0.25, entries["firstaddr"].VotingPowerPercent, 0.001) + assert.InDelta(t, 0.75, entries["secondaddr"].VotingPowerPercent, 0.001) + assert.Equal(t, 2, entries["firstaddr"].Rank) + assert.Equal(t, 1, entries["secondaddr"].Rank) + assert.InDelta(t, float64(1), entries["firstaddr"].CumulativeVotingPowerPercent, 0.001) + assert.InDelta(t, 0.75, entries["secondaddr"].CumulativeVotingPowerPercent, 0.001) +} diff --git a/pkg/types/types.go b/pkg/types/types.go index 835dbc3..489ea2d 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -1,10 +1,5 @@ package types -import ( - "math/big" - "sort" -) - type WebsocketEmittable interface { Hash() string } @@ -28,50 +23,6 @@ func (validators Validators) ToMap() ValidatorsMap { return validatorsMap } -func (validators Validators) GetActive() Validators { - activeValidators := make(Validators, 0) - for _, validator := range validators { - if validator.Active() { - activeValidators = append(activeValidators, validator) - } - } - - return activeValidators -} - -func (validators Validators) GetTotalVotingPower() *big.Float { - sum := big.NewFloat(0) - - for _, validator := range validators { - if validator.Active() { - sum.Add(sum, validator.VotingPower) - } - } - - return sum -} - -func (validators Validators) SetVotingPowerPercent() { - totalVP := validators.GetTotalVotingPower() - - activeAndSortedValidators := validators.GetActive() - - // sorting by voting power desc - sort.Slice(activeAndSortedValidators, func(first, second int) bool { - return activeAndSortedValidators[first].VotingPower.Cmp(activeAndSortedValidators[second].VotingPower) > 0 - }) - - var cumulativeVotingPowerPercent float64 = 0 - for index, validator := range activeAndSortedValidators { - percent, _ := new(big.Float).Quo(validator.VotingPower, totalVP).Float64() - validator.VotingPowerPercent = percent - validator.Rank = index + 1 - - cumulativeVotingPowerPercent += percent - validator.CumulativeVotingPowerPercent = cumulativeVotingPowerPercent - } -} - func (validatorsMap ValidatorsMap) ToSlice() Validators { validators := make(Validators, len(validatorsMap)) index := 0 diff --git a/pkg/types/types_test.go b/pkg/types/types_test.go index b375404..0edf67a 100644 --- a/pkg/types/types_test.go +++ b/pkg/types/types_test.go @@ -1,7 +1,6 @@ package types import ( - "math/big" "testing" "github.com/stretchr/testify/assert" @@ -40,22 +39,3 @@ func TestValidatorsToSlice(t *testing.T) { assert.Contains(t, monikers, "first", "Validator mismatch!") assert.Contains(t, monikers, "second", "Validator mismatch!") } - -func TestValidatorsSetTotalVotingPower(t *testing.T) { - t.Parallel() - - validators := Validators{ - {Moniker: "first", OperatorAddress: "firstaddr", Status: 3, VotingPower: big.NewFloat(1)}, - {Moniker: "second", OperatorAddress: "secondaddr", Status: 3, VotingPower: big.NewFloat(3)}, - {Moniker: "third", OperatorAddress: "thirdaddr", Status: 1, VotingPower: big.NewFloat(2)}, - } - - validators.SetVotingPowerPercent() - assert.Len(t, validators, 3) - assert.InDelta(t, 0.25, validators[0].VotingPowerPercent, 0.001) - assert.InDelta(t, 0.75, validators[1].VotingPowerPercent, 0.001) - assert.Equal(t, 2, validators[0].Rank) - assert.Equal(t, 1, validators[1].Rank) - assert.InDelta(t, float64(1), validators[0].CumulativeVotingPowerPercent, 0.001) - assert.InDelta(t, 0.75, validators[1].CumulativeVotingPowerPercent, 0.001) -} diff --git a/pkg/types/validator.go b/pkg/types/validator.go index 39ca616..5fe0cda 100644 --- a/pkg/types/validator.go +++ b/pkg/types/validator.go @@ -1,7 +1,6 @@ package types import ( - "main/pkg/constants" "math/big" ) @@ -20,7 +19,6 @@ type Validator struct { ConsensusAddressValcons string OperatorAddress string Commission float64 - Status int32 Jailed bool SigningInfo *SigningInfo @@ -29,7 +27,3 @@ type Validator struct { CumulativeVotingPowerPercent float64 Rank int } - -func (v *Validator) Active() bool { - return v.Status == constants.ValidatorBonded -}