-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: staking module using mocks (#12827)
## Description Closes: #12504 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
- Loading branch information
1 parent
d11196a
commit bc274d8
Showing
24 changed files
with
4,316 additions
and
3,309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/bank/testutil" | ||
"github.com/cosmos/cosmos-sdk/x/staking/keeper" | ||
"github.com/cosmos/cosmos-sdk/x/staking/teststaking" | ||
"github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
func TestUnbondingDelegationsMaxEntries(t *testing.T) { | ||
_, app, ctx := createTestInput(t) | ||
|
||
addrDels := simapp.AddTestAddrsIncremental(app, ctx, 1, sdk.NewInt(10000)) | ||
addrVals := simtestutil.ConvertAddrsToValAddrs(addrDels) | ||
|
||
startTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 10) | ||
|
||
bondDenom := app.StakingKeeper.BondDenom(ctx) | ||
notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx) | ||
|
||
require.NoError(t, testutil.FundModuleAccount(app.BankKeeper, ctx, notBondedPool.GetName(), sdk.NewCoins(sdk.NewCoin(bondDenom, startTokens)))) | ||
app.AccountKeeper.SetModuleAccount(ctx, notBondedPool) | ||
|
||
// create a validator and a delegator to that validator | ||
validator := teststaking.NewValidator(t, addrVals[0], PKs[0]) | ||
|
||
validator, issuedShares := validator.AddTokensFromDel(startTokens) | ||
require.Equal(t, startTokens, issuedShares.RoundInt()) | ||
|
||
validator = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validator, true) | ||
require.True(math.IntEq(t, startTokens, validator.BondedTokens())) | ||
require.True(t, validator.IsBonded()) | ||
|
||
delegation := types.NewDelegation(addrDels[0], addrVals[0], issuedShares) | ||
app.StakingKeeper.SetDelegation(ctx, delegation) | ||
|
||
maxEntries := app.StakingKeeper.MaxEntries(ctx) | ||
|
||
oldBonded := app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
oldNotBonded := app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
|
||
// should all pass | ||
var completionTime time.Time | ||
for i := int64(0); i < int64(maxEntries); i++ { | ||
var err error | ||
ctx = ctx.WithBlockHeight(i) | ||
completionTime, err = app.StakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], math.LegacyNewDec(1)) | ||
require.NoError(t, err) | ||
} | ||
|
||
newBonded := app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
newNotBonded := app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
require.True(math.IntEq(t, newBonded, oldBonded.SubRaw(int64(maxEntries)))) | ||
require.True(math.IntEq(t, newNotBonded, oldNotBonded.AddRaw(int64(maxEntries)))) | ||
|
||
oldBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
oldNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
|
||
// an additional unbond should fail due to max entries | ||
_, err := app.StakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], math.LegacyNewDec(1)) | ||
require.Error(t, err) | ||
|
||
newBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
newNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
|
||
require.True(math.IntEq(t, newBonded, oldBonded)) | ||
require.True(math.IntEq(t, newNotBonded, oldNotBonded)) | ||
|
||
// mature unbonding delegations | ||
ctx = ctx.WithBlockTime(completionTime) | ||
_, err = app.StakingKeeper.CompleteUnbonding(ctx, addrDels[0], addrVals[0]) | ||
require.NoError(t, err) | ||
|
||
newBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
newNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
require.True(math.IntEq(t, newBonded, oldBonded)) | ||
require.True(math.IntEq(t, newNotBonded, oldNotBonded.SubRaw(int64(maxEntries)))) | ||
|
||
oldNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
|
||
// unbonding should work again | ||
_, err = app.StakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], math.LegacyNewDec(1)) | ||
require.NoError(t, err) | ||
|
||
newBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
newNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount | ||
require.True(math.IntEq(t, newBonded, oldBonded.SubRaw(1))) | ||
require.True(math.IntEq(t, newNotBonded, oldNotBonded.AddRaw(1))) | ||
} |
File renamed without changes.
Oops, something went wrong.