Skip to content

Commit

Permalink
feat(sponsorship): updated the scale system for gauge weights (#1190)
Browse files Browse the repository at this point in the history
Co-authored-by: Omri <[email protected]>
  • Loading branch information
keruch and omritoptix authored Sep 5, 2024
1 parent b50f568 commit 094de0c
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 151 deletions.
9 changes: 6 additions & 3 deletions proto/dymensionxyz/dymension/sponsorship/sponsorship.proto
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ message Vote {
message GaugeWeight {
// GaugeID is the ID of the gauge.
uint64 gauge_id = 1;
// Weight is a portion of the voting power that is allocated for the given
// gauge. The value must fall between Params.MinAllocationWeight and 100,
// inclusive.
// Weight is a portion of the voting power that is allocated for the given gauge.
// The value is measured in percentages and must fall between 1 and 100 * 10^18,
// inclusive. The base unit is 10^-18%, so
// * 1 --> 10^-18%
// * 10^18 --> 1%
// * 100 * 10^18 --> 100%.
string weight = 2 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"
Expand Down
12 changes: 6 additions & 6 deletions x/sponsorship/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@ func ParseGaugeWeights(inputWeights string) ([]types.GaugeWeight, error) {
return nil, fmt.Errorf("invalid gauge ID '%s': %w", idValue[0], err)
}

weight, err := strconv.Atoi(idValue[1])
if err != nil {
return nil, fmt.Errorf("invalid gauge weight '%s': %w", idValue[1], err)
weight, ok := math.NewIntFromString(idValue[1])
if !ok {
return nil, fmt.Errorf("invalid gauge weight '%s'", idValue[1])
}

if weight < 0 || weight > 100 {
return nil, fmt.Errorf("weight must be between 0 and 100, got %d", weight)
if weight.LT(types.MinAllocationWeight) || weight.GT(types.MaxAllocationWeight) {
return nil, fmt.Errorf("weight must be between 1 and 100 * 10^18, got %s", weight)
}

weights = append(weights, types.GaugeWeight{
GaugeId: gaugeID,
Weight: math.NewInt(int64(weight)),
Weight: weight,
})
}

Expand Down
37 changes: 22 additions & 15 deletions x/sponsorship/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,60 @@ func TestParseGaugeWeights(t *testing.T) {
}{
{
name: "Valid input",
input: "15=60,10=30,12=10",
input: "15=60000000000000000000,10=3000,12=10000",
expected: []types.GaugeWeight{
{GaugeId: 15, Weight: math.NewInt(60)},
{GaugeId: 10, Weight: math.NewInt(30)},
{GaugeId: 12, Weight: math.NewInt(10)},
{GaugeId: 15, Weight: types.DYM.MulRaw(60)},
{GaugeId: 10, Weight: math.NewInt(3000)},
{GaugeId: 12, Weight: math.NewInt(10000)},
},
expectError: false,
errorContains: "",
},
{
name: "Valid, sum of weighs < 100",
input: "15=30,10=30,12=39",
input: "15=30000000000000000000,10=30000000000000000000,12=39000000000000000000",
expected: []types.GaugeWeight{
{GaugeId: 15, Weight: math.NewInt(30)},
{GaugeId: 10, Weight: math.NewInt(30)},
{GaugeId: 12, Weight: math.NewInt(39)},
{GaugeId: 15, Weight: types.DYM.MulRaw(30)},
{GaugeId: 10, Weight: types.DYM.MulRaw(30)},
{GaugeId: 12, Weight: types.DYM.MulRaw(39)},
},
expectError: false,
errorContains: "invalid gauge weights",
errorContains: "",
},
{
name: "Invalid input format",
input: "15,10=70,12=10",
input: "15,10=70000000000000000000,12=10000000000000000000",
expected: nil,
expectError: true,
errorContains: "invalid gauge weight format",
},
{
name: "Weight > 100",
input: "15=101,10=70,12=10",
input: "15=101000000000000000000,10=70000000000000000000,12=10000000000000000000",
expected: nil,
expectError: true,
errorContains: "weight must be between 0 and 100",
errorContains: "weight must be between 1 and 100 * 10^18, got 101000000000000000000",
},
{
name: "Weight < 0",
input: "15=-10,10=70,12=10",
expected: nil,
expectError: true,
errorContains: "weight must be between 0 and 100",
errorContains: "weight must be between 1 and 100 * 10^18, got -10",
},
{
name: "Sum of weighs > 100",
input: "15=30,10=30,12=41",
input: "15=30000000000000000000,10=30000000000000000000,12=41000000000000000000",
expected: nil,
expectError: true,
errorContains: "total weight must be less than 100 * 10^18, got 101000000000000000000",
},
{
name: "Zero weight",
input: "15=0,10=30000000000000000000,12=41000000000000000000",
expected: nil,
expectError: true,
errorContains: "invalid gauge weights",
errorContains: "weight must be between 1 and 100 * 10^18, got 0",
},
{
name: "Empty input",
Expand Down
4 changes: 2 additions & 2 deletions x/sponsorship/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *KeeperTestSuite) TestGenesis() {
Vote: types.Vote{
VotingPower: math.NewInt(600),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(100)},
{GaugeId: 1, Weight: types.DYM.MulRaw(100)},
},
},
Validators: []types.ValidatorVotingPower{
Expand All @@ -45,7 +45,7 @@ func (s *KeeperTestSuite) TestGenesis() {
Vote: types.Vote{
VotingPower: math.NewInt(400),
Weights: []types.GaugeWeight{
{GaugeId: 2, Weight: math.NewInt(100)},
{GaugeId: 2, Weight: types.DYM.MulRaw(100)},
},
},
Validators: []types.ValidatorVotingPower{
Expand Down
44 changes: 22 additions & 22 deletions x/sponsorship/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand Down Expand Up @@ -63,8 +63,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand Down Expand Up @@ -97,8 +97,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand Down Expand Up @@ -134,8 +134,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand Down Expand Up @@ -168,8 +168,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand Down Expand Up @@ -201,8 +201,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand Down Expand Up @@ -231,8 +231,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand Down Expand Up @@ -267,8 +267,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand All @@ -295,8 +295,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand Down Expand Up @@ -329,8 +329,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand Down Expand Up @@ -366,8 +366,8 @@ func (s *KeeperTestSuite) TestHooks() {
s.Vote(types.MsgVote{
Voter: del.GetDelegatorAddr().String(),
Weights: []types.GaugeWeight{
{GaugeId: 1, Weight: math.NewInt(20)},
{GaugeId: 2, Weight: math.NewInt(50)},
{GaugeId: 1, Weight: types.DYM.MulRaw(20)},
{GaugeId: 2, Weight: types.DYM.MulRaw(50)},
},
})

Expand Down
15 changes: 15 additions & 0 deletions x/sponsorship/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func (s *KeeperTestSuite) SetupTest() {
s.Ctx = ctx
s.queryClient = queryClient
s.msgServer = msgServer

s.SetDefaultTestParams()
}

func (s *KeeperTestSuite) CreateGauge() uint64 {
Expand Down Expand Up @@ -288,3 +290,16 @@ func (s *KeeperTestSuite) AssertDelegatorValidator(delAddr sdk.AccAddress, valAd
s.Require().NoError(err)
s.Require().Equal(expectedPower, vp)
}

// SetDefaultTestParams sets module params with MinVotingPower = 1 for convenience.
func (s *KeeperTestSuite) SetDefaultTestParams() {
err := s.App.SponsorshipKeeper.SetParams(s.Ctx, DefaultTestParams())
s.Require().NoError(err)
}

// DefaultTestParams returns module params with MinVotingPower = 1 for convenience.
func DefaultTestParams() types.Params {
params := types.DefaultParams()
params.MinVotingPower = math.NewInt(1)
return params
}
3 changes: 1 addition & 2 deletions x/sponsorship/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper_test

import (
"cosmossdk.io/math"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
Expand Down Expand Up @@ -46,7 +45,7 @@ func (s *KeeperTestSuite) TestUpdateParams() {
msg: types.MsgUpdateParams{
Authority: authority,
NewParams: types.Params{
MinAllocationWeight: math.NewInt(101), // > 100%
MinAllocationWeight: types.DYM.MulRaw(101), // > 100%
MinVotingPower: types.DefaultMinVotingPower,
},
},
Expand Down
4 changes: 2 additions & 2 deletions x/sponsorship/keeper/votes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (k Keeper) Vote(ctx sdk.Context, voter sdk.AccAddress, weights []types.Gaug

// Validate that the user has min voting power
if vpBreakdown.TotalPower.LT(params.MinVotingPower) {
return types.Vote{}, types.Distribution{}, fmt.Errorf("voting power '%d' is less than min voting power expected '%d'", vpBreakdown.TotalPower.Int64(), params.MinVotingPower.Int64())
return types.Vote{}, types.Distribution{}, fmt.Errorf("voting power '%s' is less than min voting power expected '%s'", vpBreakdown.TotalPower, params.MinVotingPower)
}

// Apply the vote weights to the power -> get a distribution update in absolute values
Expand Down Expand Up @@ -111,7 +111,7 @@ func (k Keeper) revokeVote(ctx sdk.Context, voter sdk.AccAddress, vote types.Vot
func (k Keeper) validateWeights(ctx sdk.Context, weights []types.GaugeWeight, minAllocationWeight math.Int) error {
for _, weight := range weights {
if weight.Weight.LT(minAllocationWeight) {
return fmt.Errorf("gauge weight '%d' is less than min allocation weight '%d'", weight.Weight.Int64(), minAllocationWeight.Int64())
return fmt.Errorf("gauge weight '%s' is less than min allocation weight '%s'", weight.Weight, minAllocationWeight)
}

gauge, err := k.incentivesKeeper.GetGaugeByID(ctx, weight.GaugeId)
Expand Down
Loading

0 comments on commit 094de0c

Please sign in to comment.