feat: support for multi gRPC query clients serve with old binary (#25565)

Co-authored-by: Alex | Cosmos Labs <alex@cosmoslabs.io>
This commit is contained in:
mmsqe
2025-11-19 15:46:50 +00:00
committed by GitHub
co-authored by Alex | Cosmos Labs
parent 2667feb515
commit f4e2ce0ea4
10 changed files with 644 additions and 28 deletions
+17
View File
@@ -30,6 +30,7 @@ type Context struct {
FromAddress sdk.AccAddress
Client CometRPC
GRPCClient *grpc.ClientConn
GRPCConnProvider *GRPCConnProvider
ChainID string
Codec codec.Codec
InterfaceRegistry codectypes.InterfaceRegistry
@@ -155,6 +156,22 @@ func (ctx Context) WithGRPCClient(grpcClient *grpc.ClientConn) Context {
return ctx
}
// WithGRPCConnProvider returns a copy of the context with an updated GRPCConnProvider.
func (ctx Context) WithGRPCConnProvider(provider *GRPCConnProvider) Context {
ctx.GRPCConnProvider = provider
return ctx
}
// GetGRPCConn returns the appropriate gRPC connection for the given height.
// If GRPCConnProvider is set, it uses it to determine the connection.
// Otherwise, it falls back to the default GRPCClient.
func (ctx Context) GetGRPCConn(height int64) *grpc.ClientConn {
if ctx.GRPCConnProvider != nil {
return ctx.GRPCConnProvider.GetGRPCConn(height)
}
return ctx.GRPCClient
}
// WithUseLedger returns a copy of the context with an updated UseLedger flag.
func (ctx Context) WithUseLedger(useLedger bool) Context {
ctx.UseLedger = useLedger
+84 -13
View File
@@ -16,11 +16,56 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server/config"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/cosmos/cosmos-sdk/types/tx"
)
// GRPCConn provides a method to get the appropriate gRPC connection based on block height.
type GRPCConn interface {
GetGRPCConn(height int64) *grpc.ClientConn
}
// GRPCConnProvider manages gRPC connections with optional historical connections for historical queries.
type GRPCConnProvider struct {
// DefaultConn is the primary gRPC connection
DefaultConn *grpc.ClientConn
// HistoricalConns maps block ranges to historical gRPC connections for routing historical queries
HistoricalConns config.HistoricalGRPCConnections
}
// NewGRPCConnProvider creates a new GRPCConnProvider with the given connections.
func NewGRPCConnProvider(defaultConn *grpc.ClientConn, historicalConns config.HistoricalGRPCConnections) *GRPCConnProvider {
if historicalConns == nil {
historicalConns = make(config.HistoricalGRPCConnections)
}
return &GRPCConnProvider{
DefaultConn: defaultConn,
HistoricalConns: historicalConns,
}
}
// GetGRPCConn returns the appropriate gRPC connection based on the block height.
// For height <= 0 (latest block), it returns the default connection.
// For positive heights, it checks if a historical connection exists for that height range.
func (g *GRPCConnProvider) GetGRPCConn(height int64) *grpc.ClientConn {
// height = 0 means latest block, use the default connection
if height <= 0 {
return g.DefaultConn
}
// Check if there's a historical connection for this height
for blockRange, conn := range g.HistoricalConns {
if int64(blockRange[0]) <= height && int64(blockRange[1]) >= height {
return conn
}
}
// Default to the primary connection if no historical matches
return g.DefaultConn
}
var _ gogogrpc.ClientConn = Context{}
// fallBackCodec is used by Context in case Codec is not set.
@@ -28,6 +73,27 @@ var _ gogogrpc.ClientConn = Context{}
// interfaces in their types.
var fallBackCodec = codec.NewProtoCodec(types.NewInterfaceRegistry())
// GetHeightFromMetadata extracts the block height from gRPC metadata in the context.
// Returns 0 if no valid height is found.
func GetHeightFromMetadata(grpcCtx gocontext.Context) int64 {
md, ok := metadata.FromOutgoingContext(grpcCtx)
if !ok {
return 0
}
heights := md.Get(grpctypes.GRPCBlockHeightHeader)
if len(heights) == 0 {
return 0
}
height, err := strconv.ParseInt(heights[0], 10, 64)
if err != nil {
return 0
}
if height < 0 {
return 0
}
return height
}
// Invoke implements the grpc ClientConn.Invoke method
func (ctx Context) Invoke(grpcCtx gocontext.Context, method string, req, reply any, opts ...grpc.CallOption) (err error) {
// Two things can happen here:
@@ -58,7 +124,16 @@ func (ctx Context) Invoke(grpcCtx gocontext.Context, method string, req, reply a
if ctx.GRPCClient != nil {
// Case 2-1. Invoke grpc.
return ctx.GRPCClient.Invoke(grpcCtx, method, req, reply, opts...)
grpcConn := ctx.GRPCClient
if ctx.GRPCConnProvider != nil {
height := ctx.Height
if height <= 0 {
height = GetHeightFromMetadata(grpcCtx)
}
grpcConn = ctx.GRPCConnProvider.GetGRPCConn(height)
}
return grpcConn.Invoke(grpcCtx, method, req, reply, opts...)
}
// Case 2-2. Querying state via abci query.
@@ -68,18 +143,14 @@ func (ctx Context) Invoke(grpcCtx gocontext.Context, method string, req, reply a
}
// parse height header
md, _ := metadata.FromOutgoingContext(grpcCtx)
if heights := md.Get(grpctypes.GRPCBlockHeightHeader); len(heights) > 0 {
height, err := strconv.ParseInt(heights[0], 10, 64)
if err != nil {
return err
}
if height < 0 {
return errorsmod.Wrapf(
sdkerrors.ErrInvalidRequest,
"client.Context.Invoke: height (%d) from %q must be >= 0", height, grpctypes.GRPCBlockHeightHeader)
}
height := GetHeightFromMetadata(grpcCtx)
if height < 0 {
return errorsmod.Wrapf(
sdkerrors.ErrInvalidRequest,
"client.Context.Invoke: height (%d) from %q must be >= 0", height, grpctypes.GRPCBlockHeightHeader)
}
if height > 0 {
ctx = ctx.WithHeight(height)
}
@@ -104,7 +175,7 @@ func (ctx Context) Invoke(grpcCtx gocontext.Context, method string, req, reply a
// We then parse all the call options, if the call option is a
// HeaderCallOption, then we manually set the value of that header to the
// metadata.
md = metadata.Pairs(grpctypes.GRPCBlockHeightHeader, strconv.FormatInt(res.Height, 10))
md := metadata.Pairs(grpctypes.GRPCBlockHeightHeader, strconv.FormatInt(res.Height, 10))
for _, callOpt := range opts {
header, ok := callOpt.(grpc.HeaderCallOption)
if !ok {
+194
View File
@@ -7,8 +7,10 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
cmtjson "github.com/cometbft/cometbft/libs/json"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"cosmossdk.io/depinject"
@@ -16,13 +18,16 @@ import (
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/cosmos/cosmos-sdk/x/auth/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
@@ -136,3 +141,192 @@ func (s *IntegrationTestSuite) TestGRPCQuery() {
func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
func (s *IntegrationTestSuite) TestGetGRPCConnWithContext() {
defaultConn, err := grpc.NewClient("localhost:9090",
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
s.Require().NoError(err)
defer defaultConn.Close()
historicalConn, err := grpc.NewClient("localhost:9091",
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
s.Require().NoError(err)
defer historicalConn.Close()
historicalConns := config.HistoricalGRPCConnections{
config.BlockRange{100, 500}: historicalConn,
}
provider := client.NewGRPCConnProvider(defaultConn, historicalConns)
testCases := []struct {
name string
height int64
setupCtx func() client.Context
expectedConn *grpc.ClientConn
}{
{
name: "context with GRPCConnProvider and historical height",
height: 300,
setupCtx: func() client.Context {
return client.Context{}.
WithCodec(s.cdc).
WithGRPCClient(defaultConn).
WithGRPCConnProvider(provider).
WithHeight(300)
},
expectedConn: historicalConn,
},
{
name: "context with GRPCConnProvider and latest height",
height: 0,
setupCtx: func() client.Context {
return client.Context{}.
WithCodec(s.cdc).
WithGRPCClient(defaultConn).
WithGRPCConnProvider(provider).
WithHeight(0)
},
expectedConn: defaultConn,
},
{
name: "context without GRPCConnProvider",
height: 300,
setupCtx: func() client.Context {
return client.Context{}.
WithCodec(s.cdc).
WithGRPCClient(defaultConn).
WithHeight(300)
},
expectedConn: defaultConn,
},
{
name: "context with nil historical connections map",
height: 100,
setupCtx: func() client.Context {
nilProvider := client.NewGRPCConnProvider(defaultConn, nil)
return client.Context{}.
WithCodec(s.cdc).
WithGRPCClient(defaultConn).
WithGRPCConnProvider(nilProvider).
WithHeight(100)
},
expectedConn: defaultConn,
},
{
name: "context with empty historical connections map",
height: 100,
setupCtx: func() client.Context {
emptyProvider := client.NewGRPCConnProvider(defaultConn, config.HistoricalGRPCConnections{})
return client.Context{}.
WithCodec(s.cdc).
WithGRPCClient(defaultConn).
WithGRPCConnProvider(emptyProvider).
WithHeight(100)
},
expectedConn: defaultConn,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
ctx := tc.setupCtx()
var actualConn *grpc.ClientConn
if ctx.GRPCConnProvider != nil {
actualConn = ctx.GRPCConnProvider.GetGRPCConn(ctx.Height)
} else {
actualConn = ctx.GRPCClient
}
s.Require().Equal(tc.expectedConn, actualConn)
})
}
}
func TestGetHeightFromMetadata(t *testing.T) {
tests := []struct {
name string
setupContext func() context.Context
expectedHeight int64
}{
{
name: "valid height in metadata",
setupContext: func() context.Context {
md := metadata.Pairs(grpctypes.GRPCBlockHeightHeader, "12345")
return metadata.NewOutgoingContext(context.Background(), md)
},
expectedHeight: 12345,
},
{
name: "zero height in metadata",
setupContext: func() context.Context {
md := metadata.Pairs(grpctypes.GRPCBlockHeightHeader, "0")
return metadata.NewOutgoingContext(context.Background(), md)
},
expectedHeight: 0,
},
{
name: "negative height returns zero",
setupContext: func() context.Context {
md := metadata.Pairs(grpctypes.GRPCBlockHeightHeader, "-100")
return metadata.NewOutgoingContext(context.Background(), md)
},
expectedHeight: 0,
},
{
name: "no metadata returns zero",
setupContext: context.Background,
expectedHeight: 0,
},
{
name: "empty height header returns zero",
setupContext: func() context.Context {
md := metadata.New(map[string]string{})
return metadata.NewOutgoingContext(context.Background(), md)
},
expectedHeight: 0,
},
{
name: "invalid height string returns zero",
setupContext: func() context.Context {
md := metadata.Pairs(grpctypes.GRPCBlockHeightHeader, "not-a-number")
return metadata.NewOutgoingContext(context.Background(), md)
},
expectedHeight: 0,
},
{
name: "multiple height values uses first",
setupContext: func() context.Context {
md := metadata.Pairs(
grpctypes.GRPCBlockHeightHeader, "100",
grpctypes.GRPCBlockHeightHeader, "200",
)
return metadata.NewOutgoingContext(context.Background(), md)
},
expectedHeight: 100,
},
{
name: "very large height",
setupContext: func() context.Context {
md := metadata.Pairs(grpctypes.GRPCBlockHeightHeader, "9223372036854775807") // max int64
return metadata.NewOutgoingContext(context.Background(), md)
},
expectedHeight: 9223372036854775807,
},
{
name: "height exceeding int64 returns zero",
setupContext: func() context.Context {
md := metadata.Pairs(grpctypes.GRPCBlockHeightHeader, "9223372036854775808") // max int64 + 1
return metadata.NewOutgoingContext(context.Background(), md)
},
expectedHeight: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := tt.setupContext()
height := client.GetHeightFromMetadata(ctx)
require.Equal(t, tt.expectedHeight, height)
})
}
}