Skip to content

Commit

Permalink
fix(streamer): don't distribute abstained part of sponsored distribut…
Browse files Browse the repository at this point in the history
…ion (#1097)
  • Loading branch information
keruch authored Aug 15, 2024
1 parent ce6b05c commit f1df3d7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 17 deletions.
23 changes: 6 additions & 17 deletions x/streamer/types/distr_info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 @@ -36,29 +35,19 @@ func (r DistrRecord) ValidateBasic() error {
return nil
}

var hundred = math.NewInt(100)

// DistrInfoFromDistribution converts sponsorship distribution to the DistrInfo type and performs a
// basic validation for DistrInfo.Records. 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.
func DistrInfoFromDistribution(d sponsorshiptypes.Distribution) *DistrInfo {
totalWeight := math.ZeroInt()
records := make([]DistrRecord, 0, len(d.Gauges))
for _, g := range d.Gauges {
// d.VotingPower is always > 0 to the Distribution type contract
weight := g.Power.Mul(hundred).Quo(d.VotingPower)

record := DistrRecord{
records = append(records, DistrRecord{
GaugeId: g.GaugeId,
Weight: weight,
}

totalWeight = totalWeight.Add(weight)
records = append(records, record)
Weight: g.Power,
})
}

return &DistrInfo{
TotalWeight: totalWeight,
TotalWeight: d.VotingPower,
Records: records,
}
}
116 changes: 116 additions & 0 deletions x/streamer/types/distr_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package types_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"

sponsorshiptypes "github.com/dymensionxyz/dymension/v3/x/sponsorship/types"
"github.com/dymensionxyz/dymension/v3/x/streamer/types"
)

func TestDistrInfoFromDistribution(t *testing.T) {
testCases := []struct {
name string
distr sponsorshiptypes.Distribution
expDistr *types.DistrInfo
}{
{
name: "Empty distribution",
distr: sponsorshiptypes.NewDistribution(),
expDistr: &types.DistrInfo{
TotalWeight: sdk.NewInt(0),
Records: []types.DistrRecord{},
},
},
{
name: "Distribution with single gauge",
distr: sponsorshiptypes.Distribution{
VotingPower: sdk.NewInt(10),
Gauges: []sponsorshiptypes.Gauge{
{
GaugeId: 1,
Power: sdk.NewInt(10),
},
},
},
expDistr: &types.DistrInfo{
TotalWeight: sdk.NewInt(10),
Records: []types.DistrRecord{
{
GaugeId: 1,
Weight: sdk.NewInt(10),
},
},
},
},
{
name: "Distribution with multiple gauges",
distr: sponsorshiptypes.Distribution{
VotingPower: sdk.NewInt(30),
Gauges: []sponsorshiptypes.Gauge{
{
GaugeId: 1,
Power: sdk.NewInt(10),
},
{
GaugeId: 2,
Power: sdk.NewInt(20),
},
},
},
expDistr: &types.DistrInfo{
TotalWeight: sdk.NewInt(30),
Records: []types.DistrRecord{
{
GaugeId: 1,
Weight: sdk.NewInt(10),
},
{
GaugeId: 2,
Weight: sdk.NewInt(20),
},
},
},
},
{
name: "Distribution with abstained gauge",
distr: sponsorshiptypes.Distribution{
VotingPower: sdk.NewInt(100),
Gauges: []sponsorshiptypes.Gauge{
// 30 is abstained
{
GaugeId: 1,
Power: sdk.NewInt(50),
},
{
GaugeId: 2,
Power: sdk.NewInt(20),
},
},
},
expDistr: &types.DistrInfo{
TotalWeight: sdk.NewInt(100),
Records: []types.DistrRecord{
// 30 is abstained
{
GaugeId: 1,
Weight: sdk.NewInt(50),
},
{
GaugeId: 2,
Weight: sdk.NewInt(20),
},
},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
distr := types.DistrInfoFromDistribution(tc.distr)
assert.Equal(t, tc.expDistr, distr)
})
}
}

0 comments on commit f1df3d7

Please sign in to comment.