Skip to content

Commit

Permalink
add query to retrieve params for height
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec committed Dec 3, 2024
1 parent 47b28f3 commit 1f856cf
Show file tree
Hide file tree
Showing 5 changed files with 662 additions and 122 deletions.
16 changes: 16 additions & 0 deletions proto/babylon/btcstaking/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ service Query {
option (google.api.http).get = "/babylon/btcstaking/v1/params/{version}";
}

// ParamsByBTCHeight queries the parameters of the module for a specific BTC height
rpc ParamsByBTCHeight(QueryParamsByBTCHeightRequest) returns (QueryParamsByBTCHeightResponse) {
option (google.api.http).get = "/babylon/btcstaking/v1/params/btc_height/{btc_height}";
}

// FinalityProviders queries all finality providers
rpc FinalityProviders(QueryFinalityProvidersRequest) returns (QueryFinalityProvidersResponse) {
option (google.api.http).get = "/babylon/btcstaking/v1/finality_providers";
Expand Down Expand Up @@ -69,6 +74,17 @@ message QueryParamsByVersionResponse {
Params params = 1 [(gogoproto.nullable) = false];
}

// QueryParamsByBTCHeightRequest is request type for the Query/ParamsByBTCHeight RPC method.
message QueryParamsByBTCHeightRequest {
uint32 btc_height = 1;
}

// QueryParamsByBTCHeightResponse is response type for the Query/QueryParamsByBTCHeightResponse RPC method.
message QueryParamsByBTCHeightResponse {
// params holds all the parameters of this module.
Params params = 1 [(gogoproto.nullable) = false];
}

// QueryFinalityProvidersRequest is the request type for the
// Query/FinalityProviders RPC method.
message QueryFinalityProvidersRequest {
Expand Down
17 changes: 17 additions & 0 deletions x/btcstaking/keeper/query_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,20 @@ func (k Keeper) ParamsByVersion(goCtx context.Context, req *types.QueryParamsByV

return &types.QueryParamsByVersionResponse{Params: *pv}, nil
}

func (k Keeper) ParamsByBTCHeight(
goCtx context.Context,
req *types.QueryParamsByBTCHeightRequest,
) (*types.QueryParamsByBTCHeightResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)

p, _, err := k.GetParamsForBtcHeight(ctx, uint64(req.BtcHeight))
if err != nil {
return nil, types.ErrParamsNotFound.Wrapf("params for btc height %d not found", req.BtcHeight)
}

return &types.QueryParamsByBTCHeightResponse{Params: *p}, nil
}
35 changes: 35 additions & 0 deletions x/btcstaking/keeper/query_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,38 @@ func TestParamsByVersionQuery(t *testing.T) {
require.NoError(t, err)
require.Equal(t, &types.QueryParamsByVersionResponse{Params: params3}, resp2)
}

func TestParamsByBTCHeightQuery(t *testing.T) {
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
currParams := keeper.GetParams(ctx)

// starting with `1` as BTCStakingKeeper creates params with version 0
params1 := types.DefaultParams()
params1.MinUnbondingTimeBlocks = 10000
params1.BtcActivationHeight = currParams.BtcActivationHeight + 1

params2 := types.DefaultParams()
params2.MinUnbondingTimeBlocks = 20000
params2.BtcActivationHeight = currParams.BtcActivationHeight + 2

// Check that after update we always return the latest version of params through Params query
err := keeper.SetParams(ctx, params1)
require.NoError(t, err)
response, err := keeper.Params(ctx, &types.QueryParamsRequest{})
require.NoError(t, err)
require.Equal(t, &types.QueryParamsResponse{Params: params1}, response)

err = keeper.SetParams(ctx, params2)
require.NoError(t, err)
response, err = keeper.Params(ctx, &types.QueryParamsRequest{})
require.NoError(t, err)
require.Equal(t, &types.QueryParamsResponse{Params: params2}, response)

resp0, err := keeper.ParamsByBTCHeight(ctx, &types.QueryParamsByBTCHeightRequest{BtcHeight: params1.BtcActivationHeight})
require.NoError(t, err)
require.Equal(t, &types.QueryParamsByBTCHeightResponse{Params: params1}, resp0)

resp1, err := keeper.ParamsByBTCHeight(ctx, &types.QueryParamsByBTCHeightRequest{BtcHeight: params2.BtcActivationHeight})
require.NoError(t, err)
require.Equal(t, &types.QueryParamsByBTCHeightResponse{Params: params2}, resp1)
}
Loading

0 comments on commit 1f856cf

Please sign in to comment.