Skip to content

Commit

Permalink
fix!: update ABCI query to use request height (backport #9879) (#10144)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Sep 15, 2021
1 parent 1af4f49 commit 16e6b08
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Unreleased

### 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.

### API Breaking Changes

* [\#10077](https://github.com/cosmos/cosmos-sdk/pull/10077) Remove telemetry on `GasKV` and `CacheKV` store Get/Set operations, significantly improving their performance.
Expand Down
5 changes: 3 additions & 2 deletions client/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -71,8 +73,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,
}

Expand Down
62 changes: 62 additions & 0 deletions client/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// +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: append(banktypes.BalancesPrefix, val.Address.Bytes()...),
Prove: true,
}

res, err := clientCtx.QueryABCI(req)
s.Require().NoError(err)

s.Require().Equal(tc.expHeight, res.Height)
})
}
}

0 comments on commit 16e6b08

Please sign in to comment.