Skip to content

Commit

Permalink
fix: improve rpc message when app is at height 0 (#14692)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Jan 19, 2023
1 parent 6e96378 commit d67e92a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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.
* [#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.
* [#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.
Expand Down
10 changes: 7 additions & 3 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,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 @@ -753,9 +757,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 @@ -181,20 +181,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

0 comments on commit d67e92a

Please sign in to comment.