11package config
22
33import (
4+ "encoding/json"
45 "fmt"
56 "math"
67
78 "github.com/spf13/viper"
9+ "google.golang.org/grpc"
810
911 pruningtypes "cosmossdk.io/store/pruning/types"
1012
11- _ "github.com/cosmos/cosmos-sdk/telemetry"
13+ "github.com/cosmos/cosmos-sdk/telemetry"
1214 sdk "github.com/cosmos/cosmos-sdk/types"
1315 sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
1416)
@@ -124,6 +126,21 @@ type APIConfig struct {
124126 // Ref: https://github.com/cosmos/cosmos-sdk/issues/6420
125127}
126128
129+ // BlockRange represents a range of block heights as [start_block, end_block] (inclusive).
130+ // It is used to map gRPC historical connections to specific block height ranges for routing
131+ // historical queries to appropriate archive nodes.
132+ //
133+ // Example:
134+ // - [0, 1000] represents blocks from genesis (0) through block 1000
135+ // - [1001, 2000] represents blocks from 1001 through 2000
136+ //
137+ // Both start and end blocks must be non-negative, and start must be less than or equal to end.
138+ type BlockRange [2 ]int
139+
140+ // HistoricalGRPCConnections is a map of block ranges to gRPC client connections
141+ // used for routing requests to different backend nodes based on block height.
142+ type HistoricalGRPCConnections map [BlockRange ]* grpc.ClientConn
143+
127144// GRPCConfig defines configuration for the gRPC server.
128145type GRPCConfig struct {
129146 // Enable defines if the gRPC server should be enabled.
@@ -142,6 +159,9 @@ type GRPCConfig struct {
142159
143160 // SkipCheckHeader defines if the gRPC server should bypass header checking.
144161 SkipCheckHeader bool `mapstructure:"skip-check-header"`
162+
163+ // HistoricalGRPCAddressBlockRange maps block ranges to gRPC addresses for routing historical queries.
164+ HistoricalGRPCAddressBlockRange map [BlockRange ]string `mapstructure:"-"`
145165}
146166
147167// GRPCWebConfig defines configuration for the gRPC-web server.
@@ -190,12 +210,13 @@ type Config struct {
190210 BaseConfig `mapstructure:",squash"`
191211
192212 // Telemetry defines the application telemetry configuration
193- API APIConfig `mapstructure:"api"`
194- GRPC GRPCConfig `mapstructure:"grpc"`
195- GRPCWeb GRPCWebConfig `mapstructure:"grpc-web"`
196- StateSync StateSyncConfig `mapstructure:"state-sync"`
197- Streaming StreamingConfig `mapstructure:"streaming"`
198- Mempool MempoolConfig `mapstructure:"mempool"`
213+ Telemetry telemetry.Config `mapstructure:"telemetry"`
214+ API APIConfig `mapstructure:"api"`
215+ GRPC GRPCConfig `mapstructure:"grpc"`
216+ GRPCWeb GRPCWebConfig `mapstructure:"grpc-web"`
217+ StateSync StateSyncConfig `mapstructure:"state-sync"`
218+ Streaming StreamingConfig `mapstructure:"streaming"`
219+ Mempool MempoolConfig `mapstructure:"mempool"`
199220}
200221
201222// SetMinGasPrices sets the validator's minimum gas prices.
@@ -233,6 +254,10 @@ func DefaultConfig() *Config {
233254 IAVLDisableFastNode : false ,
234255 AppDBBackend : "" ,
235256 },
257+ Telemetry : telemetry.Config {
258+ Enabled : false ,
259+ GlobalLabels : [][]string {},
260+ },
236261 API : APIConfig {
237262 Enable : false ,
238263 Swagger : false ,
@@ -273,6 +298,26 @@ func GetConfig(v *viper.Viper) (Config, error) {
273298 if err := v .Unmarshal (conf ); err != nil {
274299 return Config {}, fmt .Errorf ("error extracting app config: %w" , err )
275300 }
301+ raw := v .GetString ("grpc.historical-grpc-address-block-range" )
302+ if len (raw ) > 0 {
303+ data := make (map [string ]BlockRange )
304+ if err := json .Unmarshal ([]byte (raw ), & data ); err != nil {
305+ return Config {}, fmt .Errorf ("failed to parse historical-grpc-address-block-range as JSON: %w (value: %s)" , err , raw )
306+ }
307+ historicalGRPCAddressBlockRange := make (map [BlockRange ]string , len (data ))
308+ for address , blockRange := range data {
309+ if blockRange [0 ] < 0 || blockRange [1 ] < 0 {
310+ return Config {}, fmt .Errorf ("invalid block range [%d, %d] for address %s: block numbers cannot be negative" ,
311+ blockRange [0 ], blockRange [1 ], address )
312+ }
313+ if blockRange [0 ] > blockRange [1 ] {
314+ return Config {}, fmt .Errorf ("invalid block range [%d, %d] for address %s: start block must be <= end block" ,
315+ blockRange [0 ], blockRange [1 ], address )
316+ }
317+ historicalGRPCAddressBlockRange [blockRange ] = address
318+ }
319+ conf .GRPC .HistoricalGRPCAddressBlockRange = historicalGRPCAddressBlockRange
320+ }
276321 return * conf , nil
277322}
278323
0 commit comments