From 7330f38bdcd5a32f66e891461fd5ccbd95018710 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 17 Sep 2021 00:51:53 +0200 Subject: [PATCH] fix!: update ABCI query to use request height (backport #9879) (#10185) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix!: update ABCI query to use request height (#9879) (cherry picked from commit c04ab50fd0dc74f704136f1a8e3c0e6c594e53c5) # Conflicts: # CHANGELOG.md * Update CHANGELOG.md Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> Co-authored-by: Robert Zaremba --- CHANGELOG.md | 4 +++ client/grpc_query.go | 5 ++-- client/query.go | 14 ++++++++-- client/query_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 client/query_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 5896a5e86821..15e6e2dcb69f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/genutil) [#10104](https://github.com/cosmos/cosmos-sdk/pull/10104) Ensure the `init` command reads the `--home` flag value correctly. * [\#10061](https://github.com/cosmos/cosmos-sdk/pull/10061) Ensure that `LegacyAminoPubKey` struct correctly unmarshals from JSON +### Client Breaking Changes + +* [\#9879](https://github.com/cosmos/cosmos-sdk/pull/9879) Modify ABCI Queries to use `abci.QueryRequest` Height field if it is non-zero, otherwise continue using context height. + ## [v0.44.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.44.0) - 2021-09-01 ### Features diff --git a/client/grpc_query.go b/client/grpc_query.go index 011523944c54..cbaba73caa2a 100644 --- a/client/grpc_query.go +++ b/client/grpc_query.go @@ -112,8 +112,9 @@ func RunGRPCQuery(ctx Context, grpcCtx gocontext.Context, method string, req int } abciReq := abci.RequestQuery{ - Path: method, - Data: reqBz, + Path: method, + Data: reqBz, + Height: ctx.Height, } abciRes, err := ctx.QueryABCI(abciReq) diff --git a/client/query.go b/client/query.go index e79779ce571b..be603dfbf6e2 100644 --- a/client/query.go +++ b/client/query.go @@ -50,7 +50,9 @@ func (ctx Context) QueryStore(key tmbytes.HexBytes, storeName string) ([]byte, i } // QueryABCI performs a query to a Tendermint node with the provide RequestQuery. -// It returns the ResultQuery obtained from the query. +// It returns the ResultQuery obtained from the query. The height used to perform +// the query is the RequestQuery Height if it is non-zero, otherwise the context +// height is used. func (ctx Context) QueryABCI(req abci.RequestQuery) (abci.ResponseQuery, error) { return ctx.queryABCI(req) } @@ -76,8 +78,16 @@ func (ctx Context) queryABCI(req abci.RequestQuery) (abci.ResponseQuery, error) return abci.ResponseQuery{}, err } + var queryHeight int64 + if req.Height != 0 { + queryHeight = req.Height + } else { + // fallback on the context height + queryHeight = ctx.Height + } + opts := rpcclient.ABCIQueryOptions{ - Height: ctx.Height, + Height: queryHeight, Prove: req.Prove, } diff --git a/client/query_test.go b/client/query_test.go new file mode 100644 index 000000000000..14cc25ba9d97 --- /dev/null +++ b/client/query_test.go @@ -0,0 +1,63 @@ +// +build norace + +package client_test + +import ( + "fmt" + + abci "github.com/tendermint/tendermint/abci/types" + + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" +) + +func (s *IntegrationTestSuite) TestQueryABCIHeight() { + + testCases := []struct { + name string + reqHeight int64 + ctxHeight int64 + expHeight int64 + }{ + { + name: "non zero request height", + reqHeight: 3, + ctxHeight: 1, // query at height 1 or 2 would cause an error + expHeight: 3, + }, + { + name: "empty request height - use context height", + reqHeight: 0, + ctxHeight: 3, + expHeight: 3, + }, + { + name: "empty request height and context height - use latest height", + reqHeight: 0, + ctxHeight: 0, + expHeight: 4, + }, + } + + for _, tc := range testCases { + s.Run(tc.name, func() { + s.network.WaitForHeight(tc.expHeight) + + val := s.network.Validators[0] + + clientCtx := val.ClientCtx + clientCtx = clientCtx.WithHeight(tc.ctxHeight) + + req := abci.RequestQuery{ + Path: fmt.Sprintf("store/%s/key", banktypes.StoreKey), + Height: tc.reqHeight, + Data: banktypes.CreateAccountBalancesPrefix(val.Address), + Prove: true, + } + + res, err := clientCtx.QueryABCI(req) + s.Require().NoError(err) + + s.Require().Equal(tc.expHeight, res.Height) + }) + } +}