Skip to content

Commit

Permalink
test(wip): add self delegation for validator, remove constant total b…
Browse files Browse the repository at this point in the history
…onded
  • Loading branch information
tbruyelle committed Aug 29, 2024
1 parent 82cc870 commit 85f0689
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
6 changes: 6 additions & 0 deletions x/gov/keeper/tally.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -64,6 +66,7 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal v1.Proposal) (passes bool,
results[option.Option] = results[option.Option].Add(subPower)
}
totalVotingPower = totalVotingPower.Add(votingPower)
fmt.Println("ADD VOTE", votingPower)
}

return false
Expand All @@ -78,6 +81,7 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal v1.Proposal) (passes bool,
if len(val.Vote) == 0 {
continue
}
fmt.Println("VAL DEL SHARES", val.DelegatorShares, val.DelegatorDeductions)

sharesAfterDeductions := val.DelegatorShares.Sub(val.DelegatorDeductions)
votingPower := sharesAfterDeductions.MulInt(val.BondedTokens).Quo(val.DelegatorShares)
Expand All @@ -88,6 +92,7 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal v1.Proposal) (passes bool,
results[option.Option] = results[option.Option].Add(subPower)
}
totalVotingPower = totalVotingPower.Add(votingPower)
fmt.Println("ADD VAL VOTE", votingPower)
}

params := keeper.GetParams(ctx)
Expand All @@ -96,6 +101,7 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal v1.Proposal) (passes bool,
// TODO: Upgrade the spec to cover all of these cases & remove pseudocode.
// If there is no staked coins, the proposal fails
totalBondedTokens := keeper.sk.TotalBondedTokens(ctx)
fmt.Println("total bonded", totalBondedTokens)
if totalBondedTokens.IsZero() {
return false, false, tallyResults
}
Expand Down
54 changes: 27 additions & 27 deletions x/gov/keeper/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ func TestTally(t *testing.T) {
}

var (
totalBonded int64
// handy functions
setTotalBonded = func(s suite, n int64) {
s.mocks.stakingKeeper.EXPECT().TotalBondedTokens(gomock.Any()).Return(sdkmath.NewInt(n))
}
delegatorVote = func(s suite, voter sdk.AccAddress, delegations []stakingtypes.Delegation, vote v1.VoteOption) {
err := s.keeper.AddVote(s.ctx, s.proposal.Id, voter, v1.NewNonSplitVoteOption(vote), "")
require.NoError(s.t, err)
// Increment total bonded according to each delegations
for _, d := range delegations {
totalBonded += d.Shares.RoundInt64()
}
s.mocks.stakingKeeper.EXPECT().
IterateDelegations(s.ctx, voter, gomock.Any()).
DoAndReturn(
Expand All @@ -48,8 +50,14 @@ func TestTally(t *testing.T) {
})
}
validatorVote = func(s suite, voter sdk.ValAddress, vote v1.VoteOption) {
// validatorVote is like delegatorVote but without delegations
delegatorVote(s, sdk.AccAddress(voter), nil, vote)
accAddr := sdk.AccAddress(voter)
// validator self delegation
selfDelegation := []stakingtypes.Delegation{{
DelegatorAddress: accAddr.String(),
ValidatorAddress: voter.String(),
Shares: sdkmath.LegacyNewDec(1),
}}
delegatorVote(s, accAddr, selfDelegation, vote)
}
)
tests := []struct {
Expand All @@ -61,10 +69,7 @@ func TestTally(t *testing.T) {
expectedError string
}{
{
name: "no votes, no bonded tokens: prop fails",
setup: func(s suite) {
setTotalBonded(s, 0)
},
name: "no votes, no bonded tokens: prop fails",
expectedPass: false,
expectedBurn: false,
expectedTally: v1.TallyResult{
Expand All @@ -75,10 +80,7 @@ func TestTally(t *testing.T) {
},
},
{
name: "no votes: prop fails/burn deposit",
setup: func(s suite) {
setTotalBonded(s, 10000000)
},
name: "no votes: prop fails/burn deposit",
expectedPass: false,
expectedBurn: true, // burn because quorum not reached
expectedTally: v1.TallyResult{
Expand All @@ -91,7 +93,6 @@ func TestTally(t *testing.T) {
{
name: "one validator votes: prop fails/burn deposit",
setup: func(s suite) {
setTotalBonded(s, 10000000)
validatorVote(s, s.valAddrs[0], v1.VoteOption_VOTE_OPTION_NO)
},
expectedPass: false,
Expand All @@ -106,7 +107,6 @@ func TestTally(t *testing.T) {
{
name: "one account votes without delegation: prop fails/burn deposit",
setup: func(s suite) {
setTotalBonded(s, 10000000)
delegatorVote(s, s.delAddrs[0], nil, v1.VoteOption_VOTE_OPTION_YES)
},
expectedPass: false,
Expand All @@ -121,7 +121,6 @@ func TestTally(t *testing.T) {
{
name: "one delegator votes: prop fails/burn deposit",
setup: func(s suite) {
setTotalBonded(s, 10000000)
delegations := []stakingtypes.Delegation{{
DelegatorAddress: s.delAddrs[0].String(),
ValidatorAddress: s.valAddrs[0].String(),
Expand All @@ -141,7 +140,6 @@ func TestTally(t *testing.T) {
{
name: "one delegator votes yes, validator votes also yes: prop fails/burn deposit",
setup: func(s suite) {
setTotalBonded(s, 10000000)
delegations := []stakingtypes.Delegation{{
DelegatorAddress: s.delAddrs[0].String(),
ValidatorAddress: s.valAddrs[0].String(),
Expand All @@ -162,7 +160,6 @@ func TestTally(t *testing.T) {
{
name: "one delegator votes yes, validator votes no: prop fails/burn deposit",
setup: func(s suite) {
setTotalBonded(s, 10000000)
delegations := []stakingtypes.Delegation{{
DelegatorAddress: s.delAddrs[0].String(),
ValidatorAddress: s.valAddrs[0].String(),
Expand All @@ -188,7 +185,6 @@ func TestTally(t *testing.T) {
// third validator (no delegation) votes abstain
name: "delegator with mixed delegations: prop fails/burn deposit",
setup: func(s suite) {
setTotalBonded(s, 10000000)
delegations := []stakingtypes.Delegation{
{
DelegatorAddress: s.delAddrs[0].String(),
Expand Down Expand Up @@ -218,7 +214,6 @@ func TestTally(t *testing.T) {
{
name: "quorum reached with only abstain: prop fails",
setup: func(s suite) {
setTotalBonded(s, 10000000)
validatorVote(s, s.valAddrs[0], v1.VoteOption_VOTE_OPTION_ABSTAIN)
validatorVote(s, s.valAddrs[1], v1.VoteOption_VOTE_OPTION_ABSTAIN)
validatorVote(s, s.valAddrs[2], v1.VoteOption_VOTE_OPTION_ABSTAIN)
Expand All @@ -236,7 +231,6 @@ func TestTally(t *testing.T) {
{
name: "quorum reached with veto>1/3: prop fails/burn deposit",
setup: func(s suite) {
setTotalBonded(s, 10000000)
validatorVote(s, s.valAddrs[0], v1.VoteOption_VOTE_OPTION_YES)
validatorVote(s, s.valAddrs[1], v1.VoteOption_VOTE_OPTION_YES)
validatorVote(s, s.valAddrs[2], v1.VoteOption_VOTE_OPTION_YES)
Expand All @@ -257,7 +251,6 @@ func TestTally(t *testing.T) {
{
name: "quorum reached with yes<=.5: prop fails",
setup: func(s suite) {
setTotalBonded(s, 10000000)
validatorVote(s, s.valAddrs[0], v1.VoteOption_VOTE_OPTION_YES)
validatorVote(s, s.valAddrs[1], v1.VoteOption_VOTE_OPTION_YES)
validatorVote(s, s.valAddrs[2], v1.VoteOption_VOTE_OPTION_NO)
Expand All @@ -275,7 +268,6 @@ func TestTally(t *testing.T) {
{
name: "quorum reached with yes>.5: prop succeeds",
setup: func(s suite) {
setTotalBonded(s, 10000000)
validatorVote(s, s.valAddrs[0], v1.VoteOption_VOTE_OPTION_YES)
validatorVote(s, s.valAddrs[1], v1.VoteOption_VOTE_OPTION_YES)
validatorVote(s, s.valAddrs[2], v1.VoteOption_VOTE_OPTION_YES)
Expand All @@ -296,7 +288,6 @@ func TestTally(t *testing.T) {
{
name: "quorum reached thanks to abstain, yes>.5: prop succeeds",
setup: func(s suite) {
setTotalBonded(s, 10000000)
validatorVote(s, s.valAddrs[0], v1.VoteOption_VOTE_OPTION_YES)
validatorVote(s, s.valAddrs[1], v1.VoteOption_VOTE_OPTION_YES)
validatorVote(s, s.valAddrs[2], v1.VoteOption_VOTE_OPTION_NO)
Expand All @@ -323,6 +314,12 @@ func TestTally(t *testing.T) {
params.BurnVoteVeto = true
err := govKeeper.SetParams(ctx, params)
require.NoError(t, err)
// Reset total bonded
totalBonded = 0
mocks.stakingKeeper.EXPECT().TotalBondedTokens(gomock.Any()).
DoAndReturn(func(_ context.Context) sdkmath.Int {
return sdkmath.NewInt(totalBonded)
}).MaxTimes(1)
var (
numVals = 10
numDelegators = 5
Expand All @@ -339,8 +336,8 @@ func TestTally(t *testing.T) {
fn(i, stakingtypes.Validator{
OperatorAddress: valAddrs[i].String(),
Status: stakingtypes.Bonded,
Tokens: sdkmath.NewInt(1000000),
DelegatorShares: sdkmath.LegacyNewDec(1000000),
Tokens: sdkmath.NewInt(totalBonded),
DelegatorShares: sdkmath.LegacyNewDec(totalBonded),
})
}
return nil
Expand All @@ -358,13 +355,16 @@ func TestTally(t *testing.T) {
keeper: govKeeper,
mocks: mocks,
}
tt.setup(suite)
if tt.setup != nil {
tt.setup(suite)
}

pass, burn, tally := govKeeper.Tally(ctx, proposal)

assert.Equal(t, tt.expectedPass, pass, "wrong pass")
assert.Equal(t, tt.expectedBurn, burn, "wrong burn")
assert.Equal(t, tt.expectedTally, tally)
assert.Empty(t, govKeeper.GetVotes(ctx, proposal.Id), "votes not be removed after tally")
})
}
}

0 comments on commit 85f0689

Please sign in to comment.