-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
R4R: Querier - Get all delegations to validator #2565
Merged
jackzampolin
merged 19 commits into
develop
from
sunny/get_all_delegations_validator_querier
Nov 13, 2018
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d378db3
added querier redelegation
sunnya97 9c6ef3e
added validatorDelegations querier endpoint
sunnya97 2d42973
LCD and CLI
sunnya97 f8b030a
cli fixes
sunnya97 eeff820
removed redelegation stuff
sunnya97 327f7e1
address other comments
sunnya97 0fc536a
rebased
sunnya97 4c3ac3f
addressed comments
sunnya97 dfddac3
Merge branch 'develop' into sunny/get_all_delegations_validator_querier
cwgoes 080a781
Update x/stake/client/rest/query.go
fedekunze cc652a3
addressed fede's comment
sunnya97 4d1246d
Merge branch 'develop' into sunny/get_all_delegations_validator_querier
cwgoes 182f111
update swagger.yaml
b255a7d
Merge branch 'develop' into sunny/get_all_delegations_validator_querier
jackzampolin 7dfeb7d
use newQuery...Params
a6af76e
Merge branch 'sunny/get_all_delegations_validator_querier' of https:/…
695395a
Merge branch 'develop' into sunny/get_all_delegations_validator_querier
jackzampolin 4ebe99b
add back in CLI command after rebase
jackzampolin f38f57d
Fix CLI tests
jackzampolin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
|
@@ -37,6 +37,21 @@ func (k Keeper) GetAllDelegations(ctx sdk.Context) (delegations []types.Delegati | |
return delegations | ||
} | ||
|
||
// return all delegations to a specific validator. Useful for querier. | ||
func (k Keeper) GetValidatorDelegations(ctx sdk.Context, valAddr sdk.ValAddress) (delegations []types.Delegation) { | ||
store := ctx.KVStore(k.storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, DelegationKey) | ||
defer iterator.Close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because this is such a simple function, we should probably just close the iterator after the for loop and avoid using a |
||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) | ||
if delegation.GetValidatorAddr().Equals(valAddr) { | ||
delegations = append(delegations, delegation) | ||
} | ||
} | ||
return delegations | ||
} | ||
|
||
// return 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) { | ||
|
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ const ( | |
QueryDelegatorDelegations = "delegatorDelegations" | ||
QueryDelegatorUnbondingDelegations = "delegatorUnbondingDelegations" | ||
QueryDelegatorRedelegations = "delegatorRedelegations" | ||
QueryValidatorDelegations = "validatorDelegations" | ||
QueryValidatorUnbondingDelegations = "validatorUnbondingDelegations" | ||
QueryValidatorRedelegations = "validatorRedelegations" | ||
QueryDelegator = "delegator" | ||
|
@@ -34,6 +35,8 @@ func NewQuerier(k keep.Keeper, cdc *codec.Codec) sdk.Querier { | |
return queryValidators(ctx, cdc, k) | ||
case QueryValidator: | ||
return queryValidator(ctx, cdc, req, k) | ||
case QueryValidatorDelegations: | ||
return queryValidatorDelegations(ctx, cdc, req, k) | ||
case QueryValidatorUnbondingDelegations: | ||
return queryValidatorUnbondingDelegations(ctx, cdc, req, k) | ||
case QueryValidatorRedelegations: | ||
|
@@ -73,6 +76,7 @@ type QueryDelegatorParams struct { | |
|
||
// defines the params for the following queries: | ||
// - 'custom/stake/validator' | ||
// - 'custom/stake/validatorDelegations' | ||
// - 'custom/stake/validatorUnbondingDelegations' | ||
// - 'custom/stake/validatorRedelegations' | ||
type QueryValidatorParams struct { | ||
|
@@ -88,6 +92,28 @@ type QueryBondsParams struct { | |
ValidatorAddr sdk.ValAddress | ||
} | ||
|
||
// creates a new QueryDelegatorParams | ||
func NewQueryDelegatorParams(delegatorAddr sdk.AccAddress) QueryDelegatorParams { | ||
return QueryDelegatorParams{ | ||
DelegatorAddr: delegatorAddr, | ||
} | ||
} | ||
|
||
// creates a new QueryValidatorParams | ||
func NewQueryValidatorParams(validatorAddr sdk.ValAddress) QueryValidatorParams { | ||
return QueryValidatorParams{ | ||
ValidatorAddr: validatorAddr, | ||
} | ||
} | ||
|
||
// creates a new QueryBondsParams | ||
func NewQueryBondsParams(delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress) QueryBondsParams { | ||
return QueryBondsParams{ | ||
DelegatorAddr: delegatorAddr, | ||
ValidatorAddr: validatorAddr, | ||
} | ||
} | ||
|
||
func queryValidators(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) (res []byte, err sdk.Error) { | ||
stakeParams := k.GetParams(ctx) | ||
validators := k.GetValidators(ctx, stakeParams.MaxValidators) | ||
|
@@ -119,6 +145,23 @@ func queryValidator(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k | |
return res, nil | ||
} | ||
|
||
func queryValidatorDelegations(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) { | ||
var params QueryValidatorParams | ||
|
||
errRes := cdc.UnmarshalJSON(req.Data, ¶ms) | ||
fedekunze marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if errRes != nil { | ||
return []byte{}, sdk.ErrUnknownAddress("") | ||
} | ||
|
||
delegations := k.GetValidatorDelegations(ctx, params.ValidatorAddr) | ||
|
||
res, errRes = codec.MarshalJSONIndent(cdc, delegations) | ||
if errRes != nil { | ||
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. split this line up? |
||
} | ||
return res, nil | ||
} | ||
|
||
func queryValidatorUnbondingDelegations(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) { | ||
var params QueryValidatorParams | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like there should be a helper function like
StakeValidatorsDelegationsPath(validatorAddr.String())
https://guides.rubyonrails.org/routing.html#path-and-url-helpers
In Ruby, you can use magic to create such functions on the fly. In Go, we'd need to write them ourselves. Still, there may be a benefit in having them.