-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use a single delegation type (#57)
## Description While starting to implement unbonding, I realized that @hallazzang suggestion of using a single `Delegation` object was the best thing to do in order to avoid having code that is copy/pasted just because of different field names. This PR refactors the code to use a single `Delegation` type to represent delegations. Various delegations are distinguished between one another because of their `type`. <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### 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... - [x] 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 - [x] targeted the correct branch (see [PR Targeting](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://docs.cosmos.network/v0.44/building-modules/intro.html) - [x] included the necessary unit and integration [tests](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] 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
Showing
24 changed files
with
756 additions
and
2,266 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,64 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
storetypes "cosmossdk.io/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/milkyway-labs/milkyway/x/restaking/types" | ||
) | ||
|
||
// GetAllPoolDelegations returns all the pool delegations | ||
func (k *Keeper) GetAllPoolDelegations(ctx sdk.Context) []types.PoolDelegation { | ||
func (k *Keeper) GetAllPoolDelegations(ctx sdk.Context) []types.Delegation { | ||
store := ctx.KVStore(k.storeKey) | ||
iterator := store.Iterator(types.PoolDelegationPrefix, storetypes.PrefixEndBytes(types.PoolDelegationPrefix)) | ||
defer iterator.Close() | ||
|
||
var delegations []types.PoolDelegation | ||
var delegations []types.Delegation | ||
for ; iterator.Valid(); iterator.Next() { | ||
delegation := types.MustUnmarshalPoolDelegation(k.cdc, iterator.Value()) | ||
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value()) | ||
delegations = append(delegations, delegation) | ||
} | ||
|
||
return delegations | ||
} | ||
|
||
// GetAllDelegatorPoolDelegations returns all the pool delegations of a given delegator | ||
func (k *Keeper) GetAllDelegatorPoolDelegations(ctx sdk.Context, delegator string) []types.PoolDelegation { | ||
// GetAllOperatorDelegations returns all the operator delegations | ||
func (k *Keeper) GetAllOperatorDelegations(ctx sdk.Context) []types.Delegation { | ||
store := ctx.KVStore(k.storeKey) | ||
delegatorPrefixKey := types.UserPoolDelegationsStorePrefix(delegator) | ||
|
||
iterator := store.Iterator(delegatorPrefixKey, storetypes.PrefixEndBytes(delegatorPrefixKey)) // Smallest to largest | ||
iterator := store.Iterator(types.OperatorDelegationPrefix, storetypes.PrefixEndBytes(types.OperatorDelegationPrefix)) | ||
defer iterator.Close() | ||
|
||
var delegations []types.PoolDelegation | ||
var delegations []types.Delegation | ||
for ; iterator.Valid(); iterator.Next() { | ||
delegation := types.MustUnmarshalPoolDelegation(k.cdc, iterator.Value()) | ||
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value()) | ||
delegations = append(delegations, delegation) | ||
} | ||
|
||
return delegations | ||
} | ||
|
||
// GetPoolDelegations returns all the delegations to a given pool | ||
func (k *Keeper) GetPoolDelegations(ctx sdk.Context, poolID uint32) ([]types.PoolDelegation, error) { | ||
store := ctx.KVStore(k.storeKey) | ||
prefix := types.DelegationsByPoolIDStorePrefix(poolID) | ||
iterator := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) | ||
defer iterator.Close() | ||
|
||
var delegations []types.PoolDelegation | ||
for ; iterator.Valid(); iterator.Next() { | ||
_, delegatorAddress, err := types.ParseDelegationsByPoolIDKey(iterator.Key()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
delegation, found := k.GetPoolDelegation(ctx, poolID, delegatorAddress) | ||
if !found { | ||
return nil, fmt.Errorf("delegation not found for pool %d and delegator %s", poolID, delegatorAddress) | ||
} | ||
|
||
delegations = append(delegations, delegation) | ||
} | ||
|
||
return delegations, nil | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
func (k *Keeper) GetAllOperatorDelegations(ctx sdk.Context) []types.OperatorDelegation { | ||
// GetAllServiceDelegations returns all the service delegations | ||
func (k *Keeper) GetAllServiceDelegations(ctx sdk.Context) []types.Delegation { | ||
store := ctx.KVStore(k.storeKey) | ||
iterator := store.Iterator(types.OperatorDelegationPrefix, storetypes.PrefixEndBytes(types.OperatorDelegationPrefix)) | ||
iterator := store.Iterator(types.ServiceDelegationPrefix, storetypes.PrefixEndBytes(types.ServiceDelegationPrefix)) | ||
defer iterator.Close() | ||
|
||
var delegations []types.OperatorDelegation | ||
var delegations []types.Delegation | ||
for ; iterator.Valid(); iterator.Next() { | ||
delegation := types.MustUnmarshalOperatorDelegation(k.cdc, iterator.Value()) | ||
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value()) | ||
delegations = append(delegations, delegation) | ||
} | ||
|
||
return delegations | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
func (k *Keeper) GetAllServiceDelegations(ctx sdk.Context) []types.ServiceDelegation { | ||
store := ctx.KVStore(k.storeKey) | ||
iterator := store.Iterator(types.ServiceDelegationPrefix, storetypes.PrefixEndBytes(types.ServiceDelegationPrefix)) | ||
defer iterator.Close() | ||
// GetAllDelegations returns all the delegations | ||
func (k *Keeper) GetAllDelegations(ctx sdk.Context) []types.Delegation { | ||
var delegations []types.Delegation | ||
|
||
var delegations []types.ServiceDelegation | ||
for ; iterator.Valid(); iterator.Next() { | ||
delegation := types.MustUnmarshalServiceDelegation(k.cdc, iterator.Value()) | ||
delegations = append(delegations, delegation) | ||
} | ||
delegations = append(delegations, k.GetAllPoolDelegations(ctx)...) | ||
delegations = append(delegations, k.GetAllOperatorDelegations(ctx)...) | ||
delegations = append(delegations, k.GetAllServiceDelegations(ctx)...) | ||
|
||
return delegations | ||
} |
Oops, something went wrong.