Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert(streamer): don't distribute abstained part of sponsored distri… #1117

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions x/streamer/types/distr_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

sponsorshiptypes "github.com/dymensionxyz/dymension/v3/x/sponsorship/types"
Expand Down Expand Up @@ -35,19 +36,38 @@ func (r DistrRecord) ValidateBasic() error {
return nil
}

// DistrInfoFromDistribution converts sponsorship distribution to the DistrInfo type.
// Returning an empty DistrInfo (with zero DistrInfo.TotalWeight) is a valid scenario.
// DistrInfoFromDistribution converts sponsorship distribution to the DistrInfo type. Returning an empty
// DistrInfo (with zero DistrInfo.TotalWeight) is a valid scenario. Note that some part of the distribution
// might be abstained. In that case, the sum of gauge powers would be less than the total distribution weight.
//
// Example! Let's say we have the following distribution in the sponsorship:
//
// Total: 100
// Gauge1: 30%
// Gauge2: 50%
// Abstained: 20%
//
// We want to distribute 100 DYM according to this distribution. Since 20% is abstained, we will normalize the figures
// for Gauge1 and Gauge2. The total "active" voting power is 80%, which comes from 30% (Gauge1) + 50% (Gauge2).
//
// Gauge1: 30% / 80% = 37.5% (in the new distribution)
// Gauge2: 50% / 80% = 62.5% (in the new distribution)
//
// So, Gauge1 gets 37.5 DYM, and Gauge2 gets 62.5 DYM.
func DistrInfoFromDistribution(d sponsorshiptypes.Distribution) *DistrInfo {
totalWeight := math.ZeroInt()

records := make([]DistrRecord, 0, len(d.Gauges))
for _, g := range d.Gauges {
records = append(records, DistrRecord{
GaugeId: g.GaugeId,
Weight: g.Power,
})
totalWeight = totalWeight.Add(g.Power)
}

return &DistrInfo{
TotalWeight: d.VotingPower,
TotalWeight: totalWeight,
Records: records,
}
}
Comment on lines 57 to 73
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very subtle so IMO it would be a good idea to explain the precise intended behavior in more detail the docstring. Especially since it has changed couple times

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

13 changes: 12 additions & 1 deletion x/streamer/types/distr_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ func TestDistrInfoFromDistribution(t *testing.T) {
},
},
},
{
name: "Distribution with empty gauges",
distr: sponsorshiptypes.Distribution{
VotingPower: sdk.NewInt(30),
Gauges: []sponsorshiptypes.Gauge{},
},
expDistr: &types.DistrInfo{
TotalWeight: sdk.ZeroInt(),
Records: []types.DistrRecord{},
},
},
{
name: "Distribution with abstained gauge",
distr: sponsorshiptypes.Distribution{
Expand All @@ -91,7 +102,7 @@ func TestDistrInfoFromDistribution(t *testing.T) {
},
},
expDistr: &types.DistrInfo{
TotalWeight: sdk.NewInt(100),
TotalWeight: sdk.NewInt(70),
Records: []types.DistrRecord{
// 30 is abstained
{
Expand Down
Loading