Skip to content

Commit 30bd414

Browse files
fix: improve rpc message when app is at height 0 (backport #14692) (#14703)
Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent aa76274 commit 30bd414

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
3939

4040
## Improvements
4141

42+
* [#14692](https://github.com/cosmos/cosmos-sdk/pull/14692) Improve RPC queries error message when app is at height 0.
4243
* [#14017](https://github.com/cosmos/cosmos-sdk/pull/14017) Simplify ADR-028 and `address.Module`.
4344
* This updates the [ADR-028](https://docs.cosmos.network/main/architecture/adr-028-public-key-addresses) and enhance the `address.Module` API to support module addresses and sub-module addresses in a backward compatible way.
4445
* (snapshots) [#14608](https://github.com/cosmos/cosmos-sdk/pull/14608/) Deprecate unused structs `SnapshotKVItem` and `SnapshotSchema`.

baseapp/abci.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,10 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, e
721721
}
722722

723723
lastBlockHeight := qms.LatestVersion()
724+
if lastBlockHeight == 0 {
725+
return sdk.Context{}, sdkerrors.Wrapf(sdkerrors.ErrInvalidHeight, "%s is not ready; please wait for first block", app.Name())
726+
}
727+
724728
if height > lastBlockHeight {
725729
return sdk.Context{},
726730
sdkerrors.Wrap(
@@ -752,9 +756,9 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, e
752756
}
753757

754758
// branch the commit-multistore for safety
755-
ctx := sdk.NewContext(
756-
cacheMS, app.checkState.ctx.BlockHeader(), true, app.logger,
757-
).WithMinGasPrices(app.minGasPrices).WithBlockHeight(height)
759+
ctx := sdk.NewContext(cacheMS, app.checkState.ctx.BlockHeader(), true, app.logger).
760+
WithMinGasPrices(app.minGasPrices).
761+
WithBlockHeight(height)
758762

759763
return ctx, nil
760764
}

baseapp/abci_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,27 @@ func TestABCI_GRPCQuery(t *testing.T) {
179179
ConsensusParams: &tmproto.ConsensusParams{},
180180
})
181181

182-
header := tmproto.Header{Height: suite.baseApp.LastBlockHeight() + 1}
183-
suite.baseApp.BeginBlock(abci.RequestBeginBlock{Header: header})
184-
suite.baseApp.Commit()
185-
186182
req := testdata.SayHelloRequest{Name: "foo"}
187183
reqBz, err := req.Marshal()
188184
require.NoError(t, err)
189185

186+
resQuery := suite.baseApp.Query(abci.RequestQuery{
187+
Data: reqBz,
188+
Path: "/testdata.Query/SayHello",
189+
})
190+
require.Equal(t, sdkerrors.ErrInvalidHeight.ABCICode(), resQuery.Code, resQuery)
191+
require.Contains(t, resQuery.Log, "TestABCI_GRPCQuery is not ready; please wait for first block")
192+
193+
header := tmproto.Header{Height: suite.baseApp.LastBlockHeight() + 1}
194+
suite.baseApp.BeginBlock(abci.RequestBeginBlock{Header: header})
195+
suite.baseApp.Commit()
196+
190197
reqQuery := abci.RequestQuery{
191198
Data: reqBz,
192199
Path: "/testdata.Query/SayHello",
193200
}
194201

195-
resQuery := suite.baseApp.Query(reqQuery)
202+
resQuery = suite.baseApp.Query(reqQuery)
196203
require.Equal(t, abci.CodeTypeOK, resQuery.Code, resQuery)
197204

198205
var res testdata.SayHelloResponse

0 commit comments

Comments
 (0)