Skip to content
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

x/upgrade gRPC query service #6590

Merged
merged 11 commits into from
Jul 4, 2020
36 changes: 36 additions & 0 deletions proto/cosmos/upgrade/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
syntax = "proto3";
package cosmos.upgrade;

import "cosmos/upgrade/upgrade.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/upgrade/types";

// Query provides defines the gRPC querier service
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
service Query {
// CurrentPlan queries the current plan
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
rpc CurrentPlan(QueryCurrentPlanRequest) returns (QueryCurrentPlanResponse) {}

// AppliedPlan queries a previously applied plan by its name
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
rpc AppliedPlan(QueryAppliedPlanRequest) returns (QueryAppliedPlanResponse) {}
}

// QueryCurrentPlanRequest is the request type for the Query/CurrentPlan RPC method
message QueryCurrentPlanRequest {}

// QueryCurrentPlanResponse is the response type for the Query/CurrentPlan RPC method
message QueryCurrentPlanResponse {
// plan is the current plan
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
Plan plan = 1;
}

// QueryCurrentPlanRequest is the request type for the Query/AppliedPlan RPC method
message QueryAppliedPlanRequest {
// name is the name of the applied plan to query for
string name = 1;
}

// QueryAppliedPlanResponse is the response type for the Query/AppliedPlan RPC method
message QueryAppliedPlanResponse {
// height is the block height at which the plan was applied
int64 height = 1;
}
34 changes: 34 additions & 0 deletions x/upgrade/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

var _ types.QueryServer = Keeper{}

// CurrentPlan implements the Query/CurrentPlan gRPC method
func (k Keeper) CurrentPlan(c context.Context, req *types.QueryCurrentPlanRequest) (*types.QueryCurrentPlanResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

plan, found := k.GetUpgradePlan(ctx)
if !found {
return &types.QueryCurrentPlanResponse{}, nil
}

return &types.QueryCurrentPlanResponse{Plan: &plan}, nil
}

// AppliedPlan implements the Query/AppliedPlan gRPC method
func (k Keeper) AppliedPlan(c context.Context, req *types.QueryAppliedPlanRequest) (*types.QueryAppliedPlanResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

applied := k.GetDoneHeight(ctx, req.Name)
if applied == 0 {
return &types.QueryAppliedPlanResponse{}, nil
}

return &types.QueryAppliedPlanResponse{Height: applied}, nil
}
43 changes: 43 additions & 0 deletions x/upgrade/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package keeper_test

import (
gocontext "context"
"testing"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
)

func TestGRPCQueryUpgrade(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})

queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.UpgradeKeeper)
queryClient := types.NewQueryClient(queryHelper)

t.Log("Verify that the scheduled upgrade plan can be queried")
plan := types.Plan{Name: "test-plan", Height: 5}
app.UpgradeKeeper.ScheduleUpgrade(ctx, plan)

res, err := queryClient.CurrentPlan(gocontext.Background(), &types.QueryCurrentPlanRequest{})
require.NoError(t, err)
require.Equal(t, res.Plan, &plan)

t.Log("Verify that the upgrade plan can be successfully applied and queried")
ctx = ctx.WithBlockHeight(5)
app.UpgradeKeeper.SetUpgradeHandler("test-plan", func(ctx sdk.Context, plan types.Plan) {})
app.UpgradeKeeper.ApplyUpgrade(ctx, plan)

res, err = queryClient.CurrentPlan(gocontext.Background(), &types.QueryCurrentPlanRequest{})
require.NoError(t, err)
require.Nil(t, res.Plan)

appliedRes, appliedErr := queryClient.AppliedPlan(gocontext.Background(), &types.QueryAppliedPlanRequest{Name: "test-plan"})
require.NoError(t, appliedErr)
require.Equal(t, int64(5), appliedRes.Height)
}
10 changes: 10 additions & 0 deletions x/upgrade/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ const (
QueryApplied = "applied"
)

// NewQueryCurrentPlanRequest creates a new instance of QueryCurrentPlanRequest.
func NewQueryCurrentPlanRequest() *QueryCurrentPlanRequest {
return &QueryCurrentPlanRequest{}
}

// NewQueryAppliedPlanRequest creates a new instance of QueryAppliedPlanRequest.
func NewQueryAppliedPlanRequest(name string) *QueryAppliedPlanRequest {
return &QueryAppliedPlanRequest{Name: name}
}

// QueryAppliedParams is passed as data with QueryApplied
type QueryAppliedParams struct {
Name string
Expand Down
Loading