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

Problem: block time is not included in grpc rsp #196

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion baseapp/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down
15 changes: 11 additions & 4 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###
###############################################################################
Expand Down
4 changes: 1 addition & 3 deletions server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions server/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions types/grpc/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Loading