Skip to content

Commit

Permalink
Merge pull request cosmos#10 from classic-terra/blockheight-middleware
Browse files Browse the repository at this point in the history
feat: blockHeightMiddleware
  • Loading branch information
nghuyenthevinh2000 authored Aug 29, 2023
2 parents c4e15be + 2e15622 commit 93ca93d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
30 changes: 28 additions & 2 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -84,6 +85,31 @@ func New(clientCtx client.Context, logger log.Logger) *Server {
}
}

// blockHeightMiddleware parses height query parameter and sets GRPCBlockHeightHeader
func blockHeightMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
heightStr := r.FormValue("height")
if heightStr != "" {
height, err := strconv.ParseInt(heightStr, 10, 64)
if err != nil {
writeErrorResponse(w, http.StatusBadRequest, "syntax error")
return
}

if height < 0 {
writeErrorResponse(w, http.StatusBadRequest, "height must be equal or greater than zero")
return
}

if height > 0 {
r.Header.Set(grpctypes.GRPCBlockHeightHeader, heightStr)
}
}

next.ServeHTTP(w, r)
})
}

// Start starts the API server. Internally, the API server leverages Tendermint's
// JSON RPC server. Configuration options are provided via config.APIConfig
// and are delegated to the Tendermint JSON RPC server. The process is
Expand All @@ -105,7 +131,7 @@ func (s *Server) Start(cfg config.Config) error {

s.registerGRPCGatewayRoutes()
s.listener = listener
var h http.Handler = s.Router
h := blockHeightMiddleware(s.Router)

s.mtx.Unlock()

Expand All @@ -115,7 +141,7 @@ func (s *Server) Start(cfg config.Config) error {
}

s.logger.Info("starting API server...")
return tmrpcserver.Serve(s.listener, s.Router, s.logger, tmCfg)
return tmrpcserver.Serve(s.listener, h, s.logger, tmCfg)
}

// Close closes the API server.
Expand Down
22 changes: 11 additions & 11 deletions x/bank/client/testutil/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ func (s *IntegrationTestSuite) TestTotalSupplyGRPCHandler() {
Amount: sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(20))),
},
},
{
"Query params shouldn't be considered as height",
fmt.Sprintf("%s/cosmos/bank/v1beta1/supply/by_denom?denom=%s&height=2", baseURL, s.cfg.BondDenom),
map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
},
&types.QuerySupplyOfResponse{},
&types.QuerySupplyOfResponse{
Amount: sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
},
},
// {
// "Query params shouldn't be considered as height",
// fmt.Sprintf("%s/cosmos/bank/v1beta1/supply/by_denom?denom=%s&height=2", baseURL, s.cfg.BondDenom),
// map[string]string{
// grpctypes.GRPCBlockHeightHeader: "1",
// },
// &types.QuerySupplyOfResponse{},
// &types.QuerySupplyOfResponse{
// Amount: sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
// },
// },
{
"GRPC total supply of a bogus denom",
fmt.Sprintf("%s/cosmos/bank/v1beta1/supply/by_denom?denom=foobar", baseURL),
Expand Down

0 comments on commit 93ca93d

Please sign in to comment.