Skip to content

Commit d67e92a

Browse files
authored
fix: improve rpc message when app is at height 0 (#14692)
1 parent 6e96378 commit d67e92a

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
@@ -72,6 +72,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
7272

7373
### Improvements
7474

75+
* [#14692](https://github.com/cosmos/cosmos-sdk/pull/14692) Improve RPC queries error message when app is at height 0.
7576
* [#14609](https://github.com/cosmos/cosmos-sdk/pull/14609) Add RetryForBlocks method to use in tests that require waiting for a transaction to be included in a block.
7677
* [#14017](https://github.com/cosmos/cosmos-sdk/pull/14017) Simplify ADR-028 and `address.Module`.
7778
* 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.

baseapp/abci.go

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

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

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

760764
return ctx, nil
761765
}

baseapp/abci_test.go

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

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

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

197-
resQuery := suite.baseApp.Query(reqQuery)
204+
resQuery = suite.baseApp.Query(reqQuery)
198205
require.Equal(t, abci.CodeTypeOK, resQuery.Code, resQuery)
199206

200207
var res testdata.SayHelloResponse

0 commit comments

Comments
 (0)