Skip to content

Commit

Permalink
adds last valset request before height query (#365)
Browse files Browse the repository at this point in the history
* adds last valset request before  height query

* regenerating correct query.pb.go

* adds check before querying next valset

* Update x/qgb/keeper/query_valset.go

Co-authored-by: John Adler <[email protected]>

* adds comment

Co-authored-by: John Adler <[email protected]>
  • Loading branch information
rach-id and adlerjohn authored Apr 28, 2022
1 parent 4fe09dc commit 0e5db96
Show file tree
Hide file tree
Showing 4 changed files with 595 additions and 79 deletions.
13 changes: 13 additions & 0 deletions proto/qgb/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ service Query {
returns (QueryValsetConfirmResponse) {
option (google.api.http).get = "/qgb/valset/confirm";
}
// LastValsetBeforeHeight Queries last Valset before height
rpc LastValsetBeforeHeight(QueryLastValsetBeforeHeightRequest)
returns (QueryLastValsetBeforeHeightResponse) {
option (google.api.http).get = "/qgb/valset/request/before/{height}";
}
// ValsetConfirmsByNonce
rpc ValsetConfirmsByNonce(QueryValsetConfirmsByNonceRequest)
returns (QueryValsetConfirmsByNonceResponse) {
Expand Down Expand Up @@ -123,6 +128,14 @@ message QueryValsetConfirmRequest {
// QueryValsetConfirmResponse ValSet confirm query response
message QueryValsetConfirmResponse { MsgValsetConfirm confirm = 1; }

// QueryLastValsetRequestBeforeHeightRequest last Valset request before height
// request
message QueryLastValsetBeforeHeightRequest { uint64 height = 1; }

// QueryLastValsetRequestBeforeHeightResponse last Valset request before height
// response
message QueryLastValsetBeforeHeightResponse { Valset valset = 1; }

// QueryDataCommitmentConfirmsRequest Data commitment confirm by commitment and
// validator address request
message QueryDataCommitmentConfirmRequest {
Expand Down
26 changes: 26 additions & 0 deletions x/qgb/keeper/query_valset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// TODO add unit tests for all of these requests
// LastValsetRequests queries the LastValsetRequests of the qgb module
func (k Keeper) LastValsetRequests(
c context.Context,
Expand Down Expand Up @@ -41,3 +42,28 @@ func (k Keeper) ValsetRequestByNonce(

return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "valset request nonce not found")
}

// LastValsetBeforeHeight queries the last valset request before height
func (k Keeper) LastValsetBeforeHeight(
c context.Context,
req *types.QueryLastValsetBeforeHeightRequest) (*types.QueryLastValsetBeforeHeightResponse, error) {
valReq := k.GetValsets(sdk.UnwrapSDKContext(c))
for _, valset := range valReq {
// The first check is correct because we will always have a valset at block 0.
// We're creating valsets:
// - If we have no valset
// - We're an unbonding height
// - There was a significant power difference in the validator set
// For more information, check qgb/abci.go.EndBlocker:42
if !k.HasValsetRequest(sdk.UnwrapSDKContext(c), valset.Nonce+1) ||
(valset.Height < req.Height && k.GetValset(sdk.UnwrapSDKContext(c), valset.Nonce+1).Height >= req.Height) {
vs, err := types.CopyValset(valset)
if err != nil {
return nil, err
}
return &types.QueryLastValsetBeforeHeightResponse{Valset: vs}, nil
}
}

return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "last valset request before height not found")
}
Loading

0 comments on commit 0e5db96

Please sign in to comment.