Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (server) [#25632](https://github.com/cosmos/cosmos-sdk/pull/25632) Add missing call to close the app on shutdown.
* (server) [#25740](https://github.com/cosmos/cosmos-sdk/pull/25740) Add variadic `grpc.DialOption` parameter to `StartGrpcServer` for custom gRPC client connection options.
* (blockstm) [#25765](https://github.com/cosmos/cosmos-sdk/pull/25765) Minor code readability improvement in block-stm.
* (server/config) [#25807](https://github.com/cosmos/cosmos-sdk/pull/25807) fix(server): reject overlapping historical gRPC block ranges.

### Bug Fixes

Expand Down
12 changes: 12 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,25 @@ func GetConfig(v *viper.Viper) (Config, error) {
return Config{}, fmt.Errorf("invalid block range [%d, %d] for address %s: start block must be <= end block",
blockRange[0], blockRange[1], address)
}
for existingRange, existingAddr := range historicalGRPCAddressBlockRange {
if rangesOverlap(existingRange, blockRange) {
return Config{}, fmt.Errorf(
"historical gRPC block range [%d, %d] for address %s overlaps with existing range [%d, %d] for address %s",
blockRange[0], blockRange[1], address, existingRange[0], existingRange[1], existingAddr,
)
}
}
historicalGRPCAddressBlockRange[blockRange] = address
}
conf.GRPC.HistoricalGRPCAddressBlockRange = historicalGRPCAddressBlockRange
}
return *conf, nil
}

func rangesOverlap(a, b BlockRange) bool {
return a[1] >= b[0] && a[0] <= b[1]
}

// ValidateBasic returns an error if min-gas-prices field is empty in BaseConfig. Otherwise, it returns nil.
func (c Config) ValidateBasic() error {
if c.MinGasPrices == "" {
Expand Down
22 changes: 22 additions & 0 deletions server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ func TestGetConfig_HistoricalGRPCAddressBlockRange(t *testing.T) {
require.Equal(t, "localhost:9091", address)
},
},
{
name: "overlapping ranges",
setupViper: func(v *viper.Viper) {
v.Set("grpc.historical-grpc-address-block-range", `{"localhost:9091": [0, 1000], "localhost:9092": [900, 1500]}`)
},
expectError: true,
errorMsg: "overlaps with existing range",
},
{
name: "invalid array length (too few elements)",
setupViper: func(v *viper.Viper) {
Expand Down Expand Up @@ -502,3 +510,17 @@ func TestConfigTemplate_HistoricalGRPCAddressBlockRange(t *testing.T) {
})
}
}

func Test_rangesOverlap(t *testing.T) {
tests := []struct {
a, b BlockRange
expect bool
}{
{BlockRange{0, 10}, BlockRange{11, 20}, false},
{BlockRange{0, 10}, BlockRange{5, 15}, true},
{BlockRange{0, 10}, BlockRange{10, 20}, true},
}
for _, tt := range tests {
require.Equal(t, tt.expect, rangesOverlap(tt.a, tt.b))
}
}
Loading