Skip to content

Commit

Permalink
x/upgrade gRPC query service (#6590)
Browse files Browse the repository at this point in the history
* Add .proto and make proto-gen

* Correct proto types

* Update pseudo tests

* Update tests

* line break

* Fix tests after merge

* Update proto/cosmos/upgrade/query.proto

Co-authored-by: Aaron Craelius <[email protected]>

* Update proto/cosmos/upgrade/query.proto

Co-authored-by: Aaron Craelius <[email protected]>

* Update proto/cosmos/upgrade/query.proto

Co-authored-by: Aaron Craelius <[email protected]>

* Update proto/cosmos/upgrade/query.proto

Co-authored-by: Aaron Craelius <[email protected]>

Co-authored-by: Aaron Craelius <[email protected]>
  • Loading branch information
amaury1093 and aaronc authored Jul 4, 2020
1 parent 80e53a4 commit 4459201
Show file tree
Hide file tree
Showing 5 changed files with 1,041 additions and 0 deletions.
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 defines the gRPC upgrade querier service
service Query {
// CurrentPlan queries the current upgrade plan
rpc CurrentPlan(QueryCurrentPlanRequest) returns (QueryCurrentPlanResponse) {}

// AppliedPlan queries a previously applied upgrade plan by its name
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 upgrade plan
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

0 comments on commit 4459201

Please sign in to comment.