From a9403e32d98445f30668fa745201db9fad7ba4be Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 13 Apr 2022 16:34:42 +0200 Subject: [PATCH] feat: add (re)delegation getters (backport #11596) (#11615) * feat: add (re)delegation getters (#11596) This PR adds general helper functions to the `x/staking` module that are used in the Evmos `x/vesting` module and originated from Agoric's custom staking module implementation https://github.com/agoric-labs/cosmos-sdk/blob/4085-true-vesting/x/staking/keeper/delegation.go. - `GetDelegatorUnbonding` - `GetDelegatorBonded` - `IterateDelegatorUnbondingDelegations` - `IterateDelegatorDelegations` - `IterateDelegatorRedelegations` (cherry picked from commit b8270fc9ba2ed95eb691a2395ec28b62d75b82fb) * fix RemoveDelegation method conflict * fix CHANGELOG conflict * add new PR in CHANGELOG Co-authored-by: Daniel Burckhardt --- CHANGELOG.md | 9 +- x/staking/keeper/delegation.go | 162 ++++++++++++++++++++-------- x/staking/keeper/delegation_test.go | 11 +- 3 files changed, 133 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a9744ac7465..d6c2fe1e8d34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,16 +60,17 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#11124](https://github.com/cosmos/cosmos-sdk/pull/11124) Add `GetAllVersions` to application store * (x/auth) [\#10880](https://github.com/cosmos/cosmos-sdk/pull/10880) Added a new query to the tx query service that returns a block with transactions fully decoded. * [#11314](https://github.com/cosmos/cosmos-sdk/pull/11314) Add state rollback command. +* (x/staking) [\#11596](https://github.com/cosmos/cosmos-sdk/pull/11596) Add (re)delegation getters ### Bug Fixes * [\#11354](https://github.com/cosmos/cosmos-sdk/pull/11355) Added missing pagination flag for `bank q total` query. -* [\#11197](https://github.com/cosmos/cosmos-sdk/pull/11197) Signing with multisig now works with multisig address which is not in the keyring. +* [\#11197](https://github.com/cosmos/cosmos-sdk/pull/11197) Signing with multisig now works with multisig address which is not in the keyring. * (client) [\#11283](https://github.com/cosmos/cosmos-sdk/issues/11283) Support multiple keys for tx simulation and setting automatic gas for txs. * (store) [\#11177](https://github.com/cosmos/cosmos-sdk/pull/11177) Update the prune `everything` strategy to store the last two heights. * (store) [\#11117](https://github.com/cosmos/cosmos-sdk/pull/11117) Fix data race in store trace component * (x/authz) [\#11252](https://github.com/cosmos/cosmos-sdk/pull/11252) Allow insufficient funds error for authz simulation -* (crypto) [\#11298](https://github.com/cosmos/cosmos-sdk/pull/11298) Fix cgo secp signature verification and update libscep256k1 library. +* (crypto) [\#11298](https://github.com/cosmos/cosmos-sdk/pull/11298) Fix cgo secp signature verification and update libscep256k1 library. ### Improvements @@ -124,7 +125,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes -* (std/codec) [/#10595](https://github.com/cosmos/cosmos-sdk/pull/10595) Add evidence to std/codec to be able to decode evidence in client interactions. +* (std/codec) [/#10595](https://github.com/cosmos/cosmos-sdk/pull/10595) Add evidence to std/codec to be able to decode evidence in client interactions. * (types) [\#9627](https://github.com/cosmos/cosmos-sdk/pull/9627) Fix nil pointer panic on `NewBigIntFromInt`. * [#10725](https://github.com/cosmos/cosmos-sdk/pull/10725) populate `ctx.ConsensusParams` for begin/end blockers. * [\#9829](https://github.com/cosmos/cosmos-sdk/pull/9829) Fixed Coin denom sorting not being checked during `Balance.Validate` check. Refactored the Validation logic to use `Coins.Validate` for `Balance.Coins` @@ -134,7 +135,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements -* (baseapp) [\#10631](https://github.com/cosmos/cosmos-sdk/pull/10631) Emit ante events even for the failed txs. +* (baseapp) [\#10631](https://github.com/cosmos/cosmos-sdk/pull/10631) Emit ante events even for the failed txs. ### Features diff --git a/x/staking/keeper/delegation.go b/x/staking/keeper/delegation.go index 5b180cc9d802..4b19ea975165 100644 --- a/x/staking/keeper/delegation.go +++ b/x/staking/keeper/delegation.go @@ -11,7 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// return a specific delegation +// GetDelegation returns a specific delegation. func (k Keeper) GetDelegation(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) (delegation types.Delegation, found bool) { store := ctx.KVStore(k.storeKey) @@ -27,7 +27,7 @@ func (k Keeper) GetDelegation(ctx sdk.Context, return delegation, true } -// IterateAllDelegations iterate through all of the delegations +// IterateAllDelegations iterates through all of the delegations. func (k Keeper) IterateAllDelegations(ctx sdk.Context, cb func(delegation types.Delegation) (stop bool)) { store := ctx.KVStore(k.storeKey) @@ -58,7 +58,7 @@ func (k Keeper) IterateDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAd } } -// GetAllDelegations returns all delegations used during genesis dump +// GetAllDelegations returns all delegations used during genesis dump. func (k Keeper) GetAllDelegations(ctx sdk.Context) (delegations []types.Delegation) { k.IterateAllDelegations(ctx, func(delegation types.Delegation) bool { delegations = append(delegations, delegation) @@ -68,7 +68,8 @@ func (k Keeper) GetAllDelegations(ctx sdk.Context) (delegations []types.Delegati return delegations } -// return all delegations to a specific validator. Useful for querier. +// GetValidatorDelegations returns all delegations to a specific validator. +// Useful for querier. func (k Keeper) GetValidatorDelegations(ctx sdk.Context, valAddr sdk.ValAddress) (delegations []types.Delegation) { //nolint:interfacer store := ctx.KVStore(k.storeKey) @@ -85,7 +86,8 @@ func (k Keeper) GetValidatorDelegations(ctx sdk.Context, valAddr sdk.ValAddress) return delegations } -// return a given amount of all the delegations from a delegator +// GetDelegatorDelegations returns a given amount of all the delegations from a +// delegator. func (k Keeper) GetDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAddress, maxRetrieve uint16) (delegations []types.Delegation) { delegations = make([]types.Delegation, maxRetrieve) @@ -121,7 +123,7 @@ func (k Keeper) GetDelegatorBonded(ctx sdk.Context, delegator sdk.AccAddress) sd return bonded } -// set a delegation +// SetDelegation sets a delegation. func (k Keeper) SetDelegation(ctx sdk.Context, delegation types.Delegation) { delegatorAddress, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress) if err != nil { @@ -133,7 +135,7 @@ func (k Keeper) SetDelegation(ctx sdk.Context, delegation types.Delegation) { store.Set(types.GetDelegationKey(delegatorAddress, delegation.GetValidatorAddr()), b) } -// remove a delegation +// RemoveDelegation removes a delegation. func (k Keeper) RemoveDelegation(ctx sdk.Context, delegation types.Delegation) { delegatorAddress, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress) if err != nil { @@ -145,7 +147,7 @@ func (k Keeper) RemoveDelegation(ctx sdk.Context, delegation types.Delegation) { store.Delete(types.GetDelegationKey(delegatorAddress, delegation.GetValidatorAddr())) } -// return a given amount of all the delegator unbonding-delegations +// GetUnbondingDelegations returns a given amount of all the delegator unbonding-delegations. func (k Keeper) GetUnbondingDelegations(ctx sdk.Context, delegator sdk.AccAddress, maxRetrieve uint16) (unbondingDelegations []types.UnbondingDelegation) { unbondingDelegations = make([]types.UnbondingDelegation, maxRetrieve) @@ -166,7 +168,7 @@ func (k Keeper) GetUnbondingDelegations(ctx sdk.Context, delegator sdk.AccAddres return unbondingDelegations[:i] // trim if the array length < maxRetrieve } -// return a unbonding delegation +// GetUnbondingDelegation returns a unbonding delegation. func (k Keeper) GetUnbondingDelegation( ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, ) (ubd types.UnbondingDelegation, found bool) { @@ -183,7 +185,8 @@ func (k Keeper) GetUnbondingDelegation( return ubd, true } -// return all unbonding delegations from a particular validator +// GetUnbondingDelegationsFromValidator returns all unbonding delegations from a +// particular validator. func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sdk.ValAddress) (ubds []types.UnbondingDelegation) { store := ctx.KVStore(k.storeKey) @@ -200,7 +203,7 @@ func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sd return ubds } -// iterate through all of the unbonding delegations +// IterateUnbondingDelegations iterates through all of the unbonding delegations. func (k Keeper) IterateUnbondingDelegations(ctx sdk.Context, fn func(index int64, ubd types.UnbondingDelegation) (stop bool)) { store := ctx.KVStore(k.storeKey) @@ -216,10 +219,22 @@ func (k Keeper) IterateUnbondingDelegations(ctx sdk.Context, fn func(index int64 } } -// iterate through a delegator's unbonding delegations -func (k Keeper) IterateDelegatorUnbondingDelegations(ctx sdk.Context, delegator sdk.AccAddress, - cb func(ubd types.UnbondingDelegation) (stop bool)) { +// GetDelegatorUnbonding returns the total amount a delegator has unbonding. +func (k Keeper) GetDelegatorUnbonding(ctx sdk.Context, delegator sdk.AccAddress) sdk.Int { + unbonding := sdk.ZeroInt() + k.IterateDelegatorUnbondingDelegations(ctx, delegator, func(ubd types.UnbondingDelegation) bool { + for _, entry := range ubd.Entries { + unbonding = unbonding.Add(entry.Balance) + } + return false + }) + return unbonding +} + +// IterateDelegatorUnbondingDelegations iterates through a delegator's unbonding delegations. +func (k Keeper) IterateDelegatorUnbondingDelegations(ctx sdk.Context, delegator sdk.AccAddress, cb func(ubd types.UnbondingDelegation) (stop bool)) { store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, types.GetUBDsKey(delegator)) defer iterator.Close() @@ -243,7 +258,58 @@ func (k Keeper) GetDelegatorUnbonding(ctx sdk.Context, delegator sdk.AccAddress) return unbonding } -// HasMaxUnbondingDelegationEntries - check if unbonding delegation has maximum number of entries +// GetDelegatorBonded returs the total amount a delegator has bonded. +func (k Keeper) GetDelegatorBonded(ctx sdk.Context, delegator sdk.AccAddress) sdk.Int { + bonded := sdk.ZeroDec() + + k.IterateDelegatorDelegations(ctx, delegator, func(delegation types.Delegation) bool { + validatorAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress) + if err != nil { + panic(err) // shouldn't happen + } + validator, found := k.GetValidator(ctx, validatorAddr) + if found { + shares := delegation.Shares + tokens := validator.TokensFromSharesTruncated(shares) + bonded = bonded.Add(tokens) + } + return false + }) + return bonded.RoundInt() +} + +// IterateDelegatorDelegations iterates through one delegator's delegations. +func (k Keeper) IterateDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAddress, cb func(delegation types.Delegation) (stop bool)) { + store := ctx.KVStore(k.storeKey) + delegatorPrefixKey := types.GetDelegationsKey(delegator) + iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value()) + if cb(delegation) { + break + } + } +} + +// IterateDelegatorRedelegations iterates through one delegator's redelegations. +func (k Keeper) IterateDelegatorRedelegations(ctx sdk.Context, delegator sdk.AccAddress, cb func(red types.Redelegation) (stop bool)) { + store := ctx.KVStore(k.storeKey) + delegatorPrefixKey := types.GetREDsKey(delegator) + + iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + red := types.MustUnmarshalRED(k.cdc, iterator.Value()) + if cb(red) { + break + } + } +} + +// HasMaxUnbondingDelegationEntries - check if unbonding delegation has maximum number of entries. func (k Keeper) HasMaxUnbondingDelegationEntries(ctx sdk.Context, delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress) bool { ubd, found := k.GetUnbondingDelegation(ctx, delegatorAddr, validatorAddr) @@ -254,7 +320,7 @@ func (k Keeper) HasMaxUnbondingDelegationEntries(ctx sdk.Context, return len(ubd.Entries) >= int(k.MaxEntries(ctx)) } -// set the unbonding delegation and associated index +// SetUnbondingDelegation sets the unbonding delegation and associated index. func (k Keeper) SetUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { delegatorAddress, err := sdk.AccAddressFromBech32(ubd.DelegatorAddress) if err != nil { @@ -271,7 +337,7 @@ func (k Keeper) SetUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDeleg store.Set(types.GetUBDByValIndexKey(delegatorAddress, addr), []byte{}) // index, store empty bytes } -// remove the unbonding delegation object and associated index +// RemoveUnbondingDelegation removes the unbonding delegation object and associated index. func (k Keeper) RemoveUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { delegatorAddress, err := sdk.AccAddressFromBech32(ubd.DelegatorAddress) if err != nil { @@ -289,7 +355,7 @@ func (k Keeper) RemoveUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDe } // SetUnbondingDelegationEntry adds an entry to the unbonding delegation at -// the given addresses. It creates the unbonding delegation if it does not exist +// the given addresses. It creates the unbonding delegation if it does not exist. func (k Keeper) SetUnbondingDelegationEntry( ctx sdk.Context, delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress, creationHeight int64, minTime time.Time, balance sdk.Int, @@ -308,8 +374,9 @@ func (k Keeper) SetUnbondingDelegationEntry( // unbonding delegation queue timeslice operations -// gets a specific unbonding queue timeslice. A timeslice is a slice of DVPairs -// corresponding to unbonding delegations that expire at a certain time. +// GetUBDQueueTimeSlice gets a specific unbonding queue timeslice. A timeslice +// is a slice of DVPairs corresponding to unbonding delegations that expire at a +// certain time. func (k Keeper) GetUBDQueueTimeSlice(ctx sdk.Context, timestamp time.Time) (dvPairs []types.DVPair) { store := ctx.KVStore(k.storeKey) @@ -324,14 +391,15 @@ func (k Keeper) GetUBDQueueTimeSlice(ctx sdk.Context, timestamp time.Time) (dvPa return pairs.Pairs } -// Sets a specific unbonding queue timeslice. +// SetUBDQueueTimeSlice sets a specific unbonding queue timeslice. func (k Keeper) SetUBDQueueTimeSlice(ctx sdk.Context, timestamp time.Time, keys []types.DVPair) { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshal(&types.DVPairs{Pairs: keys}) store.Set(types.GetUnbondingDelegationTimeKey(timestamp), bz) } -// Insert an unbonding delegation to the appropriate timeslice in the unbonding queue +// InsertUBDQueue inserts an unbonding delegation to the appropriate timeslice +// in the unbonding queue. func (k Keeper) InsertUBDQueue(ctx sdk.Context, ubd types.UnbondingDelegation, completionTime time.Time) { dvPair := types.DVPair{DelegatorAddress: ubd.DelegatorAddress, ValidatorAddress: ubd.ValidatorAddress} @@ -345,15 +413,15 @@ func (k Keeper) InsertUBDQueue(ctx sdk.Context, ubd types.UnbondingDelegation, } } -// Returns all the unbonding queue timeslices from time 0 until endTime +// UBDQueueIterator returns all the unbonding queue timeslices from time 0 until endTime. func (k Keeper) UBDQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator { store := ctx.KVStore(k.storeKey) return store.Iterator(types.UnbondingQueueKey, sdk.InclusiveEndBytes(types.GetUnbondingDelegationTimeKey(endTime))) } -// Returns a concatenated list of all the timeslices inclusively previous to -// currTime, and deletes the timeslices from the queue +// DequeueAllMatureUBDQueue returns a concatenated list of all the timeslices inclusively previous to +// currTime, and deletes the timeslices from the queue. func (k Keeper) DequeueAllMatureUBDQueue(ctx sdk.Context, currTime time.Time) (matureUnbonds []types.DVPair) { store := ctx.KVStore(k.storeKey) @@ -374,7 +442,7 @@ func (k Keeper) DequeueAllMatureUBDQueue(ctx sdk.Context, currTime time.Time) (m return matureUnbonds } -// return a given amount of all the delegator redelegations +// GetRedelegations returns a given amount of all the delegator redelegations. func (k Keeper) GetRedelegations(ctx sdk.Context, delegator sdk.AccAddress, maxRetrieve uint16) (redelegations []types.Redelegation) { redelegations = make([]types.Redelegation, maxRetrieve) @@ -395,7 +463,7 @@ func (k Keeper) GetRedelegations(ctx sdk.Context, delegator sdk.AccAddress, return redelegations[:i] // trim if the array length < maxRetrieve } -// return a redelegation +// GetRedelegation returns a redelegation. func (k Keeper) GetRedelegation(ctx sdk.Context, delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) (red types.Redelegation, found bool) { store := ctx.KVStore(k.storeKey) @@ -411,7 +479,8 @@ func (k Keeper) GetRedelegation(ctx sdk.Context, return red, true } -// return all redelegations from a particular validator +// GetRedelegationsFromSrcValidator returns all redelegations from a particular +// validator. func (k Keeper) GetRedelegationsFromSrcValidator(ctx sdk.Context, valAddr sdk.ValAddress) (reds []types.Redelegation) { store := ctx.KVStore(k.storeKey) @@ -428,7 +497,7 @@ func (k Keeper) GetRedelegationsFromSrcValidator(ctx sdk.Context, valAddr sdk.Va return reds } -// check if validator is receiving a redelegation +// HasReceivingRedelegation checks if validator is receiving a redelegation. func (k Keeper) HasReceivingRedelegation(ctx sdk.Context, delAddr sdk.AccAddress, valDstAddr sdk.ValAddress) bool { store := ctx.KVStore(k.storeKey) @@ -440,7 +509,7 @@ func (k Keeper) HasReceivingRedelegation(ctx sdk.Context, return iterator.Valid() } -// HasMaxRedelegationEntries - redelegation has maximum number of entries +// HasMaxRedelegationEntries checks if redelegation has maximum number of entries. func (k Keeper) HasMaxRedelegationEntries(ctx sdk.Context, delegatorAddr sdk.AccAddress, validatorSrcAddr, validatorDstAddr sdk.ValAddress) bool { @@ -452,7 +521,7 @@ func (k Keeper) HasMaxRedelegationEntries(ctx sdk.Context, return len(red.Entries) >= int(k.MaxEntries(ctx)) } -// set a redelegation and associated index +// SetRedelegation set a redelegation and associated index. func (k Keeper) SetRedelegation(ctx sdk.Context, red types.Redelegation) { delegatorAddress, err := sdk.AccAddressFromBech32(red.DelegatorAddress) if err != nil { @@ -475,8 +544,8 @@ func (k Keeper) SetRedelegation(ctx sdk.Context, red types.Redelegation) { store.Set(types.GetREDByValDstIndexKey(delegatorAddress, valSrcAddr, valDestAddr), []byte{}) } -// SetUnbondingDelegationEntry adds an entry to the unbonding delegation at -// the given addresses. It creates the unbonding delegation if it does not exist +// SetRedelegationEntry adds an entry to the unbonding delegation at the given +// addresses. It creates the unbonding delegation if it does not exist. func (k Keeper) SetRedelegationEntry(ctx sdk.Context, delegatorAddr sdk.AccAddress, validatorSrcAddr, validatorDstAddr sdk.ValAddress, creationHeight int64, @@ -495,7 +564,7 @@ func (k Keeper) SetRedelegationEntry(ctx sdk.Context, return red } -// iterate through all redelegations +// IterateRedelegations iterates through all redelegations. func (k Keeper) IterateRedelegations(ctx sdk.Context, fn func(index int64, red types.Redelegation) (stop bool)) { store := ctx.KVStore(k.storeKey) @@ -527,7 +596,7 @@ func (k Keeper) IterateDelegatorRedelegations(ctx sdk.Context, delegator sdk.Acc } } -// remove a redelegation object and associated index +// RemoveRedelegation removes a redelegation object and associated index. func (k Keeper) RemoveRedelegation(ctx sdk.Context, red types.Redelegation) { delegatorAddress, err := sdk.AccAddressFromBech32(red.DelegatorAddress) if err != nil { @@ -550,8 +619,9 @@ func (k Keeper) RemoveRedelegation(ctx sdk.Context, red types.Redelegation) { // redelegation queue timeslice operations -// Gets a specific redelegation queue timeslice. A timeslice is a slice of DVVTriplets corresponding to redelegations -// that expire at a certain time. +// GetRedelegationQueueTimeSlice gets a specific redelegation queue timeslice. A +// timeslice is a slice of DVVTriplets corresponding to redelegations that +// expire at a certain time. func (k Keeper) GetRedelegationQueueTimeSlice(ctx sdk.Context, timestamp time.Time) (dvvTriplets []types.DVVTriplet) { store := ctx.KVStore(k.storeKey) @@ -566,14 +636,15 @@ func (k Keeper) GetRedelegationQueueTimeSlice(ctx sdk.Context, timestamp time.Ti return triplets.Triplets } -// Sets a specific redelegation queue timeslice. +// SetRedelegationQueueTimeSlice sets a specific redelegation queue timeslice. func (k Keeper) SetRedelegationQueueTimeSlice(ctx sdk.Context, timestamp time.Time, keys []types.DVVTriplet) { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshal(&types.DVVTriplets{Triplets: keys}) store.Set(types.GetRedelegationTimeKey(timestamp), bz) } -// Insert an redelegation delegation to the appropriate timeslice in the redelegation queue +// InsertRedelegationQueue insert an redelegation delegation to the appropriate +// timeslice in the redelegation queue. func (k Keeper) InsertRedelegationQueue(ctx sdk.Context, red types.Redelegation, completionTime time.Time) { timeSlice := k.GetRedelegationQueueTimeSlice(ctx, completionTime) @@ -590,14 +661,16 @@ func (k Keeper) InsertRedelegationQueue(ctx sdk.Context, red types.Redelegation, } } -// Returns all the redelegation queue timeslices from time 0 until endTime +// RedelegationQueueIterator returns all the redelegation queue timeslices from +// time 0 until endTime. func (k Keeper) RedelegationQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator { store := ctx.KVStore(k.storeKey) return store.Iterator(types.RedelegationQueueKey, sdk.InclusiveEndBytes(types.GetRedelegationTimeKey(endTime))) } -// Returns a concatenated list of all the timeslices inclusively previous to -// currTime, and deletes the timeslices from the queue +// DequeueAllMatureRedelegationQueue returns a concatenated list of all the +// timeslices inclusively previous to currTime, and deletes the timeslices from +// the queue. func (k Keeper) DequeueAllMatureRedelegationQueue(ctx sdk.Context, currTime time.Time) (matureRedelegations []types.DVVTriplet) { store := ctx.KVStore(k.storeKey) @@ -853,7 +926,7 @@ func (k Keeper) TransferDelegation(ctx sdk.Context, fromAddr, toAddr sdk.AccAddr return transferred } -// Unbond a particular delegation and perform associated store operations. +// Unbond unbonds a particular delegation and perform associated store operations. func (k Keeper) Unbond( ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, shares sdk.Dec, ) (amount sdk.Int, err error) { @@ -1078,7 +1151,8 @@ func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress, valAd return balances, nil } -// begin unbonding / redelegation; create a redelegation record +// BeginRedelegation begins unbonding / redelegation and creates a redelegation +// record. func (k Keeper) BeginRedelegation( ctx sdk.Context, delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress, sharesAmount sdk.Dec, ) (completionTime time.Time, err error) { diff --git a/x/staking/keeper/delegation_test.go b/x/staking/keeper/delegation_test.go index 7e8c35dacafe..6d15d8efd1ea 100644 --- a/x/staking/keeper/delegation_test.go +++ b/x/staking/keeper/delegation_test.go @@ -107,6 +107,11 @@ func TestDelegation(t *testing.T) { require.Len(t, resDels, 2) } + // test total bonded for single delegator + expBonded := bond1to1.Shares.Add(bond2to1.Shares).Add(bond1to3.Shares) + resDelBond := app.StakingKeeper.GetDelegatorBonded(ctx, addrDels[0]) + require.Equal(t, expBonded, sdk.NewDecFromInt(resDelBond)) + // delete a record app.StakingKeeper.RemoveDelegation(ctx, bond2to3) _, found = app.StakingKeeper.GetDelegation(ctx, addrDels[1], valAddrs[2]) @@ -246,7 +251,8 @@ func TestUnbondingDelegation(t *testing.T) { require.Equal(t, ubd, resUnbond) // modify a records, save, and retrieve - ubd.Entries[0].Balance = sdk.NewInt(21) + expUnbond := sdk.NewInt(21) + ubd.Entries[0].Balance = expUnbond app.StakingKeeper.SetUnbondingDelegation(ctx, ubd) resUnbonds := app.StakingKeeper.GetUnbondingDelegations(ctx, delAddrs[0], 5) @@ -259,6 +265,9 @@ func TestUnbondingDelegation(t *testing.T) { require.True(t, found) require.Equal(t, ubd, resUnbond) + resDelUnbond := app.StakingKeeper.GetDelegatorUnbonding(ctx, delAddrs[0]) + require.Equal(t, expUnbond, resDelUnbond) + // delete a record app.StakingKeeper.RemoveUnbondingDelegation(ctx, ubd) _, found = app.StakingKeeper.GetUnbondingDelegation(ctx, delAddrs[0], valAddrs[0])