-
Notifications
You must be signed in to change notification settings - Fork 138
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
feat: Add queries for PSS and consumer commission rate #1733
Changes from 34 commits
b0d4cf9
0162242
7ad2993
5cd5ba5
5393f19
03af237
07429fa
0ab0b32
1a0fa9e
56ca38d
c614fd2
6503f9b
330c085
629cfd1
afd4652
2e95f89
a9db491
1e3a8e7
149ba7a
24be8b2
b311f35
22ca561
5509108
ad91b0e
559b28f
00e43a9
4f9084f
d4fc4cb
938d930
9739ddd
c01ab08
4661a99
219d310
10dbb53
d642e8a
69d0b28
f9e63f0
3ae3031
ed402f8
c75594f
899f3ea
b1c1beb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package keeper | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
@@ -42,7 +43,6 @@ func (k Keeper) QueryConsumerChains(goCtx context.Context, req *types.QueryConsu | |
|
||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
// convert to array of pointers | ||
chains := []*types.Chain{} | ||
for _, chain := range k.GetAllConsumerChains(ctx) { | ||
// prevent implicit memory aliasing | ||
|
@@ -221,3 +221,104 @@ func (k Keeper) QueryParams(c context.Context, _ *types.QueryParamsRequest) (*ty | |
|
||
return &types.QueryParamsResponse{Params: params}, nil | ||
} | ||
|
||
// QueryConsumerChainOptedInValidators returns all validators that opted-in to a given consumer chain | ||
func (k Keeper) QueryConsumerChainOptedInValidators(goCtx context.Context, req *types.QueryConsumerChainOptedInValidatorsRequest) (*types.QueryConsumerChainOptedInValidatorsResponse, error) { | ||
if req == nil { | ||
return nil, status.Error(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
consumerChainID := req.ChainId | ||
if consumerChainID == "" { | ||
return nil, status.Error(codes.InvalidArgument, "empty chainId") | ||
} | ||
|
||
optedInVals := []string{} | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
if !k.IsConsumerProposedOrRegistered(ctx, consumerChainID) { | ||
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("unknown consumer chain: %s", consumerChainID)) | ||
} | ||
|
||
for _, v := range k.GetAllOptedIn(ctx, ctx.ChainID()) { | ||
optedInVals = append(optedInVals, v.ToSdkConsAddr().String()) | ||
} | ||
|
||
return &types.QueryConsumerChainOptedInValidatorsResponse{ | ||
ValidatorsProviderAddresses: optedInVals, | ||
}, nil | ||
} | ||
|
||
// QueryConsumerChainsValidatorHasToValidate returns all consumer chains a given validator has to validate | ||
func (k Keeper) QueryConsumerChainsValidatorHasToValidate(goCtx context.Context, req *types.QueryConsumerChainsValidatorHasToValidateRequest) (*types.QueryConsumerChainsValidatorHasToValidateResponse, error) { | ||
if req == nil { | ||
return nil, status.Error(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
if req.ProviderAddress == "" { | ||
return nil, status.Error(codes.InvalidArgument, "empty provider address") | ||
} | ||
|
||
consAddr, err := sdk.ConsAddressFromBech32(req.ProviderAddress) | ||
if err != nil { | ||
return nil, status.Error(codes.InvalidArgument, "invalid provider address") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
// get all the consumer chains for which the validator is already or will be | ||
// opted-in starting from the next epoch | ||
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. This comment doesn't match the code below, right? 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. I think it does. When 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. Hm, I guess you are right. I'm just a bit confused what should happen when someone has opted out, but is still currently in power on the consumer chain |
||
consumersToValidate := []string{} | ||
for _, consumer := range k.GetAllConsumerChains(ctx) { | ||
chainID := consumer.ChainId | ||
|
||
if k.IsOptedIn(ctx, chainID, types.NewProviderConsAddress(consAddr)) { | ||
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. Note that top N validators are only opted in right when the epoch begins, so this doesn't tell you who will get opted into the chain because of being in the top N. Of course we cannot know for sure, but I would change this to just compute the MinPowerToOptIn for the chain, and then compute the validators that would be opted in, if the epoch would end without further power changes 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. Good point. Will add that. Thanks! |
||
consumersToValidate = append(consumersToValidate, chainID) | ||
} | ||
} | ||
|
||
return &types.QueryConsumerChainsValidatorHasToValidateResponse{ | ||
ConsumerChainIds: consumersToValidate, | ||
}, nil | ||
} | ||
|
||
// QueryValidatorConsumerCommissionRate returns the commission rate a given | ||
// validator charges on a given consumer chain | ||
func (k Keeper) QueryValidatorConsumerCommissionRate(goCtx context.Context, req *types.QueryValidatorConsumerCommissionRateRequest) (*types.QueryValidatorConsumerCommissionRateResponse, error) { | ||
if req == nil { | ||
return nil, status.Error(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
consumerChainID := req.ChainId | ||
if consumerChainID == "" { | ||
return nil, status.Error(codes.InvalidArgument, "empty chainId") | ||
} | ||
|
||
consAddr, err := sdk.ConsAddressFromBech32(req.ProviderAddress) | ||
if err != nil { | ||
return nil, status.Error(codes.InvalidArgument, "invalid provider address") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
if !k.IsConsumerProposedOrRegistered(ctx, consumerChainID) { | ||
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("unknown consumer chain: %s", consumerChainID)) | ||
} | ||
|
||
res := &types.QueryValidatorConsumerCommissionRateResponse{} | ||
|
||
// Check if the validator has a commission rate set for the consumer chain, | ||
// otherwise use the commission rate from the validator staking module struct | ||
consumerRate, found := k.GetConsumerCommissionRate(ctx, consumerChainID, types.NewProviderConsAddress(consAddr)) | ||
if found { | ||
res.Rate = consumerRate | ||
} else { | ||
v, ok := k.stakingKeeper.GetValidatorByConsAddr(ctx, consAddr) | ||
if !ok { | ||
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("unknown validator: %s", consAddr.String())) | ||
} | ||
res.Rate = v.Commission.Rate | ||
} | ||
|
||
return res, nil | ||
} |
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.
Could we make this a bit more explicit?