Skip to content

Commit

Permalink
feat: gRPC query for operator and chain configuration (backport cosmo…
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored and JeancarloBarrios committed Sep 28, 2024
1 parent 93091e4 commit 93ab9eb
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 857 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Features

* (grpc) [#13485](https://github.com/cosmos/cosmos-sdk/pull/13485) Implement a new gRPC query, `/cosmos/base/node/v1beta1/config`, which provides operator configuration.

### Improvements

* [#13369](https://github.com/cosmos/cosmos-sdk/pull/13369) Improve UX for `keyring.List` by returning all retrieved keys.
Expand Down
714 changes: 22 additions & 692 deletions client/grpc/node/query.pb.go

Large diffs are not rendered by default.

72 changes: 1 addition & 71 deletions client/grpc/node/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 6 additions & 29 deletions client/grpc/node/service.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package node

import (
"context"
context "context"

gogogrpc "github.com/cosmos/gogoproto/grpc"
gogogrpc "github.com/gogo/protobuf/grpc"
"github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// RegisterNodeService registers the node gRPC service on the provided gRPC router.
func RegisterNodeService(clientCtx client.Context, server gogogrpc.Server, cfg config.Config) {
RegisterServiceServer(server, NewQueryServer(clientCtx, cfg))
func RegisterNodeService(clientCtx client.Context, server gogogrpc.Server) {
RegisterServiceServer(server, NewQueryServer(clientCtx))
}

// RegisterGRPCGatewayRoutes mounts the node gRPC service's GRPC-gateway routes
Expand All @@ -26,40 +25,18 @@ var _ ServiceServer = queryServer{}

type queryServer struct {
clientCtx client.Context
cfg config.Config
}

func NewQueryServer(clientCtx client.Context, cfg config.Config) ServiceServer {
func NewQueryServer(clientCtx client.Context) ServiceServer {
return queryServer{
clientCtx: clientCtx,
cfg: cfg,
}
}

func (s queryServer) Config(ctx context.Context, _ *ConfigRequest) (*ConfigResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

return &ConfigResponse{
MinimumGasPrice: sdkCtx.MinGasPrices().String(),
PruningKeepRecent: s.cfg.PruningKeepRecent,
PruningInterval: s.cfg.PruningInterval,
HaltHeight: s.cfg.HaltHeight,
}, nil
}

func (s queryServer) Status(ctx context.Context, _ *StatusRequest) (*StatusResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

blockTime := sdkCtx.HeaderInfo().Time

return &StatusResponse{
// TODO: Get earliest version from store.
//
// Ref: ...
// EarliestStoreHeight: sdkCtx.MultiStore(),
Height: uint64(sdkCtx.BlockHeight()),
Timestamp: &blockTime,
AppHash: sdkCtx.BlockHeader().AppHash,
ValidatorHash: sdkCtx.BlockHeader().NextValidatorsHash,
MinimumGasPrice: sdkCtx.MinGasPrices().String(),
}, nil
}
18 changes: 7 additions & 11 deletions client/grpc/node/service_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package node

import (
context "context"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestServiceServer_Config(t *testing.T) {
defaultCfg := config.DefaultConfig()
defaultCfg.PruningKeepRecent = "2000"
defaultCfg.PruningInterval = "10"
defaultCfg.HaltHeight = 100
svr := NewQueryServer(client.Context{}, *defaultCfg)
ctx := sdk.Context{}.WithMinGasPrices(sdk.NewDecCoins(sdk.NewInt64DecCoin("stake", 15)))
svr := NewQueryServer(client.Context{})
ctx := sdk.Context{}.
WithMinGasPrices(sdk.NewDecCoins(sdk.NewInt64DecCoin("stake", 15))).
WithContext(context.Background())
goCtx := sdk.WrapSDKContext(ctx)

resp, err := svr.Config(ctx, &ConfigRequest{})
resp, err := svr.Config(goCtx, &ConfigRequest{})
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, ctx.MinGasPrices().String(), resp.MinimumGasPrice)
require.Equal(t, defaultCfg.PruningKeepRecent, resp.PruningKeepRecent)
require.Equal(t, defaultCfg.PruningInterval, resp.PruningInterval)
require.Equal(t, defaultCfg.HaltHeight, resp.HaltHeight)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ require (
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.0
google.golang.org/grpc v1.49.0
google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v2 v2.4.0
)

Expand Down Expand Up @@ -114,7 +114,7 @@ require (
github.com/zondax/hid v0.9.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/net v0.0.0-20220726230323-06994584191e // indirect
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -961,8 +961,8 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 h1:dyU22nBWzrmTQxtNrr4dzVOvaw35nUYE279vF9UmsI8=
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
Expand Down Expand Up @@ -1122,8 +1122,8 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
23 changes: 1 addition & 22 deletions proto/cosmos/base/node/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ syntax = "proto3";
package cosmos.base.node.v1beta1;

import "google/api/annotations.proto";
import "google/protobuf/timestamp.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/cosmos/cosmos-sdk/client/grpc/node";

Expand All @@ -13,31 +11,12 @@ service Service {
rpc Config(ConfigRequest) returns (ConfigResponse) {
option (google.api.http).get = "/cosmos/base/node/v1beta1/config";
}
// Status queries for the node status.
rpc Status(StatusRequest) returns (StatusResponse) {
option (google.api.http).get = "/cosmos/base/node/v1beta1/status";
}
}

// ConfigRequest defines the request structure for the Config gRPC query.
message ConfigRequest {}

// ConfigResponse defines the response structure for the Config gRPC query.
message ConfigResponse {
string minimum_gas_price = 1;
string pruning_keep_recent = 2;
string pruning_interval = 3;
uint64 halt_height = 4;
}

// StatusRequest defines the request structure for the status of a node.
message StatusRequest {}

// StatusResponse defines the response structure for the status of a node.
message StatusResponse {
uint64 earliest_store_height = 1; // earliest block height available in the store
uint64 height = 2; // current block height
google.protobuf.Timestamp timestamp = 3 [(gogoproto.stdtime) = true]; // block height timestamp
bytes app_hash = 4; // app hash of the current block
bytes validator_hash = 5; // validator hash provided by the consensus header
string minimum_gas_price = 1;
}
16 changes: 4 additions & 12 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,19 +396,11 @@ func startCmtNode(
// service if API or gRPC is enabled, and avoid doing so in the general
// case, because it spawns a new local tendermint RPC client.
if (config.API.Enable || config.GRPC.Enable) && tmNode != nil {
clientCtx = clientCtx.WithClient(local.New(tmNode))
clientCtx := clientCtx.WithClient(local.New(tmNode))

return tmNode, cleanupFn, nil
}

func getAndValidateConfig(svrCtx *Context) (serverconfig.Config, error) {
config, err := serverconfig.GetConfig(svrCtx.Viper)
if err != nil {
return config, err
}

if err := config.ValidateBasic(); err != nil {
return config, err
app.RegisterTxService(clientCtx)
app.RegisterTendermintService(clientCtx)
app.RegisterNodeService(clientCtx)
}
return config, nil
}
Expand Down
7 changes: 2 additions & 5 deletions server/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ type (
// simulation, fetching txs by hash...).
RegisterTxService(client.Context)

// RegisterTendermintService registers the gRPC Query service for CometBFT queries.
// RegisterTendermintService registers the gRPC Query service for tendermint queries.
RegisterTendermintService(client.Context)

// RegisterNodeService registers the node gRPC Query service.
RegisterNodeService(client.Context, config.Config)

// RegisterTendermintService registers the gRPC Query service for tendermint queries.
RegisterTendermintService(clientCtx client.Context)
RegisterNodeService(client.Context)

// CommitMultiStore Returns the multistore instance
CommitMultiStore() sdk.CommitMultiStore
Expand Down
15 changes: 11 additions & 4 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
nodeservice "github.com/cosmos/cosmos-sdk/client/grpc/node"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -749,10 +749,17 @@ func (app *SimApp) RegisterTendermintService(clientCtx client.Context) {
)
}

func (app *SimApp) RegisterNodeService(clientCtx client.Context, cfg config.Config) {
nodeservice.RegisterNodeService(clientCtx, app.GRPCQueryRouter(), cfg)
func (app *SimApp) RegisterNodeService(clientCtx client.Context) {
nodeservice.RegisterNodeService(clientCtx, app.GRPCQueryRouter())
}

// RegisterSwaggerAPI registers swagger route with API Server
func RegisterSwaggerAPI(ctx client.Context, rtr *mux.Router) {
statikFS, err := fs.New()
if err != nil {
panic(err)
}

// ValidatorKeyProvider returns a function that generates a validator key
// Supported key types are those supported by Comet: ed25519, secp256k1, bls12-381
func (app *SimApp) ValidatorKeyProvider() runtime.KeyGenF {
Expand Down
Loading

0 comments on commit 93ab9eb

Please sign in to comment.