Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve rpc message when app is at height 0 (backport #14692) #14703

Merged
merged 2 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Improvements

* [#14692](https://github.com/cosmos/cosmos-sdk/pull/14692) Improve RPC queries error message when app is at height 0.
* [#14017](https://github.com/cosmos/cosmos-sdk/pull/14017) Simplify ADR-028 and `address.Module`.
* 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.
* (snapshots) [#14608](https://github.com/cosmos/cosmos-sdk/pull/14608/) Deprecate unused structs `SnapshotKVItem` and `SnapshotSchema`.
Expand Down
10 changes: 7 additions & 3 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,10 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, e
}

lastBlockHeight := qms.LatestVersion()
if lastBlockHeight == 0 {
return sdk.Context{}, sdkerrors.Wrapf(sdkerrors.ErrInvalidHeight, "%s is not ready; please wait for first block", app.Name())
}

if height > lastBlockHeight {
return sdk.Context{},
sdkerrors.Wrap(
Expand Down Expand Up @@ -752,9 +756,9 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, e
}

// branch the commit-multistore for safety
ctx := sdk.NewContext(
cacheMS, app.checkState.ctx.BlockHeader(), true, app.logger,
).WithMinGasPrices(app.minGasPrices).WithBlockHeight(height)
ctx := sdk.NewContext(cacheMS, app.checkState.ctx.BlockHeader(), true, app.logger).
WithMinGasPrices(app.minGasPrices).
WithBlockHeight(height)

return ctx, nil
}
Expand Down
17 changes: 12 additions & 5 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,27 @@ func TestABCI_GRPCQuery(t *testing.T) {
ConsensusParams: &tmproto.ConsensusParams{},
})

header := tmproto.Header{Height: suite.baseApp.LastBlockHeight() + 1}
suite.baseApp.BeginBlock(abci.RequestBeginBlock{Header: header})
suite.baseApp.Commit()

req := testdata.SayHelloRequest{Name: "foo"}
reqBz, err := req.Marshal()
require.NoError(t, err)

resQuery := suite.baseApp.Query(abci.RequestQuery{
Data: reqBz,
Path: "/testdata.Query/SayHello",
})
require.Equal(t, sdkerrors.ErrInvalidHeight.ABCICode(), resQuery.Code, resQuery)
require.Contains(t, resQuery.Log, "TestABCI_GRPCQuery is not ready; please wait for first block")

header := tmproto.Header{Height: suite.baseApp.LastBlockHeight() + 1}
suite.baseApp.BeginBlock(abci.RequestBeginBlock{Header: header})
suite.baseApp.Commit()

reqQuery := abci.RequestQuery{
Data: reqBz,
Path: "/testdata.Query/SayHello",
}

resQuery := suite.baseApp.Query(reqQuery)
resQuery = suite.baseApp.Query(reqQuery)
require.Equal(t, abci.CodeTypeOK, resQuery.Code, resQuery)

var res testdata.SayHelloResponse
Expand Down