Skip to content

Commit

Permalink
Merge pull request #312 from ComposableFi/iterate-delegation-storage
Browse files Browse the repository at this point in the history
iterate delegation storage
  • Loading branch information
RustNinja authored Dec 19, 2023
2 parents 007db4d + 1212864 commit 78e50e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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(

Check failure on line 53 in custom/staking/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: sdkerrors.Wrapf is deprecated: functionality of this package has been moved to it's own module: (staticcheck)
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

0 comments on commit 78e50e1

Please sign in to comment.