Skip to content

Commit

Permalink
Problem: block time is not included in grpc rsp
Browse files Browse the repository at this point in the history
add cfg
  • Loading branch information
mmsqe committed Nov 27, 2023
1 parent eec0174 commit 3c523b6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
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"
)

0 comments on commit 3c523b6

Please sign in to comment.