Skip to content

Commit

Permalink
Fix the way validator's power percentage was calculated #374
Browse files Browse the repository at this point in the history
  • Loading branch information
Vizualni authored Aug 1, 2022
1 parent 76430c9 commit 078bac2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions x/valset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ func (k Keeper) isNewSnapshotWorthy(currentSnapshot, newSnapshot *types.Snapshot
// could own 60% of the network. And all other validators stayed the
// (relatively) same.
for i := 0; i < len(sortedCurrent); i++ {
percentageCurrent := currentSnapshot.TotalShares.ToDec().QuoInt(sortedCurrent[i].ShareCount)
percentageNow := newSnapshot.TotalShares.ToDec().QuoInt(sortedNew[i].ShareCount)
percentageCurrent := sortedCurrent[i].ShareCount.ToDec().QuoInt(currentSnapshot.TotalShares)
percentageNow := sortedNew[i].ShareCount.ToDec().QuoInt(newSnapshot.TotalShares)

if percentageCurrent.Sub(percentageNow).Abs().MustFloat64() >= 0.01 {
return true
Expand Down
32 changes: 32 additions & 0 deletions x/valset/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,38 @@ func TestIsNewSnapshotWorthy(t *testing.T) {
},
},
},
{
expRes: false,
name: "if the powers are still the same then it's not worthy",
curr: &types.Snapshot{
TotalShares: sdk.NewInt(100),
Validators: []types.Validator{
{Address: sdk.ValAddress("123"), ShareCount: sdk.NewInt(20)},
},
},
neww: &types.Snapshot{
TotalShares: sdk.NewInt(100),
Validators: []types.Validator{
{Address: sdk.ValAddress("123"), ShareCount: sdk.NewInt(20)},
},
},
},
{
expRes: false,
name: "if the powers have change for less than 1%, then it's not worthy",
curr: &types.Snapshot{
TotalShares: sdk.NewInt(1000),
Validators: []types.Validator{
{Address: sdk.ValAddress("123"), ShareCount: sdk.NewInt(20)},
},
},
neww: &types.Snapshot{
TotalShares: sdk.NewInt(1000),
Validators: []types.Validator{
{Address: sdk.ValAddress("123"), ShareCount: sdk.NewInt(21)},
},
},
},
}

for _, tt := range testcases {
Expand Down

0 comments on commit 078bac2

Please sign in to comment.