From d67e92a2246457bd352639766a9a70e2341672f2 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 19 Jan 2023 20:15:35 +0100 Subject: [PATCH] fix: improve rpc message when app is at height 0 (#14692) --- CHANGELOG.md | 1 + baseapp/abci.go | 10 +++++++--- baseapp/abci_test.go | 17 ++++++++++++----- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e9afc75a238..a1c5819012b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/baseapp/abci.go b/baseapp/abci.go index 71e1582ee419..d6e5e7233d99 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -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( @@ -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 } diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index bd81ea41e1af..b775dc1daf37 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -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