From 3c523b6081b576584522118d061e6c3a085fd1c7 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 27 Nov 2023 09:22:28 +0800 Subject: [PATCH 1/2] Problem: block time is not included in grpc rsp add cfg --- baseapp/grpcserver.go | 14 +++++++++++++- server/config/config.go | 15 +++++++++++---- server/config/toml.go | 3 +++ server/grpc/server.go | 4 +--- server/types/app.go | 4 ++++ types/grpc/headers.go | 2 ++ 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/baseapp/grpcserver.go b/baseapp/grpcserver.go index d04f71d1abce..c1b39c017c08 100644 --- a/baseapp/grpcserver.go +++ b/baseapp/grpcserver.go @@ -22,6 +22,12 @@ func (app *BaseApp) GRPCQueryRouter() *GRPCQueryRouter { return app.grpcQueryRou // RegisterGRPCServer registers gRPC services directly with the gRPC server. func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) { + app.RegisterGRPCServerWitMDBlockTime(server, false) +} + +// RegisterGRPCServer registers gRPC services directly with the gRPC +// server and config if block time is included in every response header. +func (app *BaseApp) RegisterGRPCServerWitMDBlockTime(server gogogrpc.Server, mdWithBlockTime bool) { // Define an interceptor for all gRPC queries: this interceptor will create // a new sdk.Context, and pass it into the query handler. interceptor := func(grpcCtx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { @@ -64,7 +70,13 @@ func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) { if err = grpc.SetHeader(grpcCtx, md); err != nil { app.logger.Error("failed to set gRPC header", "err", err) } - + if mdWithBlockTime { + time := sdkCtx.BlockHeader().Time.Unix() + md = metadata.Pairs(grpctypes.GRPCBlockTimeHeader, strconv.FormatInt(time, 10)) + if err = grpc.SetHeader(grpcCtx, md); err != nil { + app.logger.Error("failed to set gRPC header time", "err", err) + } + } return handler(grpcCtx, req) } diff --git a/server/config/config.go b/server/config/config.go index 3e01afe60757..6bd835fa6dab 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -34,6 +34,10 @@ const ( // bytes the server can send. DefaultGRPCMaxSendMsgSize = math.MaxInt32 + // DefaultMDWithBlockTime defines the default gRPC response including block time in + // header (i.e false) + DefaultMDWithBlockTime = false + // FileStreamer defines the store streaming type for file streaming. FileStreamer = "file" ) @@ -177,6 +181,8 @@ type GRPCConfig struct { // MaxSendMsgSize defines the max message size in bytes the server can send. // The default value is math.MaxInt32. MaxSendMsgSize int `mapstructure:"max-send-msg-size"` + + MDWithBlockTime bool `mapstructure:"md-with-block-time"` } // GRPCWebConfig defines configuration for the gRPC-web server. @@ -315,10 +321,11 @@ func DefaultConfig() *Config { RPCMaxBodyBytes: 1000000, }, GRPC: GRPCConfig{ - Enable: true, - Address: DefaultGRPCAddress, - MaxRecvMsgSize: DefaultGRPCMaxRecvMsgSize, - MaxSendMsgSize: DefaultGRPCMaxSendMsgSize, + Enable: true, + Address: DefaultGRPCAddress, + MaxRecvMsgSize: DefaultGRPCMaxRecvMsgSize, + MaxSendMsgSize: DefaultGRPCMaxSendMsgSize, + MDWithBlockTime: DefaultMDWithBlockTime, }, Rosetta: RosettaConfig{ Enable: false, diff --git a/server/config/toml.go b/server/config/toml.go index 870bcb618bdc..534210933bf2 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -207,6 +207,9 @@ max-recv-msg-size = "{{ .GRPC.MaxRecvMsgSize }}" # The default value is math.MaxInt32. max-send-msg-size = "{{ .GRPC.MaxSendMsgSize }}" +# MDWithBlockTime defines the gRPC response including block time in header or not. +md-with-block-time = {{ .GRPC.MDWithBlockTime }} + ############################################################################### ### gRPC Web Configuration ### ############################################################################### diff --git a/server/grpc/server.go b/server/grpc/server.go index 79a9be3dca24..fc073d9f3926 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -34,9 +34,7 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config grpc.MaxSendMsgSize(maxSendMsgSize), grpc.MaxRecvMsgSize(maxRecvMsgSize), ) - - app.RegisterGRPCServer(grpcSrv) - + app.RegisterGRPCServerWitMDBlockTime(grpcSrv, cfg.MDWithBlockTime) // Reflection allows consumers to build dynamic clients that can write to any // Cosmos SDK application without relying on application packages at compile // time. diff --git a/server/types/app.go b/server/types/app.go index 727f767fc35e..80e28710cc4d 100644 --- a/server/types/app.go +++ b/server/types/app.go @@ -48,6 +48,10 @@ type ( // server. RegisterGRPCServer(grpc.Server) + // RegisterGRPCServer registers gRPC services directly with the gRPC + // server and config if block time is included in every response header. + RegisterGRPCServerWitMDBlockTime(grpc.Server, bool) + // RegisterTxService registers the gRPC Query service for tx (such as tx // simulation, fetching txs by hash...). RegisterTxService(client.Context) diff --git a/types/grpc/headers.go b/types/grpc/headers.go index 091530795807..db9fbc20fea8 100644 --- a/types/grpc/headers.go +++ b/types/grpc/headers.go @@ -3,4 +3,6 @@ package grpc const ( // GRPCBlockHeightHeader is the gRPC header for block height. GRPCBlockHeightHeader = "x-cosmos-block-height" + // GRPCBlockTimeHeader is the gRPC header for block time. + GRPCBlockTimeHeader = "x-cosmos-block-time" ) From a017c45c85db719ae151ee1d5347868f603d3f4b Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 27 Nov 2023 09:40:03 +0800 Subject: [PATCH 2/2] Add doc --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad5540def3e5..cf67da583ce3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (client/rpc) [#17274](https://github.com/cosmos/cosmos-sdk/pull/17274) Add `QueryEventForTxCmd` cmd to subscribe and wait event for transaction by hash. * (keyring) [#17424](https://github.com/cosmos/cosmos-sdk/pull/17424) Allows to import private keys encoded in hex. +* (server) [#196](https://github.com/crypto-org-chain/cosmos-sdk/pull/196) Add `md-with-block-time` config for grpc server to include block time in response header. ### Improvements