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