Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterate delegation storage #312

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions custom/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand Down Expand Up @@ -47,6 +48,12 @@ func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*typ
k.stakingmiddleware.SetLastTotalPower(ctx, math.Int{})

k.stakingmiddleware.SetDelegation(ctx, msg.DelegatorAddress, msg.ValidatorAddress, msg.Amount.Denom, msg.Amount.Amount)
delegations := k.stakingmiddleware.DequeueAllDelegation(ctx)
if len(delegations) >= 1 {
return nil, sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest, "should always be less then 2 : got %s, expected %s", len(delegations),
)
}

return &types.MsgDelegateResponse{}, nil
// return nil, fmt.Errorf("My custom error: Nikita")
Expand Down
22 changes: 22 additions & 0 deletions x/stakingmiddleware/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ func (k Keeper) IterateDelegations(ctx sdk.Context, fn func(index int64, ubd typ
}
}

// DequeueAllMatureUBDQueue returns a concatenated list of all the timeslices inclusively previous to
// currTime, and deletes the timeslices from the queue.
func (k Keeper) DequeueAllDelegation(ctx sdk.Context) (delegations []types.Delegation) {
store := ctx.KVStore(k.storeKey)

// gets an iterator for all timeslices from time 0 until the current Blockheader time
delegationIterator := sdk.KVStorePrefixIterator(store, types.DelegationKey)
defer delegationIterator.Close()

for ; delegationIterator.Valid(); delegationIterator.Next() {
delegation := types.Delegation{}
value := delegationIterator.Value()
k.cdc.MustUnmarshal(value, &delegation)

delegations = append(delegations, delegation)

store.Delete(delegationIterator.Key())
}

return delegations
}

func GetValidatorAddr(d types.Delegation) sdk.ValAddress {
addr, err := sdk.ValAddressFromBech32(d.ValidatorAddress)
if err != nil {
Expand Down