Skip to content

Commit

Permalink
Show height on legacy REST endpoints (#7997)
Browse files Browse the repository at this point in the history
* Fix showing height in rest endpoints

* Fix query height

* Manually inject req.Height

* Small tweaks

* Use withHeight

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 24, 2020
1 parent 251a27d commit 7443513
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
10 changes: 5 additions & 5 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ func (app *BaseApp) snapshot(height int64) {
func (app *BaseApp) Query(req abci.RequestQuery) abci.ResponseQuery {
defer telemetry.MeasureSince(time.Now(), "abci", "query")

// when a client did not provide a query height, manually inject the latest
if req.Height == 0 {
req.Height = app.LastBlockHeight()
}

// handle gRPC routes first rather than calling splitPath because '/' characters
// are used as part of gRPC paths
if grpcHandler := app.grpcQueryRouter.Route(req.Path); grpcHandler != nil {
Expand Down Expand Up @@ -742,11 +747,6 @@ func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) abci.R

req.Path = "/" + strings.Join(path[1:], "/")

// when a client did not provide a query height, manually inject the latest
if req.Height == 0 {
req.Height = app.LastBlockHeight()
}

if req.Height <= 1 && req.Prove {
return sdkerrors.QueryResult(
sdkerrors.Wrap(
Expand Down
22 changes: 0 additions & 22 deletions client/errors.go

This file was deleted.

3 changes: 1 addition & 2 deletions client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec/legacy"
"github.com/cosmos/cosmos-sdk/types/rest"
)

Expand Down Expand Up @@ -68,7 +67,7 @@ func getBlock(clientCtx client.Context, height *int64) ([]byte, error) {
return nil, err
}

return legacy.Cdc.MarshalJSON(res)
return clientCtx.LegacyAmino.MarshalJSON(res)
}

// get the current blockchain height
Expand Down
45 changes: 35 additions & 10 deletions x/bank/client/rest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
s.cfg = cfg
s.network = network.New(s.T(), cfg)

_, err := s.network.WaitForHeight(1)
_, err := s.network.WaitForHeight(2)
s.Require().NoError(err)
}

Expand All @@ -43,14 +43,26 @@ func (s *IntegrationTestSuite) TestQueryBalancesRequestHandlerFn() {
baseURL := val.APIAddress

testCases := []struct {
name string
url string
respType fmt.Stringer
expected fmt.Stringer
name string
url string
expHeight int64
respType fmt.Stringer
expected fmt.Stringer
}{
{
"total account balance",
fmt.Sprintf("%s/bank/balances/%s", baseURL, val.Address),
-1,
&sdk.Coins{},
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Sub(s.cfg.BondedTokens)),
),
},
{
"total account balance with height",
fmt.Sprintf("%s/bank/balances/%s?height=1", baseURL, val.Address),
1,
&sdk.Coins{},
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
Expand All @@ -59,13 +71,15 @@ func (s *IntegrationTestSuite) TestQueryBalancesRequestHandlerFn() {
},
{
"total account balance of a specific denom",
fmt.Sprintf("%s/bank/balances/%s?height=1&denom=%s", baseURL, val.Address, s.cfg.BondDenom),
fmt.Sprintf("%s/bank/balances/%s?denom=%s", baseURL, val.Address, s.cfg.BondDenom),
-1,
&sdk.Coin{},
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Sub(s.cfg.BondedTokens)),
},
{
"total account balance of a bogus denom",
fmt.Sprintf("%s/bank/balances/%s?height=1&denom=foobar", baseURL, val.Address),
fmt.Sprintf("%s/bank/balances/%s?denom=foobar", baseURL, val.Address),
-1,
&sdk.Coin{},
sdk.NewCoin("foobar", sdk.ZeroInt()),
},
Expand All @@ -74,12 +88,23 @@ func (s *IntegrationTestSuite) TestQueryBalancesRequestHandlerFn() {
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
resp, err := rest.GetRequest(tc.url)
respJSON, err := rest.GetRequest(tc.url)
s.Require().NoError(err)

bz, err := rest.ParseResponseWithHeight(val.ClientCtx.LegacyAmino, resp)
var resp = rest.ResponseWithHeight{}
err = val.ClientCtx.LegacyAmino.UnmarshalJSON(respJSON, &resp)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(bz, tc.respType))

// Check height.
if tc.expHeight >= 0 {
s.Require().Equal(resp.Height, tc.expHeight)
} else {
// To avoid flakiness, just test that height is positive.
s.Require().Greater(resp.Height, int64(0))
}

// Check result.
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(resp.Result, tc.respType))
s.Require().Equal(tc.expected.String(), tc.respType.String())
})
}
Expand Down

0 comments on commit 7443513

Please sign in to comment.