|
1 | 1 | package keeper_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/hex" |
4 | 5 | "fmt"
|
5 | 6 | "testing"
|
6 | 7 |
|
7 | 8 | cmtabcitypes "github.com/cometbft/cometbft/abci/types"
|
8 | 9 | "github.com/cometbft/cometbft/proto/tendermint/types"
|
| 10 | + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" |
| 11 | + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" |
| 12 | + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" |
9 | 13 | "github.com/stretchr/testify/require"
|
10 | 14 | "gotest.tools/v3/assert"
|
11 | 15 |
|
@@ -73,6 +77,7 @@ func initFixture(tb testing.TB) *fixture {
|
73 | 77 |
|
74 | 78 | maccPerms := map[string][]string{
|
75 | 79 | distrtypes.ModuleName: {authtypes.Minter},
|
| 80 | + minttypes.ModuleName: {authtypes.Minter}, |
76 | 81 | stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
|
77 | 82 | stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
|
78 | 83 | }
|
@@ -133,11 +138,20 @@ func initFixture(tb testing.TB) *fixture {
|
133 | 138 | })
|
134 | 139 |
|
135 | 140 | sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context())
|
| 141 | + require.NoError(tb, stakingKeeper.SetParams(sdkCtx, stakingtypes.DefaultParams())) |
| 142 | + |
| 143 | + stakingKeeper.SetHooks( |
| 144 | + stakingtypes.NewMultiStakingHooks( |
| 145 | + distrKeeper.Hooks(), // Needed for reward distribution on staking events |
| 146 | + ), |
| 147 | + ) |
136 | 148 |
|
137 | 149 | // Register MsgServer and QueryServer
|
138 | 150 | distrtypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), distrkeeper.NewMsgServerImpl(distrKeeper))
|
139 | 151 | distrtypes.RegisterQueryServer(integrationApp.QueryHelper(), distrkeeper.NewQuerier(distrKeeper))
|
140 | 152 |
|
| 153 | + stakingtypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), stakingkeeper.NewMsgServerImpl(stakingKeeper)) |
| 154 | + |
141 | 155 | return &fixture{
|
142 | 156 | app: integrationApp,
|
143 | 157 | sdkCtx: sdkCtx,
|
@@ -974,3 +988,108 @@ func TestMsgDepositValidatorRewardsPool(t *testing.T) {
|
974 | 988 | })
|
975 | 989 | }
|
976 | 990 | }
|
| 991 | + |
| 992 | +func TestCannotDepositIfRewardPoolFull(t *testing.T) { |
| 993 | + f := initFixture(t) |
| 994 | + err := f.distrKeeper.FeePool.Set(f.sdkCtx, distrtypes.FeePool{ |
| 995 | + CommunityPool: sdk.NewDecCoins(sdk.DecCoin{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(10000)}), |
| 996 | + }) |
| 997 | + assert.NilError(t, err) |
| 998 | + assert.NilError(t, f.distrKeeper.Params.Set(f.sdkCtx, distrtypes.DefaultParams())) |
| 999 | + _, err = f.distrKeeper.FeePool.Get(f.sdkCtx) |
| 1000 | + assert.NilError(t, err) |
| 1001 | + |
| 1002 | + ctx := f.sdkCtx.WithIsCheckTx(false).WithBlockHeight(1) |
| 1003 | + populateValidators(t, f) |
| 1004 | + |
| 1005 | + valPubKey := newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB53") |
| 1006 | + operatorAddr := sdk.ValAddress(valPubKey.Address()) |
| 1007 | + |
| 1008 | + tstaking := stakingtestutil.NewHelper(t, ctx, f.stakingKeeper) |
| 1009 | + |
| 1010 | + assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, minttypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)))) |
| 1011 | + assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, minttypes.ModuleName, sdk.AccAddress(operatorAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)))) |
| 1012 | + |
| 1013 | + tstaking.Commission = stakingtypes.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()) |
| 1014 | + selfDelegation := math.OneInt() |
| 1015 | + tstaking.CreateValidator(operatorAddr, valPubKey, selfDelegation, true) |
| 1016 | + |
| 1017 | + _, err = f.stakingKeeper.EndBlocker(f.sdkCtx) |
| 1018 | + assert.NilError(t, err) |
| 1019 | + |
| 1020 | + testDenom := "utesttest" |
| 1021 | + maxSupply, ok := math.NewIntFromString("115792089237316195423570985008687907853269984665640564039457584007913129639934") |
| 1022 | + assert.Assert(t, ok) |
| 1023 | + maxCoins := sdk.NewCoins(sdk.NewCoin(testDenom, maxSupply)) |
| 1024 | + assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, minttypes.ModuleName, maxCoins)) |
| 1025 | + assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, minttypes.ModuleName, sdk.AccAddress(operatorAddr), maxCoins)) |
| 1026 | + |
| 1027 | + fundValMsg := &distrtypes.MsgDepositValidatorRewardsPool{ |
| 1028 | + Depositor: sdk.AccAddress(operatorAddr).String(), |
| 1029 | + ValidatorAddress: operatorAddr.String(), |
| 1030 | + Amount: maxCoins, |
| 1031 | + } |
| 1032 | + |
| 1033 | + // fund the rewards pool. this will set the current rewards. |
| 1034 | + _, err = f.app.RunMsg( |
| 1035 | + fundValMsg, |
| 1036 | + integration.WithAutomaticFinalizeBlock(), |
| 1037 | + integration.WithAutomaticCommit(), |
| 1038 | + ) |
| 1039 | + assert.NilError(t, err) |
| 1040 | + |
| 1041 | + // now we delegate to increment the validator period, setting the current rewards to the previous. |
| 1042 | + power := int64(1) |
| 1043 | + delegationAmount := sdk.TokensFromConsensusPower(power, sdk.DefaultPowerReduction) |
| 1044 | + delMsg := stakingtypes.NewMsgDelegate(sdk.AccAddress(operatorAddr).String(), operatorAddr.String(), sdk.NewCoin(sdk.DefaultBondDenom, delegationAmount)) |
| 1045 | + _, err = f.app.RunMsg( |
| 1046 | + delMsg, |
| 1047 | + integration.WithAutomaticFinalizeBlock(), |
| 1048 | + integration.WithAutomaticCommit(), |
| 1049 | + ) |
| 1050 | + assert.NilError(t, err) |
| 1051 | + |
| 1052 | + // this should fail since this amount cannot be added to the previous amount without overflowing. |
| 1053 | + _, err = f.app.RunMsg( |
| 1054 | + fundValMsg, |
| 1055 | + integration.WithAutomaticFinalizeBlock(), |
| 1056 | + integration.WithAutomaticCommit(), |
| 1057 | + ) |
| 1058 | + assert.ErrorContains(t, err, "unable to deposit coins") |
| 1059 | +} |
| 1060 | + |
| 1061 | +var ( |
| 1062 | + pubkeys = []cryptotypes.PubKey{ |
| 1063 | + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"), |
| 1064 | + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"), |
| 1065 | + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"), |
| 1066 | + } |
| 1067 | + |
| 1068 | + valAddresses = []sdk.ValAddress{ |
| 1069 | + sdk.ValAddress(pubkeys[0].Address()), |
| 1070 | + sdk.ValAddress(pubkeys[1].Address()), |
| 1071 | + sdk.ValAddress(pubkeys[2].Address()), |
| 1072 | + } |
| 1073 | + |
| 1074 | + initAmt = sdk.TokensFromConsensusPower(1000000, sdk.DefaultPowerReduction) |
| 1075 | + initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) |
| 1076 | +) |
| 1077 | + |
| 1078 | +func populateValidators(t assert.TestingT, f *fixture) { |
| 1079 | + totalSupplyAmt := initAmt.MulRaw(int64(len(valAddresses))) |
| 1080 | + totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupplyAmt)) |
| 1081 | + assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, totalSupply)) |
| 1082 | + |
| 1083 | + for _, addr := range valAddresses { |
| 1084 | + assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, distrtypes.ModuleName, (sdk.AccAddress)(addr), initCoins)) |
| 1085 | + } |
| 1086 | +} |
| 1087 | + |
| 1088 | +func newPubKey(pk string) (res cryptotypes.PubKey) { |
| 1089 | + pkBytes, err := hex.DecodeString(pk) |
| 1090 | + if err != nil { |
| 1091 | + panic(err) |
| 1092 | + } |
| 1093 | + pubkey := &ed25519.PubKey{Key: pkBytes} |
| 1094 | + return pubkey |
| 1095 | +} |
0 commit comments