Skip to content

Commit

Permalink
fix(sponsorship): removed redundant ceiling while calculating staking…
Browse files Browse the repository at this point in the history
… power (#1515)
  • Loading branch information
keruch authored Nov 20, 2024
1 parent f8d71e7 commit 294d8c4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions x/sponsorship/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (h Hooks) afterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress,
}

// Calculate a staking voting power
stakingVP := v.TokensFromShares(d.GetShares()).Ceil().TruncateInt()
stakingVP := v.TokensFromShares(d.GetShares()).TruncateInt()

// Get the current voting power saved in x/sponsorship. If the VP is not found, then we yet don't
// have a relevant record. This is a valid case when the VP is zero.
Expand Down Expand Up @@ -189,7 +189,7 @@ func (h Hooks) processHook(
distribution: distr,
votePruned: true,
vpDiff: powerDiff,
newTotal: vote.VotingPower,
newTotal: newTotalVP,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions x/sponsorship/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *KeeperTestSuite) Vote(vote types.MsgVote) {
func (s *KeeperTestSuite) CreateValidator() stakingtypes.ValidatorI {
s.T().Helper()

valAddrs := apptesting.AddTestAddrs(s.App, s.Ctx, 1, sdk.NewInt(1_000_000_000))
valAddrs := apptesting.AddTestAddrs(s.App, s.Ctx, 1, types.DYM.MulRaw(1_000))

// Build MsgCreateValidator
valAddr := sdk.ValAddress(valAddrs[0].Bytes())
Expand Down Expand Up @@ -178,7 +178,7 @@ func (s *KeeperTestSuite) CreateValidatorWithAddress(acc sdk.AccAddress, balance
func (s *KeeperTestSuite) CreateDelegator(valAddr sdk.ValAddress, coin sdk.Coin) stakingtypes.DelegationI {
s.T().Helper()

delAddrs := apptesting.AddTestAddrs(s.App, s.Ctx, 1, sdk.NewInt(1_000_000_000))
delAddrs := apptesting.AddTestAddrs(s.App, s.Ctx, 1, types.DYM.MulRaw(1_000))
delAddr := delAddrs[0]
return s.Delegate(delAddr, valAddr, coin)
}
Expand Down
21 changes: 2 additions & 19 deletions x/sponsorship/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,11 @@ func (m MsgServer) Vote(goCtx context.Context, msg *types.MsgVote) (*types.MsgVo
// Don't check the error since it's part of validation
voter := sdk.MustAccAddressFromBech32(msg.Voter)

vote, distr, err := m.k.Vote(ctx, voter, msg.Weights)
_, _, err = m.k.Vote(ctx, voter, msg.Weights)
if err != nil {
return nil, err
}

err = uevent.EmitTypedEvent(ctx, &types.EventVote{
Voter: msg.Voter,
Vote: vote,
Distribution: distr,
})
if err != nil {
return nil, fmt.Errorf("emit event: %w", err)
}

return &types.MsgVoteResponse{}, nil
}

Expand All @@ -58,19 +49,11 @@ func (m MsgServer) RevokeVote(goCtx context.Context, msg *types.MsgRevokeVote) (
// Don't check the error since it's part of validation
voter := sdk.MustAccAddressFromBech32(msg.Voter)

distr, err := m.k.RevokeVote(ctx, voter)
_, err = m.k.RevokeVote(ctx, voter)
if err != nil {
return nil, err
}

err = uevent.EmitTypedEvent(ctx, &types.EventRevokeVote{
Voter: msg.Voter,
Distribution: distr,
})
if err != nil {
return nil, fmt.Errorf("emit event: %w", err)
}

return &types.MsgRevokeVoteResponse{}, nil
}

Expand Down
20 changes: 19 additions & 1 deletion x/sponsorship/keeper/votes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/dymensionxyz/sdk-utils/utils/uevent"

incentivestypes "github.com/dymensionxyz/dymension/v3/x/incentives/types"
"github.com/dymensionxyz/dymension/v3/x/sponsorship/types"
Expand Down Expand Up @@ -74,6 +75,15 @@ func (k Keeper) Vote(ctx sdk.Context, voter sdk.AccAddress, weights []types.Gaug
}
}

err = uevent.EmitTypedEvent(ctx, &types.EventVote{
Voter: voter.String(),
Vote: vote,
Distribution: distr,
})
if err != nil {
return types.Vote{}, types.Distribution{}, fmt.Errorf("emit event: %w", err)
}

return vote, distr, nil
}

Expand Down Expand Up @@ -107,6 +117,14 @@ func (k Keeper) revokeVote(ctx sdk.Context, voter sdk.AccAddress, vote types.Vot
return types.Distribution{}, fmt.Errorf("failed to delete delegator's vote breakdown: %w", err)
}

err = uevent.EmitTypedEvent(ctx, &types.EventRevokeVote{
Voter: voter.String(),
Distribution: d,
})
if err != nil {
return types.Distribution{}, fmt.Errorf("emit event: %w", err)
}

return d, nil
}

Expand Down Expand Up @@ -174,7 +192,7 @@ func (k Keeper) GetValidatorBreakdown(ctx sdk.Context, voter sdk.AccAddress) (Va
}

// VotingPower = Ceil(DelegationShares * BondedTokens / TotalShares)
votingPower := v.TokensFromShares(d.GetShares()).Ceil().TruncateInt()
votingPower := v.TokensFromShares(d.GetShares()).TruncateInt()
totalPower = totalPower.Add(votingPower)

breakdown = append(breakdown, ValidatorPower{
Expand Down

0 comments on commit 294d8c4

Please sign in to comment.