Skip to content

Commit

Permalink
feat(server): allow align block header with skip check heaader in grp…
Browse files Browse the repository at this point in the history
…c server

ensure overwrite with latest header under grpc only or pruned node
  • Loading branch information
mmsqe committed Jan 8, 2025
1 parent 2b31145 commit d0a5250
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 9 deletions.
8 changes: 7 additions & 1 deletion baseapp/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import (

// RegisterGRPCServer registers gRPC services directly with the gRPC server.
func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) {
app.RegisterGRPCServerWithSkipCheckHeader(server, false)
}

// RegisterGRPCServerWithSkipCheckHeader registers gRPC services with the specified gRPC server
// and bypass check header flag.
func (app *BaseApp) RegisterGRPCServerWithSkipCheckHeader(server gogogrpc.Server, skipCheckHeader 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 All @@ -48,7 +54,7 @@ func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) {

// Create the sdk.Context. Passing false as 2nd arg, as we can't
// actually support proofs with gRPC right now.
sdkCtx, err := app.CreateQueryContext(height, false)
sdkCtx, err := app.CreateQueryContextWithCheckHeader(height, false, !skipCheckHeader)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ 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"`

// SkipCheckHeader defines if the gRPC server should bypass check header.
SkipCheckHeader bool `mapstructure:"skip-check-header"`
}

// StateSyncConfig defines the state sync snapshot configuration.
Expand Down
3 changes: 3 additions & 0 deletions server/config/config.toml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ max-recv-msg-size = "{{ .GRPC.MaxRecvMsgSize }}"
# The default value is math.MaxInt32.
max-send-msg-size = "{{ .GRPC.MaxSendMsgSize }}"

# SkipCheckHeader defines if the gRPC server should bypass check header.
skip-check-header = {{ .GRPC.SkipCheckHeader }}

###############################################################################
### State Sync Configuration ###
###############################################################################
Expand Down
6 changes: 4 additions & 2 deletions server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (

// NewGRPCServer returns a correctly configured and initialized gRPC server.
// Note, the caller is responsible for starting the server. See StartGRPCServer.
func NewGRPCServer(clientCtx client.Context, app interface{ RegisterGRPCServer(gogogrpcserver.Server) }, cfg config.GRPCConfig) (*grpc.Server, error) {
func NewGRPCServer(clientCtx client.Context, app interface {
RegisterGRPCServerWithSkipCheckHeader(gogogrpcserver.Server, bool)
}, cfg config.GRPCConfig) (*grpc.Server, error) {
maxSendMsgSize := cfg.MaxSendMsgSize
if maxSendMsgSize == 0 {
maxSendMsgSize = config.DefaultGRPCMaxSendMsgSize
Expand All @@ -37,7 +39,7 @@ func NewGRPCServer(clientCtx client.Context, app interface{ RegisterGRPCServer(g
grpc.MaxRecvMsgSize(maxRecvMsgSize),
)

app.RegisterGRPCServer(grpcSrv)
app.RegisterGRPCServerWithSkipCheckHeader(grpcSrv, cfg.SkipCheckHeader)

// Reflection allows consumers to build dynamic clients that can write to any
// Cosmos SDK application without relying on application packages at compile
Expand Down
8 changes: 5 additions & 3 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ const (

// gRPC-related flags

flagGRPCOnly = "grpc-only"
flagGRPCEnable = "grpc.enable"
flagGRPCAddress = "grpc.address"
flagGRPCOnly = "grpc-only"
flagGRPCEnable = "grpc.enable"
flagGRPCAddress = "grpc.address"
flagGRPCSkipCheckHeader = "grpc.skip-check-header"

// mempool flags

Expand Down Expand Up @@ -1028,6 +1029,7 @@ func addStartNodeFlags[T types.Application](cmd *cobra.Command, opts StartCmdOpt
cmd.Flags().Bool(flagGRPCOnly, false, "Start the node in gRPC query only mode (no CometBFT process is started)")
cmd.Flags().Bool(flagGRPCEnable, true, "Define if the gRPC server should be enabled")
cmd.Flags().String(flagGRPCAddress, serverconfig.DefaultGRPCAddress, "the gRPC server address to listen on")
cmd.Flags().Bool(flagGRPCSkipCheckHeader, false, "Define if the gRPC server should bypass check header")
cmd.Flags().Uint64(FlagStateSyncSnapshotInterval, 0, "State sync snapshot interval")
cmd.Flags().Uint32(FlagStateSyncSnapshotKeepRecent, 2, "State sync snapshot to keep")
cmd.Flags().Bool(FlagDisableIAVLFastNode, false, "Disable fast node for IAVL tree")
Expand Down
6 changes: 3 additions & 3 deletions server/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ type (

RegisterAPIRoutes(*api.Server, config.APIConfig)

// RegisterGRPCServer registers gRPC services directly with the gRPC
// server.
RegisterGRPCServer(grpc.Server)
// RegisterGRPCServerWithSkipCheckHeader registers gRPC services directly with the gRPC
// server and bypass check header flag.
RegisterGRPCServerWithSkipCheckHeader(grpc.Server, bool)

// RegisterTxService registers the gRPC Query service for tx (such as tx
// simulation, fetching txs by hash...).
Expand Down
3 changes: 3 additions & 0 deletions server/v2/api/grpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Config 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" toml:"max-send-msg-size" comment:"MaxSendMsgSize defines the max message size in bytes the server can send.\nThe default value is math.MaxInt32."`

// SkipCheckHeader defines if the gRPC server should bypass check header.
SkipCheckHeader bool `mapstructure:"skip-check-header" toml:"skip-check-header" comment:"SkipCheckHeader defines if the gRPC server should bypass check header."`
}

// CfgOption is a function that allows to overwrite the default server configuration.
Expand Down
2 changes: 2 additions & 0 deletions server/v2/testdata/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ max-recv-msg-size = 10485760
# MaxSendMsgSize defines the max message size in bytes the server can send.
# The default value is math.MaxInt32.
max-send-msg-size = 2147483647
# SkipCheckHeader defines if the gRPC server should bypass check header.
skip-check-header = false

[mock-server-1]
# Mock field
Expand Down
2 changes: 2 additions & 0 deletions tools/confix/data/v2-app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ max-recv-msg-size = 10485760
# MaxSendMsgSize defines the max message size in bytes the server can send.
# The default value is math.MaxInt32.
max-send-msg-size = 2147483647
# SkipCheckHeader defines if the gRPC server should bypass check header.
skip-check-header = false

[grpc-gateway]
# Enable defines if the gRPC-gateway should be enabled.
Expand Down

0 comments on commit d0a5250

Please sign in to comment.