|
| 1 | +package ante_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + |
| 8 | + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" |
| 9 | + |
| 10 | + "cosmossdk.io/math" |
| 11 | + |
| 12 | + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" |
| 13 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 14 | + govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" |
| 15 | + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" |
| 16 | + |
| 17 | + "github.com/cosmos/gaia/v15/ante" |
| 18 | + "github.com/cosmos/gaia/v15/app/helpers" |
| 19 | +) |
| 20 | + |
| 21 | +func TestVoteSpamDecorator(t *testing.T) { |
| 22 | + gaiaApp := helpers.Setup(t) |
| 23 | + ctx := gaiaApp.NewUncachedContext(true, tmproto.Header{}) |
| 24 | + decorator := ante.NewGovVoteDecorator(gaiaApp.AppCodec(), gaiaApp.StakingKeeper) |
| 25 | + stakingKeeper := gaiaApp.StakingKeeper |
| 26 | + |
| 27 | + // Get validator |
| 28 | + valAddr1 := stakingKeeper.GetAllValidators(ctx)[0].GetOperator() |
| 29 | + |
| 30 | + // Create one more validator |
| 31 | + pk := ed25519.GenPrivKeyFromSecret([]byte{uint8(13)}).PubKey() |
| 32 | + validator2, err := stakingtypes.NewValidator( |
| 33 | + sdk.ValAddress(pk.Address()), |
| 34 | + pk, |
| 35 | + stakingtypes.Description{}, |
| 36 | + ) |
| 37 | + valAddr2 := validator2.GetOperator() |
| 38 | + require.NoError(t, err) |
| 39 | + // Make sure the validator is bonded so it's not removed on Undelegate |
| 40 | + validator2.Status = stakingtypes.Bonded |
| 41 | + stakingKeeper.SetValidator(ctx, validator2) |
| 42 | + err = stakingKeeper.SetValidatorByConsAddr(ctx, validator2) |
| 43 | + require.NoError(t, err) |
| 44 | + stakingKeeper.SetNewValidatorByPowerIndex(ctx, validator2) |
| 45 | + err = stakingKeeper.Hooks().AfterValidatorCreated(ctx, validator2.GetOperator()) |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + // Get delegator (this account was created during setup) |
| 49 | + addr := gaiaApp.AccountKeeper.GetAccountAddressByID(ctx, 0) |
| 50 | + delegator, err := sdk.AccAddressFromBech32(addr) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + bondAmt math.Int |
| 56 | + validators []sdk.ValAddress |
| 57 | + expectPass bool |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "delegate 0.1 atom", |
| 61 | + bondAmt: sdk.NewInt(100000), |
| 62 | + validators: []sdk.ValAddress{valAddr1}, |
| 63 | + expectPass: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "delegate 1 atom", |
| 67 | + bondAmt: sdk.NewInt(1000000), |
| 68 | + validators: []sdk.ValAddress{valAddr1}, |
| 69 | + expectPass: true, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "delegate 1 atom to two validators", |
| 73 | + bondAmt: sdk.NewInt(1000000), |
| 74 | + validators: []sdk.ValAddress{valAddr1, valAddr2}, |
| 75 | + expectPass: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "delegate 0.9 atom to two validators", |
| 79 | + bondAmt: sdk.NewInt(900000), |
| 80 | + validators: []sdk.ValAddress{valAddr1, valAddr2}, |
| 81 | + expectPass: false, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "delegate 10 atom", |
| 85 | + bondAmt: sdk.NewInt(10000000), |
| 86 | + validators: []sdk.ValAddress{valAddr1}, |
| 87 | + expectPass: true, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for _, tc := range tests { |
| 92 | + // Unbond all tokens for this delegator |
| 93 | + delegations := stakingKeeper.GetAllDelegatorDelegations(ctx, delegator) |
| 94 | + for _, del := range delegations { |
| 95 | + _, err := stakingKeeper.Undelegate(ctx, delegator, del.GetValidatorAddr(), del.GetShares()) |
| 96 | + require.NoError(t, err) |
| 97 | + } |
| 98 | + |
| 99 | + // Delegate tokens |
| 100 | + amt := tc.bondAmt.Quo(sdk.NewInt(int64(len(tc.validators)))) |
| 101 | + for _, valAddr := range tc.validators { |
| 102 | + val, found := stakingKeeper.GetValidator(ctx, valAddr) |
| 103 | + require.True(t, found) |
| 104 | + _, err := stakingKeeper.Delegate(ctx, delegator, amt, stakingtypes.Unbonded, val, true) |
| 105 | + require.NoError(t, err) |
| 106 | + } |
| 107 | + |
| 108 | + // Create vote message |
| 109 | + msg := govv1beta1.NewMsgVote( |
| 110 | + delegator, |
| 111 | + 0, |
| 112 | + govv1beta1.OptionYes, |
| 113 | + ) |
| 114 | + |
| 115 | + // Validate vote message |
| 116 | + err := decorator.ValidateVoteMsgs(ctx, []sdk.Msg{msg}) |
| 117 | + if tc.expectPass { |
| 118 | + require.NoError(t, err, "expected %v to pass", tc.name) |
| 119 | + } else { |
| 120 | + require.Error(t, err, "expected %v to fail", tc.name) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments